changeset 135:b43599b7f8a2

Cursor highlights
author Stefano Rivera <stefano@rivera.za.net>
date Tue, 24 Aug 2010 20:33:20 +0200
parents faac82748f5a
children d264850806dc
files gamelib/cursor.py gamelib/scenes/cryo.py gamelib/state.py
diffstat 3 files changed, 33 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/cursor.py	Tue Aug 24 20:21:07 2010 +0200
+++ b/gamelib/cursor.py	Tue Aug 24 20:33:20 2010 +0200
@@ -2,30 +2,45 @@
 # Copyright Boomslang team, 2010 (see COPYING File)
 # Sprite Cursor
 
+from albow.resource import get_image
 from albow.widget import Widget
-from albow.resource import get_image
-import pygame.mouse as mouse
 from pygame.sprite import Sprite, RenderUpdates
+import pygame
+import pygame.color
 import pygame.cursors
 import pygame.mouse
 
-HAND = ('hand.png', 12, 0)
-
 class CursorSprite(Sprite):
     "A Sprite that follows the Cursor"
 
     def __init__(self, filename, x, y):
         Sprite.__init__(self)
-        self.image = get_image('items', filename)
-        self.rect = self.image.get_rect()
+        self.filename = filename
         self.pointer_x = x
         self.pointer_y = y
 
+    def load(self):
+        if not hasattr(self, 'plain_image'):
+            self.plain_image = get_image('items', self.filename)
+            self.image = self.plain_image
+            self.rect = self.image.get_rect()
+            self.highlight = pygame.Surface(self.rect.size)
+            color = pygame.color.Color(255, 100, 100, 0)
+            self.highlight.fill(color)
+
     def update(self):
-        pos = mouse.get_pos()
+        pos = pygame.mouse.get_pos()
         self.rect.left = pos[0] - self.pointer_x
         self.rect.top = pos[1] - self.pointer_y
 
+    def set_highlight(self, enable):
+        self.image = self.plain_image.copy()
+        if enable:
+            self.image.blit(self.highlight, self.highlight.get_rect(), None, pygame.BLEND_MULT)
+
+
+HAND = CursorSprite('hand.png', 12, 0)
+
 
 class CursorWidget(Widget):
     """Mix-in widget to ensure that mouse_move is propogated to parents"""
@@ -41,13 +56,15 @@
         Widget.draw_all(self, _surface)
         surface = self.get_root().surface
         if self.cursor != self._loaded_cursor:
+            self._loaded_cursor = self.cursor
             if self.cursor is None:
                 pygame.mouse.set_visible(1)
                 self._cursor_group.empty()
             else:
                 pygame.mouse.set_visible(0)
+                self.cursor.load()
                 self._cursor_group.empty()
-                self._cursor_group.add(CursorSprite(*self.cursor))
+                self._cursor_group.add(self.cursor)
         if self.cursor is not None:
             self._cursor_group.update()
             self._cursor_group.draw(surface)
@@ -57,3 +74,6 @@
 
     def set_cursor(self, cursor):
         CursorWidget.cursor = cursor
+
+    def cursor_highlight(self, enable):
+        self.cursor.set_highlight(enable)
--- a/gamelib/scenes/cryo.py	Tue Aug 24 20:21:07 2010 +0200
+++ b/gamelib/scenes/cryo.py	Tue Aug 24 20:33:20 2010 +0200
@@ -2,10 +2,11 @@
 
 import random
 
+from gamelib import speech
+from gamelib.cursor import CursorSprite
 from gamelib.state import Scene, Item, Thing, Result, \
                           InteractImage, InteractNoImage, InteractRectUnion, \
                           InteractAnimated
-from gamelib import speech
 
 
 class Cryo(Scene):
@@ -40,7 +41,7 @@
     "Titanium leg, found on a piratical corpse."
 
     INVENTORY_IMAGE = "titanium_femur.png"
-    CURSOR = ('titanium_femur_cursor.png', 47, 3)
+    CURSOR = CursorSprite('titanium_femur_cursor.png', 47, 3)
 
 
 class CryoUnitAlpha(Thing):
--- a/gamelib/state.py	Tue Aug 24 20:21:07 2010 +0200
+++ b/gamelib/state.py	Tue Aug 24 20:33:20 2010 +0200
@@ -494,11 +494,11 @@
 
     def enter(self, item):
         """Called when the cursor enters the Thing."""
-        pass
+        self.state.screen.cursor_highlight(True)
 
     def leave(self):
         """Called when the cursr leaves the Thing."""
-        pass
+        self.state.screen.cursor_highlight(False)
 
     def interact(self, item):
         if not self.is_interactive():