Package joy :: Module events
[hide private]
[frames] | no frames]

Source Code for Module joy.events

 1  #  This library is free software; you can redistribute it and/or 
 2  #  modify it under the terms of the GNU General Public 
 3  #  License as published by the Free Software Foundation; either 
 4  #  version 3.0 of the License, or (at your option) any later version. 
 5  # 
 6  #  The library is distributed in the hope that it will be useful, 
 7  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 8  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 9  #  General Public License for more details. 
10  # 
11  # (c) Shai Revzen, U Penn, 2010 
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   
35 -def describeEvt( evt, parseOnly = False ):
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
73 -def JoyEvent( type_code=None, **kw ):
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