diff --git a/selfdrive/ui/widgets/exp_mode_button.py b/selfdrive/ui/widgets/exp_mode_button.py index be50c59d06..272bcd0b28 100644 --- a/selfdrive/ui/widgets/exp_mode_button.py +++ b/selfdrive/ui/widgets/exp_mode_button.py @@ -14,7 +14,6 @@ class ExperimentalModeButton(Widget): self.params = Params() self.experimental_mode = self.params.get_bool("ExperimentalMode") - self.is_pressed = False self.chill_pixmap = gui_app.texture("icons/couch.png", self.img_width, self.img_width) self.experimental_pixmap = gui_app.texture("icons/experimental_grey.png", self.img_width, self.img_width) @@ -32,18 +31,23 @@ class ExperimentalModeButton(Widget): rl.draw_rectangle_gradient_h(int(rect.x), int(rect.y), int(rect.width), int(rect.height), start_color, end_color) - def _handle_interaction(self, rect): - mouse_pos = rl.get_mouse_position() - mouse_in_rect = rl.check_collision_point_rec(mouse_pos, rect) + # def _handle_interaction(self, rect): + # mouse_pos = rl.get_mouse_position() + # mouse_in_rect = rl.check_collision_point_rec(mouse_pos, rect) + # + # # self.is_pressed = mouse_in_rect and rl.is_mouse_button_down(rl.MOUSE_BUTTON_LEFT) + # return mouse_in_rect and rl.is_mouse_button_released(rl.MOUSE_BUTTON_LEFT) - self.is_pressed = mouse_in_rect and rl.is_mouse_button_down(rl.MOUSE_BUTTON_LEFT) - return mouse_in_rect and rl.is_mouse_button_released(rl.MOUSE_BUTTON_LEFT) + def _handle_mouse_release(self, mouse_pos): + self.experimental_mode = not self.experimental_mode + # TODO: Opening settings for ExperimentalMode + self.params.put_bool("ExperimentalMode", self.experimental_mode) def _render(self, rect): - if self._handle_interaction(rect): - self.experimental_mode = not self.experimental_mode - # TODO: Opening settings for ExperimentalMode - self.params.put_bool("ExperimentalMode", self.experimental_mode) + # if self._handle_interaction(rect): + # self.experimental_mode = not self.experimental_mode + # # TODO: Opening settings for ExperimentalMode + # self.params.put_bool("ExperimentalMode", self.experimental_mode) rl.draw_rectangle_rounded(rect, 0.08, 20, rl.Color(255, 255, 255, 255)) diff --git a/system/ui/widgets/__init__.py b/system/ui/widgets/__init__.py index a2c34799f5..b8e394b906 100644 --- a/system/ui/widgets/__init__.py +++ b/system/ui/widgets/__init__.py @@ -15,6 +15,7 @@ class Widget(abc.ABC): def __init__(self): self._rect: rl.Rectangle = rl.Rectangle(0, 0, 0, 0) self._parent_rect: rl.Rectangle = rl.Rectangle(0, 0, 0, 0) + self._start_pressed = [False] * MAX_TOUCH_SLOTS self._is_pressed = [False] * MAX_TOUCH_SLOTS self._enabled: bool | Callable[[], bool] = True self._is_visible: bool | Callable[[], bool] = True @@ -83,18 +84,48 @@ class Widget(abc.ABC): for mouse_event in gui_app.mouse_events: if not self._multi_touch and mouse_event.slot != 0: continue + + # Track if mouse down started within our rect + # TODO: move down if mouse_event.left_pressed and self._touch_valid(): if rl.check_collision_point_rec(mouse_event.pos, self._rect): - self._is_pressed[mouse_event.slot] = True + self._start_pressed[mouse_event.slot] = True + # Callback such as scroll panel signifies user is scrolling elif not self._touch_valid(): - self._is_pressed[mouse_event.slot] = False + self._start_pressed[mouse_event.slot] = False + + elif mouse_event.left_released: + self._start_pressed[mouse_event.slot] = False + + if mouse_event.left_pressed and self._touch_valid(): + print('left pressed') + if rl.check_collision_point_rec(mouse_event.pos, self._rect): + self._is_pressed[mouse_event.slot] = True elif mouse_event.left_released: if self._is_pressed[mouse_event.slot] and rl.check_collision_point_rec(mouse_event.pos, self._rect): self._handle_mouse_release(mouse_event.pos) self._is_pressed[mouse_event.slot] = False + elif rl.check_collision_point_rec(mouse_event.pos, self._rect): + print('inside rect') + if self._start_pressed[mouse_event.slot]: + # Mouse/touch is still within our rect + self._is_pressed[mouse_event.slot] = True + + # Mouse/touch left our rect + elif not rl.check_collision_point_rec(mouse_event.pos, self._rect): + self._is_pressed[mouse_event.slot] = False + + # Callback such as scroll panel signifies user is scrolling + elif not self._touch_valid(): + self._is_pressed[mouse_event.slot] = False + + if self.__class__.__name__ == "ExperimentalModeButton": + print(f"Widget {self.__class__.__name__}: SLOT: {mouse_event.slot}, pressed: {self._is_pressed[mouse_event.slot]}, start_pressed: {self._start_pressed[mouse_event.slot]}") + + print() return ret @abc.abstractmethod