From c176ac17e82d94520f300a75813eb456d858a733 Mon Sep 17 00:00:00 2001 From: Perny Date: Mon, 16 Feb 2026 01:28:07 +0200 Subject: [PATCH] feat(api): image state rendering --- api/src/main/kotlin/Hud.kt | 37 +++++++++++++- api/src/main/kotlin/document/HudLayout.kt | 23 ++++++++- .../main/kotlin/preprocess/HUDPreprocessor.kt | 51 +++++++++++++++++-- 3 files changed, 105 insertions(+), 6 deletions(-) diff --git a/api/src/main/kotlin/Hud.kt b/api/src/main/kotlin/Hud.kt index 9724581..6d9203e 100644 --- a/api/src/main/kotlin/Hud.kt +++ b/api/src/main/kotlin/Hud.kt @@ -13,6 +13,9 @@ class Hud(val audience: Audience, val placeholderResolver: PlaceholderResolver) // Array of keys to hide val hiddenElements = mutableSetOf() + // Map of element key -> state value for dynamic images + val elementStates = mutableMapOf() + val bossbar = BossBar.bossBar( Component.empty(), 0.0f, @@ -28,7 +31,7 @@ class Hud(val audience: Audience, val placeholderResolver: PlaceholderResolver) get() = _hudDefinition set(value) { _hudDefinition = value - layout = value?.let { HudLayout(it.document.firstChild as Element, value, placeholderResolver, audience, hiddenElements) } + layout = value?.let { HudLayout(it.document.firstChild as Element, value, placeholderResolver, audience, hiddenElements, elementStates) } render() } @@ -76,6 +79,38 @@ class Hud(val audience: Audience, val placeholderResolver: PlaceholderResolver) render() } + /** + * Sets the state of a dynamic image element by key. + * + * Causes the HUD to re-render. + * @param key The key of the dynamic image element. + * @param value The state value to display. + */ + fun setState(key: String, value: String) { + elementStates[key] = value + render() + } + + /** + * Gets the current state value of a dynamic image element. + * @param key The key of the dynamic image element. + * @return The current state value, or null if no state is set. + */ + fun getState(key: String): String? { + return elementStates[key] + } + + /** + * Clears the state of a dynamic image element, reverting it to the default state. + * + * Causes the HUD to re-render. + * @param key The key of the dynamic image element. + */ + fun clearState(key: String) { + elementStates.remove(key) + render() + } + /** * Rerenders the HUD element. */ diff --git a/api/src/main/kotlin/document/HudLayout.kt b/api/src/main/kotlin/document/HudLayout.kt index 9b01afd..7684126 100644 --- a/api/src/main/kotlin/document/HudLayout.kt +++ b/api/src/main/kotlin/document/HudLayout.kt @@ -19,7 +19,8 @@ class HudLayout(layout: Element, val definition: AbstractHudDefinition, val placeholderResolver: PlaceholderResolver, val audience: net.kyori.adventure.audience.Audience, - val hiddenElements: Set = emptySet() + val hiddenElements: Set = emptySet(), + val elementStates: Map = emptyMap() ) { val mm = MiniMessage.miniMessage() val rootElement: Element = layout @@ -207,11 +208,12 @@ class HudLayout(layout: Element, return resolvePlaceholders(text, font) } "image" -> { + val resolvedFont = resolveImageFont(item, font) var component = Component.text("\uE300") if (item.getAttribute("shadow") == "false") { component = component.shadowColor(ShadowColor.shadowColor(0x00000000)) } - return component.font(font) + return component.font(resolvedFont) } "rectangle", "background" -> { if (item.tagName == "background" && item.attributes.get("type", "") == "fill") { @@ -228,6 +230,23 @@ class HudLayout(layout: Element, return Component.empty() } + private fun resolveImageFont(item: Element, defaultFont: Key): Key { + val key = item.attributes.getNamedItem("key")?.nodeValue ?: return defaultFont + val stateValue = elementStates[key] ?: return defaultFont + + // Find the matching child element + for (i in 0 until item.childNodes.length) { + val child = item.childNodes.item(i) + if (child is Element && child.tagName == "state" && child.getAttribute("value") == stateValue) { + val stateIsolate = child.attributes.get("isolate", "") + if (stateIsolate.isNotEmpty()) { + return Key.key(Constants.NAMESPACE, stateIsolate) + } + } + } + return defaultFont + } + private val placeholderPattern = Regex("%([^%]+)%") private fun resolvePlaceholders(text: String, font: Key): Component { diff --git a/api/src/main/kotlin/preprocess/HUDPreprocessor.kt b/api/src/main/kotlin/preprocess/HUDPreprocessor.kt index e936f81..4e4ca9b 100644 --- a/api/src/main/kotlin/preprocess/HUDPreprocessor.kt +++ b/api/src/main/kotlin/preprocess/HUDPreprocessor.kt @@ -32,25 +32,70 @@ class HUDPreprocessor(val document: Document, val getAsset: (name: String) -> By val y = element.getAttribute("y").toIntOrNull() + // Detect children for dynamic images + val stateChildren = if (element.tagName == "image") { + val children = mutableListOf() + for (i in 0 until element.childNodes.length) { + val node = element.childNodes.item(i) + if (node is Element && node.tagName == "state") { + children.add(node) + } + } + children + } else emptyList() + + val hasDynamicStates = stateChildren.isNotEmpty() + + // For dynamic images, don't pass src (it's on the children, not the parent) val isolate = isolateManager.createIsolate( y = y, anchor = element.getAttribute("anchor").takeIf { it.isNotEmpty() }?.let { Anchor.fromString(it) }, size = element.getAttribute("size").toIntOrNull(), - type = type, + type = if (hasDynamicStates) null else type, rounding = element.getAttribute("rounding").toIntOrNull(), pixelation = element.getAttribute("pixelation").toFloatOrNull(), color = element.getAttribute("color").takeIf { it.isNotEmpty() }?.let { parseColor(it) }, - src = element.getAttribute("src"), + src = if (hasDynamicStates) null else element.getAttribute("src"), ascent = element.getAttribute("ascent").toIntOrNull(), inheritedFrom = isolate ) element.setAttribute("isolate", isolate.getIsolateString()) + + // Handle dynamic image states + if (hasDynamicStates) { + val defaultValue = element.getAttribute("default").takeIf { it.isNotEmpty() } + + for (stateElement in stateChildren) { + val stateSrc = stateElement.getAttribute("src") + val stateIsolate = isolateManager.createIsolate( + y = y, + anchor = element.getAttribute("anchor").takeIf { it.isNotEmpty() }?.let { Anchor.fromString(it) }, + size = element.getAttribute("size").toIntOrNull(), + type = IsolateType.IMAGE, + src = stateSrc, + ascent = element.getAttribute("ascent").toIntOrNull(), + inheritedFrom = isolate + ) + stateElement.setAttribute("isolate", stateIsolate.getIsolateString()) + + // Set the parent image's isolate to the default state's isolate + if (stateElement.getAttribute("value") == defaultValue) { + element.setAttribute("isolate", stateIsolate.getIsolateString()) + } + } + + // If no default matched, use the first state's isolate + if (defaultValue == null || stateChildren.none { it.getAttribute("value") == defaultValue }) { + element.setAttribute("isolate", stateChildren[0].getAttribute("isolate")) + } + } + // text elements contain (X)MiniMessage content and should not have additional attributes if (element.tagName !== "text") { val children = element.childNodes for (i in 0 until children.length) { val node = children.item(i) - if (node is Element) { + if (node is Element && node.tagName != "state") { treverseNodes(node, isolate) } } -- knotmirror 0.0.1