diff gamelib/gamescreen.py @ 120:48d24a48d0ce

Enter and leave hooks
author Neil Muller <neil@dip.sun.ac.za>
date Tue, 24 Aug 2010 17:24:54 +0200
parents d5f7cccfdb6c
children 2f672e98d488
line wrap: on
line diff
--- a/gamelib/gamescreen.py	Tue Aug 24 17:04:56 2010 +0200
+++ b/gamelib/gamescreen.py	Tue Aug 24 17:24:54 2010 +0200
@@ -75,25 +75,33 @@
     def draw(self, surface):
         self.state.draw(surface)
 
+    def _process_result(self, result):
+        """Helper function to do the right thing with a result object"""
+        if result:
+            if result.sound:
+                result.sound.play()
+            if result.message:
+                # Display the message as a modal dialog
+                MessageDialog(result.message, 60).present()
+                # queue a redraw to show updated state
+                self.invalidate()
+
     def mouse_down(self, event):
         if self.subwidgets:
             self.remove(self.detail)
             self.state.set_current_detail(None)
         else:
             result = self.state.interact(event.pos)
-            if result:
-                if result.sound:
-                    result.sound.play()
-                if result.message:
-                    # Display the message as a modal dialog
-                    MessageDialog(result.message, 60).present()
-                    # queue a redraw to show updated state
-                    self.invalidate()
+            self._process_result(result)
 
     def animate(self):
         if self.state.animate():
             # queue a redraw
             self.invalidate()
+        # We do this here so we can get enter and leave events regardless
+        # of what happens
+        result = self.state.check_enter_leave()
+        self._process_result(result)
 
     def mouse_move(self, event):
         if not self.subwidgets: