00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "util/structures/purge.h"
00031 #include "model/metamodel/abstractpather.h"
00032 #include "model/metamodel/object.h"
00033 #include "model/metamodel/grids/cellgrid.h"
00034 #include "structures/map.h"
00035 #include "structures/layer.h"
00036 #include "structures/instance.h"
00037 #include "util/base/exception.h"
00038 #include "view/rendererbase.h"
00039 #include "video/renderbackend.h"
00040 #include "video/imagepool.h"
00041 #include "video/animationpool.h"
00042
00043 #include "model.h"
00044
00045 namespace FIFE {
00046
00047 Model::Model(RenderBackend* renderbackend, const std::vector<RendererBase*>& renderers,
00048 ImagePool* imagepool, AnimationPool* animpool):
00049 FifeClass(),
00050 m_last_namespace(NULL),
00051 m_renderbackend(renderbackend),
00052 m_renderers(renderers),
00053 m_imagepool(imagepool),
00054 m_animpool(animpool),
00055 m_timeprovider(NULL) {
00056
00057 }
00058
00059 Model::~Model() {
00060 purge(m_maps);
00061 for(std::list<namespace_t>::iterator nspace = m_namespaces.begin(); nspace != m_namespaces.end(); ++nspace)
00062 purge_map(nspace->second);
00063 purge(m_pathers);
00064 purge(m_created_grids);
00065 purge(m_adopted_grids);
00066 }
00067
00068 Map* Model::createMap(const std::string& identifier) {
00069 std::list<Map*>::const_iterator it = m_maps.begin();
00070 for(; it != m_maps.end(); ++it) {
00071 if(identifier == (*it)->getId()) {
00072 throw NameClash(identifier);
00073 }
00074 }
00075
00076 Map* map = new Map(identifier, m_renderbackend, m_renderers, m_imagepool, m_animpool, &m_timeprovider);
00077 m_maps.push_back(map);
00078 return map;
00079 }
00080
00081 void Model::adoptPather(AbstractPather* pather) {
00082 m_pathers.push_back(pather);
00083 }
00084
00085 AbstractPather* Model::getPather(const std::string& pathername) {
00086 std::vector<AbstractPather*>::const_iterator it = m_pathers.begin();
00087 for(; it != m_pathers.end(); ++it) {
00088 if ((*it)->getName() == pathername) {
00089 return *it;
00090 }
00091 }
00092 return NULL;
00093 }
00094
00095 void Model::adoptCellGrid(CellGrid* grid) {
00096 m_adopted_grids.push_back(grid);
00097 }
00098
00099 CellGrid* Model::getCellGrid(const std::string& gridtype) {
00100 std::vector<CellGrid*>::const_iterator it = m_adopted_grids.begin();
00101 for(; it != m_adopted_grids.end(); ++it) {
00102 if ((*it)->getType() == gridtype) {
00103 CellGrid* newcg = (*it)->clone();
00104 m_created_grids.push_back(newcg);
00105 return newcg;
00106 }
00107 }
00108 return NULL;
00109 }
00110
00111
00112 Map* Model::getMap(const std::string& identifier) const {
00113 std::list<Map*>::const_iterator it = m_maps.begin();
00114 for(; it != m_maps.end(); ++it) {
00115 if((*it)->getId() == identifier)
00116 return *it;
00117 }
00118
00119 throw NotFound(std::string("Tried to get non-existent map: ") + identifier + ".");
00120 }
00121
00122 void Model::deleteMap(Map* map) {
00123 std::list<Map*>::iterator it = m_maps.begin();
00124 for(; it != m_maps.end(); ++it) {
00125 if(*it == map) {
00126 delete *it;
00127 m_maps.erase(it);
00128 return ;
00129 }
00130 }
00131 }
00132
00133 size_t Model::getNumMaps() const {
00134 return m_maps.size();
00135 }
00136
00137 void Model::deleteMaps() {
00138 purge(m_maps);
00139 m_maps.clear();
00140 }
00141
00142 std::list<std::string> Model::getNamespaces() const {
00143 std::list<std::string> namespace_list;
00144 std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
00145 for(; nspace != m_namespaces.end(); ++nspace) {
00146 namespace_list.push_back(nspace->first);
00147 }
00148 return namespace_list;
00149 }
00150
00151 Object* Model::createObject(const std::string& identifier, const std::string& name_space, Object* parent) {
00152
00153 namespace_t* nspace = selectNamespace(name_space);
00154 if(!nspace) {
00155 m_namespaces.push_back(namespace_t(name_space,objectmap_t()));
00156 nspace = selectNamespace(name_space);
00157 }
00158
00159
00160 objectmap_t::const_iterator it = nspace->second.find(identifier);
00161 if( it != nspace->second.end() ) {
00162 throw NameClash(identifier);
00163 }
00164
00165
00166 Object* object = new Object(identifier, name_space, parent);
00167 nspace->second[identifier] = object;
00168 return object;
00169 }
00170
00171 bool Model::deleteObject(Object* object) {
00172
00173
00174
00175 std::list<Layer*>::const_iterator jt;
00176 std::vector<Instance*>::const_iterator kt;
00177 for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) {
00178 for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) {
00179 for(kt = (*jt)->getInstances().begin(); kt != (*jt)->getInstances().end(); ++kt) {
00180 Object* o = (*kt)->getObject();
00181 if(o == object) {
00182 return false;
00183 }
00184 }
00185 }
00186 }
00187
00188
00189 namespace_t* nspace = selectNamespace(object->getNamespace());
00190 if(!nspace)
00191 return true;
00192
00193
00194 objectmap_t::iterator it = nspace->second.find(object->getId());
00195 if( it != nspace->second.end()) {
00196 delete it->second;
00197 nspace->second.erase(it);
00198 }
00199
00200 return true;
00201 }
00202
00203 bool Model::deleteObjects() {
00204
00205 std::list<Layer*>::const_iterator jt;
00206 for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) {
00207 for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) {
00208 if((*jt)->hasInstances())
00209 return false;
00210 }
00211 }
00212
00213
00214 std::list<namespace_t>::iterator nspace = m_namespaces.begin();
00215 while(nspace != m_namespaces.end()) {
00216 objectmap_t::iterator it = nspace->second.begin();
00217 for(; it != nspace->second.end(); ++it) {
00218 delete it->second;
00219 }
00220 nspace = m_namespaces.erase(nspace);
00221 }
00222 m_last_namespace = 0;
00223 return true;
00224 }
00225
00226 Object* Model::getObject(const std::string& id, const std::string& name_space) {
00227 namespace_t* nspace = selectNamespace(name_space);
00228 if(nspace) {
00229 objectmap_t::iterator it = nspace->second.find(id);
00230 if( it != nspace->second.end() )
00231 return it->second;
00232 }
00233 return 0;
00234 }
00235
00236 std::list<Object*> Model::getObjects(const std::string& name_space) const {
00237 std::list<Object*> object_list;
00238 const namespace_t* nspace = selectNamespace(name_space);
00239 if(nspace) {
00240 objectmap_t::const_iterator it = nspace->second.begin();
00241 for(; it != nspace->second.end(); ++it )
00242 object_list.push_back(it->second);
00243 return object_list;
00244 }
00245 throw NotFound(name_space);
00246 }
00247
00248 const Model::namespace_t* Model::selectNamespace(const std::string& name_space) const {
00249 std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
00250 for(; nspace != m_namespaces.end(); ++nspace) {
00251 if( nspace->first == name_space ) {
00252 return &(*nspace);
00253 }
00254 }
00255 return 0;
00256 }
00257
00258 Model::namespace_t* Model::selectNamespace(const std::string& name_space) {
00259 if( m_last_namespace && m_last_namespace->first == name_space )
00260 return m_last_namespace;
00261 std::list<namespace_t>::iterator nspace = m_namespaces.begin();
00262 for(; nspace != m_namespaces.end(); ++nspace) {
00263 if( nspace->first == name_space ) {
00264 m_last_namespace = &(*nspace);
00265 return m_last_namespace;
00266 }
00267 }
00268 m_last_namespace = 0;
00269 return 0;
00270 }
00271
00272 void Model::update() {
00273 std::list<Map*>::iterator it = m_maps.begin();
00274 for(; it != m_maps.end(); ++it) {
00275 (*it)->update();
00276 }
00277 std::vector<AbstractPather*>::iterator jt = m_pathers.begin();
00278 for(; jt != m_pathers.end(); ++jt) {
00279 (*jt)->update();
00280 }
00281 }
00282
00283 }
00284