comparison clients/editor/scripts/gui/filemanager.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 0adf9423062e
comparison
equal deleted inserted replaced
254:10b5f7f36dd4 255:51cc05d862f2
1 import math, fife, pychan, filebrowser
2 import loaders, savers
3 import action
4 import scripts.editor
5 import pychan.widgets as widgets
6
7 from action import Action, ActionGroup
8 from input import InputDialog
9 from selection import SelectionDialog, ClickSelectionDialog
10
11 class FileManager(object):
12 def __init__(self):
13 self.editor = scripts.editor.getEditor()
14 self.engine = self.editor.getEngine()
15 self._map = None
16 self._layer = None
17 self._mapdlg = None
18 self._layerdlg = None
19 self._cameradlg = None
20
21 self._filebrowser = None
22 self._savebrowser = None
23
24 newAction = Action(u"New map", "gui/icons/new_map.png")
25 loadAction = Action(u"Open", "gui/icons/load_map.png")
26 saveAction = Action(u"Save", "gui/icons/save_map.png")
27 saveAsAction = Action(u"Save as", "gui/icons/save_mapas.png")
28 saveAllAction = Action(u"Save all", "gui/icons/save_allmaps.png")
29
30 newAction.helptext = u"Create new map"
31 loadAction.helptext = u"Open existing map"
32 saveAction.helptext = u"Save map"
33 saveAsAction.helptext = u"Save map as"
34 saveAllAction.helptext = u"Save all opened maps"
35
36 action.activated.connect(self.showMapWizard, sender=newAction)
37 action.activated.connect(self.showLoadDialog, sender=loadAction)
38 action.activated.connect(self.save, sender=saveAction)
39 action.activated.connect(self.saveAs, sender=saveAsAction)
40 action.activated.connect(self.editor.saveAll, sender=saveAllAction)
41
42 eventlistener = self.editor.getEventListener()
43 eventlistener.getKeySequenceSignal(fife.Key.N, ["ctrl"]).connect(self.showMapWizard)
44 eventlistener.getKeySequenceSignal(fife.Key.O, ["ctrl"]).connect(self.showLoadDialog)
45 eventlistener.getKeySequenceSignal(fife.Key.S, ["ctrl"]).connect(self.save)
46 eventlistener.getKeySequenceSignal(fife.Key.S, ["ctrl", "shift"]).connect(self.editor.saveAll)
47
48 fileGroup = ActionGroup()
49 fileGroup.addAction(newAction)
50 fileGroup.addAction(loadAction)
51 fileGroup.addAction(saveAction)
52 fileGroup.addAction(saveAsAction)
53 fileGroup.addAction(saveAllAction)
54
55 self.editor.getToolBar().insertAction(fileGroup, 0)
56 self.editor.getToolBar().insertSeparator(None, 1)
57 self.editor._fileMenu.insertAction(fileGroup, 0)
58 self.editor._fileMenu.insertSeparator(None, 1)
59
60 def showLoadDialog(self):
61 if self._filebrowser is None:
62 self._filebrowser = filebrowser.FileBrowser(self.engine, self.loadFile, extensions = loaders.fileExtensions)
63 self._filebrowser.showBrowser()
64
65 def showSaveDialog(self):
66 if self._savebrowser is None:
67 self._savebrowser = filebrowser.FileBrowser(self.engine, self.saveFile, savefile=True, extensions = loaders.fileExtensions)
68 self._savebrowser.showBrowser()
69
70 def saveFile(self, path, filename):
71 mapview = self.editor.getActiveMapView()
72 if mapview is None:
73 print "No map is open"
74 return
75
76 fname = '/'.join([path, filename])
77 mapview.saveAs(fname)
78
79 def saveAs(self):
80 mapview = self.editor.getActiveMapView()
81 if mapview is None:
82 print "No map is open"
83 return
84 self.showSaveDialog(self)
85
86 def loadFile(self, path, filename):
87 self.editor.openFile('/'.join([path, filename]))
88
89 def showMapWizard(self):
90 if self._cameradlg:
91 self._cameradlg._widget.show()
92 elif self._layerdlg:
93 self._layerdlg._widget.show()
94 elif self._mapdlg:
95 self._mapdlg._widget.show()
96 else:
97 self._newMap()
98
99 def _newMap(self):
100 self._mapdlg = InputDialog(u'Enter a map identifier:', self._newLayer, self._clean)
101
102 def _newLayer(self, mapId):
103 self._map = self.engine.getModel().createMap(str(mapId))
104 self._layerdlg = InputDialog(u'Enter a layer identifier for a default layer:', self._newCamera, self._clean)
105
106 def _newCamera(self, layerId):
107 grid = fife.SquareGrid()
108 layer = self._map.createLayer(str(layerId), grid)
109 grid.thisown = 0
110
111 self._cameradlg = CameraEditor(self.engine, self._addMap, self._clean, self._map, self._layer)
112
113 def _addMap(self):
114 self.editor.newMapView(self._map)
115 self._clean()
116
117 def _clean(self):
118 self._mapdlg = None
119 self._layerdlg = None
120 self._cameradlg = None
121 self._map = None
122 self._layer = None
123
124 def save(self):
125 curname = None
126 mapview = self.editor.getActiveMapView()
127 if mapview is None:
128 print "No map is open"
129 return
130
131 try:
132 curname = mapview.getMap().getResourceLocation().getFilename()
133 except RuntimeError:
134 self.showSaveDialog()
135 return
136
137 mapview.save()
138
139 class CameraEditor(object):
140 """
141 CameraEditor provides a gui dialog for camera creation. The callback is called when camera creation is complete. A
142 partial specification of the camera parameters may optionally be given.
143 """
144 def __init__(self, engine, callback=None, onCancel=None, map=None, layer=None):
145 self.engine = engine
146 self.callback = callback
147 self.onCancel = onCancel
148 self._widget = pychan.loadXML('gui/cameraedit.xml')
149
150 if map:
151 self._widget.distributeData({
152 'mapBox' : unicode(map.getId()),
153 })
154
155 if layer:
156 self._widget.distributeData({
157 'layerBox' : unicode(layer.getId()),
158 })
159
160 self._widget.mapEvents({
161 'okButton' : self._finished,
162 'cancelButton' : self._cancelled
163 })
164
165 self._widget.show()
166
167 def _cancelled(self):
168 if self.onCancel:
169 self.onCancel()
170 self._widget.hide()
171
172
173 def _finished(self):
174 id = self._widget.collectData('idBox')
175 if id == '':
176 print 'Please enter a camera id.'
177 return
178
179 try:
180 map = self.engine.getModel().getMap(str(self._widget.collectData('mapBox')))
181 except fife.Exception:
182 print 'Cannot find the specified map id.'
183 return
184
185 try:
186 layer = map.getLayer(str(self._widget.collectData('layerBox')))
187 except fife.Exception:
188 print 'Cannot find the specified layer id.'
189 return
190
191 try:
192 vals = self._widget.collectData('viewBox').split(',')
193 if len(vals) != 4:
194 raise ValueError
195
196 viewport = fife.Rect(*[int(c) for c in vals])
197 except ValueError:
198 print 'Please enter 4 comma (,) delimited values for viewport x,y,width,height.'
199 return
200
201 try:
202 refh = int(self._widget.collectData('refhBox'))
203 refw = int(self._widget.collectData('refwBox'))
204 except ValueError:
205 print 'Please enter positive integer values for reference width and height.'
206 return
207
208 try:
209 rot = int(self._widget.collectData('rotBox'))
210 tilt = int(self._widget.collectData('tiltBox'))
211 except ValueError:
212 print 'Please enter positive integer values for rotation and tilt.'
213 return
214
215 cam = self.engine.getView().addCamera(str(id), layer, viewport, fife.ExactModelCoordinate(0,0,0))
216 cam.setCellImageDimensions(refw, refh)
217 cam.setRotation(rot)
218 cam.setTilt(tilt)
219
220 self._widget.hide()
221
222 if self.callback:
223 self.callback()