diff options
author | Linnea Gräf <nea@nea.moe> | 2024-07-05 22:52:06 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-07-05 22:52:06 +0200 |
commit | dc3adecc1b34758ccd62b5086d0052ef70555282 (patch) | |
tree | dc487f4c14901c5dc59cabbaceaeae13c7feecf0 /src/main/kotlin/moe/nea | |
parent | 3d792dbef7c7e0f186e29f2e3999f99c2c14f5d7 (diff) | |
download | Firmament-dc3adecc1b34758ccd62b5086d0052ef70555282.tar.gz Firmament-dc3adecc1b34758ccd62b5086d0052ef70555282.tar.bz2 Firmament-dc3adecc1b34758ccd62b5086d0052ef70555282.zip |
Improve keybinding support
Diffstat (limited to 'src/main/kotlin/moe/nea')
4 files changed, 47 insertions, 20 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) diff --git a/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt b/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt index a5844e5..3fbdd84 100644 --- a/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt +++ b/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt @@ -9,10 +9,11 @@ package moe.nea.firmament.keybindings import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper import net.minecraft.client.option.KeyBinding import net.minecraft.client.util.InputUtil -import moe.nea.firmament.gui.config.ManagedConfig +import moe.nea.firmament.gui.config.KeyBindingHandler +import moe.nea.firmament.gui.config.ManagedOption object FirmamentKeyBindings { - fun registerKeyBinding(name: String, config: ManagedConfig) { + fun registerKeyBinding(name: String, config: ManagedOption<SavedKeyBinding>) { val vanillaKeyBinding = KeyBindingHelper.registerKeyBinding( KeyBinding( name, @@ -24,6 +25,6 @@ object FirmamentKeyBindings { keyBindings[vanillaKeyBinding] = config } - val keyBindings = mutableMapOf<KeyBinding, ManagedConfig>() + val keyBindings = mutableMapOf<KeyBinding, ManagedOption<SavedKeyBinding>>() } diff --git a/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt b/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt index 56865cc..f07cd29 100644 --- a/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt +++ b/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt @@ -10,6 +10,7 @@ import org.lwjgl.glfw.GLFW import kotlinx.serialization.Serializable import net.minecraft.client.MinecraftClient import net.minecraft.client.util.InputUtil +import net.minecraft.text.Text import moe.nea.firmament.util.MC @Serializable @@ -87,4 +88,20 @@ data class SavedKeyBinding( return keyCode == this.keyCode && getMods(modifiers) == Triple(shift, ctrl, alt) } + fun format(): Text { + val stroke = Text.literal("") + if (ctrl) { + stroke.append("CTRL + ") + } + if (alt) { + stroke.append("ALT + ") + } + if (shift) { + stroke.append("SHIFT + ") // TODO: translations? + } + + stroke.append(InputUtil.Type.KEYSYM.createFromCode(keyCode).localizedText) + return stroke + } + } |