diff --git a/src/main/java/de/bixilon/minosoft/config/config/game/controls/KeyBindingNames.kt b/src/main/java/de/bixilon/minosoft/config/config/game/controls/KeyBindingsNames.kt
similarity index 100%
rename from src/main/java/de/bixilon/minosoft/config/config/game/controls/KeyBindingNames.kt
rename to src/main/java/de/bixilon/minosoft/config/config/game/controls/KeyBindingsNames.kt
diff --git a/src/main/java/de/bixilon/minosoft/gui/input/camera/Camera.kt b/src/main/java/de/bixilon/minosoft/gui/input/camera/Camera.kt
index 980733bd5..1ba27edc7 100644
--- a/src/main/java/de/bixilon/minosoft/gui/input/camera/Camera.kt
+++ b/src/main/java/de/bixilon/minosoft/gui/input/camera/Camera.kt
@@ -99,7 +99,7 @@ class Camera(
var yOffset = yPos - this.lastMouseY
lastMouseX = xPos
lastMouseY = yPos
- if (renderWindow.currentKeyConsumer != null) {
+ if (renderWindow.inputHandler.currentKeyConsumer != null) {
return
}
xOffset *= mouseSensitivity
@@ -123,31 +123,31 @@ class Camera(
}
fun init(renderWindow: RenderWindow) {
- renderWindow.registerKeyCallback(KeyBindingsNames.MOVE_FORWARD) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.MOVE_FORWARD) { _: KeyCodes, keyAction: KeyAction ->
keyForwardDown = keyAction == KeyAction.PRESS
}
- renderWindow.registerKeyCallback(KeyBindingsNames.MOVE_LEFT) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.MOVE_LEFT) { _: KeyCodes, keyAction: KeyAction ->
keyLeftDown = keyAction == KeyAction.PRESS
}
- renderWindow.registerKeyCallback(KeyBindingsNames.MOVE_BACKWARDS) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.MOVE_BACKWARDS) { _: KeyCodes, keyAction: KeyAction ->
keyBackDown = keyAction == KeyAction.PRESS
}
- renderWindow.registerKeyCallback(KeyBindingsNames.MOVE_RIGHT) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.MOVE_RIGHT) { _: KeyCodes, keyAction: KeyAction ->
keyRightDown = keyAction == KeyAction.PRESS
}
- renderWindow.registerKeyCallback(KeyBindingsNames.MOVE_FLY_UP) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.MOVE_FLY_UP) { _: KeyCodes, keyAction: KeyAction ->
keyFlyUp = keyAction == KeyAction.PRESS
}
- renderWindow.registerKeyCallback(KeyBindingsNames.MOVE_FLY_DOWN) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.MOVE_FLY_DOWN) { _: KeyCodes, keyAction: KeyAction ->
keyFlyDown = keyAction == KeyAction.PRESS
}
- renderWindow.registerKeyCallback(KeyBindingsNames.MOVE_SPRINT) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.MOVE_SPRINT) { _: KeyCodes, keyAction: KeyAction ->
keySprintDown = keyAction == KeyAction.PRESS
}
- renderWindow.registerKeyCallback(KeyBindingsNames.ZOOM) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.ZOOM) { _: KeyCodes, keyAction: KeyAction ->
keyZoomDown = keyAction == KeyAction.PRESS
}
- renderWindow.registerKeyCallback(KeyBindingsNames.MOVE_JUMP) { _: KeyCodes, keyAction: KeyAction ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.MOVE_JUMP) { _: KeyCodes, keyAction: KeyAction ->
keyJumpDown = keyAction == KeyAction.PRESS
}
@@ -158,7 +158,7 @@ class Camera(
}
fun handleInput(deltaTime: Double) {
- if (renderWindow.currentKeyConsumer != null) { // ToDo
+ if (renderWindow.inputHandler.currentKeyConsumer != null) { // ToDo
return
}
var cameraSpeed = movementSpeed * deltaTime
diff --git a/src/main/java/de/bixilon/minosoft/gui/input/key/RenderWindowInputHandler.kt b/src/main/java/de/bixilon/minosoft/gui/input/key/RenderWindowInputHandler.kt
new file mode 100644
index 000000000..3bfde6f61
--- /dev/null
+++ b/src/main/java/de/bixilon/minosoft/gui/input/key/RenderWindowInputHandler.kt
@@ -0,0 +1,195 @@
+/*
+ * Minosoft
+ * Copyright (C) 2021 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.input.key
+
+import de.bixilon.minosoft.Minosoft
+import de.bixilon.minosoft.config.StaticConfiguration
+import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames
+import de.bixilon.minosoft.config.key.KeyAction
+import de.bixilon.minosoft.config.key.KeyBinding
+import de.bixilon.minosoft.config.key.KeyCodes
+import de.bixilon.minosoft.data.mappings.ResourceLocation
+import de.bixilon.minosoft.gui.input.camera.Camera
+import de.bixilon.minosoft.gui.rendering.RenderWindow
+import de.bixilon.minosoft.gui.rendering.hud.elements.input.KeyConsumer
+import de.bixilon.minosoft.protocol.network.connection.PlayConnection
+import org.lwjgl.glfw.GLFW
+
+class RenderWindowInputHandler(
+ val renderWindow: RenderWindow,
+) {
+ val connection: PlayConnection = renderWindow.connection
+
+ private val keyBindingCallbacks: MutableMap Unit)>>> = mutableMapOf()
+ private val keysDown: MutableSet = mutableSetOf()
+ private val keyBindingDown: MutableSet = mutableSetOf()
+ val camera: Camera = Camera(connection, Minosoft.getConfig().config.game.camera.fov, renderWindow)
+
+ private var skipNextCharPress = false
+
+ private var _currentInputConsumer: KeyConsumer? = null
+ var mouseCatch = !StaticConfiguration.DEBUG_MODE
+
+
+ init {
+ registerKeyCallback(KeyBindingsNames.DEBUG_MOUSE_CATCH) { _: KeyCodes, _: KeyAction ->
+ mouseCatch = !mouseCatch
+ if (mouseCatch) {
+ GLFW.glfwSetInputMode(renderWindow.windowId, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED)
+ } else {
+ GLFW.glfwSetInputMode(renderWindow.windowId, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL)
+ }
+ renderWindow.sendDebugMessage("Toggled mouse catch!")
+ }
+ }
+
+ var currentKeyConsumer: KeyConsumer?
+ get() = _currentInputConsumer
+ set(value) {
+ _currentInputConsumer = value
+ for ((_, binding) in keyBindingCallbacks) {
+ if (!keyBindingDown.contains(binding.first)) {
+ continue
+ }
+ if (!binding.first.action.containsKey(KeyAction.TOGGLE) && !binding.first.action.containsKey(KeyAction.CHANGE)) {
+ continue
+ }
+
+ for (keyCallback in binding.second) {
+ keyCallback.invoke(KeyCodes.KEY_UNKNOWN, KeyAction.RELEASE)
+ }
+ }
+ // ToDo: move to mouse consumer
+ if (value == null) {
+ if (mouseCatch) {
+ renderWindow.renderQueue.add { GLFW.glfwSetInputMode(renderWindow.windowId, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED) }
+ }
+ } else {
+ renderWindow.renderQueue.add { GLFW.glfwSetInputMode(renderWindow.windowId, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL) }
+ }
+ keyBindingDown.clear()
+ }
+
+ fun invoke(window: Long, key: Int, scancode: Int, action: Int, mods: Int) {
+ val keyCode = KeyCodes.KEY_CODE_GLFW_ID_MAP[key] ?: KeyCodes.KEY_UNKNOWN
+ val keyAction = when (action) {
+ GLFW.GLFW_PRESS -> KeyAction.PRESS
+ GLFW.GLFW_RELEASE -> KeyAction.RELEASE
+ // ToDo: Double, Hold
+ else -> return
+ }
+ if (keyAction == KeyAction.PRESS) {
+ keysDown.add(keyCode)
+ } else if (keyAction == KeyAction.RELEASE) {
+ keysDown.remove(keyCode)
+ }
+
+ if (keyAction == KeyAction.PRESS) {
+ // ToDo: Repeatable keys, long holding, etc
+ currentKeyConsumer?.keyInput(keyCode)
+ }
+
+ val previousKeyConsumer = currentKeyConsumer
+ for ((_, keyCallbackPair) in keyBindingCallbacks) {
+ run {
+ val keyBinding = keyCallbackPair.first
+ val keyCallbacks = keyCallbackPair.second
+
+ var anyCheckRun = false
+
+ keyBinding.action[KeyAction.MODIFIER]?.let {
+ val previousKeysDown = if (keyAction == KeyAction.RELEASE) {
+ val previousKeysDown = keysDown.toMutableList()
+ previousKeysDown.add(keyCode)
+ previousKeysDown
+ } else {
+ keysDown
+ }
+ if (!previousKeysDown.containsAll(it)) {
+ return@run
+ }
+ anyCheckRun = true
+ }
+ keyBinding.action[KeyAction.CHANGE]?.let {
+ if (!it.contains(keyCode)) {
+ return@run
+ }
+ anyCheckRun = true
+ }
+
+ // release or press
+ if (keyBinding.action[KeyAction.CHANGE] == null) {
+ keyBinding.action[keyAction].let {
+ if (it == null) {
+ return@run
+ }
+ if (!it.contains(keyCode)) {
+ return@run
+ }
+ anyCheckRun = true
+ }
+ }
+
+ if (!anyCheckRun) {
+ return@run
+ }
+
+ if (keyAction == KeyAction.PRESS) {
+ keyBindingDown.add(keyBinding)
+ } else if (keyAction == KeyAction.RELEASE) {
+ keyBindingDown.remove(keyBinding)
+ }
+ for (keyCallback in keyCallbacks) {
+ keyCallback.invoke(keyCode, keyAction)
+ if (previousKeyConsumer != currentKeyConsumer) {
+ skipNextCharPress = true
+ }
+ }
+ }
+ }
+ }
+
+ fun invoke(window: Long, char: Int) {
+ if (skipNextCharPress) {
+ skipNextCharPress = false
+ return
+ }
+ currentKeyConsumer?.charInput(char.toChar())
+ }
+
+ fun invoke(window: Long, xPos: Double, yPos: Double) {
+ camera.mouseCallback(xPos, yPos)
+ }
+
+ fun registerKeyCallback(resourceLocation: ResourceLocation, ignoreConsumer: Boolean = false, callback: ((keyCode: KeyCodes, keyEvent: KeyAction) -> Unit)) {
+ var resourceLocationCallbacks = keyBindingCallbacks[resourceLocation]?.second
+ if (resourceLocationCallbacks == null) {
+ resourceLocationCallbacks = mutableSetOf()
+ val keyBinding = Minosoft.getConfig().config.game.controls.keyBindings.entries[resourceLocation] ?: return
+ keyBindingCallbacks[resourceLocation] = Pair(keyBinding, resourceLocationCallbacks)
+ }
+ resourceLocationCallbacks.add { keyCode, keyEvent ->
+ if (!ignoreConsumer) {
+ if (currentKeyConsumer != null) {
+ return@add
+ }
+ }
+ callback.invoke(keyCode, keyEvent)
+ }
+ }
+
+ fun unregisterKeyBinding(it: ResourceLocation) {
+ keyBindingCallbacks.remove(it)
+ }
+}
diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt
index bf42a2d9f..08046d296 100644
--- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt
+++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt
@@ -13,23 +13,19 @@
package de.bixilon.minosoft.gui.rendering
-import de.bixilon.minosoft.Minosoft
-import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames
import de.bixilon.minosoft.config.key.KeyAction
-import de.bixilon.minosoft.config.key.KeyBinding
import de.bixilon.minosoft.config.key.KeyCodes
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.text.RGBColor
-import de.bixilon.minosoft.gui.input.camera.Camera
import de.bixilon.minosoft.gui.input.camera.FrustumChangeCallback
+import de.bixilon.minosoft.gui.input.key.RenderWindowInputHandler
import de.bixilon.minosoft.gui.modding.events.RenderingStateChangeEvent
import de.bixilon.minosoft.gui.rendering.chunk.WorldRenderer
import de.bixilon.minosoft.gui.rendering.font.Font
import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer
import de.bixilon.minosoft.gui.rendering.hud.atlas.TextureLike
import de.bixilon.minosoft.gui.rendering.hud.atlas.TextureLikeTexture
-import de.bixilon.minosoft.gui.rendering.hud.elements.input.KeyConsumer
import de.bixilon.minosoft.gui.rendering.shader.ShaderHolder
import de.bixilon.minosoft.gui.rendering.textures.Texture
import de.bixilon.minosoft.gui.rendering.textures.TextureArray
@@ -57,26 +53,22 @@ class RenderWindow(
val connection: PlayConnection,
val rendering: Rendering,
) {
- private val keyBindingCallbacks: MutableMap Unit)>>> = mutableMapOf()
- private val keysDown: MutableSet = mutableSetOf()
- private val keyBindingDown: MutableSet = mutableSetOf()
val renderStats = RenderStats()
var screenDimensions = Vec2i(900, 500)
private set
var screenDimensionsF = Vec2(screenDimensions)
private set
+ val inputHandler = RenderWindowInputHandler(this)
- private var windowId = 0L
+ var windowId = 0L
private var deltaFrameTime = 0.0 // time between current frame and last frame
private var lastFrame = 0.0
- val camera: Camera = Camera(connection, Minosoft.getConfig().config.game.camera.fov, this)
private val latch = CountUpAndDownLatch(1)
private var renderingState = RenderingStates.RUNNING
private var polygonEnabled = false
- private var mouseCatch = !StaticConfiguration.DEBUG_MODE
private val screenshotTaker = ScreenshotTaker(this)
val tintColorCalculator = TintColorCalculator(connection.world)
@@ -87,45 +79,14 @@ class RenderWindow(
val renderQueue = ConcurrentLinkedQueue()
- private var _currentInputConsumer: KeyConsumer? = null
-
- private var skipNextCharPress = false
-
lateinit var WHITE_TEXTURE: TextureLike
- val screenResizeCallbacks: MutableSet = mutableSetOf(camera)
+ val screenResizeCallbacks: MutableSet = mutableSetOf(inputHandler.camera)
var tickCount = 0L
var lastTickTimer = System.currentTimeMillis()
- var currentKeyConsumer: KeyConsumer?
- get() = _currentInputConsumer
- set(value) {
- _currentInputConsumer = value
- for ((_, binding) in keyBindingCallbacks) {
- if (!keyBindingDown.contains(binding.first)) {
- continue
- }
- if (!binding.first.action.containsKey(KeyAction.TOGGLE) && !binding.first.action.containsKey(KeyAction.CHANGE)) {
- continue
- }
-
- for (keyCallback in binding.second) {
- keyCallback.invoke(KeyCodes.KEY_UNKNOWN, KeyAction.RELEASE)
- }
- }
- // ToDo: move to mouse consumer
- if (value == null) {
- if (mouseCatch) {
- renderQueue.add { glfwSetInputMode(windowId, GLFW_CURSOR, GLFW_CURSOR_DISABLED) }
- }
- } else {
- renderQueue.add { glfwSetInputMode(windowId, GLFW_CURSOR, GLFW_CURSOR_NORMAL) }
- }
- keyBindingDown.clear()
- }
-
init {
connection.registerEvent(CallbackEventInvoker.of {
if (it.connection.isDisconnected) {
@@ -143,8 +104,8 @@ class RenderWindow(
latch.countDown()
}
renderQueue.add {
- camera.setPosition(packet.position)
- camera.setRotation(packet.rotation.yaw, packet.rotation.pitch)
+ inputHandler.camera.setPosition(packet.position)
+ inputHandler.camera.setRotation(packet.rotation.yaw, packet.rotation.pitch)
}
})
@@ -174,102 +135,20 @@ class RenderWindow(
glfwTerminate()
throw RuntimeException("Failed to create the GLFW window")
}
- camera.init(this)
+ inputHandler.camera.init(this)
tintColorCalculator.init(connection.assetsManager)
- glfwSetKeyCallback(this.windowId) { _: Long, key: Int, _: Int, action: Int, _: Int ->
- val keyCode = KeyCodes.KEY_CODE_GLFW_ID_MAP[key] ?: KeyCodes.KEY_UNKNOWN
- val keyAction = when (action) {
- GLFW_PRESS -> KeyAction.PRESS
- GLFW_RELEASE -> KeyAction.RELEASE
- // ToDo: Double, Hold
- else -> return@glfwSetKeyCallback
- }
- if (keyAction == KeyAction.PRESS) {
- keysDown.add(keyCode)
- } else if (keyAction == KeyAction.RELEASE) {
- keysDown.remove(keyCode)
- }
+ glfwSetKeyCallback(this.windowId, inputHandler::invoke)
- if (keyAction == KeyAction.PRESS) {
- // ToDo: Repeatable keys, long holding, etc
- currentKeyConsumer?.keyInput(keyCode)
- }
+ glfwSetCharCallback(windowId, inputHandler::invoke)
- val previousKeyConsumer = currentKeyConsumer
- for ((_, keyCallbackPair) in keyBindingCallbacks) {
- run {
- val keyBinding = keyCallbackPair.first
- val keyCallbacks = keyCallbackPair.second
-
- var anyCheckRun = false
-
- keyBinding.action[KeyAction.MODIFIER]?.let {
- val previousKeysDown = if (keyAction == KeyAction.RELEASE) {
- val previousKeysDown = keysDown.toMutableList()
- previousKeysDown.add(keyCode)
- previousKeysDown
- } else {
- keysDown
- }
- if (!previousKeysDown.containsAll(it)) {
- return@run
- }
- anyCheckRun = true
- }
- keyBinding.action[KeyAction.CHANGE]?.let {
- if (!it.contains(keyCode)) {
- return@run
- }
- anyCheckRun = true
- }
-
- // release or press
- if (keyBinding.action[KeyAction.CHANGE] == null) {
- keyBinding.action[keyAction].let {
- if (it == null) {
- return@run
- }
- if (!it.contains(keyCode)) {
- return@run
- }
- anyCheckRun = true
- }
- }
-
- if (!anyCheckRun) {
- return@run
- }
-
- if (keyAction == KeyAction.PRESS) {
- keyBindingDown.add(keyBinding)
- } else if (keyAction == KeyAction.RELEASE) {
- keyBindingDown.remove(keyBinding)
- }
- for (keyCallback in keyCallbacks) {
- keyCallback.invoke(keyCode, keyAction)
- if (previousKeyConsumer != currentKeyConsumer) {
- skipNextCharPress = true
- }
- }
- }
- }
- }
-
- glfwSetCharCallback(windowId) { _: Long, char: Int ->
- if (skipNextCharPress) {
- skipNextCharPress = false
- return@glfwSetCharCallback
- }
- currentKeyConsumer?.charInput(char.toChar())
- }
-
- if (mouseCatch) {
+ if (inputHandler.mouseCatch) {
glfwSetInputMode(windowId, GLFW_CURSOR, GLFW_CURSOR_DISABLED)
}
- glfwSetCursorPosCallback(windowId) { _: Long, xPos: Double, yPos: Double -> camera.mouseCallback(xPos, yPos) }
+ glfwSetCursorPosCallback(windowId, inputHandler::invoke)
+
MemoryStack.stackPush().let { stack ->
val pWidth = stack.mallocInt(1)
val pHeight = stack.mallocInt(1)
@@ -328,7 +207,7 @@ class RenderWindow(
for (renderer in rendererMap.values) {
renderer.postInit()
if (renderer is ShaderHolder) {
- camera.addShaders(renderer.shader)
+ inputHandler.camera.addShaders(renderer.shader)
}
}
@@ -382,7 +261,7 @@ class RenderWindow(
}
private fun registerGlobalKeyCombinations() {
- registerKeyCallback(KeyBindingsNames.DEBUG_POLYGON) { _: KeyCodes, _: KeyAction ->
+ inputHandler.registerKeyCallback(KeyBindingsNames.DEBUG_POLYGON) { _: KeyCodes, _: KeyAction ->
polygonEnabled = !polygonEnabled
glPolygonMode(GL_FRONT_AND_BACK, if (polygonEnabled) {
GL_LINE
@@ -391,19 +270,10 @@ class RenderWindow(
})
sendDebugMessage("Toggled polygon mode!")
}
- registerKeyCallback(KeyBindingsNames.DEBUG_MOUSE_CATCH) { _: KeyCodes, _: KeyAction ->
- mouseCatch = !mouseCatch
- if (mouseCatch) {
- glfwSetInputMode(windowId, GLFW_CURSOR, GLFW_CURSOR_DISABLED)
- } else {
- glfwSetInputMode(windowId, GLFW_CURSOR, GLFW_CURSOR_NORMAL)
- }
- sendDebugMessage("Toggled mouse catch!")
- }
- registerKeyCallback(KeyBindingsNames.QUIT_RENDERING) { _: KeyCodes, _: KeyAction ->
+ inputHandler.registerKeyCallback(KeyBindingsNames.QUIT_RENDERING) { _: KeyCodes, _: KeyAction ->
glfwSetWindowShouldClose(windowId, true)
}
- registerKeyCallback(KeyBindingsNames.TAKE_SCREENSHOT, true) { _: KeyCodes, _: KeyAction ->
+ inputHandler.registerKeyCallback(KeyBindingsNames.TAKE_SCREENSHOT, true) { _: KeyCodes, _: KeyAction ->
screenshotTaker.takeScreenshot()
}
}
@@ -422,7 +292,7 @@ class RenderWindow(
val currentTickTime = System.currentTimeMillis()
if (currentTickTime - this.lastTickTimer > ProtocolDefinition.TICK_TIME) {
tickCount++
- currentKeyConsumer?.tick(tickCount)
+ inputHandler.currentKeyConsumer?.tick(tickCount)
this.lastTickTimer = currentTickTime
}
@@ -443,8 +313,8 @@ class RenderWindow(
glfwSwapBuffers(windowId)
glfwPollEvents()
- camera.draw()
- camera.handleInput(deltaFrameTime)
+ inputHandler.camera.draw()
+ inputHandler.camera.handleInput(deltaFrameTime)
// handle opengl context tasks, but limit it per frame
var actionsDone = 0
@@ -496,23 +366,6 @@ class RenderWindow(
connection.fireEvent(RenderingStateChangeEvent(connection, previousState, renderingState))
}
- fun registerKeyCallback(resourceLocation: ResourceLocation, ignoreConsumer: Boolean = false, callback: ((keyCode: KeyCodes, keyEvent: KeyAction) -> Unit)) {
- var resourceLocationCallbacks = keyBindingCallbacks[resourceLocation]?.second
- if (resourceLocationCallbacks == null) {
- resourceLocationCallbacks = mutableSetOf()
- val keyBinding = Minosoft.getConfig().config.game.controls.keyBindings.entries[resourceLocation] ?: return
- keyBindingCallbacks[resourceLocation] = Pair(keyBinding, resourceLocationCallbacks)
- }
- resourceLocationCallbacks.add { keyCode, keyEvent ->
- if (!ignoreConsumer) {
- if (currentKeyConsumer != null) {
- return@add
- }
- }
- callback.invoke(keyCode, keyEvent)
- }
- }
-
fun registerRenderer(renderBuilder: RenderBuilder) {
val renderer = renderBuilder.build(connection, this)
rendererMap[renderBuilder.RESOURCE_LOCATION] = renderer
@@ -520,7 +373,7 @@ class RenderWindow(
screenResizeCallbacks.add(renderer)
}
if (renderer is FrustumChangeCallback) {
- camera.addFrustumChangeCallback(renderer)
+ inputHandler.camera.addFrustumChangeCallback(renderer)
}
}
@@ -531,8 +384,4 @@ class RenderWindow(
fun sendDebugMessage(message: String) {
connection.sender.sendFakeChatMessage(RenderConstants.DEBUG_MESSAGES_PREFIX + message)
}
-
- fun unregisterKeyBinding(it: ResourceLocation) {
- keyBindingCallbacks.remove(it)
- }
}
diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldRenderer.kt
index 36b8a0b02..124361e61 100644
--- a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldRenderer.kt
+++ b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/WorldRenderer.kt
@@ -118,7 +118,7 @@ class WorldRenderer(
// register keybindings
- renderWindow.registerKeyCallback(KeyBindingsNames.DEBUG_CLEAR_CHUNK_CACHE) { _, _ ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.DEBUG_CLEAR_CHUNK_CACHE) { _, _ ->
clearChunkCache()
renderWindow.sendDebugMessage("Cleared chunk cache!")
prepareWorld(world)
@@ -324,7 +324,7 @@ class WorldRenderer(
sectionMap[index] = meshCollection
- if (renderWindow.camera.frustum.containsChunk(chunkPosition, lowestBlockHeight, highestBlockHeight)) {
+ if (renderWindow.inputHandler.camera.frustum.containsChunk(chunkPosition, lowestBlockHeight, highestBlockHeight)) {
visibleChunks.getOrPut(chunkPosition, { ConcurrentHashMap() })[index] = meshCollection
} else {
visibleChunks[chunkPosition]?.remove(index)
@@ -392,7 +392,7 @@ class WorldRenderer(
for ((chunkLocation, indexMap) in allChunkSections) {
val visibleIndexMap: MutableMap = Collections.synchronizedMap(ConcurrentHashMap())
for ((index, mesh) in indexMap) {
- if (renderWindow.camera.frustum.containsChunk(chunkLocation, mesh.lowestBlockHeight, mesh.highestBlockHeight)) {
+ if (renderWindow.inputHandler.camera.frustum.containsChunk(chunkLocation, mesh.lowestBlockHeight, mesh.highestBlockHeight)) {
visibleIndexMap[index] = mesh
}
}
diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/HUDRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/HUDRenderer.kt
index 78829d37c..d7d968bb7 100644
--- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/HUDRenderer.kt
+++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/HUDRenderer.kt
@@ -65,7 +65,7 @@ class HUDRenderer(val connection: PlayConnection, val renderWindow: RenderWindow
registerDefaultElements()
- renderWindow.registerKeyCallback(KeyBindingsNames.TOGGLE_HUD) { _, _ ->
+ renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.TOGGLE_HUD) { _, _ ->
hudEnabled = !hudEnabled
}
@@ -116,7 +116,7 @@ class HUDRenderer(val connection: PlayConnection, val renderWindow: RenderWindow
properties.toggleKeyBinding?.let {
// register key binding
- renderWindow.registerKeyCallback(it) { _, _ ->
+ renderWindow.inputHandler.registerKeyCallback(it) { _, _ ->
if (enabledHUDElement.contains(resourceLocation)) {
enabledHUDElement.remove(resourceLocation)
} else {
@@ -142,7 +142,7 @@ class HUDRenderer(val connection: PlayConnection, val renderWindow: RenderWindow
}
element.first.toggleKeyBinding?.let {
- renderWindow.unregisterKeyBinding(it)
+ renderWindow.inputHandler.unregisterKeyBinding(it)
}
enabledHUDElement.remove(resourceLocation)
hudElements.remove(resourceLocation)
diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/chat/ChatBoxHUDElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/chat/ChatBoxHUDElement.kt
index 33a3f6147..b7c9282f7 100644
--- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/chat/ChatBoxHUDElement.kt
+++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/chat/ChatBoxHUDElement.kt
@@ -40,10 +40,10 @@ class ChatBoxHUDElement(hudRenderer: HUDRenderer) : HUDElement(hudRenderer), Scr
layout.addChild(Vec2i(0, 0), inputField)
inputField.apply()
- hudRenderer.renderWindow.registerKeyCallback(KeyBindingsNames.OPEN_CHAT) { _, _ ->
+ hudRenderer.renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.OPEN_CHAT) { _, _ ->
openChat()
}
- hudRenderer.renderWindow.registerKeyCallback(KeyBindingsNames.CLOSE_CHAT) { _, _ ->
+ hudRenderer.renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.CLOSE_CHAT) { _, _ ->
closeChat()
}
}
@@ -58,12 +58,12 @@ class ChatBoxHUDElement(hudRenderer: HUDRenderer) : HUDElement(hudRenderer), Scr
fun openChat() {
layout.addChild(Vec2i(0, 0), inputFieldBackground)
- hudRenderer.renderWindow.currentKeyConsumer = inputField
+ hudRenderer.renderWindow.inputHandler.currentKeyConsumer = inputField
}
fun closeChat() {
layout.removeChild(inputFieldBackground)
inputField.clearText()
- hudRenderer.renderWindow.currentKeyConsumer = null
+ hudRenderer.renderWindow.inputHandler.currentKeyConsumer = null
}
}
diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/debug/HUDWorldDebugNode.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/debug/HUDWorldDebugNode.kt
index 2541af237..9b33b8c95 100644
--- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/debug/HUDWorldDebugNode.kt
+++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/debug/HUDWorldDebugNode.kt
@@ -22,7 +22,7 @@ import de.bixilon.minosoft.util.UnitFormatter
class HUDWorldDebugNode(hudRenderer: HUDRenderer) : DebugScreenNode(hudRenderer) {
- private val camera = hudRenderer.renderWindow.camera
+ private val camera = hudRenderer.renderWindow.inputHandler.camera
private val worldRenderer = hudRenderer.renderWindow.rendererMap[WorldRenderer.RESOURCE_LOCATION] as WorldRenderer?
init {
@@ -128,8 +128,8 @@ class HUDWorldDebugNode(hudRenderer: HUDRenderer) : DebugScreenNode(hudRenderer)
}
private fun getFacing(): String {
- val yaw = hudRenderer.renderWindow.camera.playerEntity.rotation.yaw
- val pitch = hudRenderer.renderWindow.camera.playerEntity.rotation.pitch
+ val yaw = hudRenderer.renderWindow.inputHandler.camera.playerEntity.rotation.yaw
+ val pitch = hudRenderer.renderWindow.inputHandler.camera.playerEntity.rotation.pitch
val direction = Directions.byDirection(camera.cameraFront)
return "${Directions.byDirection(camera.cameraFront).name.toLowerCase()} ${direction.directionVector} (${formatRotation(yaw.toDouble())} / ${formatRotation(pitch.toDouble())})"
}
diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/connection/PlayConnection.kt b/src/main/java/de/bixilon/minosoft/protocol/network/connection/PlayConnection.kt
index 9c3e6b667..1d35a88e8 100644
--- a/src/main/java/de/bixilon/minosoft/protocol/network/connection/PlayConnection.kt
+++ b/src/main/java/de/bixilon/minosoft/protocol/network/connection/PlayConnection.kt
@@ -121,7 +121,7 @@ class PlayConnection(
for (entity in world.entityIdMap.values) {
entity.computeTimeStep(deltaTime)
}
- renderer?.renderWindow?.camera?.checkPosition()
+ renderer?.renderWindow?.inputHandler?.camera?.checkPosition()
velocityHandlerLastExecutionTime = currentTime
}
TimeWorker.addTask(velocityHandlerTask)