aboutsummaryrefslogtreecommitdiff
path: root/src/compat/yacl/java/KeybindingController.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat/yacl/java/KeybindingController.kt')
-rw-r--r--src/compat/yacl/java/KeybindingController.kt71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/compat/yacl/java/KeybindingController.kt b/src/compat/yacl/java/KeybindingController.kt
new file mode 100644
index 0000000..6be9dd1
--- /dev/null
+++ b/src/compat/yacl/java/KeybindingController.kt
@@ -0,0 +1,71 @@
+package moe.nea.firmament.compat.yacl
+
+import dev.isxander.yacl3.api.Controller
+import dev.isxander.yacl3.api.Option
+import dev.isxander.yacl3.api.utils.Dimension
+import dev.isxander.yacl3.gui.AbstractWidget
+import dev.isxander.yacl3.gui.YACLScreen
+import dev.isxander.yacl3.gui.controllers.ControllerWidget
+import net.minecraft.text.Text
+import moe.nea.firmament.gui.config.KeyBindingHandler
+import moe.nea.firmament.gui.config.KeyBindingStateManager
+import moe.nea.firmament.gui.config.ManagedOption
+import moe.nea.firmament.keybindings.SavedKeyBinding
+
+class KeybindingController(
+ val option: Option<SavedKeyBinding>,
+ val managedOption: ManagedOption<SavedKeyBinding>,
+) : Controller<SavedKeyBinding> {
+ val handler = managedOption.handler as KeyBindingHandler
+ override fun option(): Option<SavedKeyBinding> {
+ return option
+ }
+
+ override fun formatValue(): Text {
+ return option.pendingValue().format()
+ }
+
+ override fun provideWidget(screen: YACLScreen, widgetDimension: Dimension<Int>): AbstractWidget {
+ lateinit var button: ControllerWidget<KeybindingController>
+ val sm = KeyBindingStateManager(
+ { option.pendingValue() },
+ { option.requestSet(it) },
+ { screen.focused = null },
+ { screen.focused = button }
+ )
+ button = object : ControllerWidget<KeybindingController>(this, screen, widgetDimension) {
+ override fun getHoveredControlWidth(): Int {
+ return 130
+ }
+
+ override fun getValueText(): Text {
+ return sm.label
+ }
+
+ override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
+ return sm.keyboardEvent(keyCode, true)
+ }
+
+ override fun keyReleased(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
+ return sm.keyboardEvent(keyCode, false)
+ }
+
+ override fun unfocus() {
+ sm.onLostFocus()
+ }
+
+ override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
+ if (button == 0 && isHovered) {
+ sm.onClick()
+ return true
+ }
+ return super.mouseClicked(mouseX, mouseY, button)
+ }
+ }
+ option.addListener { t, u ->
+ sm.updateLabel()
+ }
+ sm.updateLabel()
+ return button
+ }
+}