comparison engine/core/model/structures/instancetree.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23
24 // 3rd party library includes
25
26
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "model/structures/instance.h"
32 #include "util/structures/rect.h"
33
34 #include "instancetree.h"
35
36
37 namespace FIFE {
38
39 InstanceTree::InstanceTree(): FifeClass() {
40 }
41
42 InstanceTree::~InstanceTree() {
43 }
44
45 bool InstanceTree::addInstance(Instance* instance) {
46 ModelCoordinate coords = instance->getLocationRef().getLayerCoordinates();
47 InstanceList& list = m_tree.find_container(coords.x,coords.y,0,0)->data();
48 list.push_back(instance);
49 return true;
50 }
51
52 bool InstanceTree::removeInstance(Instance* instance) {
53 ModelCoordinate coords = instance->getLocationRef().getLayerCoordinates();
54 InstanceList& list = m_tree.find_container(coords.x,coords.y, 0, 0)->data();
55
56 for(InstanceList::iterator i = list.begin(); i != list.end(); ++i) {
57 if((*i) == instance) {
58 list.erase(i);
59 return true;
60 }
61 }
62
63 return false;
64 }
65
66 class InstanceListCollector {
67 public:
68 InstanceTree::InstanceList& instanceList;
69 Rect searchRect;
70 InstanceListCollector(InstanceTree::InstanceList& a_instanceList, const Rect& rect)
71 : instanceList(a_instanceList), searchRect(rect) {
72 }
73 bool visit(InstanceTree::InstanceTreeNode* node, int d);
74 };
75
76 bool InstanceListCollector::visit(InstanceTree::InstanceTreeNode* node, int d) {
77 InstanceTree::InstanceList& list = node->data();
78 for(InstanceTree::InstanceList::const_iterator it(list.begin()); it != list.end(); ++it) {
79 ModelCoordinate coords = (*it)->getLocationRef().getLayerCoordinates();
80 if( searchRect.contains(Point(coords.x,coords.y)) ) {
81 instanceList.push_back(*it);
82 }
83 }
84 return true;
85 }
86
87 void InstanceTree::findInstances(const ModelCoordinate& point, int w, int h, InstanceTree::InstanceList& list) {
88 InstanceTreeNode * node = m_tree.find_container(point.x, point.y, w, h);
89 Rect rect(point.x, point.y, w, h);
90 InstanceListCollector collector(list,rect);
91
92 node->apply_visitor(collector);
93
94 node = node->parent();
95 while( node ) {
96 for(InstanceList::const_iterator it(node->data().begin()); it != node->data().end(); ++it) {
97 ModelCoordinate coords = (*it)->getLocationRef().getLayerCoordinates();
98 if( rect.contains(Point(coords.x,coords.y)) ) {
99 list.push_back(*it);
100 }
101 }
102 node = node->parent();
103 }
104 }
105
106 }