diff options
author | nea <nea@nea.moe> | 2023-08-25 14:18:43 +0200 |
---|---|---|
committer | nea <nea@nea.moe> | 2023-08-25 14:18:43 +0200 |
commit | 784231941661a3108549a1b5cd499bc5f7de2e46 (patch) | |
tree | 1e7c85001aef50af5d0491b2d97ae4fa09bb87d7 /src/main/kotlin/moe/nea/firmament/keybindings | |
parent | a79452c25406e17b81dd0ed1209f590d01991c68 (diff) | |
download | Firmament-784231941661a3108549a1b5cd499bc5f7de2e46.tar.gz Firmament-784231941661a3108549a1b5cd499bc5f7de2e46.tar.bz2 Firmament-784231941661a3108549a1b5cd499bc5f7de2e46.zip |
Add better key binding support
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/keybindings')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt | 23 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt | 36 |
2 files changed, 51 insertions, 8 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt b/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt index a7f25c0..770f4f6 100644 --- a/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt +++ b/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt @@ -7,17 +7,24 @@ package moe.nea.firmament.keybindings import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper -import org.lwjgl.glfw.GLFW import net.minecraft.client.option.KeyBinding import net.minecraft.client.util.InputUtil +import moe.nea.firmament.features.inventory.SlotLocking +import moe.nea.firmament.gui.config.ManagedConfig object FirmamentKeyBindings { - val SLOT_LOCKING = KeyBindingHelper.registerKeyBinding( - KeyBinding( - "firmament.key.slotlocking", - InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_L, - "firmament.key.category" + fun registerKeyBinding(name: String, config: ManagedConfig) { + val vanillaKeyBinding = KeyBindingHelper.registerKeyBinding( + KeyBinding( + name, + InputUtil.Type.KEYSYM, + -1, + "firmament.key.category" + ) ) - ) + keyBindings[vanillaKeyBinding] = config + } + + val keyBindings = mutableMapOf<KeyBinding, ManagedConfig>() + } diff --git a/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt b/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt new file mode 100644 index 0000000..f73eb49 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt @@ -0,0 +1,36 @@ +package moe.nea.firmament.keybindings + +import kotlinx.serialization.Serializable +import org.lwjgl.glfw.GLFW + +@Serializable +data class SavedKeyBinding( + val keyCode: Int, + val shift: Boolean = false, + val ctrl: Boolean = false, + val alt: Boolean = false, +) : IKeyBinding { + constructor(keyCode: Int, mods: Triple<Boolean, Boolean, Boolean>) : this( + keyCode, + mods.first && keyCode != GLFW.GLFW_KEY_LEFT_SHIFT && keyCode != GLFW.GLFW_KEY_RIGHT_SHIFT, + mods.second && keyCode != GLFW.GLFW_KEY_LEFT_CONTROL && keyCode != GLFW.GLFW_KEY_RIGHT_CONTROL, + mods.third && keyCode != GLFW.GLFW_KEY_LEFT_ALT && keyCode != GLFW.GLFW_KEY_RIGHT_ALT, + ) + + constructor(keyCode: Int, mods: Int) : this(keyCode, getMods(mods)) + + companion object { + fun getMods(modifiers: Int): Triple<Boolean, Boolean, Boolean> { + return Triple( + modifiers and GLFW.GLFW_MOD_SHIFT != 0, + modifiers and GLFW.GLFW_MOD_CONTROL != 0, + modifiers and GLFW.GLFW_MOD_ALT != 0 + ) + } + } + + override fun matches(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { + return keyCode == this.keyCode && getMods(modifiers) == Triple(shift, ctrl, alt) + } + +} |