From 0e5c86ad042d1f5ca43b06e83ff9463157dd06eb Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 26 Sep 2021 15:34:24 +0900 Subject: [PATCH] press tab to bring up the game menu --- src/net/torvald/terrarum/DefaultConfig.kt | 10 ++-- .../torvald/terrarum/UIItemInventoryCatBar.kt | 19 +++++-- .../modulebasegame/ui/UIInventoryFull.kt | 24 +++++++++ .../torvald/terrarum/serialise/WriteConfig.kt | 9 +++- src/net/torvald/terrarum/ui/Movement.kt | 17 +++++- src/net/torvald/terrarum/ui/UIHandler.kt | 53 +++++++++++++++---- src/net/torvald/terrarum/ui/UIItemList.kt | 2 +- .../terrarum/ui/UIItemTextButtonList.kt | 2 +- .../terrarum/ui/UIItemTransitionContainer.kt | 10 +++- src/net/torvald/terrarum/ui/UIUtils.kt | 24 --------- 10 files changed, 120 insertions(+), 50 deletions(-) delete mode 100644 src/net/torvald/terrarum/ui/UIUtils.kt diff --git a/src/net/torvald/terrarum/DefaultConfig.kt b/src/net/torvald/terrarum/DefaultConfig.kt index 9374ad95a..f31738db4 100644 --- a/src/net/torvald/terrarum/DefaultConfig.kt +++ b/src/net/torvald/terrarum/DefaultConfig.kt @@ -58,6 +58,7 @@ object DefaultConfig { "control_key_down" to Input.Keys.D, "control_key_right" to Input.Keys.F, // ESDF Masterrace + "control_key_jump" to Input.Keys.SPACE, "control_key_movementaux" to Input.Keys.A, // movement-auxiliary, or hookshot "control_key_inventory" to Input.Keys.Q, "control_key_interact" to Input.Keys.R, @@ -67,15 +68,14 @@ object DefaultConfig { "control_key_gamemenu" to Input.Keys.TAB, "control_key_quicksel" to Input.Keys.SHIFT_LEFT, // pie menu is now LShift because GDX does not read CapsLock + "control_mouse_quicksel" to Input.Buttons.MIDDLE, // middle click to open pie menu + // Colemak, Workman and some typers use CapsLock as Backspace, Apple-JIS and HHKB has Control in place of CapsLock and often re-assigned to Command // so these keys are treated as the same. // FOR ~~FUCKS~~ERGONOMICS' SAKE DON'T USE CTRL AND ALT AS A KEY! - "control_key_quickselalt" to intArrayOf(Input.Keys.BACKSPACE, Input.Keys.CONTROL_LEFT, Input.Keys.BACKSLASH), - "control_mouse_quicksel" to Input.Buttons.MIDDLE, // middle click to open pie menu + "control_key_quickslots" to (Input.Keys.NUM_0..Input.Keys.NUM_9).map { 1.0*it }.toDoubleArray(), + "control_key_quickselalt" to intArrayOf(Input.Keys.BACKSPACE, Input.Keys.CONTROL_LEFT, Input.Keys.BACKSLASH).map { 1.0*it }.toDoubleArray(), - "control_key_jump" to Input.Keys.SPACE, - - "control_key_quickslots" to (Input.Keys.NUM_0..Input.Keys.NUM_9).toList().toIntArray(), "config_mouseprimary" to Input.Buttons.LEFT, // left mouse "config_mousesecondary" to Input.Buttons.RIGHT, // right mouse diff --git a/src/net/torvald/terrarum/UIItemInventoryCatBar.kt b/src/net/torvald/terrarum/UIItemInventoryCatBar.kt index 72594c068..b068d4635 100644 --- a/src/net/torvald/terrarum/UIItemInventoryCatBar.kt +++ b/src/net/torvald/terrarum/UIItemInventoryCatBar.kt @@ -6,11 +6,10 @@ import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.gameitem.GameItem -import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull +import net.torvald.terrarum.ui.Movement import net.torvald.terrarum.ui.UICanvas import net.torvald.terrarum.ui.UIItem import net.torvald.terrarum.ui.UIItemImageButton -import net.torvald.terrarum.ui.UIUtils import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import kotlin.math.roundToInt @@ -136,8 +135,18 @@ class UIItemInventoryCatBar( /** * 0: map, 1: inventory caticons, 2: menu */ - var selectedPanel = 1 - private set + var selectedPanel = 1; private set + + + fun setSelectedPanel(n: Int) { + if (n !in 0..2) throw IllegalArgumentException("$n") + selectedPanel = n + + sideButtons[0].highlighted = (n == 0) + mainButtons[selectedIndex].highlighted = (n == 1) + sideButtons[3].highlighted = (n == 2) + } + // set up underlined indicator init { @@ -171,7 +180,7 @@ class UIItemInventoryCatBar( if (highlighterMoving) { highlighterMoveTimer += delta - highlighterXPos = UIUtils.moveQuick( + highlighterXPos = Movement.moveQuick( highlighterXStart, highlighterXEnd, highlighterMoveTimer, diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt index a30797500..b3a18e99d 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt @@ -184,6 +184,30 @@ class UIInventoryFull( } + // force position of things to UI when opened with "control_key_inventory" + this.handler.uiTogglerFunctionDefault = { + if (it.isClosed) { + transitionPanel.forcePosition(1) + catBar.setSelectedPanel(1) + it.setAsOpen() + } + else if (it.isOpened) + setAsClose() + } + + // allow "control_key_gamemenu" to open this UI + this.handler.toggleKeyExtra.add { App.getConfigInt("control_key_gamemenu") } + this.handler.toggleKeyExtraAction.add { + if (it.isClosed) { + transitionPanel.forcePosition(2) + catBar.setSelectedPanel(2) + it.setAsOpen() + } + else if (it.isOpened) + setAsClose() + } + + rebuildList() diff --git a/src/net/torvald/terrarum/serialise/WriteConfig.kt b/src/net/torvald/terrarum/serialise/WriteConfig.kt index b5ffd3cac..6cf9ed5b6 100644 --- a/src/net/torvald/terrarum/serialise/WriteConfig.kt +++ b/src/net/torvald/terrarum/serialise/WriteConfig.kt @@ -20,7 +20,14 @@ object WriteConfig { override fun write(json: Json, obj: KVHashMap, knownType: Class<*>?) { json.writeObjectStart() obj.hashMap.toSortedMap().forEach { (k, v) -> - json.writeValue(k, v) + json.writeValue(k, + if (v is Int) v as Int + else if (v is Double) v as Double + else if (v is IntArray) v as IntArray + else if (v is DoubleArray) v as DoubleArray + else if (v is Boolean) v as Boolean + else v.toString() + ) } json.writeObjectEnd() } diff --git a/src/net/torvald/terrarum/ui/Movement.kt b/src/net/torvald/terrarum/ui/Movement.kt index a25ca0dc9..69d75b83b 100644 --- a/src/net/torvald/terrarum/ui/Movement.kt +++ b/src/net/torvald/terrarum/ui/Movement.kt @@ -1,6 +1,7 @@ package net.torvald.terrarum.ui import com.jme3.math.FastMath +import net.torvald.terrarum.sqr /** * Created by minjaesong on 2016-03-22. @@ -26,5 +27,19 @@ object Movement{ if (scale < 0f) start else if (scale > 1f) end else (start - end) * FastMath.cos2(0.5f * FastMath.PI * scale) + end - + 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): 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) + } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/UIHandler.kt b/src/net/torvald/terrarum/ui/UIHandler.kt index 077e04a8f..c9342227c 100644 --- a/src/net/torvald/terrarum/ui/UIHandler.kt +++ b/src/net/torvald/terrarum/ui/UIHandler.kt @@ -24,12 +24,13 @@ import net.torvald.terrarum.modulebasegame.TerrarumIngame * Created by minjaesong on 2015-12-31. */ class UIHandler(//var UI: UICanvas, - var toggleKeyLiteral: Int? = null, - var toggleButtonLiteral: Int? = null, + var toggleKeyLiteral: Int? = null, + var toggleButtonLiteral: Int? = null, // UI positions itself? (you must g.flush() yourself after the g.translate(Int, Int)) - var customPositioning: Boolean = false, // mainly used by vital meter - var doNotWarnConstant: Boolean = false, - internal var allowESCtoClose: Boolean = false + var customPositioning: Boolean = false, // mainly used by vital meter + var doNotWarnConstant: Boolean = false, + internal var allowESCtoClose: Boolean = false, + var uiTogglerFunctionDefault: ((UIHandler) -> Unit)? = null ): Disposable { companion object { @@ -128,6 +129,21 @@ void main() { private val toggleButton: Int?; get() = toggleButtonLiteral // to support in-screen keybind changing + val toggleKeyExtra: ArrayList<() -> Int> = arrayListOf() + + /** + * Takes a function that works with UIHandler. + * For the function, try starting from the: + * ``` + * if (it.isClosed) + * it.setAsOpen() + * else if (it.isOpened) + * setAsClose() + * ``` + */ + val toggleKeyExtraAction: ArrayList<(UIHandler) -> Unit> = arrayListOf() + + val subUIs = ArrayList() val mouseUp: Boolean @@ -165,15 +181,30 @@ void main() { // some UIs will pause the game, and they still need to be closed if (!uiToggleLocked && (Terrarum.ingame?.consoleOpened == false && (Terrarum.ingame?.paused == false || isOpened))) { if (toggleKey != null && Gdx.input.isKeyJustPressed(toggleKey!!)) { - if (isClosed) - setAsOpen() - else if (isOpened) - setAsClose() + if (uiTogglerFunctionDefault == null) { + if (isClosed) + setAsOpen() + else if (isOpened) + setAsClose() + } + else uiTogglerFunctionDefault!!.invoke(this) // for the case of intermediate states, do nothing. } - if (toggleButton != null) { - /* */ + if (toggleButton != null && Gdx.input.isButtonJustPressed(toggleButton!!)) { + if (uiTogglerFunctionDefault == null) { + if (isClosed) + setAsOpen() + else if (isOpened) + setAsClose() + } + else uiTogglerFunctionDefault!!.invoke(this) + } + + toggleKeyExtra.forEachIndexed { index, getKey -> + if (Gdx.input.isKeyJustPressed(getKey())) { + toggleKeyExtraAction[0].invoke(this) + } } // ESC is a master key for closing diff --git a/src/net/torvald/terrarum/ui/UIItemList.kt b/src/net/torvald/terrarum/ui/UIItemList.kt index d106b9a70..b686452bc 100644 --- a/src/net/torvald/terrarum/ui/UIItemList.kt +++ b/src/net/torvald/terrarum/ui/UIItemList.kt @@ -63,7 +63,7 @@ class UIItemList( highlighterMoveTimer += delta if (selectedIndex != null) { - highlightY = UIUtils.moveQuick( + highlightY = Movement.moveQuick( highlighterYStart!!, highlighterYEnd!!, highlighterMoveTimer, diff --git a/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt b/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt index 524d346f8..f9b0370ca 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt @@ -183,7 +183,7 @@ class UIItemTextButtonList( highlighterMoveTimer += delta if (selectedIndex != null) { - highlightY = UIUtils.moveQuick( + highlightY = Movement.moveQuick( highlighterYStart!!, highlighterYEnd!!, highlighterMoveTimer.toFloat(), diff --git a/src/net/torvald/terrarum/ui/UIItemTransitionContainer.kt b/src/net/torvald/terrarum/ui/UIItemTransitionContainer.kt index 87bac6a45..e499c8b2c 100644 --- a/src/net/torvald/terrarum/ui/UIItemTransitionContainer.kt +++ b/src/net/torvald/terrarum/ui/UIItemTransitionContainer.kt @@ -37,6 +37,14 @@ open class UIItemTransitionContainer( } } + fun forcePosition(target: Int) { + transitionOngoing = false + transitionRequested = false + transitionTimer = 0f + currentPosition = target.toFloat() + onTransition(currentPosition, uis) + } + override fun update(delta: Float) { super.update(delta) uis.forEachIndexed { index, ui -> if (timeToUpdate(index)) ui.update(delta) } @@ -56,7 +64,7 @@ open class UIItemTransitionContainer( if (transitionOngoing) { transitionTimer += Gdx.graphics.deltaTime - currentPosition = UIUtils.moveLinear(transitionReqSource, transitionReqTarget, transitionTimer, transitionLength) + currentPosition = Movement.moveLinear(transitionReqSource, transitionReqTarget, transitionTimer, transitionLength) if (transitionTimer > transitionLength) { transitionOngoing = false diff --git a/src/net/torvald/terrarum/ui/UIUtils.kt b/src/net/torvald/terrarum/ui/UIUtils.kt deleted file mode 100644 index 576226d2e..000000000 --- a/src/net/torvald/terrarum/ui/UIUtils.kt +++ /dev/null @@ -1,24 +0,0 @@ -package net.torvald.terrarum.ui - -import net.torvald.terrarum.sqr - -/** - * Created by minjaesong on 2017-03-14. - */ -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): 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) - } -} \ No newline at end of file