mirror of
https://codeberg.org/moonleay/Gimbal.git
synced 2024-11-25 22:22:10 +00:00
feat: created custom slider widget
Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
parent
7325798098
commit
19f4b3649a
2 changed files with 87 additions and 0 deletions
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* Gimbal
|
||||||
|
* Copyright (C) 2024 moonleay
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.moonleay.gimbal.client.screen.widgets
|
||||||
|
|
||||||
|
import net.minecraft.client.gui.widget.SliderWidget
|
||||||
|
import net.minecraft.text.Text
|
||||||
|
import net.moonleay.gimbal.client.util.NumberUtil
|
||||||
|
|
||||||
|
class GimbalSliderWidget(
|
||||||
|
x: Int,
|
||||||
|
y: Int,
|
||||||
|
width: Int,
|
||||||
|
height: Int,
|
||||||
|
private val text: Text,
|
||||||
|
value: Double,
|
||||||
|
private val lowerEnd: Double,
|
||||||
|
private val upperEnd: Double,
|
||||||
|
private val setValue: (Double) -> Unit,
|
||||||
|
) :
|
||||||
|
SliderWidget(x, y, width, height, text, value) {
|
||||||
|
init {
|
||||||
|
this.message = this.text.copy()
|
||||||
|
.append(": " + (NumberUtil.interpolate(this.value, 0.0, 1.0, this.lowerEnd, this.upperEnd).toInt()))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun updateMessage() {
|
||||||
|
this.message = this.text.copy()
|
||||||
|
.append(": " + (NumberUtil.interpolate(this.value, 0.0, 1.0, this.lowerEnd, this.upperEnd).toInt()))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyValue() {
|
||||||
|
this.setValue(this.value)
|
||||||
|
}
|
||||||
|
}
|
37
src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt
Normal file
37
src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* Gimbal
|
||||||
|
* Copyright (C) 2024 moonleay
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.moonleay.gimbal.client.util
|
||||||
|
|
||||||
|
object NumberUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interpolate a Position between two numbers
|
||||||
|
*/
|
||||||
|
fun interpolate(number: Double, min: Double, max: Double, lowerEnd: Double, upperEnd: Double): Double {
|
||||||
|
val toAddToMin = (0 - min) * -1
|
||||||
|
val minMaxDiff = max - min
|
||||||
|
val onePercent = minMaxDiff / 100.0
|
||||||
|
val percentOfNumber = (number - toAddToMin) / onePercent
|
||||||
|
|
||||||
|
val toAddToLowerEnd = (0 - lowerEnd) * -1
|
||||||
|
val upperLowerDiff = upperEnd - lowerEnd
|
||||||
|
val onePercentOfEnd = upperLowerDiff / 100.0
|
||||||
|
return (percentOfNumber * onePercentOfEnd) + toAddToLowerEnd
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue