mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 10:25:06 -04:00
hud: add json serializing
This commit is contained in:
parent
995e923f6c
commit
54d5a3c9e4
@ -1,5 +1,6 @@
|
||||
package de.bixilon.minosoft.gui.rendering.hud.elements.position
|
||||
|
||||
import com.squareup.moshi.JsonWriter
|
||||
import de.bixilon.minosoft.data.Axes
|
||||
import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer
|
||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil.EMPTY
|
||||
@ -25,6 +26,11 @@ class HUDElementVec2(
|
||||
return HUDElementVec2(x + vec2.x, xUnit, y + vec2.y, yUnit)
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val ZERO_VECTOR = HUDElementVec2(Vec2.EMPTY, HUDElementPositionUnits.PIXELS)
|
||||
|
||||
|
@ -13,6 +13,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.HUDElementPositionAnchors
|
||||
@ -79,4 +80,14 @@ open class HUDElement(
|
||||
open fun update() {}
|
||||
|
||||
open fun prepare(mesh: SimpleTextureMesh) {}
|
||||
|
||||
open fun toJson(jsonWriter: JsonWriter) {
|
||||
jsonWriter.name("position")
|
||||
position.toJson(jsonWriter)
|
||||
jsonWriter.name("size")
|
||||
jsonWriter.beginObject()
|
||||
size.toJson(jsonWriter)
|
||||
jsonWriter.endObject()
|
||||
jsonWriter.name("z").value(z)
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.bixilon.minosoft.gui.rendering.hud.elements.primitive
|
||||
|
||||
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
|
||||
@ -47,6 +48,17 @@ class HUDElementPosition(
|
||||
return HUDElementPosition(position + vec2, anchor, parentName, parentAnchor)
|
||||
}
|
||||
|
||||
fun toJson(jsonWriter: JsonWriter) {
|
||||
jsonWriter.beginObject()
|
||||
position.toJson(jsonWriter)
|
||||
jsonWriter.name("anchor").value(anchor.resourceLocation.full)
|
||||
parentName?.let {
|
||||
jsonWriter.name("parent").value(parentName)
|
||||
jsonWriter.name("parent_anchor").value(parentAnchor!!.resourceLocation.full)
|
||||
}
|
||||
jsonWriter.endObject()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun deserialize(json: Map<String, Any>): HUDElementPosition {
|
||||
val position = HUDElementVec2.deserialize(json)!!
|
||||
|
@ -24,6 +24,11 @@ object HUDElementSerializer {
|
||||
|
||||
@ToJson
|
||||
fun toJson(jsonWriter: JsonWriter, hudElement: HUDElement?) {
|
||||
hudElement ?: return
|
||||
jsonWriter.beginObject()
|
||||
jsonWriter.name("type").value(HUD_ELEMENT_TYPES_REVERSED[hudElement::class]!!.toString())
|
||||
hudElement.toJson(jsonWriter)
|
||||
jsonWriter.endObject()
|
||||
}
|
||||
|
||||
private val HUD_ELEMENT_TYPES = mutableMapOf(
|
||||
@ -31,4 +36,6 @@ object HUDElementSerializer {
|
||||
ResourceLocation("minosoft:text_element") to HUDTextElement::class,
|
||||
// modding here!
|
||||
)
|
||||
|
||||
val HUD_ELEMENT_TYPES_REVERSED = HUD_ELEMENT_TYPES.entries.associateBy({ it.value }) { it.key }
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.bixilon.minosoft.gui.rendering.hud.elements.primitive
|
||||
|
||||
import com.squareup.moshi.JsonWriter
|
||||
import de.bixilon.minosoft.data.registries.ResourceLocation
|
||||
import de.bixilon.minosoft.data.text.ChatColors
|
||||
import de.bixilon.minosoft.data.text.RGBColor
|
||||
@ -76,6 +77,15 @@ class HUDImageElement: HUDElement {
|
||||
textureMesh.load()
|
||||
}
|
||||
|
||||
override fun toJson(jsonWriter: JsonWriter) {
|
||||
super.toJson(jsonWriter)
|
||||
if (textureName != null) {
|
||||
jsonWriter.name("texture").value(textureName.full)
|
||||
}
|
||||
jsonWriter.name("tint")
|
||||
RGBColorSerializer.toJson(jsonWriter, tint)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val DRAW_ORDER = arrayOf(0, 1, 2, 1, 3, 2)
|
||||
}
|
||||
|
@ -14,6 +14,7 @@
|
||||
|
||||
package de.bixilon.minosoft.gui.rendering.hud.elements.text
|
||||
|
||||
import com.squareup.moshi.JsonWriter
|
||||
import de.bixilon.minosoft.Minosoft
|
||||
import de.bixilon.minosoft.data.text.BaseComponent
|
||||
import de.bixilon.minosoft.data.text.ChatComponent
|
||||
@ -114,6 +115,32 @@ class HUDTextElement : HUDElement {
|
||||
return newLinePositions
|
||||
}
|
||||
|
||||
override fun toJson(jsonWriter: JsonWriter) {
|
||||
super.toJson(jsonWriter)
|
||||
jsonWriter.name("alignment").value(alignment.resourceLocation.full)
|
||||
jsonWriter.name("content")
|
||||
when (contents) {
|
||||
is List<*> -> {
|
||||
jsonWriter.beginArray()
|
||||
for (item in contents as List<*>) {
|
||||
when (item) {
|
||||
is String -> jsonWriter.value(item)
|
||||
is Map<*, *> -> {
|
||||
jsonWriter.beginObject()
|
||||
for ((key, value) in item) {
|
||||
jsonWriter.name(key as String).value(value as String)
|
||||
}
|
||||
jsonWriter.endObject()
|
||||
}
|
||||
}
|
||||
}
|
||||
jsonWriter.endArray()
|
||||
}
|
||||
is String -> jsonWriter.value(contents as String)
|
||||
else -> TODO("no handling possible for $contents")
|
||||
}
|
||||
}
|
||||
|
||||
private fun getLastSpacerElementPosition(position: Int): Int {
|
||||
for (i in 0..position) {
|
||||
val j = glm.max(0, position - i - 1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user