Add the ability to unbind keys using ESCAPE

This commit is contained in:
nea
2023-08-31 23:23:43 +02:00
parent 36d5ef29e4
commit 41dc53e00e
2 changed files with 6 additions and 21 deletions

View File

@@ -8,16 +8,16 @@ package moe.nea.firmament.gui.config
import io.github.cottonmc.cotton.gui.widget.WButton import io.github.cottonmc.cotton.gui.widget.WButton
import io.github.cottonmc.cotton.gui.widget.data.InputResult import io.github.cottonmc.cotton.gui.widget.data.InputResult
import org.lwjgl.glfw.GLFW
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromJsonElement import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement import kotlinx.serialization.json.encodeToJsonElement
import moe.nea.firmament.keybindings.FirmamentKeyBindings
import moe.nea.firmament.keybindings.SavedKeyBinding
import net.minecraft.client.util.InputUtil import net.minecraft.client.util.InputUtil
import net.minecraft.text.Text import net.minecraft.text.Text
import net.minecraft.util.Formatting import net.minecraft.util.Formatting
import org.lwjgl.glfw.GLFW import moe.nea.firmament.keybindings.FirmamentKeyBindings
import moe.nea.firmament.keybindings.SavedKeyBinding
class KeyBindingHandler(name: String, managedConfig: ManagedConfig) : ManagedConfig.OptionHandler<SavedKeyBinding> { class KeyBindingHandler(name: String, managedConfig: ManagedConfig) : ManagedConfig.OptionHandler<SavedKeyBinding> {
init { init {
@@ -46,6 +46,7 @@ class KeyBindingHandler(name: String, managedConfig: ManagedConfig) : ManagedCon
lastPressedNonModifier = 0 lastPressedNonModifier = 0
editing = false editing = false
lastPressed = 0 lastPressed = 0
opt.value = SavedKeyBinding(GLFW.GLFW_KEY_UNKNOWN)
updateButton!!() updateButton!!()
return InputResult.PROCESSED return InputResult.PROCESSED
} }

View File

@@ -38,25 +38,8 @@ data class SavedKeyBinding(
} }
} }
fun hasShiftDown(): Boolean {
return InputUtil.isKeyPressed(
MinecraftClient.getInstance().window.handle,
GLFW.GLFW_KEY_LEFT_SHIFT
) || InputUtil.isKeyPressed(
MinecraftClient.getInstance().window.handle, GLFW.GLFW_KEY_RIGHT_SHIFT
)
}
fun hasAltDown(): Boolean {
return InputUtil.isKeyPressed(
MinecraftClient.getInstance().window.handle,
GLFW.GLFW_KEY_LEFT_ALT
) || InputUtil.isKeyPressed(
MinecraftClient.getInstance().window.handle, GLFW.GLFW_KEY_RIGHT_ALT
)
}
fun isPressed(): Boolean { fun isPressed(): Boolean {
if (this.keyCode == GLFW.GLFW_KEY_UNKNOWN) return false
val h = MC.window.handle val h = MC.window.handle
if (!InputUtil.isKeyPressed(h, keyCode)) return false if (!InputUtil.isKeyPressed(h, keyCode)) return false
@@ -76,6 +59,7 @@ data class SavedKeyBinding(
} }
override fun matches(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { override fun matches(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
if (this.keyCode == GLFW.GLFW_KEY_UNKNOWN) return false
return keyCode == this.keyCode && getMods(modifiers) == Triple(shift, ctrl, alt) return keyCode == this.keyCode && getMods(modifiers) == Triple(shift, ctrl, alt)
} }