From b6f24c356e8efcebb68e224329e0bc135ace6e16 Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 8 Sep 2016 20:24:43 +0200 Subject: [PATCH] Hack in support for tool cursors --- data/images/cursors/seed.png | Bin 0 -> 899 bytes tabakrolletjie/cursor.py | 29 +++++++++++++++++++++++++++++ tabakrolletjie/scenes/base.py | 19 +++++++++++++++++++ tabakrolletjie/scenes/day.py | 5 +++++ 4 files changed, 53 insertions(+) create mode 100644 data/images/cursors/seed.png create mode 100644 tabakrolletjie/cursor.py diff --git a/data/images/cursors/seed.png b/data/images/cursors/seed.png new file mode 100644 index 0000000000000000000000000000000000000000..2f11b2299fc535a1aa61b07cfe12e364e703683c GIT binary patch literal 899 zcmV-}1AP36P)gn#9o|ztC7{_3U5J1F7Ofv!N<1A=Z8z2nz1mlcu#%&`yGCI(&tm4F%AJU2PU|7 zqRi=Oi~PnwF>fmKm9jcv8Av3aAJP7|pFHWhayhIJ(k@s^(K0h>Mow4St#zx>leQc2{gBACq>9CtZ&r9` z`-PFpWcfL=B^pVzb7vk^%m`Q<@XJXmQ6kqqnTxP2ML zf_-@wa&U4mKGXtJ=gjLT25W%BBj9-8k zL!_`YQa~r*+kaeY;})>eW3}sV&4!J-$D$bdj%^(hXnNcu&Nj$z z*Bp->>JPhIY6X0i#Tn&yuNQN(9r8n?&y*8m8Y+|4h)b&F6aL;}ak$|MChdn%h Z{0CRm8cf)PRU!ZY002ovPDHLkV1k4OqoM!+ literal 0 HcmV?d00001 diff --git a/tabakrolletjie/cursor.py b/tabakrolletjie/cursor.py new file mode 100644 index 0000000..69b57a3 --- /dev/null +++ b/tabakrolletjie/cursor.py @@ -0,0 +1,29 @@ + +import pygame.mouse +import pygame.sprite + +from .loader import loader + + +class Cursor(pygame.sprite.Sprite): + + def __init__(self): + super(Cursor, self).__init__() + self._active = False + + def activate(self, name, group): + self.image = loader.load_image('cursors', name + '.png') + self.rect = self.image.get_rect() + self._active = True + group.empty() + group.add(self) + pygame.mouse.set_visible(0) + + def deactivate(self, group): + self._active = False + pygame.mouse.set_visible(1) + group.empty() + + def update(self): + if self._active: + self.rect.center = pygame.mouse.get_pos() diff --git a/tabakrolletjie/scenes/base.py b/tabakrolletjie/scenes/base.py index 71f7d6c..320ff80 100644 --- a/tabakrolletjie/scenes/base.py +++ b/tabakrolletjie/scenes/base.py @@ -1,7 +1,26 @@ """ Base scene class. """ +from pygame.sprite import RenderUpdates +from ..cursor import Cursor + class BaseScene(object): + def __init__(self): + self._cursor = Cursor() + self._cursor_group = RenderUpdates() + + def _set_cursor(self, name): + self._cursor.activate(name, self._cursor_group) + + def _unset_cursor(self): + self._cursor.deactivate(self._cursor_group) + + def _draw_cursor(self, surface): + """Draw the cursor. Should be called at the end of the render + method by scenes that need it.""" + self._cursor_group.update() + self._cursor_group.draw(surface) + def enter(self, gamestate): """ Enter the scene. """ diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index 44d3529..e3c5f72 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -26,6 +26,9 @@ class DayScene(BaseScene): pos=(50, SCREEN_SIZE[1] - 40)), ] + def exit(self, gamestate): + self._unset_cursor() + @debug_timer("day.render") def render(self, surface, gamestate): surface.fill((0, 0, 155)) @@ -34,6 +37,7 @@ class DayScene(BaseScene): self._lights.render_fittings(surface) for tool in self._tools: tool.render(surface) + self._draw_cursor(surface) def event(self, ev, gamestate): if ev.type == pgl.KEYDOWN: @@ -49,6 +53,7 @@ class DayScene(BaseScene): for tool in self._tools: if tool.pressed(ev): print 'tool', tool.name + self._set_cursor(tool.name) return # Not tool, so check lights self._lights.toggle_nearest(ev.pos, surfpos=True) -- 2.34.1