Add TextButton and SurfActor actors.
authorSimon Cross <hodgestar@gmail.com>
Sun, 28 Feb 2016 20:25:13 +0000 (22:25 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 28 Feb 2016 20:25:13 +0000 (22:25 +0200)
koperkapel/actors/buttons.py [new file with mode: 0644]
koperkapel/actors/surf.py [new file with mode: 0644]

diff --git a/koperkapel/actors/buttons.py b/koperkapel/actors/buttons.py
new file mode 100644 (file)
index 0000000..dfe0ca7
--- /dev/null
@@ -0,0 +1,12 @@
+""" Buttons. """
+
+from pgzero.ptext import getsurf
+from .surf import SurfActor
+
+
+class TextButton(SurfActor):
+    def __init__(self, text, **kwargs):
+        super(TextButton, self).__init__(getsurf(text, **kwargs))
+
+    def text(self, *args, **kw):
+        self.surf = getsurf(*args, **kw)
diff --git a/koperkapel/actors/surf.py b/koperkapel/actors/surf.py
new file mode 100644 (file)
index 0000000..23dccc6
--- /dev/null
@@ -0,0 +1,30 @@
+""" Actor that takes a surface. """
+
+from pgzero.actor import Actor, POS_TOPLEFT, ANCHOR_CENTER
+
+
+class SurfActor(Actor):
+    def __init__(self, surf, pos=POS_TOPLEFT, anchor=ANCHOR_CENTER, **kwargs):
+        self._handle_unexpected_kwargs(kwargs)
+
+        self.surf = surf
+        # Initialise it at (0,0). We'll move it to the right place later
+        super(Actor, self).__init__((0, 0), self._surf.get_size())
+
+        self._init_position(pos, anchor, **kwargs)
+
+    def _update_pos(self):
+        p = self.pos
+        self.width, self.height = self._surf.get_size()
+        self._calc_anchor()
+        self.pos = p
+
+    @property
+    def surf(self):
+        """ The original surface displaying the actor. """
+        return self._orig_surf
+
+    @surf.setter
+    def surf(self, surf):
+        self._orig_surf = self._surf = surf
+        self._update_pos()