aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui/config
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/gui/config')
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt41
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt2
2 files changed, 26 insertions, 17 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt
index d347101..13c6592 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt
@@ -17,16 +17,17 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
-import net.minecraft.client.util.InputUtil
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import moe.nea.firmament.gui.FirmButtonComponent
import moe.nea.firmament.keybindings.FirmamentKeyBindings
import moe.nea.firmament.keybindings.SavedKeyBinding
-class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : ManagedConfig.OptionHandler<SavedKeyBinding> {
- init {
- FirmamentKeyBindings.registerKeyBinding(name, 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? {
@@ -44,19 +45,22 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
var label: String = ""
var button: FirmButtonComponent? = null
fun updateLabel() {
- val stroke = Text.literal("")
- if (opt.value.shift) {
- stroke.append("SHIFT + ") // TODO: translations?
- }
- if (opt.value.alt) {
- stroke.append("ALT + ")
- }
- if (opt.value.ctrl) {
- stroke.append("CTRL + ")
- }
- stroke.append(InputUtil.Type.KEYSYM.createFromCode(opt.value.keyCode).localizedText)
- if (editing)
+ var stroke = opt.value.format()
+ if (editing) {
+ stroke = Text.literal("")
+ val (shift, alt, ctrl) = SavedKeyBinding.getMods(SavedKeyBinding.getModInt())
+ if (shift) {
+ stroke.append("SHIFT + ")
+ }
+ if (alt) {
+ stroke.append("ALT + ")
+ }
+ if (ctrl) {
+ stroke.append("CTRL + ")
+ }
+ stroke.append("???")
stroke.styled { it.withColor(Formatting.YELLOW) }
+ }
label = (stroke).string
managedConfig.save()
}
@@ -79,7 +83,7 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
}) {
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
if (event is KeyboardEvent.KeyPressed) {
- if (event.pressed) onKeyPressed(event.keycode, SavedKeyBinding.getModInt())
+ return if (event.pressed) onKeyPressed(event.keycode, SavedKeyBinding.getModInt())
else onKeyReleased(event.keycode, SavedKeyBinding.getModInt())
}
return super.keyboardEvent(event, context)
@@ -100,6 +104,7 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
lastPressed = 0
opt.value = SavedKeyBinding(GLFW.GLFW_KEY_UNKNOWN)
updateLabel()
+ blur()
return true
}
if (ch == GLFW.GLFW_KEY_LEFT_SHIFT || ch == GLFW.GLFW_KEY_RIGHT_SHIFT
@@ -112,6 +117,7 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
ch, modifiers
)
editing = false
+ blur()
lastPressed = 0
lastPressedNonModifier = 0
}
@@ -132,6 +138,7 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
if (lastPressedNonModifier == ch || (lastPressedNonModifier == 0 && ch == lastPressed)) {
opt.value = SavedKeyBinding(ch, modifiers)
editing = false
+ blur()
lastPressed = 0
lastPressedNonModifier = 0
}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
index 104cd52..2caea99 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
@@ -34,6 +34,7 @@ import moe.nea.firmament.util.ScreenUtil.setScreenLater
abstract class ManagedConfig(override val name: String) : ManagedConfigElement() {
interface OptionHandler<T : Any> {
+ fun initOption(opt: ManagedOption<T>) {}
fun toJson(element: T): JsonElement?
fun fromJson(element: JsonElement): T
fun emitGuiElements(opt: ManagedOption<T>, guiAppender: GuiAppender)
@@ -74,6 +75,7 @@ abstract class ManagedConfig(override val name: String) : ManagedConfigElement()
): ManagedOption<T> {
if (propertyName in allOptions) error("Cannot register the same name twice")
return ManagedOption(this, propertyName, default, handler).also {
+ it.handler.initOption(it)
it.load(data)
allOptions[propertyName] = it
sortedOptions.add(it)