14 |
|
|
15 |
#include "scene-manager.hpp" |
#include "scene-manager.hpp" |
16 |
#include <boost/bind.hpp> |
#include <boost/bind.hpp> |
17 |
|
#include <utility> |
18 |
|
#include <functional> |
19 |
#include <map> |
#include <map> |
20 |
|
#include <vector> |
21 |
#include <iostream> |
#include <iostream> |
22 |
namespace |
namespace |
23 |
usata |
usata |
24 |
{ |
{ |
25 |
|
|
|
|
|
26 |
namespace SM_internal |
namespace SM_internal |
27 |
{ |
{ |
28 |
|
template <typename compare=std::less<int> > |
29 |
|
struct DrawOrderCompare : public std::binary_function<Object*,Object*, bool> |
30 |
|
{ |
31 |
|
|
32 |
|
bool |
33 |
|
operator()(Object * lhs, Object*rhs) |
34 |
|
{ |
35 |
|
NodeInterface * ni = dynamic_cast<NodeInterface*>(lhs); |
36 |
|
int lhs_do = USATA_DRAW_ORDER_DEFAULT; |
37 |
|
int rhs_do = USATA_DRAW_ORDER_DEFAULT; |
38 |
|
if (ni) |
39 |
|
ni->query(Node::QUERY_DRAW_ORDER,lhs_do); |
40 |
|
|
41 |
|
ni = dynamic_cast<NodeInterface*>(rhs); |
42 |
|
if (ni) |
43 |
|
ni->query(Node::QUERY_DRAW_ORDER,rhs_do); |
44 |
|
|
45 |
|
return compare()(lhs_do, rhs_do); |
46 |
|
} |
47 |
|
|
48 |
|
}; |
49 |
|
|
50 |
struct Impl |
struct Impl |
51 |
{ |
{ |
53 |
enum AddStatus { OK, DUPLICATE_NAME, BAD_NAME }; |
enum AddStatus { OK, DUPLICATE_NAME, BAD_NAME }; |
54 |
|
|
55 |
//! \todo make this optionaly a hash_map instead |
//! \todo make this optionaly a hash_map instead |
56 |
typedef std::map<std::string, Object_sp> ChildrenMap; |
typedef std::vector<Object_sp> DrawList; |
57 |
ChildrenMap children; |
|
58 |
|
DrawList drawlist; |
59 |
Object* get_ptr(const std::string&); |
Object* get_ptr(const std::string&); |
60 |
|
|
61 |
void draw() |
void draw() |
62 |
{ |
{ |
63 |
using boost::bind; |
using boost::bind; |
64 |
std::for_each(children.begin(), children.end(), |
std::for_each(drawlist.begin(),drawlist.end(), |
65 |
bind(&Object::draw, bind(&Object_sp::get, |
bind(&Object::draw, bind (&Object_sp::get, _1))); |
|
bind(&ChildrenMap::value_type::second, _1)))); |
|
66 |
} |
} |
67 |
|
|
68 |
|
|
69 |
void update() |
void update() |
70 |
{ |
{ |
71 |
using std::for_each; |
using std::for_each; |
72 |
using boost::bind; |
using boost::bind; |
73 |
for_each(children.begin(), children.end(), |
|
74 |
bind(&Object::update, |
for_each(drawlist.begin(), drawlist.end(), |
75 |
bind(&Object_sp::get, |
bind(&Object::update, bind(&Object_sp::get,_1))); |
76 |
bind(&ChildrenMap::value_type::second,_1) |
return; |
|
) |
|
|
)); |
|
|
|
|
77 |
} |
} |
78 |
|
|
79 |
AddStatus add(const Object_sp& obj) |
AddStatus add(const Object_sp& obj) |
80 |
{ |
{ |
81 |
ChildrenMap::iterator it( children.find(obj->name())); |
|
82 |
if (it != children.end()) |
DrawList::iterator it= |
83 |
{ |
std::find_if(drawlist.begin(), drawlist.end(), |
84 |
return DUPLICATE_NAME; |
boost::bind(std::equal_to<std::string>(), |
85 |
} |
obj->name(), |
86 |
|
boost::bind(&Object::name, boost::bind(&Object_sp::get, _1)) |
87 |
|
)); |
88 |
|
|
89 |
//! \todo verify valid name |
//! \todo verify valid name |
90 |
children.insert(std::make_pair(obj->name(), obj)); |
drawlist.push_back(obj); |
91 |
|
|
92 |
|
// sorting here is acceptable because there are probably always only going to be 2 |
93 |
|
// children so, a check every cycle whether the list needs to be sorted would be |
94 |
|
// less efficiant. |
95 |
|
std::sort(drawlist.begin(), drawlist.end(), |
96 |
|
boost::bind(DrawOrderCompare<>(), |
97 |
|
boost::bind(&Object_sp::get,_1), |
98 |
|
boost::bind(&Object_sp::get,_2))); |
99 |
|
|
100 |
return OK; |
return OK; |
101 |
} |
} |
102 |
|
|
103 |
|
DrawList::iterator |
104 |
|
find(const std::string& name) |
105 |
|
{ |
106 |
|
DrawList::iterator retval= |
107 |
|
std::find_if(drawlist.begin(), drawlist.end(), |
108 |
|
boost::bind(std::equal_to<std::string>(), |
109 |
|
name, |
110 |
|
boost::bind(&Object::name, boost::bind(&Object_sp::get, _1)) |
111 |
|
)); |
112 |
|
return retval; |
113 |
|
} |
114 |
|
|
115 |
Object* |
Object* |
116 |
lookup_ptr(const std::string& name) |
lookup_ptr(const std::string& name) |
117 |
{ |
{ |
118 |
|
using boost::bind; |
119 |
Object* retval=0; |
Object* retval=0; |
120 |
ChildrenMap::iterator it(children.find(name)); |
DrawList::iterator it = |
121 |
if (it != children.end()) |
std::find_if(drawlist.begin(), |
122 |
{ |
drawlist.end(), |
123 |
retval=it->second.get(); |
bind(std::equal_to<std::string>(), name, |
124 |
} |
bind(&Object::name, bind( &Object_sp::get, _1)))); |
125 |
|
if (it != drawlist.end()) |
126 |
|
retval = it->get(); |
127 |
return retval; |
return retval; |
128 |
} |
} |
129 |
Object_sp |
Object_sp |
130 |
lookup(const std::string& name) |
lookup(const std::string& name) |
131 |
{ |
{ |
132 |
Object_sp retval; |
Object_sp retval; |
133 |
ChildrenMap::iterator it(children.find(name)); |
DrawList::iterator it(find(name)); |
134 |
if (it != children.end()) |
if (it != drawlist.end()) |
135 |
{ |
{ |
136 |
retval = it->second; |
retval = *it; |
137 |
} |
} |
138 |
return retval; |
return retval; |
139 |
} |
} |
152 |
SceneManager::SceneManager() |
SceneManager::SceneManager() |
153 |
: impl(new Impl) |
: impl(new Impl) |
154 |
{ |
{ |
155 |
|
name("SceneManager"); |
156 |
} |
} |
157 |
|
|
158 |
SceneManager::~SceneManager() |
SceneManager::~SceneManager() |
182 |
std::string cpy(path); |
std::string cpy(path); |
183 |
std::string |
std::string |
184 |
name=NodeInterface::NodePathPop(cpy); |
name=NodeInterface::NodePathPop(cpy); |
185 |
|
|
186 |
Object *child = impl->lookup_ptr(name); |
Object *child = impl->lookup_ptr(name); |
187 |
if (child == 0) |
if (child == 0) |
188 |
{ |
{ |