Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/fonts/nunito-medium.ttf
Binary file not shown.
4 changes: 4 additions & 0 deletions src/com/reco1l/andengine/UIEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class UIEngine(val context: Activity, options: EngineOptions) : Engine(options)
*/
val overlay = HUD()

/**
* The resource manager for loading and accessing UI resources (fonts, textures, etc).
*/
val resources = UIResourceManager(context)

/**
* The current focused entity.
Expand Down
77 changes: 77 additions & 0 deletions src/com/reco1l/andengine/UIResourceManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
@file:Suppress("ConstPropertyName")

package com.reco1l.andengine

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.Log
import com.reco1l.andengine.component.UIComponent
import org.anddev.andengine.opengl.font.Font
import org.anddev.andengine.opengl.texture.TextureOptions
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas
import org.anddev.andengine.opengl.util.GLHelper
import java.lang.ref.WeakReference

class UIResourceManager(private val context: Context) {

private val fonts = mutableMapOf<String, Font>()
private val fontSubscribers = mutableMapOf<Font, MutableList<WeakReference<UIComponent>>>()


fun getOrStoreFont(size: Float, family: String): Font {

val fontIdentifier = "${family}-${size}"

val fetchedFont = fonts[fontIdentifier]
if (fetchedFont != null) {
return fetchedFont
}

Log.i("UIResourceManager", "Loading font: $fontIdentifier with texture size ${GLHelper.GlMaxTextureWidth / 2}x${GLHelper.GlMaxTextureWidth / 2}")

val texture = BitmapTextureAtlas(
GLHelper.GlMaxTextureWidth / 2,
GLHelper.GlMaxTextureWidth / 2,
TextureOptions.BILINEAR_PREMULTIPLYALPHA
)
val typeface = Typeface.createFromAsset(context.assets, "fonts/${family}")
val font = Font(texture, typeface, size, true, Color.WHITE)

UIEngine.current.apply {
textureManager.loadTexture(texture)
fontManager.loadFont(font)

fonts[fontIdentifier] = font
}

return font
}

fun subscribeToFont(font: Font, component: UIComponent) {
val subscribers = fontSubscribers.getOrPut(font) { mutableListOf() }
if (subscribers.none { it.get() === component }) {
subscribers.add(WeakReference(component))
}
}

fun unsubscribeFromFont(font: Font, component: UIComponent) {

val subscribers = fontSubscribers[font] ?: return
subscribers.removeAll { it.get() === component || it.get() == null }

if (subscribers.isEmpty()) {
val fontKey = fonts.entries.find { it.value == font }?.key
Log.i("UIResourceManager", "Unloading font: $fontKey")

fonts.remove(fontKey)
fontSubscribers.remove(font)

UIEngine.current.apply {
fontManager.unloadFont(font)
textureManager.unloadTexture(font.texture)
}
}
}

}
7 changes: 7 additions & 0 deletions src/com/reco1l/andengine/theme/Font.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@file:Suppress("ConstPropertyName")

package com.reco1l.andengine.theme

object Fonts {
const val NunitoMedium = "nunito-medium.ttf"
}