1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/***************************************************************************
* Copyright (C) 2005-2013 by the FIFE team *
* http://www.fifengine.net *
* This file is part of FIFE. *
* *
* FIFE is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
// Standard C++ library includes
// 3rd party library includes
// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "util/structures/purge.h"
#include "util/log/logger.h"
#include "model/metamodel/ipather.h"
#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 "view/rendererbase.h"
#include "video/renderbackend.h"
#include "model.h"
namespace FIFE {
static Logger _log(LM_MODEL);
Model::Model(RenderBackend* renderbackend, const std::vector<RendererBase*>& renderers)
: FifeClass(),
m_last_namespace(NULL),
m_timeprovider(NULL),
m_renderbackend(renderbackend),
m_renderers(renderers){
}
Model::~Model() {
purge(m_maps);
for(std::list<namespace_t>::iterator nspace = m_namespaces.begin(); nspace != m_namespaces.end(); ++nspace)
purge_map(nspace->second);
purge(m_pathers);
purge(m_created_grids);
purge(m_adopted_grids);
}
Map* Model::createMap(const std::string& identifier) {
std::list<Map*>::const_iterator it = m_maps.begin();
for(; it != m_maps.end(); ++it) {
if(identifier == (*it)->getId()) {
throw NameClash(identifier);
}
}
Map* map = new Map(identifier, m_renderbackend, m_renderers, &m_timeprovider);
m_maps.push_back(map);
return map;
}
void Model::adoptPather(IPather* pather) {
m_pathers.push_back(pather);
}
IPather* Model::getPather(const std::string& pathername) {
std::vector<IPather*>::const_iterator it = m_pathers.begin();
for(; it != m_pathers.end(); ++it) {
if ((*it)->getName() == pathername) {
return *it;
}
}
FL_WARN(_log, "No pather of requested type \"" + pathername + "\" found.");
return NULL;
}
void Model::adoptCellGrid(CellGrid* grid) {
m_adopted_grids.push_back(grid);
}
CellGrid* Model::getCellGrid(const std::string& gridtype) {
std::vector<CellGrid*>::const_iterator it = m_adopted_grids.begin();
for(; it != m_adopted_grids.end(); ++it) {
if ((*it)->getType() == gridtype) {
CellGrid* newcg = (*it)->clone();
m_created_grids.push_back(newcg);
return newcg;
}
}
FL_WARN(_log, "No cellgrid of requested type \"" + gridtype + "\" found.");
return NULL;
}
Map* Model::getMap(const std::string& identifier) const {
std::list<Map*>::const_iterator it = m_maps.begin();
for(; it != m_maps.end(); ++it) {
if((*it)->getId() == identifier)
return *it;
}
throw NotFound(std::string("Tried to get non-existent map: ") + identifier + ".");
}
void Model::deleteMap(Map* map) {
std::list<Map*>::iterator it = m_maps.begin();
for(; it != m_maps.end(); ++it) {
if(*it == map) {
delete *it;
m_maps.erase(it);
return ;
}
}
}
uint32_t Model::getMapCount() const {
return m_maps.size();
}
void Model::deleteMaps() {
purge(m_maps);
m_maps.clear();
}
std::list<std::string> Model::getNamespaces() const {
std::list<std::string> namespace_list;
std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
for(; nspace != m_namespaces.end(); ++nspace) {
namespace_list.push_back(nspace->first);
}
return namespace_list;
}
Object* Model::createObject(const std::string& identifier, const std::string& name_space, Object* parent) {
// Find or create namespace
namespace_t* nspace = selectNamespace(name_space);
if(!nspace) {
m_namespaces.push_back(namespace_t(name_space,objectmap_t()));
nspace = selectNamespace(name_space);
}
// Check for nameclashes
objectmap_t::const_iterator it = nspace->second.find(identifier);
if( it != nspace->second.end() ) {
throw NameClash(identifier);
}
// Finally insert & create
Object* object = new Object(identifier, name_space, parent);
nspace->second[identifier] = object;
return object;
}
bool Model::deleteObject(Object* object) {
// WARNING: This code has obviously not been tested (thoroughly).
// Check if any instances exist. If yes - bail out.
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();
if(o == object) {
return false;
}
}
}
}
// Check if the namespace exists
namespace_t* nspace = selectNamespace(object->getNamespace());
if(!nspace)
return true;
// If yes - delete+erase object.
objectmap_t::iterator it = nspace->second.find(object->getId());
if( it != nspace->second.end()) {
delete it->second;
nspace->second.erase(it);
}
return true;
}
bool Model::deleteObjects() {
// If we have layers with instances - bail out.
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;
}
}
// Otherwise delete every object in every namespace
std::list<namespace_t>::iterator nspace = m_namespaces.begin();
while(nspace != m_namespaces.end()) {
objectmap_t::iterator it = nspace->second.begin();
for(; it != nspace->second.end(); ++it) {
delete it->second;
}
nspace = m_namespaces.erase(nspace);
}
m_last_namespace = 0;
return true;
}
Object* Model::getObject(const std::string& id, const std::string& name_space) {
namespace_t* nspace = selectNamespace(name_space);
if(nspace) {
objectmap_t::iterator it = nspace->second.find(id);
if( it != nspace->second.end() )
return it->second;
}
return NULL;
}
std::list<Object*> Model::getObjects(const std::string& name_space) const {
std::list<Object*> object_list;
const namespace_t* nspace = selectNamespace(name_space);
if(nspace) {
objectmap_t::const_iterator it = nspace->second.begin();
for(; it != nspace->second.end(); ++it )
object_list.push_back(it->second);
}
return object_list;
}
const Model::namespace_t* Model::selectNamespace(const std::string& name_space) const {
std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
for(; nspace != m_namespaces.end(); ++nspace) {
if( nspace->first == name_space ) {
return &(*nspace);
}
}
return NULL;
}
Model::namespace_t* Model::selectNamespace(const std::string& name_space) {
if( m_last_namespace && m_last_namespace->first == name_space )
return m_last_namespace;
std::list<namespace_t>::iterator nspace = m_namespaces.begin();
for(; nspace != m_namespaces.end(); ++nspace) {
if( nspace->first == name_space ) {
m_last_namespace = &(*nspace);
return m_last_namespace;
}
}
m_last_namespace = 0;
return NULL;
}
void Model::update() {
std::list<Map*>::iterator it = m_maps.begin();
for(; it != m_maps.end(); ++it) {
(*it)->update();
}
std::vector<IPather*>::iterator jt = m_pathers.begin();
for(; jt != m_pathers.end(); ++jt) {
(*jt)->update();
}
}
} //FIFE
|