def removeInstances(self, instances, layer=None):
        """ Removes all provided instances """
        mname = 'removeInstances'
        if not instances:
            if self.debug: print 'No instances assigned in %s' % mname
            return

        for i in instances:
            if self.debug:
                print 'Deleting instance ' + i.getObject().getId(
                ) + ' at ' + str(i.getLocation().getExactLayerCoordinates())

            if not self._undo:
                object = i.getObject()
                position = i.getLocation().getExactLayerCoordinates()
                undocall = cbwa(self.placeInstance, position, object,
                                i.getLocation().getLayer())
                redocall = cbwa(self.removeInstanceOfObjectAt, position,
                                object,
                                i.getLocation().getLayer())
                undoobject = undomanager.UndoObject(undocall, redocall,
                                                    "Removed instance")
                self._undomanager.addAction(undoobject)

            if layer:
                layer.deleteInstance(i)
            else:
                self._layer.deleteInstance(i)

            self.decrementReferenceCountForObject(i.getObject())
    def placeInstance(self, position, object, layer=None):
        """ Places an instance of object at position. Any existing instances on position are removed. """
        mname = 'placeInstance'
        if not object:
            if self.debug: print 'No object assigned in %s' % mname
            return
        if not position:
            if self.debug: print 'No position assigned in %s' % mname
            return
        if not self._layer:
            if self.debug: print 'No layer assigned in %s' % mname
            return

        if self.debug:
            print 'Placing instance of ' + object.getId() + ' at ' + str(
                position)

        # Remove instances from target position
        if not self._undo:
            instances = self.getInstancesFromPosition(position)
            if len(instances) == 1:
                # Check if the only instance at position has the same object
                objectId = instances[0].getObject().getId()
                objectNs = instances[0].getObject().getNamespace()
                if objectId == object.getId(
                ) and objectNs == object.getNamespace():
                    if self.debug: print "Tried to place duplicate instance"
                    return

            self._undomanager.startGroup("Placed instance")
            self.removeInstances(instances)

        if layer:
            inst = layer.createInstance(object, position)
        else:
            inst = self._layer.createInstance(object, position)
        fife.InstanceVisual.create(inst)

        # update reference count in import list for object
        self.incrementReferenceCountForObject(object)

        if not self._undo:
            redocall = cbwa(self.placeInstance, position, object,
                            inst.getLocation().getLayer())
            undocall = cbwa(self.removeInstanceOfObjectAt, position, object,
                            inst.getLocation().getLayer())
            undoobject = undomanager.UndoObject(undocall, redocall,
                                                "Placed instance")
            self._undomanager.addAction(undoobject)
            self._undomanager.endGroup()
예제 #3
0
	def moveInstances(self, instances, moveBy, exact=False, origLoc=None, origFacing=None):
		""" Moves provided instances by moveBy. If exact is false, the instances are
		snapped to closest cell. origLoc and origFacing are only set when an undo/redo
		operation takes place and will have no effect and should not be used under normal
		circumstances."""
		mname = 'moveInstances'
		if not self._layer:
			if self.debug: print 'No layer assigned in %s' % mname
			return
			
		if exact and type(moveBy) != fife.ExactModelCoordinate:
			moveBy = fife.ExactModelCoordinate(float(moveBy.x), float(moveBy.y), float(moveBy.z))
		elif exact is False and type(moveBy) != fife.ModelCoordinate:
			moveBy = fife.ModelCoordinate(int(round(moveBy.x)), int(round(moveBy.y)), int(round(moveBy.z)))
			
		for i in instances:
			loc = i.getLocation()
			f = i.getFacingLocation()
			if exact:
				newCoords = loc.getExactLayerCoordinates() + moveBy
				loc.setExactLayerCoordinates(newCoords)
				f.setExactLayerCoordinates(f.getExactLayerCoordinates() + moveBy)
			else:
				# Move instance and snap to closest cell
				newCoords = loc.getLayerCoordinates() + moveBy
				loc.setLayerCoordinates(newCoords)
				f.setLayerCoordinates(f.getLayerCoordinates() + moveBy)
				
			if not self._undo:
				undocall = cbwa(self.moveInstances, [i], moveBy, exact, i.getLocation(), i.getFacingLocation())
				redocall = cbwa(self.moveInstances, [i], moveBy, exact, i.getLocation(), i.getFacingLocation())
				undoobject = undomanager.UndoObject(undocall, redocall, "Moved instance")
				self._undomanager.addAction(undoobject)
				i.setLocation(loc)
				i.setFacingLocation(f)
				
			else:
				assert(origLoc)
				assert(origFacing)
				i.setLocation(origLoc)
				i.setFacingLocation(origFacing)
    def moveInstances(self,
                      instances,
                      moveBy,
                      exact=False,
                      origLoc=None,
                      origFacing=None):
        """ Moves provided instances by moveBy. If exact is false, the instances are
			snapped to closest cell. origLoc and origFacing are only set when an undo/redo
			operation takes place and will have no effect and should not be used under normal
			circumstances.
		
		@type	instances:	list
		@param	instances:	a bunch of selected fife.Instance objects
		@type	moveBy:	fife.Point3D
		@param	moveBy:	diff of last and current exact model coordinate relative to the cursor
		@type	exact:	bool
		@param	exact:	flag to either set exactLayerCoordinates or LayerCoordinates
		@rtype	result:	bool
		@return	result:	flag wether the instances were moved or not (always True if exact=True)
		"""
        result = True
        mname = 'moveInstances'
        if not self._layer:
            if self.debug: print 'No layer assigned in %s' % mname
            return not result

        moveBy = fife.ExactModelCoordinate(float(moveBy.x), float(moveBy.y),
                                           float(moveBy.z))

        for i in instances:
            loc = i.getLocation()
            f = i.getFacingLocation()

            nloc = fife.Location(self._layer)
            nloc.setMapCoordinates(loc.getMapCoordinates() + moveBy)

            if loc.getLayerCoordinates() == nloc.getLayerCoordinates(
            ) and not exact:
                result = False
                break

            if exact:
                loc.setMapCoordinates(nloc.getMapCoordinates())
                f.setMapCoordinates(loc.getMapCoordinates())
            else:
                loc.setLayerCoordinates(nloc.getLayerCoordinates())
                f.setLayerCoordinates(loc.getLayerCoordinates())

            if not self._undo:
                undocall = cbwa(self.moveInstances, [i], moveBy, exact,
                                i.getLocation(), i.getFacingLocation())
                redocall = cbwa(self.moveInstances, [i], moveBy, exact,
                                i.getLocation(), i.getFacingLocation())
                undoobject = undomanager.UndoObject(undocall, redocall,
                                                    "Moved instance")
                self._undomanager.addAction(undoobject)
                i.setLocation(loc)
                i.setFacingLocation(f)

            else:
                assert (origLoc)
                assert (origFacing)
                i.setLocation(origLoc)
                i.setFacingLocation(origFacing)

        return result