horizontal slider

This commit is contained in:
minjaesong
2023-07-14 14:34:28 +09:00
parent e7ed3d8eae
commit d05364f43f
4 changed files with 122 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ object DefaultConfig {
val hashMap = hashMapOf<String, Any>(
"jvm_xmx" to 4,
"jvm_extra_cmd" to "",
"testvalue" to 1,
"displayfps" to 0, // 0: no limit, non-zero: limit
"displayfpsidle" to 0, // 0: no limit, non-zero: limit
@@ -24,7 +25,7 @@ object DefaultConfig {
"language" to App.getSysLang(),
"notificationshowuptime" to 4000, // 4s
"selecteditemnameshowuptime" to 4000, // 4s
"autosaveinterval" to 300000, // 5s
"autosaveinterval" to 300000, // 5m
"multithread" to true,
"showhealthmessageonstartup" to true,

View File

@@ -31,6 +31,7 @@ class UIPerformanceControlPanel(remoCon: UIRemoCon?) : UICanvas() {
arrayOf("", { Lang["MENU_OPTIONS_GAMEPLAY"] }, "h1"),
arrayOf("autosaveinterval", { Lang["MENU_OPTIONS_AUTOSAVE"] + " (${Lang["CONTEXT_TIME_MINUTE_PLURAL"]})" }, "spinnerimul,1,120,1,60000"),
arrayOf("notificationshowuptime", { Lang["MENU_OPTIONS_NOTIFICATION_DISPLAY_DURATION"] + " (${Lang["CONTEXT_TIME_SECOND_PLURAL"]})" }, "spinnerimul,2,10,1,1000"),
// arrayOf("testvalue", { "TestValue" }, "sliderd,1,100,1"),
arrayOf("", { Lang["MENU_LABEL_JVM_DNT"] }, "h1"),
arrayOf("jvm_xmx", { Lang["MENU_OPTIONS_JVM_HEAP_MAX"] + " (GB)" }, "spinner,2,32,1"),
arrayOf("jvm_extra_cmd", { Lang["MENU_LABEL_EXTRA_JVM_ARGUMENTS"] }, "typein"),
@@ -107,6 +108,14 @@ class UIPerformanceControlPanel(remoCon: UIRemoCon?) : UICanvas() {
}
}
}
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()

View File

@@ -0,0 +1,101 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.*
import kotlin.math.absoluteValue
import kotlin.math.roundToInt
/**
* For stepped values use UIItemHorzSliderStep (has different design tho:
* ```
* [-]--|--|--[]--|--[+]
* ```
*
* Created by minjaesong on 2023-07-14.
*/
class UIItemHorzSlider(
parentUI: UICanvas,
initialX: Int, initialY: Int,
private var initialValue: Double,
val min: Double,
val max: Double,
override val width: Int,
val handleWidth: Int = 12,
private val backgroundTexture: TextureRegion? = null
) : UIItem(parentUI, initialX, initialY) {
override val height = 24
private var mouseOnHandle = false
private val handleTravelDist = width - handleWidth
private var handlePos = 0.0
var value: Double = initialValue; private set
var selectionChangeListener: (Double) -> Unit = {}
override fun update(delta: Float) {
super.update(delta)
mouseOnHandle = itemRelativeMouseX in handlePos.roundToInt() until handlePos.roundToInt() + handleWidth && itemRelativeMouseY in 0 until height
// update handle position and value
if (mouseUp && mousePushed) {
handlePos = (itemRelativeMouseX - handleWidth/2.0).coerceIn(0.0, handleTravelDist.toDouble())
value = interpolateLinear(handlePos / handleTravelDist, min, max)
selectionChangeListener(value)
}
}
val troughBorderCol: Color; get() = if (mouseUp) Toolkit.Theme.COL_MOUSE_UP else Toolkit.Theme.COL_INACTIVE
val handleCol: Color; get() = if (mouseOnHandle && mousePushed) Toolkit.Theme.COL_SELECTED
else if (mouseOnHandle) Toolkit.Theme.COL_MOUSE_UP else Color.WHITE
private val renderJobs = arrayOf(
// trough fill
{ batch: SpriteBatch ->
if (backgroundTexture != null) {
batch.color = Color.WHITE
batch.draw(backgroundTexture, posX.toFloat(), posY.toFloat(), width.toFloat(), height.toFloat())
}
else {
batch.color = UIItemTextLineInput.TEXTINPUT_COL_BACKGROUND
Toolkit.fillArea(batch, posX, posY, width, height)
}
},
// trough border
{ batch: SpriteBatch ->
batch.color = troughBorderCol
Toolkit.drawBoxBorder(batch, posX - 1, posY - 1, width + 2, height + 2)
},
// handle fill
{ batch: SpriteBatch ->
batch.color = handleCol.cpy().mul(Color.LIGHT_GRAY)
Toolkit.fillArea(batch, posX + handlePos.roundToInt(), posY, handleWidth, height)
},
// handle border
{ batch: SpriteBatch ->
batch.color = handleCol
Toolkit.drawBoxBorder(batch, posX + handlePos.roundToInt() - 1, posY - 1, handleWidth + 2, height + 2)
},
)
private val renderOrderMouseUp = arrayOf(0,2,3,1).map { renderJobs[it] }
override fun render(batch: SpriteBatch, camera: Camera) {
blendNormalStraightAlpha(batch)
renderOrderMouseUp.forEach { it(batch) }
super.render(batch, camera)
}
override fun dispose() {
}
}

View File

@@ -1,12 +1,14 @@
package net.torvald.terrarum.ui
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.App
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.ceilToInt
import kotlin.math.absoluteValue
import kotlin.math.roundToInt
/**
* Internal properties, namely initialValue, min, max, step; have the type of [Double] regardless of their input type.
@@ -43,9 +45,13 @@ class UIItemSpinner(
val stepd = step.toDouble()
val id = initialValue.toDouble()
initialValue = (0..(maxd - mind).div(stepd).ceilToInt()).map {
val intermediate = (0..(maxd - mind).div(stepd).ceilToInt()).map {
it to ((mind + stepd * it) - id).absoluteValue
}.minBy { it.second }.first * stepd + mind
initialValue = when (initialValue.javaClass.simpleName) {
"Integer" -> intermediate.toInt()
else -> intermediate
}
}
}
@@ -173,6 +179,9 @@ class UIItemSpinner(
App.fontGame.draw(batch, textCache, posX + buttonW + 3f + (fboWidth - textCacheLen).div(2), posY.toFloat())
super.render(batch, camera)
// batch.color = Color.WHITE
// App.fontSmallNumbers.draw(batch, "${valueType.simpleName}", posX.toFloat(), posY.toFloat()) // draws "Integer" or "Double"
}
override fun scrolled(amountX: Float, amountY: Float): Boolean {