wip input manager tests

This commit is contained in:
Bixilon 2023-06-29 19:58:17 +02:00
parent 50045e8c70
commit 99da699006
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 85 additions and 11 deletions

View File

@ -222,7 +222,7 @@ testing {
options {
val options = this as TestNGOptions
options.preserveOrder = true
// options.excludeGroups("command", "registry", "biome", "input", "version", "fluid", "world", "raycasting", "pixlyzer", "item", "block", "physics", "light", "packet", "container", "item_stack", "signature", "private_key", "interaction", "item_digging", "world_renderer", "rendering")
options.excludeGroups("font", "command", "registry", "biome", "version", "fluid", "world", "raycasting", "pixlyzer", "item", "block", "physics", "light", "packet", "container", "item_stack", "signature", "private_key", "interaction", "item_digging", "world_renderer", "rendering")
}
}
}

View File

@ -0,0 +1,76 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.input.key.manager
import de.bixilon.kutil.collections.map.SynchronizedMap
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
import de.bixilon.minosoft.config.key.KeyActions
import de.bixilon.minosoft.config.key.KeyBinding
import de.bixilon.minosoft.config.key.KeyCodes
import de.bixilon.minosoft.config.profile.profiles.controls.ControlsProfile
import de.bixilon.minosoft.data.registries.identified.Namespaces.minosoft
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.gui.rendering.input.InputHandler
import de.bixilon.minosoft.gui.rendering.input.key.manager.binding.BindingsManager
import de.bixilon.minosoft.gui.rendering.input.key.manager.binding.actions.bindingsPressed
import de.bixilon.minosoft.gui.rendering.input.key.manager.binding.actions.keysPressed
import de.bixilon.minosoft.gui.rendering.system.window.KeyChangeTypes
import de.bixilon.minosoft.test.IT.OBJENESIS
import org.testng.Assert.assertTrue
import org.testng.annotations.Test
@Test(groups = ["input"])
class InputManagerTest {
private val profile = BindingsManager::class.java.getDeclaredField("profile").apply { isAccessible = true }
private val bindings = BindingsManager::class.java.getDeclaredField("bindings").apply { isAccessible = true }
private val onKey = InputManager::class.java.getDeclaredMethod("onKey", KeyCodes::class.java, KeyChangeTypes::class.java).apply { isAccessible = true }
private fun create(): InputManager {
val manager = OBJENESIS.newInstance(InputManager::class.java)
val bindings = OBJENESIS.newInstance(BindingsManager::class.java)
bindingsPressed[bindings] = mutableSetOf<ResourceLocation>()
profile[bindings] = ControlsProfile()
this.bindings[bindings] = SynchronizedMap<Any, Any>()
manager::bindings.forceSet(bindings)
keysPressed[manager] = mutableSetOf<KeyCodes>()
return manager
}
fun `just register key binding`() {
val input = create()
input.bindings.register(dummy, KeyBinding(mapOf(KeyActions.PRESS to setOf(KeyCodes.KEY_1)))) {}
}
fun `simple pressing`() {
val input = create()
var invoked = false
input.bindings.register(dummy, KeyBinding(mapOf(KeyActions.PRESS to setOf(KeyCodes.KEY_1)))) { invoked = true }
input.simulate(KeyCodes.KEY_1, KeyChangeTypes.PRESS)
assertTrue(invoked)
}
private fun InputManager.simulate(code: KeyCodes, change: KeyChangeTypes) {
onKey.invoke(this, code, change)
}
private object Handler : InputHandler
private val dummy = minosoft("dummy")
}

View File

@ -80,7 +80,7 @@ class InputHandlerManager(
private fun enable() {
context.window.cursorMode = CursorModes.NORMAL
// todo: disable all key combinations
input.clear()
}
private fun disable() {

View File

@ -41,19 +41,18 @@ class InputManager(
val cameraInput = CameraInput(context, context.camera.matrixHandler)
val bindings = BindingsManager(this)
val handler = InputHandlerManager(this)
val interaction = InteractionManagerKeys(this, connection.camera.interactions)
private val pressed: MutableSet<KeyCodes> = mutableSetOf()
private val times: Object2LongMap<KeyCodes> = Object2LongOpenHashMap<KeyCodes>().apply { defaultReturnValue(-1L) }
var mousePosition: Vec2d = Vec2d.EMPTY
private set
val interactionKeys = InteractionManagerKeys(this, connection.camera.interactions)
fun init() {
interactionKeys.register()
interaction.register()
connection.events.listen<CharInputEvent> { onChar(it.char) }
connection.events.listen<KeyInputEvent> { onKey(it.code, it.change) }
@ -63,11 +62,10 @@ class InputManager(
cameraInput.init()
}
private fun deactivateAll() {
fun clear() {
pressed.clear()
times.clear()
// TODO: disable key bindings
bindings.clear()
}
private fun onMouse(delta: Vec2d, position: Vec2d) {
@ -143,7 +141,7 @@ class InputManager(
fun draw(delta: Double) {
cameraInput.updateInput(delta)
interactionKeys.draw()
interaction.draw()
}
fun getLastPressed(key: KeyCodes): Long {

View File

@ -46,7 +46,7 @@ class BindingsManager(
}
}
private fun deactivateAll() {
fun clear() {
for ((name, pair) in bindings) {
val down = name in pressed
if (!down || pair.pressed) {