Skip to content

Commit 88ed39e

Browse files
committed
Added gui component
1 parent abd6a3f commit 88ed39e

File tree

6 files changed

+142
-49
lines changed

6 files changed

+142
-49
lines changed

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import com.google.gson.reflect.TypeToken
2323
import com.lambda.Lambda
2424
import com.lambda.Lambda.LOG
2525
import com.lambda.config.settings.CharSetting
26-
import com.lambda.config.settings.DurationSetting
26+
import com.lambda.config.settings.comparable.DurationSetting
2727
import com.lambda.config.settings.FunctionSetting
2828
import com.lambda.config.settings.StringSetting
2929
import com.lambda.config.settings.collections.ListSetting

common/src/main/kotlin/com/lambda/config/settings/DurationSetting.kt

Lines changed: 0 additions & 46 deletions
This file was deleted.

common/src/main/kotlin/com/lambda/config/settings/NumericSetting.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import kotlin.reflect.KProperty
2626
/**
2727
* @see [com.lambda.config.Configurable]
2828
*/
29-
abstract class NumericSetting<T : Comparable<T>>(
29+
abstract class NumericSetting<T>(
3030
value: T,
3131
open val range: ClosedRange<T>,
3232
open val step: T,
@@ -38,7 +38,7 @@ abstract class NumericSetting<T : Comparable<T>>(
3838
TypeToken.get(value::class.java).type,
3939
description,
4040
visibility
41-
) {
41+
) where T : Number, T : Comparable<T> {
4242
private val formatter = NumberFormat.getNumberInstance(Locale.getDefault())
4343

4444
override fun toString() = "${formatter.format(value)}$unit"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.config.settings.comparable
19+
20+
import com.google.gson.reflect.TypeToken
21+
import com.lambda.config.AbstractSetting
22+
import kotlin.reflect.KProperty
23+
import kotlin.time.Duration
24+
25+
class DurationSetting(
26+
override val name: String,
27+
defaultValue: Duration,
28+
val range: ClosedRange<Duration>,
29+
val step: Duration,
30+
description: String,
31+
visibility: () -> Boolean,
32+
) : AbstractSetting<Duration>(
33+
defaultValue,
34+
TypeToken.get(defaultValue::class.java).type,
35+
description,
36+
visibility,
37+
) {
38+
override fun toString() = value.toString()
39+
40+
override operator fun setValue(thisRef: Any?, property: KProperty<*>, valueIn: Duration) {
41+
value = valueIn.coerceIn(range)
42+
}
43+
}

common/src/main/kotlin/com/lambda/gui/GuiManager.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.lambda.gui
2020
import com.lambda.config.settings.comparable.BooleanSetting
2121
import com.lambda.config.settings.comparable.EnumSetting
2222
import com.lambda.config.settings.FunctionSetting
23+
import com.lambda.config.settings.comparable.DurationSetting
2324
import com.lambda.config.settings.complex.ColorSetting
2425
import com.lambda.config.settings.complex.KeyBindSetting
2526
import com.lambda.config.settings.numeric.DoubleSetting
@@ -31,6 +32,7 @@ import com.lambda.gui.component.core.UIBuilder
3132
import com.lambda.gui.component.layout.Layout
3233
import com.lambda.gui.impl.clickgui.module.setting.settings.BooleanButton.Companion.booleanSetting
3334
import com.lambda.gui.impl.clickgui.module.setting.settings.ColorPicker.Companion.colorPicker
35+
import com.lambda.gui.impl.clickgui.module.setting.settings.DurationSlider.Companion.durationSlider
3436
import com.lambda.gui.impl.clickgui.module.setting.settings.EnumSlider.Companion.enumSetting
3537
import com.lambda.gui.impl.clickgui.module.setting.settings.KeybindPicker.Companion.keybindSetting
3638
import com.lambda.gui.impl.clickgui.module.setting.settings.NumberSlider.Companion.numberSlider
@@ -115,6 +117,18 @@ object GuiManager : Loadable {
115117
}
116118
}
117119

120+
typeAdapter<DurationSetting> { owner, ref ->
121+
owner.durationSlider(
122+
ref.name,
123+
ref.range.start,
124+
ref.range.endInclusive,
125+
ref.step,
126+
ref::value
127+
).apply {
128+
visibility { ref.visibility() }
129+
}
130+
}
131+
118132
return "Loaded ${typeMap.size} gui type adapters."
119133
}
120134

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.gui.impl.clickgui.module.setting.settings
19+
20+
import com.lambda.gui.component.core.UIBuilder
21+
import com.lambda.gui.component.layout.Layout
22+
import com.lambda.gui.impl.clickgui.module.setting.SettingSlider
23+
import com.lambda.util.math.MathUtils.roundToStep
24+
import com.lambda.util.math.lerp
25+
import com.lambda.util.math.transform
26+
import kotlin.reflect.KMutableProperty0
27+
import kotlin.time.Duration
28+
import com.lambda.config.settings.comparable.DurationSetting
29+
import com.lambda.util.extension.highestUnit
30+
import kotlin.time.toDuration
31+
32+
class DurationSlider(
33+
owner: Layout,
34+
name: String,
35+
min: Duration,
36+
max: Duration,
37+
step: Duration,
38+
field: KMutableProperty0<Duration>,
39+
) : SettingSlider<Duration>(owner, name, field) {
40+
override val settingValue: String
41+
get() = settingDelegate.toString()
42+
43+
private val unit = step.highestUnit
44+
45+
private val minNanos = min.toDouble(unit)
46+
private val maxNanos = max.toDouble(unit)
47+
private val stepNanos = step.toDouble(unit)
48+
49+
init {
50+
// Slider logic
51+
slider.progress {
52+
transform(
53+
settingDelegate.toDouble(unit),
54+
minNanos,
55+
maxNanos,
56+
0.0,
57+
1.0,
58+
)
59+
}
60+
61+
slider.onSlide {
62+
settingDelegate = lerp(it, minNanos, maxNanos)
63+
.roundToStep(stepNanos)
64+
.toDuration(unit)
65+
.coerceIn(min..max)
66+
}
67+
}
68+
69+
companion object {
70+
/**
71+
* Creates an [DurationSlider] - visual representation of the [DurationSetting]
72+
*/
73+
@UIBuilder
74+
fun Layout.durationSlider(
75+
name: String,
76+
min: Duration,
77+
max: Duration,
78+
step: Duration,
79+
field: KMutableProperty0<Duration>
80+
) = DurationSlider(this, name, min, max, step, field).apply(children::add)
81+
}
82+
}

0 commit comments

Comments
 (0)