comparison clients/editor/scripts/editor.py @ 255:51cc05d862f2

Merged editor_rewrite branch to trunk. This contains changes that may break compatibility against existing clients. For a list of changes that may affect your client, see: http://wiki.fifengine.de/Changes_to_pychan_and_FIFE_in_editor_rewrite_branch
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 08 Jun 2009 16:00:02 +0000
parents
children 043d71a192b5
comparison
equal deleted inserted replaced
254:10b5f7f36dd4 255:51cc05d862f2
1 from basicapplication import ApplicationBase
2 import pychan
3 import fife
4 import loaders
5 import events
6 import plugin
7 from mapview import MapView
8 from events import EventListener
9 from events import *
10 from gui import ToolBar, action
11 from gui.action import Action, ActionGroup
12 from gui.filemanager import FileManager
13 from gui.mainwindow import MainWindow
14 from gui.mapeditor import MapEditor
15 from gui.menubar import Menu, MenuBar
16 from gui.error import ErrorDialog
17 from settings import Settings
18 from pychan.tools import callbackWithArguments as cbwa
19 import sys
20
21 def getEditor():
22 """ Returns the Global editor instance """
23 if Editor.editor is None:
24 Editor(None)
25 return Editor.editor
26
27 class Editor(ApplicationBase, MainWindow):
28 """ Editor sets up all subsystems and provides access to them """
29 editor = None
30
31 def __init__(self, params, *args, **kwargs):
32 Editor.editor = self
33
34 self._filemanager = None
35
36 self.params = params
37 self._eventlistener = None
38 self._pluginmanager = None
39
40 self._inited = False
41
42 self._mapview = None
43 self._mapviewList = []
44 self._mapgroup = None
45 self._mapbar = None
46 self._maparea = None
47 self._mapeditor = None
48
49 self._fileMenu = None
50 self._editMenu = None
51 self._viewMenu = None
52 self._toolsMenu = None
53 self._helpMenu = None
54
55 self._settings = None
56
57 self._helpDialog = None
58
59 ApplicationBase.__init__(self, *args, **kwargs)
60 MainWindow.__init__(self, *args, **kwargs)
61 pychan.init(self.engine, debug=False)
62
63
64 def loadSettings(self):
65 """
66 Load the settings from a python file and load them into the engine.
67 Called in the ApplicationBase constructor.
68 """
69 self._settings = Settings()
70 TDS = self._settings
71
72 glyphDft = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\\\""
73 engineSetting = self.engine.getSettings()
74 engineSetting.setDefaultFontGlyphs(TDS.get("FIFE", "FontGlyphs", glyphDft))
75 engineSetting.setDefaultFontPath(TDS.get("FIFE", "Font", "fonts/FreeSans.ttf"))
76 engineSetting.setDefaultFontSize(12)
77 engineSetting.setBitsPerPixel(TDS.get("FIFE", "BitsPerPixel", 0))
78 engineSetting.setInitialVolume(TDS.get("FIFE", "InitialVolume", 5.0))
79 engineSetting.setSDLRemoveFakeAlpha(TDS.get("FIFE", "SDLRemoveFakeAlpha", 1))
80 engineSetting.setScreenWidth(TDS.get("FIFE", "ScreenWidth", 1024))
81 engineSetting.setScreenHeight(TDS.get("FIFE", "ScreenHeight", 768))
82 engineSetting.setRenderBackend(TDS.get("FIFE", "RenderBackend", "OpenGL"))
83 engineSetting.setFullScreen(TDS.get("FIFE", "FullScreen", 0))
84
85 engineSetting.setWindowTitle(TDS.get("FIFE", "WindowTitle", "No window title set"))
86 engineSetting.setWindowIcon(TDS.get("FIFE", "WindowIcon", ""))
87 engineSetting.setImageChunkingSize(TDS.get("FIFE", "ImageChunkSize", 256))
88
89 def initLogging(self):
90 """
91 Initialize the LogManager.
92 """
93 import fifelog
94
95 LogModules = self._settings.get("FIFE", "LogModules")
96 self.log = fifelog.LogManager(self.engine, self._settings.get("FIFE", "LogToPrompt"), self._settings.get("FIFE", "LogToFile"))
97 if LogModules:
98 self.log.setVisibleModules(*LogModules)
99
100 def _initTools(self):
101 """ Initializes tools """
102 self._pluginmanager = plugin.PluginManager()
103
104 self._filemanager = FileManager()
105 self._toolbar.adaptLayout()
106 self._mapeditor = MapEditor()
107
108 def _initGui(self):
109 """ Sets up the GUI """
110 screen_width = self.engine.getSettings().getScreenWidth()
111 screen_height = self.engine.getSettings().getScreenHeight()
112 MainWindow.initGui(self, screen_width, screen_height)
113
114 self._toolbox = ToolBar(title=u"", orientation=1)
115 self._toolbox.position_technique = "explicit"
116 self._toolbox.position = (150, 150)
117
118 self._mapbar = ToolBar(panel_size=20)
119 self._mapbar.setDocked(True)
120
121 self._maparea = pychan.widgets.VBox()
122 self._maparea.opaque = False
123 self._maparea.is_focusable = True
124
125 # Capture mouse and key events for EventListener
126 cw = self._maparea
127 cw.capture(self.__sendMouseEvent, "mouseEntered")
128 cw.capture(self.__sendMouseEvent, "mouseExited")
129 cw.capture(self.__sendMouseEvent, "mousePressed")
130 cw.capture(self.__sendMouseEvent, "mouseReleased")
131 cw.capture(self.__sendMouseEvent, "mouseClicked")
132 cw.capture(self.__sendMouseEvent, "mouseMoved")
133 cw.capture(self.__sendMouseEvent, "mouseWheelMovedUp")
134 cw.capture(self.__sendMouseEvent, "mouseWheelMovedDown")
135 cw.capture(self.__sendMouseEvent, "mouseDragged")
136 cw.capture(self.__sendKeyEvent, "keyPressed")
137 cw.capture(self.__sendKeyEvent, "keyReleased")
138
139 self._centralwidget.addChild(self._mapbar)
140 self._centralwidget.addChild(self._maparea)
141
142 self._initActions()
143
144 self._toolbox.show()
145
146 def _initActions(self):
147 """ Initializes toolbar and menubar buttons """
148 exitAction = Action(u"Exit", "gui/icons/quit.png")
149 exitAction.helptext = u"Exit program"
150 action.activated.connect(self.quit, sender=exitAction)
151
152 self._fileMenu = Menu(name=u"File")
153 self._fileMenu.addAction(exitAction)
154
155 self._editMenu = Menu(name=u"Edit")
156 self._viewMenu = Menu(name=u"View")
157 self._toolsMenu = Menu(name=u"Tools")
158 self._windowMenu = Menu(name=u"Window")
159 self._helpMenu = Menu(name=u"Help")
160
161 self._actionShowStatusbar = Action(u"Statusbar")
162 self._actionShowStatusbar.helptext = u"Toggle statusbar"
163 action.activated.connect(self.toggleStatusbar, sender=self._actionShowStatusbar)
164
165 self._actionShowToolbar = Action(u"Toolbar")
166 self._actionShowToolbar.helptext = u"Toggle toolbar"
167 action.activated.connect(self.toggleToolbar, sender=self._actionShowToolbar)
168
169 self._actionShowToolbox = Action(u"Tool box")
170 self._actionShowToolbox.helptext = u"Toggle tool box"
171 action.activated.connect(self.toggleToolbox, sender=self._actionShowToolbox)
172
173 self._viewMenu.addAction(self._actionShowStatusbar)
174 self._viewMenu.addAction(self._actionShowToolbar)
175 self._viewMenu.addAction(self._actionShowToolbox)
176 self._viewMenu.addSeparator()
177
178
179 testAction1 = Action(u"Cycle buttonstyles", "gui/icons/cycle_styles.png")
180 testAction1.helptext = u"Cycles button styles. There are currently four button styles."
181 action.activated.connect(self._actionActivated, sender=testAction1)
182 self._viewMenu.addAction(testAction1)
183
184 self._mapgroup = ActionGroup(exclusive=True, name="Mapgroup")
185 self._mapbar.addAction(self._mapgroup)
186 self._mapbar.addAction(ActionGroup(exclusive=True, name="Mapgroup2"))
187 self._windowMenu.addAction(self._mapgroup)
188
189 helpAction = Action(u"Help", "gui/icons/help.png")
190 action.activated.connect(self._showHelpDialog, sender=helpAction)
191 self._helpMenu.addAction(helpAction)
192
193 self._menubar.addMenu(self._fileMenu)
194 self._menubar.addMenu(self._editMenu)
195 self._menubar.addMenu(self._viewMenu)
196 self._menubar.addMenu(self._toolsMenu)
197 self._menubar.addMenu(self._windowMenu)
198 self._menubar.addMenu(self._helpMenu)
199
200 def _actionActivated(self, sender):
201 self._toolbar.button_style += 1
202
203 def _showHelpDialog(self, sender):
204 """ Shows the help dialog """
205 if self._helpDialog is not None:
206 self._helpDialog.show()
207 return
208
209 self._helpDialog = pychan.loadXML("gui/help.xml")
210 self._helpDialog.findChild(name="closeButton").capture(self._helpDialog.hide)
211
212 f = open('lang/infotext.txt', 'r')
213 self._helpDialog.findChild(name="helpText").text = unicode(f.read())
214 f.close()
215
216 self._helpDialog.show()
217
218 def toggleStatusbar(self):
219 """ Toggles status bar """
220 statusbar = self.getStatusBar()
221 if statusbar.max_size[1] > 0:
222 statusbar.min_size=(statusbar.min_size[0], 0)
223 statusbar.max_size=(statusbar.max_size[0], 0)
224 self._actionShowStatusbar.setChecked(False)
225 else:
226 statusbar.min_size=(statusbar.min_size[0], statusbar.min_size[0])
227 statusbar.max_size=(statusbar.max_size[0], statusbar.max_size[0])
228 self._actionShowStatusbar.setChecked(True)
229 statusbar.adaptLayout()
230
231 def toggleToolbar(self):
232 """ Toggles toolbar """
233 toolbar = self.getToolBar()
234 if toolbar.isVisible():
235 toolbar.setDocked(False)
236 toolbar.hide()
237 self._actionShowToolbar.setChecked(False)
238 else:
239 tx = toolbar.x
240 ty = toolbar.y
241 toolbar.show()
242 toolbar.x = tx
243 toolbar.y = ty
244 self._actionShowToolbar.setChecked(True)
245
246 def toggleToolbox(self):
247 """ Toggles tool box """
248 toolbox = self.getToolbox()
249 if toolbox.isVisible():
250 toolbox.setDocked(False)
251 toolbox.hide()
252 self._actionShowToolbox.setChecked(False)
253 else:
254 tx = toolbox.x
255 ty = toolbox.y
256 toolbox.show()
257 toolbox.x = tx
258 toolbox.y = ty
259 self._actionShowToolbox.setChecked(True)
260 toolbox.adaptLayout()
261
262 def getToolbox(self):
263 return self._toolbox
264
265 def getPluginManager(self):
266 return self._pluginmanager
267
268 def getEngine(self):
269 return self.engine
270
271 def getMapViews(self):
272 return self._mapviewList
273
274 def getActiveMapView(self):
275 return self._mapview
276
277 def getSettings(self):
278 return self._settings;
279
280 def showMapView(self, mapview):
281 """ Switches to mapview. """
282 if mapview is None or mapview == self._mapview:
283 return
284
285 events.preMapShown.send(sender=self, mapview=mapview)
286 self._mapview = mapview
287 self._mapview.show()
288 events.postMapShown.send(sender=self, mapview=mapview)
289
290 def createListener(self):
291 """ Creates the event listener. This is called by ApplicationBase """
292 if self._eventlistener is None:
293 self._eventlistener = EventListener(self.engine)
294
295 return self._eventlistener
296
297 def getEventListener(self):
298 """ Returns the event listener """
299 return self._eventlistener
300
301 def newMapView(self, map):
302 """ Creates a new map view """
303 mapview = MapView(map)
304
305 self._mapviewList.append(mapview)
306
307 mapAction = Action(unicode(map.getId()))
308 action.activated.connect(cbwa(self.showMapView, mapview), sender=mapAction, weak=False)
309 self._mapgroup.addAction(mapAction)
310
311 self.showMapView(mapview)
312
313 events.mapAdded.send(sender=self, map=map)
314
315 return mapview
316
317 def openFile(self, path):
318 """ Opens a file """
319 try:
320 map = loaders.loadMapFile(path, self.engine)
321 return self.newMapView(map)
322 except:
323 errormsg = u"Opening map failed:\n"
324 errormsg += u"File: "+unicode(path)+"\n"
325 errormsg += u"Error: "+unicode(sys.exc_info()[1])
326 ErrorDialog(errormsg)
327 return None
328
329
330 def saveAll(self):
331 """ Saves all open maps """
332 tmpView = self._mapview
333 for mapView in self._mapviewList:
334 self._mapview = mapView
335 self._filemanager.save()
336 self._mapview = tmpView
337
338 def quit(self):
339 """ Quits the editor. An onQuit signal is sent before the application closes """
340 events.onQuit.send(sender=self)
341
342 self._settings.saveSettings()
343 ApplicationBase.quit(self)
344
345 def _pump(self):
346 """ Called once per frame """
347 # ApplicationBase and Engine should be done initializing at this point
348 if self._inited == False:
349 self._initGui()
350 self._initTools()
351 self._inited = True
352
353 events.onPump.send(sender=self)
354
355 def __sendMouseEvent(self, event, **kwargs):
356 """ Function used to capture mouse events for EventListener """
357 msEvent = fife.MouseEvent
358 type = event.getType()
359
360 if type == msEvent.MOVED:
361 mouseMoved.send(sender=self._maparea, event=event)
362
363 elif type == msEvent.PRESSED:
364 mousePressed.send(sender=self._maparea, event=event)
365
366 elif type == msEvent.RELEASED:
367 mouseReleased.send(sender=self._maparea, event=event)
368
369 elif type == msEvent.WHEEL_MOVED_DOWN:
370 mouseWheelMovedDown.send(sender=self._maparea, event=event)
371
372 elif type == msEvent.WHEEL_MOVED_UP:
373 mouseWheelMovedUp.send(sender=self._maparea, event=event)
374
375 elif type == msEvent.CLICKED:
376 mouseClicked.send(sender=self._maparea, event=event)
377
378 elif type == msEvent.ENTERED:
379 mouseEntered.send(sender=self._maparea, event=event)
380
381 elif type == msEvent.EXITED:
382 mouseExited.send(sender=self._maparea, event=event)
383
384 elif type == msEvent.DRAGGED:
385 mouseDragged.send(sender=self._maparea, event=event)
386
387 def __sendKeyEvent(self, event, **kwargs):
388 """ Function used to capture key events for EventListener """
389 type = event.getType()
390
391 if type == fife.KeyEvent.PRESSED:
392 self._eventlistener.keyPressed(event)
393
394 elif type == fife.KeyEvent.RELEASED:
395 self._eventlistener.keyReleased(event)
396
397
398
399