Package joy ::
Module events
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """
16 joy.events defines a few utility functions for handling pygame events in the JoyApp framework.
17 Because joy extends pygame with several new event types, you should use joy.event
18 functions to handle tasks such as printing human readable event descriptions --
19 the pygame builtins won't know anything about the JoyApp specific event types.
20
21 New Event Types
22 ---------------
23
24 TIMEREVENT -- timeslice event timer for Plan execution
25 CKBOTPOSITION -- a CKBot moved
26 SCRATCHUPDATE -- a Scratch variable was updated
27
28 Main Functions
29 --------------
30 describeEvt -- describe an event in a string
31 """
32 import pygix
33
34
36 """
37 Describe an event stored in a pygame EventType object.
38
39 Returns a human readable string that consists of all fields in the
40 event object. These are printed in a format that makes it easy to
41 cut and paste them into code that pattern matches events, e.g.
42
43 >>> evt = pygame.event.Event(pygame.locals.MOUSEMOTION,pos=(176, 140),rel=(0, 1),buttons=(0, 0, 0))
44 >>> print describeEvt(evt,0)
45 type==MOUSEMOTION pos==(176, 140) rel==(0, 1) buttons==(0, 0, 0)
46
47 If parseOnly is set, returns a dictionary with the extracted fields:
48
49 >>> print joy.events.describeEvt(evt,1)
50 {'buttons': (0, 0, 0), 'type': 'MouseMotion', 'pos': (176, 140), 'rel': (0, 1)}
51 """
52 assert type(evt) is pygix.EventType
53 plan = pygix.EVENT_STRUCTURE[evt.type]
54 if evt.type<pygix.USEREVENT:
55 nm = pygix.event_name(evt.type)
56 else:
57 nm = pygix.JOY_EVENT_NAMES.get(evt.type,None)
58 if nm is None:
59 nm = "EVENT_%02d" % evt.type
60 if parseOnly:
61 res = dict(type=nm, type_code=evt.type)
62 for item in plan:
63 res[item] = getattr(evt,item)
64 else:
65 detail = ["type==%s" % nm.upper()]
66 if plan:
67 for item in plan:
68 detail.append("%s==%s" %(
69 item, repr(getattr(evt,item)) ))
70 res = " ".join(detail)
71 return res
72
74 """
75 Wrapper for pygame.Event constructor, which understands the additional
76 event types unique to JoyApp
77
78 INPUT:
79 type_code -- integer -- the typecode for the event type
80
81 OUTPUT:
82 Event object, or ValueError object with error details, if failed
83 """
84 if type_code is not None:
85 et = type_code
86 else:
87 if kw.has_key('type'):
88 et = kw['type']
89 else:
90 return ValueError('Unknown event type -- pass a type_code parameter')
91 atr = {}
92 for nm in pygix.EVENT_STRUCTURE[et]:
93 if kw.has_key(nm):
94 atr[nm] = kw[nm]
95 else:
96 return ValueError('Missing property "%s"' % nm)
97 return pygix.Event( et, atr )
98