1313BORDER_STYLE_LIGHT_OUTLINE = 3
1414BORDER_STYLE_DARK_OUTLINE = 4
1515
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+
1662class InputFields :
1763
1864 ALPHANUMERIC = const (0 )
@@ -123,52 +169,6 @@ def _reassign_indices(self, bitmap, foreground_color_index, background_color_ind
123169 new_bitmap [(x , y )] = foreground_color_index
124170 return new_bitmap
125171
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-
172172 def _draw_button (self , buffer , text , font , x_position , y_position ,
173173 width = None , height = None , center_button = True , ** kwargs ):
174174 del kwargs ["center_dialog_vertically" ]
@@ -227,17 +227,17 @@ def _draw_background(
227227
228228 background_bitmap = displayio .Bitmap (width , height , len (self .shader ))
229229 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 )
231231 bitmaptools .blit (buffer , background_bitmap , x_position , y_position )
232232
233233 def _calculate_dialog_size (self , text , font , width , height , padding ):
234234 # Calculate the size of the dialog based on the text and font
235235 if text is not None :
236- text_width = self . _text_bounding_box (text , font )[2 ]
236+ text_width = text_bounding_box (text , font )[2 ]
237237 if width is None :
238238 width = text_width + padding ["right" ] + padding ["left" ]
239239 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" ]
241241 else :
242242 if width is None :
243243 width = 0
@@ -277,13 +277,13 @@ def display_simple(
277277 if background_color_index is None :
278278 background_color_index = self ._color_index ["dialog_background" ]
279279
280- padding = self . _convert_padding (padding )
280+ padding = convert_padding (padding )
281281
282282 if text is not None :
283283 text_area_padding = (0 , 0 )
284284 if width is None :
285285 # 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 ]
287287 text_area_padding = (- padding ["left" ], - padding ["right" ])
288288 else :
289289 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
357357 button_font = kwargs .pop ("button_font" )
358358 if "button_text" in kwargs :
359359 button_text = kwargs .pop ("button_text" )
360- padding = self . _convert_padding (kwargs .get ("padding" , 5 ))
360+ padding = convert_padding (kwargs .get ("padding" , 5 ))
361361 control_spacing = 5
362362 button_height = button_font .get_bounding_box ()[1 ] + control_spacing + padding ["bottom" ]
363363
@@ -403,7 +403,7 @@ def draw_field(self, field, first_draw=False):
403403 # draw the label
404404 label = TextBox (
405405 field ["font" ],
406- self . _text_bounding_box (field ["label" ], field ["font" ])[2 ],
406+ text_bounding_box (field ["label" ], field ["font" ])[2 ],
407407 TextBox .DYNAMIC_HEIGHT ,
408408 align = TextBox .ALIGN_RIGHT ,
409409 text = field ["label" ],
@@ -445,7 +445,7 @@ def draw_field(self, field, first_draw=False):
445445 border_color_ul , border_color_br = col_index ["black" ], col_index ["black" ]
446446 else :
447447 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 )
449449 bitmaptools .blit (field ["buffer" ], textbox_bmp , field ["x" ], field ["y" ] - 2 )
450450 field ["redraw" ] = False
451451
@@ -456,12 +456,12 @@ def display_input(self, text, font, fields, buttons, width,
456456 padding = 10
457457 if "button_font" in kwargs :
458458 button_font = kwargs .pop ("button_font" )
459- padding = self . _convert_padding (kwargs .get ("padding" , 10 ))
459+ padding = convert_padding (kwargs .get ("padding" , 10 ))
460460 control_spacing = 8
461461 button_height = button_font .get_bounding_box ()[1 ] + control_spacing + padding ["bottom" ]
462462
463463 # 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 ]
465465 field_area_height = (field_height + control_spacing )* len (fields )
466466
467467 # Draw dialog (and text if present)
@@ -487,7 +487,7 @@ def display_input(self, text, font, fields, buttons, width,
487487 for field in fields :
488488 max_field_label_width = max (
489489 max_field_label_width ,
490- self . _text_bounding_box (
490+ text_bounding_box (
491491 field ["label" ], font )[2 ] + padding ["right" ] + padding ["left" ]
492492 )
493493
@@ -528,7 +528,7 @@ def display_input(self, text, font, fields, buttons, width,
528528 # Figure out the maximum width of the buttons by checking the bounding box of their text
529529 total_button_width = 0
530530 for button_text in buttons :
531- total_button_width += self . _text_bounding_box (
531+ total_button_width += text_bounding_box (
532532 button_text , button_font )[2 ] + padding ["right" ] + padding ["left" ] + 2
533533
534534 button_spacing = (dialog_width - total_button_width ) // (len (buttons ) + 1 )
0 commit comments