python ui: display FPS on top-left corner if DEBUG_FPS=1 (#34595)

* display FPS on top-left corner if DEBUG_FPS=1

* use generator

* use rl.draw_fps
not-so-secret-good-op^2
Dean Lee 2 months ago committed by GitHub
parent 99ef66de4b
commit d4d0312794
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 15
      system/ui/lib/application.py
  2. 1
      system/ui/lib/utils.py
  3. 5
      system/ui/reset.py
  4. 10
      system/ui/spinner.py
  5. 7
      system/ui/text.py

@ -8,6 +8,9 @@ DEFAULT_TEXT_SIZE = 60
DEFAULT_FPS = 60 DEFAULT_FPS = 60
FONT_DIR = os.path.join(BASEDIR, "selfdrive/assets/fonts") FONT_DIR = os.path.join(BASEDIR, "selfdrive/assets/fonts")
DEBUG_FPS = os.getenv("DEBUG_FPS") == '1'
class FontWeight(IntEnum): class FontWeight(IntEnum):
BLACK = 0 BLACK = 0
BOLD = 1 BOLD = 1
@ -58,6 +61,18 @@ class GuiApplication:
rl.close_window() rl.close_window()
def render(self):
while not rl.window_should_close():
rl.begin_drawing()
rl.clear_background(rl.BLACK)
yield
if DEBUG_FPS:
rl.draw_fps(10, 10)
rl.end_drawing()
def font(self, font_wight: FontWeight=FontWeight.NORMAL): def font(self, font_wight: FontWeight=FontWeight.NORMAL):
return self._fonts[font_wight] return self._fonts[font_wight]

@ -1,5 +1,6 @@
import pyray as rl import pyray as rl
class GuiStyleContext: class GuiStyleContext:
def __init__(self, styles: list[tuple[int, int, int]]): def __init__(self, styles: list[tuple[int, int, int]]):
"""styles is a list of tuples (control, prop, new_value)""" """styles is a list of tuples (control, prop, new_value)"""

@ -110,12 +110,9 @@ def main():
if mode == ResetMode.FORMAT: if mode == ResetMode.FORMAT:
reset.start_reset() reset.start_reset()
while not rl.window_should_close(): for _ in gui_app.render():
rl.begin_drawing()
rl.clear_background(rl.BLACK)
if not reset.render(rl.Rectangle(45, 200, gui_app.width - 90, gui_app.height - 245)): if not reset.render(rl.Rectangle(45, 200, gui_app.width - 90, gui_app.height - 245)):
break break
rl.end_drawing()
if __name__ == "__main__": if __name__ == "__main__":

@ -15,14 +15,17 @@ MARGIN = 200
TEXTURE_SIZE = 360 TEXTURE_SIZE = 360
FONT_SIZE = 80 FONT_SIZE = 80
def clamp(value, min_value, max_value): def clamp(value, min_value, max_value):
return max(min(value, max_value), min_value) return max(min(value, max_value), min_value)
def check_input_non_blocking(): def check_input_non_blocking():
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]: if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
return sys.stdin.readline().strip() return sys.stdin.readline().strip()
return "" return ""
def main(): def main():
gui_app.init_window("Spinner") gui_app.init_window("Spinner")
@ -37,10 +40,7 @@ def main():
spinner_origin = rl.Vector2(TEXTURE_SIZE / 2.0, TEXTURE_SIZE / 2.0) spinner_origin = rl.Vector2(TEXTURE_SIZE / 2.0, TEXTURE_SIZE / 2.0)
comma_position = rl.Vector2(center.x - TEXTURE_SIZE / 2.0, center.y - TEXTURE_SIZE / 2.0) comma_position = rl.Vector2(center.x - TEXTURE_SIZE / 2.0, center.y - TEXTURE_SIZE / 2.0)
while not rl.window_should_close(): for _ in gui_app.render():
rl.begin_drawing()
rl.clear_background(rl.BLACK)
# Update rotation # Update rotation
rotation = (rotation + ROTATION_RATE) % 360.0 rotation = (rotation + ROTATION_RATE) % 360.0
@ -69,8 +69,6 @@ def main():
rl.draw_text_ex(gui_app.font(), user_input, rl.draw_text_ex(gui_app.font(), user_input,
rl.Vector2(center.x - text_size.x / 2, y_pos), FONT_SIZE, 1.0, rl.WHITE) rl.Vector2(center.x - text_size.x / 2, y_pos), FONT_SIZE, 1.0, rl.WHITE)
rl.end_drawing()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

@ -44,10 +44,7 @@ def main():
content_rect = rl.Rectangle(0, 0, textarea_rect.width - 20, len(wrapped_lines) * LINE_HEIGHT) content_rect = rl.Rectangle(0, 0, textarea_rect.width - 20, len(wrapped_lines) * LINE_HEIGHT)
scroll_panel = GuiScrollPanel(textarea_rect, content_rect, show_vertical_scroll_bar=True) scroll_panel = GuiScrollPanel(textarea_rect, content_rect, show_vertical_scroll_bar=True)
while not rl.window_should_close(): for _ in gui_app.render():
rl.begin_drawing()
rl.clear_background(rl.BLACK)
scroll = scroll_panel.handle_scroll() scroll = scroll_panel.handle_scroll()
rl.begin_scissor_mode(int(textarea_rect.x), int(textarea_rect.y), int(textarea_rect.width), int(textarea_rect.height)) rl.begin_scissor_mode(int(textarea_rect.x), int(textarea_rect.y), int(textarea_rect.width), int(textarea_rect.height))
@ -60,8 +57,6 @@ def main():
if gui_button(button_bounds, "Reboot"): if gui_button(button_bounds, "Reboot"):
HARDWARE.reboot() HARDWARE.reboot()
rl.end_drawing()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

Loading…
Cancel
Save