diff gamelib/cursor.py @ 129:4223d66d88b4

Cursor change when you select a Tool
author Stefano Rivera <stefano@rivera.za.net>
date Tue, 24 Aug 2010 18:36:31 +0200
parents 97322b78d1c1
children b43599b7f8a2
line wrap: on
line diff
--- a/gamelib/cursor.py	Tue Aug 24 18:23:24 2010 +0200
+++ b/gamelib/cursor.py	Tue Aug 24 18:36:31 2010 +0200
@@ -9,45 +9,51 @@
 import pygame.cursors
 import pygame.mouse
 
+HAND = ('hand.png', 12, 0)
 
 class CursorSprite(Sprite):
     "A Sprite that follows the Cursor"
 
-    def __init__(self, filename):
+    def __init__(self, filename, x, y):
         Sprite.__init__(self)
-        self.image = get_image('items', filename + '.png')
+        self.image = get_image('items', filename)
         self.rect = self.image.get_rect()
+        self.pointer_x = x
+        self.pointer_y = y
 
     def update(self):
-        self.rect.midtop = mouse.get_pos()
+        pos = mouse.get_pos()
+        self.rect.left = pos[0] - self.pointer_x
+        self.rect.top = pos[1] - self.pointer_y
 
 
 class CursorWidget(Widget):
     """Mix-in widget to ensure that mouse_move is propogated to parents"""
 
+    cursor = HAND
+
     def __init__(self, *args, **kwargs):
         Widget.__init__(self, *args, **kwargs)
         self._cursor_group = RenderUpdates()
-        self._cursor_name = ''
+        self._loaded_cursor = None
 
     def draw_all(self, _surface):
         Widget.draw_all(self, _surface)
         surface = self.get_root().surface
-        cursor = self.get_sprite_cursor()
-        if cursor != self._cursor_name:
-            if self.get_sprite_cursor() is None:
+        if self.cursor != self._loaded_cursor:
+            if self.cursor is None:
                 pygame.mouse.set_visible(1)
                 self._cursor_group.empty()
             else:
                 pygame.mouse.set_visible(0)
                 self._cursor_group.empty()
-                self._cursor_group.add(CursorSprite(cursor))
-        if cursor is not None:
+                self._cursor_group.add(CursorSprite(*self.cursor))
+        if self.cursor is not None:
             self._cursor_group.update()
             self._cursor_group.draw(surface)
 
     def mouse_delta(self, event):
         self.invalidate()
 
-    def get_sprite_cursor(self):
-        return 'hand'
+    def set_cursor(self, cursor):
+        CursorWidget.cursor = cursor