implement simple stroke undo based on actions history

This commit is contained in:
2017-04-16 01:57:54 +02:00
parent 357f37e3d0
commit 28fe61704f
10 changed files with 95 additions and 9 deletions

View File

@@ -100,7 +100,7 @@ void ui::Canvas::stroke_draw()
bb_max = glm::min({ m_width, m_height }, glm::max(bb_max, P2[i].xy()));
}
auto bb_sz = bb_max - bb_min;
glm::vec2 pad(1);
glm::ivec2 tex_pos = glm::clamp(glm::floor(bb_min) - pad , { 0, 0 }, { m_width, m_height });
glm::ivec2 tex_sz = glm::clamp(glm::ceil(bb_sz ) + pad*2.f, { 0, 0 }, (glm::vec2)(glm::ivec2(m_width, m_height) - tex_pos));
@@ -108,6 +108,9 @@ void ui::Canvas::stroke_draw()
tex_pos.x, tex_pos.y,
tex_pos.x, tex_pos.y,
tex_sz.x, tex_sz.y);
m_box.xy = glm::min(m_box.xy(), (glm::vec2)tex_pos);
m_box.zw = glm::max(m_box.zw(), (glm::vec2)(tex_pos + tex_sz));
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
ShaderManager::u_float(kShaderUniform::Alpha, s.flow);
@@ -136,7 +139,12 @@ void ui::Canvas::stroke_commit()
m_dirty = false;
m_layers[m_current_layer_idx].m_rtt.bindFramebuffer();
// save image before commit
glm::vec2 box_sz = m_box.zw() - m_box.xy();
auto image = std::make_unique<uint8_t[]>(box_sz.x * box_sz.y * 4);
glReadPixels(m_box.x, m_box.y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, image.get());
// copy to tmp2 for layer blending
m_tex2.bind();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height);
@@ -181,6 +189,15 @@ void ui::Canvas::stroke_commit()
glClearColor(cc[0], cc[1], cc[2], cc[3]);
m_layers[m_current_layer_idx].m_rtt.unbindFramebuffer();
// save history
auto action = new ActionStroke;
action->m_image = std::move(image);
action->m_box = m_box;
action->m_layer_idx = m_current_layer_idx;
action->m_canvas = this;
action->m_stroke = std::move(m_current_stroke);
ActionManager::add(action);
}
void ui::Canvas::stroke_update(glm::vec2 point, float pressure)
{
@@ -188,10 +205,11 @@ void ui::Canvas::stroke_update(glm::vec2 point, float pressure)
}
void ui::Canvas::stroke_start(glm::vec2 point, float pressure, const ui::Brush& brush)
{
m_strokes.emplace_back();
m_strokes.back().start(brush);
m_strokes.back().add_point(point, pressure);
m_current_stroke = &m_strokes.back();
m_current_stroke = std::make_unique<Stroke>();
m_current_stroke->start(brush);
m_current_stroke->add_point(point, pressure);
m_box = glm::vec4(m_width, m_height, 0, 0); // reset bounding box
if (m_erase)
{