aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/gui/ConfigGui.kt
blob: 3792d80f1627b0959a80c9d9db36eb83341b1534 (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
package moe.nea.notenoughupdates.gui

import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
import io.github.cottonmc.cotton.gui.widget.WButton
import io.github.cottonmc.cotton.gui.widget.WLabel
import io.github.cottonmc.cotton.gui.widget.WTextField
import io.github.cottonmc.cotton.gui.widget.WToggleButton
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment
import io.github.cottonmc.cotton.gui.widget.data.Insets
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
import moe.nea.notenoughupdates.NotEnoughUpdates
import moe.nea.notenoughupdates.util.ConfigHolder
import net.minecraft.text.Text
import kotlin.reflect.KMutableProperty1

class ConfigGui<K>(val holder: ConfigHolder<K>, val build: ConfigGui<K>.() -> Unit) : LightweightGuiDescription() {
    private val root = WGridPanelWithPadding(verticalPadding = 4)
    private val reloadables = mutableListOf<(() -> Unit)>()

    init {
        setRootPanel(root)
        root.insets = Insets.ROOT_PANEL
        build()
        reload()
    }

    fun title(text: Text) {
        if (col != 0) {
            NotEnoughUpdates.logger.warn("Set title not at the top of the ConfigGui")
        }
        val label = WLabel(text)
        label.verticalAlignment = VerticalAlignment.TOP
        label.horizontalAlignment = HorizontalAlignment.CENTER
        root.add(label, 0, col, 11, 1)
        col++
    }

    private fun label(text: Text) {
        val label = WLabel(text)
        label.verticalAlignment = VerticalAlignment.CENTER
        root.add(label, 0, col, 5, 1)
    }

    fun toggle(text: Text, prop: KMutableProperty1<K, Boolean>) {
        val toggle = WToggleButton(text)
        reloadables.add { toggle.toggle = prop.get(holder.config) }
        toggle.setOnToggle {
            prop.set(holder.config, true)
            holder.markDirty()
        }
        root.add(toggle, 5, col, 6, 1)
        label(text)
        col++
    }

    fun button(text: Text, buttonText: Text, runnable: () -> Unit) {
        val button = WButton(buttonText)
        button.setOnClick {
            runnable.invoke()
        }
        root.add(button, 5, col, 6, 1)
        label(text)
        col++
    }

    fun textfield(
        text: Text,
        background: Text,
        prop: KMutableProperty1<K, String>,
        maxLength: Int = 255
    ) {
        val textfield = WTextField(background)
        textfield.isEditable = true
        reloadables.add {
            textfield.text = prop.get(holder.config)
        }
        textfield.maxLength = maxLength
        textfield.setChangedListener {
            prop.set(holder.config, it)
            holder.markDirty()
        }
        root.add(textfield, 5, col, 6, 11)
        label(text)
        col++
    }

    fun reload() {
        reloadables.forEach { it.invoke() }
    }

    private var col = 0


}