From 2a1631dadfc0f78eb3b7768f9bca3f9fa1ea619e Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Wed, 4 Jun 2025 01:06:51 +0200 Subject: feat: Add basic combo buttons (without editor for now) --- src/main/kotlin/features/macros/KeyComboTrie.kt | 57 +++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/kotlin/features/macros/KeyComboTrie.kt (limited to 'src/main/kotlin/features/macros/KeyComboTrie.kt') 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, + ): 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, +) + +data class Leaf(val action: HotkeyAction) : KeyComboTrie { + override val label: Text + get() = action.label + + fun execute() { + action.execute() + } +} + +data class Branch( + val nodes: Map +) : KeyComboTrie { + override val label: Text + get() = Text.literal("...") // TODO: better labels +} -- cgit