rendering: double pressing key

This commit is contained in:
Bixilon 2021-04-23 14:34:05 +02:00
parent d4c3e406eb
commit 1ccf8b20f3
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 43 additions and 17 deletions

View File

@ -27,7 +27,7 @@ enum class KeyAction {
RELEASE, RELEASE,
/** /**
* Key must be pressed or released * Key is pressed or released
*/ */
CHANGE, CHANGE,
@ -38,20 +38,20 @@ enum class KeyAction {
*/ */
MODIFIER, MODIFIER,
/**
* ToDo: Key must be pressed twice
*/
DOUBLE_CLICK,
/** /**
* Pressing the key makes it sticky, you have to press it again to make it not pressed anymore * Pressing the key makes it sticky, you have to press it again to make it not pressed anymore
*/ */
STICKY, STICKY,
/** /**
* Exactly the same as STICKY, but inverted. Initial pressed * Exactly the same as STICKY, but inverted. Initial pressed.
*/ */
STICKY_INVERTED, STICKY_INVERTED,
/**
* Key must be pressed twice in a certain time. Behaves like sticky (press twice again to disable)
*/
DOUBLE_PRESS,
; ;
companion object { companion object {

View File

@ -19,6 +19,7 @@ import de.bixilon.minosoft.config.key.KeyAction
import de.bixilon.minosoft.config.key.KeyCodes import de.bixilon.minosoft.config.key.KeyCodes
import de.bixilon.minosoft.data.mappings.ResourceLocation import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.gui.input.camera.Camera import de.bixilon.minosoft.gui.input.camera.Camera
import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.hud.elements.input.KeyConsumer import de.bixilon.minosoft.gui.rendering.hud.elements.input.KeyConsumer
import de.bixilon.minosoft.protocol.network.connection.PlayConnection import de.bixilon.minosoft.protocol.network.connection.PlayConnection
@ -33,7 +34,8 @@ class RenderWindowInputHandler(
private val keyBindingCallbacks: MutableMap<ResourceLocation, KeyBindingCallbackPair> = mutableMapOf() private val keyBindingCallbacks: MutableMap<ResourceLocation, KeyBindingCallbackPair> = mutableMapOf()
private val keysDown: MutableList<KeyCodes> = mutableListOf() private val keysDown: MutableList<KeyCodes> = mutableListOf()
private val keyCombinationsDown: MutableList<ResourceLocation> = mutableListOf() private val keyBindingsDown: MutableList<ResourceLocation> = mutableListOf()
private val keysLastDownTime: MutableMap<KeyCodes, Long> = mutableMapOf()
private var skipNextCharPress = false private var skipNextCharPress = false
@ -75,10 +77,11 @@ class RenderWindowInputHandler(
return return
} }
else -> { else -> {
Log.game("Unknown glfw action $action") Log.warn("Unknown glfw action $action")
return return
} }
} }
val currentTime = System.currentTimeMillis()
if (keyDown) { if (keyDown) {
keysDown += keyCode keysDown += keyCode
@ -92,7 +95,7 @@ class RenderWindowInputHandler(
if (currentKeyConsumer != null && !pair.keyBinding.ignoreConsumer) { if (currentKeyConsumer != null && !pair.keyBinding.ignoreConsumer) {
continue continue
} }
var combinationDown = keyDown var thisKeyBindingDown = keyDown
var checksRun = 0 var checksRun = 0
var thisIsChange = true var thisIsChange = true
@ -131,7 +134,7 @@ class RenderWindowInputHandler(
} }
fun checkSticky(keys: MutableSet<KeyCodes>, invert: Boolean) { fun checkSticky(keys: MutableSet<KeyCodes>, invert: Boolean) {
var alreadyActive = keyCombinationsDown.contains(resourceLocation) var alreadyActive = keyBindingsDown.contains(resourceLocation)
if (invert) { if (invert) {
alreadyActive = !alreadyActive alreadyActive = !alreadyActive
} }
@ -144,7 +147,7 @@ class RenderWindowInputHandler(
thisIsChange = false thisIsChange = false
return return
} }
combinationDown = !alreadyActive thisKeyBindingDown = !alreadyActive
} }
pair.keyBinding.action[KeyAction.STICKY]?.let { pair.keyBinding.action[KeyAction.STICKY]?.let {
@ -155,21 +158,42 @@ class RenderWindowInputHandler(
checkSticky(it, true) checkSticky(it, true)
} }
pair.keyBinding.action[KeyAction.DOUBLE_PRESS]?.let {
if (!it.contains(keyCode)) {
thisIsChange = false
return
}
val lastDownTime = keysLastDownTime[keyCode]
if (lastDownTime == null) {
thisIsChange = false
return
}
if (currentTime - lastDownTime > RenderConstants.DOUBLE_PRESS_KEY_MAX_DELAY) {
thisIsChange = false
return
}
thisKeyBindingDown = !isKeyBindingDown(resourceLocation)
}
if (!thisIsChange || checksRun == 0) { if (!thisIsChange || checksRun == 0) {
continue continue
} }
// Log.debug("Changing $resourceLocation because of $keyCode -> $combinationDown") // Log.debug("Changing $resourceLocation because of $keyCode -> $combinationDown")
for (callback in pair.callback) { for (callback in pair.callback) {
callback.invoke(combinationDown) callback.invoke(thisKeyBindingDown)
} }
if (combinationDown) { if (thisKeyBindingDown) {
keyCombinationsDown += resourceLocation keyBindingsDown += resourceLocation
} else { } else {
keyCombinationsDown -= resourceLocation keyBindingsDown -= resourceLocation
} }
} }
if (keyDown) {
keysLastDownTime[keyCode] = currentTime
}
if (previousKeyConsumer != currentKeyConsumer) { if (previousKeyConsumer != currentKeyConsumer) {
skipNextCharPress = true skipNextCharPress = true
} }
@ -216,7 +240,7 @@ class RenderWindowInputHandler(
} }
fun isKeyBindingDown(resourceLocation: ResourceLocation): Boolean { fun isKeyBindingDown(resourceLocation: ResourceLocation): Boolean {
return keyCombinationsDown.contains(resourceLocation) return keyBindingsDown.contains(resourceLocation)
} }
fun unregisterKeyBinding(it: ResourceLocation) { fun unregisterKeyBinding(it: ResourceLocation) {

View File

@ -70,4 +70,6 @@ object RenderConstants {
val PIXEL_UV_PIXEL_ADD = Vec2(0, 0.1f) val PIXEL_UV_PIXEL_ADD = Vec2(0, 0.1f)
const val CAMPFIRE_ITEMS = 4 const val CAMPFIRE_ITEMS = 4
const val DOUBLE_PRESS_KEY_MAX_DELAY = 200
} }