From e22518530d25fa6ee8dfa9231ab9bceacc4bc4b1 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 17 Sep 2021 23:52:10 +0900 Subject: [PATCH] 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 ... ) --- .../modulebasegame/ui/UIInventoryEscMenu.kt | 24 ++++++++----------- src/net/torvald/terrarum/ui/UICanvas.kt | 4 ++++ src/net/torvald/terrarum/ui/UIItem.kt | 17 +++++-------- .../terrarum/ui/UIItemTextButtonList.kt | 15 +++++++++++- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt index 37202af04..d495290ef 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryEscMenu.kt @@ -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) { diff --git a/src/net/torvald/terrarum/ui/UICanvas.kt b/src/net/torvald/terrarum/ui/UICanvas.kt index cd3d12c2d..7ad192908 100644 --- a/src/net/torvald/terrarum/ui/UICanvas.kt +++ b/src/net/torvald/terrarum/ui/UICanvas.kt @@ -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. */ diff --git a/src/net/torvald/terrarum/ui/UIItem.kt b/src/net/torvald/terrarum/ui/UIItem.kt index 3abefecc4..61d6f8b85 100644 --- a/src/net/torvald/terrarum/ui/UIItem.kt +++ b/src/net/torvald/terrarum/ui/UIItem.kt @@ -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) { diff --git a/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt b/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt index e797f6d82..902a584f8 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt @@ -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 }