]> git.mar77i.info Git - zenbook_gui/commitdiff
split up ui/base.py
authormar77i <mar77i@protonmail.ch>
Tue, 11 Feb 2025 08:25:57 +0000 (09:25 +0100)
committermar77i <mar77i@protonmail.ch>
Tue, 11 Feb 2025 08:25:57 +0000 (09:25 +0100)
19 files changed:
ui/__init__.py
ui/base.py [deleted file]
ui/button.py
ui/child.py [new file with mode: 0644]
ui/drop_down.py
ui/event_method_dispatcher.py [new file with mode: 0644]
ui/fps_widget.py
ui/icon.py
ui/label.py
ui/modal.py
ui/parent.py [new file with mode: 0644]
ui/rect.py
ui/root.py [new file with mode: 0644]
ui/scroll.py
ui/slider.py
ui/spinner.py
ui/switch.py
ui/tab_bar.py
ui/text_input.py

index affe335fe41d78eac9e82764141b5ed95ec9693d..ec510bade3bd52620a7e0356892c2cb320b4faaa 100644 (file)
@@ -1,12 +1,15 @@
-from .base import Child, Parent, Root
 from .button import Button
+from .child import Child
 from .drop_down import DropDown
+from .event_method_dispatcher import EventMethodDispatcher
 from .fps_widget import FPSWidget
 from .icon import Icon
 from .icon_button import IconButton
 from .label import Label
 from .message_box import MessageBox
 from .modal import Modal
+from .parent import Parent
+from .root import Root
 from .scroll import Scroll
 from .slider import Slider
 from .spinner import Spinner
diff --git a/ui/base.py b/ui/base.py
deleted file mode 100644 (file)
index 3eb6a3d..0000000
+++ /dev/null
@@ -1,170 +0,0 @@
-from functools import cached_property, partial
-
-import pygame
-
-from .focus import FocusRootMixin
-
-
-class EventMethodDispatcher:
-    MODS = (pygame.KMOD_CTRL, pygame.KMOD_ALT, pygame.KMOD_META, pygame.KMOD_SHIFT)
-    KEY_METHODS = {}
-
-    def get_key_method(self, key, mod):
-        mods = set()
-        for mask in self.MODS:
-            if mod & mask:
-                mods.add(mask)
-        method = self.KEY_METHODS.get(frozenset(mods), {}).get(key)
-        if method is not None:
-            return partial(method, self)
-        return None
-
-    def handle_keydown(self, ev):
-        if not self.KEY_METHODS:
-            return
-        key_method = self.get_key_method(ev.key, ev.mod)
-        if key_method is not None:
-            key_method()
-
-    def handle_event(self, ev):
-        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
-
-
-class Parent(EventMethodDispatcher):
-    root: "Root"
-    running: bool
-    stop_event: bool
-
-    def __init__(self, *args, **kwargs):
-        super().__init__(*args, **kwargs)
-        self.children = []
-
-    def handle_event(self, ev):
-        super().handle_event(ev)
-        if not self.running or self.stop_event:
-            return
-        for child in self.children:
-            child.handle_event(ev)
-            if not self.running or self.stop_event:
-                break
-
-    def update(self):
-        super().update()
-        for child in self.children:
-            child.update()
-
-    def draw(self):
-        super().draw()
-        for child in self.children:
-            child.draw()
-
-
-class Child(EventMethodDispatcher):
-    def __init__(self, parent):
-        self._parent = parent
-        if parent is not None:
-            parent.children.append(self)
-
-    @property
-    def parent(self):
-        return self._parent
-
-    @parent.setter
-    def parent(self, parent):
-        if self._parent is not None:
-            self._parent.children.remove(self)
-        self._parent = parent
-        if parent is not None:
-            parent.children.append(self)
-        if "root" in self.__dict__:
-            self.__dict__.pop("root")
-
-    @cached_property
-    def root(self):
-        parent = self.parent
-        while hasattr(parent, "parent"):
-            parent = parent.root or parent.parent
-        if not isinstance(parent, Root):
-            raise AttributeError(f"No root found for {self}")
-        return parent
-
-    @property
-    def dirty(self):
-        return self.root.dirty
-
-    @dirty.setter
-    def dirty(self, value):
-        self.root.dirty = value
-
-    @cached_property
-    def font(self):
-        return self.root.font
-
-    @property
-    def surf(self):
-        parent = self.parent
-        while not hasattr(parent, "surf"):
-            parent = parent.root or parent.parent
-        return parent.surf
-
-    @property
-    def running(self):
-        return self.root.running
-
-    @property
-    def stop_event(self):
-        return self.root.stop_event
-
-
-class Root(FocusRootMixin, Parent):
-    BACKGROUND_COLOR: pygame.Color
-
-    def __init__(self, surf, font=None):
-        super().__init__()
-        self.font = font
-        self.running = True
-        self.dirty = False
-        self.surf = surf
-        self.clock = pygame.time.Clock()
-        self.stop_event = False
-        self.root = self
-
-    def handle_quit(self, _=None):
-        self.running = False
-
-    KEY_METHODS = {frozenset(set()): {pygame.K_ESCAPE: handle_quit}}
-
-    def handle_event(self, ev):
-        if ev.type in (pygame.WINDOWEXPOSED, pygame.ACTIVEEVENT):
-            self.dirty = True
-            return
-        super().handle_event(ev)
-
-    def draw(self):
-        if hasattr(self, "BACKGROUND_COLOR"):
-            self.surf.fill(self.BACKGROUND_COLOR)
-        super().draw()
-
-    def run(self):
-        while True:
-            for ev in pygame.event.get():
-                self.stop_event = False
-                self.handle_event(ev)
-                if not self.running:
-                    break
-            if not self.running:
-                break
-            self.update()
-            if self.dirty:
-                self.draw()
-                pygame.display.update()
-                self.dirty = False
-            self.clock.tick(60)
index 3d6286f3f7e385f41f86656b995cb6c6ce9c06e6..52fe025fde3dd5aed8b1afaa8ab2e667412dd2e5 100644 (file)
@@ -1,6 +1,6 @@
 import pygame
 
-from .base import Child
+from .child import Child
 
 
 class Button(Child):
diff --git a/ui/child.py b/ui/child.py
new file mode 100644 (file)
index 0000000..efbec9a
--- /dev/null
@@ -0,0 +1,61 @@
+from functools import cached_property
+
+from .event_method_dispatcher import EventMethodDispatcher
+from .root import Root
+
+
+class Child(EventMethodDispatcher):
+    def __init__(self, parent):
+        self._parent = parent
+        if parent is not None:
+            parent.children.append(self)
+
+    @property
+    def parent(self):
+        return self._parent
+
+    @parent.setter
+    def parent(self, parent):
+        if self._parent is not None:
+            self._parent.children.remove(self)
+        self._parent = parent
+        if parent is not None:
+            parent.children.append(self)
+        if "root" in self.__dict__:
+            self.__dict__.pop("root")
+
+    @cached_property
+    def root(self):
+        parent = self.parent
+        while hasattr(parent, "parent"):
+            parent = parent.root or parent.parent
+        if not isinstance(parent, Root):
+            raise AttributeError(f"No root found for {self}")
+        return parent
+
+    @property
+    def dirty(self):
+        return self.root.dirty
+
+    @dirty.setter
+    def dirty(self, value):
+        self.root.dirty = value
+
+    @cached_property
+    def font(self):
+        return self.root.font
+
+    @property
+    def surf(self):
+        parent = self.parent
+        while not hasattr(parent, "surf"):
+            parent = parent.root or parent.parent
+        return parent.surf
+
+    @property
+    def running(self):
+        return self.root.running
+
+    @property
+    def stop_event(self):
+        return self.root.stop_event
index 2673d3b2afb67b25cfbe063268027947d74745ef..92076e8b169a43432580f69b7f83a34868869fb5 100644 (file)
@@ -2,9 +2,9 @@ from functools import partial
 
 import pygame
 
-from .base import Parent
 from .button import Button
 from .modal import Modal
+from .parent import Parent
 
 
 class DropDownMenu(Modal):
diff --git a/ui/event_method_dispatcher.py b/ui/event_method_dispatcher.py
new file mode 100644 (file)
index 0000000..8e54854
--- /dev/null
@@ -0,0 +1,36 @@
+from functools import partial
+
+import pygame
+
+
+class EventMethodDispatcher:
+    MODS = (pygame.KMOD_CTRL, pygame.KMOD_ALT, pygame.KMOD_META, pygame.KMOD_SHIFT)
+    KEY_METHODS = {}
+
+    def get_key_method(self, key, mod):
+        mods = set()
+        for mask in self.MODS:
+            if mod & mask:
+                mods.add(mask)
+        method = self.KEY_METHODS.get(frozenset(mods), {}).get(key)
+        if method is not None:
+            return partial(method, self)
+        return None
+
+    def handle_keydown(self, ev):
+        if not self.KEY_METHODS:
+            return
+        key_method = self.get_key_method(ev.key, ev.mod)
+        if key_method is not None:
+            key_method()
+
+    def handle_event(self, ev):
+        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
index d14866bd5ad1a595e0c9829bca5b082fa96028fb..25530adc014953b92461e237c4543cbe39e53779 100644 (file)
@@ -1,4 +1,4 @@
-from .base import Child
+from .child import Child
 
 
 class FPSWidget(Child):
index 9e3becb10d946fafeac2798147de05a9d6d35a02..9bcc485255b41e4bfa1e4235c64971a9a8ca688b 100644 (file)
@@ -1,4 +1,4 @@
-from .base import Child
+from .child import Child
 
 
 class Icon(Child):
index 20de04cf451e0ffe3fe03f397dd1ce9578eb7e92..275c25df17b923d236ab435c5a4d8680ed8d52c3 100644 (file)
@@ -1,4 +1,4 @@
-from .base import Child
+from .child import Child
 
 
 class Label(Child):
index e9115a5f634732366581b92fdfd0cebcc5e5c7ed..20c3b37a069dc381800439c25b45c38936687f6a 100644 (file)
@@ -1,7 +1,8 @@
 import pygame
 
-from .base import Child,Parent
+from .child import Child
 from .focus import FocusableMixin
+from .parent import Parent
 
 
 class Modal(FocusableMixin, Parent, Child):
diff --git a/ui/parent.py b/ui/parent.py
new file mode 100644 (file)
index 0000000..bfd87d2
--- /dev/null
@@ -0,0 +1,30 @@
+from .event_method_dispatcher import EventMethodDispatcher
+
+
+class Parent(EventMethodDispatcher):
+    root: "root.Root"
+    running: bool
+    stop_event: bool
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.children = []
+
+    def handle_event(self, ev):
+        super().handle_event(ev)
+        if not self.running or self.stop_event:
+            return
+        for child in self.children:
+            child.handle_event(ev)
+            if not self.running or self.stop_event:
+                break
+
+    def update(self):
+        super().update()
+        for child in self.children:
+            child.update()
+
+    def draw(self):
+        super().draw()
+        for child in self.children:
+            child.draw()
index 863eb000d0993690711627718decaff0b328c713..3959c317d4c4295ad9538c17db3d2099fd56910e 100644 (file)
@@ -1,6 +1,6 @@
 import pygame
 
-from .base import Child
+from .child import Child
 
 
 class Rect(Child):
diff --git a/ui/root.py b/ui/root.py
new file mode 100644 (file)
index 0000000..89974c6
--- /dev/null
@@ -0,0 +1,50 @@
+import pygame
+
+from .parent import Parent
+from .focus import FocusRootMixin
+
+
+class Root(FocusRootMixin, Parent):
+    BACKGROUND_COLOR: pygame.Color
+
+    def __init__(self, surf, font=None):
+        super().__init__()
+        self.font = font
+        self.running = True
+        self.dirty = False
+        self.surf = surf
+        self.clock = pygame.time.Clock()
+        self.stop_event = False
+        self.root = self
+
+    def handle_quit(self, _=None):
+        self.running = False
+
+    KEY_METHODS = {frozenset(set()): {pygame.K_ESCAPE: handle_quit}}
+
+    def handle_event(self, ev):
+        if ev.type in (pygame.WINDOWEXPOSED, pygame.ACTIVEEVENT):
+            self.dirty = True
+            return
+        super().handle_event(ev)
+
+    def draw(self):
+        if hasattr(self, "BACKGROUND_COLOR"):
+            self.surf.fill(self.BACKGROUND_COLOR)
+        super().draw()
+
+    def run(self):
+        while True:
+            for ev in pygame.event.get():
+                self.stop_event = False
+                self.handle_event(ev)
+                if not self.running:
+                    break
+            if not self.running:
+                break
+            self.update()
+            if self.dirty:
+                self.draw()
+                pygame.display.update()
+                self.dirty = False
+            self.clock.tick(60)
index 6a98cf0c0e45b52f27f083e4e8cf16ac258f679d..4053676407f2ffb1e82ba61ee4f0a587fdffea3b 100644 (file)
@@ -1,6 +1,7 @@
 import pygame
 
-from .base import Child, Parent
+from .child import Child
+from .parent import Parent
 
 
 class Scroll(Parent, Child):
index 5f1b510a59e2e795f7f1cf2aab73dac6bfc2547a..cb6494391f82107d6cd7312b013d3f2cc3ca8ab2 100644 (file)
@@ -1,6 +1,6 @@
 import pygame
 
-from .base import Child
+from .child import Child
 
 
 class Slider(Child):
index 8154829fa77f883ab7c6a86d00664729ed284253..4dbfca40b4756e3dfb5f1afd988ab4f37eb5ccee 100644 (file)
@@ -5,8 +5,9 @@ from time import time
 
 import pygame
 
-from .base import Child, Parent
+from .child import Child
 from .button import Button
+from .parent import Parent
 from .text_input import TextInput
 
 
index 14279db3bb415ea9bc2cd0b59d607d77bb5eea6a..ed2921b145301c8fe50c5520aef267f6a41c97e4 100644 (file)
@@ -4,7 +4,7 @@ import pygame
 from colorsys import hsv_to_rgb
 from time import time
 
-from .base import Child
+from .child import Child
 
 
 class EaseInOutElastic:
index 5d44e2e5da1d34908c069bf29cf8a58a0a5ff209..c1732ec55010fb2d2d5cdf67936b748fb5a969a2 100644 (file)
@@ -2,8 +2,9 @@ from functools import partial
 
 import pygame
 
-from .base import Child, Parent
 from .button import Button
+from .child import Child
+from .parent import Parent
 
 
 class TabBar(Parent, Child):
index ed2017169fe1a6a62857a8ad36ac88ac758997dc..772c621b4297f25f53f43f72d3e4d0fce8cca039 100644 (file)
@@ -7,7 +7,7 @@ from typing import Optional, Callable
 
 import pygame
 
-from .base import Child
+from .child import Child
 from .focus import FocusableMixin