22
22
23
23
24
24
class EventDrivenFeedback (Feedback ):
25
- """
26
- Example Feedback:
25
+ """Event Driven Feedback Base Class.
27
26
28
- #!/usr/bin/env python
29
27
30
- import pygame
28
+ Example Feedback::
31
29
32
- from FeedbackBase.EventDrivenFeedback import EventDrivenFeedback
30
+ #!/usr/bin/env python
33
31
34
- class TestEventDrivenFeedback(EventDrivenFeedback):
32
+ import pygame
35
33
34
+ from FeedbackBase.EventDrivenFeedback import EventDrivenFeedback
36
35
37
- def run(self):
38
- pygame.init()
39
- pygame.display.set_mode([800, 600], pygame.HWSURFACE)
40
- # Glue pygame events to ours
41
- self.bind_event_to_method(pygame.VIDEOEXPOSE, self.paint)
42
- self.bind_event_to_method(pygame.KEYDOWN, self.keyboard)
43
- self.bind_event_to_method(pygame.MOUSEBUTTONDOWN, self.mouse)
44
- # Let's start the show
45
- print "executing paint"
46
- self.execute([self.paint], 10.0)
47
- print "executing mouse, keyboard, paint"
48
- self.execute([self.mouse, self.keyboard, self.paint], 10.0)
49
- pygame.quit()
36
+ class TestEventDrivenFeedback(EventDrivenFeedback):
50
37
51
38
52
- def tick(self):
53
- # Poll pygames event queue and dispatch our events
54
- for event in pygame.event.get():
55
- self.dispatch_event(event.type)
39
+ def run(self):
40
+ pygame.init()
41
+ pygame.display.set_mode([800, 600], pygame.HWSURFACE)
42
+ # Glue pygame events to ours
43
+ self.bind_event_to_method(pygame.VIDEOEXPOSE, self.paint)
44
+ self.bind_event_to_method(pygame.KEYDOWN, self.keyboard)
45
+ self.bind_event_to_method(pygame.MOUSEBUTTONDOWN, self.mouse)
46
+ # Let's start the show
47
+ print "executing paint"
48
+ self.execute([self.paint], 10.0)
49
+ print "executing mouse, keyboard, paint"
50
+ self.execute([self.mouse, self.keyboard, self.paint], 10.0)
51
+ pygame.quit()
52
+
56
53
54
+ def tick(self):
55
+ # Poll pygames event queue and dispatch our events
56
+ for event in pygame.event.get():
57
+ self.dispatch_event(event.type)
57
58
58
- #########################################################################
59
- # Our event handlers
60
- #########################################################################
61
- def paint(self):
62
- print "print"
63
59
64
- def mouse(self):
65
- print "mouse"
60
+ #########################################################################
61
+ # Our event handlers
62
+ #########################################################################
63
+ def paint(self):
64
+ print "print"
66
65
67
- def keyboard(self):
68
- print "keyboard"
69
- """
66
+ def mouse(self):
67
+ print "mouse"
68
+
69
+ def keyboard(self):
70
+ print "keyboard"
71
+ """
70
72
71
73
def on_init (self ):
72
74
self .event_method_mapping = dict ()
@@ -78,20 +80,32 @@ def on_play(self):
78
80
79
81
80
82
def run (self ):
81
- # Your script goes here.
82
- # You should populate this method with execute calls.
83
+ """Start the Feedback activity.
84
+
85
+ Overwrite this method and put your script here. You should populate
86
+ this method with :func:`execute` calls.
87
+ """
83
88
pass
84
89
85
90
86
91
def tick (self ):
87
- # Your code to get events and dispatch events goes here.
88
- # You should poll the event queue of your toolkit/library/etc and
89
- # translate their events to yours.
92
+ """
93
+ Your code to get events and dispatch events goes here. You should poll
94
+ the event queue of your toolkit/library/etc and translate their events
95
+ to yours.
96
+ """
90
97
pass
91
98
92
99
93
100
def execute (self , methodlist , runtime ):
94
- """Enable given methods for a certain time."""
101
+ """Enable given methods for a certain time.
102
+
103
+ :param methodlist: Callables
104
+ :type methodlist: list
105
+ :param runtime: Fractions of Secons
106
+ :type runtime: float
107
+
108
+ """
95
109
self .enabled_methods = methodlist
96
110
elapsed_time = 0.0
97
111
t_start = time .time ()
@@ -101,14 +115,24 @@ def execute(self, methodlist, runtime):
101
115
102
116
103
117
def dispatch_event (self , event ):
104
- """Dispatch the event and run the associated method if enabled."""
118
+ """Dispatch the event and run the associated method if enabled.
119
+
120
+ :param event: Event
121
+
122
+ """
105
123
method = self .event_method_mapping .get (event )
106
124
if method and method in self .enabled_methods :
107
125
# Call the event method
108
126
method ()
109
127
110
128
111
129
def bind_event_to_method (self , event , method ):
112
- """Bind event to methodcall."""
130
+ """Bind event to methodcall.
131
+
132
+ :param event: An event
133
+ :param method: The method which should be called when ``event`` was received.
134
+ :type method: callable
135
+
136
+ """
113
137
self .event_method_mapping [event ] = method
114
138
0 commit comments