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
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 }