|
|
@ -15,12 +15,13 @@ class Widget(abc.ABC): |
|
|
|
def __init__(self): |
|
|
|
def __init__(self): |
|
|
|
self._rect: rl.Rectangle = rl.Rectangle(0, 0, 0, 0) |
|
|
|
self._rect: rl.Rectangle = rl.Rectangle(0, 0, 0, 0) |
|
|
|
self._parent_rect: rl.Rectangle = rl.Rectangle(0, 0, 0, 0) |
|
|
|
self._parent_rect: rl.Rectangle = rl.Rectangle(0, 0, 0, 0) |
|
|
|
self._is_pressed = [False] * MAX_TOUCH_SLOTS |
|
|
|
self.__is_pressed = [False] * MAX_TOUCH_SLOTS |
|
|
|
# if current mouse/touch down started within the widget's rectangle |
|
|
|
# if current mouse/touch down started within the widget's rectangle |
|
|
|
self._tracking_is_pressed = [False] * MAX_TOUCH_SLOTS |
|
|
|
self.__tracking_is_pressed = [False] * MAX_TOUCH_SLOTS |
|
|
|
self._enabled: bool | Callable[[], bool] = True |
|
|
|
self._enabled: bool | Callable[[], bool] = True |
|
|
|
self._is_visible: bool | Callable[[], bool] = True |
|
|
|
self._is_visible: bool | Callable[[], bool] = True |
|
|
|
self._touch_valid_callback: Callable[[], bool] | None = None |
|
|
|
self._touch_valid_callback: Callable[[], bool] | None = None |
|
|
|
|
|
|
|
self._click_callback: Callable[[], None] | None = None |
|
|
|
self._multi_touch = False |
|
|
|
self._multi_touch = False |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
@property |
|
|
@ -40,7 +41,7 @@ class Widget(abc.ABC): |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
@property |
|
|
|
def is_pressed(self) -> bool: |
|
|
|
def is_pressed(self) -> bool: |
|
|
|
return any(self._is_pressed) |
|
|
|
return any(self.__is_pressed) |
|
|
|
|
|
|
|
|
|
|
|
@property |
|
|
|
@property |
|
|
|
def enabled(self) -> bool: |
|
|
|
def enabled(self) -> bool: |
|
|
@ -56,6 +57,10 @@ class Widget(abc.ABC): |
|
|
|
def set_visible(self, visible: bool | Callable[[], bool]) -> None: |
|
|
|
def set_visible(self, visible: bool | Callable[[], bool]) -> None: |
|
|
|
self._is_visible = visible |
|
|
|
self._is_visible = visible |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_click_callback(self, click_callback: Callable[[], None] | None) -> None: |
|
|
|
|
|
|
|
"""Set a callback to be called when the widget is clicked.""" |
|
|
|
|
|
|
|
self._click_callback = click_callback |
|
|
|
|
|
|
|
|
|
|
|
def set_touch_valid_callback(self, touch_callback: Callable[[], bool]) -> None: |
|
|
|
def set_touch_valid_callback(self, touch_callback: Callable[[], bool]) -> None: |
|
|
|
"""Set a callback to determine if the widget can be clicked.""" |
|
|
|
"""Set a callback to determine if the widget can be clicked.""" |
|
|
|
self._touch_valid_callback = touch_callback |
|
|
|
self._touch_valid_callback = touch_callback |
|
|
@ -91,28 +96,28 @@ class Widget(abc.ABC): |
|
|
|
# Allows touch to leave the rect and come back in focus if mouse did not release |
|
|
|
# Allows touch to leave the rect and come back in focus if mouse did not release |
|
|
|
if mouse_event.left_pressed and self._touch_valid(): |
|
|
|
if mouse_event.left_pressed and self._touch_valid(): |
|
|
|
if rl.check_collision_point_rec(mouse_event.pos, self._rect): |
|
|
|
if rl.check_collision_point_rec(mouse_event.pos, self._rect): |
|
|
|
self._is_pressed[mouse_event.slot] = True |
|
|
|
self.__is_pressed[mouse_event.slot] = True |
|
|
|
self._tracking_is_pressed[mouse_event.slot] = True |
|
|
|
self.__tracking_is_pressed[mouse_event.slot] = True |
|
|
|
|
|
|
|
|
|
|
|
# Callback such as scroll panel signifies user is scrolling |
|
|
|
# Callback such as scroll panel signifies user is scrolling |
|
|
|
elif not self._touch_valid(): |
|
|
|
elif not self._touch_valid(): |
|
|
|
self._is_pressed[mouse_event.slot] = False |
|
|
|
self.__is_pressed[mouse_event.slot] = False |
|
|
|
self._tracking_is_pressed[mouse_event.slot] = False |
|
|
|
self.__tracking_is_pressed[mouse_event.slot] = False |
|
|
|
|
|
|
|
|
|
|
|
elif mouse_event.left_released: |
|
|
|
elif mouse_event.left_released: |
|
|
|
if self._is_pressed[mouse_event.slot] and rl.check_collision_point_rec(mouse_event.pos, self._rect): |
|
|
|
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._handle_mouse_release(mouse_event.pos) |
|
|
|
self._is_pressed[mouse_event.slot] = False |
|
|
|
self.__is_pressed[mouse_event.slot] = False |
|
|
|
self._tracking_is_pressed[mouse_event.slot] = False |
|
|
|
self.__tracking_is_pressed[mouse_event.slot] = False |
|
|
|
|
|
|
|
|
|
|
|
# Mouse/touch is still within our rect |
|
|
|
# Mouse/touch is still within our rect |
|
|
|
elif rl.check_collision_point_rec(mouse_event.pos, self._rect): |
|
|
|
elif rl.check_collision_point_rec(mouse_event.pos, self._rect): |
|
|
|
if self._tracking_is_pressed[mouse_event.slot]: |
|
|
|
if self.__tracking_is_pressed[mouse_event.slot]: |
|
|
|
self._is_pressed[mouse_event.slot] = True |
|
|
|
self.__is_pressed[mouse_event.slot] = True |
|
|
|
|
|
|
|
|
|
|
|
# Mouse/touch left our rect but may come back into focus later |
|
|
|
# Mouse/touch left our rect but may come back into focus later |
|
|
|
elif not rl.check_collision_point_rec(mouse_event.pos, self._rect): |
|
|
|
elif not rl.check_collision_point_rec(mouse_event.pos, self._rect): |
|
|
|
self._is_pressed[mouse_event.slot] = False |
|
|
|
self.__is_pressed[mouse_event.slot] = False |
|
|
|
|
|
|
|
|
|
|
|
return ret |
|
|
|
return ret |
|
|
|
|
|
|
|
|
|
|
@ -128,6 +133,8 @@ class Widget(abc.ABC): |
|
|
|
|
|
|
|
|
|
|
|
def _handle_mouse_release(self, mouse_pos: MousePos) -> bool: |
|
|
|
def _handle_mouse_release(self, mouse_pos: MousePos) -> bool: |
|
|
|
"""Optionally handle mouse release events.""" |
|
|
|
"""Optionally handle mouse release events.""" |
|
|
|
|
|
|
|
if self._click_callback: |
|
|
|
|
|
|
|
self._click_callback() |
|
|
|
return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def show_event(self): |
|
|
|
def show_event(self): |
|
|
|