mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-16 14:08:59 +07:00
few helper functions for children extracting
This commit is contained in:
@@ -127,15 +127,13 @@ namespace e2d
|
|||||||
|
|
||||||
template < typename F >
|
template < typename F >
|
||||||
void for_each_child(F&& f);
|
void for_each_child(F&& f);
|
||||||
|
|
||||||
template < typename F >
|
template < typename F >
|
||||||
void for_each_child(F&& f) const;
|
void for_each_child(F&& f) const;
|
||||||
|
|
||||||
template < typename Iter >
|
template < typename F >
|
||||||
std::size_t extract_all_nodes(Iter iter);
|
void for_each_child_reversed(F&& f);
|
||||||
|
template < typename F >
|
||||||
template < typename Iter >
|
void for_each_child_reversed(F&& f) const;
|
||||||
std::size_t extract_all_nodes(Iter iter) const;
|
|
||||||
protected:
|
protected:
|
||||||
node() = default;
|
node() = default;
|
||||||
node(gobject owner);
|
node(gobject owner);
|
||||||
@@ -160,4 +158,39 @@ namespace e2d
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace e2d::nodes
|
||||||
|
{
|
||||||
|
template < typename Iter >
|
||||||
|
std::size_t extract_nodes(const node_iptr& root, Iter iter);
|
||||||
|
template < typename Iter >
|
||||||
|
std::size_t extract_nodes(const const_node_iptr& root, Iter iter);
|
||||||
|
|
||||||
|
template < typename Iter >
|
||||||
|
std::size_t extract_nodes_reversed(const node_iptr& root, Iter iter);
|
||||||
|
template < typename Iter >
|
||||||
|
std::size_t extract_nodes_reversed(const const_node_iptr& root, Iter iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace e2d::nodes
|
||||||
|
{
|
||||||
|
vector<node_iptr> extract_nodes(const node_iptr& root);
|
||||||
|
vector<const_node_iptr> extract_nodes(const const_node_iptr& root);
|
||||||
|
|
||||||
|
vector<node_iptr> extract_nodes_reversed(const node_iptr& root);
|
||||||
|
vector<const_node_iptr> extract_nodes_reversed(const const_node_iptr& root);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace e2d::nodes
|
||||||
|
{
|
||||||
|
template < typename F >
|
||||||
|
void for_extracted_nodes(const node_iptr& root, F&& f);
|
||||||
|
template < typename F >
|
||||||
|
void for_extracted_nodes(const const_node_iptr& root, F&& f);
|
||||||
|
|
||||||
|
template < typename F >
|
||||||
|
void for_extracted_nodes_reversed(const node_iptr& root, F&& f);
|
||||||
|
template < typename F >
|
||||||
|
void for_extracted_nodes_reversed(const const_node_iptr& root, F&& f);
|
||||||
|
}
|
||||||
|
|
||||||
#include "node.inl"
|
#include "node.inl"
|
||||||
|
|||||||
@@ -12,35 +12,151 @@ namespace e2d
|
|||||||
{
|
{
|
||||||
template < typename F >
|
template < typename F >
|
||||||
void node::for_each_child(F&& f) {
|
void node::for_each_child(F&& f) {
|
||||||
for ( node& child : children_ ) {
|
for ( auto iter = children_.begin(); iter != children_.end(); ++iter ) {
|
||||||
f(node_iptr(&child));
|
f(node_iptr(&*iter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename F >
|
template < typename F >
|
||||||
void node::for_each_child(F&& f) const {
|
void node::for_each_child(F&& f) const {
|
||||||
for ( const node& child : children_ ) {
|
for ( auto iter = children_.begin(); iter != children_.end(); ++iter ) {
|
||||||
f(const_node_iptr(&child));
|
f(const_node_iptr(&*iter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename F >
|
||||||
|
void node::for_each_child_reversed(F&& f) {
|
||||||
|
for ( auto iter = children_.rbegin(); iter != children_.rend(); ++iter ) {
|
||||||
|
f(node_iptr(&*iter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F >
|
||||||
|
void node::for_each_child_reversed(F&& f) const {
|
||||||
|
for ( auto iter = children_.rbegin(); iter != children_.rend(); ++iter ) {
|
||||||
|
f(const_node_iptr(&*iter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace e2d::nodes
|
||||||
|
{
|
||||||
template < typename Iter >
|
template < typename Iter >
|
||||||
std::size_t node::extract_all_nodes(Iter iter) {
|
std::size_t extract_nodes(const node_iptr& root, Iter iter) {
|
||||||
std::size_t count{1u};
|
std::size_t count{0u};
|
||||||
iter++ = node_iptr(this);
|
if ( root ) {
|
||||||
for_each_child([&iter, &count](const node_iptr& child){
|
++count;
|
||||||
count += child->extract_all_nodes(iter);
|
iter++ = root;
|
||||||
});
|
root->for_each_child([&iter, &count](const node_iptr& child){
|
||||||
|
count += extract_nodes(child, iter);
|
||||||
|
});
|
||||||
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Iter >
|
template < typename Iter >
|
||||||
std::size_t node::extract_all_nodes(Iter iter) const {
|
std::size_t extract_nodes(const const_node_iptr& root, Iter iter) {
|
||||||
std::size_t count{1u};
|
std::size_t count{0u};
|
||||||
iter++ = const_node_iptr(this);
|
if ( root ) {
|
||||||
for_each_child([&iter, &count](const const_node_iptr& child){
|
++count;
|
||||||
count += child->extract_all_nodes(iter);
|
iter++ = root;
|
||||||
});
|
root->for_each_child([&iter, &count](const const_node_iptr& child){
|
||||||
|
count += extract_nodes(child, iter);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Iter >
|
||||||
|
std::size_t extract_nodes_reversed(const node_iptr& root, Iter iter) {
|
||||||
|
std::size_t count{0u};
|
||||||
|
if ( root ) {
|
||||||
|
root->for_each_child_reversed([&iter, &count](const node_iptr& child){
|
||||||
|
count += extract_nodes_reversed(child, iter);
|
||||||
|
});
|
||||||
|
++count;
|
||||||
|
iter++ = root;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Iter >
|
||||||
|
std::size_t extract_nodes_reversed(const const_node_iptr& root, Iter iter) {
|
||||||
|
std::size_t count{0u};
|
||||||
|
if ( root ) {
|
||||||
|
root->for_each_child_reversed([&iter, &count](const const_node_iptr& child){
|
||||||
|
count += extract_nodes_reversed(child, iter);
|
||||||
|
});
|
||||||
|
++count;
|
||||||
|
iter++ = root;
|
||||||
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace e2d::nodes
|
||||||
|
{
|
||||||
|
template < typename F >
|
||||||
|
void for_extracted_nodes(const node_iptr& root, F&& f) {
|
||||||
|
//TODO(BlackMat): replace it to frame allocator
|
||||||
|
static thread_local vector<node_iptr> nodes;
|
||||||
|
try {
|
||||||
|
extract_nodes(root, std::back_inserter(nodes));
|
||||||
|
for ( const node_iptr& n : nodes ) {
|
||||||
|
f(n);
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
nodes.clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
nodes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F >
|
||||||
|
void for_extracted_nodes(const const_node_iptr& root, F&& f) {
|
||||||
|
//TODO(BlackMat): replace it to frame allocator
|
||||||
|
static thread_local vector<const_node_iptr> nodes;
|
||||||
|
try {
|
||||||
|
extract_nodes(root, std::back_inserter(nodes));
|
||||||
|
for ( const const_node_iptr& n : nodes ) {
|
||||||
|
f(n);
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
nodes.clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
nodes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F >
|
||||||
|
void for_extracted_nodes_reversed(const node_iptr& root, F&& f) {
|
||||||
|
//TODO(BlackMat): replace it to frame allocator
|
||||||
|
static thread_local vector<node_iptr> nodes;
|
||||||
|
try {
|
||||||
|
extract_nodes_reversed(root, std::back_inserter(nodes));
|
||||||
|
for ( const node_iptr& n : nodes ) {
|
||||||
|
f(n);
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
nodes.clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
nodes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename F >
|
||||||
|
void for_extracted_nodes_reversed(const const_node_iptr& root, F&& f) {
|
||||||
|
//TODO(BlackMat): replace it to frame allocator
|
||||||
|
static thread_local vector<const_node_iptr> nodes;
|
||||||
|
try {
|
||||||
|
extract_nodes_reversed(root, std::back_inserter(nodes));
|
||||||
|
for ( const const_node_iptr& n : nodes ) {
|
||||||
|
f(n);
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
nodes.clear();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
nodes.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -439,3 +439,30 @@ namespace e2d
|
|||||||
: local_matrix();
|
: local_matrix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace e2d::nodes
|
||||||
|
{
|
||||||
|
vector<node_iptr> extract_nodes(const node_iptr& root) {
|
||||||
|
vector<node_iptr> nodes;
|
||||||
|
extract_nodes(root, std::back_inserter(nodes));
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<const_node_iptr> extract_nodes(const const_node_iptr& root) {
|
||||||
|
vector<const_node_iptr> nodes;
|
||||||
|
extract_nodes(root, std::back_inserter(nodes));
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<node_iptr> extract_nodes_reversed(const node_iptr& root) {
|
||||||
|
vector<node_iptr> nodes;
|
||||||
|
extract_nodes_reversed(root, std::back_inserter(nodes));
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<const_node_iptr> extract_nodes_reversed(const const_node_iptr& root) {
|
||||||
|
vector<const_node_iptr> nodes;
|
||||||
|
extract_nodes_reversed(root, std::back_inserter(nodes));
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,23 +19,6 @@ namespace
|
|||||||
using namespace e2d;
|
using namespace e2d;
|
||||||
using namespace e2d::render_system_impl;
|
using namespace e2d::render_system_impl;
|
||||||
|
|
||||||
template < typename F >
|
|
||||||
void for_each_by_nodes(const const_node_iptr& root, F&& f) {
|
|
||||||
static vector<const_node_iptr> temp_nodes;
|
|
||||||
try {
|
|
||||||
if ( root ) {
|
|
||||||
root->extract_all_nodes(std::back_inserter(temp_nodes));
|
|
||||||
for ( const const_node_iptr& node : temp_nodes ) {
|
|
||||||
f(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (...) {
|
|
||||||
temp_nodes.clear();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
temp_nodes.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
template < typename T, typename Comp, typename F >
|
template < typename T, typename Comp, typename F >
|
||||||
void for_each_by_sorted_components(ecs::registry& owner, Comp&& comp, F&& f) {
|
void for_each_by_sorted_components(ecs::registry& owner, Comp&& comp, F&& f) {
|
||||||
static vector<std::pair<ecs::const_entity,T>> temp_components;
|
static vector<std::pair<ecs::const_entity,T>> temp_components;
|
||||||
@@ -67,7 +50,7 @@ namespace
|
|||||||
const auto func = [&ctx](const ecs::const_entity& scn_e, const scene&) {
|
const auto func = [&ctx](const ecs::const_entity& scn_e, const scene&) {
|
||||||
const actor* scn_a = scn_e.find_component<actor>();
|
const actor* scn_a = scn_e.find_component<actor>();
|
||||||
if ( scn_a && scn_a->node() ) {
|
if ( scn_a && scn_a->node() ) {
|
||||||
for_each_by_nodes(scn_a->node(), [&ctx](const const_node_iptr& node){
|
nodes::for_extracted_nodes(scn_a->node(), [&ctx](const const_node_iptr& node){
|
||||||
ctx.draw(node);
|
ctx.draw(node);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user