Skip to content

Commit 0b72461

Browse files
committed
Persistent GUI configuration
1 parent 391e9e5 commit 0b72461

File tree

15 files changed

+119
-46
lines changed

15 files changed

+119
-46
lines changed

common/src/main/kotlin/com/lambda/Lambda.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package com.lambda
33
import com.google.gson.Gson
44
import com.google.gson.GsonBuilder
55
import com.lambda.config.serializer.*
6+
import com.lambda.config.serializer.gui.TagWindowSerializer
67
import com.lambda.core.Loader
8+
import com.lambda.gui.impl.clickgui.windows.TagWindow
79
import com.lambda.module.tag.ModuleTag
810
import com.lambda.util.KeyCode
911
import net.minecraft.block.Block
@@ -24,6 +26,7 @@ object Lambda {
2426
val gson: Gson = GsonBuilder()
2527
.setPrettyPrinting()
2628
.registerTypeAdapter(ModuleTag::class.java, ModuleTagSerializer)
29+
.registerTypeAdapter(TagWindow::class.java, TagWindowSerializer)
2730
.registerTypeAdapter(KeyCode::class.java, KeyCodeSerializer)
2831
.registerTypeAdapter(Color::class.java, ColorSerializer)
2932
.registerTypeAdapter(BlockPos::class.java, BlockPosSerializer)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
149149
* The type parameter [T] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
150150
*
151151
* @param name The unique identifier for the setting.
152-
* @param defaultValue The default [List] value of type [T] for the setting.
152+
* @param defaultValue The default [MutableList] value of type [T] for the setting.
153153
* @param description A brief explanation of the setting's purpose and behavior.
154154
* @param visibility A lambda expression that determines the visibility status of the setting.
155155
*
@@ -162,7 +162,7 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
162162
*/
163163
inline fun <reified T : Any> setting(
164164
name: String,
165-
defaultValue: List<T>,
165+
defaultValue: MutableList<T>,
166166
description: String = "",
167167
noinline visibility: () -> Boolean = { true },
168168
) = ListSetting(name, defaultValue, description, visibility).also {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.lambda.config.configurations
2+
3+
import com.lambda.config.Configuration
4+
import com.lambda.util.FolderRegister
5+
6+
object GuiConfig : Configuration() {
7+
override val configName = "gui"
8+
override val primary = FolderRegister.config.resolve("$configName.json")
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.lambda.config.serializer.gui
2+
3+
import com.google.gson.*
4+
import com.lambda.gui.impl.clickgui.windows.TagWindow
5+
import com.lambda.util.math.Vec2d
6+
import java.lang.reflect.Type
7+
8+
object TagWindowSerializer : JsonSerializer<TagWindow>, JsonDeserializer<TagWindow> {
9+
10+
override fun serialize(
11+
src: TagWindow?,
12+
typeOfSrc: Type?,
13+
context: JsonSerializationContext?,
14+
): JsonElement = src?.let {
15+
JsonObject().apply {
16+
addProperty("title", it.title)
17+
addProperty("width", it.width)
18+
addProperty("height", it.height)
19+
addProperty("isOpen", it.isOpen)
20+
add("position", JsonArray().apply {
21+
add(it.position.x)
22+
add(it.position.y)
23+
})
24+
}
25+
} ?: JsonNull.INSTANCE
26+
27+
override fun deserialize(
28+
json: JsonElement?,
29+
typeOfT: Type?,
30+
context: JsonDeserializationContext?,
31+
) = json?.asJsonObject?.let {
32+
TagWindow().apply {
33+
width = it["width"].asDouble
34+
height = it["height"].asDouble
35+
isOpen = it["isOpen"].asBoolean
36+
position = Vec2d(
37+
it["position"].asJsonArray[0].asDouble,
38+
it["position"].asJsonArray[1].asDouble
39+
)
40+
}
41+
} ?: TagWindow()
42+
}

common/src/main/kotlin/com/lambda/config/settings/collections/ListSetting.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import com.lambda.config.AbstractSetting
77

88
class ListSetting<T>(
99
override val name: String,
10-
defaultValue: List<T>,
10+
defaultValue: MutableList<T>,
1111
description: String,
1212
visibility: () -> Boolean,
13-
) : AbstractSetting<List<T>>(
13+
) : AbstractSetting<MutableList<T>>(
1414
defaultValue,
1515
description,
1616
visibility
1717
) {
1818
override fun loadFromJson(serialized: JsonElement) {
19-
val listType = object : TypeToken<List<T>>() {}.type
19+
val listType = object : TypeToken<MutableList<T>>() {}.type
2020
value = gson.fromJson(serialized, listType)
2121
}
2222
}

common/src/main/kotlin/com/lambda/core/Loader.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package com.lambda.core
33
import com.lambda.Lambda
44
import com.lambda.Lambda.LOG
55
import com.lambda.command.CommandManager
6+
import com.lambda.config.configurations.GuiConfig
67
import com.lambda.graphics.renderer.gui.font.LambdaFont
8+
import com.lambda.gui.impl.clickgui.LambdaClickGui
79
import com.lambda.interaction.PlayerPacketManager
810
import com.lambda.interaction.RotationManager
911
import com.lambda.module.ModuleRegistry
@@ -35,5 +37,7 @@ object Loader {
3537
}
3638

3739
LOG.info("${Lambda.MOD_NAME} ${Lambda.VERSION} was successfully initialized (${initTime}ms)")
40+
41+
LambdaClickGui
3842
}
3943
}

common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lambda.gui.api
22

33
import com.lambda.Lambda.mc
4-
import com.lambda.event.EventFlow
4+
import com.lambda.event.EventFlow.syncListeners
55
import com.lambda.event.events.RenderEvent
66
import com.lambda.event.events.TickEvent
77
import com.lambda.event.listener.UnsafeListener
@@ -17,7 +17,10 @@ import net.minecraft.client.gui.screen.Screen
1717
import net.minecraft.text.Text
1818

1919
@Suppress("LeakingThis")
20-
abstract class LambdaGui(override val name: String, private val owner: Module? = null) : Screen(Text.of(name)), IComponent, Nameable {
20+
abstract class LambdaGui(
21+
override val name: String,
22+
private val owner: Module? = null
23+
) : Screen(Text.of(name)), IComponent, Nameable {
2124
private var screenSize = Vec2d.ZERO
2225

2326
private val renderListener = UnsafeListener(0, this, false) { event ->
@@ -38,15 +41,15 @@ abstract class LambdaGui(override val name: String, private val owner: Module? =
3841
fun show() {
3942
mc.currentScreen?.close()
4043

41-
recordRenderCall { // wait for previous screen to be closed
44+
recordRenderCall { // wait for the previous screen to be closed
4245
mc.setScreen(this)
4346
}
4447
}
4548

4649
final override fun onDisplayed() {
4750
onShow()
4851

49-
with(EventFlow.syncListeners) {
52+
with(syncListeners) {
5053
subscribe<RenderEvent.GUI.Scaled>(renderListener)
5154
subscribe<TickEvent.Pre>(tickListener)
5255
}
@@ -60,7 +63,7 @@ abstract class LambdaGui(override val name: String, private val owner: Module? =
6063
owner?.disable()
6164
mc.currentScreen = this
6265

63-
with(EventFlow.syncListeners) {
66+
with(syncListeners) {
6467
unsubscribe(renderListener)
6568
unsubscribe(tickListener)
6669
}

common/src/main/kotlin/com/lambda/gui/api/component/InteractiveComponent.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ abstract class InteractiveComponent : IComponent, IRectComponent {
2929
hovered = rect.contains(mouse)
3030
}
3131

32-
override fun onMouseClick(button: Mouse.Button, action: Mouse.Action, mouse: Vec2d) {
33-
activeMouseButton = button.takeUnless { it.isMainButton && action == Mouse.Action.Click }
32+
override fun onMouseClick(
33+
button: Mouse.Button, action: Mouse.Action, mouse: Vec2d
34+
) {
35+
activeMouseButton = button.takeUnless {
36+
it.isMainButton && action == Mouse.Action.Click
37+
}
3438
pressed = hovered && button.isMainButton && action == Mouse.Action.Click
3539
}
3640
}

common/src/main/kotlin/com/lambda/gui/api/component/WindowComponent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class WindowComponent <T : ChildComponent> : InteractiveComponent(), IL
2424

2525
var position = Vec2d.ZERO
2626

27-
private var isOpen = true
27+
var isOpen = true
2828
private var dragOffset: Vec2d? = null
2929
private val padding get() = ClickGui.windowPadding
3030

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package com.lambda.gui.impl.clickgui
22

3+
import com.lambda.command.CommandManager.setting
4+
import com.lambda.config.Configurable
5+
import com.lambda.config.configurations.GuiConfig
36
import com.lambda.gui.api.LambdaGui
47
import com.lambda.gui.api.component.WindowComponent
58
import com.lambda.gui.api.component.core.list.IListComponent
6-
import com.lambda.gui.api.component.sub.ButtonComponent
79
import com.lambda.gui.impl.clickgui.windows.TagWindow
8-
import com.lambda.module.ModuleRegistry
910
import com.lambda.module.modules.client.ClickGui
10-
import com.lambda.util.Mouse
11-
import com.lambda.util.math.Vec2d
1211

13-
class LambdaClickGui : LambdaGui("Lambda ClickGui", ClickGui), IListComponent<WindowComponent<*>> {
14-
override val children = mutableListOf<WindowComponent<*>>()
12+
object LambdaClickGui : LambdaGui("ClickGui", ClickGui), IListComponent<WindowComponent<*>> {
13+
private val windows = setting("windows", mutableListOf<WindowComponent<*>>(TagWindow()))
14+
override val children: MutableList<WindowComponent<*>> get() = windows.value
1515

1616
init {
17-
children.add(TagWindow())
17+
object : Configurable(GuiConfig) {
18+
init { settings.add(windows) }
19+
20+
override val name = this@LambdaClickGui.name
21+
}
1822
}
1923
}

0 commit comments

Comments
 (0)