You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					81 lines
				
				3.0 KiB
			
		
		
			
		
	
	
					81 lines
				
				3.0 KiB
			| 
											9 months ago
										 | import pyray as rl
 | ||
| 
											8 months ago
										 | from openpilot.system.ui.lib.application import gui_app, FontWeight, DEFAULT_TEXT_SIZE, DEFAULT_TEXT_COLOR
 | ||
| 
											5 months ago
										 | from openpilot.system.ui.lib.text_measure import measure_text_cached
 | ||
| 
											9 months ago
										 | from openpilot.system.ui.lib.utils import GuiStyleContext
 | ||
|  | 
 | ||
| 
											8 months ago
										 | 
 | ||
| 
											4 months ago
										 | # TODO: This should be a Widget class
 | ||
|  | 
 | ||
| 
											8 months ago
										 | def gui_label(
 | ||
|  |   rect: rl.Rectangle,
 | ||
|  |   text: str,
 | ||
|  |   font_size: int = DEFAULT_TEXT_SIZE,
 | ||
|  |   color: rl.Color = DEFAULT_TEXT_COLOR,
 | ||
|  |   font_weight: FontWeight = FontWeight.NORMAL,
 | ||
|  |   alignment: int = rl.GuiTextAlignment.TEXT_ALIGN_LEFT,
 | ||
| 
											6 months ago
										 |   alignment_vertical: int = rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE,
 | ||
|  |   elide_right: bool = True
 | ||
| 
											8 months ago
										 | ):
 | ||
|  |   font = gui_app.font(font_weight)
 | ||
| 
											5 months ago
										 |   text_size = measure_text_cached(font, text, font_size)
 | ||
| 
											6 months ago
										 |   display_text = text
 | ||
|  | 
 | ||
|  |   # Elide text to fit within the rectangle
 | ||
|  |   if elide_right and text_size.x > rect.width:
 | ||
|  |     ellipsis = "..."
 | ||
|  |     left, right = 0, len(text)
 | ||
|  |     while left < right:
 | ||
|  |       mid = (left + right) // 2
 | ||
|  |       candidate = text[:mid] + ellipsis
 | ||
| 
											5 months ago
										 |       candidate_size = measure_text_cached(font, candidate, font_size)
 | ||
| 
											6 months ago
										 |       if candidate_size.x <= rect.width:
 | ||
|  |         left = mid + 1
 | ||
|  |       else:
 | ||
|  |         right = mid
 | ||
|  |     display_text = text[: left - 1] + ellipsis if left > 0 else ellipsis
 | ||
| 
											5 months ago
										 |     text_size = measure_text_cached(font, display_text, font_size)
 | ||
| 
											8 months ago
										 | 
 | ||
|  |   # Calculate horizontal position based on alignment
 | ||
|  |   text_x = rect.x + {
 | ||
|  |     rl.GuiTextAlignment.TEXT_ALIGN_LEFT: 0,
 | ||
|  |     rl.GuiTextAlignment.TEXT_ALIGN_CENTER: (rect.width - text_size.x) / 2,
 | ||
|  |     rl.GuiTextAlignment.TEXT_ALIGN_RIGHT: rect.width - text_size.x,
 | ||
|  |   }.get(alignment, 0)
 | ||
|  | 
 | ||
|  |   # Calculate vertical position based on alignment
 | ||
|  |   text_y = rect.y + {
 | ||
|  |     rl.GuiTextAlignmentVertical.TEXT_ALIGN_TOP: 0,
 | ||
|  |     rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE: (rect.height - text_size.y) / 2,
 | ||
|  |     rl.GuiTextAlignmentVertical.TEXT_ALIGN_BOTTOM: rect.height - text_size.y,
 | ||
|  |   }.get(alignment_vertical, 0)
 | ||
|  | 
 | ||
|  |   # Draw the text in the specified rectangle
 | ||
| 
											6 months ago
										 |   rl.draw_text_ex(font, display_text, rl.Vector2(text_x, text_y), font_size, 0, color)
 | ||
| 
											8 months ago
										 | 
 | ||
|  | 
 | ||
|  | def gui_text_box(
 | ||
|  |   rect: rl.Rectangle,
 | ||
|  |   text: str,
 | ||
|  |   font_size: int = DEFAULT_TEXT_SIZE,
 | ||
|  |   color: rl.Color = DEFAULT_TEXT_COLOR,
 | ||
|  |   alignment: int = rl.GuiTextAlignment.TEXT_ALIGN_LEFT,
 | ||
| 
											5 months ago
										 |   alignment_vertical: int = rl.GuiTextAlignmentVertical.TEXT_ALIGN_TOP,
 | ||
|  |   font_weight: FontWeight = FontWeight.NORMAL,
 | ||
| 
											8 months ago
										 | ):
 | ||
| 
											9 months ago
										 |   styles = [
 | ||
| 
											8 months ago
										 |     (rl.GuiControl.DEFAULT, rl.GuiControlProperty.TEXT_COLOR_NORMAL, rl.color_to_int(color)),
 | ||
| 
											9 months ago
										 |     (rl.GuiControl.DEFAULT, rl.GuiDefaultProperty.TEXT_SIZE, font_size),
 | ||
|  |     (rl.GuiControl.DEFAULT, rl.GuiDefaultProperty.TEXT_LINE_SPACING, font_size),
 | ||
| 
											8 months ago
										 |     (rl.GuiControl.DEFAULT, rl.GuiControlProperty.TEXT_ALIGNMENT, alignment),
 | ||
|  |     (rl.GuiControl.DEFAULT, rl.GuiDefaultProperty.TEXT_ALIGNMENT_VERTICAL, alignment_vertical),
 | ||
| 
											9 months ago
										 |     (rl.GuiControl.DEFAULT, rl.GuiDefaultProperty.TEXT_WRAP_MODE, rl.GuiTextWrapMode.TEXT_WRAP_WORD)
 | ||
|  |   ]
 | ||
| 
											5 months ago
										 |   if font_weight != FontWeight.NORMAL:
 | ||
|  |     rl.gui_set_font(gui_app.font(font_weight))
 | ||
| 
											9 months ago
										 | 
 | ||
|  |   with GuiStyleContext(styles):
 | ||
|  |     rl.gui_label(rect, text)
 | ||
| 
											5 months ago
										 | 
 | ||
|  |   if font_weight != FontWeight.NORMAL:
 | ||
|  |     rl.gui_set_font(gui_app.font(FontWeight.NORMAL))
 |