Skip to content

Commit

Permalink
Restore minecraft image glyph.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Nov 12, 2024
1 parent fcf1e6f commit ecc7499
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 60 deletions.
22 changes: 5 additions & 17 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -61,33 +59,23 @@ 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<String, WidthComponent>()
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())
)
)
}
}
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()
}
Expand Down Expand Up @@ -134,7 +122,7 @@ class HudTextElement(
}
TextRenderer(
text.text.charWidth,
text.text.imageCharWidth,
text.imageCharMap,
text.color,
HudTextData(
keys,
Expand Down
26 changes: 26 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +21,18 @@ class TextLayout(
yamlObject: YamlObject,
loc: PixelLocation
) : HudLayout(loc, yamlObject) {

companion object {
private fun interface EmojiProvider : (TextLayout, () -> Int) -> Map<Int, ImageCharWidth>
private val emojiProviderMap: List<EmojiProvider> = 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")
Expand Down Expand Up @@ -65,4 +79,16 @@ class TextLayout(
}
)
)

val imageCharMap: Map<Int, ImageCharWidth> = 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
}
}
36 changes: 15 additions & 21 deletions dist/src/main/kotlin/kr/toxicity/hud/manager/MinecraftManager.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,34 +13,30 @@ 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 {

private const val ASSETS_LOCATION = "assets/minecraft/textures/"

private val assetsMap = Collections.synchronizedSet(HashSet<MinecraftAsset>())

fun applyAll(json: JsonArray, ascent: Int, scale: Double, font: Key, intGetter: () -> Int): Map<String, WidthComponent> {
val map = HashMap<String, WidthComponent>()
fun applyAll(layout: TextLayout, intGetter: () -> Int): Map<Int, ImageCharWidth> {
val map = HashMap<Int, ImageCharWidth>()
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
)
}
}

Expand All @@ -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()
Expand Down Expand Up @@ -119,7 +113,7 @@ object MinecraftManager : BetterHudManager {
}

}
}
} else assetsMap.clear()
}

override fun end() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
Expand Down Expand Up @@ -575,13 +575,12 @@ object TextManagerImpl : BetterHudManager, TextManager {
var imageStart = TEXT_IMAGE_START_CODEPOINT
val imageCharWidthMap = HashMap<Int, ImageCharWidth>()
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()
Expand Down
20 changes: 5 additions & 15 deletions dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -212,33 +212,23 @@ class PopupLayout(
)
}
}
val imageMap = HashMap<String, WidthComponent>()
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())
)
)
}
}
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()
}
Expand Down Expand Up @@ -285,7 +275,7 @@ class PopupLayout(
}
TextRenderer(
textLayout.text.charWidth,
textLayout.text.imageCharWidth,
textLayout.imageCharMap,
textLayout.color,
HudTextData(
keys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<(?<type>(image|space)):(?<name>(([a-zA-Z]|[0-9]|-)+))>")
private val imagePattern = Pattern.compile("<(?<type>(image|space)):(?<name>(([a-zA-Z]|[0-9]|_|-)+))>")
}

private val followHudPlayer = follow?.let {
Expand Down
8 changes: 7 additions & 1 deletion dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
class ImageCharWidth(
val name: String,
val fileName: String,
val location: PixelLocation,
width: Int,
height: Int
) : CharWidth(width, height)

0 comments on commit ecc7499

Please sign in to comment.