Mercurial > fife-parpg
changeset 35:bf7f838e6684
Added methods for removing Objects from the Model.
author | jwt@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sat, 12 Jul 2008 02:50:09 +0000 |
parents | d66538926e78 |
children | 6f1227f4932b |
files | doc/dependencies/filedeps.dot engine/core/model/model.cpp engine/core/model/model.h engine/core/model/model.i tests/swig_tests/model_tests.py |
diffstat | 5 files changed, 92 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/dependencies/filedeps.dot Sat Jul 12 00:20:26 2008 +0000 +++ b/doc/dependencies/filedeps.dot Sat Jul 12 02:50:09 2008 +0000 @@ -235,6 +235,8 @@ "engine/core/model/model.cpp" -> "model/metamodel/abstractpather.h" "engine/core/model/model.cpp" -> "model/metamodel/grids/cellgrid.h" "engine/core/model/model.cpp" -> "model/metamodel/object.h" + "engine/core/model/model.cpp" -> "structures/instance.h" + "engine/core/model/model.cpp" -> "structures/layer.h" "engine/core/model/model.cpp" -> "structures/map.h" "engine/core/model/model.cpp" -> "util/base/exception.h" "engine/core/model/model.cpp" -> "util/structures/purge.h"
--- a/engine/core/model/model.cpp Sat Jul 12 00:20:26 2008 +0000 +++ b/engine/core/model/model.cpp Sat Jul 12 02:50:09 2008 +0000 @@ -32,6 +32,8 @@ #include "model/metamodel/object.h" #include "model/metamodel/grids/cellgrid.h" #include "structures/map.h" +#include "structures/layer.h" +#include "structures/instance.h" #include "util/base/exception.h" #include "model.h" @@ -159,6 +161,62 @@ return object; } + bool Model::deleteObject(Object* object) { + std::list<Layer*>::const_iterator jt; + std::vector<Instance*>::const_iterator kt; + for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) { + for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) { + for(kt = (*jt)->getInstances().begin(); kt != (*jt)->getInstances().end(); ++kt) { + Object* o = (*kt)->getObject(); + while(o != 0) { + if(o == object) + return false; + } + } + } + } + + std::string name_space = object->getNamespace(); + std::list<namespace_t>::iterator nspace = m_namespaces.begin(); + for(; nspace != m_namespaces.end(); ++nspace) { + if(nspace->first == name_space) break; + } + + if(nspace == m_namespaces.end()) + return true; + + std::list<Object*>::iterator it = nspace->second.begin(); + for(; it != nspace->second.end(); ++it) { + if(*it == object) { + delete *it; + nspace->second.erase(it); + return true; + } + } + + return true; + } + + bool Model::deleteObjects() { + std::list<Layer*>::const_iterator jt; + for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) { + for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) { + if((*jt)->hasInstances()) + return false; + } + } + + std::list<namespace_t>::iterator nspace = m_namespaces.begin(); + while(nspace != m_namespaces.end()) { + std::list<Object*>::iterator it = nspace->second.begin(); + for(; it != nspace->second.end(); ++it) { + delete *it; + } + nspace = m_namespaces.erase(nspace); + } + return true; + } + Object* Model::getObject(const std::string& id, const std::string& name_space) { std::list<namespace_t>::iterator nspace = m_namespaces.begin(); for(; nspace != m_namespaces.end(); ++nspace) {
--- a/engine/core/model/model.h Sat Jul 12 00:20:26 2008 +0000 +++ b/engine/core/model/model.h Sat Jul 12 02:50:09 2008 +0000 @@ -97,6 +97,16 @@ */ Object* createObject(const std::string& identifier, const std::string& name_space, Object* parent=0); + /** Attempt to remove an object from the model + * Fails and returns false if the object is referenced by an instance. + */ + bool deleteObject(Object*); + + /** Attempt to remove all objects from the model + * Fails and returns false if any maps with instances are present. + */ + bool deleteObjects(); + /** Get an object by its id. If no namespace is specified, the namespaces are searched in order * and the first matching object is returned. Returns 0 if object is not found. */
--- a/engine/core/model/model.i Sat Jul 12 00:20:26 2008 +0000 +++ b/engine/core/model/model.i Sat Jul 12 02:50:09 2008 +0000 @@ -33,6 +33,8 @@ std::list<std::string> getNamespaces() const; Object* createObject(const std::string& identifier, const std::string& name_space, Object* parent=0); + bool deleteObject(Object*); + bool deleteObjects(); Object* getObject(const std::string& id, const std::string& name_space); const std::list<Object*>& getObjects(const std::string& name_space) const;
--- a/tests/swig_tests/model_tests.py Sat Jul 12 00:20:26 2008 +0000 +++ b/tests/swig_tests/model_tests.py Sat Jul 12 02:50:09 2008 +0000 @@ -89,7 +89,26 @@ #p2 = inst.getLocation().getLayerCoordinates() #print p2.x, p2.y #self.assertEqual(inst.getLocation().getLayerCoordinates(), fife.ModelCoordinate(4,4)) - + + def testObjects(self): + obj1 = self.model.createObject("object003","test_nspace") + obj2 = self.model.createObject("object004","test_nspace") + self.model.createObject("object005","test_nspace") + self.model.createObject("object006","test_nspace") + + self.assertEqual(self.model.deleteObject(obj2),True) + + map = self.model.createMap("map007") + grid = fife.SquareGrid() + layer = map.createLayer("layer004",grid) + + inst = layer.createInstance(obj1, fife.ModelCoordinate(4,4)) + self.assertEqual(self.model.deleteObject(obj1),False) + self.assertEqual(self.model.deleteObjects(),False) + layer.deleteInstance(inst) + self.assertEqual(self.model.deleteObject(obj1),True) + self.assertEqual(self.model.deleteObjects(),True) + class TestActionAngles(unittest.TestCase): def setUp(self): self.runaction = fife.Action("action001")