From 99da699006a0f096d01cab783c4d261663f6f404 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Thu, 29 Jun 2023 19:58:17 +0200 Subject: [PATCH] wip input manager tests --- build.gradle.kts | 2 +- .../input/key/manager/InputManagerTest.kt | 76 +++++++++++++++++++ .../input/key/manager/InputHandlerManager.kt | 2 +- .../input/key/manager/InputManager.kt | 14 ++-- .../key/manager/binding/BindingsManager.kt | 2 +- 5 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/input/key/manager/InputManagerTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 8f81d8f57..68f121068 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") } } } diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/input/key/manager/InputManagerTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/input/key/manager/InputManagerTest.kt new file mode 100644 index 000000000..cf96a39e2 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/input/key/manager/InputManagerTest.kt @@ -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 . + * + * 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() + profile[bindings] = ControlsProfile() + this.bindings[bindings] = SynchronizedMap() + + + manager::bindings.forceSet(bindings) + keysPressed[manager] = mutableSetOf() + + 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") +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/InputHandlerManager.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/InputHandlerManager.kt index 2a58d3536..5a95749a0 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/InputHandlerManager.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/InputHandlerManager.kt @@ -80,7 +80,7 @@ class InputHandlerManager( private fun enable() { context.window.cursorMode = CursorModes.NORMAL - // todo: disable all key combinations + input.clear() } private fun disable() { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/InputManager.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/InputManager.kt index 31de06208..55089b883 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/InputManager.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/InputManager.kt @@ -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 = mutableSetOf() private val times: Object2LongMap = Object2LongOpenHashMap().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 { onChar(it.char) } connection.events.listen { 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 { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/binding/BindingsManager.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/binding/BindingsManager.kt index 92f3ed937..2039b9cf2 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/binding/BindingsManager.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/manager/binding/BindingsManager.kt @@ -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) {