drag canvas, render layers and tmp, add/select layer, opacity for layer, color bucket filler

This commit is contained in:
2017-04-07 21:21:43 +01:00
parent 2b4915154e
commit bb7e94d06b
9 changed files with 99 additions and 49 deletions

View File

@@ -175,6 +175,7 @@ public:
glm::vec2 m_pos;
glm::vec2 m_size;
glm::vec4 m_clip;
glm::vec4 m_clip_uncut;
std::string m_name;
bool m_display = true;
Node(const Node&) = delete;
@@ -212,6 +213,7 @@ public:
void SetHeight(float value) { YGNodeStyleSetHeight(y_node, value); }
void SetHeightP(float value) { YGNodeStyleSetHeightPercent(y_node, value); }
void SetSize(glm::vec2 value) { SetWidth(value.x); SetHeight(value.y); }
void SetSize(float w, float h) { SetWidth(w); SetHeight(h); }
void SetPadding(float t, float r, float b, float l)
{
@@ -232,6 +234,10 @@ public:
YGNodeStyleSetPosition(y_node, YGEdgeTop, t);
YGNodeStyleSetPosition(y_node, YGEdgeLeft, l);
}
void SetPosition(const glm::vec2 pos)
{
SetPosition(pos.x, pos.y);
}
void SetFlexGrow(float value) { YGNodeStyleSetFlexGrow(y_node, value); }
void SetFlexShrink(float value) { YGNodeStyleSetFlexShrink(y_node, value); }
@@ -1259,6 +1265,7 @@ class NodeLayer : public NodeBorder
{
public:
std::function<void(NodeLayer* target)> on_selected;
std::function<void(NodeLayer* target, float opacity)> on_opacity_changed;
bool m_selected = false;
glm::vec4 m_color_normal = glm::vec4(.4, .4, .4, 1);
glm::vec4 m_color_selected = glm::vec4(.3, .3, .3, 1);
@@ -1266,6 +1273,7 @@ public:
std::string m_label_text;
NodeText* m_label;
NodeCheckBox* m_visibility;
NodeSliderH* m_opacity;
virtual Node* clone_instantiate() const override { return new NodeLayer(); }
virtual void clone_children(Node* dest) const override
{
@@ -1289,6 +1297,7 @@ public:
m_thinkness = m_template->m_thinkness;
m_label = find<NodeText>("label");
m_visibility = find<NodeCheckBox>("cb");
m_opacity = find<NodeSliderH>("sl-opacity");
}
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) override
{
@@ -1309,6 +1318,10 @@ public:
NodeBorder::loaded();
if (!m_label_text.empty())
m_label->set_text(m_label_text.c_str());
m_opacity->on_value_changed = [this](Node*, float value) {
if (on_opacity_changed)
on_opacity_changed(this, value);
};
}
virtual kEventResult handle_event(Event* e) override
{
@@ -1354,6 +1367,7 @@ class NodePanelLayer : public Node
int id_counter = 0;
public:
std::function<void(Node* target, int old_idx, int new_idx)> on_layer_change;
std::function<void(Node* target, int idx, float value)> on_layer_opacity_changed;
std::function<void(Node* target, int index)> on_layer_delete;
std::function<void(Node* target)> on_layer_add;
NodeLayer* m_current_layer = nullptr;
@@ -1411,6 +1425,7 @@ public:
l->loaded();
l->set_name(name);
l->on_selected = std::bind(&NodePanelLayer::handle_layer_selected, this, std::placeholders::_1);
l->on_opacity_changed = std::bind(&NodePanelLayer::handle_layer_opacity, this, std::placeholders::_1, std::placeholders::_2);
m_layers.push_back(l);
if (on_layer_add)
on_layer_add(this);
@@ -1429,6 +1444,11 @@ public:
if (on_layer_change)
on_layer_change(this, -1, i);
}
void handle_layer_opacity(NodeLayer* target, float value)
{
if (on_layer_opacity_changed)
on_layer_opacity_changed(this, m_layers_container->get_child_index(target), value);
}
void handle_layer_selected(NodeLayer* target)
{
if (m_current_layer)
@@ -1850,6 +1870,9 @@ public:
class NodeCanvas : public Node
{
bool m_dragging = false;
bool m_draggingR = false;
glm::vec2 m_dragR_start;
glm::vec2 m_pan_start;
public:
std::unique_ptr<ui::Canvas> m_canvas;
ui::Brush m_brush;
@@ -1860,8 +1883,12 @@ public:
m_mouse_ignore = false;
m_canvas = std::make_unique<ui::Canvas>();
m_canvas->create(512, 512);
m_canvas->layer_add("asd");
m_canvas->clear();
m_sampler.create();
SetPositioning(YGPositionTypeAbsolute);
SetPosition(100, 100);
SetSize({ 512, 512 });
}
virtual void draw() override
{
@@ -1874,10 +1901,10 @@ public:
glGetIntegerv(GL_VIEWPORT, vp);
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
glClearColor(1, 1, 1, 1);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
float zoom = root()->m_zoom;
auto box = m_clip * zoom;
auto box = m_clip_uncut * zoom;
glm::ivec4 c = (glm::ivec4)glm::vec4(box.x, (int)(vp[3] - box.y - box.w), box.z, box.w);
glViewport(c.x, c.y, c.z, c.w);
@@ -1889,34 +1916,31 @@ public:
glm::translate(glm::vec3(.5f, .5f, 0.f)); // pivot
m_sampler.bind(0);
ui::ShaderManager::use(kShader::Texture);
ui::ShaderManager::use(kShader::TextureAlpha);
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
auto blend = glIsEnabled(GL_BLEND);
glEnable(GL_BLEND);
m_canvas->m_fb.bindTexture();
NodeBorder::m_plane.draw_fill();
m_canvas->m_fb.unbindTexture();
if (m_canvas->m_show_tmp)
for (int i = 0; i < m_canvas->m_layers.size(); i++)
{
glEnable(GL_BLEND);
ui::ShaderManager::use(kShader::TextureAlpha);
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
ui::ShaderManager::u_float(kShaderUniform::Alpha, m_canvas->m_current_stroke->m_brush.m_tip_opacity);
m_canvas->m_tmp.bindTexture();
ui::ShaderManager::u_float(kShaderUniform::Alpha, m_canvas->m_layers[i].m_opacity);
m_canvas->m_layers[i].m_rtt.bindTexture();
NodeBorder::m_plane.draw_fill();
m_canvas->m_tmp.unbindTexture();
m_canvas->m_layers[i].m_rtt.unbindTexture();
if (m_canvas->m_show_tmp && m_canvas->m_current_layer_idx == i)
{
glEnable(GL_BLEND);
ui::ShaderManager::u_float(kShaderUniform::Alpha, m_canvas->m_current_stroke->m_brush.m_tip_opacity);
m_canvas->m_tmp.bindTexture();
NodeBorder::m_plane.draw_fill();
m_canvas->m_tmp.unbindTexture();
}
}
blend ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
m_sampler.unbind();
glViewport(vp[0], vp[1], vp[2], vp[3]);
glClearColor(cc[0], cc[1], cc[2], cc[3]);
}
@@ -1948,9 +1972,21 @@ public:
m_dragging = false;
mouse_release();
break;
case kEventType::MouseDownR:
m_draggingR = true;
m_dragR_start = me->m_pos;
m_pan_start = GetPosition();
mouse_capture();
break;
case kEventType::MouseUpR:
m_draggingR = false;
mouse_release();
break;
case kEventType::MouseMove:
if (m_dragging)
m_canvas->stroke_update(cur, 1.f);
if (m_draggingR)
SetPosition(m_pan_start + me->m_pos - m_dragR_start);
break;
default:
break;