fix sidebar scroll when adding or removing panels

This commit is contained in:
2017-08-12 18:45:49 +01:00
parent 2711d4e9b0
commit 6d3c9380b2
4 changed files with 53 additions and 54 deletions

View File

@@ -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<NodePanelStroke> stroke;
NodeCanvas* canvas;
Node* current_panel = nullptr;
Node* panels;
NodeScroll* panels;
std::function<void(int)> on_brush_select;
std::function<void(glm::vec4 color)> on_color_change;
std::function<void()> on_stroke_change;

View File

@@ -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<NodeBorder>("sidebar");
panels = layout[main_id]->find<Node>("panels");
panels = layout[main_id]->find<NodeScroll>("panels");
canvas = layout[main_id]->find<NodeCanvas>("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<NodeButtonCustom>("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<NodeButtonCustom>("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<NodeButtonCustom>("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<NodeButtonCustom>("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<NodeButton>("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<NodeButtonCustom>("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<NodeButtonCustom>("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<NodeButtonCustom>("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<NodeButtonCustom>("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()

View File

@@ -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();

View File

@@ -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();
};