Initial commit from version 1e-99
[beatscape.git] / event.py
1 import pygame
2 from pygame.locals import *
3 import config
4
5 LEFT = 0
6 RIGHT = 1
7 UP = 2
8 DOWN = 3
9 OK = 4
10 CANCEL = 5
11 OPTION = 6
12
13 keymap = {K_UP: UP, K_DOWN: DOWN, K_LEFT: LEFT, K_RIGHT: RIGHT, K_RETURN: OK, K_ESCAPE: CANCEL}
14 jsamap = {(0,-1.0): OK, (1,-1.0): DOWN, (1,1.0): UP }
15 jsbmap = {3: OK, 6: CANCEL, 2: OK, 7: CANCEL, 1: OK, 4: CANCEL, 10: OPTION, 9: OK}
16
17 def parseevent(e):
18         try: 
19                 if e.type == KEYDOWN:
20                         return keymap[e.key]
21                 elif e.type == JOYBUTTONDOWN:
22                         return jsbmap[e.button]
23                 elif e.type == JOYAXISMOTION:
24                         if e.value != 0.0:
25                                 return jsamap[(e.axis,e.value)]
26                 else:
27                         return None
28         except KeyError:
29                 return None
30
31 last_action = None
32 last_action_time = 0
33
34 def getaction():
35         global last_action, last_action_time
36
37         e = pygame.event.poll()
38         if e.type != NOEVENT:
39                 last_action = parseevent(e)
40                 if last_action:
41                         last_action_time = pygame.time.get_ticks()
42                 return last_action
43         else:
44                 if pygame.time.get_ticks() - last_action_time > config.keytimeout:
45                         last_action_time = pygame.time.get_ticks()
46                         # Only repeat for directionals
47                         if last_action in (LEFT,RIGHT,UP,DOWN):
48                                 return last_action
49         return None