diff options
author | Linnea Gräf <nea@nea.moe> | 2024-10-13 19:53:10 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-10-13 19:53:10 +0200 |
commit | 87b851373071490bb371ba1160f85f0341579cb2 (patch) | |
tree | 45d267d83baad2e3cb6caad6127f6a4d40e7fcbf /src/main/kotlin/gui/config/KeyBindingStateManager.kt | |
parent | e6142bb93619dee768fc18b87ffdd28558d4bcab (diff) | |
download | Firmament-87b851373071490bb371ba1160f85f0341579cb2.tar.gz Firmament-87b851373071490bb371ba1160f85f0341579cb2.tar.bz2 Firmament-87b851373071490bb371ba1160f85f0341579cb2.zip |
Add YACL config menu
Diffstat (limited to 'src/main/kotlin/gui/config/KeyBindingStateManager.kt')
-rw-r--r-- | src/main/kotlin/gui/config/KeyBindingStateManager.kt | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/main/kotlin/gui/config/KeyBindingStateManager.kt b/src/main/kotlin/gui/config/KeyBindingStateManager.kt new file mode 100644 index 0000000..cc8178d --- /dev/null +++ b/src/main/kotlin/gui/config/KeyBindingStateManager.kt @@ -0,0 +1,108 @@ +package moe.nea.firmament.gui.config + +import org.lwjgl.glfw.GLFW +import net.minecraft.text.Text +import net.minecraft.util.Formatting +import moe.nea.firmament.keybindings.SavedKeyBinding + +class KeyBindingStateManager( + val value: () -> SavedKeyBinding, + val setValue: (key: SavedKeyBinding) -> Unit, + val blur: () -> Unit, + val requestFocus: () -> Unit, +) { + var editing = false + var lastPressed = 0 + var lastPressedNonModifier = 0 + var label: Text = Text.literal("") + + fun onClick() { + if (editing) { + editing = false + blur() + } else { + editing = true + requestFocus() + } + updateLabel() + } + + fun keyboardEvent(keyCode: Int, pressed: Boolean): Boolean { + return if (pressed) onKeyPressed(keyCode, SavedKeyBinding.getModInt()) + else onKeyReleased(keyCode, SavedKeyBinding.getModInt()) + } + + fun onKeyPressed(ch: Int, modifiers: Int): Boolean { + if (!editing) { + return false + } + if (ch == GLFW.GLFW_KEY_ESCAPE) { + lastPressedNonModifier = 0 + editing = false + lastPressed = 0 + setValue(SavedKeyBinding(GLFW.GLFW_KEY_UNKNOWN)) + updateLabel() + blur() + return true + } + if (ch == GLFW.GLFW_KEY_LEFT_SHIFT || ch == GLFW.GLFW_KEY_RIGHT_SHIFT + || ch == GLFW.GLFW_KEY_LEFT_ALT || ch == GLFW.GLFW_KEY_RIGHT_ALT + || ch == GLFW.GLFW_KEY_LEFT_CONTROL || ch == GLFW.GLFW_KEY_RIGHT_CONTROL + ) { + lastPressed = ch + } else { + setValue(SavedKeyBinding( + ch, modifiers + )) + editing = false + blur() + lastPressed = 0 + lastPressedNonModifier = 0 + } + updateLabel() + return true + } + + fun onLostFocus() { + lastPressedNonModifier = 0 + editing = false + lastPressed = 0 + updateLabel() + } + + fun onKeyReleased(ch: Int, modifiers: Int): Boolean { + if (!editing) + return false + if (lastPressedNonModifier == ch || (lastPressedNonModifier == 0 && ch == lastPressed)) { + setValue(SavedKeyBinding(ch, modifiers)) + editing = false + blur() + lastPressed = 0 + lastPressedNonModifier = 0 + } + updateLabel() + return true + } + + fun updateLabel() { + var stroke = value().format() + if (editing) { + stroke = Text.literal("") + val (shift, ctrl, alt) = 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 + } + + +} |