From e3284572596c36cf2299a2df6e40dd704b66a781 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 14 Jul 2023 17:03:04 +0900 Subject: [PATCH] improved control panel making --- .../modulebasegame/ui/ControlPanelCommon.kt | 110 ++++++++++++++++++ .../ui/UIGraphicsControlPanel.kt | 73 +----------- .../ui/UIPerformanceControlPanel.kt | 98 +--------------- 3 files changed, 114 insertions(+), 167 deletions(-) create mode 100644 src/net/torvald/terrarum/modulebasegame/ui/ControlPanelCommon.kt diff --git a/src/net/torvald/terrarum/modulebasegame/ui/ControlPanelCommon.kt b/src/net/torvald/terrarum/modulebasegame/ui/ControlPanelCommon.kt new file mode 100644 index 000000000..98639369e --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/ui/ControlPanelCommon.kt @@ -0,0 +1,110 @@ +package net.torvald.terrarum.modulebasegame.ui + +import com.badlogic.gdx.Input +import net.torvald.terrarum.App +import net.torvald.terrarum.ui.* + +/** + * Created by minjaesong on 2023-07-14. + */ +object ControlPanelCommon { + + var CONFIG_SPINNER_WIDTH = 140 + var CONFIG_TYPEIN_WIDTH = 240 + + // @return Pair of + fun makeButton(parent: UICanvas, args: String, x: Int, y: Int, optionName: String): Pair Unit> { + return if (args.startsWith("h1") || args.startsWith("p")) { + (object : UIItem(parent, x, y) { + override val width = 1 + override val height = 1 + override fun dispose() {} + }) to { _, _ -> } + } + else if (args.startsWith("toggle")) { + UIItemToggleButton(parent, x, y, CONFIG_SPINNER_WIDTH, App.getConfigBoolean(optionName)) to { it: UIItem, optionStr: String -> + (it as UIItemToggleButton).clickOnceListener = { _, _ -> + it.toggle() + App.setConfig(optionStr, it.getStatus()) + } + } + } + else if (args.startsWith("spinner,")) { + val arg = args.split(',') + UIItemSpinner(parent, x, y, App.getConfigInt(optionName), arg[1].toInt(), arg[2].toInt(), arg[3].toInt(), CONFIG_SPINNER_WIDTH, numberToTextFunction = { "${it.toLong()}" }) to { it: UIItem, optionStr: String -> + (it as UIItemSpinner).selectionChangeListener = { + App.setConfig(optionStr, it) + } + } + } + else if (args.startsWith("spinnerd,")) { + val arg = args.split(',') + UIItemSpinner(parent, x, y, App.getConfigDouble(optionName), arg[1].toDouble(), arg[2].toDouble(), arg[3].toDouble(), CONFIG_SPINNER_WIDTH, numberToTextFunction = { "${((it as Double)*100).toInt()}%" }) to { it: UIItem, optionStr: String -> + (it as UIItemSpinner).selectionChangeListener = { + App.setConfig(optionStr, it) + } + } + } + else if (args.startsWith("sliderd,")) { + val arg = args.split(',') + UIItemHorzSlider(parent, x, y, App.getConfigDouble(optionName), arg[1].toDouble(), arg[2].toDouble(), CONFIG_SPINNER_WIDTH) to { it: UIItem, optionStr: String -> + (it as UIItemHorzSlider).selectionChangeListener = { + App.setConfig(optionStr, it) + } + } + } + else if (args.startsWith("spinnerimul,")) { + val arg = args.split(',') + val mult = arg[4].toInt() + UIItemSpinner(parent, x, y, App.getConfigInt(optionName) / mult, arg[1].toInt(), arg[2].toInt(), arg[3].toInt(), CONFIG_SPINNER_WIDTH, numberToTextFunction = { "${it.toLong()}" }) to { it: UIItem, optionStr: String -> + (it as UIItemSpinner).selectionChangeListener = { + App.setConfig(optionStr, it.toInt() * mult) + } + } + } + else if (args.startsWith("typeinint")) { +// val arg = args.split(',') // args: none + UIItemTextLineInput(parent, x, y, CONFIG_SPINNER_WIDTH, + defaultValue = { "${App.getConfigInt(optionName)}" }, + maxLen = InputLenCap(4, InputLenCap.CharLenUnit.CODEPOINTS), + keyFilter = { it.headkey in Input.Keys.NUM_0..Input.Keys.NUM_9 || it.headkey == Input.Keys.BACKSPACE } + ) to { it: UIItem, optionStr: String -> + (it as UIItemTextLineInput).textCommitListener = { + App.setConfig(optionStr, it.toInt()) // HAXXX!!! + } + } + } + else if (args.startsWith("typeinres")) { + val keyWidth = optionName.substringBefore(',') + val keyHeight = optionName.substringAfter(',') + UIItemTextLineInput(parent, x, y, CONFIG_SPINNER_WIDTH, + defaultValue = { "${App.getConfigInt(keyWidth)}x${App.getConfigInt(keyHeight)}" }, + maxLen = InputLenCap(9, InputLenCap.CharLenUnit.CODEPOINTS), + keyFilter = { it.headkey == Input.Keys.ENTER || it.headkey == Input.Keys.BACKSPACE || it.character?.matches(Regex("[0-9xX]")) == true }, + alignment = UIItemTextButton.Companion.Alignment.CENTRE + ) to { it: UIItem, optionStr: String -> + (it as UIItemTextLineInput).textCommitListener = { text -> + val text = text.lowercase() + if (text.matches(Regex("""[0-9]+x[0-9]+"""))) { + it.markAsNormal() + val width = text.substringBefore('x').toInt() + val height = text.substringAfter('x').toInt() + App.setConfig(keyWidth, width) + App.setConfig(keyHeight, height) + } + else it.markAsInvalid() + } + } + } + else if (args.startsWith("typein")) { + //args: none + UIItemTextLineInput(parent, x, y, CONFIG_TYPEIN_WIDTH, defaultValue = { App.getConfigString(optionName) }) to { it: UIItem, optionStr: String -> + (it as UIItemTextLineInput).textCommitListener = { + App.setConfig(optionStr, it) + } + } + } + else throw IllegalArgumentException(args) + } + +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIGraphicsControlPanel.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIGraphicsControlPanel.kt index a9be5c0e1..dedf3ed7b 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIGraphicsControlPanel.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIGraphicsControlPanel.kt @@ -9,6 +9,7 @@ import net.torvald.terrarum.App import net.torvald.terrarum.CommonResourcePool import net.torvald.terrarum.ceilToInt import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.modulebasegame.ui.ControlPanelCommon.makeButton import net.torvald.terrarum.ui.* import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import net.torvald.unicode.TIMES @@ -78,78 +79,8 @@ class UIGraphicsControlPanel(remoCon: UIRemoCon?) : UICanvas() { private val drawX = (Toolkit.drawWidth - width) / 2 private val drawY = (App.scr.height - height) / 2 - // @return Pair of - private fun makeButton(args: String, x: Int, y: Int, optionName: String): Pair Unit> { - return if (args.startsWith("h1") || args.startsWith("p")) { - (object : UIItem(this, x, y) { - override val width = 1 - override val height = 1 - override fun dispose() {} - }) to { _, _ -> } - } - else if (args.startsWith("toggle")) { - UIItemToggleButton(this, x, y, spinnerWidth, App.getConfigBoolean(optionName)) to { it: UIItem, optionStr: String -> - (it as UIItemToggleButton).clickOnceListener = { _, _ -> - it.toggle() - App.setConfig(optionStr, it.getStatus()) - } - } - } - else if (args.startsWith("spinner,")) { - val arg = args.split(',') - UIItemSpinner(this, x, y, App.getConfigInt(optionName), arg[1].toInt(), arg[2].toInt(), arg[3].toInt(), spinnerWidth, numberToTextFunction = { "${it.toLong()}" }) to { it: UIItem, optionStr: String -> - (it as UIItemSpinner).selectionChangeListener = { - App.setConfig(optionStr, it) - } - } - } - else if (args.startsWith("spinnerd,")) { - val arg = args.split(',') - UIItemSpinner(this, x, y, App.getConfigDouble(optionName), arg[1].toDouble(), arg[2].toDouble(), arg[3].toDouble(), spinnerWidth, numberToTextFunction = { "${((it as Double)*100).toInt()}%" }) to { it: UIItem, optionStr: String -> - (it as UIItemSpinner).selectionChangeListener = { - App.setConfig(optionStr, it) - } - } - } - else if (args.startsWith("typeinint")) { -// val arg = args.split(',') // args: none - UIItemTextLineInput(this, x, y, spinnerWidth, - defaultValue = { "${App.getConfigInt(optionName)}" }, - maxLen = InputLenCap(4, InputLenCap.CharLenUnit.CODEPOINTS), - keyFilter = { it.headkey in Input.Keys.NUM_0..Input.Keys.NUM_9 || it.headkey == Input.Keys.BACKSPACE } - ) to { it: UIItem, optionStr: String -> - (it as UIItemTextLineInput).textCommitListener = { - App.setConfig(optionStr, it.toInt()) // HAXXX!!! - } - } - } - else if (args.startsWith("typeinres")) { - val keyWidth = optionName.substringBefore(',') - val keyHeight = optionName.substringAfter(',') - UIItemTextLineInput(this, x, y, spinnerWidth, - defaultValue = { "${App.getConfigInt(keyWidth)}x${App.getConfigInt(keyHeight)}" }, - maxLen = InputLenCap(9, InputLenCap.CharLenUnit.CODEPOINTS), - keyFilter = { it.headkey == Input.Keys.ENTER || it.headkey == Input.Keys.BACKSPACE || it.character?.matches(Regex("[0-9xX]")) == true }, - alignment = UIItemTextButton.Companion.Alignment.CENTRE - ) to { it: UIItem, optionStr: String -> - (it as UIItemTextLineInput).textCommitListener = { text -> - val text = text.lowercase() - if (text.matches(Regex("""[0-9]+x[0-9]+"""))) { - it.markAsNormal() - val width = text.substringBefore('x').toInt() - val height = text.substringAfter('x').toInt() - App.setConfig(keyWidth, width) - App.setConfig(keyHeight, height) - } - else it.markAsInvalid() - } - } - } - else throw IllegalArgumentException(args) - } - private val optionControllers: List Unit>> = options.mapIndexed { index, strings -> - makeButton(options[index][2] as String, + makeButton(this, options[index][2] as String, drawX + width / 2 + panelgap, drawY - 2 + optionsYpos[index], options[index][0] as String diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIPerformanceControlPanel.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIPerformanceControlPanel.kt index 398c32acd..124441603 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIPerformanceControlPanel.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIPerformanceControlPanel.kt @@ -9,6 +9,7 @@ import net.torvald.terrarum.App import net.torvald.terrarum.CommonResourcePool import net.torvald.terrarum.ceilToInt import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.modulebasegame.ui.ControlPanelCommon.makeButton import net.torvald.terrarum.ui.* import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import net.torvald.unicode.TIMES @@ -75,103 +76,8 @@ class UIPerformanceControlPanel(remoCon: UIRemoCon?) : UICanvas() { private val drawX = (Toolkit.drawWidth - width) / 2 private val drawY = (App.scr.height - height) / 2 - // @return Pair of - private fun makeButton(args: String, x: Int, y: Int, optionName: String): Pair Unit> { - return if (args.startsWith("h1") || args.startsWith("p")) { - (object : UIItem(this, x, y) { - override val width = 1 - override val height = 1 - override fun dispose() {} - }) to { _, _ -> } - } - else if (args.startsWith("toggle")) { - UIItemToggleButton(this, x, y, spinnerWidth, App.getConfigBoolean(optionName)) to { it: UIItem, optionStr: String -> - (it as UIItemToggleButton).clickOnceListener = { _, _ -> - it.toggle() - App.setConfig(optionStr, it.getStatus()) - } - } - } - else if (args.startsWith("spinner,")) { - val arg = args.split(',') - UIItemSpinner(this, x, y, App.getConfigInt(optionName), arg[1].toInt(), arg[2].toInt(), arg[3].toInt(), spinnerWidth, numberToTextFunction = { "${it.toLong()}" }) to { it: UIItem, optionStr: String -> - (it as UIItemSpinner).selectionChangeListener = { - App.setConfig(optionStr, it) - } - } - } - else if (args.startsWith("spinnerd,")) { - val arg = args.split(',') - UIItemSpinner(this, x, y, App.getConfigDouble(optionName), arg[1].toDouble(), arg[2].toDouble(), arg[3].toDouble(), spinnerWidth, numberToTextFunction = { "${((it as Double)*100).toInt()}%" }) to { it: UIItem, optionStr: String -> - (it as UIItemSpinner).selectionChangeListener = { - App.setConfig(optionStr, it) - } - } - } - else if (args.startsWith("sliderd,")) { - val arg = args.split(',') - UIItemHorzSlider(this, x, y, App.getConfigDouble(optionName), arg[1].toDouble(), arg[2].toDouble(), spinnerWidth) to { it: UIItem, optionStr: String -> - (it as UIItemHorzSlider).selectionChangeListener = { - App.setConfig(optionStr, it) - } - } - } - else if (args.startsWith("spinnerimul,")) { - val arg = args.split(',') - val mult = arg[4].toInt() - UIItemSpinner(this, x, y, App.getConfigInt(optionName) / mult, arg[1].toInt(), arg[2].toInt(), arg[3].toInt(), spinnerWidth, numberToTextFunction = { "${it.toLong()}" }) to { it: UIItem, optionStr: String -> - (it as UIItemSpinner).selectionChangeListener = { - App.setConfig(optionStr, it.toInt() * mult) - } - } - } - else if (args.startsWith("typeinint")) { -// val arg = args.split(',') // args: none - UIItemTextLineInput(this, x, y, spinnerWidth, - defaultValue = { "${App.getConfigInt(optionName)}" }, - maxLen = InputLenCap(4, InputLenCap.CharLenUnit.CODEPOINTS), - keyFilter = { it.headkey in Input.Keys.NUM_0..Input.Keys.NUM_9 || it.headkey == Input.Keys.BACKSPACE } - ) to { it: UIItem, optionStr: String -> - (it as UIItemTextLineInput).textCommitListener = { - App.setConfig(optionStr, it.toInt()) // HAXXX!!! - } - } - } - else if (args.startsWith("typeinres")) { - val keyWidth = optionName.substringBefore(',') - val keyHeight = optionName.substringAfter(',') - UIItemTextLineInput(this, x, y, spinnerWidth, - defaultValue = { "${App.getConfigInt(keyWidth)}x${App.getConfigInt(keyHeight)}" }, - maxLen = InputLenCap(9, InputLenCap.CharLenUnit.CODEPOINTS), - keyFilter = { it.headkey == Input.Keys.ENTER || it.headkey == Input.Keys.BACKSPACE || it.character?.matches(Regex("[0-9xX]")) == true }, - alignment = UIItemTextButton.Companion.Alignment.CENTRE - ) to { it: UIItem, optionStr: String -> - (it as UIItemTextLineInput).textCommitListener = { text -> - val text = text.lowercase() - if (text.matches(Regex("""[0-9]+x[0-9]+"""))) { - it.markAsNormal() - val width = text.substringBefore('x').toInt() - val height = text.substringAfter('x').toInt() - App.setConfig(keyWidth, width) - App.setConfig(keyHeight, height) - } - else it.markAsInvalid() - } - } - } - else if (args.startsWith("typein")) { - //args: none - UIItemTextLineInput(this, x, y, typeinWidth, defaultValue = { App.getConfigString(optionName) }) to { it: UIItem, optionStr: String -> - (it as UIItemTextLineInput).textCommitListener = { - App.setConfig(optionStr, it) - } - } - } - else throw IllegalArgumentException(args) - } - private val optionControllers: List Unit>> = options.mapIndexed { index, strings -> - makeButton(options[index][2] as String, + makeButton(this, options[index][2] as String, drawX + width / 2 + panelgap, drawY - 2 + optionsYpos[index], options[index][0] as String