changeset 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 a2f6e975af67
children 11afefc4aeaf
files gamelib/cursor.py gamelib/gamescreen.py gamelib/scenes/cryo.py gamelib/state.py
diffstat 4 files changed, 29 insertions(+), 15 deletions(-) [+]
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
--- a/gamelib/gamescreen.py	Tue Aug 24 18:23:24 2010 +0200
+++ b/gamelib/gamescreen.py	Tue Aug 24 18:36:31 2010 +0200
@@ -129,7 +129,7 @@
     def start_game(self):
         self._clear_all()
         # TODO: Randomly plonk the state here for now
-        self.state = initial_state()
+        self.state = initial_state(self)
         self.state_widget = StateWidget(self.state)
         self.add(self.state_widget)
 
--- a/gamelib/scenes/cryo.py	Tue Aug 24 18:23:24 2010 +0200
+++ b/gamelib/scenes/cryo.py	Tue Aug 24 18:36:31 2010 +0200
@@ -40,6 +40,7 @@
     "Titanium leg, found on a piratical corpse."
 
     INVENTORY_IMAGE = "titanium_femur.png"
+    CURSOR = ('titanium_femur_cursor.png', 47, 3)
 
 
 class CryoUnitAlpha(Thing):
--- a/gamelib/state.py	Tue Aug 24 18:23:24 2010 +0200
+++ b/gamelib/state.py	Tue Aug 24 18:36:31 2010 +0200
@@ -9,6 +9,7 @@
 
 import constants
 from sound import get_sound
+from cursor import HAND
 
 
 class Result(object):
@@ -31,9 +32,9 @@
             scene.invalidate()
 
 
-def initial_state():
+def initial_state(screen):
     """Load the initial state."""
-    state = State()
+    state = State(screen)
     state.load_scenes("cryo")
     state.load_scenes("bridge")
     #state.load_scenes("mess")
@@ -54,7 +55,7 @@
     * scenes
     """
 
-    def __init__(self):
+    def __init__(self, screen):
         # map of scene name -> Scene object
         self.scenes = {}
         # map of detail view name -> DetailView object
@@ -75,6 +76,8 @@
         self.do_check = None
         self.old_pos = None
 
+        self.screen = screen
+
     def add_scene(self, scene):
         self.scenes[scene.name] = scene
 
@@ -117,6 +120,10 @@
 
     def set_tool(self, item):
         self.tool = item
+        if item is None:
+            self.screen.set_cursor(HAND)
+        else:
+            self.screen.set_cursor(item.CURSOR)
 
     def draw(self, surface):
         if self.do_check and self.previous_scene and self.do_check == constants.LEAVE: