hud: WIP: text

This commit is contained in:
Lukas 2021-06-08 15:37:17 +02:00
parent 2296b4ef43
commit 03f6683697
5 changed files with 82 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementVec2
import glm_.mat4x4.Mat4 import glm_.mat4x4.Mat4
abstract class HUDElement( abstract class HUDElement(
json: Map<String, Any>,
val position: HUDElementVec2, val position: HUDElementVec2,
val positionAnchor: HUDElementPositionAnchors, val positionAnchor: HUDElementPositionAnchors,
val content: ResourceLocation, val content: ResourceLocation,

View File

@ -15,6 +15,7 @@ object HUDElementSerializer {
val positionAnchor = HUDElementPositionAnchors.HUD_ELEMENT_POSITION_ATTACHMENTS_MAPPING[ResourceLocation((json["position"] as Map<*,*>)["location"].toString())]!! val positionAnchor = HUDElementPositionAnchors.HUD_ELEMENT_POSITION_ATTACHMENTS_MAPPING[ResourceLocation((json["position"] as Map<*,*>)["location"].toString())]!!
val size = HUDElementVec2.deserialize(json["size"]) val size = HUDElementVec2.deserialize(json["size"])
return HUD_ELEMENT_TYPES[ResourceLocation(json["type"].toString())]?.constructors?.first()?.call( return HUD_ELEMENT_TYPES[ResourceLocation(json["type"].toString())]?.constructors?.first()?.call(
json,
position, position,
positionAnchor, positionAnchor,
ResourceLocation(json["content"].toString()), ResourceLocation(json["content"].toString()),

View File

@ -16,13 +16,14 @@ import glm_.vec4.Vec4
import glm_.vec4.swizzle.xy import glm_.vec4.swizzle.xy
class HUDImageElement( class HUDImageElement(
json: Map<String, Any>,
position: HUDElementVec2, position: HUDElementVec2,
positionAnchor: HUDElementPositionAnchors, positionAnchor: HUDElementPositionAnchors,
content: ResourceLocation, content: ResourceLocation,
size: HUDElementVec2, size: HUDElementVec2,
realZ: Float, realZ: Float,
tint: RGBColor = ChatColors.WHITE, tint: RGBColor = ChatColors.WHITE,
) : HUDElement(position, positionAnchor, content, size, realZ, tint) { ) : HUDElement(json, position, positionAnchor, content, size, realZ, tint) {
private var mesh = SimpleTextureMesh() private var mesh = SimpleTextureMesh()

View File

@ -0,0 +1,46 @@
package de.bixilon.minosoft.gui.rendering.hud.elements.primitive
import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementPositionAnchors
import de.bixilon.minosoft.gui.rendering.hud.elements.position.HUDElementVec2
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import glm_.glm
class HUDTextElement(
json: Map<String, Any>,
position: HUDElementVec2,
positionAnchor: HUDElementPositionAnchors,
content: ResourceLocation,
size: HUDElementVec2,
realZ: Float,
tint: RGBColor = ChatColors.WHITE,
): HUDElement(json, position, positionAnchor, content, size, realZ, tint) {
val charImages = mutableListOf<HUDImageElement>()
val HUDTextElementAlignment = HUDTextElementAlignments.deserialize(json["alignment"] as String)!!
fun renderChatComponent(chatComponent: ChatComponent) {
var width = 0
var lines = 1
var maxHeight = 0
fun newLine() {
lines++
}
val chars = chatComponent.message.toCharArray()
for (char in chars) {
if (ProtocolDefinition.LINE_BREAK_CHARS.contains(char)) {
newLine()
}
val fontChar = hudRenderer.renderWindow.font.getChar(char)
width += fontChar.size.x
maxHeight = glm.max(maxHeight, fontChar.size.y)
}
}
}

View File

@ -0,0 +1,32 @@
package de.bixilon.minosoft.gui.rendering.hud.elements.primitive
import de.bixilon.minosoft.data.mappings.ResourceLocation
enum class HUDTextElementAlignments(val resourceLocation: ResourceLocation, val transform: (Float, Float) -> Float) {
LEFT(ResourceLocation("minosoft:left"), { _, position ->
position
}),
CENTER(ResourceLocation("minosoft:center"), { size, position ->
size/2 - position/2
}),
RIGHT(ResourceLocation("minosoft:right"), { size, position ->
size - position
}),
;
companion object {
val HUD_TEXT_ELEMENT_ALIGNMENTS = values()
val HUD_TEXT_ELEMENT_ALIGNMENT_NAMES = run {
val result = mutableMapOf<ResourceLocation, HUDTextElementAlignments>()
for (hudTextElementAlignment in HUD_TEXT_ELEMENT_ALIGNMENTS) {
result[hudTextElementAlignment.resourceLocation] = hudTextElementAlignment
}
result
}
fun deserialize(string: String): HUDTextElementAlignments? {
return HUD_TEXT_ELEMENT_ALIGNMENT_NAMES[ResourceLocation(string)]
}
}
}