comparison clients/editor/scripts/mapcontroller.py @ 266:475f83e227e0

Fixed undo/redo for remove- and placeInstance
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 13 Jun 2009 15:47:16 +0000
parents 51cc05d862f2
children 7ddec4ce99b3
comparison
equal deleted inserted replaced
265:043d71a192b5 266:475f83e227e0
5 5
6 import fife 6 import fife
7 import editor 7 import editor
8 import events 8 import events
9 import undomanager 9 import undomanager
10
11 from pychan.tools import callbackWithArguments as cbwa
10 12
11 class MapController(object): 13 class MapController(object):
12 """ MapController provides an interface for editing maps """ 14 """ MapController provides an interface for editing maps """
13 def __init__(self, map): 15 def __init__(self, map):
14 16
183 if not self._layer: 185 if not self._layer:
184 if self.debug: print 'No layer assigned in %s' % mname 186 if self.debug: print 'No layer assigned in %s' % mname
185 return 187 return
186 188
187 if self.debug: print 'Placing instance of ' + object.getId() + ' at ' + str(position) 189 if self.debug: print 'Placing instance of ' + object.getId() + ' at ' + str(position)
188 190
189 # Remove instances from target position 191 # Remove instances from target position
190 if not self._undo: 192 if not self._undo:
193 instances = self.getInstancesFromPosition(position)
194 if len(instances) == 1:
195 # Check if the only instance at position has the same object
196 objectId = instances[0].getObject().getId()
197 objectNs = instances[0].getObject().getNamespace()
198 if objectId == object.getId() and objectNs == object.getNamespace():
199 if self.debug: print "Tried to place duplicate instance"
200 return
201
191 self._undomanager.startGroup("Placed instance") 202 self._undomanager.startGroup("Placed instance")
192 self.removeInstances(self.getInstancesFromPosition(position)) 203 self.removeInstances(instances)
193 204
194 inst = self._layer.createInstance(object, position) 205 inst = self._layer.createInstance(object, position)
195 fife.InstanceVisual.create(inst) 206 fife.InstanceVisual.create(inst)
196 207
197 if not self._undo: 208 if not self._undo:
198 redocall = lambda: self.placeInstance(position, object) 209 redocall = cbwa(self.placeInstance, position, object)
199 undocall = lambda: self.removeInstances([inst]) 210 undocall = cbwa(self.removeInstanceOfObjectAt, position, object)
200 undoobject = undomanager.UndoObject(undocall, redocall, "Placed instance") 211 undoobject = undomanager.UndoObject(undocall, redocall, "Placed instance")
201 self._undomanager.addAction(undoobject) 212 self._undomanager.addAction(undoobject)
202 self._undomanager.endGroup() 213 self._undomanager.endGroup()
203 214
215 def removeInstanceOfObjectAt(self, position, object):
216 """ Removes the first instance of object it can find on position """
217 instances = self.getInstancesFromPosition(position)
218 for i in instances:
219 if i.getObject().getId() == object.getId() and i.getObject().getNamespace() == object.getNamespace():
220 self.removeInstances([i])
221 return
222
204 def removeInstances(self, instances): 223 def removeInstances(self, instances):
205 """ Removes all provided instances """ 224 """ Removes all provided instances """
206 mname = 'removeInstances' 225 mname = 'removeInstances'
207 if not instances: 226 if not instances:
208 if self.debug: print 'No instances assigned in %s' % mname 227 if self.debug: print 'No instances assigned in %s' % mname
211 for i in instances: 230 for i in instances:
212 if self.debug: print 'Deleting instance ' + i.getObject().getId() + ' at ' + str(i.getLocation().getExactLayerCoordinates()) 231 if self.debug: print 'Deleting instance ' + i.getObject().getId() + ' at ' + str(i.getLocation().getExactLayerCoordinates())
213 232
214 if not self._undo: 233 if not self._undo:
215 object = i.getObject() 234 object = i.getObject()
216
217 position = i.getLocation().getExactLayerCoordinates() 235 position = i.getLocation().getExactLayerCoordinates()
218 undocall = lambda: self.placeInstance(position, object) 236 undocall = cbwa(self.placeInstance, position, object)
219 redocall = lambda: self.removeInstances([i]) 237 redocall = cbwa(self.removeInstanceOfObjectAt, position, object)
220 undoobject = undomanager.UndoObject(undocall, redocall, "Removed instance") 238 undoobject = undomanager.UndoObject(undocall, redocall, "Removed instance")
221 self._undomanager.addAction(undoobject) 239 self._undomanager.addAction(undoobject)
222 240
223 self._layer.deleteInstance(i) 241 self._layer.deleteInstance(i)
224 242