inventory ui transition wip

This commit is contained in:
minjaesong
2020-08-30 16:09:42 +09:00
parent dd1989320d
commit 7a58cf9edb
30 changed files with 415 additions and 217 deletions

View File

@@ -53,6 +53,13 @@ abstract class UICanvas(
get() = handler.posY
set(value) { handler.posY = value }
inline var initialX: Int
get() = handler.initialX
set(value) { handler.initialX = value }
inline var initialY: Int
get() = handler.initialY
set(value) { handler.initialY = value }
/**
* Usage: (in StateInGame:) uiHandlerField.ui.handler = uiHandlerField
*/
@@ -103,9 +110,12 @@ abstract class UICanvas(
}
/** Override this for the actual update. Note that you must update uiItems by yourself. */
/** **DO NOT CALL THIS FUNCTION FOR THE ACTUAL UPDATING OF THE UI — USE update() INSTEAD**
*
* Override this for the actual update. Note that you must update uiItems by yourself. */
abstract fun updateUI(delta: Float)
/**
/** **DO NOT CALL THIS FUNCTION FOR THE ACTUAL RENDERING OF THE UI — USE render() INSTEAD**
*
* Override this for the actual render. Note that you must render uiItems by yourself.
*
* Under normal circumstances, draws are automatically translated as per the handler's X/Y position.
@@ -138,7 +148,7 @@ abstract class UICanvas(
abstract override fun dispose()
fun addItem(uiItem: UIItem) {
fun addUIitem(uiItem: UIItem) {
uiItems.add(uiItem)
}

View File

@@ -32,6 +32,9 @@ class UIHandler(//var UI: UICanvas,
var posX: Int = 0
var posY: Int = 0
var initialX = posX
var initialY = posY
private var alwaysVisible = false
var isOpening = false

View File

@@ -66,7 +66,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
* oldPosX = posX
* ```
*/
protected abstract var oldPosX: Int
protected var oldPosX: Int = initialX
/** This variable is NOT updated on its own.
* ```
* val posYDelta = posY - oldPosY
@@ -75,7 +75,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
* oldPosY = posY
* ```
*/
protected abstract var oldPosY: Int
protected var oldPosY: Int = initialY
/** Position of mouse relative to this item */
protected val relativeMouseX: Int

View File

@@ -36,9 +36,6 @@ class UIItemConfigKeycap(
override val width = capTex.tileW * keySize
override val height = capTex.tileH
override var oldPosX = posX
override var oldPosY = posY
override fun update(delta: Float) {
super.update(delta)
}

View File

@@ -0,0 +1,41 @@
package net.torvald.terrarum.ui
import kotlin.math.roundToInt
/**
* @param width size of the canvas where transition occurs
* @param height size of the canvas where transition occurs
*/
class UIItemHorizontalFadeSlide(
parent: UICanvas,
initialX: Int,
initialY: Int,
width: Int,
height: Int,
//transitionLength: Float,
currentPosition: Float,
vararg uis: UICanvas
) : UIItemTransitionContainer(parent, initialX, initialY, width, height, 0.212f, currentPosition, uis) {
fun getOffX(index: Int) = ((currentPosition - index) * width).roundToInt()
fun getOpacity(index: Int) = (currentPosition - index).coerceIn(0f, 1f)
init {
// re-position the uis according to the initial choice of currentPosition
uis.forEachIndexed { index, it ->
it.posX = posX + getOffX(index)
it.initialX = posX + getOffX(index)
it.posY = posY
it.initialY = posY
it.opacity = getOpacity(index)
}
}
override fun onTransition(currentPosition: Float, uis: Array<out UICanvas>) {
uis.forEachIndexed { index, it ->
it.posX = it.initialX + getOffX(index)
it.posY = posY
it.opacity = getOpacity(index)
}
}
}

View File

@@ -41,10 +41,6 @@ open class UIItemImageButton(
var highlightable: Boolean
) : UIItem(parent, initialX, initialY) {
// deal with the moving position
override var oldPosX = posX
override var oldPosY = posY
var highlighted = false
override fun render(batch: SpriteBatch, camera: Camera) {

View File

@@ -20,10 +20,6 @@ class UIItemImageGallery(
val column: Int = 1
) : UIItem(parentUI, initialX, initialY) {
// deal with the moving position
override var oldPosX = posX
override var oldPosY = posY
override fun update(delta: Float) {
}

View File

@@ -75,10 +75,6 @@ class UIItemIntSlider(
)
// deal with the moving position
override var oldPosX = posX
override var oldPosY = posY
var value = initValue

View File

@@ -36,10 +36,6 @@ class UIItemList<Item: UIItem>(
val border: Int = 0
) : UIItem(parentUI, initialX, initialY) {
// deal with the moving position
override var oldPosX = posX
override var oldPosY = posY
init {
itemList.forEachIndexed { index, item ->
item.posX = this.posX + border

View File

@@ -16,10 +16,6 @@ class UIItemModuleInfoCell(
initialY: Int
) : UIItem(parent, initialX, initialY) {
// deal with the moving position
override var oldPosX = posX
override var oldPosY = posY
override val height: Int = AppLoader.fontGame.lineHeight.toInt() * 2
private val numberAreaWidth = AppLoader.fontSmallNumbers.W * 3 + 4

View File

@@ -17,10 +17,6 @@ class UIItemTextArea(
val align: Alignment = Alignment.LEFT
) : UIItem(parentUI, initialX, initialY) {
// deal with the moving position
override var oldPosX = posX
override var oldPosY = posY
private var entireText: List<String> = listOf("") // placeholder
var scrollPos = 0

View File

@@ -48,10 +48,6 @@ open class UIItemTextButton(
val hitboxSize: Int = UIItemTextButton.height
) : UIItem(parentUI, initialX, initialY) {
// deal with the moving position
override var oldPosX = posX
override var oldPosY = posY
companion object {
val font = AppLoader.fontGame
val height = font.lineHeight.toInt()

View File

@@ -57,10 +57,6 @@ class UIItemTextButtonList(
val DEFAULT_LINE_HEIGHT = 36
}
// deal with the moving position
override var oldPosX = posX
override var oldPosY = posY
val iconToTextGap = 20
val iconCellWidth = (iconSpriteSheet?.tileW ?: -iconToTextGap) / (iconSpriteSheet?.horizontalCount ?: 1)
val iconCellHeight = (iconSpriteSheet?.tileH ?: 0) / (iconSpriteSheet?.verticalCount ?: 1)

View File

@@ -34,8 +34,6 @@ class UIItemToggleButton(
get() = togglerBase.width
override val height: Int
get() = togglerBase.height
override var oldPosX = posX
override var oldPosY = posY
private var togglerBase = CommonResourcePool.getAsTexture("ui_item_toggler_base")
private var togglerHandle = CommonResourcePool.getAsTexture("ui_item_toggler_handle")

View File

@@ -0,0 +1,95 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.AppLoader
open class UIItemTransitionContainer(
parent: UICanvas,
initialX: Int,
initialY: Int,
override val width: Int,
override val height: Int,
val transitionLength: Float = 0.212f,
var currentPosition: Float = 0f,
val uis: Array<out UICanvas>
) : UIItem(parent, initialX, initialY) {
val debugvals = true
private var transitionRequested = false
private var transitionOngoing = false
private var transitionReqSource = currentPosition
private var transitionReqTarget = currentPosition
private var transitionTimer = 0f
private val epsilon = 0.001f
fun requestTransition(target: Int) {
if (!transitionOngoing) {
transitionRequested = true
transitionReqSource = Math.round(currentPosition).toFloat()
transitionReqTarget = target.toFloat()
}
}
override fun update(delta: Float) {
super.update(delta)
uis.forEachIndexed { index, ui ->
if (currentPosition > index - 1 + epsilon && currentPosition < index + 1 - epsilon) {
ui.update(delta)
}
}
}
open fun onTransition(currentPosition: Float, uis: Array<out UICanvas>) {}
override fun render(batch: SpriteBatch, camera: Camera) {
super.render(batch, camera)
if (transitionRequested && !transitionOngoing) {
transitionRequested = false
transitionOngoing = true
transitionTimer = 0f
}
if (transitionOngoing) {
transitionTimer += Gdx.graphics.rawDeltaTime
currentPosition = UIUtils.moveLinear(transitionReqSource, transitionReqTarget, transitionTimer, transitionLength)
if (transitionTimer > transitionLength) {
transitionOngoing = false
currentPosition = transitionReqTarget
}
onTransition(currentPosition, uis)
}
uis.forEachIndexed { index, ui ->
if (currentPosition > index - 1 + epsilon && currentPosition < index + 1 - epsilon) {
ui.setAsOpen()
ui.render(batch, camera)
if (debugvals) {
AppLoader.fontSmallNumbers.draw(batch, "$index", 300f + (20 * index), 10f)
}
}
else {
ui.setAsClose()
}
}
if (debugvals) {
batch.color = Color.WHITE
AppLoader.fontSmallNumbers.draw(batch, "position:$currentPosition", 500f, 10f)
}
}
override fun dispose() {
uis.forEach { it.dispose() }
}
}

View File

@@ -8,6 +8,17 @@ import net.torvald.terrarum.sqr
object UIUtils {
fun moveQuick(start: Float, end: Float, timer: Float, duration: Float) =
(start - end) * ((timer / duration) - 1).sqr() + end
fun moveLinear(start: Float, end: Float, timer: Float, duration: Float) =
(start - end) * (1 - (timer / duration)) + end
fun moveLinear(start: Float, end: Float, timer: Float, duration: Float): Float {
val scale = timer / duration
if (start == end) {
return start
}
if (scale <= 0f) {
return start
}
if (scale >= 1f) {
return end
}
return ((1f - scale) * start) + (scale * end)
}
}