changeset 367:598547404022

* Initial support for closing maps: * Lots of cleanup functions has been added * TODO: Cameras are only removed when all maps are closed * TODO: Screen leaves traces when the last map is closed * Replaced pre/postMapRemove events with pre/postMapClosed * Fixed a bug in the Toolbar where it didn't properly handle removal of actions from an actiongroup
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 29 Oct 2009 22:11:48 +0000
parents dcf6f6091868
children c4df3fbbbc91
files clients/editor/LICENSE clients/editor/gui/icons/close_map.png clients/editor/plugins/LayerTool.py clients/editor/scripts/editor.py clients/editor/scripts/events/events.py clients/editor/scripts/gui/filemanager.py clients/editor/scripts/gui/mapeditor.py clients/editor/scripts/gui/toolbar.py clients/editor/scripts/mapcontroller.py clients/editor/scripts/mapview.py
diffstat 10 files changed, 115 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/clients/editor/LICENSE	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/LICENSE	Thu Oct 29 22:11:48 2009 +0000
@@ -13,6 +13,7 @@
 gui/icons/add_instance.png
 gui/icons/add_layer.png
 gui/icons/cycle_styles.png
+gui/icons/close_map.png
 gui/icons/delete_layer.png
 gui/icons/edit.png
 gui/icons/erase_instance.png
Binary file clients/editor/gui/icons/close_map.png has changed
--- a/clients/editor/plugins/LayerTool.py	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/plugins/LayerTool.py	Thu Oct 29 22:11:48 2009 +0000
@@ -84,6 +84,7 @@
 		self.toggle()
 		
 		events.postMapShown.connect(self.update)
+		events.preMapClosed.connect(self._mapClosed)
 
 	def disable(self):
 		""" Disable plugin """
@@ -93,6 +94,7 @@
 		self._container.hide()
 		
 		events.postMapShown.disconnect(self.update)
+		events.preMapClosed.disconnect(self._mapClosed)
 		
 		self._editor._tools_menu.removeAction(self._action_show)
 
@@ -105,6 +107,10 @@
 		return u"Layertool"
 	
 	#--- End plugin functions ---#
+	def _mapClosed(self):
+		self.update(mapview=None)
+		
+	
 	def showLayerWizard(self):
 		""" Show layer wizard """
 		if not self._mapview: return
--- a/clients/editor/scripts/editor.py	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/scripts/editor.py	Thu Oct 29 22:11:48 2009 +0000
@@ -149,7 +149,7 @@
 		self._toolbox.position_technique = "explicit"
 		self._toolbox.position = (150, 150)
 		
-		self._mapbar = ToolBar(panel_size=20)
+		self._mapbar = ToolBar(name="MapBar", panel_size=20)
 		self._mapbar.setDocked(True)
 		
 		self._maparea = pychan.widgets.VBox()
@@ -217,7 +217,6 @@
 		
 		self._mapgroup = ActionGroup(exclusive=True, name="Mapgroup")
 		self._mapbar.addAction(self._mapgroup)
-		self._mapbar.addAction(ActionGroup(exclusive=True, name="Mapgroup2"))
 		self._window_menu.addAction(self._mapgroup)
 		
 		help_action = Action(u"Help", "gui/icons/help.png")
@@ -345,10 +344,30 @@
 		
 		self.showMapView(mapview)
 		
+		events.preMapClosed.connect(self._mapRemoved)
 		events.mapAdded.send(sender=self, map=map)
 		
 		return mapview
-	
+		
+	def _mapRemoved(self, mapview):
+		events.preMapClosed.disconnect(self._mapRemoved)
+		
+		index = self._mapviewlist.index(mapview)-1
+		self._mapviewlist.remove(mapview)
+		
+		# Remove tab
+		for map_action in self._mapgroup.getActions():
+			if map_action.text == unicode(mapview.getMap().getId()):
+				self._mapgroup.removeAction(map_action)
+				break
+				
+		# Change mapview
+		if index >= 0:
+			self.showMapView(self._mapviewlist[index])
+		else:
+			self._mapview = None
+			self.getEngine().getView().clearCameras()
+
 	def openFile(self, path):
 		""" Opens a file """
 		try:
@@ -361,7 +380,6 @@
 			errormsg += u"Error: "+unicode(sys.exc_info()[1])
 			ErrorDialog(errormsg)
 			return None
-			
 	
 	def saveAll(self):
 		""" Saves all open maps """
--- a/clients/editor/scripts/events/events.py	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/scripts/events/events.py	Thu Oct 29 22:11:48 2009 +0000
@@ -30,10 +30,10 @@
 preSave	= Signal(providing_args=["mapview"])
 postSave = Signal(providing_args=["mapview"])
 mapAdded = Signal(providing_args=["mapview"])
-preMapRemove = Signal(providing_args=["mapview"])
-postMapRemove = Signal(providing_args=["mapview"])
 preMapShown = Signal(providing_args=["mapview"])
 postMapShown = Signal(providing_args=["mapview"])
+preMapClosed = Signal(providing_args=["mapview"])
+postMapClosed = Signal(providing_args=["mapview"])
 onInstancesSelected = Signal(providing_args=["instances"])
 onObjectSelected = Signal(providing_args=["object"])
 onObjectsImported = Signal(providing_args=[])
--- a/clients/editor/scripts/gui/filemanager.py	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/scripts/gui/filemanager.py	Thu Oct 29 22:11:48 2009 +0000
@@ -51,6 +51,7 @@
 
 		newAction = Action(u"New map", "gui/icons/new_map.png")
 		loadAction = Action(u"Open", "gui/icons/load_map.png")
+		closeAction = Action(u"Close", "gui/icons/close_map.png")
 		saveAction = Action(u"Save", "gui/icons/save_map.png")
 		saveAsAction = Action(u"Save as", "gui/icons/save_mapas.png")
 		saveAllAction = Action(u"Save all", "gui/icons/save_allmaps.png")
@@ -59,6 +60,7 @@
 		
 		newAction.helptext = u"Create new map"
 		loadAction.helptext = u"Open existing map"
+		closeAction.helptext = u"Close map"
 		saveAction.helptext = u"Save map"
 		saveAsAction.helptext = u"Save map as"
 		saveAllAction.helptext = u"Save all opened maps"
@@ -67,6 +69,7 @@
 		
 		action.activated.connect(self.showMapWizard, sender=newAction)
 		action.activated.connect(self.showLoadDialog, sender=loadAction)
+		action.activated.connect(self.closeCurrentMap, sender=closeAction)
 		action.activated.connect(self.save, sender=saveAction)
 		action.activated.connect(self.saveAs, sender=saveAsAction)
 		action.activated.connect(self.editor.saveAll, sender=saveAllAction)
@@ -79,12 +82,16 @@
 		eventlistener = self.editor.getEventListener()
 		eventlistener.getKeySequenceSignal(fife.Key.N, ["ctrl"]).connect(self.showMapWizard)
 		eventlistener.getKeySequenceSignal(fife.Key.O, ["ctrl"]).connect(self.showLoadDialog)
+		eventlistener.getKeySequenceSignal(fife.Key.W, ["ctrl"]).connect(self.closeCurrentMap)
 		eventlistener.getKeySequenceSignal(fife.Key.S, ["ctrl"]).connect(self.save)
 		eventlistener.getKeySequenceSignal(fife.Key.S, ["ctrl", "shift"]).connect(self.editor.saveAll)
 		
 		fileGroup = ActionGroup()
 		fileGroup.addAction(newAction)
 		fileGroup.addAction(loadAction)
+		fileGroup.addSeparator()
+		fileGroup.addAction(closeAction)
+		fileGroup.addSeparator()
 		fileGroup.addAction(saveAction)
 		fileGroup.addAction(saveAsAction)
 		fileGroup.addAction(saveAllAction)
@@ -96,6 +103,14 @@
 		self.editor.getToolBar().insertSeparator(None, 1)
 		self.editor._file_menu.insertAction(fileGroup, 0)
 		self.editor._file_menu.insertSeparator(None, 1)
+		
+	def closeCurrentMap(self):
+		mapview = self.editor.getActiveMapView()
+		if mapview is None:
+			print "No map to close"
+			return
+			
+		mapview.close()
 
 	def showLoadDialog(self):
 		if self._filebrowser is None:
--- a/clients/editor/scripts/gui/mapeditor.py	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/scripts/gui/mapeditor.py	Thu Oct 29 22:11:48 2009 +0000
@@ -66,6 +66,7 @@
 		self._instances = []
 		
 		events.postMapShown.connect(self._mapChanged)
+		events.preMapClosed.connect(self._mapClosed)
 		events.onObjectSelected.connect(self.setObject)
 		self._undogroup = False
 		
@@ -112,6 +113,9 @@
 	def _mapChanged(self, sender, mapview):
 		self.setController(mapview.getController())
 		
+	def _mapClosed(self, sender, mapview):
+		self.setController(None)
+		
 	def _setCursor(self):
 		engine = self._editor.getEngine()
 		cursor = engine.getCursor()
--- a/clients/editor/scripts/gui/toolbar.py	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/scripts/gui/toolbar.py	Thu Oct 29 22:11:48 2009 +0000
@@ -101,10 +101,16 @@
 		self._insertButton(action, position)
 		
 	def _updateActionGroup(self, sender):
-		position = self._actions.index(sender)
-		self.removeAction(sender)
-		self.insertAction(sender, position)
-		self.adaptLayout(False)
+		if isinstance(sender, ActionGroup):
+			# Toolbar didn't properly handle events where
+			# an action in actiongroup was removed
+			self._updateToolbar()
+		else:
+			position = self._actions.index(sender)
+			self.removeAction(sender)
+			self.insertAction(sender, position)
+			self.adaptLayout()
+		
 		
 	def _insertButton(self, action, position):
 		actions = [action]
--- a/clients/editor/scripts/mapcontroller.py	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/scripts/mapcontroller.py	Thu Oct 29 22:11:48 2009 +0000
@@ -56,6 +56,20 @@
 		
 		if map is not None:
 			self.setMap(map.getId())
+			
+	def cleanUp(self):
+		undomanager.preUndo.disconnect(self._startUndo, sender=self._undomanager)
+		undomanager.preRedo.disconnect(self._startUndo, sender=self._undomanager)
+		undomanager.postUndo.disconnect(self._endUndo, sender=self._undomanager)
+		undomanager.postRedo.disconnect(self._endUndo, sender=self._undomanager)
+		self._undomanager.clear()
+		
+		self._editor = None
+		self._engine = None
+		self._map = None
+		self._selection = []
+		self._layer = None
+		self._camera = None
 		
 	def setMap(self, mapid):
 		""" Set the map to be edited """
--- a/clients/editor/scripts/mapview.py	Thu Oct 29 20:19:08 2009 +0000
+++ b/clients/editor/scripts/mapview.py	Thu Oct 29 22:11:48 2009 +0000
@@ -37,23 +37,37 @@
 		self._map = map
 		self._editor = editor.getEditor()
 		self._controller = MapController(self._map)
+		self._camera = None
 		
 		if map is None:
-			print "No map passed to MapView!"
-		else:
-			if not self._map.getLayers():
-				raise AttributeError('Editor error: map ' + self._map.getId() + ' has no layers. Cannot edit.')
+			raise AttributeError("No map passed to MapView!")
+		
+		if not self._map.getLayers():
+			raise AttributeError('Editor error: map ' + self._map.getId() + ' has no layers. Cannot edit.')
 
-			map.addChangeListener(self._editor.getEventListener().mapchangelistener)
-			for layer in map.getLayers():
-				layer.addChangeListener(self._editor.getEventListener().layerchangelistener)
-				
-			events.onLayerCreate.connect(self._layerCreated)
+		map.addChangeListener(self._editor.getEventListener().mapchangelistener)
+		for layer in map.getLayers():
+			layer.addChangeListener(self._editor.getEventListener().layerchangelistener)
+			
+		events.onLayerCreate.connect(self._layerCreated)
 
 		self.importlist = []
 		if hasattr(map, "importDirs"):
 			self.importlist.extend(map.importDirs)
 			
+	def _cleanUp(self):
+		events.onLayerCreate.disconnect(self._layerCreated)
+		
+		if self._map:
+			for layer in self._map.getLayers():
+				layer.removeChangeListener(self._editor.getEventListener().layerchangelistener)
+		
+		self.importlist = []
+		self._map = None
+		self._editor = None
+		self._controller = None
+		self._camera = None
+			
 	def _layerCreated(self, map, layer):
 		if map.getId() == self._map.getId():
 			layer.addChangeListener(self._editor.getEventListener().layerchangelistener)
@@ -126,6 +140,23 @@
 		
 	def close(self):
 		""" Closes the mapview """
-		pass
+		events.preMapClosed.send(sender=self, mapview=self)
+		
+		self._controller.cleanUp()
+
+		# Remove cameras
+		view = self._editor.getEngine().getView()
+		for cam in view.getCameras():
+			if cam.getLocationRef().getMap().getId() == self._map.getId():
+				cam.setEnabled(False)
+				#view.removeCamera(cam)
+		view.resetRenderers()
+		
+		# Unload the map from FIFE
+		self._editor.getEngine().getModel().deleteMap(self._map)
+		self._map = None
+		self._cleanUp()
+		
+		events.postMapClosed.send(sender=self, mapview=self)
 
 	
\ No newline at end of file