@@ -112,11 +112,7 @@ def __init__(
112
112
113
113
self ._background_color = background_color
114
114
self ._background_palette = displayio .Palette (1 )
115
- self .append (
116
- displayio .TileGrid (
117
- displayio .Bitmap (0 , 0 , 1 ), pixel_shader = self ._background_palette
118
- )
119
- ) # initialize with a blank tilegrid placeholder for background
115
+ self ._added_background_tilegrid = False
120
116
121
117
self ._padding_top = padding_top
122
118
self ._padding_bottom = padding_bottom
@@ -160,7 +156,6 @@ def _create_background_box(self, lines, y_offset):
160
156
)
161
157
y_box_offset = - ascender_max + y_offset - self ._padding_top
162
158
163
- self ._update_background_color (self ._background_color )
164
159
box_width = max (0 , box_width ) # remove any negative values
165
160
box_height = max (0 , box_height ) # remove any negative values
166
161
@@ -178,15 +173,65 @@ def _update_background_color(self, new_color):
178
173
179
174
if new_color is None :
180
175
self ._background_palette .make_transparent (0 )
176
+ if self ._added_background_tilegrid :
177
+ self .pop (0 )
178
+ self ._added_background_tilegrid = False
181
179
else :
182
180
self ._background_palette .make_opaque (0 )
183
181
self ._background_palette [0 ] = new_color
184
182
self ._background_color = new_color
185
183
186
- def _update_text (self , new_text ): # pylint: disable=too-many-locals
184
+ y_offset = int (
185
+ (
186
+ self ._font .get_glyph (ord ("M" )).height
187
+ - self .text .count ("\n " ) * self .height * self .line_spacing
188
+ )
189
+ / 2
190
+ )
191
+ lines = self .text .count ("\n " ) + 1
192
+
193
+ if not self ._added_background_tilegrid : # no bitmap is in the self Group
194
+ # add bitmap if text is present and bitmap sizes > 0 pixels
195
+ if (
196
+ (len (self ._text ) > 0 )
197
+ and (
198
+ self ._boundingbox [2 ] + self ._padding_left + self ._padding_right > 0
199
+ )
200
+ and (
201
+ self ._boundingbox [3 ] + self ._padding_top + self ._padding_bottom > 0
202
+ )
203
+ ):
204
+ if len (self ) > 0 :
205
+ self .insert (0 , self ._create_background_box (lines , y_offset ))
206
+ else :
207
+ self .append (self ._create_background_box (lines , y_offset ))
208
+ self ._added_background_tilegrid = True
209
+
210
+ else : # a bitmap is present in the self Group
211
+ # update bitmap if text is present and bitmap sizes > 0 pixels
212
+ if (
213
+ (len (self ._text ) > 0 )
214
+ and (
215
+ self ._boundingbox [2 ] + self ._padding_left + self ._padding_right > 0
216
+ )
217
+ and (
218
+ self ._boundingbox [3 ] + self ._padding_top + self ._padding_bottom > 0
219
+ )
220
+ ):
221
+ self [0 ] = self ._create_background_box (lines , y_offset )
222
+ else : # delete the existing bitmap
223
+ self .pop (0 )
224
+ self ._added_background_tilegrid = False
225
+
226
+ def _update_text (
227
+ self , new_text
228
+ ): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
187
229
x = 0
188
230
y = 0
189
- i = 1
231
+ if self ._added_background_tilegrid :
232
+ i = 1
233
+ else :
234
+ i = 0
190
235
old_c = 0
191
236
y_offset = int (
192
237
(
@@ -268,7 +313,8 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
268
313
self .pop ()
269
314
self ._text = new_text
270
315
self ._boundingbox = (left , top , left + right , bottom - top )
271
- self [0 ] = self ._create_background_box (lines , y_offset )
316
+
317
+ self ._update_background_color (self ._background_color )
272
318
273
319
@property
274
320
def bounding_box (self ):
@@ -351,15 +397,11 @@ def anchored_position(self):
351
397
"""Position relative to the anchor_point. Tuple containing x,y
352
398
pixel coordinates."""
353
399
return (
354
- int (
355
- self .x
356
- + self ._boundingbox [0 ]
357
- + self ._anchor_point [0 ] * self ._boundingbox [2 ]
358
- ),
400
+ int (self .x + (self ._anchor_point [0 ] * self ._boundingbox [2 ] * self ._scale )),
359
401
int (
360
402
self .y
361
- + self ._boundingbox [1 ]
362
- + self ._anchor_point [ 1 ] * self ._boundingbox [ 3 ]
403
+ + ( self ._anchor_point [1 ] * self . _boundingbox [ 3 ] * self . _scale )
404
+ - round (( self ._boundingbox [ 3 ] * self ._scale ) / 2.0 )
363
405
),
364
406
)
365
407
@@ -369,11 +411,10 @@ def anchored_position(self, new_position):
369
411
new_position [0 ]
370
412
- self ._anchor_point [0 ] * (self ._boundingbox [2 ] * self ._scale )
371
413
)
372
- new_y = self . y = int (
414
+ new_y = int (
373
415
new_position [1 ]
374
- - self ._anchor_point [1 ] * ( self ._boundingbox [3 ] * self ._scale )
375
- + ( self ._boundingbox [3 ] * self ._scale ) / 2
416
+ - ( self ._anchor_point [1 ] * self ._boundingbox [3 ] * self ._scale )
417
+ + round (( self ._boundingbox [3 ] * self ._scale ) / 2.0 )
376
418
)
377
- self ._boundingbox = (new_x , new_y , self ._boundingbox [2 ], self ._boundingbox [3 ])
378
419
self .x = new_x
379
420
self .y = new_y
0 commit comments