Better event handling.
authorJeremy Thurgood <firxen@gmail.com>
Tue, 13 May 2014 20:21:19 +0000 (22:21 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Tue, 13 May 2014 20:21:19 +0000 (22:21 +0200)
naja/events.py
naja/widgets/info_area.py
naja/widgets/robot.py

index 11adab0f7528cbd68e88adf32275d4ab39ff8be9..158672611d3b57e9735a716dc1b6f8f11fb5a8e3 100644 (file)
@@ -6,8 +6,15 @@ import pygame.event as pge
 import pygame.locals as pgl
 
 
-class NajaEvent(object):
+def finish_event(handled=True, events=(), skip_invalidate=False):
+    for event in events:
+        event.post()
+    if not skip_invalidate:
+        InvalidateTheWorld.post()
+    return handled
+
 
+class NajaEvent(object):
     TYPE = "UNKNOWN"
 
     @classmethod
index a2956a0d139a4d1d1cd369fed03cccddf6769228..ad31e68f9bee50974c71f661e0b58cf61fc0d957 100644 (file)
@@ -5,7 +5,7 @@ import pygame
 import pygame.locals as pgl
 
 from naja.constants import INFO_SIZE, EIGHT_BIT_SCALE, MOVE, ACT, KEYS
-from naja.events import InvalidateTheWorld, PlayerMoved
+from naja.events import PlayerMoved, finish_event
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
 
@@ -88,19 +88,16 @@ class InfoAreaWidget(Widget):
                 action = self.card.actions[self.chosen]
                 if not action.check_available(self.state.gameboard.player):
                     print "BEEP!"
-                    return True
-                action.perform_action(self.state.gameboard, self.card)
-                self.state.gameboard.change_mode()
-                InvalidateTheWorld.post()
-                return True
+                else:
+                    action.perform_action(self.state.gameboard, self.card)
+                    self.state.gameboard.change_mode()
+                return finish_event()
             if ev.key in KEYS.UP:
                 if self.chosen > 0:
                     self.chosen -= 1
-                    InvalidateTheWorld.post()
-                    return True
+                return finish_event()
             if ev.key in KEYS.DOWN:
                 if self.chosen + 1 < len(self.card.actions):
                     self.chosen += 1
-                    InvalidateTheWorld.post()
-                    return True
+                return finish_event()
         return super(InfoAreaWidget, self).handle_event(ev)
index 60c3f406ed9b8f08460b8c968e5b56d42e8eb907..75082d061395512a96bf4e5bc0a6013363027c6c 100644 (file)
@@ -3,7 +3,7 @@
 import pygame.locals as pgl
 
 from naja.constants import PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS
-from naja.events import InvalidateTheWorld, PlayerMoved
+from naja.events import PlayerMoved, finish_event
 from naja.resources import resources
 from naja.resources.mutators import EIGHT_BIT
 from naja.widgets.base import Widget
@@ -45,29 +45,20 @@ class RobotWidget(Widget):
             if ev.key in KEYS.UP:
                 if self.state.player.move(BITS.NORTH):
                     self.state.gameboard.change_mode()
-                    PlayerMoved.post()
-                    InvalidateTheWorld.post()
-                    return True
+                    return finish_event(events=[PlayerMoved])
             if ev.key in KEYS.DOWN:
                 if self.state.player.move(BITS.SOUTH):
                     self.state.gameboard.change_mode()
-                    PlayerMoved.post()
-                    InvalidateTheWorld.post()
-                    return True
+                    return finish_event(events=[PlayerMoved])
             if ev.key in KEYS.LEFT:
                 if self.state.player.move(BITS.WEST):
                     self.state.gameboard.change_mode()
-                    PlayerMoved.post()
-                    InvalidateTheWorld.post()
-                    return True
+                    return finish_event(events=[PlayerMoved])
             if ev.key in KEYS.RIGHT:
                 if self.state.player.move(BITS.EAST):
                     self.state.gameboard.change_mode()
-                    PlayerMoved.post()
-                    InvalidateTheWorld.post()
-                    return True
+                    return finish_event(events=[PlayerMoved])
             if ev.key in (pgl.K_SPACE,):
                 self.state.gameboard.change_mode()
-                InvalidateTheWorld.post()
-                return True
+                return finish_event()
         return super(RobotWidget, self).handle_event(ev)