fix some key bugs

This commit is contained in:
Bixilon 2023-06-29 21:50:44 +02:00
parent 1c6f1b4b9a
commit 3babe28375
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 30 additions and 20 deletions

View File

@ -86,15 +86,25 @@ class BindingManagerTest {
assertEquals(b, 1)
}
fun `check binding`() {
fun `check press binding`() {
val input = create()
input.bindings.registerCheck(dummy to KeyBinding(mapOf(KeyActions.PRESS to setOf(KeyCodes.KEY_1))))
assertFalse(input.bindings.isDown(dummy))
input.simulate(KeyCodes.KEY_1, KeyChangeTypes.PRESS)
assertFalse(input.bindings.isDown(dummy))
}
fun `check change binding`() {
val input = create()
input.bindings.registerCheck(dummy to KeyBinding(mapOf(KeyActions.CHANGE to setOf(KeyCodes.KEY_1))))
assertFalse(input.bindings.isDown(dummy))
input.simulate(KeyCodes.KEY_1, KeyChangeTypes.PRESS)
assertTrue(input.bindings.isDown(dummy))
}
fun `ignore if consumer is set`() {
val input = create()

View File

@ -173,7 +173,6 @@ class Change {
)
assertTrue(state.satisfied)
assertTrue(state.forceNotify)
}
fun `simple release`() {
@ -187,7 +186,6 @@ class Change {
)
assertTrue(state.satisfied)
assertTrue(state.forceNotify)
}
fun `wrong key`() {
@ -201,7 +199,6 @@ class Change {
)
assertFalse(state.satisfied)
assertFalse(state.forceNotify)
}
fun `multiple keys`() {
@ -215,7 +212,6 @@ class Change {
)
assertTrue(state.satisfied)
assertTrue(state.forceNotify)
}
}

View File

@ -31,13 +31,13 @@ class InteractionManagerKeys(
private fun registerAttack() {
input.bindings.register(ATTACK, KeyBinding(
KeyActions.CHANGE to setOf(KeyCodes.MOUSE_BUTTON_LEFT),
KeyActions.PRESS to setOf(KeyCodes.MOUSE_BUTTON_LEFT),
)) { interactions.tryAttack(it) }
}
private fun registerInteraction() {
input.bindings.register(USE_ITEM, KeyBinding(
KeyActions.CHANGE to setOf(KeyCodes.MOUSE_BUTTON_RIGHT),
KeyActions.PRESS to setOf(KeyCodes.MOUSE_BUTTON_RIGHT),
)) { interactions.use.change(it) }
}

View File

@ -83,21 +83,21 @@ class BindingsManager(
if (!filterState.satisfied) return
val previous = name in this
if (previous == filterState.result && !filterState.forceNotify) return
if (previous == filterState.result) return
for (callback in state.callback) {
callback(filterState.result)
}
if (previous == filterState.result) return
state.lastChange = millis
if (!filterState.store) return
if (filterState.result) {
this.pressed += name
} else {
this.pressed -= name
}
// skipCharPress = true
}
fun onKey(code: KeyCodes, pressed: Boolean, millis: Long) {

View File

@ -16,5 +16,5 @@ package de.bixilon.minosoft.gui.rendering.input.key.manager.binding
data class KeyBindingFilterState(
var result: Boolean,
var satisfied: Boolean = true,
var forceNotify: Boolean = false,
var store: Boolean = true,
)

View File

@ -29,30 +29,34 @@ interface KeyActionFilter {
object Press : KeyActionFilter {
override fun check(filter: KeyBindingFilterState, codes: Set<KeyCodes>, input: InputManager, name: ResourceLocation, state: KeyBindingState, code: KeyCodes, pressed: Boolean, millis: Long) {
if (code in codes) return
if (!pressed || code !in codes) {
filter.satisfied = false
return
}
filter.store = false
}
}
object Release : KeyActionFilter {
override fun check(filter: KeyBindingFilterState, codes: Set<KeyCodes>, input: InputManager, name: ResourceLocation, state: KeyBindingState, code: KeyCodes, pressed: Boolean, millis: Long) {
if (code in codes) return
if (pressed || code !in codes) {
filter.satisfied = false
return
}
filter.result = true
filter.store = false
}
}
object Change : KeyActionFilter {
override fun check(filter: KeyBindingFilterState, codes: Set<KeyCodes>, input: InputManager, name: ResourceLocation, state: KeyBindingState, code: KeyCodes, pressed: Boolean, millis: Long) {
if (code !in codes) {
filter.satisfied = false
return
}
if (code in codes) return
filter.forceNotify = true
filter.satisfied = false
}
}