Skip to content

Commit 8d3fa99

Browse files
committed
Enum setting
1 parent 8b555a1 commit 8d3fa99

File tree

9 files changed

+202
-52
lines changed

9 files changed

+202
-52
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
package com.lambda.gui
1919

2020
import com.lambda.config.settings.comparable.BooleanSetting
21+
import com.lambda.config.settings.comparable.EnumSetting
2122
import com.lambda.core.Loadable
2223
import com.lambda.gui.component.core.UIBuilder
2324
import com.lambda.gui.component.layout.Layout
2425
import com.lambda.gui.impl.clickgui.settings.BooleanButton.Companion.booleanSetting
26+
import com.lambda.gui.impl.clickgui.settings.EnumSelector.Companion.enumSetting
2527
import kotlin.reflect.KClass
2628

2729
object GuiManager : Loadable {
@@ -36,6 +38,10 @@ object GuiManager : Loadable {
3638
owner.booleanSetting(ref)
3739
}
3840

41+
typeAdapter<EnumSetting<*>> { owner, ref ->
42+
owner.enumSetting(ref)
43+
}
44+
3945
return "Loaded ${typeMap.size} gui type adapters."
4046
}
4147

common/src/main/kotlin/com/lambda/gui/component/window/Window.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ open class Window(
5353

5454
val titleBar = titleBar(initialTitle, draggable)
5555

56-
protected val titleBarBackground by titleBar::backgroundRect
57-
protected val contentBackground = rect { // It's here because content cannot contain something by default
56+
val titleBarBackground by titleBar::backgroundRect
57+
val contentBackground = rect { // It's here because content cannot contain something by default
5858
onUpdate {
5959
rectangle = Rect(titleBar.leftBottom, this@Window.rightBottom)
6060
setColor(ClickGui.backgroundColor)
@@ -68,7 +68,7 @@ open class Window(
6868

6969
val content = windowContent(scrollable)
7070

71-
protected val outlineRect = outline {
71+
val outlineRect = outline {
7272
onUpdate {
7373
rectangle = this@Window.rect
7474
setColor(ClickGui.outlineColor)

common/src/main/kotlin/com/lambda/gui/component/window/WindowContent.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class WindowContent(
6060
}
6161

6262
it.overrideY {
63-
prev.renderPositionY + layoutHeight(prev, true) + ClickGui.listStep
63+
prev.renderPositionY + layoutHeight(prev, true)
6464
}
6565
}
6666
}
@@ -75,9 +75,14 @@ class WindowContent(
7575

7676
overrideWidth(owner::renderWidth)
7777
overrideHeight {
78-
children.sumOf { layoutHeight(it, false) } +
79-
ClickGui.listStep * (children.size - 1).coerceAtLeast(0) +
80-
ClickGui.padding * 2
78+
var height = ClickGui.padding * 2
79+
80+
val lastIndex = children.lastIndex
81+
children.forEachIndexed { i, it ->
82+
height += layoutHeight(it, false, i == lastIndex)
83+
}
84+
85+
height
8186
}
8287

8388
onShow {
@@ -111,8 +116,8 @@ class WindowContent(
111116
}
112117
}
113118

114-
private fun layoutHeight(layout: Layout, animate: Boolean): Double {
115-
var height = layout.renderHeight
119+
private fun layoutHeight(layout: Layout, animate: Boolean, isLast: Boolean = false): Double {
120+
var height = layout.renderHeight + ClickGui.listStep * (!isLast).toInt()
116121
val animated = layout as? AnimatedWindowChild ?: return height
117122

118123
height *= if (!animate) animated.staticShowAnimation

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

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.lambda.gui.component.core.FilledRect.Companion.rectBehind
2727
import com.lambda.gui.component.core.UIBuilder
2828
import com.lambda.gui.component.layout.Layout
2929
import com.lambda.gui.component.window.AnimatedWindowChild
30+
import com.lambda.gui.component.window.Window
3031
import com.lambda.util.Mouse
3132
import com.lambda.util.math.*
3233
import java.awt.Color
@@ -56,59 +57,66 @@ class ModuleLayout(
5657
// ToDo: replace with timer
5758
private var lastHover = 0L
5859

59-
init {
60-
rectBehind(titleBar) { // base rect with lowest y to avoid children overlying
61-
onUpdate {
62-
rectangle = this@ModuleLayout.rect.shrink(shrink)
63-
shade = ClickGui.backgroundShade
60+
val backgroundRect = rectBehind(titleBar) { // base rect with lowest y to avoid children overlying
61+
onUpdate {
62+
rectangle = this@ModuleLayout.rect.shrink(shrink)
63+
shade = ClickGui.backgroundShade
6464

65-
val openRev = 1.0 - openAnimation // 1.0 <-> 0.0
66-
val openRevSigned = openRev * 2 - 1 // 1.0 <-> -1.0
67-
val enableRev = 1.0 - enableAnimation // 1.0 <-> 0.0
65+
val openRev = 1.0 - openAnimation // 1.0 <-> 0.0
66+
val openRevSigned = openRev * 2 - 1 // 1.0 <-> -1.0
67+
val enableRev = 1.0 - enableAnimation // 1.0 <-> 0.0
6868

69-
var progress = enableAnimation
69+
var progress = enableAnimation
7070

71-
// hover: +0.1 to alpha if minimized, -0.1 to alpha if maximized
72-
progress += hoverAnimation * ClickGui.moduleHoverAccent * openRevSigned
71+
// hover: +0.1 to alpha if minimized, -0.1 to alpha if maximized
72+
progress += hoverAnimation * ClickGui.moduleHoverAccent * openRevSigned
7373

74-
// +0.4 to alpha if opened and disabled
75-
progress += openAnimation * ClickGui.moduleOpenAccent * enableRev
74+
// +0.4 to alpha if opened and disabled
75+
progress += openAnimation * ClickGui.moduleOpenAccent * enableRev
7676

77-
// interpolate and set the color
78-
setColor(lerp(progress, ClickGui.moduleDisabledColor, ClickGui.moduleEnabledColor))
79-
}
77+
// interpolate and set the color
78+
setColor(
79+
lerp(progress,
80+
ClickGui.moduleDisabledColor,
81+
ClickGui.moduleEnabledColor
82+
).multAlpha(showAnimation)
83+
)
84+
}
8085

81-
onUpdate {
82-
setRadius(hoverAnimation)
86+
onUpdate {
87+
setRadius(hoverAnimation)
8388

84-
if (isLast && ClickGui.autoResize) {
85-
leftBottomRadius = ClickGui.roundRadius - (ClickGui.padding + shrink)
86-
rightBottomRadius = leftBottomRadius
87-
}
89+
if (isLast && ClickGui.autoResize) {
90+
leftBottomRadius = ClickGui.roundRadius - (ClickGui.padding + shrink)
91+
rightBottomRadius = leftBottomRadius
8892
}
93+
}
8994

90-
rect { // hover fx
91-
onUpdate {
92-
val base = this@rect.owner as FilledRect
95+
rect { // hover fx
96+
onUpdate {
97+
val base = this@rect.owner as FilledRect
9398

94-
rectangle = base.rectangle
95-
shade = base.shade
99+
rectangle = base.rectangle
100+
shade = base.shade
96101

97-
setRadius(
98-
base.leftTopRadius,
99-
base.rightTopRadius,
100-
base.rightBottomRadius,
101-
base.leftBottomRadius
102-
)
102+
setRadius(
103+
base.leftTopRadius,
104+
base.rightTopRadius,
105+
base.rightBottomRadius,
106+
base.leftBottomRadius
107+
)
103108

104-
val hoverColor = Color.WHITE.setAlpha(
105-
ClickGui.moduleHoverAccent * hoverAnimation * (1.0 - openAnimation)
106-
)
109+
val hoverColor = Color.WHITE.setAlpha(
110+
ClickGui.moduleHoverAccent * hoverAnimation * (1.0 - openAnimation) * showAnimation
111+
)
107112

108-
setColorH(hoverColor.setAlpha(0.0), hoverColor)
109-
}
113+
setColorH(hoverColor.setAlpha(0.0), hoverColor)
110114
}
111115
}
116+
}
117+
118+
init {
119+
backgroundTint()
112120

113121
isMinimized = true
114122
height = 100.0
@@ -179,5 +187,47 @@ class ModuleLayout(
179187
@UIBuilder
180188
fun Layout.moduleLayout(module: Module) =
181189
ModuleLayout(this, module).apply(children::add)
190+
191+
/**
192+
* Used to dark the background of the settings a bit
193+
*
194+
* Not for external usage
195+
*/
196+
@UIBuilder
197+
fun Window.backgroundTint(tintTitleBar: Boolean = false) {
198+
check(this is SettingLayout<*, *> || this is ModuleLayout || this is ModuleWindow)
199+
200+
val base = this@backgroundTint
201+
202+
rectBehind(content) {
203+
onUpdate {
204+
rectangle = if (tintTitleBar) base.rect
205+
else Rect(titleBar.leftBottom, base.rightBottom)
206+
207+
setColor(Color.BLACK.setAlpha(0.08 * heightAnimation))
208+
209+
val round = (base as? ModuleLayout?)?.backgroundRect
210+
?: (base as? ModuleWindow)?.contentBackground
211+
212+
round?.let {
213+
leftBottomRadius = it.leftBottomRadius
214+
rightBottomRadius = it.rightBottomRadius
215+
}
216+
}
217+
218+
val bg = this
219+
220+
rect { // top shadow
221+
onUpdate {
222+
rectangle = Rect(
223+
bg.rectangle.leftTop,
224+
bg.rectangle.rightTop + Vec2d.BOTTOM * titleBar.renderHeight * 0.2
225+
)
226+
227+
setColorV(Color.BLACK.setAlpha(0.1 * heightAnimation), Color.BLACK.setAlpha(0.0))
228+
}
229+
}
230+
}
231+
}
182232
}
183233
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.gui.component.core.UIBuilder
2222
import com.lambda.gui.component.layout.Layout
2323
import com.lambda.gui.component.window.Window
2424
import com.lambda.gui.component.window.WindowContent
25+
import com.lambda.gui.impl.clickgui.ModuleLayout.Companion.backgroundTint
2526
import com.lambda.gui.impl.clickgui.ModuleLayout.Companion.moduleLayout
2627
import com.lambda.module.ModuleRegistry
2728
import com.lambda.util.math.Vec2d
@@ -32,6 +33,8 @@ class ModuleWindow(
3233
initialPosition: Vec2d
3334
) : Window(owner, tag.name, initialPosition, minimizing = Minimizing.Absolute, autoResize = AutoResize.ByConfig) {
3435
init {
36+
backgroundTint()
37+
3538
val modules = ModuleRegistry.modules
3639
.filter { it.defaultTags.firstOrNull() == tag }
3740
.map { module -> content.moduleLayout(module) }

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.module.modules.client.ClickGui
2222
import com.lambda.gui.component.HAlign
2323
import com.lambda.gui.component.layout.Layout
2424
import com.lambda.gui.component.window.AnimatedWindowChild
25+
import com.lambda.gui.impl.clickgui.ModuleLayout.Companion.backgroundTint
2526
import com.lambda.util.math.*
2627

2728
/**
@@ -56,7 +57,7 @@ abstract class SettingLayout <V : Any, T: AbstractSetting<V>> (
5657
if (!expandable) {
5758
overrideHeight(titleBar::renderHeight)
5859
content.destroy()
59-
}
60+
} else backgroundTint(true)
6061

6162
titleBar.textField.use {
6263
text = setting.name

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class BooleanButton(
6969
setRadius(100.0)
7070

7171
onUpdate {
72-
val knobStart = Rect.basedOn(checkBox.leftTop, Vec2d.ONE * checkBox.renderHeight)
73-
val knobEnd = Rect(checkBox.rightBottom - checkBox.renderHeight, checkBox.rightBottom)
72+
val knobStart = Rect.basedOn(checkBox.rectangle.leftTop, Vec2d.ONE * checkBox.rectangle.size.y)
73+
val knobEnd = Rect(checkBox.rectangle.rightBottom - checkBox.rectangle.size.y, checkBox.rectangle.rightBottom)
7474

7575
rectangle = lerp(
7676
lerp(showAnimation, 1.0 - activeAnimation, activeAnimation),
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.settings
19+
20+
import com.lambda.config.settings.comparable.EnumSetting
21+
import com.lambda.gui.component.HAlign
22+
import com.lambda.gui.component.core.TextField.Companion.textField
23+
import com.lambda.gui.component.core.UIBuilder
24+
import com.lambda.gui.component.layout.Layout
25+
import com.lambda.gui.impl.clickgui.ModuleLayout
26+
import com.lambda.gui.impl.clickgui.SettingLayout
27+
import com.lambda.util.Mouse
28+
29+
// ToDO: complete or transform to a slider
30+
class EnumSelector <T : Enum<T>>(
31+
owner: Layout,
32+
setting: EnumSetting<T>
33+
) : SettingLayout<T, EnumSetting<T>>(owner, setting, true) {
34+
35+
init {
36+
(owner.owner as? ModuleLayout)?.let {
37+
it.onWindowExpand {
38+
this@EnumSelector.isMinimized = true
39+
}
40+
41+
it.onWindowMinimize {
42+
this@EnumSelector.isMinimized = true
43+
}
44+
}
45+
46+
setting.enumValues.map { EnumEntry(this, it) }
47+
.onEach(content.children::add)
48+
49+
content.listify()
50+
}
51+
52+
class EnumEntry <T : Enum<T>>(
53+
private val base: EnumSelector<T>,
54+
private val entry: T
55+
) : Layout(base) {
56+
init {
57+
overrideSize(base::renderWidth) {
58+
base.titleBar.renderHeight * 0.8
59+
}
60+
61+
textField {
62+
text = entry.name
63+
textHAlignment = HAlign.CENTER
64+
65+
onUpdate {
66+
scale = base.titleBar.textField.scale
67+
color = base.titleBar.textField.color
68+
}
69+
}
70+
71+
onMouseClick(Mouse.Button.Left, Mouse.Action.Click) {
72+
base.settingDelegate = entry
73+
}
74+
}
75+
}
76+
77+
companion object {
78+
/**
79+
* Creates an [EnumSelector] - visual representation of the [EnumSetting]
80+
*/
81+
@UIBuilder
82+
fun <T: Enum<T>> Layout.enumSetting(setting: EnumSetting<T>) =
83+
EnumSelector(this, setting).apply(children::add)
84+
}
85+
}

common/src/main/kotlin/com/lambda/module/modules/client/ClickGui.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object ClickGui : Module(
4242

4343
val backgroundTint by setting("Background Tint", Color.BLACK.setAlpha(0.4))
4444

45-
val titleBackgroundColor by setting("Title Background Color", Color(60, 60, 60))
45+
val titleBackgroundColor by setting("Title Background Color", Color(80, 80, 80))
4646
val backgroundColor by setting("Background Color", titleBackgroundColor)
4747
val backgroundShade by setting("Background Shade", true)
4848

@@ -54,7 +54,7 @@ object ClickGui : Module(
5454
val fontOffset by setting("Font Offset", 4.0, 0.0..5.0, 0.1)
5555
val dockingGridSize by setting("Docking Grid Size", 1.0, 0.1..10.0, 0.1)
5656

57-
val moduleEnabledColor by setting("Module Enabled Color", Color.WHITE.setAlpha(0.5))
57+
val moduleEnabledColor by setting("Module Enabled Color", Color.WHITE.setAlpha(0.4))
5858
val moduleDisabledColor by setting("Module Disabled Color", Color.WHITE.setAlpha(0.0))
5959
val moduleHoverAccent by setting("Module Hover Accent", 0.15, 0.0..0.3, 0.01)
6060
val moduleOpenAccent by setting("Module Open Accent", 0.3, 0.0..0.5, 0.01)

0 commit comments

Comments
 (0)