node: add_child_at function

This commit is contained in:
BlackMATov
2020-04-22 13:00:40 +07:00
parent 3f8bb69955
commit 379218055d
5 changed files with 67 additions and 7 deletions

View File

@@ -85,6 +85,10 @@ namespace e2d
bool add_child(
const node_iptr& child) noexcept;
bool add_child_at(
const node_iptr& child,
std::size_t index) noexcept;
bool add_child_to_back(
const node_iptr& child) noexcept;

View File

@@ -87,6 +87,12 @@ function node.remove_all_children(self) end
---@return boolean
function node.add_child(self, child) end
---@param self node
---@param child node
---@param index integer
---@return boolean
function node.add_child_at(self, child, index) end
---@param self node
---@param child node
---@return boolean

View File

@@ -87,6 +87,12 @@ namespace e2d::bindings::high
return n.add_child(c);
},
"add_child_at", [](node& n, const node_iptr& c, i32 i) -> bool {
return i >= 0
? n.add_child_at(c, math::numeric_cast<std::size_t>(i))
: false;
},
"add_child_to_back", [](node& n, const node_iptr& c) -> bool {
return n.add_child_to_back(c);
},
@@ -115,9 +121,9 @@ namespace e2d::bindings::high
return n.remove_child(c);
},
"remove_child_at", [](node& n, i32 index) -> node_iptr {
return index >= 0
? n.remove_child_at(math::numeric_cast<std::size_t>(index))
"remove_child_at", [](node& n, i32 i) -> node_iptr {
return i >= 0
? n.remove_child_at(math::numeric_cast<std::size_t>(i))
: node_iptr();
},
@@ -159,9 +165,9 @@ namespace e2d::bindings::high
"next_sibling", sol::property(
[](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))
"child_at", [](node& n, i32 i) -> node_iptr {
return i >= 0
? n.child_at(math::numeric_cast<std::size_t>(i))
: node_iptr();
},

View File

@@ -207,10 +207,21 @@ namespace e2d
return count;
}
bool node::add_child(const node_iptr& child) noexcept {
bool node::add_child(
const node_iptr& child) noexcept
{
return add_child_to_front(child);
}
bool node::add_child_at(
const node_iptr& child,
std::size_t index) noexcept
{
return index == 0u
? add_child_to_back(child)
: add_child_after(child_at(index - 1u), child);
}
bool node::add_child_to_back(
const node_iptr& child) noexcept
{

View File

@@ -559,6 +559,39 @@ TEST_CASE("node") {
REQUIRE(p->child_at(1u) == n2);
REQUIRE(p->child_at(2u) == n1);
}
SECTION("add_child_at") {
auto p = node::create();
auto n1 = node::create();
auto n2 = node::create();
auto n3 = node::create();
auto n4 = node::create();
REQUIRE_FALSE(p->add_child_at(p, 0u));
REQUIRE_FALSE(p->add_child_at(nullptr, 0u));
REQUIRE_FALSE(p->add_child_at(n1, 1u));
REQUIRE(p->add_child_at(n1, 0u)); // n1
REQUIRE(p->add_child_at(n2, 0u)); // n2 n1
REQUIRE(p->add_child_at(n3, 2u)); // n2 n1 n3
REQUIRE(p->add_child_at(n4, 2u)); // n2 n1 n4 n3
REQUIRE(p->child_at(0u) == n2);
REQUIRE(p->child_at(1u) == n1);
REQUIRE(p->child_at(2u) == n4);
REQUIRE(p->child_at(3u) == n3);
REQUIRE_FALSE(p->add_child_at(n1, 5u));
REQUIRE(p->add_child_at(n1, 4u)); // n2 n4 n3 n1
REQUIRE(p->add_child_at(n3, 0u)); // n3 n2 n4 n1
REQUIRE(p->add_child_at(n4, 1u)); // n3 n4 n2 n1
REQUIRE(p->add_child_at(n4, p->child_index(n4).first)); // n3 n4 n2 n1
REQUIRE(p->child_at(0u) == n3);
REQUIRE(p->child_at(1u) == n4);
REQUIRE(p->child_at(2u) == n2);
REQUIRE(p->child_at(3u) == n1);
}
SECTION("add_child_to_back/add_child_to_front") {
auto p = node::create();
auto n1 = node::create();