From: Jeremy Thurgood Date: Tue, 13 May 2014 20:21:19 +0000 (+0200) Subject: Better event handling. X-Git-Tag: 0.1~323 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=c8c4a15980bf4b49dd307e83d5e2b10a1fe7b28c;p=naja.git Better event handling. --- diff --git a/naja/events.py b/naja/events.py index 11adab0..1586726 100644 --- a/naja/events.py +++ b/naja/events.py @@ -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 diff --git a/naja/widgets/info_area.py b/naja/widgets/info_area.py index a2956a0..ad31e68 100644 --- a/naja/widgets/info_area.py +++ b/naja/widgets/info_area.py @@ -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) diff --git a/naja/widgets/robot.py b/naja/widgets/robot.py index 60c3f40..75082d0 100644 --- a/naja/widgets/robot.py +++ b/naja/widgets/robot.py @@ -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)