Port reset to new raylib events handling (#35762)

* test

* more

* type

* type

* order

* _

* __
This commit is contained in:
Maxime Desroches
2025-07-18 20:09:35 -07:00
committed by GitHub
parent 48892e339d
commit 1936c42ee7
2 changed files with 78 additions and 29 deletions
+47 -2
View File
@@ -1,7 +1,11 @@
import pyray as rl
from collections.abc import Callable
from enum import IntEnum
from openpilot.system.ui.lib.application import gui_app, FontWeight
import pyray as rl
from openpilot.system.ui.lib.application import gui_app, FontWeight, MousePos
from openpilot.system.ui.lib.text_measure import measure_text_cached
from openpilot.system.ui.widgets import Widget
class ButtonStyle(IntEnum):
@@ -148,3 +152,44 @@ def gui_button(
rl.draw_text_ex(font, text, text_pos, font_size, 0, color)
return result
class Button(Widget):
def __init__(self,
text: str,
click_callback: Callable[[], None] = None,
font_size: int = DEFAULT_BUTTON_FONT_SIZE,
font_weight: FontWeight = FontWeight.MEDIUM,
button_style: ButtonStyle = ButtonStyle.NORMAL,
border_radius: int = 10,
):
super().__init__()
self._text = text
self._click_callback = click_callback
self._label_font = gui_app.font(FontWeight.SEMI_BOLD)
self._button_style = button_style
self._font_size = font_size
self._border_radius = border_radius
self._font_size = font_size
self._text_color = BUTTON_TEXT_COLOR[button_style]
self._text_size = measure_text_cached(gui_app.font(font_weight), text, font_size)
def _handle_mouse_release(self, mouse_pos: MousePos):
if self._click_callback:
print(f"Button clicked: {self._text}")
self._click_callback()
def _get_background_color(self) -> rl.Color:
if self._is_pressed:
return BUTTON_PRESSED_BACKGROUND_COLORS[self._button_style]
else:
return BUTTON_BACKGROUND_COLORS[self._button_style]
def _render(self, _):
roundness = self._border_radius / (min(self._rect.width, self._rect.height) / 2)
rl.draw_rectangle_rounded(self._rect, roundness, 10, self._get_background_color())
text_pos = rl.Vector2(0, self._rect.y + (self._rect.height - self._text_size.y) // 2)
text_pos.x = self._rect.x + (self._rect.width - self._text_size.x) // 2
rl.draw_text_ex(self._label_font, self._text, text_pos, self._font_size, 0, self._text_color)