aboutsummaryrefslogtreecommitdiff
path: root/src/compat/yacl/java/KeybindingController.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-10-13 19:53:10 +0200
committerLinnea Gräf <nea@nea.moe>2024-10-13 19:53:10 +0200
commit87b851373071490bb371ba1160f85f0341579cb2 (patch)
tree45d267d83baad2e3cb6caad6127f6a4d40e7fcbf /src/compat/yacl/java/KeybindingController.kt
parente6142bb93619dee768fc18b87ffdd28558d4bcab (diff)
downloadFirmament-87b851373071490bb371ba1160f85f0341579cb2.tar.gz
Firmament-87b851373071490bb371ba1160f85f0341579cb2.tar.bz2
Firmament-87b851373071490bb371ba1160f85f0341579cb2.zip
Add YACL config menu
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
+ }
+}