From 969180f3db83d0596c04cf662d657e4b19961175 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 22 Apr 2020 08:20:53 +0700 Subject: [PATCH] node: remove_child_at function --- headers/enduro2d/high/node.hpp | 2 ++ .../bin/library/scripts/emmy/high/node.lua | 5 +++++ .../high/bindings/high_binds/node_binds.cpp | 6 ++++++ sources/enduro2d/high/node.cpp | 7 +++++++ untests/sources/untests_high/node.cpp | 21 +++++++++++++++++++ 5 files changed, 41 insertions(+) diff --git a/headers/enduro2d/high/node.hpp b/headers/enduro2d/high/node.hpp index 6890ddc5..d1e40f3e 100644 --- a/headers/enduro2d/high/node.hpp +++ b/headers/enduro2d/high/node.hpp @@ -128,6 +128,8 @@ namespace e2d node_iptr child_at(std::size_t index) noexcept; const_node_iptr child_at(std::size_t index) const noexcept; + + node_iptr remove_child_at(std::size_t index) noexcept; protected: node() = default; node(gobject owner); diff --git a/samples/bin/library/scripts/emmy/high/node.lua b/samples/bin/library/scripts/emmy/high/node.lua index 00df09ed..f60010d5 100644 --- a/samples/bin/library/scripts/emmy/high/node.lua +++ b/samples/bin/library/scripts/emmy/high/node.lua @@ -145,5 +145,10 @@ function node.bring_to_front(self) end ---@return node function node.child_at(self, index) end +---@param self node +---@param index integer +---@return node +function node.remove_child_at(self, index) end + ---@type node _G.node = _G.node or node diff --git a/sources/enduro2d/high/bindings/high_binds/node_binds.cpp b/sources/enduro2d/high/bindings/high_binds/node_binds.cpp index a765eaf2..d1bd51fd 100644 --- a/sources/enduro2d/high/bindings/high_binds/node_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/node_binds.cpp @@ -147,6 +147,12 @@ namespace e2d::bindings::high return index >= 0 ? n.child_at(math::numeric_cast(index)) : node_iptr(); + }, + + "remove_child_at", [](node& n, i32 index) -> node_iptr { + return index >= 0 + ? n.remove_child_at(math::numeric_cast(index)) + : node_iptr(); } ); } diff --git a/sources/enduro2d/high/node.cpp b/sources/enduro2d/high/node.cpp index 00199354..fd41bb1f 100644 --- a/sources/enduro2d/high/node.cpp +++ b/sources/enduro2d/high/node.cpp @@ -430,6 +430,13 @@ namespace e2d } return child; } + + node_iptr node::remove_child_at(std::size_t index) noexcept { + node_iptr child = child_at(index); + return remove_child(child) + ? child + : node_iptr(); + } } namespace e2d diff --git a/untests/sources/untests_high/node.cpp b/untests/sources/untests_high/node.cpp index 9a6563fe..9bde1862 100644 --- a/untests/sources/untests_high/node.cpp +++ b/untests/sources/untests_high/node.cpp @@ -450,6 +450,27 @@ TEST_CASE("node") { REQUIRE_FALSE(cp->child_at(3)); } } + SECTION("remove_child_at") { + auto p = node::create(); + + auto n1 = node::create(p); + auto n2 = node::create(p); + auto n3 = node::create(p); + + REQUIRE_FALSE(n1->remove_child_at(0)); + + REQUIRE(p->remove_child_at(1) == n2); + REQUIRE_FALSE(n2->has_parent()); + REQUIRE(p->child_count() == 2u); + + REQUIRE(p->remove_child_at(0) == n1); + REQUIRE_FALSE(n1->has_parent()); + REQUIRE(p->child_count() == 1u); + + REQUIRE(p->remove_child_at(0) == n3); + REQUIRE_FALSE(n3->has_parent()); + REQUIRE(p->child_count() == 0u); + } SECTION("add_child_to_back/add_child_to_front") { auto p = node::create(); auto n1 = node::create();