blob: 3c08da27231e48b092ee2c1806d8ef074bfe214f (
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
|
package moe.nea.firmament.gui.config
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import moe.nea.firmament.gui.FirmButtonComponent
import moe.nea.firmament.keybindings.FirmamentKeyBindings
import moe.nea.firmament.keybindings.SavedKeyBinding
import moe.nea.firmament.util.data.ManagedConfig
class KeyBindingHandler(val name: String, val managedConfig: ManagedConfig) :
ManagedConfig.OptionHandler<SavedKeyBinding> {
override fun initOption(opt: ManagedOption<SavedKeyBinding>) {
FirmamentKeyBindings.registerKeyBinding(name, opt)
}
override fun toJson(element: SavedKeyBinding): JsonElement? {
return Json.encodeToJsonElement(element)
}
override fun fromJson(element: JsonElement): SavedKeyBinding {
return Json.decodeFromJsonElement(element)
}
fun createButtonComponent(opt: ManagedOption<SavedKeyBinding>): FirmButtonComponent {
lateinit var button: FirmButtonComponent
val sm = KeyBindingStateManager(
{ opt.value },
{
opt.value = it
opt.element.markDirty()
},
{ button.blur() },
{ button.requestFocus() }
)
button = sm.createButton()
sm.updateLabel()
return button
}
override fun emitGuiElements(opt: ManagedOption<SavedKeyBinding>, guiAppender: GuiAppender) {
guiAppender.appendLabeledRow(opt.labelText, createButtonComponent(opt))
}
}
|