@@ -13,10 +13,19 @@ import javax.microedition.khronos.opengles.GL10
1313 */
1414abstract class UIBufferedComponent <T : IBuffer > : UIComponent () {
1515
16+ override var radius: Float
17+ get() = super .radius
18+ set(value) {
19+ if (super .radius != value) {
20+ super .radius = value
21+ requestBufferUpdate()
22+ }
23+ }
24+
1625 /* *
1726 * The buffer itself.
1827 */
19- open var buffer: T ? = null
28+ var buffer: T ? = null
2029 set(value) {
2130 if (field != value) {
2231 field?.finalize()
@@ -29,11 +38,6 @@ abstract class UIBufferedComponent<T : IBuffer> : UIComponent() {
2938 */
3039 var allowBufferCache = true
3140
32- /* *
33- * Whether to allow the buffer to be updated dynamically.
34- */
35- var allowBufferDynamicUpdate = true
36-
3741 /* *
3842 * The blend information of the entity.
3943 */
@@ -70,7 +74,18 @@ abstract class UIBufferedComponent<T : IBuffer> : UIComponent() {
7074 protected abstract fun generateBufferCacheKey (): String
7175
7276 /* *
73- * Called when the buffer needs to be built.
77+ * Determines if the current buffer can be reused instead of creating a new one.
78+ *
79+ * Override this method to implement custom buffer reuse logic based on buffer properties.
80+ * Return `true` if the existing buffer is compatible and can be reused, `false` otherwise.
81+ *
82+ * @param buffer The current buffer to check for reusability
83+ * @return `true` if the buffer can be reused, `false` if a new buffer should be created
84+ */
85+ protected open fun canReuseBuffer (buffer : T ): Boolean = false
86+
87+ /* *
88+ * Called when a new buffer needs to be created.
7489 */
7590 protected abstract fun createBuffer (): T
7691
@@ -96,7 +111,8 @@ abstract class UIBufferedComponent<T : IBuffer> : UIComponent() {
96111 requestBufferUpdate()
97112 }
98113
99- override fun onManagedDraw (gl : GL10 , camera : Camera ) {
114+ override fun onHandleInvalidations () {
115+ super .onHandleInvalidations()
100116
101117 // Buffer update is done after invalidations are handled so we can
102118 // refer the buffer in those invalidations.
@@ -105,34 +121,38 @@ abstract class UIBufferedComponent<T : IBuffer> : UIComponent() {
105121
106122 val cacheKey = generateBufferCacheKey()
107123
108- if (bufferCacheKey != cacheKey) {
109-
110- if (allowBufferCache) {
111- val newBuffer = UIEngine .current.resources.getOrStoreBuffer(cacheKey) { createBuffer() }
124+ if (allowBufferCache) {
125+ if (bufferCacheKey != cacheKey) {
126+ val newBuffer = UIEngine .current.resources.getOrStoreBuffer(cacheKey) {
127+ val currentBuffer = buffer
128+ if (currentBuffer != null && canReuseBuffer(currentBuffer)) {
129+ currentBuffer
130+ } else {
131+ createBuffer()
132+ }
133+ }
112134
113135 val oldBuffer = buffer
114- if (oldBuffer != null ) {
136+ if (oldBuffer != null && oldBuffer != = newBuffer ) {
115137 UIEngine .current.resources.unsubscribeFromBuffer(oldBuffer, this )
116138 }
117139
118140 UIEngine .current.resources.subscribeToBuffer(newBuffer, this )
119141
120142 @Suppress(" UNCHECKED_CAST" )
121143 buffer = newBuffer as T ?
122- } else {
123- if (! allowBufferDynamicUpdate || buffer == null ) {
124- buffer = createBuffer()
125- }
144+ bufferCacheKey = cacheKey
145+ }
146+ } else {
147+ val currentBuffer = buffer
148+ if (currentBuffer == null || ! canReuseBuffer(currentBuffer)) {
149+ buffer = createBuffer()
126150 }
127-
128- bufferCacheKey = cacheKey
129151 }
130152
131153 onUpdateBuffer()
132154 buffer?.invalidateOnHardware()
133155 }
134-
135- super .onManagedDraw(gl, camera)
136156 }
137157
138158 override fun doDraw (gl : GL10 , camera : Camera ) {
0 commit comments