improve dialog browse, wrap ui task for every Node method that modifies the children list, update the ui every time the scene tree changes, restore fps and stylus state on win, fix bucket tool, fix snapshop restore, init in ui thread

This commit is contained in:
2019-07-10 21:08:14 +02:00
parent 4cbf0c47b4
commit 48c39c4ef2
10 changed files with 293 additions and 131 deletions

View File

@@ -39,7 +39,8 @@
void Node::app_redraw()
{
App::I.ui_task([] { App::I.redraw = true; });
App::I.redraw = true;
App::I.ui_cv.notify_all();
}
void Node::watch(std::function<bool(Node*)> observer)
@@ -301,68 +302,84 @@ void Node::removed(Node* parent)
const Node* Node::init_template(const char* id)
{
auto hid = const_hash(id);
Node* top = m_manager->get(hid);
Node* m_template = static_cast<Node*>(top->m_children[0].get());
for (auto& c : m_template->m_children)
Node* m_template = nullptr;
App::I.ui_task([&]
{
auto node = c->clone();
add_child(node);
//node->init();
//node->create();
//node->loaded();
}
YGNodeCopyStyle(y_node, m_template->y_node);
m_template->clone_copy(this);
auto hid = const_hash(id);
Node* top = m_manager->get(hid);
m_template = static_cast<Node*>(top->m_children[0].get());
for (auto& c : m_template->m_children)
{
auto node = c->clone();
add_child(node);
//node->init();
//node->create();
//node->loaded();
}
YGNodeCopyStyle(y_node, m_template->y_node);
m_template->clone_copy(this);
});
return m_template;
}
void Node::add_child(Node* n)
{
if (n->m_parent)
n->m_parent->remove_child(n);
m_children.emplace_back(n);
n->m_parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
n->added(this);
on_child_added(n);
App::I.ui_task([&]
{
if (n->m_parent)
n->m_parent->remove_child(n);
m_children.emplace_back(n);
n->m_parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
n->added(this);
on_child_added(n);
});
}
void Node::add_child(Node* n, int index)
{
if (n->m_parent)
n->m_parent->remove_child(n);
m_children.emplace_back(n);
n->m_parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, index);
n->added(this);
on_child_added(n);
App::I.ui_task([&]
{
if (n->m_parent)
n->m_parent->remove_child(n);
m_children.emplace_back(n);
n->m_parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, index);
n->added(this);
on_child_added(n);
});
}
void Node::add_child(std::shared_ptr<Node> n)
{
if (n->m_parent)
n->m_parent->remove_child(n.get());
m_children.push_back(n);
n->m_parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
n->added(this);
on_child_added(n.get());
App::I.ui_task([&]
{
if (n->m_parent)
n->m_parent->remove_child(n.get());
m_children.push_back(n);
n->m_parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
n->added(this);
on_child_added(n.get());
});
}
void Node::add_child(std::shared_ptr<Node> n, int index)
{
if (n->m_parent)
n->m_parent->remove_child(n.get());
m_children.insert(m_children.begin() + index, n);
n->m_parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, index);
n->added(this);
on_child_added(n.get());
App::I.ui_task([&]
{
if (n->m_parent)
n->m_parent->remove_child(n.get());
m_children.insert(m_children.begin() + index, n);
n->m_parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, index);
n->added(this);
on_child_added(n.get());
});
}
void Node::remove_from_parent()
@@ -376,59 +393,74 @@ void Node::remove_child(Node* n)
auto i = std::find_if(m_children.begin(), m_children.end(), [=](auto& ptr) { return ptr.get() == n; });
if (i != m_children.end())
{
n->removed(this);
n->m_parent = nullptr;
YGNodeRemoveChild(y_node, n->y_node);
on_child_removed(n);
m_children.erase(i);
if (child_mouse_focus == n)
child_mouse_focus = nullptr;
App::I.ui_task([&]
{
n->removed(this);
n->m_parent = nullptr;
YGNodeRemoveChild(y_node, n->y_node);
on_child_removed(n);
m_children.erase(i);
if (child_mouse_focus == n)
child_mouse_focus = nullptr;
});
}
}
void Node::remove_all_children()
{
for (auto& n : m_children)
App::I.ui_task([&]
{
n->removed(this);
n->m_parent = nullptr;
YGNodeRemoveChild(y_node, n->y_node);
on_child_removed(n.get());
}
m_children.clear();
child_mouse_focus = nullptr;
for (auto& n : m_children)
{
n->removed(this);
n->m_parent = nullptr;
YGNodeRemoveChild(y_node, n->y_node);
on_child_removed(n.get());
}
m_children.clear();
child_mouse_focus = nullptr;
});
}
void Node::move_child(Node* n, int index)
{
YGNodeRemoveChild(y_node, n->y_node);
YGNodeInsertChild(y_node, n->y_node, index);
auto it = std::find_if(m_children.begin(), m_children.end(),
[n](const std::shared_ptr<Node>& o) { return o.get() == n; });
auto tmp = *it; // copy the ptr before removing it
m_children.erase(it);
m_children.insert(m_children.begin() + index, tmp);
App::I.ui_task([&]
{
YGNodeRemoveChild(y_node, n->y_node);
YGNodeInsertChild(y_node, n->y_node, index);
auto it = std::find_if(m_children.begin(), m_children.end(),
[n](const std::shared_ptr<Node>& o) { return o.get() == n; });
auto tmp = *it; // copy the ptr before removing it
m_children.erase(it);
m_children.insert(m_children.begin() + index, tmp);
});
}
void Node::move_child_front(Node* n)
{
int count = YGNodeGetChildCount(y_node);
move_child(n, count - 1);
App::I.ui_task([&]
{
int count = YGNodeGetChildCount(y_node);
move_child(n, count - 1);
});
}
void Node::move_child_offset(Node* n, int offset)
{
int count = YGNodeGetChildCount(y_node);
for (int i = 0; i < count; i++)
App::I.ui_task([&]
{
if (YGNodeGetChild(y_node, i) == n->y_node)
int count = YGNodeGetChildCount(y_node);
for (int i = 0; i < count; i++)
{
int new_index = glm::clamp<int>(i + offset, 0, count - 1);
YGNodeRemoveChild(y_node, n->y_node);
YGNodeInsertChild(y_node, n->y_node, new_index);
break;
if (YGNodeGetChild(y_node, i) == n->y_node)
{
int new_index = glm::clamp<int>(i + offset, 0, count - 1);
YGNodeRemoveChild(y_node, n->y_node);
YGNodeInsertChild(y_node, n->y_node, new_index);
break;
}
}
}
});
}
int Node::get_child_index(Node* n)
@@ -647,74 +679,88 @@ void Node::SetWidth(float value)
{
YGNodeStyleSetWidth(y_node, value);
m_size.x = value;
app_redraw();
}
void Node::SetWidthP(float value)
{
YGNodeStyleSetWidthPercent(y_node, value);
app_redraw();
}
void Node::SetHeight(float value)
{
YGNodeStyleSetHeight(y_node, value);
m_size.y = value;
app_redraw();
}
void Node::SetHeightP(float value)
{
YGNodeStyleSetHeightPercent(y_node, value);
app_redraw();
}
void Node::SetSize(float w, float h)
{
SetWidth(w); SetHeight(h);
m_size = {w, h};
app_redraw();
}
void Node::SetSize(glm::vec2 value)
{
SetWidth(value.x); SetHeight(value.y);
m_size = value;
app_redraw();
}
void Node::SetMaxWidth(float value)
{
YGNodeStyleSetMaxWidth(y_node, value);
app_redraw();
}
void Node::SetMaxWidthP(float value)
{
YGNodeStyleSetMaxWidthPercent(y_node, value);
app_redraw();
}
void Node::SetMaxHeight(float value)
{
YGNodeStyleSetMaxHeight(y_node, value);
app_redraw();
}
void Node::SetMaxHeightP(float value)
{
YGNodeStyleSetMaxHeightPercent(y_node, value);
app_redraw();
}
void Node::SetMinWidth(float value)
{
YGNodeStyleSetMinWidth(y_node, value);
app_redraw();
}
void Node::SetMinWidthP(float value)
{
YGNodeStyleSetMinWidthPercent(y_node, value);
app_redraw();
}
void Node::SetMinHeight(float value)
{
YGNodeStyleSetMinHeight(y_node, value);
app_redraw();
}
void Node::SetMinHeightP(float value)
{
YGNodeStyleSetMinHeightPercent(y_node, value);
app_redraw();
}
void Node::SetPadding(float t, float r, float b, float l)
@@ -723,6 +769,7 @@ void Node::SetPadding(float t, float r, float b, float l)
YGNodeStyleSetPadding(y_node, YGEdgeRight, r);
YGNodeStyleSetPadding(y_node, YGEdgeBottom, b);
YGNodeStyleSetPadding(y_node, YGEdgeLeft, l);
app_redraw();
}
glm::vec4 Node::GetPadding() const
@@ -740,6 +787,7 @@ void Node::SetMargin(float t, float r, float b, float l)
YGNodeStyleSetMargin(y_node, YGEdgeRight, r);
YGNodeStyleSetMargin(y_node, YGEdgeBottom, b);
YGNodeStyleSetMargin(y_node, YGEdgeLeft, l);
app_redraw();
}
glm::vec4 Node::GetMargin() const
@@ -755,6 +803,7 @@ void Node::SetPosition(const glm::vec2 pos)
{
SetPosition(pos.x, pos.y);
m_pos = pos;
app_redraw();
}
void Node::SetPosition(float l, float t)
@@ -762,6 +811,7 @@ void Node::SetPosition(float l, float t)
YGNodeStyleSetPosition(y_node, YGEdgeTop, t);
YGNodeStyleSetPosition(y_node, YGEdgeLeft, l);
m_pos = {l, t};
app_redraw();
}
void Node::SetPosition(float l, float t, float r, float b)
@@ -770,79 +820,93 @@ void Node::SetPosition(float l, float t, float r, float b)
YGNodeStyleSetPosition(y_node, YGEdgeRight, r);
YGNodeStyleSetPosition(y_node, YGEdgeBottom, b);
YGNodeStyleSetPosition(y_node, YGEdgeLeft, l);
app_redraw();
}
void Node::SetFlexGrow(float value)
{
YGNodeStyleSetFlexGrow(y_node, value);
app_redraw();
}
void Node::SetFlexShrink(float value)
{
YGNodeStyleSetFlexShrink(y_node, value);
app_redraw();
}
void Node::SetFlexDir(YGFlexDirection value)
{
YGNodeStyleSetFlexDirection(y_node, value);
app_redraw();
}
void Node::SetFlexWrap(YGWrap value)
{
YGNodeStyleSetFlexWrap(y_node, value);
app_redraw();
}
void Node::SetJustify(YGJustify value)
{
YGNodeStyleSetJustifyContent(y_node, value);
app_redraw();
}
void Node::SetAlign(YGAlign value)
{
YGNodeStyleSetAlignItems(y_node, value);
app_redraw();
}
void Node::SetPositioning(YGPositionType value)
{
YGNodeStyleSetPositionType(y_node, value);
app_redraw();
}
void Node::SetAspectRatio(float ar)
{
YGNodeStyleSetAspectRatio(y_node, ar);
app_redraw();
}
void Node::SetRTL(YGDirection dir)
{
YGNodeStyleSetDirection(y_node, dir);
app_redraw();
}
void Node::SetVisibility(bool visible)
{
if (m_display && !visible)
App::I.ui_task([&]
{
// hide
int idx = m_parent->get_child_index(this);
YGNodeRemoveChild(m_parent->y_node, y_node);
y_placeholder = YGNodeNew();
YGNodeInsertChild(m_parent->y_node, y_placeholder, idx);
}
else if (!m_display && visible)
{
int count = YGNodeGetChildCount(m_parent->y_node);
for (int i = 0; i < count; i++)
if (m_display && !visible)
{
if (YGNodeGetChild(m_parent->y_node, i) == y_placeholder)
// hide
int idx = m_parent->get_child_index(this);
YGNodeRemoveChild(m_parent->y_node, y_node);
y_placeholder = YGNodeNew();
YGNodeInsertChild(m_parent->y_node, y_placeholder, idx);
}
else if (!m_display && visible)
{
int count = YGNodeGetChildCount(m_parent->y_node);
for (int i = 0; i < count; i++)
{
YGNodeRemoveChild(m_parent->y_node, y_placeholder);
YGNodeInsertChild(m_parent->y_node, y_node, i);
YGNodeFree(y_placeholder);
y_placeholder = nullptr;
break;
if (YGNodeGetChild(m_parent->y_node, i) == y_placeholder)
{
YGNodeRemoveChild(m_parent->y_node, y_placeholder);
YGNodeInsertChild(m_parent->y_node, y_node, i);
YGNodeFree(y_placeholder);
y_placeholder = nullptr;
break;
}
}
}
}
});
m_display = visible;
app_redraw();
}
void Node::ToggleVisibility()
@@ -852,7 +916,7 @@ void Node::ToggleVisibility()
glm::vec2 Node::GetPosition()
{
return{ YGNodeLayoutGetLeft(y_node), YGNodeLayoutGetTop(y_node) };
return { YGNodeLayoutGetLeft(y_node), YGNodeLayoutGetTop(y_node) };
}
float Node::GetWidth()