add template Node::clone() and remove destroy_immediate
This commit is contained in:
67
src/node.cpp
67
src/node.cpp
@@ -61,17 +61,23 @@ void Node::destroy()
|
||||
m_destroyed = true;
|
||||
mouse_release();
|
||||
key_release();
|
||||
}
|
||||
|
||||
void Node::destroy_immediate()
|
||||
{
|
||||
for (auto c : m_children)
|
||||
c->destroy_immediate();
|
||||
for (auto& c : m_children)
|
||||
c->destroy();
|
||||
for (auto p = m_parent; p; p = p->m_parent)
|
||||
if (p->child_mouse_focus.get() == this)
|
||||
p->child_mouse_focus = nullptr;
|
||||
//App::I->ui_task([&]
|
||||
//{
|
||||
// auto cp = m_children;
|
||||
// for (auto& c : cp)
|
||||
// c->destroy();
|
||||
// remove_all_children();
|
||||
// remove_from_parent();
|
||||
//});
|
||||
}
|
||||
|
||||
Node* Node::root()
|
||||
{
|
||||
|
||||
Node* ret = this;
|
||||
while (ret->m_parent)
|
||||
ret = ret->m_parent;
|
||||
@@ -137,7 +143,7 @@ kEventResult Node::on_event(Event* e)
|
||||
if (e->m_cat == kEventCategory::MouseEvent && child_mouse_focus.get() != it->get())
|
||||
{
|
||||
MouseEvent* me = static_cast<MouseEvent*>(e);
|
||||
if (child_mouse_focus)
|
||||
if (child_mouse_focus && !child_mouse_focus->m_destroyed)
|
||||
{
|
||||
MouseEvent e2 = *me;
|
||||
e2.m_type = kEventType::MouseUnfocus;
|
||||
@@ -147,8 +153,15 @@ kEventResult Node::on_event(Event* e)
|
||||
MouseEvent e2 = *me;
|
||||
e2.m_type = kEventType::MouseFocus;
|
||||
(*it)->handle_event(&e2);
|
||||
child_mouse_focus = *it;
|
||||
child_mouse_focus->m_mouse_focus = true;
|
||||
if (!(*it)->m_destroyed)
|
||||
{
|
||||
child_mouse_focus = *it;
|
||||
child_mouse_focus->m_mouse_focus = true;
|
||||
}
|
||||
//else
|
||||
//{
|
||||
// child_mouse_focus = nullptr;
|
||||
//}
|
||||
}
|
||||
ret = kEventResult::Consumed;
|
||||
break;
|
||||
@@ -298,7 +311,9 @@ void Node::added(Node* parent)
|
||||
|
||||
void Node::removed(Node* parent)
|
||||
{
|
||||
|
||||
for (auto p = m_parent; p; p = p->m_parent)
|
||||
if (p->child_mouse_focus.get() == this)
|
||||
p->child_mouse_focus = nullptr;
|
||||
}
|
||||
|
||||
const Node* Node::init_template(const char* id)
|
||||
@@ -332,6 +347,7 @@ void Node::add_child(Node* n)
|
||||
m_children.emplace_back(n);
|
||||
n->m_parent = this;
|
||||
n->m_manager = m_manager;
|
||||
n->m_destroyed = false;
|
||||
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
|
||||
n->added(this);
|
||||
on_child_added(n);
|
||||
@@ -347,6 +363,7 @@ void Node::add_child(Node* n, int index)
|
||||
m_children.emplace_back(n);
|
||||
n->m_parent = this;
|
||||
n->m_manager = m_manager;
|
||||
n->m_destroyed = false;
|
||||
YGNodeInsertChild(y_node, n->y_node, index);
|
||||
n->added(this);
|
||||
on_child_added(n);
|
||||
@@ -362,6 +379,7 @@ void Node::add_child(std::shared_ptr<Node> n)
|
||||
m_children.push_back(n);
|
||||
n->m_parent = this;
|
||||
n->m_manager = m_manager;
|
||||
n->m_destroyed = false;
|
||||
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
|
||||
n->added(this);
|
||||
on_child_added(n.get());
|
||||
@@ -377,6 +395,7 @@ void Node::add_child(std::shared_ptr<Node> n, int index)
|
||||
m_children.insert(m_children.begin() + index, n);
|
||||
n->m_parent = this;
|
||||
n->m_manager = m_manager;
|
||||
n->m_destroyed = false;
|
||||
YGNodeInsertChild(y_node, n->y_node, index);
|
||||
n->added(this);
|
||||
on_child_added(n.get());
|
||||
@@ -1009,16 +1028,6 @@ void Node::update_internal(const glm::vec2& origin, const glm::mat4& proj, float
|
||||
m_mvp = proj * pos * scale * pivot * prescale;
|
||||
m_proj = proj;
|
||||
|
||||
for (int i = 0; i < m_children.size(); i++)
|
||||
{
|
||||
if (m_children[i]->m_destroyed)
|
||||
{
|
||||
m_children[i]->destroy_immediate();
|
||||
remove_child(m_children[i].get());
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_size != old_size || m_zoom != zoom)
|
||||
{
|
||||
m_zoom = zoom;
|
||||
@@ -1029,6 +1038,9 @@ void Node::update_internal(const glm::vec2& origin, const glm::mat4& proj, float
|
||||
|
||||
for (auto& c : m_children)
|
||||
c->update_internal(m_pos, proj, zoom);
|
||||
|
||||
m_children.erase(std::remove_if(m_children.begin(), m_children.end(),
|
||||
[](const auto& n) { return n->m_destroyed; }), m_children.end());
|
||||
}
|
||||
|
||||
void Node::tick(float dt)
|
||||
@@ -1334,15 +1346,6 @@ void Node::draw()
|
||||
|
||||
}
|
||||
|
||||
Node* Node::clone()
|
||||
{
|
||||
Node* n = clone_instantiate();
|
||||
clone_copy(n);
|
||||
clone_children(n);
|
||||
clone_finalize(n);
|
||||
return n;
|
||||
}
|
||||
|
||||
Node* Node::clone_instantiate() const
|
||||
{
|
||||
return new Node();
|
||||
@@ -1384,8 +1387,8 @@ void Node::clone_children(Node* dest) const
|
||||
{
|
||||
for (auto& c : m_children)
|
||||
{
|
||||
Node* cn = c->clone();
|
||||
dest->m_children.emplace_back(cn);
|
||||
std::shared_ptr<Node> cn = c->clone();
|
||||
dest->m_children.push_back(cn);
|
||||
cn->m_parent = dest;
|
||||
cn->m_manager = dest->m_manager;
|
||||
cn->loaded();
|
||||
|
||||
Reference in New Issue
Block a user