mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-14 16:09:06 +07:00
node: child_index function
This commit is contained in:
@@ -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<std::size_t, bool> child_index(
|
||||
const const_node_iptr& child) const noexcept;
|
||||
|
||||
node_iptr remove_child_at(std::size_t index) noexcept;
|
||||
protected:
|
||||
node() = default;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -149,6 +149,10 @@ namespace e2d::bindings::high
|
||||
: node_iptr();
|
||||
},
|
||||
|
||||
"child_index", [](node& n, const node_iptr& c) -> std::pair<std::size_t, bool> {
|
||||
return n.child_index(c);
|
||||
},
|
||||
|
||||
"remove_child_at", [](node& n, i32 index) -> node_iptr {
|
||||
return index >= 0
|
||||
? n.remove_child_at(math::numeric_cast<std::size_t>(index))
|
||||
|
||||
@@ -431,6 +431,20 @@ namespace e2d
|
||||
return child;
|
||||
}
|
||||
|
||||
std::pair<std::size_t, bool> 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<std::size_t>(distance), true};
|
||||
}
|
||||
|
||||
node_iptr node::remove_child_at(std::size_t index) noexcept {
|
||||
node_iptr child = child_at(index);
|
||||
return remove_child(child)
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user