diff --git a/korge/src/commonMain/kotlin/korlibs/korge/internal/KorgeInternal.kt b/korge/src/commonMain/kotlin/korlibs/korge/internal/KorgeInternal.kt index c3afcb5512..3f604eaee2 100644 --- a/korge/src/commonMain/kotlin/korlibs/korge/internal/KorgeInternal.kt +++ b/korge/src/commonMain/kotlin/korlibs/korge/internal/KorgeInternal.kt @@ -3,3 +3,6 @@ package korlibs.korge.internal /** A mechanism to annotate Korge internal properties and methods that were left open in KorGE 1.0 so they can be marked as internal in KorGE 2.0 */ @RequiresOptIn(level = RequiresOptIn.Level.WARNING) annotation class KorgeInternal + +//object KorgeInternalApi + diff --git a/korge/src/commonMain/kotlin/korlibs/korge/ui/UIView.kt b/korge/src/commonMain/kotlin/korlibs/korge/ui/UIView.kt index 294b52563f..ae15e46ad8 100644 --- a/korge/src/commonMain/kotlin/korlibs/korge/ui/UIView.kt +++ b/korge/src/commonMain/kotlin/korlibs/korge/ui/UIView.kt @@ -54,7 +54,8 @@ open class UIView( parent?.onChildChangedSize(this) } - override fun onParentChanged() { + override fun onAncestorChanged() { + super.onAncestorChanged() updateState() } diff --git a/korge/src/commonMain/kotlin/korlibs/korge/view/BaseGraphics.kt b/korge/src/commonMain/kotlin/korlibs/korge/view/BaseGraphics.kt index 6db5453e33..ad4aa0d196 100644 --- a/korge/src/commonMain/kotlin/korlibs/korge/view/BaseGraphics.kt +++ b/korge/src/commonMain/kotlin/korlibs/korge/view/BaseGraphics.kt @@ -51,14 +51,38 @@ abstract class BaseGraphics( invalidateLocalBounds() } - @OptIn(KorgeExperimental::class) - override fun renderInternal(ctx: RenderContext) { + override fun onAncestorChanged() { + super.onAncestorChanged() + if (parent == null || stage == null) { + if (bitmap.base != Bitmaps.transparent.bmp) { + //println("REMOVED FROM STAGE! $bitmap") + bitmapsToRemove.add(bitmap.base) + } + clearBitmaps(onParentChanged = true) + } + } + + private fun clearBitmaps(onParentChanged: Boolean = false) { + if (bitmapsToRemove.isEmpty()) return + + if (onParentChanged) { + //println("clearBitmaps! onParentChanged=$onParentChanged : $bitmapsToRemove") + } + bitmapsToRemove.fastForEach { if (it != Bitmaps.transparent.bmp) { - ctx.agBitmapTextureManager.removeBitmap(it, "BaseGraphics") + _ctx?.agBitmapTextureManager?.removeBitmap(it, "BaseGraphics") } } bitmapsToRemove.clear() + } + + private var _ctx: RenderContext? = null + + @OptIn(KorgeExperimental::class) + override fun renderInternal(ctx: RenderContext) { + _ctx = ctx + clearBitmaps(onParentChanged = false) if (redrawIfRequired()) { //ctx.coroutineContext.launchUnscoped { this.bitmap.bmpBase.writeTo(localVfs("/tmp/image.png"), PNG) } diff --git a/korge/src/commonMain/kotlin/korlibs/korge/view/Container.kt b/korge/src/commonMain/kotlin/korlibs/korge/view/Container.kt index a309db194c..802ad38cbe 100644 --- a/korge/src/commonMain/kotlin/korlibs/korge/view/Container.kt +++ b/korge/src/commonMain/kotlin/korlibs/korge/view/Container.kt @@ -146,6 +146,13 @@ open class Container( open fun onChildChangedSize(view: View) { } + override fun onAncestorChanged() { + super.onAncestorChanged() + forEachChild { + //it.apply { KorgeInternalApi._onAncestorChanged() } + it._onAncestorChanged() + } + } /** * Retrieves the index of a given child [View]. */ diff --git a/korge/src/commonMain/kotlin/korlibs/korge/view/View.kt b/korge/src/commonMain/kotlin/korlibs/korge/view/View.kt index dc833972b2..a0b2660d41 100644 --- a/korge/src/commonMain/kotlin/korlibs/korge/view/View.kt +++ b/korge/src/commonMain/kotlin/korlibs/korge/view/View.kt @@ -768,6 +768,15 @@ abstract class View internal constructor( } protected open fun onParentChanged() { + onAncestorChanged() + } + + //fun KorgeInternalApi._onAncestorChanged() { + internal fun _onAncestorChanged() { + onAncestorChanged() + } + + protected open fun onAncestorChanged() { } override fun invalidateRender() {