21 #include "hx3d/ecs/scene_graph.hpp" 27 SceneGraph::SceneGraph():
28 _root(
Make<Node>(
"/"))
30 _root->_parent =
nullptr;
31 _entityEnabled =
false;
32 _indices[
"/"] = _root;
35 SceneGraph::~SceneGraph() {}
38 _entityEnabled = enabled;
46 return _root->getChildCount();
50 const std::string& path =
object->getPath();
51 for (
const auto& pair: _indices) {
52 if (pair.first == object->_name) {
53 Log.
Error(
"SceneGraph: Index `%s` already exists !", path.c_str());
58 _indices[path] = object;
63 for (
const auto& pair: _indices) {
64 Log.
Info(
"\t%s: %s", pair.first.c_str(), pair.second->_name.c_str());
69 std::stack<Ptr<Node>> stack;
72 while (!stack.empty()) {
78 for (
const Ptr<Node>& child: node->_children) {
85 std::stack<Ptr<Node>> stack;
88 while (!stack.empty()) {
94 for (
const Ptr<Node>& child: node->_children) {
100 _engine.update(delta);
108 if (_indices.find(path) == _indices.end()) {
109 Log.
Error(
"SceneGraph: Index `%s` does not exists.", path.c_str());
114 Log.
Error(
"SceneGraph: Root object can not be removed.");
118 internalRemove(_indices[path]);
122 std::string path = node->getPath();
125 for (
const Ptr<Node>& child: node->_children) {
126 remove(child->getPath());
129 for (
unsigned int i = 0; i < parent->_children.size(); ++i) {
130 if (parent->_children[i] == node) {
131 parent->_children.erase(parent->_children.begin() + i);
135 if (_entityEnabled) {
136 _engine.removeEntity(node);
139 _indices.erase(path);
143 if (path.size() == 0 || path[0] !=
'/') {
144 Log.
Error(
"SceneGraph: ill-formed path: `%s`. Must start with `/`", path.c_str());
148 std::vector<std::string> folders =
split(path,
'/');
149 folders.erase(folders.begin());
152 while (folders.size() > 0) {
153 const std::string folder = folders[0];
155 if (!node->childNameExists(folder)) {
159 node = node->template getChild<Node>(folder);
160 folders.erase(folders.begin());
void showIndices()
Show the graph indices.
void setEntityMode(bool enabled)
Activate/Deactivate the entity management mode.
void remove(const std::string path)
Remove a game object from a path.
void addIndex(const Ptr< Node > &object)
Add an index to the graph.
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Split a string using a delimiter and a container.
void internalRemove(const Ptr< Node > &node)
Remove a node.
Ptr< Node > pathExists(const std::string path)
Test if the path exists and returns the node.
void Error(const std::string fmt,...)
Write an error message.
Ptr< Node > getRoot()
Get the root.
static hx3d::LogImpl Log
Current log implementation.
void draw(graphics::BaseBatch &batch)
Draw the nodes.
unsigned int getNodeCount()
Get the number of nodes.
Draw meshes and texts on screen.
Ptr< T > Make(Args &&...args)
Quick-typing make shared.
void update(const float delta)
Update the nodes.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
void Info(const std::string fmt,...)
Write an info message.