Mercurial > fife-parpg
comparison engine/extensions/pychan/events.py @ 164:5b04a7d3ded6
typo fix
some missing event (de)registrations
ability to capture one event multiple times (instead of only having one slot, we have groups and each group has an own slot, the default group is "default" so we are backward compatible
author | spq@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Wed, 15 Oct 2008 14:57:04 +0000 |
parents | bb9902910067 |
children | 81a222e7bd25 |
comparison
equal
deleted
inserted
replaced
163:31718fa356f8 | 164:5b04a7d3ded6 |
---|---|
40 MOUSE_EVENT, KEY_EVENT, ACTION_EVENT = range(3) | 40 MOUSE_EVENT, KEY_EVENT, ACTION_EVENT = range(3) |
41 def getEventType(name): | 41 def getEventType(name): |
42 if "mouse" in name: | 42 if "mouse" in name: |
43 return MOUSE_EVENT | 43 return MOUSE_EVENT |
44 if "key" in name: | 44 if "key" in name: |
45 return MOUSE_EVENT | 45 return KEY_EVENT |
46 return ACTION_EVENT | 46 return ACTION_EVENT |
47 | 47 |
48 | 48 |
49 CALLBACK_NONE_MESSAGE = """\ | 49 CALLBACK_NONE_MESSAGE = """\ |
50 You passed None as parameter to %s.capture, which would normally remove a mapped event. | 50 You passed None as parameter to %s.capture, which would normally remove a mapped event. |
79 def _redirectEvent(self,name,event): | 79 def _redirectEvent(self,name,event): |
80 self.indent += 4 | 80 self.indent += 4 |
81 event = self.translateEvent(getEventType(name), event) | 81 event = self.translateEvent(getEventType(name), event) |
82 if name in self.events: | 82 if name in self.events: |
83 if self.debug: print "-"*self.indent, name | 83 if self.debug: print "-"*self.indent, name |
84 self.events[name]( event ) | 84 for f in self.events[name].itervalues(): |
85 f( event ) | |
85 self.indent -= 4 | 86 self.indent -= 4 |
86 | 87 |
87 def translateEvent(self,event_type,event): | 88 def translateEvent(self,event_type,event): |
88 if event_type == MOUSE_EVENT: | 89 if event_type == MOUSE_EVENT: |
89 return self.guimanager.translateMouseEvent(event) | 90 return self.guimanager.translateMouseEvent(event) |
141 if self.is_attached: | 142 if self.is_attached: |
142 return | 143 return |
143 if not self.listener.events: | 144 if not self.listener.events: |
144 return | 145 return |
145 if self.debug: print "Attach:",self | 146 if self.debug: print "Attach:",self |
147 self.widget.real_widget.addKeyListener( self.listener ) | |
146 self.widget.real_widget.addMouseListener( self.listener ) | 148 self.widget.real_widget.addMouseListener( self.listener ) |
147 self.widget.real_widget.addActionListener( self.listener ) | 149 self.widget.real_widget.addActionListener( self.listener ) |
148 self.is_attached = True | 150 self.is_attached = True |
149 | 151 |
150 def detach(self): | 152 def detach(self): |
154 """ | 156 """ |
155 | 157 |
156 if not self.is_attached: | 158 if not self.is_attached: |
157 return | 159 return |
158 if self.debug: print "Detach:",self | 160 if self.debug: print "Detach:",self |
161 self.widget.real_widget.removeKeyListener( self.listener ) | |
159 self.widget.real_widget.removeMouseListener( self.listener ) | 162 self.widget.real_widget.removeMouseListener( self.listener ) |
160 self.widget.real_widget.removeActionListener( self.listener ) | 163 self.widget.real_widget.removeActionListener( self.listener ) |
161 self.is_attached = False | 164 self.is_attached = False |
162 | 165 |
163 def capture(self,event_name,callback): | 166 def capture(self,event_name,callback,group_name): |
164 if event_name not in EVENTS: | 167 if event_name not in EVENTS: |
165 raise exceptions.RuntimeError("Unknown eventname: " + event_name) | 168 raise exceptions.RuntimeError("Unknown eventname: " + event_name) |
166 | 169 |
167 if callback is None and not self.isCaptured(event_name): | |
168 if self.debug: | |
169 print CALLBACK_NONE_MESSAGE % str(self.widget) | |
170 return | |
171 | |
172 if callback is None: | 170 if callback is None: |
173 del self.listener.events[event_name] | 171 if self.isCaptured(event_name): |
174 if not self.listener.events: | 172 del self.listener.events[event_name][group_name] |
175 self.detach() | 173 if not self.listener.events: |
174 self.detach() | |
175 elif self.debug: | |
176 print CALLBACK_NONE_MESSAGE % str(self.widget) | |
176 return | 177 return |
177 | 178 |
178 if not callable(callback): | 179 if not callable(callback): |
179 raise RuntimeError("An event callback must be either a callable or None - not %s" % repr(callback)) | 180 raise RuntimeError("An event callback must be either a callable or None - not %s" % repr(callback)) |
180 | 181 |
181 def captured_f(event): | 182 def captured_f(event): |
182 tools.applyOnlySuitable(callback,event=event,widget=self.widget) | 183 tools.applyOnlySuitable(callback,event=event,widget=self.widget) |
183 | 184 |
184 self.listener.events[event_name] = captured_f | 185 if event_name not in self.listener.events: |
186 self.listener.events[event_name] = {group_name : captured_f} | |
187 else: | |
188 self.listener.events[event_name][group_name] = captured_f | |
185 self.attach() | 189 self.attach() |
186 | 190 |
187 def isCaptured(self,event_name): | 191 def isCaptured(self,event_name,group_name): |
188 return event_name in self.listener.events | 192 return event_name in self.listener.events and group_name in self.listener.events[event_name] |
189 | 193 |
190 def getCapturedEvents(self): | 194 def getCapturedEvents(self): |
191 return self.listener.events.keys() | 195 return self.listener.events.keys() |
192 | 196 |
193 | 197 |
194 def splitEventDescriptor(name): | 198 def splitEventDescriptor(name): |
195 """ Utility function to split "widgetName/eventName" descriptions into tuples. """ | 199 """ Utility function to split "widgetName/eventName" descriptions into tuples. """ |
196 L = name.split("/") | 200 L = name.split("/") |
201 if len(L) not in (1,2,3): | |
202 raise exceptions.RuntimeError("Invalid widgetname / eventname combination: " + name) | |
197 if len(L) == 1: | 203 if len(L) == 1: |
198 L = L[0],"action" | 204 L = L[0],"action" |
199 if len(L) != 2: | 205 elif L[1] not in EVENTS: |
200 raise exceptions.RuntimeError("Invalid widgetname / eventname combination: " + name) | |
201 if L[1] not in EVENTS: | |
202 raise exceptions.RuntimeError("Unknown event name: " + name) | 206 raise exceptions.RuntimeError("Unknown event name: " + name) |
207 if len(L) == 2: | |
208 L = L[0],L[1],"default" | |
203 return L | 209 return L |
204 | 210 |