node: child_at function

This commit is contained in:
BlackMATov
2020-04-22 08:12:17 +07:00
parent 20c35f2911
commit 906dfcd292
5 changed files with 52 additions and 1 deletions

View File

@@ -125,6 +125,9 @@ namespace e2d
node_iptr next_sibling() noexcept; node_iptr next_sibling() noexcept;
const_node_iptr next_sibling() const noexcept; const_node_iptr next_sibling() const noexcept;
node_iptr child_at(std::size_t index) noexcept;
const_node_iptr child_at(std::size_t index) const noexcept;
protected: protected:
node() = default; node() = default;
node(gobject owner); node(gobject owner);

View File

@@ -140,5 +140,10 @@ function node.send_forward(self) end
---@return boolean ---@return boolean
function node.bring_to_front(self) end function node.bring_to_front(self) end
---@param self node
---@param index integer
---@return node
function node.child_at(self, index) end
---@type node ---@type node
_G.node = _G.node or node _G.node = _G.node or node

View File

@@ -141,7 +141,13 @@ namespace e2d::bindings::high
[](node& n) -> node_iptr { return n.prev_sibling(); }), [](node& n) -> node_iptr { return n.prev_sibling(); }),
"next_sibling", sol::property( "next_sibling", sol::property(
[](node& n) -> node_iptr { return n.next_sibling(); }) [](node& n) -> node_iptr { return n.next_sibling(); }),
"child_at", [](node& n, i32 index) -> node_iptr {
return index >= 0
? n.child_at(math::numeric_cast<std::size_t>(index))
: node_iptr();
}
); );
} }
} }

View File

@@ -414,6 +414,22 @@ namespace e2d
} }
return const_node_iptr(&*iter); return const_node_iptr(&*iter);
} }
node_iptr node::child_at(std::size_t index) noexcept {
node_iptr child = first_child();
for ( std::size_t i = 0; i < index && child; ++i ) {
child = child->next_sibling();
}
return child;
}
const_node_iptr node::child_at(std::size_t index) const noexcept {
const_node_iptr child = first_child();
for ( std::size_t i = 0; i < index && child; ++i ) {
child = child->next_sibling();
}
return child;
}
} }
namespace e2d namespace e2d

View File

@@ -429,6 +429,27 @@ TEST_CASE("node") {
REQUIRE_FALSE(cn4->next_sibling()); REQUIRE_FALSE(cn4->next_sibling());
} }
} }
SECTION("child_at") {
auto p = node::create();
auto n1 = node::create(p);
auto n2 = node::create(p);
auto n3 = node::create(p);
REQUIRE(p->child_at(0) == n1);
REQUIRE(p->child_at(1) == n2);
REQUIRE(p->child_at(2) == n3);
REQUIRE_FALSE(p->child_at(3));
{
const_node_iptr cp = p;
REQUIRE(cp->child_at(0) == n1);
REQUIRE(cp->child_at(1) == n2);
REQUIRE(cp->child_at(2) == n3);
REQUIRE_FALSE(cp->child_at(3));
}
}
SECTION("add_child_to_back/add_child_to_front") { SECTION("add_child_to_back/add_child_to_front") {
auto p = node::create(); auto p = node::create();
auto n1 = node::create(); auto n1 = node::create();