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.
57 lines
2.2 KiB
57 lines
2.2 KiB
import pyray as rl
|
|
from openpilot.system.ui.lib.application import gui_app, FontWeight, DEFAULT_TEXT_SIZE, DEFAULT_TEXT_COLOR
|
|
from openpilot.system.ui.lib.utils import GuiStyleContext
|
|
|
|
|
|
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,
|
|
alignment_vertical: int = rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE
|
|
):
|
|
# Set font based on the provided weight
|
|
font = gui_app.font(font_weight)
|
|
|
|
# Measure text size
|
|
text_size = rl.measure_text_ex(font, text, font_size, 0)
|
|
|
|
# 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
|
|
rl.draw_text_ex(font, text, rl.Vector2(text_x, text_y), font_size, 0, color)
|
|
|
|
|
|
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,
|
|
alignment_vertical: int = rl.GuiTextAlignmentVertical.TEXT_ALIGN_TOP
|
|
):
|
|
styles = [
|
|
(rl.GuiControl.DEFAULT, rl.GuiControlProperty.TEXT_COLOR_NORMAL, rl.color_to_int(color)),
|
|
(rl.GuiControl.DEFAULT, rl.GuiDefaultProperty.TEXT_SIZE, font_size),
|
|
(rl.GuiControl.DEFAULT, rl.GuiDefaultProperty.TEXT_LINE_SPACING, font_size),
|
|
(rl.GuiControl.DEFAULT, rl.GuiControlProperty.TEXT_ALIGNMENT, alignment),
|
|
(rl.GuiControl.DEFAULT, rl.GuiDefaultProperty.TEXT_ALIGNMENT_VERTICAL, alignment_vertical),
|
|
(rl.GuiControl.DEFAULT, rl.GuiDefaultProperty.TEXT_WRAP_MODE, rl.GuiTextWrapMode.TEXT_WRAP_WORD)
|
|
]
|
|
|
|
with GuiStyleContext(styles):
|
|
rl.gui_label(rect, text)
|
|
|