51 lines
1.1 KiB
Kotlin
51 lines
1.1 KiB
Kotlin
package ru.dbotthepony.kstarbound.client.render
|
|
|
|
import ru.dbotthepony.kstarbound.client.StarboundClient
|
|
import ru.dbotthepony.kstarbound.defs.image.ImageReference
|
|
|
|
class RebindableSprite(
|
|
val client: StarboundClient,
|
|
ref: ImageReference,
|
|
val renderParams: ((String) -> String?)? = null
|
|
) {
|
|
var sprite: BoundSprite? = null
|
|
private set
|
|
|
|
var ref: ImageReference = ref
|
|
private set
|
|
|
|
init {
|
|
val unbound = ref.sprite
|
|
|
|
if (unbound != null) {
|
|
sprite = BoundSprite(unbound, client.gl.loadTexture(ref.imagePath.value!!))
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Обновляет [ref] и [sprite] по значениям, которые выдаст [renderParams]
|
|
*
|
|
* Возвращает новое значение [sprite]
|
|
*/
|
|
fun update(): BoundSprite? {
|
|
client.gl.ensureSameThread()
|
|
|
|
if (renderParams == null)
|
|
return sprite
|
|
|
|
val newRef = ref.with(renderParams)
|
|
|
|
if (newRef !== ref) {
|
|
ref = newRef
|
|
sprite = null
|
|
val unbound = newRef.sprite
|
|
|
|
if (unbound != null) {
|
|
sprite = BoundSprite(unbound, client.gl.loadTexture(newRef.imagePath.value!!))
|
|
}
|
|
}
|
|
|
|
return sprite
|
|
}
|
|
}
|