From: mar77i Date: Fri, 14 Feb 2025 02:06:17 +0000 (+0100) Subject: having update, draw in EventMethodDispatcher lets Parent mix with Child X-Git-Url: https://git.mar77i.info/?a=commitdiff_plain;h=4bb11f4e446afceefca6b5a25d368b2bd844b9f1;p=zenbook_gui having update, draw in EventMethodDispatcher lets Parent mix with Child --- diff --git a/bookpaint/draw_ui.py b/bookpaint/draw_ui.py index 6dd724d..d362368 100644 --- a/bookpaint/draw_ui.py +++ b/bookpaint/draw_ui.py @@ -54,8 +54,8 @@ class DrawImage(Child): class ColorButton(Button): - def __init__(self, parent, rect, color, callback, is_active=False): - super().__init__(parent, rect, None, callback, is_active) + def __init__(self, parent, rect, color, callback, highlight=False): + super().__init__(parent, rect, None, callback, highlight) self.color = color def draw(self): diff --git a/ui/child.py b/ui/child.py index 35e0a84..6d9ecfe 100644 --- a/ui/child.py +++ b/ui/child.py @@ -45,9 +45,3 @@ class Child(EventMethodDispatcher): @property def stop_event(self): return self.root.stop_event - - def update(self): - pass - - def draw(self): - pass diff --git a/ui/event_method_dispatcher.py b/ui/event_method_dispatcher.py index 2b03755..8e54854 100644 --- a/ui/event_method_dispatcher.py +++ b/ui/event_method_dispatcher.py @@ -28,3 +28,9 @@ class EventMethodDispatcher: method_name = f"handle_{pygame.event.event_name(ev.type).lower()}" if hasattr(self, method_name): getattr(self, method_name)(ev) + + def update(self): + pass + + def draw(self): + pass diff --git a/ui/modal.py b/ui/modal.py index 55808f9..a9a1f99 100644 --- a/ui/modal.py +++ b/ui/modal.py @@ -6,12 +6,9 @@ from .parent import Parent class Modal(Focusable, Parent, Child): - def __init__(self, parent): - super().__init__(parent, False) - self.draw = self._check_enabled(self.draw) - self.update = self._check_enabled(self.update) - self.handle_event = self._check_enabled(self.handle_event) - self.activate = self._check_enabled(self.activate, False) + def __init__(self, parent, enabled=False): + super().__init__(parent, enabled) + self.activate = self.activate self.deactivate = self._check_enabled(self.deactivate) def _check_enabled(self, method, cmp=True): @@ -22,12 +19,16 @@ class Modal(Focusable, Parent, Child): return inner def activate(self): + if self.enabled: + return super().activate() self.enabled = True self.root.stop_event = True self.dirty = True def deactivate(self): + if not self.enabled: + return super().deactivate() self.enabled = False self.root.stop_event = True diff --git a/ui/parent.py b/ui/parent.py index f5e5cb4..9004a6d 100644 --- a/ui/parent.py +++ b/ui/parent.py @@ -2,7 +2,6 @@ from .event_method_dispatcher import EventMethodDispatcher class Parent(EventMethodDispatcher): - root: "root.Root" running: bool stop_event: bool @@ -22,17 +21,13 @@ class Parent(EventMethodDispatcher): break def update(self): - s = super() - if hasattr(s, "update"): - s.update() + super().update() for child in self.children: if child.enabled: child.update() def draw(self): - s = super() - if hasattr(s, "draw"): - s.draw() + super().draw() for child in self.children: if child.enabled: child.draw()