From db99cd71d3f066ea22632c1dfbf126ed414afc17 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 22 Apr 2020 08:53:01 +0700 Subject: [PATCH] node: child_index function --- headers/enduro2d/high/node.hpp | 3 ++ .../bin/library/scripts/emmy/high/node.lua | 5 +++ .../high/bindings/high_binds/node_binds.cpp | 4 ++ sources/enduro2d/high/node.cpp | 14 +++++++ untests/sources/untests_high/node.cpp | 42 +++++++++++++++++++ 5 files changed, 68 insertions(+) diff --git a/headers/enduro2d/high/node.hpp b/headers/enduro2d/high/node.hpp index d1e40f3e..ffcb4da0 100644 --- a/headers/enduro2d/high/node.hpp +++ b/headers/enduro2d/high/node.hpp @@ -129,6 +129,9 @@ namespace e2d node_iptr child_at(std::size_t index) noexcept; const_node_iptr child_at(std::size_t index) const noexcept; + std::pair child_index( + const const_node_iptr& child) const noexcept; + node_iptr remove_child_at(std::size_t index) noexcept; protected: node() = default; diff --git a/samples/bin/library/scripts/emmy/high/node.lua b/samples/bin/library/scripts/emmy/high/node.lua index f60010d5..31f23034 100644 --- a/samples/bin/library/scripts/emmy/high/node.lua +++ b/samples/bin/library/scripts/emmy/high/node.lua @@ -145,6 +145,11 @@ function node.bring_to_front(self) end ---@return node function node.child_at(self, index) end +---@param self node +---@param child node +---@return integer, boolean +function node.child_index(self, child) end + ---@param self node ---@param index integer ---@return node diff --git a/sources/enduro2d/high/bindings/high_binds/node_binds.cpp b/sources/enduro2d/high/bindings/high_binds/node_binds.cpp index d1bd51fd..240b9378 100644 --- a/sources/enduro2d/high/bindings/high_binds/node_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/node_binds.cpp @@ -149,6 +149,10 @@ namespace e2d::bindings::high : node_iptr(); }, + "child_index", [](node& n, const node_iptr& c) -> std::pair { + return n.child_index(c); + }, + "remove_child_at", [](node& n, i32 index) -> node_iptr { return index >= 0 ? n.remove_child_at(math::numeric_cast(index)) diff --git a/sources/enduro2d/high/node.cpp b/sources/enduro2d/high/node.cpp index fd41bb1f..ae96c15e 100644 --- a/sources/enduro2d/high/node.cpp +++ b/sources/enduro2d/high/node.cpp @@ -431,6 +431,20 @@ namespace e2d return child; } + std::pair node::child_index( + const const_node_iptr& child) const noexcept + { + if ( !child || child->parent_ != this ) { + return {std::size_t(-1), false}; + } + + const auto distance = std::distance( + children_.begin(), + node_children::iterator_to(*child)); + + return {math::numeric_cast(distance), true}; + } + node_iptr node::remove_child_at(std::size_t index) noexcept { node_iptr child = child_at(index); return remove_child(child) diff --git a/untests/sources/untests_high/node.cpp b/untests/sources/untests_high/node.cpp index 9bde1862..3b373524 100644 --- a/untests/sources/untests_high/node.cpp +++ b/untests/sources/untests_high/node.cpp @@ -450,6 +450,48 @@ TEST_CASE("node") { REQUIRE_FALSE(cp->child_at(3)); } } + SECTION("child_index") { + auto p = node::create(); + + auto n1 = node::create(p); + auto n2 = node::create(p); + auto n3 = node::create(p); + + auto p2 = node::create(); + auto n4 = node::create(p2); + + auto n5 = node::create(); + + REQUIRE(p->child_index(n1).second); + REQUIRE(p->child_index(n2).second); + REQUIRE(p->child_index(n3).second); + REQUIRE_FALSE(p->child_index(n4).second); + REQUIRE_FALSE(p->child_index(n5).second); + + REQUIRE(p->child_index(n1).first == 0u); + REQUIRE(p->child_index(n2).first == 1u); + REQUIRE(p->child_index(n3).first == 2u); + + { + const_node_iptr cp = p; + + const_node_iptr cn1 = n1; + const_node_iptr cn2 = n2; + const_node_iptr cn3 = n3; + const_node_iptr cn4 = n4; + const_node_iptr cn5 = n5; + + REQUIRE(cp->child_index(cn1).second); + REQUIRE(cp->child_index(cn2).second); + REQUIRE(cp->child_index(cn3).second); + REQUIRE_FALSE(cp->child_index(cn4).second); + REQUIRE_FALSE(cp->child_index(cn5).second); + + REQUIRE(cp->child_index(cn1).first == 0u); + REQUIRE(cp->child_index(cn2).first == 1u); + REQUIRE(cp->child_index(cn3).first == 2u); + } + } SECTION("remove_child_at") { auto p = node::create();