diff options
| author | Linnea Gräf <nea@nea.moe> | 2025-09-06 17:48:07 +0200 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2025-09-06 17:48:07 +0200 |
| commit | 67da8fd538070bcb652538741fcfe89252173abe (patch) | |
| tree | 24053cc0b409a15f562289b8b3effd13f6de4bd0 /src/main/kotlin/gui | |
| parent | d62ee562abe14cf06275b30411226e440a6ac3a7 (diff) | |
| download | Firmament-67da8fd538070bcb652538741fcfe89252173abe.tar.gz Firmament-67da8fd538070bcb652538741fcfe89252173abe.tar.bz2 Firmament-67da8fd538070bcb652538741fcfe89252173abe.zip | |
feat: add support for mouse button binds
Diffstat (limited to 'src/main/kotlin/gui')
| -rw-r--r-- | src/main/kotlin/gui/FirmButtonComponent.kt | 134 | ||||
| -rw-r--r-- | src/main/kotlin/gui/config/KeyBindingStateManager.kt | 22 |
2 files changed, 83 insertions, 73 deletions
diff --git a/src/main/kotlin/gui/FirmButtonComponent.kt b/src/main/kotlin/gui/FirmButtonComponent.kt index fe9b476..1469c09 100644 --- a/src/main/kotlin/gui/FirmButtonComponent.kt +++ b/src/main/kotlin/gui/FirmButtonComponent.kt @@ -1,4 +1,3 @@ - package moe.nea.firmament.gui import io.github.notenoughupdates.moulconfig.common.MyResourceLocation @@ -11,71 +10,78 @@ import io.github.notenoughupdates.moulconfig.observer.GetSetter open class FirmButtonComponent( - child: GuiComponent, - val isEnabled: GetSetter<Boolean> = GetSetter.constant(true), - val noBackground: Boolean = false, - val action: Runnable, + child: GuiComponent, + val isEnabled: GetSetter<Boolean> = GetSetter.constant(true), + val noBackground: Boolean = false, + val action: (mouseButton: Int) -> Unit, ) : PanelComponent(child, if (noBackground) 0 else 2, DefaultBackgroundRenderer.TRANSPARENT) { - /* TODO: make use of vanillas built in nine slicer */ - val hoveredBg = - NinePatch.builder(MyResourceLocation("minecraft", "textures/gui/sprites/widget/button_highlighted.png")) - .cornerSize(5) - .cornerUv(5 / 200F, 5 / 20F) - .mode(NinePatch.Mode.STRETCHING) - .build() - val unhoveredBg = NinePatch.builder(MyResourceLocation("minecraft", "textures/gui/sprites/widget/button.png")) - .cornerSize(5) - .cornerUv(5 / 200F, 5 / 20F) - .mode(NinePatch.Mode.STRETCHING) - .build() - val disabledBg = - NinePatch.builder(MyResourceLocation("minecraft", "textures/gui/sprites/widget/button_disabled.png")) - .cornerSize(5) - .cornerUv(5 / 200F, 5 / 20F) - .mode(NinePatch.Mode.STRETCHING) - .build() - val activeBg = NinePatch.builder(MyResourceLocation("firmament", "textures/gui/sprites/widget/button_active.png")) - .cornerSize(5) - .cornerUv(5 / 200F, 5 / 20F) - .mode(NinePatch.Mode.STRETCHING) - .build() - var isClicking = false - override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean { - if (!isEnabled.get()) return false - if (isClicking) { - if (mouseEvent is MouseEvent.Click && !mouseEvent.mouseState && mouseEvent.mouseButton == 0) { - isClicking = false - if (context.isHovered) { - action.run() - } - return true - } - } - if (!context.isHovered) return false - if (mouseEvent !is MouseEvent.Click) return false - if (mouseEvent.mouseState && mouseEvent.mouseButton == 0) { - requestFocus() - isClicking = true - return true - } - return false - } + constructor( + child: GuiComponent, + isEnabled: GetSetter<Boolean> = GetSetter.constant(true), + noBackground: Boolean = false, + action: Runnable, + ) : this(child, isEnabled, noBackground, { action.run() }) + + /* TODO: make use of vanillas built in nine slicer */ + val hoveredBg = + NinePatch.builder(MyResourceLocation("minecraft", "textures/gui/sprites/widget/button_highlighted.png")) + .cornerSize(5) + .cornerUv(5 / 200F, 5 / 20F) + .mode(NinePatch.Mode.STRETCHING) + .build() + val unhoveredBg = NinePatch.builder(MyResourceLocation("minecraft", "textures/gui/sprites/widget/button.png")) + .cornerSize(5) + .cornerUv(5 / 200F, 5 / 20F) + .mode(NinePatch.Mode.STRETCHING) + .build() + val disabledBg = + NinePatch.builder(MyResourceLocation("minecraft", "textures/gui/sprites/widget/button_disabled.png")) + .cornerSize(5) + .cornerUv(5 / 200F, 5 / 20F) + .mode(NinePatch.Mode.STRETCHING) + .build() + val activeBg = NinePatch.builder(MyResourceLocation("firmament", "textures/gui/sprites/widget/button_active.png")) + .cornerSize(5) + .cornerUv(5 / 200F, 5 / 20F) + .mode(NinePatch.Mode.STRETCHING) + .build() + var isClicking = false + override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean { + if (!isEnabled.get()) return false + if (isClicking) { + if (mouseEvent is MouseEvent.Click && !mouseEvent.mouseState) { + isClicking = false + if (context.isHovered) { + action.invoke(mouseEvent.mouseButton) + } + return true + } + } + if (!context.isHovered) return false + if (mouseEvent !is MouseEvent.Click) return false + if (mouseEvent.mouseState) { + requestFocus() + isClicking = true + return true + } + return false + } - open fun getBackground(context: GuiImmediateContext): NinePatch<MyResourceLocation> = - if (!isEnabled.get()) disabledBg - else if (context.isHovered || isClicking) hoveredBg - else unhoveredBg + open fun getBackground(context: GuiImmediateContext): NinePatch<MyResourceLocation> = + if (!isEnabled.get()) disabledBg + else if (context.isHovered || isClicking) hoveredBg + else unhoveredBg - override fun render(context: GuiImmediateContext) { - context.renderContext.pushMatrix() - if (!noBackground) - context.renderContext.drawNinePatch( - getBackground(context), - 0f, 0f, context.width, context.height - ) - context.renderContext.translate(insets.toFloat(), insets.toFloat()) - element.render(getChildContext(context)) - context.renderContext.popMatrix() - } + override fun render(context: GuiImmediateContext) { + context.renderContext.pushMatrix() + if (!noBackground) + context.renderContext.drawNinePatch( + getBackground(context), + 0f, 0f, context.width, context.height + ) + context.renderContext.translate(insets.toFloat(), insets.toFloat()) + element.render(getChildContext(context)) + context.renderContext.popMatrix() + } } diff --git a/src/main/kotlin/gui/config/KeyBindingStateManager.kt b/src/main/kotlin/gui/config/KeyBindingStateManager.kt index 500f0fa..d8ec359 100644 --- a/src/main/kotlin/gui/config/KeyBindingStateManager.kt +++ b/src/main/kotlin/gui/config/KeyBindingStateManager.kt @@ -25,11 +25,10 @@ class KeyBindingStateManager( var lastPressed: GenericInputButton? = null var label: Text = Text.literal("") - fun onClick() { + fun onClick(mouseButton: Int) { if (editing) { - editing = false - blur() - } else { + keyboardEvent(GenericInputButton.mouse(mouseButton), true) + } else if (mouseButton == GLFW.GLFW_MOUSE_BUTTON_LEFT) { editing = true requestFocus() } @@ -37,8 +36,8 @@ class KeyBindingStateManager( } fun keyboardEvent(keyCode: GenericInputButton, pressed: Boolean): Boolean { - return if (pressed) onKeyPressed(keyCode, InputModifiers.getCurrentModifiers()) - else onKeyReleased(keyCode, InputModifiers.getCurrentModifiers()) + return if (pressed) onKeyPressed(keyCode, InputModifiers.current()) + else onKeyReleased(keyCode, InputModifiers.current()) } fun onKeyPressed( @@ -91,7 +90,7 @@ class KeyBindingStateManager( var stroke = value().format() if (editing) { stroke = Text.empty() - val modifiers = InputModifiers.getCurrentModifiers() + val modifiers = InputModifiers.current() if (!modifiers.isEmpty()) { stroke.append(modifiers.format()) stroke.append(" + ") @@ -112,11 +111,16 @@ class KeyBindingStateManager( false, false ), action = { - this@KeyBindingStateManager.onClick() + this@KeyBindingStateManager.onClick(it) }) { override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean { if (event is KeyboardEvent.KeyPressed) { - return this@KeyBindingStateManager.keyboardEvent(GenericInputButton.ofKeyAndScan(event.keycode, event.scancode), event.pressed) + return this@KeyBindingStateManager.keyboardEvent( + GenericInputButton.ofKeyAndScan( + event.keycode, + event.scancode + ), event.pressed + ) } return super.keyboardEvent(event, context) } |
