From 6d3c9380b2a99d0dd04e57c527979568f98f16ce Mon Sep 17 00:00:00 2001 From: omigamedev Date: Sat, 12 Aug 2017 18:45:49 +0100 Subject: [PATCH] fix sidebar scroll when adding or removing panels --- engine/app.h | 3 +- engine/app_layout.cpp | 68 ++++++++++++++++++++++-------------------- engine/node_scroll.cpp | 35 +++++++++------------- engine/node_scroll.h | 1 + 4 files changed, 53 insertions(+), 54 deletions(-) diff --git a/engine/app.h b/engine/app.h index 88356b5..20c84f1 100644 --- a/engine/app.h +++ b/engine/app.h @@ -13,6 +13,7 @@ #include "node_panel_layer.h" #include "node_panel_color.h" #include "node_panel_stroke.h" +#include "node_scroll.h" #include "node_canvas.h" #include "node_dialog_layer_rename.h" @@ -42,7 +43,7 @@ public: std::shared_ptr stroke; NodeCanvas* canvas; Node* current_panel = nullptr; - Node* panels; + NodeScroll* panels; std::function on_brush_select; std::function on_color_change; std::function on_stroke_change; diff --git a/engine/app_layout.cpp b/engine/app_layout.cpp index f03dfee..b610cc1 100644 --- a/engine/app_layout.cpp +++ b/engine/app_layout.cpp @@ -6,6 +6,9 @@ using namespace ui; +static glm::vec4 color_button_normal{ .1, .1, .1, 1 }; +static glm::vec4 color_button_hlight{ 1, .0, .0, 1 }; + void App::init_toolbar_main() { @@ -85,7 +88,7 @@ void App::init_toolbar_main() void App::init_sidebar() { sidebar = layout[main_id]->find("sidebar"); - panels = layout[main_id]->find("panels"); + panels = layout[main_id]->find("panels"); canvas = layout[main_id]->find("paint-canvas"); canvas->data_path = data_path; @@ -177,13 +180,42 @@ void App::init_sidebar() layers->on_layer_highlight_changed = [this](Node*, int idx, bool highlight) { canvas->m_canvas->m_layers[canvas->m_canvas->m_order[idx]].m_hightlight = highlight; }; + if (auto* button = layout[main_id]->find("btn-stroke")) + { + button->on_click = [this, button](Node*) { + panels->get_child_index(stroke.get()) == -1 ? panels->add_child(stroke) : panels->remove_child(stroke.get()); + panels->fix_scroll(); + button->set_color(panels->get_child_index(stroke.get()) == -1 ? color_button_normal : color_button_hlight); + }; + } + if (auto* button = layout[main_id]->find("btn-brush")) + { + button->on_click = [this, button](Node*) { + panels->get_child_index(brushes.get()) == -1 ? panels->add_child(brushes) : panels->remove_child(brushes.get()); + panels->fix_scroll(); + button->set_color(panels->get_child_index(brushes.get()) == -1 ? color_button_normal : color_button_hlight); + }; + } + if (auto* button = layout[main_id]->find("btn-color")) + { + button->on_click = [this, button](Node*) { + panels->get_child_index(color.get()) == -1 ? panels->add_child(color) : panels->remove_child(color.get()); + panels->fix_scroll(); + button->set_color(panels->get_child_index(color.get()) == -1 ? color_button_normal : color_button_hlight); + }; + } + if (auto* button = layout[main_id]->find("btn-layer")) + { + button->on_click = [this, button](Node*) { + panels->get_child_index(layers.get()) == -1 ? panels->add_child(layers) : panels->remove_child(layers.get()); + panels->fix_scroll(); + button->set_color(panels->get_child_index(layers.get()) == -1 ? color_button_normal : color_button_hlight); + }; + } } void App::init_toolbar_draw() { - - static glm::vec4 color_button_normal{ .1, .1, .1, 1 }; - static glm::vec4 color_button_hlight{ 1, .0, .0, 1 }; if (auto* button = layout[main_id]->find("btn-pen")) { button->on_click = [this](Node*) { @@ -274,34 +306,6 @@ void App::init_toolbar_draw() canvas->m_canvas->clear(canvas->m_brush.m_tip_color); }; } - if (auto* button = layout[main_id]->find("btn-stroke")) - { - button->on_click = [this, button](Node*) { - panels->get_child_index(stroke.get()) == -1 ? panels->add_child(stroke) : panels->remove_child(stroke.get()); - button->set_color(panels->get_child_index(stroke.get()) == -1 ? color_button_normal : color_button_hlight); - }; - } - if (auto* button = layout[main_id]->find("btn-brush")) - { - button->on_click = [this, button](Node*) { - panels->get_child_index(brushes.get()) == -1 ? panels->add_child(brushes) : panels->remove_child(brushes.get()); - button->set_color(panels->get_child_index(brushes.get()) == -1 ? color_button_normal : color_button_hlight); - }; - } - if (auto* button = layout[main_id]->find("btn-color")) - { - button->on_click = [this, button](Node*) { - panels->get_child_index(color.get()) == -1 ? panels->add_child(color) : panels->remove_child(color.get()); - button->set_color(panels->get_child_index(color.get()) == -1 ? color_button_normal : color_button_hlight); - }; - } - if (auto* button = layout[main_id]->find("btn-layer")) - { - button->on_click = [this, button](Node*) { - panels->get_child_index(layers.get()) == -1 ? panels->add_child(layers) : panels->remove_child(layers.get()); - button->set_color(panels->get_child_index(layers.get()) == -1 ? color_button_normal : color_button_hlight); - }; - } } void App::init_menu_file() diff --git a/engine/node_scroll.cpp b/engine/node_scroll.cpp index c37a344..2072cfc 100644 --- a/engine/node_scroll.cpp +++ b/engine/node_scroll.cpp @@ -8,6 +8,15 @@ Node* NodeScroll::clone_instantiate() const return new NodeScroll; } +void NodeScroll::fix_scroll() +{ + auto pad = GetPadding(); + glm::vec2 padoff = { pad.y + pad.w, pad.x + pad.z }; + auto rect = get_children_rect(); + m_offset = glm::clamp(m_offset, -rect.zw() + m_clip_uncut.zw() - padoff, { 0, 0 }); + m_pos_offset_childred = m_offset; +} + kEventResult NodeScroll::handle_event(Event* e) { NodeBorder::handle_event(e); @@ -25,12 +34,8 @@ kEventResult NodeScroll::handle_event(Event* e) case kEventType::MouseMove: if (m_dragging) { - auto pad = GetPadding(); - glm::vec2 padoff = { pad.y + pad.w, pad.x + pad.z }; - auto rect = get_children_rect(); m_offset = m_offset_start + (me->m_pos - m_drag_start) * m_mask; - m_offset = glm::clamp(m_offset, -rect.zw() + m_clip_uncut.zw() - padoff, { 0, 0 }); - m_pos_offset_childred = m_offset; + fix_scroll(); } break; case kEventType::MouseUpL: @@ -38,28 +43,16 @@ kEventResult NodeScroll::handle_event(Event* e) m_dragging = false; break; case kEventType::MouseScroll: - { - auto pad = GetPadding(); - glm::vec2 padoff = { pad.y + pad.w, pad.x + pad.z }; - auto rect = get_children_rect(); - m_offset += me->m_scroll_delta * 50; - m_offset = glm::clamp(m_offset, -rect.zw() + m_clip_uncut.zw() - padoff, { 0, 0 }); - m_pos_offset_childred = m_offset; - } + m_offset += me->m_scroll_delta * 50; + fix_scroll(); break; case kEventType::GestureStart: m_offset_start = m_offset; mouse_capture(); break; case kEventType::GestureMove: - { - auto pad = GetPadding(); - glm::vec2 padoff = { pad.y + pad.w, pad.x + pad.z }; - auto rect = get_children_rect(); - m_offset = m_offset_start + ge->m_pos_delta * m_mask; - m_offset = glm::clamp(m_offset, -rect.zw() + m_clip_uncut.zw() - padoff, { 0, 0 }); - m_pos_offset_childred = m_offset; - } + m_offset = m_offset_start + ge->m_pos_delta * m_mask; + fix_scroll(); break; case kEventType::GestureEnd: mouse_release(); diff --git a/engine/node_scroll.h b/engine/node_scroll.h index 3478c81..14bdd1b 100644 --- a/engine/node_scroll.h +++ b/engine/node_scroll.h @@ -11,4 +11,5 @@ class NodeScroll : public NodeBorder public: virtual Node* clone_instantiate() const override; virtual kEventResult handle_event(Event* e) override; + void fix_scroll(); };