hud: add default hud elements

This commit is contained in:
Lukas 2021-07-11 18:31:04 +02:00
parent 8e91753c90
commit 540a974b37
13 changed files with 83 additions and 128 deletions

View File

@ -14,9 +14,19 @@
package de.bixilon.minosoft.config.config.game
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.gui.rendering.hud.DefaultHUDElements
import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.HUDElement
data class HUDGameConfig(
var scale: Float = 2.0f,
var elements: MutableMap<ResourceLocation, HUDElement> = mutableMapOf(),
)
) {
init {
for ((resourceLocation, element) in DefaultHUDElements.VALUES) {
if (!elements.containsKey(resourceLocation)) {
// add key binding
elements[resourceLocation] = element
}
}
}
}

View File

@ -0,0 +1,36 @@
/*
* Minosoft
*
* Copyright (C) 2021 Lukas Eisenhauer
*
* 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.hud
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPosition
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPositionAnchors
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPositionUnits
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementVec2
import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.HUDImageElement
import glm_.vec2.Vec2
object DefaultHUDElements {
val VALUES = mutableMapOf(
ResourceLocation("minosoft:crosshair") to HUDImageElement(
HUDElementPosition(
HUDElementVec2(Vec2(50, 50), HUDElementPositionUnits.UNIT),
HUDElementPositionAnchors.CENTER
),
HUDElementVec2(Vec2(25, 25), HUDElementPositionUnits.SCALED_PIXELS),
ResourceLocation("minecraft:crosshair"),
)
)
}

View File

@ -1,27 +0,0 @@
package de.bixilon.minosoft.gui.rendering.hud.elements
import de.bixilon.minosoft.data.registries.ResourceLocation
import glm_.vec2.Vec2i
enum class HUDElementPositionAnchors(val resourceLocation: ResourceLocation, val quadTransform: (Vec2i, Vec2i) -> Array<Vec2i>) {
CENTER(ResourceLocation("minosoft:center"), { position, size ->
arrayOf(
Vec2i(position.x + size.x * 0.5f, position.y + size.y * 0.5f),
Vec2i(position.x + size.x * 0.5f, position.y - size.y * 0.5f),
Vec2i(position.x - size.x * 0.5f, position.y + size.y * 0.5f),
Vec2i(position.x - size.x * 0.5f, position.y - size.y * 0.5f),
)
})
;
companion object {
val HUD_ELEMENT_POSITION_ATTACHMENT_OPTIONS = values()
val HUD_ELEMENT_POSITION_ATTACHMENTS_MAPPING = run {
val result = mutableMapOf<ResourceLocation, HUDElementPositionAnchors>()
for (hudPositionAttachmentOption in HUD_ELEMENT_POSITION_ATTACHMENT_OPTIONS) {
result[hudPositionAttachmentOption.resourceLocation] = hudPositionAttachmentOption
}
result
}
}
}

View File

@ -1,50 +0,0 @@
package de.bixilon.minosoft.gui.rendering.hud.elements
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.data.Axes
import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer
import de.bixilon.minosoft.gui.rendering.util.VecUtil.get
enum class HUDElementPositionUnits(val abbreviation: String, val realPositionConversion: (Float, HUDRenderer, Axes) -> Float) {
PIXELS("px", { it, _, _ ->
it
}),
SCALED_PIXELS("pxs", { it, _, _ ->
it * Minosoft.getConfig().config.game.hud.scale
}),
UNIT("%", { it, hudRenderer, axis ->
it * hudRenderer.renderWindow.screenDimensions[axis] / TOTAL_UNITS
}),
SCALED_UNIT("%s", { it, hudRenderer, axis ->
it * hudRenderer.renderWindow.screenDimensions[axis] / TOTAL_UNITS * Minosoft.getConfig().config.game.hud.scale
})
;
companion object {
private val NUMBER_REGEX = Regex("-?\\d+(.?\\d+)?")
fun deserializeNumber(string: String): Float {
val stringValue = NUMBER_REGEX.find(string)?.value ?: throw IllegalArgumentException("unknown value $string")
return stringValue.toFloatOrNull() ?: throw IllegalArgumentException("unknown value $string")
}
private val UNIT_REGEX = Regex("(?!\\d+(.?\\d+))\\D+\\B")
fun deserialize(string: String): HUDElementPositionUnits {
val stringValue = UNIT_REGEX.find(string)?.value ?: throw IllegalArgumentException("unknown value $string")
return HUD_ELEMENT_UNIT_MAP[stringValue] ?: throw IllegalArgumentException("unknown value $string")
}
val HUD_ELEMENT_POSITION_UNITS = values()
val HUD_ELEMENT_UNIT_MAP = run {
val result = mutableMapOf<String, HUDElementPositionUnits>()
for (unit in HUD_ELEMENT_POSITION_UNITS) {
result[unit.abbreviation] = unit
}
result
}
const val TOTAL_UNITS = 100f
}
}

View File

@ -1,35 +0,0 @@
package de.bixilon.minosoft.gui.rendering.hud.elements
import de.bixilon.minosoft.data.Axes
import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer
import glm_.vec2.Vec2i
class HUDElementVec2(
val x: Float,
val xUnit: HUDElementPositionUnits,
val y: Float,
val yUnit: HUDElementPositionUnits,
) {
fun getRealVector(hudRenderer: HUDRenderer): Vec2i {
return Vec2i(
xUnit.realPositionConversion.invoke(x, hudRenderer, Axes.X),
yUnit.realPositionConversion.invoke(y, hudRenderer, Axes.Y)
)
}
companion object {
fun deserialize(data: Any?): HUDElementVec2? {
if (data !is Map<*, *>) {
return null
}
val xString = data["x"] as String
val yString = data["y"] as String
val x = HUDElementPositionUnits.deserializeNumber(xString)
val xUnit = HUDElementPositionUnits.deserialize(xString)
val y = HUDElementPositionUnits.deserializeNumber(yString)
val yUnit = HUDElementPositionUnits.deserialize(yString)
return HUDElementVec2(x, xUnit, y, yUnit)
}
}
}

View File

@ -1,11 +1,24 @@
package de.bixilon.minosoft.gui.rendering.hud.elements.primitive
/*
* Minosoft
*
* Copyright (C) 2021 Lukas Eisenhauer
*
* 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.hud.elements.position
import com.squareup.moshi.JsonWriter
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPositionAnchors
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementVec2
import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.HUDElement
import glm_.vec2.Vec2
import glm_.vec2.Vec2i
@ -61,7 +74,7 @@ class HUDElementPosition(
companion object {
fun deserialize(json: Map<String, Any>): HUDElementPosition {
val position = HUDElementVec2.deserialize(json)!!
val position = HUDElementVec2.deserialize(json)
val positionAnchor = HUDElementPositionAnchors.HUD_ELEMENT_POSITION_ATTACHMENTS_MAPPING[ResourceLocation(json["anchor"].toString())]!!
val parent = json["parent"] as String?
val parentAnchor = json["parent_anchor"]?.let {

View File

@ -2,7 +2,6 @@ package de.bixilon.minosoft.gui.rendering.hud.elements.position
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.HUDElement
import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.HUDElementPosition
import de.bixilon.minosoft.gui.rendering.util.VecUtil.EMPTY
import de.bixilon.minosoft.gui.rendering.util.VecUtil.ONES
import de.bixilon.minosoft.gui.rendering.util.VecUtil.toVec2
@ -38,7 +37,7 @@ enum class HUDElementPositionAnchors(
position + size,
) },
Vec2.EMPTY,
{ left, _, y, _ -> HUDElementPosition(HUDElementVec2(Vec2(left, y), HUDElementPositionUnits.PIXELS), TOP_LEFT)},
{ left, _, y, _ -> HUDElementPosition(HUDElementVec2(Vec2(left, y), HUDElementPositionUnits.PIXELS), TOP_LEFT) },
),
TOP_RIGHT(
ResourceLocation("minosoft:top_right"),
@ -65,7 +64,7 @@ enum class HUDElementPositionAnchors(
Vec2i(position.x + size.x, position.y),
) },
Vec2(0f, 1f),
{ left, _, y, _ -> HUDElementPosition(HUDElementVec2(Vec2(left, y), HUDElementPositionUnits.PIXELS), BOTTOM_LEFT)},
{ left, _, y, _ -> HUDElementPosition(HUDElementVec2(Vec2(left, y), HUDElementPositionUnits.PIXELS), BOTTOM_LEFT) },
),
BOTTOM_RIGHT(
ResourceLocation("minosoft:bottom_right"),

View File

@ -28,16 +28,16 @@ enum class HUDElementPositionUnits(val abbreviation: String, val realPositionCon
return stringValue.toFloatOrNull() ?: throw IllegalArgumentException("unknown value $string")
}
private val UNIT_REGEX = Regex("(?!-?\\d+(.?\\d+))\\D+")
private val UNIT_REGEX = Regex("(?!-?\\d+(.?\\d+))\\D+$")
fun deserialize(string: String): HUDElementPositionUnits {
val stringValue = UNIT_REGEX.find(string)?.value ?: throw IllegalArgumentException("unknown value $string")
return HUD_ELEMENT_UNIT_MAP[stringValue] ?: throw IllegalArgumentException("unknown value $string")
return HUD_ELEMENT_UNIT_MAP[stringValue] ?: throw IllegalArgumentException("unknown unit $stringValue")
}
val HUD_ELEMENT_POSITION_UNITS = values()
private val HUD_ELEMENT_POSITION_UNITS = values()
val HUD_ELEMENT_UNIT_MAP = run {
private val HUD_ELEMENT_UNIT_MAP = run {
val result = mutableMapOf<String, HUDElementPositionUnits>()
for (unit in HUD_ELEMENT_POSITION_UNITS) {
result[unit.abbreviation] = unit

View File

@ -27,14 +27,14 @@ class HUDElementVec2(
}
fun toJson(jsonWriter: JsonWriter) {
jsonWriter.name("x").value(String.format("%s%s", x, xUnit.abbreviation))
jsonWriter.name("y").value(String.format("%s%s", y, yUnit.abbreviation))
jsonWriter.name("x").value("$x${xUnit.abbreviation}")
jsonWriter.name("y").value("$y${yUnit.abbreviation}")
}
companion object {
private val ZERO_VECTOR = HUDElementVec2(Vec2.EMPTY, HUDElementPositionUnits.PIXELS)
fun deserialize(data: Any?): HUDElementVec2? {
fun deserialize(data: Any?): HUDElementVec2 {
if (data !is Map<*, *>) {
return ZERO_VECTOR
}

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.gui.rendering.hud.elements.primitive
import com.squareup.moshi.JsonWriter
import de.bixilon.minosoft.gui.rendering.hud.HUDMenus
import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPosition
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPositionAnchors
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementVec2
import de.bixilon.minosoft.gui.rendering.util.VecUtil.toVec2

View File

@ -5,6 +5,7 @@ import com.squareup.moshi.JsonWriter
import com.squareup.moshi.ToJson
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.gui.rendering.hud.HUDMenus
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPosition
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementVec2
import de.bixilon.minosoft.gui.rendering.hud.elements.text.HUDTextElement

View File

@ -8,6 +8,7 @@ import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.hud.HUDMenus
import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer
import de.bixilon.minosoft.gui.rendering.hud.atlas.TextureLike
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPosition
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementVec2
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
import de.bixilon.minosoft.gui.rendering.util.mesh.SimpleTextureMesh
@ -30,6 +31,12 @@ class HUDImageElement: HUDElement {
tint = json?.get("tint")?.let{ RGBColorSerializer.fromJsonValue(it) } ?: ChatColors.WHITE
}
constructor(position: HUDElementPosition, size: HUDElementVec2, textureName: ResourceLocation, tint: RGBColor? = null, activeOnMenu: HUDMenus? = null, z: Int = 0):
super(position, size, z, activeOnMenu) {
this.textureName = textureName
this.tint = tint ?: ChatColors.WHITE
}
constructor(position: HUDElementPosition, size: HUDElementVec2, z: Int): super(position, size, z) {
textureName = null
tint = ChatColors.WHITE

View File

@ -22,11 +22,11 @@ import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.TextComponent
import de.bixilon.minosoft.gui.rendering.font.Font
import de.bixilon.minosoft.gui.rendering.hud.HUDMenus
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPosition
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPositionAnchors
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPositionUnits
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementVec2
import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.HUDElement
import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.HUDElementPosition
import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.HUDImageElement
import de.bixilon.minosoft.gui.rendering.util.VecUtil.toVec2
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh