hud: improve HUDMenus system

This commit is contained in:
Lukas 2021-07-05 18:43:04 +02:00
parent b2178e2cfa
commit 38abd793f8
7 changed files with 99 additions and 37 deletions

View File

@ -37,6 +37,8 @@ object KeyBindingsNames {
val QUIT_RENDERING = ResourceLocation("minosoft:quit_rendering") val QUIT_RENDERING = ResourceLocation("minosoft:quit_rendering")
val SHOW_PLAYERS = ResourceLocation("minosoft:show_players")
val TOGGLE_DEBUG_SCREEN = ResourceLocation("minosoft:toggle_debug_screen") val TOGGLE_DEBUG_SCREEN = ResourceLocation("minosoft:toggle_debug_screen")
val DEBUG_CLEAR_CHUNK_CACHE = ResourceLocation("minosoft:debug_clear_chunk_cache") val DEBUG_CLEAR_CHUNK_CACHE = ResourceLocation("minosoft:debug_clear_chunk_cache")
val DEBUG_POLYGON = ResourceLocation("minosoft:debug_polygon") val DEBUG_POLYGON = ResourceLocation("minosoft:debug_polygon")
@ -244,5 +246,10 @@ object KeyBindingsNames {
), ),
ignoreConsumer = true, ignoreConsumer = true,
), ),
SHOW_PLAYERS to KeyBinding(
mutableMapOf(
KeyAction.PRESS to mutableSetOf(KeyCodes.KEY_TAB)
)
),
) )
} }

View File

@ -17,45 +17,69 @@ package de.bixilon.minosoft.gui.rendering.hud
import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames
import de.bixilon.minosoft.data.registries.ResourceLocation import de.bixilon.minosoft.data.registries.ResourceLocation
enum class HUDMenus(val resourceLocation: ResourceLocation) { enum class HUDMenus(val resourceLocation: ResourceLocation, val keyBindingName: ResourceLocation? = null, val blockingGroup: Boolean = false) {
IN_GAME(ResourceLocation("minosoft:in_game")), IN_GAME(ResourceLocation("minosoft:in_game"), KeyBindingsNames.CLOSE, true),
PAUSE(ResourceLocation("minosoft:pause")), PAUSE(ResourceLocation("minosoft:pause"), KeyBindingsNames.CLOSE, true),
PLAYER_INVENTORY(ResourceLocation("minosoft:player_inventory")), PLAYER_INVENTORY(ResourceLocation("minosoft:player_inventory"), blockingGroup = true),
EDIT_SIGN(ResourceLocation("minosoft:edit_sign")), EDIT_SIGN(ResourceLocation("minosoft:edit_sign"), blockingGroup = true),
WRITE_CHAT(ResourceLocation("minosoft:write_chat")), 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 { companion object {
val HUD_MENUS = values() val VALUES = values()
val HUD_MENUS_MAPPING = run { val NAMES = run {
val result = mutableMapOf<ResourceLocation, HUDMenus>() val result = mutableMapOf<ResourceLocation, HUDMenus>()
for (hudMenu in HUD_MENUS) { for (hudMenu in VALUES) {
result[hudMenu.resourceLocation] = hudMenu result[hudMenu.resourceLocation] = hudMenu
} }
return@run result return@run result
} }
fun registerKeyCallbacks(hudRenderer: HUDRenderer) { val BLOCKING_MENUS = run {
hudRenderer.renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.TOGGLE_HUD) { val result = mutableSetOf<HUDMenus>()
hudRenderer.hudEnabled = !hudRenderer.hudEnabled for (hudMenu in VALUES) {
} if (hudMenu.blockingGroup) {
result.add(hudMenu)
hudRenderer.renderWindow.inputHandler.registerKeyCallback(KeyBindingsNames.CLOSE) {
if (hudRenderer.currentMenu != IN_GAME) {
hudRenderer.currentMenu = IN_GAME
} else {
hudRenderer.currentMenu = PAUSE
} }
} }
result
registerSimpleActivateKeyCallback(hudRenderer, WRITE_CHAT, KeyBindingsNames.OPEN_CHAT)
} }
private fun registerSimpleActivateKeyCallback(hudRenderer: HUDRenderer, menu: HUDMenus, keyBindingName: ResourceLocation) { fun registerKeyCallbacks(hudRenderer: HUDRenderer) {
hudRenderer.renderWindow.inputHandler.registerKeyCallback(keyBindingName) { for (hudMenu in VALUES) {
if (hudRenderer.currentMenu == IN_GAME) { if (hudMenu.keyBindingName == null) {
hudRenderer.currentMenu = menu 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)
}
} }
} }
} }

View File

@ -48,16 +48,30 @@ class HUDRenderer(val connection: PlayConnection, val renderWindow: RenderWindow
val hudTextTranslator = HUDTextTranslator(connection) val hudTextTranslator = HUDTextTranslator(connection)
var hudEnabled = true var hudEnabled = true
var currentMenu: HUDMenus = HUDMenus.IN_GAME var currentMenus = mutableSetOf<HUDMenus>()
set(value) {
if (field == value) { var ignoreNextMenuSet = false
return
} fun addMenu(hudMenus: HUDMenus) {
for (element in hudElements.values) { currentMenus.add(hudMenus)
element.prepareNext = true for (hudElement in hudElements.values) {
} hudElement.prepareNext = true
field = value
} }
}
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() { override fun init() {
hudShader.load(Minosoft.MINOSOFT_ASSETS_MANAGER) hudShader.load(Minosoft.MINOSOFT_ASSETS_MANAGER)

View File

@ -27,7 +27,7 @@ abstract class HUDElement(
private val activeOnMenu: HUDMenus? = null, private val activeOnMenu: HUDMenus? = null,
) { ) {
val isEnabled: Boolean get() = activeOnMenu?.let { val isEnabled: Boolean get() = activeOnMenu?.let {
hudRenderer.currentMenu == it it in hudRenderer.currentMenus
} ?: true } ?: true
lateinit var hudRenderer: HUDRenderer lateinit var hudRenderer: HUDRenderer

View File

@ -13,7 +13,7 @@ object HUDElementSerializer {
fun fromJson(json: Map<String, Any>): HUDElement? { fun fromJson(json: Map<String, Any>): HUDElement? {
val position = HUDElementPosition.deserialize(json["position"] as Map<String, Any>) val position = HUDElementPosition.deserialize(json["position"] as Map<String, Any>)
val size = HUDElementVec2.deserialize(json["size"]) 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( return HUD_ELEMENT_TYPES[ResourceLocation(json["type"].toString())]?.constructors?.first()?.call(
position, position,
size, size,

View File

@ -18,6 +18,7 @@ import de.bixilon.minosoft.data.locale.minecraft.Translator
import de.bixilon.minosoft.data.registries.ResourceLocation import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.text.ChatComponent import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.data.text.TextComponent 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.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.util.MMath.round import de.bixilon.minosoft.util.MMath.round
@ -40,8 +41,19 @@ class HUDTextTranslator(val connection: PlayConnection) : Translator {
init { init {
insertRunnables[ResourceLocation("minosoft:playerPosition_x")] = { connection -> 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
} }
} }

View File

@ -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.AABB
import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModelElement import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModelElement
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.MMath.round
import glm_.func.common.ceil import glm_.func.common.ceil
import glm_.func.common.clamp import glm_.func.common.clamp
import glm_.func.common.floor import glm_.func.common.floor
@ -513,4 +514,8 @@ object VecUtil {
z = 0.0 z = 0.0
} }
} }
fun Vec3d.round(digits: Int): Vec3d {
return Vec3d(this.x.round(digits), this.y.round(digits), this.z.round(digits))
}
} }