Better event handling.
[naja.git] / naja / events.py
1 """
2 Custom Pygame events.
3 """
4
5 import pygame.event as pge
6 import pygame.locals as pgl
7
8
9 def finish_event(handled=True, events=(), skip_invalidate=False):
10     for event in events:
11         event.post()
12     if not skip_invalidate:
13         InvalidateTheWorld.post()
14     return handled
15
16
17 class NajaEvent(object):
18     TYPE = "UNKNOWN"
19
20     @classmethod
21     def post(cls, **attributes):
22         event = pge.Event(pgl.USEREVENT, naja_event_type=cls.TYPE,
23                           **attributes)
24         pge.post(event)
25
26     @classmethod
27     def matches(cls, ev):
28         return ev.type == pgl.USEREVENT and ev.naja_event_type == cls.TYPE
29
30
31 class SceneChangeEvent(NajaEvent):
32     TYPE = "SCENE_CHANGE"
33
34     @classmethod
35     def post(cls, scene_cls):
36         super(SceneChangeEvent, cls).post(scene_cls=scene_cls)
37
38
39 class QuitGameEvent(NajaEvent):
40     TYPE = "QUIT_GAME"
41
42
43 class InvalidateTheWorld(NajaEvent):
44     # This is used to signal to all the widgets that the world has changed
45     # and cached state may need to be recreated
46     TYPE = "INVALIDATE"
47
48
49 class SelectEvent(NajaEvent):
50     TYPE = "SELECT"
51
52
53 class PlayerMoved(NajaEvent):
54     # This is used to signal to widgets that care that the player has moved.
55     TYPE = "PLAYER_MOVED"