comparison tools/editor/scripts/mapview.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 ac7806c46b94
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
24 from fife import fife
25 import editor
26 from fife.extensions import loaders, savers
27 from events import events
28 from mapcontroller import MapController
29
30 class MapView:
31 """ MapView represents a map in the editor.
32
33 It handles opening, saving and closing of a map,
34 as well as displaying it on screen.
35 """
36 def __init__(self, map):
37 self._map = map
38 self._editor = editor.getEditor()
39 self._controller = MapController(self._map)
40 self._camera = None
41
42 if map is None:
43 raise AttributeError("No map passed to MapView!")
44
45 if not self._map.getLayers():
46 raise AttributeError('Editor error: map ' + self._map.getId() + ' has no layers. Cannot edit.')
47
48 map.addChangeListener(self._editor.getEventListener().mapchangelistener)
49 for layer in map.getLayers():
50 layer.addChangeListener(self._editor.getEventListener().layerchangelistener)
51
52 events.onLayerCreate.connect(self._layerCreated)
53
54 self.importlist = []
55 if hasattr(map, "importDirs"):
56 self.importlist.extend(map.importDirs)
57
58 def _cleanUp(self):
59 events.onLayerCreate.disconnect(self._layerCreated)
60
61 if self._map:
62 for layer in self._map.getLayers():
63 layer.removeChangeListener(self._editor.getEventListener().layerchangelistener)
64
65 self.importlist = []
66 self._map = None
67 self._editor = None
68 self._controller = None
69 self._camera = None
70
71 def _layerCreated(self, map, layer):
72 if map.getId() == self._map.getId():
73 layer.addChangeListener(self._editor.getEventListener().layerchangelistener)
74
75 # Copied from mapeditor.py
76 def show(self):
77 """ Sets up the camera to display the map. Size of the camera is equal to the
78 screen size """
79 _camera = None
80
81 engine = self._editor.getEngine()
82 engine.getView().resetRenderers()
83 for cam in engine.getView().getCameras():
84 cam.setEnabled(False)
85
86 for cam in engine.getView().getCameras():
87 if cam.getLocationRef().getMap().getId() == self._map.getId():
88 rb = engine.getRenderBackend()
89 cam.setViewPort(fife.Rect(0, 0, rb.getScreenWidth(), rb.getScreenHeight()))
90 cam.setEnabled(True)
91 self._camera = cam
92 break
93 else:
94 raise AttributeError('No cameras found associated with this map: ' + self._map.getId())
95
96 def getCamera(self):
97 return self._camera
98
99 def setCamera(self, camera):
100 if not camera:
101 print "Camera can not be None"
102 return
103
104 if camera.getLocation().getLayer().getMap() != self._map:
105 print "Camera is not associated with this map"
106 return
107
108 self._camera = None
109
110 def getController(self):
111 return self._controller
112
113 def getMap(self):
114 return self._map
115
116 def save(self):
117 """ Saves the map using the previous filename.
118
119 Emits preSave and postSave signals.
120 """
121 curname = ""
122 try:
123 curname = self._map.getResourceLocation().getFilename()
124 except RuntimeError:
125 print "Map has no filename yet, can't save."
126 return
127
128 events.preSave.send(sender=self, mapview=self)
129 savers.saveMapFile(curname, self._editor.getEngine(), self._map, importList=self.importlist)
130 events.postSave.send(sender=self, mapview=self)
131
132 def saveAs(self, filename):
133 """ Saves the map using a specified filename.
134
135 Emits preSave and postSave signals.
136 """
137 events.preSave.send(sender=self, mapview=self)
138 savers.saveMapFile(str(filename), self._editor.getEngine(), self._map, importList=self.importlist)
139 events.postSave.send(sender=self, mapview=self)
140
141 def close(self):
142 """ Closes the mapview """
143 events.preMapClosed.send(sender=self, mapview=self)
144
145 self._controller.cleanUp()
146
147 # Remove cameras
148 view = self._editor.getEngine().getView()
149 for cam in view.getCameras():
150 if cam.getLocationRef().getMap().getId() == self._map.getId():
151 cam.setEnabled(False)
152 #view.removeCamera(cam)
153 view.resetRenderers()
154
155 # Unload the map from FIFE
156 self._editor.getEngine().getModel().deleteMap(self._map)
157 self._map = None
158 self._cleanUp()
159
160 events.postMapClosed.send(sender=self, mapview=self)
161
162