changeset 139:a5972444ad1e

Improve cursor state handling, disable highlights on modal dialogs
author Stefano Rivera <stefano@rivera.za.net>
date Tue, 24 Aug 2010 22:02:22 +0200
parents 0fe0a91c4296
children 95d882eeba12
files gamelib/cursor.py gamelib/gamescreen.py gamelib/state.py
diffstat 3 files changed, 32 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/cursor.py	Tue Aug 24 21:52:45 2010 +0200
+++ b/gamelib/cursor.py	Tue Aug 24 22:02:22 2010 +0200
@@ -27,6 +27,7 @@
             self.highlight = pygame.Surface(self.rect.size)
             color = pygame.color.Color(255, 100, 100, 0)
             self.highlight.fill(color)
+            self.highlighted = False
 
     def update(self):
         pos = pygame.mouse.get_pos()
@@ -34,9 +35,12 @@
         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)
+        if enable != self.highlighted:
+            self.highlighted = 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)
@@ -52,6 +56,12 @@
         self._cursor_group = RenderUpdates()
         self._loaded_cursor = None
 
+    def enter_screen(self):
+        pygame.mouse.set_visible(0)
+
+    def leave_screen(self):
+        pygame.mouse.set_visible(1)
+
     def draw_all(self, _surface):
         Widget.draw_all(self, _surface)
         surface = self.get_root().surface
--- a/gamelib/gamescreen.py	Tue Aug 24 21:52:45 2010 +0200
+++ b/gamelib/gamescreen.py	Tue Aug 24 22:02:22 2010 +0200
@@ -79,15 +79,19 @@
             self.state.mouse_move(event.pos)
 
     def show_message(self, message):
+        self.parent.cursor_highlight(False)
         # Display the message as a modal dialog
         MessageDialog(message, 60).present()
         # queue a redraw to show updated state
         self.invalidate()
+        # The cursor could have gone anywhere
+        self.state.current_scene.mouse_move(self.state.tool, mouse.get_pos())
 
     def show_detail(self, detail):
         w, h = self.state.set_current_detail(detail)
         self.detail.set_image_rect(Rect(0, 0, w, h))
         self.add_centered(self.detail)
+        self.parent.cursor_highlight(False)
 
 
 class DetailWindow(Widget):
@@ -171,8 +175,13 @@
         # This option does nothing, but the method needs to exist for albow
         return
 
+    def enter_screen(self):
+        CursorWidget.enter_screen(self)
+
+    def leave_screen(self):
+        CursorWidget.leave_screen(self)
+
     def main_menu_cmd(self):
-        mouse.set_visible(1)
         self.shell.show_screen(self.shell.menu_screen)
 
     def quit_cmd(self):
@@ -185,3 +194,8 @@
     def begin_frame(self):
         if self.running:
             self.state_widget.animate()
+
+    def mouse_delta(self, event):
+        CursorWidget.mouse_delta(self, event)
+        if not self.state_widget.rect.collidepoint(event.pos):
+            self.cursor_highlight(False)
--- a/gamelib/state.py	Tue Aug 24 21:52:45 2010 +0200
+++ b/gamelib/state.py	Tue Aug 24 22:02:22 2010 +0200
@@ -305,6 +305,7 @@
         """
         if self._current_thing is not None:
             if self._current_thing.contains(pos):
+                self.state.screen.cursor_highlight(True)
                 return
             else:
                 self._current_thing.leave()
@@ -317,6 +318,7 @@
                 self._current_description = self._make_description(
                     thing.get_description())
                 break
+        self.state.screen.cursor_highlight(self._current_thing is not None)
 
     def get_detail_size(self):
         return self._background.get_size()
@@ -495,11 +497,11 @@
 
     def enter(self, item):
         """Called when the cursor enters the Thing."""
-        self.state.screen.cursor_highlight(True)
+        pass
 
     def leave(self):
         """Called when the cursr leaves the Thing."""
-        self.state.screen.cursor_highlight(False)
+        pass
 
     def interact(self, item):
         if not self.is_interactive():