From 19f4b3649a60667ced30762c3993f23e95502cf6 Mon Sep 17 00:00:00 2001 From: moonleay Date: Thu, 16 May 2024 03:29:57 +0200 Subject: [PATCH] feat: created custom slider widget Signed-off-by: moonleay --- .../screen/widgets/GimbalSliderWidget.kt | 50 +++++++++++++++++++ .../moonleay/gimbal/client/util/NumberUtil.kt | 37 ++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/main/java/net/moonleay/gimbal/client/screen/widgets/GimbalSliderWidget.kt create mode 100644 src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt diff --git a/src/main/java/net/moonleay/gimbal/client/screen/widgets/GimbalSliderWidget.kt b/src/main/java/net/moonleay/gimbal/client/screen/widgets/GimbalSliderWidget.kt new file mode 100644 index 0000000..c6d2038 --- /dev/null +++ b/src/main/java/net/moonleay/gimbal/client/screen/widgets/GimbalSliderWidget.kt @@ -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 . + */ + +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) + } +} diff --git a/src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt b/src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt new file mode 100644 index 0000000..5bdb3f3 --- /dev/null +++ b/src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt @@ -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 . + */ + +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 + } +}