mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-14 09:56:37 -04:00
don't load fonts twice, load multiple fonts
This commit is contained in:
parent
6dc3002528
commit
5855cbbe42
@ -18,6 +18,9 @@ import de.bixilon.minosoft.util.KUtil.toResourceLocation
|
||||
object DefaultFontIndices {
|
||||
val DEFAULT = "font/default.json".toResourceLocation()
|
||||
val ALT = "font/alt.json".toResourceLocation()
|
||||
val ILLAGERALT = "font/allageralt.json".toResourceLocation()
|
||||
val ILLAGERALT = "font/illageralt.json".toResourceLocation()
|
||||
val UNIFORM = "font/uniform.json".toResourceLocation()
|
||||
|
||||
|
||||
val ALL = arrayOf(ALT, ILLAGERALT, UNIFORM)
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import de.bixilon.kutil.latch.AbstractLatch
|
||||
import de.bixilon.minosoft.assets.util.InputStreamUtil.readJsonObject
|
||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||
import de.bixilon.minosoft.gui.rendering.RenderContext
|
||||
import de.bixilon.minosoft.gui.rendering.font.manager.FontManager
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.FontType
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.factory.FontTypes
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.font.Font
|
||||
@ -31,7 +32,7 @@ import de.bixilon.minosoft.util.nbt.tag.NBTUtil.listCast
|
||||
object FontLoader {
|
||||
|
||||
|
||||
fun load(context: RenderContext, fontIndex: ResourceLocation, latch: AbstractLatch?): Font? {
|
||||
fun load(context: RenderContext, manager: FontManager, fontIndex: ResourceLocation, latch: AbstractLatch?): Font? {
|
||||
val fontIndex = context.connection.assetsManager.getOrNull(fontIndex)?.readJsonObject() ?: return null
|
||||
|
||||
val providersRaw = fontIndex["providers"].listCast<Map<String, Any>>()!!
|
||||
@ -46,7 +47,7 @@ object FontLoader {
|
||||
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.WARN) { "Unknown font provider: $type" }
|
||||
return@add
|
||||
}
|
||||
providers[index] = factory.build(context, provider)
|
||||
providers[index] = factory.build(context, manager, provider)
|
||||
}
|
||||
}
|
||||
worker.work(latch)
|
||||
|
@ -13,7 +13,9 @@
|
||||
|
||||
package de.bixilon.minosoft.gui.rendering.font.manager
|
||||
|
||||
import de.bixilon.kutil.cast.CastUtil.unsafeNull
|
||||
import de.bixilon.kutil.latch.AbstractLatch
|
||||
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
|
||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||
import de.bixilon.minosoft.gui.rendering.RenderContext
|
||||
import de.bixilon.minosoft.gui.rendering.font.loader.DefaultFontIndices
|
||||
@ -25,31 +27,59 @@ import de.bixilon.minosoft.util.logging.Log
|
||||
import de.bixilon.minosoft.util.logging.LogLevels
|
||||
import de.bixilon.minosoft.util.logging.LogMessageType
|
||||
|
||||
class FontManager(
|
||||
val default: FontType,
|
||||
) {
|
||||
class FontManager(default: FontType? = null) {
|
||||
private val fonts: MutableMap<ResourceLocation, FontType> = mutableMapOf()
|
||||
var default = default ?: unsafeNull()
|
||||
|
||||
init {
|
||||
if (default != null) {
|
||||
fonts[DefaultFontIndices.DEFAULT.fontName()] = default
|
||||
}
|
||||
}
|
||||
|
||||
fun postInit(latch: AbstractLatch) {
|
||||
if (default is PostInitFontType) {
|
||||
default.postInit(latch)
|
||||
for (font in fonts.values) {
|
||||
if (font !is PostInitFontType) continue
|
||||
font.postInit(latch)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
operator fun get(font: ResourceLocation?): FontType? = null
|
||||
operator fun get(name: ResourceLocation?) = fonts[name?.fontName()]
|
||||
|
||||
fun load(index: ResourceLocation, context: RenderContext, latch: AbstractLatch?): FontType? {
|
||||
val name = index.fontName()
|
||||
fonts[name]?.let { return it }
|
||||
|
||||
|
||||
val font = FontLoader.load(context, this, index, latch)
|
||||
if (font == null) {
|
||||
Log.log(LogMessageType.ASSETS, LogLevels.WARN) { "Font $index seems to be empty!" }
|
||||
return null
|
||||
}
|
||||
val type = font.trim() ?: return null
|
||||
|
||||
this.fonts[name] = type
|
||||
return type
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun create(context: RenderContext, latch: AbstractLatch): FontManager {
|
||||
val font = FontLoader.load(context, DefaultFontIndices.DEFAULT, latch)
|
||||
val manager = FontManager()
|
||||
|
||||
// TODO: load multiple fonts
|
||||
val default = manager.load(DefaultFontIndices.DEFAULT, context, latch) ?: EmptyFont
|
||||
manager::default.forceSet(default)
|
||||
|
||||
if (font == null) {
|
||||
Log.log(LogMessageType.ASSETS, LogLevels.WARN) { "Font ${DefaultFontIndices.DEFAULT} seems to be empty!" }
|
||||
for (index in DefaultFontIndices.ALL) {
|
||||
manager.load(index, context, latch)
|
||||
}
|
||||
|
||||
return FontManager(font?.trim() ?: EmptyFont)
|
||||
return manager
|
||||
}
|
||||
|
||||
private fun ResourceLocation.fontName(): ResourceLocation {
|
||||
return ResourceLocation(namespace, path.removePrefix("font/").removeSuffix(".json"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,15 +16,15 @@ package de.bixilon.minosoft.gui.rendering.font.types
|
||||
import de.bixilon.kutil.json.JsonObject
|
||||
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
|
||||
import de.bixilon.minosoft.gui.rendering.RenderContext
|
||||
import de.bixilon.minosoft.gui.rendering.font.loader.FontLoader
|
||||
import de.bixilon.minosoft.gui.rendering.font.manager.FontManager
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.factory.FontTypeFactory
|
||||
import de.bixilon.minosoft.util.KUtil.toResourceLocation
|
||||
|
||||
object ReferenceFontType : FontTypeFactory<FontType> {
|
||||
override val identifier = minecraft("reference")
|
||||
|
||||
override fun build(context: RenderContext, data: JsonObject): FontType? {
|
||||
override fun build(context: RenderContext, manager: FontManager, data: JsonObject): FontType? {
|
||||
val index = data["id"]?.toResourceLocation()?.prefix("font/")?.suffix(".json") ?: throw IllegalArgumentException("id missing!")
|
||||
return FontLoader.load(context, index, null)?.trim()
|
||||
return manager.load(index, context, null)
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
|
||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||
import de.bixilon.minosoft.gui.rendering.RenderConstants
|
||||
import de.bixilon.minosoft.gui.rendering.RenderContext
|
||||
import de.bixilon.minosoft.gui.rendering.font.manager.FontManager
|
||||
import de.bixilon.minosoft.gui.rendering.font.renderer.code.CodePointRenderer
|
||||
import de.bixilon.minosoft.gui.rendering.font.renderer.properties.FontProperties
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.PostInitFontType
|
||||
@ -58,7 +59,7 @@ class BitmapFontType(
|
||||
private const val ROW = 16
|
||||
override val identifier = minecraft("bitmap")
|
||||
|
||||
override fun build(context: RenderContext, data: JsonObject): BitmapFontType? {
|
||||
override fun build(context: RenderContext, manager: FontManager, data: JsonObject): BitmapFontType? {
|
||||
val file = data["file"]?.toString()?.let { it.toResourceLocation().texture() } ?: throw IllegalArgumentException("Missing file!")
|
||||
val height = data["height"]?.toInt() ?: 8
|
||||
val ascent = data["ascent"]?.toInt() ?: 8
|
||||
|
@ -20,6 +20,7 @@ import de.bixilon.minosoft.data.registries.identified.AliasedIdentified
|
||||
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
|
||||
import de.bixilon.minosoft.data.registries.identified.Namespaces.minosoft
|
||||
import de.bixilon.minosoft.gui.rendering.RenderContext
|
||||
import de.bixilon.minosoft.gui.rendering.font.manager.FontManager
|
||||
import de.bixilon.minosoft.gui.rendering.font.renderer.code.CodePointRenderer
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.FontType
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.factory.FontTypeFactory
|
||||
@ -44,7 +45,7 @@ class EmptyFontType(
|
||||
override val identifier = minosoft("empty")
|
||||
override val identifiers = setOf(minecraft("space"))
|
||||
|
||||
override fun build(context: RenderContext, data: JsonObject): EmptyFontType? {
|
||||
override fun build(context: RenderContext, manager: FontManager, data: JsonObject): EmptyFontType? {
|
||||
val chars = load(data)
|
||||
if (chars == null) {
|
||||
Log.log(LogMessageType.ASSETS, LogLevels.WARN) { "Empty font provider: $data!" }
|
||||
|
@ -16,10 +16,11 @@ package de.bixilon.minosoft.gui.rendering.font.types.factory
|
||||
import de.bixilon.kutil.json.JsonObject
|
||||
import de.bixilon.minosoft.data.registries.identified.Identified
|
||||
import de.bixilon.minosoft.gui.rendering.RenderContext
|
||||
import de.bixilon.minosoft.gui.rendering.font.manager.FontManager
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.FontType
|
||||
|
||||
|
||||
interface FontTypeFactory<T : FontType> : Identified {
|
||||
|
||||
fun build(context: RenderContext, data: JsonObject): T?
|
||||
fun build(context: RenderContext, manager: FontManager, data: JsonObject): T?
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import de.bixilon.minosoft.assets.AssetsManager
|
||||
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
|
||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||
import de.bixilon.minosoft.gui.rendering.RenderContext
|
||||
import de.bixilon.minosoft.gui.rendering.font.manager.FontManager
|
||||
import de.bixilon.minosoft.gui.rendering.font.renderer.properties.FontProperties
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.PostInitFontType
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.factory.FontTypeFactory
|
||||
@ -55,7 +56,7 @@ class LegacyUnicodeFontType(
|
||||
private const val PIXEL = 1.0f / (CHAR_SIZE * CHAR_ROW)
|
||||
private const val WIDTH_SCALE = FontProperties.CHAR_BASE_HEIGHT / CHAR_SIZE.toFloat()
|
||||
|
||||
override fun build(context: RenderContext, data: Map<String, Any>): LegacyUnicodeFontType? {
|
||||
override fun build(context: RenderContext, manager: FontManager, data: Map<String, Any>): LegacyUnicodeFontType? {
|
||||
val assets = context.connection.assetsManager
|
||||
val template = data["template"]?.toString() ?: throw IllegalArgumentException("template missing!")
|
||||
val sizes = data.loadSizes(assets) ?: return null
|
||||
|
@ -17,6 +17,7 @@ import de.bixilon.kutil.json.JsonObject
|
||||
import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft
|
||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||
import de.bixilon.minosoft.gui.rendering.RenderContext
|
||||
import de.bixilon.minosoft.gui.rendering.font.manager.FontManager
|
||||
import de.bixilon.minosoft.gui.rendering.font.renderer.code.CodePointRenderer
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.FontType
|
||||
import de.bixilon.minosoft.gui.rendering.font.types.factory.FontTypeFactory
|
||||
@ -40,7 +41,7 @@ class UnihexFontType(
|
||||
companion object : FontTypeFactory<UnihexFontType> {
|
||||
override val identifier = minecraft("unihex")
|
||||
|
||||
override fun build(context: RenderContext, data: JsonObject): UnihexFontType? {
|
||||
override fun build(context: RenderContext, manager: FontManager, data: JsonObject): UnihexFontType? {
|
||||
val hexFile = data["hex_file"]?.toResourceLocation() ?: throw IllegalArgumentException("hex_file missing!")
|
||||
val sizes = data["size_override"]?.listCast<JsonObject>()?.let { SizeOverride.deserialize(it) } ?: emptyList()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user