instancetree.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "util/base/exception.h"
00032 #include "model/structures/instance.h"
00033 #include "util/structures/rect.h"
00034 
00035 #include "instancetree.h"
00036 
00037 
00038 namespace FIFE {
00039 
00040     InstanceTree::InstanceTree(): FifeClass() {
00041     }
00042 
00043     InstanceTree::~InstanceTree() {
00044     }
00045 
00046     void InstanceTree::addInstance(Instance* instance) {
00047         ModelCoordinate coords = instance->getLocationRef().getLayerCoordinates();
00048         InstanceTreeNode * node = m_tree.find_container(coords.x,coords.y,0,0);
00049         InstanceList& list = node->data();
00050         list.push_back(instance);
00051         if( m_reverse.find(instance) != m_reverse.end() )
00052             throw new InconsistencyDetected("Duplicate Instance.");
00053         m_reverse[instance] = node;
00054     }
00055 
00056     void InstanceTree::removeInstance(Instance* instance) {
00057         ModelCoordinate coords = instance->getLocationRef().getLayerCoordinates();
00058         InstanceTreeNode * node = m_reverse[instance];
00059         if( !node )
00060             throw new InconsistencyDetected("Removing Ghost Instance.");
00061         m_reverse.erase(instance);
00062         InstanceList& list = node->data();
00063         for(InstanceList::iterator i = list.begin(); i != list.end(); ++i) {
00064             if((*i) == instance) {
00065                 list.erase(i);
00066                 return;
00067             }
00068         }
00069         throw new InconsistencyDetected("Removing Ghost Instance (not in list?).");
00070     }
00071 
00072     class InstanceListCollector {
00073         public:
00074             InstanceTree::InstanceList& instanceList;
00075             Rect searchRect;
00076             InstanceListCollector(InstanceTree::InstanceList& a_instanceList, const Rect& rect)
00077             : instanceList(a_instanceList), searchRect(rect) {
00078             }
00079             bool visit(InstanceTree::InstanceTreeNode* node, int d);
00080     };
00081 
00082     bool InstanceListCollector::visit(InstanceTree::InstanceTreeNode* node, int d) {
00083         InstanceTree::InstanceList& list = node->data();
00084         for(InstanceTree::InstanceList::const_iterator it(list.begin()); it != list.end(); ++it) {
00085             ModelCoordinate coords = (*it)->getLocationRef().getLayerCoordinates();
00086             if( searchRect.contains(Point(coords.x,coords.y)) ) {
00087                 instanceList.push_back(*it);
00088             }
00089         }
00090         return true;
00091     }
00092 
00093     void InstanceTree::findInstances(const ModelCoordinate& point, int w, int h, InstanceTree::InstanceList& list) {
00094         InstanceTreeNode * node = m_tree.find_container(point.x, point.y, w, h);
00095         Rect rect(point.x, point.y, w, h);
00096         InstanceListCollector collector(list,rect);
00097 
00098         node->apply_visitor(collector);
00099 
00100         node = node->parent();
00101         while( node ) {
00102             for(InstanceList::const_iterator it(node->data().begin()); it != node->data().end(); ++it) {
00103                 ModelCoordinate coords = (*it)->getLocationRef().getLayerCoordinates();
00104                 if( rect.contains(Point(coords.x,coords.y)) ) {
00105                     list.push_back(*it);
00106                 }
00107             }
00108             node = node->parent();
00109         }
00110     }
00111 
00112 }
Generated on Wed Nov 23 13:04:43 2011 for FIFE by  doxygen 1.6.3