jackson: fix color serializing

Not perfect, but pretty much the only way
This commit is contained in:
Moritz Zwerger 2025-03-31 15:23:45 +02:00
parent a21b1ea4c9
commit 83c3337f38
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 26 additions and 25 deletions

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2025 Moritz Zwerger
*
* 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.
*
@ -23,10 +23,6 @@ open class SimpleDelegate<T>(
private val verify: ((T) -> Unit)? = null,
) : DataObserver<T>(default), AbstractProfileDelegate<T> {
init {
}
override fun set(value: T): T {
validate(value)
return super.set(value)

View File

@ -15,12 +15,12 @@ package de.bixilon.minosoft.config.profile.delegate.types
import de.bixilon.minosoft.config.profile.delegate.SimpleDelegate
import de.bixilon.minosoft.config.profile.profiles.Profile
import de.bixilon.minosoft.data.text.formatting.color.RGBAColor
import de.bixilon.minosoft.data.text.formatting.color.Color
open class ColorDelegate(
override val profile: Profile,
default: RGBAColor,
) : SimpleDelegate<RGBAColor>(profile, default) {
default: Color,
) : SimpleDelegate<Color>(profile, default) {
override fun validate(value: RGBAColor) = Unit
override fun validate(value: Color) = Unit
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2025 Moritz Zwerger
*
* 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.
*

View File

@ -29,6 +29,9 @@ interface Color {
val rgb: Int
fun rgb() = RGBColor(red, green, blue)
fun rgba() = RGBAColor(red, green, blue)
companion object {
const val BITS = 8

View File

@ -66,7 +66,8 @@ value class RGBAColor(val argb: Int) : Color, TextFormattable {
inline fun with(red: Int = this.red, green: Int = this.green, blue: Int = this.blue, alpha: Int = this.alpha) = RGBAColor(red, green, blue, alpha)
inline fun with(red: Float = this.redf, green: Float = this.greenf, blue: Float = this.bluef, alpha: Float = this.alphaf) = RGBAColor(red, green, blue, alpha)
fun rgb() = RGBColor(red, green, blue)
override inline fun rgb() = RGBColor(red, green, blue)
override inline fun rgba() = this
fun mix(other: RGBAColor) = RGBAColor((red + other.red) / 2, (green + other.green) / 2, (blue + other.blue) / 2, (alpha + other.alpha) / 2)

View File

@ -58,7 +58,8 @@ value class RGBColor(override val rgb: Int) : Color, TextFormattable {
inline fun with(red: Float = this.redf, green: Float = this.greenf, blue: Float = this.bluef) = RGBColor(red, green, blue)
inline fun with(red: Float = this.redf, green: Float = this.greenf, blue: Float = this.bluef, alpha: Float) = RGBAColor(red, green, blue, alpha)
fun rgba() = RGBAColor(red, green, blue, MAX)
override inline fun rgb() = this
override inline fun rgba() = RGBAColor(red, green, blue, MAX)
fun mix(other: RGBColor) = RGBColor((red + other.red) / 2, (green + other.green) / 2, (blue + other.blue) / 2)

View File

@ -137,11 +137,11 @@ class BlockOutlineRenderer(
}
target.state.block.getOutlineShape(session, target.blockPosition, target.state)?.let { mesh.drawVoxelShape(it, blockOffset, RenderConstants.DEFAULT_LINE_WIDTH, profile.outlineColor) }
target.state.block.getOutlineShape(session, target.blockPosition, target.state)?.let { mesh.drawVoxelShape(it, blockOffset, RenderConstants.DEFAULT_LINE_WIDTH, profile.outlineColor.rgba()) }
if (target.state.block is CollidableBlock && profile.collisions) { // TODO: block entity
target.state.block.getCollisionShape(session, EntityCollisionContext(session.player), target.blockPosition, target.state, null)?.let { mesh.drawVoxelShape(it, blockOffset, RenderConstants.DEFAULT_LINE_WIDTH, profile.collisionColor, 0.005f) }
target.state.block.getCollisionShape(session, EntityCollisionContext(session.player), target.blockPosition, target.state, null)?.let { mesh.drawVoxelShape(it, blockOffset, RenderConstants.DEFAULT_LINE_WIDTH, profile.collisionColor.rgba(), 0.005f) }
}
this.nextMesh = mesh

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2024 Moritz Zwerger
* Copyright (C) 2020-2025 Moritz Zwerger
*
* 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.
*
@ -93,7 +93,7 @@ class CrosshairHUDElement(guiRenderer: GUIRenderer) : CustomHUDElement(guiRender
val mesh = GUIMesh(context, guiRenderer.halfSize, BufferedArrayFloatList(42))
val start = (guiRenderer.scaledSize - CROSSHAIR_SIZE) / 2
mesh.addQuad(start, start + CROSSHAIR_SIZE, crosshairAtlasElement, crosshairProfile.color, null)
mesh.addQuad(start, start + CROSSHAIR_SIZE, crosshairAtlasElement, crosshairProfile.color.rgba(), null)
// ToDo: Attack indicator

View File

@ -21,28 +21,28 @@ import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import de.bixilon.minosoft.data.text.formatting.color.RGBColor
import de.bixilon.minosoft.data.text.formatting.color.Color
import de.bixilon.minosoft.data.text.formatting.color.RGBColor.Companion.rgb
object RGBColorSerializer : SimpleModule() {
init {
addDeserializer(RGBColor::class.java, Deserializer)
addSerializer(RGBColor::class.java, Serializer)
addDeserializer(Color::class.java, Deserializer)
addSerializer(Color::class.java, Serializer)
}
object Deserializer : StdDeserializer<RGBColor>(RGBColor::class.java) {
object Deserializer : StdDeserializer<Color>(Color::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext?) = when (parser.currentToken) {
JsonToken.VALUE_NUMBER_INT -> RGBColor(parser.valueAsInt)
JsonToken.VALUE_NUMBER_INT -> parser.valueAsInt.rgb()
JsonToken.VALUE_STRING -> parser.valueAsString.rgb()
else -> TODO("Can not parse color!")
}
else -> TODO("Can not parse color!")
}
}
object Serializer : StdSerializer<RGBColor>(RGBColor::class.java) {
object Serializer : StdSerializer<Color>(Color::class.java) {
override fun serialize(value: RGBColor?, generator: JsonGenerator, provider: SerializerProvider?) {
override fun serialize(value: Color?, generator: JsonGenerator, provider: SerializerProvider?) {
generator.writeString(value?.toString())
}
}