From: Neil Date: Thu, 8 Sep 2016 18:24:43 +0000 (+0200) Subject: Hack in support for tool cursors X-Git-Tag: tabakrolletjie-v1.0.0~181 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=b6f24c356e8efcebb68e224329e0bc135ace6e16;p=tabakrolletjie.git Hack in support for tool cursors --- diff --git a/data/images/cursors/seed.png b/data/images/cursors/seed.png new file mode 100644 index 0000000..2f11b22 Binary files /dev/null and b/data/images/cursors/seed.png differ 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)