Hack in support for tool cursors
authorNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 18:24:43 +0000 (20:24 +0200)
committerNeil <neil@dip.sun.ac.za>
Thu, 8 Sep 2016 18:24:43 +0000 (20:24 +0200)
data/images/cursors/seed.png [new file with mode: 0644]
tabakrolletjie/cursor.py [new file with mode: 0644]
tabakrolletjie/scenes/base.py
tabakrolletjie/scenes/day.py

diff --git a/data/images/cursors/seed.png b/data/images/cursors/seed.png
new file mode 100644 (file)
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 (file)
index 0000000..69b57a3
--- /dev/null
@@ -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()
index 71f7d6ccc3d2615700e4be37c1d4a4e008d6be76..320ff8006b312abe49f96fe7af021472f9e82605 100644 (file)
@@ -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. """
 
index 44d352932f5f2c0570eaeb4bed959a2e4cde24a6..e3c5f727756303372def9c5927c8b0cfd7691f23 100644 (file)
@@ -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)