comparison tools/editor/scripts/events/events.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children fa1373b9fa16
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23 from fife import fife
24 import scripts
25 from fife.fife import IKeyListener, ICommandListener, IMouseListener, LayerChangeListener, MapChangeListener, ConsoleExecuter
26 from signal import Signal
27 import pdb
28
29 #--- Signals ---#
30 onPump = Signal()
31 preSave = Signal(providing_args=["mapview"])
32 postSave = Signal(providing_args=["mapview"])
33 mapAdded = Signal(providing_args=["mapview"])
34 preMapShown = Signal(providing_args=["mapview"])
35 postMapShown = Signal(providing_args=["mapview"])
36 preMapClosed = Signal(providing_args=["mapview"])
37 postMapClosed = Signal(providing_args=["mapview"])
38 onInstancesSelected = Signal(providing_args=["instances"])
39 onObjectSelected = Signal(providing_args=["object"])
40 onObjectsImported = Signal(providing_args=[])
41
42 # Signals emitted by EventListener
43 onQuit = Signal(providing_args=[])
44 keyPressed = Signal(providing_args=["event"])
45 keyReleased = Signal(providing_args=["event"])
46 mouseEntered = Signal(providing_args=["event"])
47 mouseExited = Signal(providing_args=["event"])
48 mousePressed = Signal(providing_args=["event"])
49 mouseReleased = Signal(providing_args=["event"])
50 mouseClicked = Signal(providing_args=["event"])
51 mouseWheelMovedUp = Signal(providing_args=["event"])
52 mouseWheelMovedDown = Signal(providing_args=["event"])
53 mouseMoved = Signal(providing_args=["event"])
54 mouseDragged = Signal(providing_args=["event"])
55 onLayerChanged = Signal(providing_args=["layer", "changedInstances"])
56 onInstanceCreate = Signal(providing_args=["layer", "instance"])
57 onInstanceDelete = Signal(providing_args=["layer", "instance"])
58 onMapChanged = Signal(providing_args=["map", "changedLayers"])
59 onLayerCreate = Signal(providing_args=["map", "layer"])
60 onLayerDelete = Signal(providing_args=["map", "layer"])
61 onToolsClick = Signal(providing_args=[])
62 onCommand = Signal(providing_args=["command"])
63 onConsoleCommand= Signal(providing_args=["command"])
64
65 class KeySequence(object):
66 def __init__(self):
67 self.key = None
68 self.modifiers = {"alt":False,"ctrl":False,"shift":False,"meta":False}
69 self.signal = None
70
71 class EventListener:
72 # NOTE: As FIFEdit currently covers the entire screen with widgets,
73 # FIFE doesn't receive any mouse or key events. Therefore we have to add
74 # mouse and key event tracking for the central widget
75
76 def __init__(self, engine):
77 self.engine = engine
78
79 eventmanager = self.engine.getEventManager()
80 self.keysequences = []
81
82 self.keylistener = _IKeyListener(engine)
83 self.mouselistener = _IMouseListener(engine)
84 self.mapchangelistener = _MapChangeListener(engine)
85 self.layerchangelistener = _LayerChangeListener(engine)
86 self.commandlistener = _ICommandListener(engine)
87 self.consoleexecuter = _ConsoleExecuter(engine)
88
89 self.controlPressed = False
90 self.altPressed = False
91 self.shiftPressed = False
92 self.metaPressed = False
93
94 eventmanager.addKeyListener(self.keylistener)
95 eventmanager.addCommandListener(self.commandlistener)
96 eventmanager.addMouseListener(self.mouselistener)
97 self.engine.getGuiManager().getConsole().setConsoleExecuter(self.consoleexecuter)
98
99 keyPressed.connect(self.keyPressed)
100 keyReleased.connect(self.keyReleased)
101
102
103 def getKeySequenceSignal(self, key, modifiers=[]):
104 # Parse modifiers
105 mods = {"alt":False,"ctrl":False,"shift":False,"meta":False}
106 for m in modifiers:
107 m = m.lower()
108 if m in mods:
109 mods[m] = True
110 else:
111 print "Unknown modifier:",m
112
113 # Check if signal for keysequence has been created
114 for k in self.keysequences:
115 if k.key == key and k.modifiers == mods:
116 return k.signal
117
118 # Create keysequence and signal
119 keysequence = KeySequence()
120 keysequence.key = key
121 keysequence.modifiers = mods
122 keysequence.signal = Signal(providing_args=["event"])
123 self.keysequences.append(keysequence)
124
125 return keysequence.signal
126
127 # IKeyListener
128 def keyPressed(self, event):
129 keyval = event.getKey().getValue()
130 keystr = event.getKey().getAsString().lower()
131
132 self.controlPressed = event.isControlPressed()
133 self.altPressed = event.isAltPressed()
134 self.shiftPressed = event.isShiftPressed()
135 self.metaPressed = event.isMetaPressed()
136
137 if keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
138 self.controlPressed = True
139 elif keyval in (fife.Key.LEFT_SHIFT, fife.Key.RIGHT_SHIFT):
140 self.shiftPressed = True
141 elif keyval in (fife.Key.LEFT_ALT, fife.Key.RIGHT_ALT):
142 self.altPressed = True
143 elif keyval in (fife.Key.RIGHT_META, fife.Key.LEFT_META):
144 self.metaPressed = True
145
146 elif keyval == fife.Key.ESCAPE:
147 scripts.editor.getEditor().quit()
148 elif keyval == fife.Key.F10:
149 self.engine.getGuiManager().getConsole().toggleShowHide()
150 elif keystr == "d":
151 pdb.set_trace()
152
153 # Check keysequences
154 for k in self.keysequences:
155 if k.modifiers["alt"] != self.altPressed: continue
156 if k.modifiers["ctrl"] != self.controlPressed: continue
157 if k.modifiers["shift"] != self.shiftPressed: continue
158 if k.modifiers["meta"] != self.metaPressed: continue
159 if keyval != k.key: continue
160 k.signal.send(sender=self, event=event)
161
162 def keyReleased(self, event):
163 keyval = event.getKey().getValue()
164
165 self.controlPressed = event.isControlPressed()
166 self.altPressed = event.isAltPressed()
167 self.shiftPressed = event.isShiftPressed()
168 self.metaPressed = event.isMetaPressed()
169
170 if keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
171 self.controlPressed = False
172 elif keyval in (fife.Key.LEFT_SHIFT, fife.Key.RIGHT_SHIFT):
173 self.shiftPressed = False
174 elif keyval in (fife.Key.LEFT_ALT, fife.Key.RIGHT_ALT):
175 self.altPressed = False
176 elif keyval in (fife.Key.RIGHT_META, fife.Key.LEFT_META):
177 self.metaPressed = False
178
179
180 #--- Internal sublistener classes ---#
181 class _IKeyListener(IKeyListener):
182 def __init__(self, engine):
183 IKeyListener.__init__(self)
184 self.engine = engine
185
186 # IKeyListener
187 def keyPressed(self, evt):
188 keyPressed.send(sender=self.engine, event=evt)
189
190 def keyReleased(self, evt):
191 keyReleased.send(sender=self.engine, event=evt)
192
193 class _ICommandListener(ICommandListener):
194 def __init__(self, engine):
195 ICommandListener.__init__(self)
196 self.engine = engine
197
198 # ICommandListener
199 def onCommand(self, command):
200 if command.getCommandType() == fife.CMD_QUIT_GAME:
201 scripts.editor.getEditor().quit()
202 else:
203 onCommand.send(sender=self.engine, command=command)
204
205 class _IMouseListener(IMouseListener):
206 def __init__(self, engine):
207 IMouseListener.__init__(self)
208 self.engine = engine
209
210 # IMouseListener
211 def mouseEntered(self, evt):
212 mouseEntered.send(sender=self.engine, event=evt)
213
214 def mouseExited(self, evt):
215 mouseExited.send(sender=self.engine, event=evt)
216
217 def mousePressed(self, evt):
218 mousePressed.send(sender=self.engine, event=evt)
219
220 def mouseReleased(self, evt):
221 mouseReleased.send(sender=self.engine, event=evt)
222
223 def mouseClicked(self, evt):
224 mouseClicked.send(sender=self.engine, event=evt)
225
226 def mouseWheelMovedUp(self, evt):
227 mouseWheelMovedUp.send(sender=self.engine, event=evt)
228
229 def mouseWheelMovedDown(self, evt):
230 mouseWheelMovedDown.send(sender=self.engine, event=evt)
231
232 def mouseMoved(self, evt):
233 mouseMoved.send(sender=self.engine, event=evt)
234
235 def mouseDragged(self, evt):
236 mouseDragged.send(sender=self.engine, event=evt)
237
238 class _MapChangeListener(MapChangeListener):
239 def __init__(self, engine):
240 MapChangeListener.__init__(self)
241 self.engine = engine
242
243 # MapChangeListener
244 def onMapChanged(self, map, changedLayers):
245 onMapChanged.send(sender=self.engine, map=map, changedLayers=changedLayers)
246
247 def onLayerCreate(self, map, layer):
248 onLayerCreate.send(sender=self.engine, map=map, layer=layer)
249
250 def onLayerDelete(self, map, layer):
251 onLayerDelete.send(sender=self.engine, map=map, layer=layer)
252
253 class _LayerChangeListener(LayerChangeListener):
254 def __init__(self, engine):
255 LayerChangeListener.__init__(self)
256 self.engine = engine
257
258 # LayerChangeListener
259 def onLayerChanged(self, layer, changedInstances):
260 onLayerChanged.send(sender=self.engine, layer=layer, changedInstances=changedInstances)
261
262 def onInstanceCreate(self, layer, instance):
263 onInstanceCreate.send(sender=self.engine, layer=layer, instance=instance)
264
265 def onInstanceDelete(self, layer, instance):
266 onInstanceDelete.send(sender=self.engine, layer=layer, instance=instance)
267
268 class _ConsoleExecuter(ConsoleExecuter):
269 def __init__(self, engine):
270 ConsoleExecuter.__init__(self)
271 self.engine = engine
272
273 # ConsoleExecuter
274 def onToolsClick(self):
275 onToolsClick.send(sender=self.engine)
276
277 def onConsoleCommand(self, command):
278 onConsoleCommand.send(sender=self.engine, command=command)
279
280