From ecc74990214755acd86c114e8f08ab0cf1661d90 Mon Sep 17 00:00:00 2001 From: toxicity Date: Tue, 12 Nov 2024 20:36:30 +0900 Subject: [PATCH] Restore minecraft image glyph. --- .../kr/toxicity/hud/hud/HudTextElement.kt | 22 +++--------- .../kr/toxicity/hud/layout/TextLayout.kt | 26 ++++++++++++++ .../toxicity/hud/manager/MinecraftManager.kt | 36 ++++++++----------- .../toxicity/hud/manager/TextManagerImpl.kt | 9 +++-- .../kr/toxicity/hud/popup/PopupLayout.kt | 20 +++-------- .../kr/toxicity/hud/renderer/TextRenderer.kt | 2 +- .../kr/toxicity/hud/text/ImageCharWidth.kt | 8 ++++- 7 files changed, 63 insertions(+), 60 deletions(-) diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt index 3834402..f172104 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt @@ -9,8 +9,6 @@ import kr.toxicity.hud.layout.BackgroundLayout import kr.toxicity.hud.layout.TextLayout import kr.toxicity.hud.location.GuiLocation import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.manager.ConfigManagerImpl -import kr.toxicity.hud.manager.MinecraftManager import kr.toxicity.hud.manager.TextManagerImpl import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.renderer.TextRenderer @@ -41,7 +39,7 @@ class HudTextElement( loc.opacity, text.property ) - val imageCodepointMap = text.text.imageCharWidth.map { + val imageCodepointMap = text.imageCharMap.map { it.value.name to it.key }.toMap() val index2 = ++parent.textIndex @@ -61,17 +59,16 @@ class HudTextElement( )) } } - var textIndex = TEXT_IMAGE_START_CODEPOINT + text.text.imageCharWidth.size + var textIndex = TEXT_IMAGE_START_CODEPOINT + text.imageCharMap.size val textEncoded = "hud_${parent.name}_text_${index2 + 1}_${lineIndex + 1}".encodeKey() - val imageMap = HashMap() val key = createAdventureKey(textEncoded) - text.text.imageCharWidth.forEach { + text.imageCharMap.forEach { val height = (it.value.height.toDouble() * text.scale * text.emojiScale).roundToInt() HudImpl.createBit(shader, loc.y + it.value.location.y + lineIndex * text.lineWidth) { y -> array.add( jsonObjectOf( "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:${"glyph_${it.value.name}".encodeKey()}.png", + "file" to it.value.fileName, "ascent" to y, "height" to height, "chars" to jsonArrayOf(it.key.parseChar()) @@ -79,15 +76,6 @@ class HudTextElement( ) } } - if (ConfigManagerImpl.loadMinecraftDefaultTextures) { - HudImpl.createBit(shader, loc.y + text.emojiLocation.y + lineIndex * text.lineWidth) { y -> - MinecraftManager.applyAll(array, y, text.emojiScale, key) { - ++textIndex - }.forEach { - imageMap[it.key] = text.emojiLocation.x.toSpaceComponent() + it.value - } - } - } PackGenerator.addTask(resource.font + "$textEncoded.json") { jsonObjectOf("providers" to array).toByteArray() } @@ -134,7 +122,7 @@ class HudTextElement( } TextRenderer( text.text.charWidth, - text.text.imageCharWidth, + text.imageCharMap, text.color, HudTextData( keys, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt index 4b94e31..723dd1c 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt @@ -7,8 +7,10 @@ import kr.toxicity.hud.layout.enums.LayoutAlign import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.manager.BackgroundManager import kr.toxicity.hud.manager.ConfigManagerImpl +import kr.toxicity.hud.manager.MinecraftManager import kr.toxicity.hud.manager.TextManagerImpl import kr.toxicity.hud.text.HudText +import kr.toxicity.hud.text.ImageCharWidth import kr.toxicity.hud.util.* import net.kyori.adventure.text.format.NamedTextColor import net.kyori.adventure.text.format.TextColor @@ -19,6 +21,18 @@ class TextLayout( yamlObject: YamlObject, loc: PixelLocation ) : HudLayout(loc, yamlObject) { + + companion object { + private fun interface EmojiProvider : (TextLayout, () -> Int) -> Map + private val emojiProviderMap: List = listOf( + EmojiProvider { layout, getter -> + if (ConfigManagerImpl.loadMinecraftDefaultTextures) { + MinecraftManager.applyAll(layout, getter) + } else emptyMap() + } + ) + } + val pattern: String = yamlObject.get("pattern")?.asString().ifNull("pattern value not set: $s") val text: HudText = yamlObject.get("name")?.asString().ifNull("name value not set: $s").let { n -> TextManagerImpl.getText(n).ifNull("this text doesn't exist: $n") @@ -65,4 +79,16 @@ class TextLayout( } ) ) + + val imageCharMap: Map = run { + val map = text.imageCharWidth.toMutableMap() + var baseValue = TEXT_IMAGE_START_CODEPOINT + map.size + val getter = { + ++baseValue + } + emojiProviderMap.forEach { + map += it(this@TextLayout, getter) + } + map + } } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/MinecraftManager.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/MinecraftManager.kt index 0b8acbb..ff35ad8 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/MinecraftManager.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/MinecraftManager.kt @@ -1,12 +1,10 @@ package kr.toxicity.hud.manager -import com.google.gson.JsonArray -import kr.toxicity.hud.api.component.WidthComponent +import kr.toxicity.hud.layout.TextLayout import kr.toxicity.hud.resource.GlobalResource +import kr.toxicity.hud.text.ImageCharWidth import kr.toxicity.hud.util.* import net.kyori.adventure.audience.Audience -import net.kyori.adventure.key.Key -import net.kyori.adventure.text.Component import java.io.File import java.io.InputStreamReader import java.net.URI @@ -15,7 +13,6 @@ import java.net.http.HttpRequest import java.net.http.HttpResponse import java.util.* import java.util.jar.JarFile -import kotlin.math.roundToInt object MinecraftManager : BetterHudManager { @@ -23,26 +20,23 @@ object MinecraftManager : BetterHudManager { private val assetsMap = Collections.synchronizedSet(HashSet()) - fun applyAll(json: JsonArray, ascent: Int, scale: Double, font: Key, intGetter: () -> Int): Map { - val map = HashMap() + fun applyAll(layout: TextLayout, intGetter: () -> Int): Map { + val map = HashMap() assetsMap.forEach { - map[it.namespace.replace('/', '_')] = it.toJson(json, intGetter().parseChar(), ascent, scale, font) + map[intGetter()] = it.toCharWidth(layout) } return map } private data class MinecraftAsset(val namespace: String, val width: Int, val height: Int) { - fun toJson(json: JsonArray, char: String, ascent: Int, scale: Double, font: Key): WidthComponent { - val newHeight = (height.toDouble() * scale).roundToInt() - val newWidth = (width.toDouble() / height.toDouble() * newHeight).roundToInt() - json.add(jsonObjectOf( - "type" to "bitmap", - "file" to "minecraft:$namespace.png", - "ascent" to ascent, - "height" to newHeight, - "chars" to jsonArrayOf(char) - )) - return WidthComponent(Component.text().content(char).font(font).append(NEGATIVE_ONE_SPACE_COMPONENT.component), newWidth) + fun toCharWidth(layout: TextLayout): ImageCharWidth { + return ImageCharWidth( + namespace.replace('/', '_'), + "minecraft:$namespace.png", + layout.emojiLocation, + width, + height + ) } } @@ -54,7 +48,7 @@ object MinecraftManager : BetterHudManager { override fun reload(sender: Audience, resource: GlobalResource) { if (ConfigManagerImpl.loadMinecraftDefaultTextures) { val current = if (ConfigManagerImpl.minecraftJarVersion == "bukkit") BOOTSTRAP.minecraftVersion() else ConfigManagerImpl.minecraftJarVersion - if (previous != current) { + if (assetsMap.isEmpty() || previous != current) { previous = current } else return assetsMap.clear() @@ -119,7 +113,7 @@ object MinecraftManager : BetterHudManager { } } - } + } else assetsMap.clear() } override fun end() { diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt index e9576af..02b979e 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt @@ -20,11 +20,11 @@ import java.io.File import java.io.InputStreamReader import java.util.* import java.util.concurrent.ConcurrentHashMap +import kotlin.collections.HashMap import kotlin.math.roundToInt object TextManagerImpl : BetterHudManager, TextManager { - private class TextCache( val name: String, val imagesName: Set @@ -575,13 +575,12 @@ object TextManagerImpl : BetterHudManager, TextManager { var imageStart = TEXT_IMAGE_START_CODEPOINT val imageCharWidthMap = HashMap() images.forEach { - val h = (it.value.image.image.height.toDouble() * it.value.scale).roundToInt() - val div = h / it.value.image.image.height.toDouble() imageCharWidthMap[++imageStart] = ImageCharWidth( it.key, + "$NAME_SPACE_ENCODED:${"glyph_${it.key}".encodeKey()}.png", it.value.location, - (it.value.image.image.width * div).roundToInt(), - h + it.value.image.image.height, + it.value.image.image.height ) PackGenerator.addTask(imageSaveFolder + "${"glyph_${it.key}".encodeKey()}.png") { it.value.image.image.toByteArray() diff --git a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt index 8a659b2..8fbc07d 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt @@ -191,7 +191,7 @@ class PopupLayout( pixel.opacity, textLayout.property ) - val imageCodepointMap = textLayout.text.imageCharWidth.map { + val imageCodepointMap = textLayout.imageCharMap.map { it.value.name to it.key }.toMap() val index = ++textIndex @@ -212,17 +212,16 @@ class PopupLayout( ) } } - val imageMap = HashMap() val textEncoded = "popup_${parent.name}_text_${index}_${lineIndex + 1}".encodeKey() val key = createAdventureKey(textEncoded) - var imageTextIndex = TEXT_IMAGE_START_CODEPOINT + textLayout.text.imageCharWidth.size - textLayout.text.imageCharWidth.forEach { + var imageTextIndex = TEXT_IMAGE_START_CODEPOINT + textLayout.imageCharMap.size + textLayout.imageCharMap.forEach { val height = (it.value.height.toDouble() * textLayout.scale).roundToInt() HudImpl.createBit(textShader, pixel.y + it.value.location.y + lineIndex * textLayout.lineWidth) { y -> array.add( jsonObjectOf( "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:${"glyph_${it.value.name}".encodeKey()}.png", + "file" to it.value.fileName, "ascent" to y, "height" to height, "chars" to jsonArrayOf(it.key.parseChar()) @@ -230,15 +229,6 @@ class PopupLayout( ) } } - if (ConfigManagerImpl.loadMinecraftDefaultTextures) { - HudImpl.createBit(textShader, pixel.y + textLayout.emojiLocation.y + lineIndex * textLayout.lineWidth) { y -> - MinecraftManager.applyAll(array, y, textLayout.emojiScale, key) { - ++imageTextIndex - }.forEach { - imageMap[it.key] = textLayout.emojiLocation.x.toSpaceComponent() + it.value - } - } - } PackGenerator.addTask(file + "$textEncoded.json") { jsonObjectOf("providers" to array).toByteArray() } @@ -285,7 +275,7 @@ class PopupLayout( } TextRenderer( textLayout.text.charWidth, - textLayout.text.imageCharWidth, + textLayout.imageCharMap, textLayout.color, HudTextData( keys, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt b/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt index 6a61055..3b3bc69 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt @@ -50,7 +50,7 @@ class TextRenderer( companion object { private val decimalPattern = Pattern.compile("([0-9]+((\\.([0-9]+))?))") private val allPattern = Pattern.compile(".+") - private val imagePattern = Pattern.compile("<(?(image|space)):(?(([a-zA-Z]|[0-9]|-)+))>") + private val imagePattern = Pattern.compile("<(?(image|space)):(?(([a-zA-Z]|[0-9]|_|-)+))>") } private val followHudPlayer = follow?.let { diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt index a9e4835..a2fed14 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt @@ -2,4 +2,10 @@ package kr.toxicity.hud.text import kr.toxicity.hud.location.PixelLocation -class ImageCharWidth(val name: String, val location: PixelLocation, width: Int, height: Int) : CharWidth(width, height) \ No newline at end of file +class ImageCharWidth( + val name: String, + val fileName: String, + val location: PixelLocation, + width: Int, + height: Int +) : CharWidth(width, height) \ No newline at end of file