aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-07-05 22:52:06 +0200
committerLinnea Gräf <nea@nea.moe>2024-07-05 22:52:06 +0200
commitdc3adecc1b34758ccd62b5086d0052ef70555282 (patch)
treedc487f4c14901c5dc59cabbaceaeae13c7feecf0 /src/main
parent3d792dbef7c7e0f186e29f2e3999f99c2c14f5d7 (diff)
downloadFirmament-dc3adecc1b34758ccd62b5086d0052ef70555282.tar.gz
Firmament-dc3adecc1b34758ccd62b5086d0052ef70555282.tar.bz2
Firmament-dc3adecc1b34758ccd62b5086d0052ef70555282.zip
Improve keybinding support
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/moe/nea/firmament/mixins/FirmKeybindsInVanillaControlsPatch.java11
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt41
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt2
-rw-r--r--src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt7
-rw-r--r--src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt17
-rw-r--r--src/main/resources/assets/firmament/lang/en_us.json3
6 files changed, 56 insertions, 25 deletions
diff --git a/src/main/java/moe/nea/firmament/mixins/FirmKeybindsInVanillaControlsPatch.java b/src/main/java/moe/nea/firmament/mixins/FirmKeybindsInVanillaControlsPatch.java
index be3a3c0..78da3b3 100644
--- a/src/main/java/moe/nea/firmament/mixins/FirmKeybindsInVanillaControlsPatch.java
+++ b/src/main/java/moe/nea/firmament/mixins/FirmKeybindsInVanillaControlsPatch.java
@@ -6,6 +6,7 @@
package moe.nea.firmament.mixins;
+import moe.nea.firmament.gui.config.KeyBindingHandler;
import moe.nea.firmament.gui.config.ManagedConfig;
import moe.nea.firmament.keybindings.FirmamentKeyBindings;
import net.minecraft.client.MinecraftClient;
@@ -40,19 +41,21 @@ public class FirmKeybindsInVanillaControlsPatch {
@ModifyArg(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/widget/ButtonWidget;builder(Lnet/minecraft/text/Text;Lnet/minecraft/client/gui/widget/ButtonWidget$PressAction;)Lnet/minecraft/client/gui/widget/ButtonWidget$Builder;"))
public ButtonWidget.PressAction onInit(ButtonWidget.PressAction action) {
- ManagedConfig config = FirmamentKeyBindings.INSTANCE.getKeyBindings().get(binding);
+ var config = FirmamentKeyBindings.INSTANCE.getKeyBindings().get(binding);
if (config == null) return action;
return button -> {
- config.showConfigEditor(MinecraftClient.getInstance().currentScreen);
+ ((KeyBindingHandler) config.getHandler())
+ .getManagedConfig()
+ .showConfigEditor(MinecraftClient.getInstance().currentScreen);
};
}
@Inject(method = "update", at = @At("HEAD"), cancellable = true)
public void onUpdate(CallbackInfo ci) {
- ManagedConfig config = FirmamentKeyBindings.INSTANCE.getKeyBindings().get(binding);
+ var config = FirmamentKeyBindings.INSTANCE.getKeyBindings().get(binding);
if (config == null) return;
resetButton.active = false;
- editButton.setMessage(Text.translatable("firmament.keybinding.external"));
+ editButton.setMessage(Text.translatable("firmament.keybinding.external", config.value.format()));
ci.cancel();
}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt
index d347101..13c6592 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt
@@ -17,16 +17,17 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
-import net.minecraft.client.util.InputUtil
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import moe.nea.firmament.gui.FirmButtonComponent
import moe.nea.firmament.keybindings.FirmamentKeyBindings
import moe.nea.firmament.keybindings.SavedKeyBinding
-class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : ManagedConfig.OptionHandler<SavedKeyBinding> {
- init {
- FirmamentKeyBindings.registerKeyBinding(name, managedConfig)
+class KeyBindingHandler(val name: String, val managedConfig: ManagedConfig) :
+ ManagedConfig.OptionHandler<SavedKeyBinding> {
+
+ override fun initOption(opt: ManagedOption<SavedKeyBinding>) {
+ FirmamentKeyBindings.registerKeyBinding(name, opt)
}
override fun toJson(element: SavedKeyBinding): JsonElement? {
@@ -44,19 +45,22 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
var label: String = ""
var button: FirmButtonComponent? = null
fun updateLabel() {
- val stroke = Text.literal("")
- if (opt.value.shift) {
- stroke.append("SHIFT + ") // TODO: translations?
- }
- if (opt.value.alt) {
- stroke.append("ALT + ")
- }
- if (opt.value.ctrl) {
- stroke.append("CTRL + ")
- }
- stroke.append(InputUtil.Type.KEYSYM.createFromCode(opt.value.keyCode).localizedText)
- if (editing)
+ var stroke = opt.value.format()
+ if (editing) {
+ stroke = Text.literal("")
+ val (shift, alt, ctrl) = SavedKeyBinding.getMods(SavedKeyBinding.getModInt())
+ if (shift) {
+ stroke.append("SHIFT + ")
+ }
+ if (alt) {
+ stroke.append("ALT + ")
+ }
+ if (ctrl) {
+ stroke.append("CTRL + ")
+ }
+ stroke.append("???")
stroke.styled { it.withColor(Formatting.YELLOW) }
+ }
label = (stroke).string
managedConfig.save()
}
@@ -79,7 +83,7 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
}) {
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
if (event is KeyboardEvent.KeyPressed) {
- if (event.pressed) onKeyPressed(event.keycode, SavedKeyBinding.getModInt())
+ return if (event.pressed) onKeyPressed(event.keycode, SavedKeyBinding.getModInt())
else onKeyReleased(event.keycode, SavedKeyBinding.getModInt())
}
return super.keyboardEvent(event, context)
@@ -100,6 +104,7 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
lastPressed = 0
opt.value = SavedKeyBinding(GLFW.GLFW_KEY_UNKNOWN)
updateLabel()
+ blur()
return true
}
if (ch == GLFW.GLFW_KEY_LEFT_SHIFT || ch == GLFW.GLFW_KEY_RIGHT_SHIFT
@@ -112,6 +117,7 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
ch, modifiers
)
editing = false
+ blur()
lastPressed = 0
lastPressedNonModifier = 0
}
@@ -132,6 +138,7 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage
if (lastPressedNonModifier == ch || (lastPressedNonModifier == 0 && ch == lastPressed)) {
opt.value = SavedKeyBinding(ch, modifiers)
editing = false
+ blur()
lastPressed = 0
lastPressedNonModifier = 0
}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
index 104cd52..2caea99 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
@@ -34,6 +34,7 @@ import moe.nea.firmament.util.ScreenUtil.setScreenLater
abstract class ManagedConfig(override val name: String) : ManagedConfigElement() {
interface OptionHandler<T : Any> {
+ fun initOption(opt: ManagedOption<T>) {}
fun toJson(element: T): JsonElement?
fun fromJson(element: JsonElement): T
fun emitGuiElements(opt: ManagedOption<T>, guiAppender: GuiAppender)
@@ -74,6 +75,7 @@ abstract class ManagedConfig(override val name: String) : ManagedConfigElement()
): ManagedOption<T> {
if (propertyName in allOptions) error("Cannot register the same name twice")
return ManagedOption(this, propertyName, default, handler).also {
+ it.handler.initOption(it)
it.load(data)
allOptions[propertyName] = it
sortedOptions.add(it)
diff --git a/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt b/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt
index a5844e5..3fbdd84 100644
--- a/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt
+++ b/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt
@@ -9,10 +9,11 @@ package moe.nea.firmament.keybindings
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper
import net.minecraft.client.option.KeyBinding
import net.minecraft.client.util.InputUtil
-import moe.nea.firmament.gui.config.ManagedConfig
+import moe.nea.firmament.gui.config.KeyBindingHandler
+import moe.nea.firmament.gui.config.ManagedOption
object FirmamentKeyBindings {
- fun registerKeyBinding(name: String, config: ManagedConfig) {
+ fun registerKeyBinding(name: String, config: ManagedOption<SavedKeyBinding>) {
val vanillaKeyBinding = KeyBindingHelper.registerKeyBinding(
KeyBinding(
name,
@@ -24,6 +25,6 @@ object FirmamentKeyBindings {
keyBindings[vanillaKeyBinding] = config
}
- val keyBindings = mutableMapOf<KeyBinding, ManagedConfig>()
+ val keyBindings = mutableMapOf<KeyBinding, ManagedOption<SavedKeyBinding>>()
}
diff --git a/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt b/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt
index 56865cc..f07cd29 100644
--- a/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt
+++ b/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt
@@ -10,6 +10,7 @@ import org.lwjgl.glfw.GLFW
import kotlinx.serialization.Serializable
import net.minecraft.client.MinecraftClient
import net.minecraft.client.util.InputUtil
+import net.minecraft.text.Text
import moe.nea.firmament.util.MC
@Serializable
@@ -87,4 +88,20 @@ data class SavedKeyBinding(
return keyCode == this.keyCode && getMods(modifiers) == Triple(shift, ctrl, alt)
}
+ fun format(): Text {
+ val stroke = Text.literal("")
+ if (ctrl) {
+ stroke.append("CTRL + ")
+ }
+ if (alt) {
+ stroke.append("ALT + ")
+ }
+ if (shift) {
+ stroke.append("SHIFT + ") // TODO: translations?
+ }
+
+ stroke.append(InputUtil.Type.KEYSYM.createFromCode(keyCode).localizedText)
+ return stroke
+ }
+
}
diff --git a/src/main/resources/assets/firmament/lang/en_us.json b/src/main/resources/assets/firmament/lang/en_us.json
index 3dcdbf0..a635517 100644
--- a/src/main/resources/assets/firmament/lang/en_us.json
+++ b/src/main/resources/assets/firmament/lang/en_us.json
@@ -142,7 +142,7 @@
"firmament.config.item-rarity-cosmetics.background-hotbar": "Hotbar Background Rarity",
"firmament.hud.edit": "Edit %s",
"firmament.key.category": "Firmament",
- "firmament.keybinding.external": "External",
+ "firmament.keybinding.external": "%s",
"firmament.config.slot-locking": "Slot Locking",
"firmament.config.slot-locking.lock": "Lock Slot",
"firmament.config.slot-locking.lock-uuid": "Lock UUID (Lock Item)",
@@ -150,6 +150,7 @@
"firmament.config.fixes.auto-sprint-keybinding": "Auto Sprint KeyBinding",
"firmament.config.fixes.auto-sprint-hud": "Sprint State Hud",
"firmament.config.fixes.peek-chat": "Peek Chat",
+ "firmament.config.fixes.clientside-lefthand": "Fix Left Handed",
"firmament.fixes.auto-sprint.on": "Sprint toggled",
"firmament.fixes.auto-sprint.sprinting": "Sprinting",
"firmament.fixes.auto-sprint.not-sprinting": "Not Sprinting",