aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
blob: 874329329939708345c6ceaf244a1916a5216772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.gui.config

import io.github.cottonmc.cotton.gui.client.CottonClientScreen
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
import io.github.cottonmc.cotton.gui.widget.WBox
import io.github.cottonmc.cotton.gui.widget.WButton
import io.github.cottonmc.cotton.gui.widget.WLabel
import io.github.cottonmc.cotton.gui.widget.data.Axis
import io.github.cottonmc.cotton.gui.widget.data.Insets
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import moe.nea.firmament.Firmament
import moe.nea.firmament.gui.WTightScrollPanel
import moe.nea.firmament.keybindings.SavedKeyBinding
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.ScreenUtil.setScreenLater
import moe.nea.jarvis.api.Point
import net.minecraft.client.gui.screen.Screen
import net.minecraft.text.Text
import kotlin.io.path.createDirectories
import kotlin.io.path.readText
import kotlin.io.path.writeText
import kotlin.time.Duration

abstract class ManagedConfig(override val name: String) : ManagedConfigElement() {

    interface OptionHandler<T : Any> {
        fun toJson(element: T): JsonElement?
        fun fromJson(element: JsonElement): T
        fun emitGuiElements(opt: ManagedOption<T>, guiAppender: GuiAppender)
    }

    val file = Firmament.CONFIG_DIR.resolve("$name.json")
    val data: JsonObject by lazy {
        try {
            Firmament.json.decodeFromString(
                file.readText()
            )
        } catch (e: Exception) {
            Firmament.logger.info("Could not read config $name. Loading empty config.")
            JsonObject(mutableMapOf())
        }
    }

    fun save() {
        val data = JsonObject(allOptions.mapNotNull { (key, value) ->
            value.toJson()?.let {
                key to it
            }
        }.toMap())
        file.parent.createDirectories()
        file.writeText(Firmament.json.encodeToString(data))
    }


    val allOptions = mutableMapOf<String, ManagedOption<*>>()
    val sortedOptions = mutableListOf<ManagedOption<*>>()

    private var latestGuiAppender: GuiAppender? = null

    protected fun <T : Any> option(
        propertyName: String,
        default: () -> T,
        handler: OptionHandler<T>
    ): ManagedOption<T> {
        if (propertyName in allOptions) error("Cannot register the same name twice")
        return ManagedOption(this, propertyName, default, handler).also {
            it.load(data)
            allOptions[propertyName] = it
            sortedOptions.add(it)
        }
    }

    protected fun toggle(propertyName: String, default: () -> Boolean): ManagedOption<Boolean> {
        return option(propertyName, default, BooleanHandler(this))
    }

    protected fun duration(
        propertyName: String,
        min: Duration,
        max: Duration,
        default: () -> Duration,
    ): ManagedOption<Duration> {
        return option(propertyName, default, DurationHandler(this, min, max))
    }


    protected fun position(
        propertyName: String,
        width: Int,
        height: Int,
        default: () -> Point,
    ): ManagedOption<HudMeta> {
        val label = Text.translatable("firmament.config.${name}.${propertyName}")
        return option(propertyName, {
            val p = default()
            HudMeta(HudPosition(p.x, p.y, 1F), label, width, height)
        }, HudMetaHandler(this, label, width, height))
    }

    protected fun keyBinding(
        propertyName: String,
        default: () -> Int,
    ): ManagedOption<SavedKeyBinding> = keyBindingWithDefaultModifiers(propertyName) { SavedKeyBinding(default()) }

    protected fun keyBindingWithDefaultModifiers(
        propertyName: String,
        default: () -> SavedKeyBinding,
    ): ManagedOption<SavedKeyBinding> {
        return option(propertyName, default, KeyBindingHandler("firmament.config.${name}.${propertyName}", this))
    }

    protected fun integer(
        propertyName: String,
        min: Int,
        max: Int,
        default: () -> Int,
    ): ManagedOption<Int> {
        return option(propertyName, default, IntegerHandler(this, min, max))
    }

    protected fun button(propertyName: String, runnable: () -> Unit): ManagedOption<Unit> {
        return option(propertyName, { }, ClickHandler(this, runnable))
    }

    protected fun string(propertyName: String, default: () -> String): ManagedOption<String> {
        return option(propertyName, default, StringHandler(this))
    }


    fun reloadGui() {
        latestGuiAppender?.reloadables?.forEach { it() }
    }

    fun getConfigEditor(parent: Screen? = null): CottonClientScreen {
        val lwgd = LightweightGuiDescription()
        var screen: Screen? = null
        val guiapp = GuiAppender(400) { requireNotNull(screen) { "Screen Accessor called too early" } }
        latestGuiAppender = guiapp
        guiapp.panel.insets = Insets.ROOT_PANEL
        guiapp.appendFullRow(WBox(Axis.HORIZONTAL).also {
            it.add(WButton(Text.literal("←")).also {
                it.setOnClick {
                    if (parent != null) {
                        setScreenLater(parent)
                    } else {
                        AllConfigsGui.showAllGuis()
                    }
                }
            })
            it.add(WLabel(Text.translatable("firmament.config.${name}")).also {
                it.verticalAlignment = VerticalAlignment.CENTER
            })
        })
        sortedOptions.forEach { it.appendToGui(guiapp) }
        guiapp.reloadables.forEach { it() }
        lwgd.setRootPanel(WTightScrollPanel(guiapp.panel).also {
            it.setSize(400, 300)
        })
        screen =
            object : CottonClientScreen(lwgd) {
                override fun init() {
                    latestGuiAppender = guiapp
                    super.init()
                }

                override fun close() {
                    latestGuiAppender = null
                    MC.screen = parent
                }
            }
        return screen
    }

    fun showConfigEditor(parent: Screen? = null) {
        setScreenLater(getConfigEditor(parent))
    }

}