diff --git a/src/main/java/de/bixilon/minosoft/config/config/game/controls/KeyBindingsNames.kt b/src/main/java/de/bixilon/minosoft/config/config/game/controls/KeyBindingsNames.kt index 4d5fe7119..9a40c9ffc 100644 --- a/src/main/java/de/bixilon/minosoft/config/config/game/controls/KeyBindingsNames.kt +++ b/src/main/java/de/bixilon/minosoft/config/config/game/controls/KeyBindingsNames.kt @@ -37,6 +37,8 @@ object KeyBindingsNames { val QUIT_RENDERING = ResourceLocation("minosoft:quit_rendering") + val SHOW_PLAYERS = ResourceLocation("minosoft:show_players") + val TOGGLE_DEBUG_SCREEN = ResourceLocation("minosoft:toggle_debug_screen") val DEBUG_CLEAR_CHUNK_CACHE = ResourceLocation("minosoft:debug_clear_chunk_cache") val DEBUG_POLYGON = ResourceLocation("minosoft:debug_polygon") @@ -244,5 +246,10 @@ object KeyBindingsNames { ), ignoreConsumer = true, ), + SHOW_PLAYERS to KeyBinding( + mutableMapOf( + KeyAction.PRESS to mutableSetOf(KeyCodes.KEY_TAB) + ) + ), ) } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/HUDMenus.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/HUDMenus.kt index dcdf667a2..c182d3d27 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/HUDMenus.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/HUDMenus.kt @@ -17,45 +17,69 @@ package de.bixilon.minosoft.gui.rendering.hud import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames import de.bixilon.minosoft.data.registries.ResourceLocation -enum class HUDMenus(val resourceLocation: ResourceLocation) { - IN_GAME(ResourceLocation("minosoft:in_game")), - PAUSE(ResourceLocation("minosoft:pause")), - PLAYER_INVENTORY(ResourceLocation("minosoft:player_inventory")), - EDIT_SIGN(ResourceLocation("minosoft:edit_sign")), - WRITE_CHAT(ResourceLocation("minosoft:write_chat")), +enum class HUDMenus(val resourceLocation: ResourceLocation, val keyBindingName: ResourceLocation? = null, val blockingGroup: Boolean = false) { + IN_GAME(ResourceLocation("minosoft:in_game"), KeyBindingsNames.CLOSE, true), + PAUSE(ResourceLocation("minosoft:pause"), KeyBindingsNames.CLOSE, true), + PLAYER_INVENTORY(ResourceLocation("minosoft:player_inventory"), blockingGroup = true), + EDIT_SIGN(ResourceLocation("minosoft:edit_sign"), blockingGroup = true), + WRITE_CHAT(ResourceLocation("minosoft:write_chat"), KeyBindingsNames.OPEN_CHAT, true), + FURNACE(ResourceLocation("minosoft:furnace"), blockingGroup = true), + OTHER_INVENTORY(ResourceLocation("minosoft:other_inventory"), blockingGroup = true), + DEBUG_SCREEN(ResourceLocation("minosoft:debug_screen"), KeyBindingsNames.TOGGLE_DEBUG_SCREEN), + SHOW_PLAYERS(ResourceLocation("minosoft:show_players"), KeyBindingsNames.SHOW_PLAYERS), ; companion object { - val HUD_MENUS = values() + val VALUES = values() - val HUD_MENUS_MAPPING = run { + val NAMES = run { val result = mutableMapOf() - for (hudMenu in HUD_MENUS) { + for (hudMenu in VALUES) { result[hudMenu.resourceLocation] = hudMenu } return@run result } - fun registerKeyCallbacks(hudRenderer: HUDRenderer) { - hudRenderer.renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.TOGGLE_HUD) { - hudRenderer.hudEnabled = !hudRenderer.hudEnabled - } - - hudRenderer.renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.CLOSE) { - if (hudRenderer.currentMenu != IN_GAME) { - hudRenderer.currentMenu = IN_GAME - } else { - hudRenderer.currentMenu = PAUSE + val BLOCKING_MENUS = run { + val result = mutableSetOf() + for (hudMenu in VALUES) { + if (hudMenu.blockingGroup) { + result.add(hudMenu) } } - - registerSimpleActivateKeyCallback(hudRenderer, WRITE_CHAT, KeyBindingsNames.OPEN_CHAT) + result } - private fun registerSimpleActivateKeyCallback(hudRenderer: HUDRenderer, menu: HUDMenus, keyBindingName: ResourceLocation) { - hudRenderer.renderWindow.inputHandler.registerKeyCallback(keyBindingName) { - if (hudRenderer.currentMenu == IN_GAME) { - hudRenderer.currentMenu = menu + fun registerKeyCallbacks(hudRenderer: HUDRenderer) { + for (hudMenu in VALUES) { + if (hudMenu.keyBindingName == null) { + continue + } + if (hudMenu.blockingGroup) { + hudRenderer.renderWindow.inputHandler.registerKeyCallback(hudMenu.keyBindingName) { + if (hudRenderer.ignoreNextMenuSet) { + hudRenderer.ignoreNextMenuSet = false + return@registerKeyCallback + } + val wasInUse = hudMenu in hudRenderer.currentMenus + hudRenderer.removeBlockingMenus () + if (wasInUse) { + hudRenderer.addMenu(IN_GAME) + } else { + if (hudMenu == IN_GAME) { + hudRenderer.ignoreNextMenuSet = true + } + hudRenderer.addMenu(hudMenu) + } + } + continue + } + hudRenderer.renderWindow.inputHandler.registerKeyCallback(hudMenu.keyBindingName) { + if (hudMenu !in hudRenderer.currentMenus) { + hudRenderer.addMenu(hudMenu) + } else { + hudRenderer.removeMenu(hudMenu) + } } } } 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 319bbc7d1..bff3caca8 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 @@ -48,16 +48,30 @@ class HUDRenderer(val connection: PlayConnection, val renderWindow: RenderWindow val hudTextTranslator = HUDTextTranslator(connection) var hudEnabled = true - var currentMenu: HUDMenus = HUDMenus.IN_GAME - set(value) { - if (field == value) { - return - } - for (element in hudElements.values) { - element.prepareNext = true - } - field = value + var currentMenus = mutableSetOf() + + var ignoreNextMenuSet = false + + fun addMenu(hudMenus: HUDMenus) { + currentMenus.add(hudMenus) + for (hudElement in hudElements.values) { + hudElement.prepareNext = true } + } + + fun removeMenu(hudMenus: HUDMenus) { + currentMenus.remove(hudMenus) + for (hudElement in hudElements.values) { + hudElement.prepareNext = true + } + } + + fun removeBlockingMenus() { + currentMenus.removeAll(HUDMenus.BLOCKING_MENUS) + for (hudElement in hudElements.values) { + hudElement.prepareNext = true + } + } override fun init() { hudShader.load(Minosoft.MINOSOFT_ASSETS_MANAGER) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/HUDElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/HUDElement.kt index 16aeac722..2fbf101b6 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/HUDElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/HUDElement.kt @@ -27,7 +27,7 @@ abstract class HUDElement( private val activeOnMenu: HUDMenus? = null, ) { val isEnabled: Boolean get() = activeOnMenu?.let { - hudRenderer.currentMenu == it + it in hudRenderer.currentMenus } ?: true lateinit var hudRenderer: HUDRenderer diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/HUDElementSerializer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/HUDElementSerializer.kt index aa3bc0b49..b94a041e8 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/HUDElementSerializer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/HUDElementSerializer.kt @@ -13,7 +13,7 @@ object HUDElementSerializer { fun fromJson(json: Map): HUDElement? { val position = HUDElementPosition.deserialize(json["position"] as Map) val size = HUDElementVec2.deserialize(json["size"]) - val menu = json["menu"]?.let { HUDMenus.HUD_MENUS_MAPPING[ResourceLocation(it as String)]!! } + val menu = (json["menu"] as String?)?.let { HUDMenus.NAMES[ResourceLocation(it)]!! } return HUD_ELEMENT_TYPES[ResourceLocation(json["type"].toString())]?.constructors?.first()?.call( position, size, diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDTextTranslator.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDTextTranslator.kt index 7421076ee..b115f8aaa 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDTextTranslator.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/text/HUDTextTranslator.kt @@ -18,6 +18,7 @@ import de.bixilon.minosoft.data.locale.minecraft.Translator import de.bixilon.minosoft.data.registries.ResourceLocation import de.bixilon.minosoft.data.text.ChatComponent import de.bixilon.minosoft.data.text.TextComponent +import de.bixilon.minosoft.gui.rendering.util.VecUtil.round import de.bixilon.minosoft.protocol.network.connection.PlayConnection import de.bixilon.minosoft.util.MMath.round @@ -40,8 +41,19 @@ class HUDTextTranslator(val connection: PlayConnection) : Translator { init { insertRunnables[ResourceLocation("minosoft:playerPosition_x")] = { connection -> - connection.player.position.x.round(2).toString() + connection.player.position.x.round(ROUND_DIGITS).toString() + } + insertRunnables[ResourceLocation("minosoft:playerPosition_y")] = { connection -> + connection.player.position.y.round(ROUND_DIGITS).toString() + } + insertRunnables[ResourceLocation("minosoft:playerPosition_z")] = { connection -> + connection.player.position.z.round(ROUND_DIGITS).toString() + } + insertRunnables[ResourceLocation("minosoft:playerPosition_x")] = { connection -> + connection.player.position.round(ROUND_DIGITS).toString() } } + + const val ROUND_DIGITS = 2 } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/util/VecUtil.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/util/VecUtil.kt index b5232fd7e..c161dd6ea 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/util/VecUtil.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/util/VecUtil.kt @@ -25,6 +25,7 @@ import de.bixilon.minosoft.data.registries.blocks.types.Block import de.bixilon.minosoft.gui.rendering.chunk.models.AABB import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModelElement import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition +import de.bixilon.minosoft.util.MMath.round import glm_.func.common.ceil import glm_.func.common.clamp import glm_.func.common.floor @@ -513,4 +514,8 @@ object VecUtil { z = 0.0 } } + + fun Vec3d.round(digits: Int): Vec3d { + return Vec3d(this.x.round(digits), this.y.round(digits), this.z.round(digits)) + } }