13
13
BORDER_STYLE_LIGHT_OUTLINE = 3
14
14
BORDER_STYLE_DARK_OUTLINE = 4
15
15
16
+ def add_border (bitmap , border_color_ul , border_color_br ):
17
+ if border_color_ul is not None :
18
+ for x in range (bitmap .width ):
19
+ bitmap [x , 0 ] = border_color_ul
20
+ for y in range (bitmap .height ):
21
+ bitmap [0 , y ] = border_color_ul
22
+ if border_color_br is not None :
23
+ for x in range (bitmap .width ):
24
+ bitmap [x , bitmap .height - 1 ] = border_color_br
25
+ for y in range (bitmap .height ):
26
+ bitmap [bitmap .width - 1 , y ] = border_color_br
27
+ return bitmap
28
+
29
+ def convert_padding (padding ):
30
+ if isinstance (padding , int ): # Top, Right, Bottom Left (same as CSS)
31
+ padding = {
32
+ "top" : padding // 2 ,
33
+ "right" : padding // 2 ,
34
+ "bottom" : padding // 2 ,
35
+ "left" : padding // 2
36
+ }
37
+ elif isinstance (padding , (tuple , list )) and len (padding ) == 2 : # Top/Bottom, Left/Right
38
+ padding = {
39
+ "top" : padding [0 ],
40
+ "right" : padding [1 ],
41
+ "bottom" : padding [0 ],
42
+ "left" : padding [1 ]
43
+ }
44
+ elif isinstance (padding , (tuple , list )) and len (padding ) == 4 : # Top, Right, Bottom, Left
45
+ padding = {
46
+ "top" : padding [0 ],
47
+ "right" : padding [1 ],
48
+ "bottom" : padding [2 ],
49
+ "left" : padding [3 ]
50
+ }
51
+ return padding
52
+
53
+ def text_bounding_box (text , font , line_spacing = 0.75 ):
54
+ temp_label = bitmap_label .Label (
55
+ font ,
56
+ text = text ,
57
+ line_spacing = line_spacing ,
58
+ background_tight = True
59
+ )
60
+ return temp_label .bounding_box
61
+
16
62
class InputFields :
17
63
18
64
ALPHANUMERIC = const (0 )
@@ -123,52 +169,6 @@ def _reassign_indices(self, bitmap, foreground_color_index, background_color_ind
123
169
new_bitmap [(x , y )] = foreground_color_index
124
170
return new_bitmap
125
171
126
- def _add_border (self , bitmap , border_color_ul , border_color_br ):
127
- if border_color_ul is not None :
128
- for x in range (bitmap .width ):
129
- bitmap [x , 0 ] = border_color_ul
130
- for y in range (bitmap .height ):
131
- bitmap [0 , y ] = border_color_ul
132
- if border_color_br is not None :
133
- for x in range (bitmap .width ):
134
- bitmap [x , bitmap .height - 1 ] = border_color_br
135
- for y in range (bitmap .height ):
136
- bitmap [bitmap .width - 1 , y ] = border_color_br
137
- return bitmap
138
-
139
- def _convert_padding (self , padding ):
140
- if isinstance (padding , int ): # Top, Right, Bottom Left (same as CSS)
141
- padding = {
142
- "top" : padding // 2 ,
143
- "right" : padding // 2 ,
144
- "bottom" : padding // 2 ,
145
- "left" : padding // 2
146
- }
147
- elif isinstance (padding , (tuple , list )) and len (padding ) == 2 : # Top/Bottom, Left/Right
148
- padding = {
149
- "top" : padding [0 ],
150
- "right" : padding [1 ],
151
- "bottom" : padding [0 ],
152
- "left" : padding [1 ]
153
- }
154
- elif isinstance (padding , (tuple , list )) and len (padding ) == 4 : # Top, Right, Bottom, Left
155
- padding = {
156
- "top" : padding [0 ],
157
- "right" : padding [1 ],
158
- "bottom" : padding [2 ],
159
- "left" : padding [3 ]
160
- }
161
- return padding
162
-
163
- def _text_bounding_box (self , text , font , line_spacing = 0.75 ):
164
- temp_label = bitmap_label .Label (
165
- font ,
166
- text = text ,
167
- line_spacing = line_spacing ,
168
- background_tight = True
169
- )
170
- return temp_label .bounding_box
171
-
172
172
def _draw_button (self , buffer , text , font , x_position , y_position ,
173
173
width = None , height = None , center_button = True , ** kwargs ):
174
174
del kwargs ["center_dialog_vertically" ]
@@ -227,17 +227,17 @@ def _draw_background(
227
227
228
228
background_bitmap = displayio .Bitmap (width , height , len (self .shader ))
229
229
background_bitmap .fill (background_color_index )
230
- background_bitmap = self . _add_border (background_bitmap , border_color_ul , border_color_br )
230
+ background_bitmap = add_border (background_bitmap , border_color_ul , border_color_br )
231
231
bitmaptools .blit (buffer , background_bitmap , x_position , y_position )
232
232
233
233
def _calculate_dialog_size (self , text , font , width , height , padding ):
234
234
# Calculate the size of the dialog based on the text and font
235
235
if text is not None :
236
- text_width = self . _text_bounding_box (text , font )[2 ]
236
+ text_width = text_bounding_box (text , font )[2 ]
237
237
if width is None :
238
238
width = text_width + padding ["right" ] + padding ["left" ]
239
239
if height is None :
240
- height = self . _text_bounding_box (text , font )[3 ] + padding ["top" ] + padding ["bottom" ]
240
+ height = text_bounding_box (text , font )[3 ] + padding ["top" ] + padding ["bottom" ]
241
241
else :
242
242
if width is None :
243
243
width = 0
@@ -277,13 +277,13 @@ def display_simple(
277
277
if background_color_index is None :
278
278
background_color_index = self ._color_index ["dialog_background" ]
279
279
280
- padding = self . _convert_padding (padding )
280
+ padding = convert_padding (padding )
281
281
282
282
if text is not None :
283
283
text_area_padding = (0 , 0 )
284
284
if width is None :
285
285
# Create a regular bitmap label with the text to get the width
286
- text_width = self . _text_bounding_box (text , font , line_spacing = line_spacing )[2 ]
286
+ text_width = text_bounding_box (text , font , line_spacing = line_spacing )[2 ]
287
287
text_area_padding = (- padding ["left" ], - padding ["right" ])
288
288
else :
289
289
text_width = width - padding ["right" ] - padding ["left" ] - border_width * 2
@@ -357,7 +357,7 @@ def display_message(self, text, font, width, height, x_position, y_position, buf
357
357
button_font = kwargs .pop ("button_font" )
358
358
if "button_text" in kwargs :
359
359
button_text = kwargs .pop ("button_text" )
360
- padding = self . _convert_padding (kwargs .get ("padding" , 5 ))
360
+ padding = convert_padding (kwargs .get ("padding" , 5 ))
361
361
control_spacing = 5
362
362
button_height = button_font .get_bounding_box ()[1 ] + control_spacing + padding ["bottom" ]
363
363
@@ -403,7 +403,7 @@ def draw_field(self, field, first_draw=False):
403
403
# draw the label
404
404
label = TextBox (
405
405
field ["font" ],
406
- self . _text_bounding_box (field ["label" ], field ["font" ])[2 ],
406
+ text_bounding_box (field ["label" ], field ["font" ])[2 ],
407
407
TextBox .DYNAMIC_HEIGHT ,
408
408
align = TextBox .ALIGN_RIGHT ,
409
409
text = field ["label" ],
@@ -445,7 +445,7 @@ def draw_field(self, field, first_draw=False):
445
445
border_color_ul , border_color_br = col_index ["black" ], col_index ["black" ]
446
446
else :
447
447
border_color_ul , border_color_br = col_index ["light_gray" ], col_index ["light_gray" ]
448
- textbox_bmp = self . _add_border (textbox_bmp , border_color_ul , border_color_br )
448
+ textbox_bmp = add_border (textbox_bmp , border_color_ul , border_color_br )
449
449
bitmaptools .blit (field ["buffer" ], textbox_bmp , field ["x" ], field ["y" ] - 2 )
450
450
field ["redraw" ] = False
451
451
@@ -456,12 +456,12 @@ def display_input(self, text, font, fields, buttons, width,
456
456
padding = 10
457
457
if "button_font" in kwargs :
458
458
button_font = kwargs .pop ("button_font" )
459
- padding = self . _convert_padding (kwargs .get ("padding" , 10 ))
459
+ padding = convert_padding (kwargs .get ("padding" , 10 ))
460
460
control_spacing = 8
461
461
button_height = button_font .get_bounding_box ()[1 ] + control_spacing + padding ["bottom" ]
462
462
463
463
# Calculate total field height
464
- field_height = self . _text_bounding_box (fields [0 ]["label" ], font , line_spacing = 0.75 )[3 ]
464
+ field_height = text_bounding_box (fields [0 ]["label" ], font , line_spacing = 0.75 )[3 ]
465
465
field_area_height = (field_height + control_spacing )* len (fields )
466
466
467
467
# Draw dialog (and text if present)
@@ -487,7 +487,7 @@ def display_input(self, text, font, fields, buttons, width,
487
487
for field in fields :
488
488
max_field_label_width = max (
489
489
max_field_label_width ,
490
- self . _text_bounding_box (
490
+ text_bounding_box (
491
491
field ["label" ], font )[2 ] + padding ["right" ] + padding ["left" ]
492
492
)
493
493
@@ -528,7 +528,7 @@ def display_input(self, text, font, fields, buttons, width,
528
528
# Figure out the maximum width of the buttons by checking the bounding box of their text
529
529
total_button_width = 0
530
530
for button_text in buttons :
531
- total_button_width += self . _text_bounding_box (
531
+ total_button_width += text_bounding_box (
532
532
button_text , button_font )[2 ] + padding ["right" ] + padding ["left" ] + 2
533
533
534
534
button_spacing = (dialog_width - total_button_width ) // (len (buttons ) + 1 )
0 commit comments