node: remove_child_at function

This commit is contained in:
BlackMATov
2020-04-22 08:20:53 +07:00
parent 906dfcd292
commit 969180f3db
5 changed files with 41 additions and 0 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -147,6 +147,12 @@ namespace e2d::bindings::high
return index >= 0
? n.child_at(math::numeric_cast<std::size_t>(index))
: node_iptr();
},
"remove_child_at", [](node& n, i32 index) -> node_iptr {
return index >= 0
? n.remove_child_at(math::numeric_cast<std::size_t>(index))
: node_iptr();
}
);
}

View File

@@ -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

View File

@@ -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();