From 5fe39c23061b1e494fc914fc46b14c9390377bc8 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Sun, 28 Feb 2016 22:25:13 +0200 Subject: [PATCH] Add TextButton and SurfActor actors. --- koperkapel/actors/buttons.py | 12 ++++++++++++ koperkapel/actors/surf.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 koperkapel/actors/buttons.py create mode 100644 koperkapel/actors/surf.py diff --git a/koperkapel/actors/buttons.py b/koperkapel/actors/buttons.py new file mode 100644 index 0000000..dfe0ca7 --- /dev/null +++ b/koperkapel/actors/buttons.py @@ -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 index 0000000..23dccc6 --- /dev/null +++ b/koperkapel/actors/surf.py @@ -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() -- 2.34.1