Skip to content

Commit 214fb20

Browse files
committed
Setting independent ui components
1 parent 55c47c9 commit 214fb20

File tree

13 files changed

+175
-74
lines changed

13 files changed

+175
-74
lines changed

common/src/main/kotlin/com/lambda/config/settings/comparable/EnumSetting.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,27 @@ class EnumSetting<T : Enum<T>>(
4343
description,
4444
visibility,
4545
) {
46-
val enumValues: Array<T> = defaultValue.declaringJavaClass.enumConstants
47-
4846
fun next() {
49-
value = enumValues[((value.ordinal + 1) % enumValues.size)]
47+
value = value.enumValues[((value.ordinal + 1) % value.enumValues.size)]
5048
}
5149

5250
override fun CommandBuilder.buildCommand(registry: CommandRegistryAccess) {
5351
required(word(name)) { parameter ->
5452
suggests { _, builder ->
55-
enumValues.forEach { builder.suggest(it.name.capitalize()) }
53+
value.enumValues.forEach { builder.suggest(it.name.capitalize()) }
5654
builder.buildFuture()
5755
}
5856
executeWithResult {
59-
val newValue = enumValues.find { it.name.equals(parameter().value(), true) }
57+
val newValue = value.enumValues.find { it.name.equals(parameter().value(), true) }
6058
?: return@executeWithResult failure("Invalid value")
6159
trySetValue(newValue)
6260
return@executeWithResult success()
6361
}
6462
}
6563
}
64+
65+
companion object {
66+
val <T : Enum<T>> T.enumValues: Array<T> get() =
67+
declaringJavaClass.enumConstants
68+
}
6669
}

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

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,27 @@
1717

1818
package com.lambda.gui
1919

20-
import com.lambda.config.settings.NumericSetting
2120
import com.lambda.config.settings.comparable.BooleanSetting
2221
import com.lambda.config.settings.comparable.EnumSetting
2322
import com.lambda.config.settings.FunctionSetting
2423
import com.lambda.config.settings.complex.ColorSetting
2524
import com.lambda.config.settings.complex.KeyBindSetting
25+
import com.lambda.config.settings.numeric.DoubleSetting
26+
import com.lambda.config.settings.numeric.FloatSetting
27+
import com.lambda.config.settings.numeric.IntegerSetting
28+
import com.lambda.config.settings.numeric.LongSetting
2629
import com.lambda.core.Loadable
2730
import com.lambda.gui.component.core.UIBuilder
2831
import com.lambda.gui.component.layout.Layout
2932
import com.lambda.gui.impl.clickgui.module.settings.impl.BooleanButton.Companion.booleanSetting
3033
import com.lambda.gui.impl.clickgui.module.settings.impl.ColorPicker.Companion.colorPicker
34+
import com.lambda.gui.impl.clickgui.module.settings.impl.EnumSlider
3135
import com.lambda.gui.impl.clickgui.module.settings.impl.EnumSlider.Companion.enumSetting
3236
import com.lambda.gui.impl.clickgui.module.settings.impl.KeybindPicker.Companion.keybindSetting
3337
import com.lambda.gui.impl.clickgui.module.settings.impl.NumberSlider.Companion.numericSetting
34-
import com.lambda.gui.impl.clickgui.module.settings.impl.UnitButton.Companion.unitSetting
38+
import com.lambda.gui.impl.clickgui.module.settings.impl.UnitButton.Companion.unitButton
3539
import kotlin.reflect.KClass
40+
import kotlin.reflect.KMutableProperty0
3641

3742
object GuiManager : Loadable {
3843
val typeMap = mutableMapOf<KClass<*>, (owner: Layout, converted: Any) -> Layout>()
@@ -43,27 +48,73 @@ object GuiManager : Loadable {
4348

4449
override fun load(): String {
4550
typeAdapter<BooleanSetting> { owner, ref ->
46-
owner.booleanSetting(ref)
51+
owner.booleanSetting(ref.name, ref::value).apply {
52+
visibility { ref.visibility() }
53+
}
4754
}
4855

4956
typeAdapter<EnumSetting<*>> { owner, ref ->
50-
owner.enumSetting(ref)
57+
owner.enumSetting(ref.name, ref::value).apply {
58+
visibility { ref.visibility() }
59+
}
5160
}
5261

5362
typeAdapter<FunctionSetting<*>> { owner, ref ->
54-
owner.unitSetting(ref)
63+
owner.unitButton(ref.name, ref::value).apply {
64+
visibility { ref.visibility() }
65+
}
5566
}
5667

57-
typeAdapter<NumericSetting<*>> { owner, ref ->
58-
owner.numericSetting(ref)
68+
typeAdapter<DoubleSetting> { owner, ref ->
69+
owner.numericSetting(
70+
ref.name, ref.unit,
71+
ref.range.start, ref.range.endInclusive, ref.step,
72+
ref::value
73+
).apply {
74+
visibility { ref.visibility() }
75+
}
76+
}
77+
78+
typeAdapter<FloatSetting> { owner, ref ->
79+
owner.numericSetting(
80+
ref.name, ref.unit,
81+
ref.range.start, ref.range.endInclusive, ref.step,
82+
ref::value
83+
).apply {
84+
visibility { ref.visibility() }
85+
}
86+
}
87+
88+
typeAdapter<IntegerSetting> { owner, ref ->
89+
owner.numericSetting(
90+
ref.name, ref.unit,
91+
ref.range.start, ref.range.endInclusive, ref.step,
92+
ref::value
93+
).apply {
94+
visibility { ref.visibility() }
95+
}
96+
}
97+
98+
typeAdapter<LongSetting> { owner, ref ->
99+
owner.numericSetting(
100+
ref.name, ref.unit,
101+
ref.range.start, ref.range.endInclusive, ref.step,
102+
ref::value
103+
).apply {
104+
visibility { ref.visibility() }
105+
}
59106
}
60107

61108
typeAdapter<KeyBindSetting> { owner, ref ->
62-
owner.keybindSetting(ref)
109+
owner.keybindSetting(ref.name, ref::value).apply {
110+
visibility { ref.visibility() }
111+
}
63112
}
64113

65114
typeAdapter<ColorSetting> { owner, ref ->
66-
owner.colorPicker(ref)
115+
owner.colorPicker(ref.name, ref::value).apply {
116+
visibility { ref.visibility() }
117+
}
67118
}
68119

69120
return "Loaded ${typeMap.size} gui type adapters."

common/src/main/kotlin/com/lambda/gui/impl/clickgui/core/AnimatedChild.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ abstract class AnimatedChild(
7171
speed = transform(index.toDouble(), 0.0, lastIndex.toDouble(), start, end)
7272
}
7373

74-
if ((this as? SettingLayout<*, *>)?.isVisible == true) speed *= 0.8
74+
if ((this as? SettingLayout<*>)?.isVisible == true) speed *= 0.8
7575

7676
speed + isShownInternal.toInt() * 0.1
7777
}) { isShownInternal }.apply {

common/src/main/kotlin/com/lambda/gui/impl/clickgui/core/SliderLayout.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SliderLayout(
3939
private val isVertical: Boolean
4040
) : AnimatedChild(owner, "") {
4141
// Not a great solution
42-
private val setting = owner as? SettingSlider<*, *>
42+
private val setting = owner as? SettingSlider<*>
4343
private val showAnim get() = setting?.showAnimation ?: showAnimation
4444
private val pressedBut get() = setting?.pressedButton ?: pressedButton
4545

common/src/main/kotlin/com/lambda/gui/impl/clickgui/module/ModuleLayout.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,13 @@ class ModuleLayout(
152152
}
153153
}
154154

155-
val settings = module.settings.map { setting ->
155+
val settings = module.settings.mapNotNull { setting ->
156156
content.layoutOf(setting)
157-
}.filterIsInstance<SettingLayout<*, *>>()
157+
}.map { it as SettingLayout<*> }.onEach {
158+
it.onUpdate {
159+
width = this@ModuleLayout.content.width
160+
}
161+
}
158162

159163
val minimizeSettings = {
160164
settings.forEach {
@@ -182,7 +186,7 @@ class ModuleLayout(
182186
*/
183187
@UIBuilder
184188
fun Window.backgroundTint(tintTitleBar: Boolean = false) {
185-
check(this is SettingLayout<*, *> || this is ModuleLayout || this is ModuleWindow)
189+
check(this is SettingLayout<*> || this is ModuleLayout || this is ModuleWindow)
186190

187191
val base = this@backgroundTint
188192

common/src/main/kotlin/com/lambda/gui/impl/clickgui/module/settings/SettingLayout.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,53 @@
1717

1818
package com.lambda.gui.impl.clickgui.module.settings
1919

20-
import com.lambda.config.AbstractSetting
20+
import com.lambda.gui.component.core.LayoutBuilder
2121
import com.lambda.module.modules.client.ClickGui
2222
import com.lambda.gui.component.layout.Layout
2323
import com.lambda.gui.impl.clickgui.module.ModuleLayout.Companion.backgroundTint
2424
import com.lambda.gui.impl.clickgui.core.AnimatedChild
2525
import com.lambda.util.math.*
26+
import kotlin.reflect.KMutableProperty0
2627

2728
/**
2829
* A base class for setting layouts.
2930
*/
30-
abstract class SettingLayout <V : Any, T: AbstractSetting<V>> (
31+
abstract class SettingLayout <V : Any> (
3132
owner: Layout,
32-
val setting: T,
33+
name: String,
34+
field: KMutableProperty0<V>,
3335
expandable: Boolean = false
3436
) : AnimatedChild(
3537
owner,
36-
setting.name,
37-
Vec2d.ZERO, Vec2d.ZERO,
38+
name,
39+
Vec2d.ZERO, Vec2d(110.0, 0.0),
3840
false, false,
3941
if (expandable) Minimizing.Absolute else Minimizing.Disabled,
4042
false,
4143
AutoResize.ForceEnabled
4244
) {
4345
protected val cursorController = cursorController()
4446

45-
var settingDelegate by setting
46-
val isVisible get() = setting.visibility()
47+
protected var settingDelegate by field
48+
private var onValueSet = mutableListOf<(V) -> Unit>()
49+
private var getVisibilityBlock = { true }
50+
val isVisible get() = getVisibilityBlock()
51+
52+
@LayoutBuilder
53+
fun visibility(action: () -> Boolean) {
54+
getVisibilityBlock = { action() }
55+
}
56+
57+
@LayoutBuilder
58+
fun valueSet(action: (V) -> Unit) {
59+
onValueSet += action
60+
}
4761

4862
override val isShown: Boolean get() = super.isShown && isVisible
4963

5064
init {
5165
isMinimized = true
5266

53-
onUpdate {
54-
width = owner.width
55-
}
56-
5767
titleBar.onUpdate {
5868
height = ClickGui.settingsHeight
5969
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/module/settings/SettingSlider.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.lambda.gui.impl.clickgui.module.settings
1919

20-
import com.lambda.config.AbstractSetting
2120
import com.lambda.graphics.animation.Animation.Companion.exp
2221
import com.lambda.gui.component.HAlign
2322
import com.lambda.gui.component.VAlign
@@ -26,10 +25,13 @@ import com.lambda.gui.component.layout.Layout
2625
import com.lambda.gui.impl.clickgui.core.SliderLayout.Companion.sliderBehind
2726
import com.lambda.module.modules.client.ClickGui
2827
import com.lambda.util.math.lerp
28+
import kotlin.reflect.KMutableProperty0
2929

30-
abstract class SettingSlider <V : Any, T: AbstractSetting<V>>(
31-
owner: Layout, setting: T
32-
) : SettingLayout<V, T>(owner, setting, false) {
30+
abstract class SettingSlider <V : Any>(
31+
owner: Layout,
32+
name: String,
33+
field: KMutableProperty0<V>
34+
) : SettingLayout<V>(owner, name, field) {
3335
abstract val settingValue: String
3436

3537
private var changeAnimation by animation.exp(0.0, 1.0, 0.5) { true }

common/src/main/kotlin/com/lambda/gui/impl/clickgui/module/settings/impl/BooleanButton.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.lambda.gui.impl.clickgui.module.settings.impl
1919

20-
import com.lambda.config.settings.comparable.BooleanSetting
2120
import com.lambda.graphics.animation.Animation.Companion.exp
2221
import com.lambda.module.modules.client.ClickGui
2322
import com.lambda.gui.component.core.FilledRect.Companion.rect
@@ -31,11 +30,13 @@ import com.lambda.util.math.Vec2d
3130
import com.lambda.util.math.lerp
3231
import com.lambda.util.math.setAlpha
3332
import java.awt.Color
33+
import kotlin.reflect.KMutableProperty0
3434

3535
class BooleanButton(
3636
owner: Layout,
37-
setting: BooleanSetting
38-
) : SettingLayout<Boolean, BooleanSetting>(owner, setting) {
37+
name: String,
38+
field: KMutableProperty0<Boolean>
39+
) : SettingLayout<Boolean>(owner, name, field) {
3940
private val activeAnimation by animation.exp(0.0, 1.0, 0.6, ::settingDelegate)
4041

4142
init {
@@ -91,16 +92,16 @@ class BooleanButton(
9192
}
9293

9394
onMouseAction(Mouse.Button.Left) {
94-
setting.value = !setting.value
95+
settingDelegate = !settingDelegate
9596
}
9697
}
9798

9899
companion object {
99100
/**
100-
* Creates a [BooleanButton] - visual representation of the [BooleanSetting]
101+
* Creates a [BooleanButton]
101102
*/
102103
@UIBuilder
103-
fun Layout.booleanSetting(setting: BooleanSetting) =
104-
BooleanButton(this, setting).apply(children::add)
104+
fun Layout.booleanSetting(name: String, field: KMutableProperty0<Boolean>) =
105+
BooleanButton(this, name, field).apply(children::add)
105106
}
106107
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/module/settings/impl/ColorPicker.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717

1818
package com.lambda.gui.impl.clickgui.module.settings.impl
1919

20-
import com.lambda.config.settings.complex.ColorSetting
2120
import com.lambda.gui.component.core.UIBuilder
2221
import com.lambda.gui.component.layout.Layout
2322
import com.lambda.gui.component.popup.Popup
2423
import com.lambda.gui.impl.clickgui.module.settings.SettingLayout
2524
import com.lambda.util.Mouse
2625
import java.awt.Color
26+
import kotlin.reflect.KMutableProperty0
2727

2828
class ColorPicker(
2929
owner: Layout,
30-
setting: ColorSetting
31-
) : SettingLayout<Color, ColorSetting>(owner, setting) {
32-
private val popup = Popup(this, setting.name).apply {
30+
name: String,
31+
field: KMutableProperty0<Color>,
32+
) : SettingLayout<Color>(owner, name, field) {
33+
private val popup = Popup(this, name).apply {
3334
window.use {
3435
windowWidth = 200.0
3536
windowHeight = 100.0
@@ -44,10 +45,10 @@ class ColorPicker(
4445

4546
companion object {
4647
/**
47-
* Creates a [ColorPicker] - visual representation of the [ColorSetting]
48+
* Creates a [ColorPicker]
4849
*/
4950
@UIBuilder
50-
fun Layout.colorPicker(setting: ColorSetting) =
51-
ColorPicker(this, setting).apply(children::add)
51+
fun Layout.colorPicker(name: String, field: KMutableProperty0<Color>) =
52+
ColorPicker(this, name, field).apply(children::add)
5253
}
5354
}

0 commit comments

Comments
 (0)