diff options
author | Linnea Gräf <nea@nea.moe> | 2025-06-04 01:06:51 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-06-04 01:06:51 +0200 |
commit | 2a1631dadfc0f78eb3b7768f9bca3f9fa1ea619e (patch) | |
tree | 422f0d17f4ee40e930a1a84ca9f0d76b4acd3e7c /src/main/kotlin/features/macros/KeyComboTrie.kt | |
parent | 9ad691bc1ba5f50d89a9c99cbf950fe2390a5a44 (diff) | |
download | Firmament-2a1631dadfc0f78eb3b7768f9bca3f9fa1ea619e.tar.gz Firmament-2a1631dadfc0f78eb3b7768f9bca3f9fa1ea619e.tar.bz2 Firmament-2a1631dadfc0f78eb3b7768f9bca3f9fa1ea619e.zip |
feat: Add basic combo buttons (without editor for now)
Diffstat (limited to 'src/main/kotlin/features/macros/KeyComboTrie.kt')
-rw-r--r-- | src/main/kotlin/features/macros/KeyComboTrie.kt | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/kotlin/features/macros/KeyComboTrie.kt b/src/main/kotlin/features/macros/KeyComboTrie.kt new file mode 100644 index 0000000..5c14bcd --- /dev/null +++ b/src/main/kotlin/features/macros/KeyComboTrie.kt @@ -0,0 +1,57 @@ +package moe.nea.firmament.features.macros + +import net.minecraft.text.Text +import moe.nea.firmament.keybindings.SavedKeyBinding + +sealed interface KeyComboTrie { + val label: Text + + companion object { + fun fromComboList( + combos: List<ComboKeyAction>, + ): Branch { + val root = Branch(mutableMapOf()) + for (combo in combos) { + var p = root + require(combo.keys.isNotEmpty()) + for ((index, key) in combo.keys.withIndex()) { + val m = (p.nodes as MutableMap) + if (index == combo.keys.lastIndex) { + if (key in m) + error("Overlapping actions found for ${combo.keys} (another action ${m[key]} already exists).") + + m[key] = Leaf(combo.action) + } else { + val c = m.getOrPut(key) { Branch(mutableMapOf()) } + if (c !is Branch) + error("Overlapping actions found for ${combo.keys} (final node exists at index $index) through another action already") + p = c + } + } + } + return root + } + } +} + + +data class ComboKeyAction( + val action: HotkeyAction, + val keys: List<SavedKeyBinding>, +) + +data class Leaf(val action: HotkeyAction) : KeyComboTrie { + override val label: Text + get() = action.label + + fun execute() { + action.execute() + } +} + +data class Branch( + val nodes: Map<SavedKeyBinding, KeyComboTrie> +) : KeyComboTrie { + override val label: Text + get() = Text.literal("...") // TODO: better labels +} |