Skip to content

Commit

Permalink
Optimizes head renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Oct 3, 2024
1 parent 505e348 commit 13dfa4b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 41 deletions.
10 changes: 6 additions & 4 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudHeadElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class HudHeadElement(parent: HudImpl, private val head: HeadLayout, gui: GuiLoca
head.outline
)
HeadRenderer(
parent.getOrCreateSpace(-1),
parent.getOrCreateSpace(-(head.head.pixel * 8 + 1)),
(0..7).map { i ->
val char = (++parent.imageChar).parseChar()
val encode = "pixel_${head.head.pixel}".encodeKey()
Expand All @@ -47,12 +49,12 @@ class HudHeadElement(parent: HudImpl, private val head: HeadLayout, gui: GuiLoca
})
}
}
val comp = Component.text(char).font(parent.imageKey)
PlayerHeadManager.setHead(shaderGroup, comp)
comp
PlayerHeadManager.setHead(shaderGroup, char)
char
}
},
head.head.pixel,
parent.imageKey,
head.head.pixel * 8,
final.x,
head.align,
head.follow,
Expand Down
13 changes: 13 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ class HudImpl(
private val imageEncoded = "hud_${internalName}_image".encodeKey()
val imageKey = createAdventureKey(imageEncoded)
var jsonArray: JsonArray? = JsonArray()
private val spaces = HashMap<Int, String>()
private val default = ConfigManagerImpl.defaultHud.contains(internalName) || section.getAsBoolean("default", false)
var textIndex = 0

fun getOrCreateSpace(int: Int) = spaces.computeIfAbsent(int) {
(++imageChar).parseChar()
}

private val elements = ArrayList<HudAnimation>().apply {
section.get("layouts")?.asObject().ifNull("layout configuration not set.").forEachSubConfiguration { s, yamlObject ->
val layout = yamlObject.get("name")?.asString().ifNull("name value not set: $s").let {
Expand Down Expand Up @@ -75,6 +80,14 @@ class HudImpl(
}
init {
jsonArray?.let { array ->
if (spaces.isNotEmpty()) array.add(JsonObject().apply {
addProperty("type", "space")
add("advances", JsonObject().apply {
spaces.forEach {
addProperty(it.value, it.key)
}
})
})
PackGenerator.addTask(
ArrayList(file).apply {
add("$imageEncoded.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ object PlayerHeadManager : BetterHudManager {
private val headCache = ConcurrentHashMap<String, CachedHead>()
private val headMap = HashMap<String, HudHead>()

private val headNameComponent = ConcurrentHashMap<ShaderGroup, Component>()
private val headNameComponent = ConcurrentHashMap<ShaderGroup, String>()


@Synchronized
fun getHead(group: ShaderGroup) = headNameComponent[group]
@Synchronized
fun setHead(group: ShaderGroup, component: Component) {
headNameComponent[group] = component
fun setHead(group: ShaderGroup, string: String) {
headNameComponent[group] = string
}

private class CachedHead(
Expand Down
49 changes: 32 additions & 17 deletions dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import kr.toxicity.hud.shader.GuiLocation
import kr.toxicity.hud.util.*
import kr.toxicity.hud.api.yaml.YamlObject
import java.util.*
import kotlin.collections.HashMap

class PopupImpl(
override val path: String,
Expand Down Expand Up @@ -55,30 +56,36 @@ class PopupImpl(
var array: JsonArray? = JsonArray()
val imageKey = createAdventureKey(imageEncoded)

private val spaces = HashMap<Int, String>()
private var imageChar = 0xCE000

fun getOrCreateSpace(int: Int) = spaces.computeIfAbsent(int) {
newChar()
}
fun newChar(): String = (++imageChar).parseChar()

private val sortType = section.get("sort")?.asString()?.let {
PopupSortType.valueOf(it.uppercase())
} ?: PopupSortType.LAST

private val layouts = section.get("layouts")?.asObject()?.let {
val json = array.ifNull("error is occurred.")
ArrayList<PopupLayout>().apply {
it.forEachSubConfiguration { _, yamlObject ->
val layout = yamlObject.get("name")?.asString().ifNull("name value not set.")
var loc = GuiLocation(yamlObject)
yamlObject.get("gui")?.asObject()?.let {
loc += GuiLocation(it)
}
add(PopupLayout(
json,
LayoutManager.getLayout(layout).ifNull("this layout doesn't exist: $layout"),
this@PopupImpl,
loc,
yamlObject.get("pixel")?.asObject()?.let {
ImageLocation(it)
} ?: ImageLocation.zero,
file,
))
it.mapSubConfiguration { _, yamlObject ->
val layout = yamlObject.get("name")?.asString().ifNull("name value not set.")
var loc = GuiLocation(yamlObject)
yamlObject.get("gui")?.asObject()?.let { gui ->
loc += GuiLocation(gui)
}
PopupLayout(
json,
LayoutManager.getLayout(layout).ifNull("this layout doesn't exist: $layout"),
this@PopupImpl,
loc,
yamlObject.get("pixel")?.asObject()?.let { pixel ->
ImageLocation(pixel)
} ?: ImageLocation.zero,
file,
)
}
}.ifNull("layouts configuration not set.").ifEmpty {
throw RuntimeException("layouts is empty.")
Expand Down Expand Up @@ -117,6 +124,14 @@ class PopupImpl(
}
}
array?.let { arr ->
if (spaces.isNotEmpty()) arr.add(JsonObject().apply {
addProperty("type", "space")
add("advances", JsonObject().apply {
spaces.forEach {
addProperty(it.value, it.key)
}
})
})
PackGenerator.addTask(ArrayList(file).apply {
add("$imageEncoded.json")
}) {
Expand Down
17 changes: 9 additions & 8 deletions dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class PopupLayout(
private val globalPixel: ImageLocation,
private val file: List<String>
) {
private var imageChar = 0xCE000
private var textIndex = 0

private val groups = parent.move.locations.map { location ->
Expand Down Expand Up @@ -127,7 +126,7 @@ class PopupLayout(
val shaderGroup = ShaderGroup(imageShader, fileName, target.scale, ascent)

val component = ImageManager.getImage(shaderGroup) ?: run {
val char = (++imageChar).parseChar()
val char = parent.newChar()
HudImpl.createBit(imageShader, ascent) { y ->
array.add(JsonObject().apply {
addProperty("type", "bitmap")
Expand All @@ -147,7 +146,7 @@ class PopupLayout(

list.add(component.toPixelComponent(pixel.x + xOffset))
} else hudImage.image[0].let {
val char = (++imageChar).parseChar()
val char = parent.newChar()
array.add(JsonObject().apply {
HudImpl.createBit(imageShader, pixel.y) { y ->
addProperty("type", "bitmap")
Expand Down Expand Up @@ -308,10 +307,12 @@ class PopupLayout(
headLayout.outline
)
HeadRenderer(
parent.getOrCreateSpace(-1),
parent.getOrCreateSpace(-(headLayout.head.pixel * 8 + 1)),
(0..7).map { i ->
val encode = "pixel_${headLayout.head.pixel}".encodeKey()
val fileName = "$NAME_SPACE_ENCODED:$encode.png"
val char = (++imageChar).parseChar()
val char = parent.newChar()
val ascent = pixel.y + i * headLayout.head.pixel
val height = headLayout.head.pixel
val shaderGroup = ShaderGroup(shader, fileName, 1.0, ascent)
Expand All @@ -328,12 +329,12 @@ class PopupLayout(
})
})
}
val comp = Component.text(char).font(parent.imageKey)
PlayerHeadManager.setHead(shaderGroup, comp)
comp
PlayerHeadManager.setHead(shaderGroup, char)
char
}
},
headLayout.head.pixel,
parent.imageKey,
headLayout.head.pixel * 8,
pixel.x,
headLayout.align,
headLayout.follow,
Expand Down
24 changes: 15 additions & 9 deletions dist/src/main/kotlin/kr/toxicity/hud/renderer/HeadRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import kr.toxicity.hud.manager.PlayerHeadManager
import kr.toxicity.hud.manager.PlayerManagerImpl
import kr.toxicity.hud.placeholder.ConditionBuilder
import kr.toxicity.hud.util.*
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component

class HeadRenderer(
private val components: List<Component>,
private val space: String,
private val nextPage: String,
private val components: List<String>,
private val font: Key,
private val pixel: Int,
private val x: Int,
private val align: LayoutAlign,
Expand All @@ -27,8 +31,6 @@ class HeadRenderer(
if (!java.lang.String::class.java.isAssignableFrom(clazz)) throw RuntimeException("This placeholder is not a string: $it")
}
}
private val nextPixel = (-pixel * 8).toSpaceComponent() + NEGATIVE_ONE_SPACE_COMPONENT

fun getHead(event: UpdateEvent): (HudPlayer) -> PixelComponent {
val cond = conditions.build(event)
val playerPlaceholder = followPlayer?.build(event)
Expand All @@ -46,18 +48,22 @@ class HeadRenderer(
targetPlayerHead = pair.second
}
if (cond(targetPlayer)) {
var comp = EMPTY_WIDTH_COMPONENT
val comp = Component.text().font(font)
var i = 0
targetPlayerHead.colors.forEachSync { next ->
val index = i++
comp += WidthComponent(Component.text().append(components[index / 8]).color(next), pixel)
comp += if (index < 63 && index % 8 == 7) nextPixel else NEGATIVE_ONE_SPACE_COMPONENT
comp.append(Component.text()
.content(buildString {
append(components[index / 8])
if (index < 63) append(if (index % 8 == 7) nextPage else space)
})
.color(next))
}
comp.toPixelComponent(
WidthComponent(comp, pixel).toPixelComponent(
when (align) {
LayoutAlign.LEFT -> x
LayoutAlign.CENTER -> x - comp.width / 2
LayoutAlign.RIGHT -> x - comp.width
LayoutAlign.CENTER -> x - pixel / 2
LayoutAlign.RIGHT -> x - pixel
}
)
} else EMPTY_PIXEL_COMPONENT
Expand Down
4 changes: 4 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/util/Files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ fun YamlObject.forEachSubConfiguration(block: (String, YamlObject) -> Unit) {
val v = it.value
if (v is YamlObject) block(it.key, v)
}
}
fun <T> YamlObject.mapSubConfiguration(block: (String, YamlObject) -> T) = mapNotNull {
val v = it.value
if (v is YamlObject) block(it.key, v) else null
}

0 comments on commit 13dfa4b

Please sign in to comment.