changeset 658:2703924c8c70 pyntnclick

Custom mouse cursors return
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 12 Feb 2012 14:25:27 +0200
parents 0df0c81a3d8b
children 363b603794f5
files pyntnclick/cursor.py pyntnclick/gamescreen.py pyntnclick/widgets/__init__.py
diffstat 3 files changed, 30 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/cursor.py	Sun Feb 12 14:04:52 2012 +0200
+++ b/pyntnclick/cursor.py	Sun Feb 12 14:25:27 2012 +0200
@@ -9,7 +9,7 @@
 import pygame.cursors
 import pygame.mouse
 
-from pyntnclick.widgets.base import Widget
+from pyntnclick.engine import Screen
 
 # XXX: Need a way to get at the constants
 from pyntnclick.constants import GameConstants
@@ -62,53 +62,39 @@
 HAND = CursorSprite('hand.png', 12, 0)
 
 
-class CursorWidget(Widget):
-    """Mix-in widget to ensure that mouse_move is propogated to parents"""
+class CursorScreen(Screen):
+    """A Screen with custom cursors"""
 
-    cursor = HAND
-    _cursor_group = RenderUpdates()
-    _loaded_cursor = None
+    def setup(self):
+        self._cursor_group = RenderUpdates()
+        self._loaded_cursor = None
+        self.set_cursor(None)
 
-    def __init__(self, screen, *args, **kwargs):
-        Widget.__init__(self, *args, **kwargs)
-        self.screen = screen
-
-    def enter_screen(self):
+    def on_enter(self):
+        super(CursorScreen, self).on_enter()
         pygame.mouse.set_visible(0)
 
-    def leave_screen(self):
+    def on_exit(self):
+        super(CursorScreen, self).on_exit()
         pygame.mouse.set_visible(1)
 
-    def draw_all(self, surface):
-        Widget.draw_all(self, surface)
-        self.draw_cursor(self.get_root().surface)
-
-    def draw_cursor(self, surface):
-        self.set_cursor(self.screen.game.tool)
-        self.cursor.set_highlight(self.cursor_highlight())
-        if self.cursor is not None:
-            self._cursor_group.update()
-            self._cursor_group.draw(surface)
-
-    def mouse_delta(self, event):
-        self.invalidate()
+    def draw(self, surface):
+        super(CursorScreen, self).draw(surface)
+        self.set_cursor(self.game.tool)
+        #XXX: self.cursor.set_highlight(self.cursor_highlight())
+        self._cursor_group.update()
+        self._cursor_group.draw(surface)
 
-    @classmethod
-    def set_cursor(cls, item):
+    def set_cursor(self, item):
         if item is None or item.CURSOR is None:
-            cls.cursor = HAND
+            cursor = HAND
         else:
-            cls.cursor = item.CURSOR
-        if cls.cursor != cls._loaded_cursor:
-            cls._loaded_cursor = cls.cursor
-            if cls.cursor is None:
-                pygame.mouse.set_visible(1)
-                cls._cursor_group.empty()
-            else:
-                pygame.mouse.set_visible(0)
-                cls.cursor.load()
-                cls._cursor_group.empty()
-                cls._cursor_group.add(cls.cursor)
+            cursor = item.CURSOR
+        if cursor != self._loaded_cursor:
+            self._loaded_cursor = cursor
+            self._loaded_cursor.load()
+            self._cursor_group.empty()
+            self._cursor_group.add(self._loaded_cursor)
 
     def cursor_highlight(self):
         if not Rect((0, 0), SCENE_SIZE).collidepoint(pygame.mouse.get_pos()):
--- a/pyntnclick/gamescreen.py	Sun Feb 12 14:04:52 2012 +0200
+++ b/pyntnclick/gamescreen.py	Sun Feb 12 14:25:27 2012 +0200
@@ -2,11 +2,11 @@
 # Copyright Boomslang team, 2010 (see COPYING File)
 # Main menu for the game
 
-from pygame import Rect, mouse, Surface
+from pygame import Rect, Surface
 from pygame.color import Color
 from pygame.locals import MOUSEBUTTONDOWN, MOUSEMOTION, KEYDOWN, K_ESCAPE
 
-from pyntnclick.cursor import CursorWidget
+from pyntnclick.cursor import CursorScreen
 from pyntnclick.engine import Screen
 from pyntnclick.state import handle_result
 from pyntnclick.widgets.base import Widget, Container
@@ -268,9 +268,10 @@
         self.inventory.unselect()
 
 
-class GameScreen(Screen):
+class GameScreen(CursorScreen):
 
     def setup(self):
+        super(GameScreen, self).setup()
         self.running = False
         self.create_initial_state = self.gd.initial_state
         self.container.add_callback(KEYDOWN, self.key_pressed)
--- a/pyntnclick/widgets/__init__.py	Sun Feb 12 14:04:52 2012 +0200
+++ b/pyntnclick/widgets/__init__.py	Sun Feb 12 14:25:27 2012 +0200
@@ -13,7 +13,6 @@
 from pygame import mouse
 
 from pyntnclick.widgets.base import Widget
-from pyntnclick.cursor import CursorWidget
 
 # XXX: Need a way to get at the constants.
 from pyntnclick.constants import GameConstants
@@ -105,7 +104,7 @@
             ], w)
 
 
-class MessageDialog(BoomLabel, CursorWidget):
+class MessageDialog(Widget):  # WAS: BoomLabel, CursorWidget):
 
     def __init__(self, screen, text, wrap_width, style=None, **kwds):
         CursorWidget.__init__(self, screen)