Skip to content

Commit

Permalink
Adds text background, text emoji.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Apr 10, 2024
1 parent d3c7349 commit 4d62199
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ allprojects {
apply(plugin = "kotlin")

group = "kr.toxicity.hud"
version = "beta-11"
version = "beta-12"

repositories {
mavenCentral()
Expand Down
2 changes: 2 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class BetterHudImpl: BetterHud() {
PlaceholderManagerImpl,
TriggerManagerImpl,

BackgroundManager,
ImageManager,
TextManager,
PlayerHeadManager,
Expand Down Expand Up @@ -238,6 +239,7 @@ class BetterHudImpl: BetterHud() {
managers.forEach {
it.end()
}
DatabaseManagerImpl.currentDatabase.close()
info("Plugin disabled.")
}

Expand Down
14 changes: 14 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.toxicity.hud.background

import kr.toxicity.hud.image.ImageLocation
import kr.toxicity.hud.image.LoadedImage

class HudBackground(
val name: String,

val left: LoadedImage,
val right: LoadedImage,
val body: LoadedImage,

val location: ImageLocation,
)
64 changes: 61 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ package kr.toxicity.hud.hud
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import kr.toxicity.hud.api.component.PixelComponent
import kr.toxicity.hud.api.component.WidthComponent
import kr.toxicity.hud.api.player.HudPlayer
import kr.toxicity.hud.api.update.UpdateEvent
import kr.toxicity.hud.image.ImageLocation
import kr.toxicity.hud.image.LoadedImage
import kr.toxicity.hud.layout.BackgroundLayout
import kr.toxicity.hud.layout.TextLayout
import kr.toxicity.hud.manager.TextManager
import kr.toxicity.hud.renderer.TextRenderer
import kr.toxicity.hud.shader.GuiLocation
import kr.toxicity.hud.shader.HudShader
import kr.toxicity.hud.shader.ShaderGroup
import kr.toxicity.hud.text.HudTextData
import kr.toxicity.hud.util.*
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import java.io.File
import kotlin.math.roundToInt

class HudTextElement(parent: HudImpl, name: String, file: File, private val text: TextLayout, index: Int, gui: GuiLocation, pixel: ImageLocation) {

Expand All @@ -30,7 +36,6 @@ class HudTextElement(parent: HudImpl, name: String, file: File, private val text
val group = ShaderGroup(shader, text.text.name, scale, yAxis)
val key = TextManager.getKey(group) ?: run {
val index2 = (++parent.textIndex)
val key = Key.key("$NAME_SPACE:hud/$name/text/text_${index + 1}_${index2 + 1}")
val array = JsonArray().apply {
add(JsonObject().apply {
addProperty("type", "space")
Expand All @@ -48,11 +53,64 @@ class HudTextElement(parent: HudImpl, name: String, file: File, private val text
add("chars", it.chars)
})
}
var textIndex = 0xC0000
val key = Key.key("$NAME_SPACE:hud/$name/text/text_${index + 1}_${index2 + 1}")
val imageMap = HashMap<String, WidthComponent>()
text.text.images.forEach {
val result = (textIndex++).parseChar()
val imageScale = it.value.scale * text.scale
val height = (it.value.image.image.height.toDouble() * imageScale).roundToInt()
val div = height.toDouble() / it.value.image.image.height
array.add(JsonObject().apply {
addProperty("type", "bitmap")
addProperty("file", "$NAME_SPACE:text/${text.text.fontName}/image_${it.key}.png")
addProperty("ascent", HudImpl.createBit(pixel.y + it.value.location.y, shader))
addProperty("height", height)
add("chars", JsonArray().apply {
add(result)
})
})
imageMap[it.key] = it.value.location.x.toSpaceComponent() + WidthComponent(Component.text()
.font(key)
.content(result)
.append(NEGATIVE_ONE_SPACE_COMPONENT.component), (it.value.image.image.width.toDouble() * div).roundToInt())
}
val result = HudTextData(
key,
imageMap,
text.background?.let {
val y = HudImpl.createBit(pixel.y + it.location.y, HudShader(
gui,
text.layer - 1,
false
))
fun getString(image: LoadedImage, file: String): WidthComponent {
val result = (textIndex++).parseChar()
val height = (image.image.height.toDouble() * text.backgroundScale).roundToInt()
val div = height.toDouble() / image.image.height
array.add(JsonObject().apply {
addProperty("type", "bitmap")
addProperty("file", "$NAME_SPACE:background/${it.name}/$file.png")
addProperty("ascent", y)
addProperty("height", height)
add("chars", JsonArray().apply {
add(result)
})
})
return it.location.x.toSpaceComponent() + WidthComponent(Component.text().font(key).content(result).append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt())
}
BackgroundLayout(
getString(it.left, "left"),
getString(it.right, "right"),
getString(it.body, "body")
)
}
)
JsonObject().apply {
add("providers", array)
}.save(File(file, "text_${index + 1}_${index2 + 1}.json"))
TextManager.setKey(group, key)
key
TextManager.setKey(group, result)
result
}
TextRenderer(
text.text.charWidth,
Expand Down
7 changes: 7 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/image/LocatedImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.toxicity.hud.image

class LocatedImage(
val image: LoadedImage,
val location: ImageLocation,
val scale: Double
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.toxicity.hud.layout

import kr.toxicity.hud.api.component.WidthComponent

class BackgroundLayout(
val left: WidthComponent,
val right: WidthComponent,
val body: WidthComponent
)
12 changes: 7 additions & 5 deletions dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package kr.toxicity.hud.layout
import kr.toxicity.hud.equation.AnimationLocation
import kr.toxicity.hud.equation.TEquation
import kr.toxicity.hud.image.ImageLocation
import kr.toxicity.hud.manager.ConfigManager
import kr.toxicity.hud.manager.ImageManager
import kr.toxicity.hud.manager.PlayerHeadManager
import kr.toxicity.hud.manager.TextManager
import kr.toxicity.hud.manager.*
import kr.toxicity.hud.util.*
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.configuration.ConfigurationSection
Expand Down Expand Up @@ -46,14 +43,15 @@ class LayoutGroup(section: ConfigurationSection) {
}
val text: List<TextLayout> = ArrayList<TextLayout>().apply {
section.getConfigurationSection("texts")?.forEachSubConfiguration { s, configurationSection ->
val scale = configurationSection.getDouble("scale", 1.0)
add(
TextLayout(
configurationSection.getString("pattern").ifNull("pattern value not set: $s"),
configurationSection.getString("name").ifNull("name value not set: $s").let { n ->
TextManager.getText(n).ifNull("this text doesn't exist: $n")
},
ImageLocation(configurationSection) + loc,
configurationSection.getDouble("scale", 1.0),
scale,
configurationSection.getInt("space", 2).coerceAtLeast(0),
configurationSection.getString("align").toLayoutAlign(),
configurationSection.getString("color")?.toTextColor() ?: NamedTextColor.WHITE,
Expand All @@ -66,6 +64,10 @@ class LayoutGroup(section: ConfigurationSection) {
configurationSection.getString("number-format")?.let {
DecimalFormat(it)
} ?: ConfigManager.numberFormat,
configurationSection.getString("background")?.let {
BackgroundManager.getBackground(it)
},
configurationSection.getDouble("background-scale", scale),
configurationSection.toConditions()
)
)
Expand Down
3 changes: 3 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.toxicity.hud.layout

import kr.toxicity.hud.background.HudBackground
import kr.toxicity.hud.equation.TEquation
import kr.toxicity.hud.image.ImageLocation
import kr.toxicity.hud.placeholder.ConditionBuilder
Expand All @@ -20,5 +21,7 @@ class TextLayout(
val deserializeText: Boolean,
val numberEquation: TEquation,
val numberFormat: DecimalFormat,
val background: HudBackground?,
val backgroundScale: Double,
val conditions: ConditionBuilder
)
54 changes: 54 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package kr.toxicity.hud.manager

import kr.toxicity.hud.background.HudBackground
import kr.toxicity.hud.image.ImageLocation
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.util.*
import java.io.File

object BackgroundManager: BetterHudManager {

private val backgroundMap = HashMap<String, HudBackground>()

override fun start() {

}

fun getBackground(name: String) = backgroundMap[name]

override fun reload(resource: GlobalResource) {
val folder = DATA_FOLDER.subFolder("backgrounds")
val outputParent = resource.textures.subFolder("background")
backgroundMap.clear()
folder.forEach {
if (it.extension == "yml") {
runCatching {
val yaml = it.toYaml()
val name = it.nameWithoutExtension
val backgroundFolder = folder.subFolder(name)
val output = outputParent.subFolder(name)
fun getImage(imageName: String) = File(backgroundFolder, "$imageName.png")
.ifNotExist("this image doesn't exist: $imageName.png in $name")
.toImage()
.removeEmptyWidth()
.ifNull("this image is empty: $imageName.png in $name").apply {
image.save(output.subFile("$imageName.png"))
}
backgroundMap[name] = HudBackground(
name,
getImage("left"),
getImage("right"),
getImage("body"),
ImageLocation(yaml)
)
}.onFailure { e ->
warn("Unable to load this yml: ${it.name}")
warn("Reason: ${e.message}")
}
}
}
}

override fun end() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,5 @@ object DatabaseManagerImpl: BetterHudManager, DatabaseManager {
}

override fun end() {
current.close()
}
}
43 changes: 36 additions & 7 deletions dist/src/main/kotlin/kr/toxicity/hud/manager/TextManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package kr.toxicity.hud.manager
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import kr.toxicity.hud.image.ImageLocation
import kr.toxicity.hud.image.LocatedImage
import kr.toxicity.hud.placeholder.ConditionBuilder
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.shader.ShaderGroup
import kr.toxicity.hud.text.HudText
import kr.toxicity.hud.text.HudTextArray
import kr.toxicity.hud.text.HudTextData
import kr.toxicity.hud.util.*
import net.kyori.adventure.key.Key
import java.awt.AlphaComposite
import java.awt.Font
import java.awt.Image
Expand All @@ -29,12 +31,12 @@ object TextManager: BetterHudManager {
private val textMap = HashMap<String, HudText>()

private val textWidthMap = HashMap<Char, Int>()
private val textKeyMap = mutableMapOf<ShaderGroup, Key>()
private val textKeyMap = mutableMapOf<ShaderGroup, HudTextData>()

private val defaultBitmapImageMap = HashMap<Char, BufferedImage>()

fun getKey(shaderGroup: ShaderGroup) = textKeyMap[shaderGroup]
fun setKey(shaderGroup: ShaderGroup, key: Key) {
fun setKey(shaderGroup: ShaderGroup, key: HudTextData) {
textKeyMap[shaderGroup] = key
}

Expand All @@ -49,6 +51,7 @@ object TextManager: BetterHudManager {
textMap.clear()
textWidthMap.clear()
textKeyMap.clear()
val assetsFolder = DATA_FOLDER.subFolder("assets")
val fontFolder = DATA_FOLDER.subFolder("fonts")
val globalSaveFolder = resource.textures.subFolder("text")
DATA_FOLDER.subFolder("texts").forEachAllYaml { file, s, section ->
Expand All @@ -62,7 +65,21 @@ object TextManager: BetterHudManager {
Font.createFont(Font.TRUETYPE_FONT, it)
} ?: BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics().font).deriveFont(scale.toFloat())
val saveName = "${fontTarget?.nameWithoutExtension ?: s}_$scale"
textMap[s] = parseFont(s, saveName, fontFile, scale, globalSaveFolder, section.toConditions(), section.getBoolean("merge-default-bitmap"))
textMap[s] = parseFont(s, saveName, fontFile, scale, globalSaveFolder, HashMap<String, LocatedImage>().apply {
section.getConfigurationSection("images")?.forEachSubConfiguration { key, configurationSection ->
put(key, LocatedImage(
File(assetsFolder, configurationSection.getString("name").ifNull("image does not set: $key"))
.ifNotExist("this image doesn't exist: $key")
.toImage()
.removeEmptyWidth()
.ifNull("invalid image: $key"),
ImageLocation(configurationSection),
configurationSection.getDouble("scale", 1.0).apply {
if (this <= 0.0) throw RuntimeException("scale cannot be <= 0: $key")
}
))
}
}, section.toConditions(), section.getBoolean("merge-default-bitmap"))
}.onFailure { e ->
warn("Unable to load this text: $s in ${file.name}")
warn("Reason: ${e.message}")
Expand All @@ -89,7 +106,7 @@ object TextManager: BetterHudManager {
}
}.getOrNull() else null) ?: BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics().font
}.deriveFont(configScale.toFloat())
val parseDefault = parseFont("default", "default", defaultFont, configScale, resource.textures.subFolder("font"), ConditionBuilder.alwaysTrue, fontConfig.getBoolean("merge-default-bitmap", true))
val parseDefault = parseFont("default", "default", defaultFont, configScale, resource.textures.subFolder("font"), emptyMap(), ConditionBuilder.alwaysTrue, fontConfig.getBoolean("merge-default-bitmap", true))
val heightMultiply = configHeight.toDouble() / parseDefault.height.toDouble()
parseDefault.charWidth.forEach {
textWidthMap[it.key] = Math.round(it.value.toDouble() * heightMultiply).toInt()
Expand Down Expand Up @@ -149,7 +166,16 @@ object TextManager: BetterHudManager {
}
}

private fun parseFont(s: String, saveName: String, fontFile: Font, scale: Int, imageSaveFolder: File, condition: ConditionBuilder, mergeDefaultBitmap: Boolean): HudText {
private fun parseFont(
s: String,
saveName: String,
fontFile: Font,
scale: Int,
imageSaveFolder: File,
images: Map<String, LocatedImage>,
condition: ConditionBuilder,
mergeDefaultBitmap: Boolean
): HudText {
val height = (scale.toDouble() * 1.4).toInt()
val pairMap = HashMap<Int, MutableList<Pair<Char, Image>>>()
val charWidthMap = HashMap<Char, Int>()
Expand Down Expand Up @@ -189,6 +215,9 @@ object TextManager: BetterHudManager {
val textList = ArrayList<HudTextArray>()
val saveFolder = imageSaveFolder.subFolder(saveName)
var i = 0
images.forEach {
it.value.image.image.save(File(saveFolder, "image_${it.key}.png"))
}
pairMap.forEach {
val width = it.key
fun save(list: List<Pair<Char, Image>>) {
Expand Down Expand Up @@ -222,7 +251,7 @@ object TextManager: BetterHudManager {
}
}
}
return HudText(s, saveName, height, textList, charWidthMap, condition)
return HudText(s, saveName, height, textList, images, charWidthMap, condition)
}

override fun end() {
Expand Down
Loading

0 comments on commit 4d62199

Please sign in to comment.