fixed a bug where text buttons won't respond immediately/esc menu text button to unintentionally "click" the newly-appeared button (ESC -> Return to Main Menu -> Cancel -> Return to ... )

This commit is contained in:
minjaesong
2021-09-17 23:52:10 +09:00
parent 575ed4410b
commit e22518530d
4 changed files with 34 additions and 26 deletions

View File

@@ -71,6 +71,7 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() {
)
private val savingUI = UIItemSaving(this, (width - UIItemSaving.WIDTH) / 2, (height - UIItemSaving.HEIGHT) / 2)
private var oldScreen = 0
private var screen = 0
init {
@@ -120,20 +121,11 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() {
}
}
private val screenUpdates = arrayOf(
{ delta: Float ->
gameMenuButtons.update(delta)
},
{ delta: Float ->
areYouSureQuitButtons.update(delta)
},
{ delta: Float ->
areYouSureMainMenuButtons.update(delta)
},
{ delta: Float ->
savingUI.update(delta)
}
private val screens = arrayOf(
gameMenuButtons, areYouSureQuitButtons, areYouSureMainMenuButtons, savingUI
)
private val screenRenders = arrayOf(
{ batch: SpriteBatch, camera: Camera ->
// control hints
@@ -161,7 +153,11 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() {
override fun updateUI(delta: Float) {
screenUpdates[screen](delta)
if (oldScreen != screen) {
screens[screen].show()
oldScreen = screen
}
screens[screen].update(delta)
}
override fun renderUI(batch: SpriteBatch, camera: Camera) {

View File

@@ -106,6 +106,10 @@ abstract class UICanvas(
}
open fun show() {}
open fun hide() {}
/** **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. */

View File

@@ -57,7 +57,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
abstract val width: Int
abstract val height: Int
private val mouseButton = App.getConfigInt("config_mouseprimary")
protected val mouseButton = App.getConfigInt("config_mouseprimary")
/** This variable is NOT updated on its own.
* ```
@@ -91,18 +91,9 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
/** If mouse is hovering over it and mouse is down */
open val mousePushed: Boolean
get() = mouseUp && Gdx.input.isButtonPressed(mouseButton)
/** If mouse is just pushed */
open val mouseJustPushed: Boolean
get() {
if (mouseUp && !mouseLatched && Gdx.input.isButtonJustPressed(mouseButton)) {
mouseLatched = true
return true
}
return false
}
private var mouseLatched = false
private var mouseLatched = Gdx.input.isButtonPressed(mouseButton)
/** UI to call (show up) while mouse is up */
open val mouseOverCall: UICanvas? = null
@@ -132,6 +123,10 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I
open var controllerInFocus = false
open fun show() {}
open fun hide() {}
open fun update(delta: Float) {
if (parentUI.isVisible) {
if (updateListener != null) {

View File

@@ -1,8 +1,10 @@
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.App.printdbg
import net.torvald.terrarum.BlendMode
import net.torvald.terrarum.Second
import net.torvald.terrarum.fillRect
@@ -156,6 +158,13 @@ class UIItemTextButtonList(
private var highlighterYStart = highlightY
private var highlighterYEnd = highlightY
private var clickLatched = false
override fun show() {
printdbg(this, "${this.javaClass.simpleName} show()")
clickLatched = true
}
/** (oldIndex: Int?, newIndex: Int) -> Unit */
var selectionChangeListener: ((Int?, Int) -> Unit)? = null
@@ -188,7 +197,7 @@ class UIItemTextButtonList(
btn.update(delta)
if (btn.mouseJustPushed && index != selectedIndex) {
if (!clickLatched && btn.mousePushed && index != selectedIndex) {
val oldIndex = selectedIndex
if (kinematic) {
@@ -208,6 +217,10 @@ class UIItemTextButtonList(
}
if (!Gdx.input.isButtonPressed(mouseButton)) {
clickLatched = false
}
oldPosX = posX
}