fix undo and redo
This commit is contained in:
133
src/canvas.cpp
133
src/canvas.cpp
@@ -180,46 +180,10 @@ void Canvas::snap_history(const std::vector<int>& planes)
|
||||
// save history
|
||||
action->m_layer_idx = m_current_layer_idx;
|
||||
action->m_canvas = this;
|
||||
action->m_stroke = std::move(m_current_stroke);
|
||||
//action->m_stroke = std::move(m_current_stroke);
|
||||
action->clear_layer = true;
|
||||
ActionManager::add(action);
|
||||
}
|
||||
ActionStroke* Canvas::create_action(int layer)
|
||||
{
|
||||
auto action = new ActionStroke;
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (!m_layers[layer].m_dirty_face[i])
|
||||
continue; // no stroke on this face, skip it
|
||||
|
||||
m_layers[layer].m_rtt[i].bindFramebuffer();
|
||||
|
||||
// save image before commit
|
||||
glm::vec2 box_or = xy(m_layers[layer].m_dirty_box[i]);
|
||||
glm::vec2 box_sz = zw(m_layers[layer].m_dirty_box[i]) - xy(m_layers[layer].m_dirty_box[i]);
|
||||
if (box_sz.x > 0 && box_sz.y > 0 && box_sz.x <= m_layers[m_current_layer_idx].w && box_sz.y <= m_layers[m_current_layer_idx].h)
|
||||
{
|
||||
action->m_image[i] = std::make_unique<uint8_t[]>(box_sz.x * box_sz.y * 4);
|
||||
glReadPixels(box_or.x, box_or.y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, action->m_image[i].get());
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG("create_action invalid box size (%d, %d)", (int)box_sz.x, (int)box_sz.y);
|
||||
}
|
||||
|
||||
action->m_box[i] = m_layers[layer].m_dirty_box[i];
|
||||
action->m_old_box[i] = m_layers[layer].m_dirty_box[i];
|
||||
action->m_old_dirty[i] = m_layers[layer].m_dirty_face[i];
|
||||
|
||||
m_layers[layer].m_rtt[i].unbindFramebuffer();
|
||||
}
|
||||
// save history
|
||||
action->m_layer_idx = layer;
|
||||
action->m_canvas = this;
|
||||
action->m_stroke = std::move(m_current_stroke);
|
||||
action->clear_layer = true;
|
||||
return action;
|
||||
}
|
||||
void Canvas::stroke_end()
|
||||
{
|
||||
if (!m_current_stroke)
|
||||
@@ -924,7 +888,7 @@ void Canvas::stroke_commit()
|
||||
// save history
|
||||
action->m_layer_idx = m_current_layer_idx;
|
||||
action->m_canvas = this;
|
||||
action->m_stroke = std::move(m_current_stroke);
|
||||
//action->m_stroke = std::move(m_current_stroke);
|
||||
ActionManager::add(action);
|
||||
}
|
||||
void Canvas::stroke_update(glm::vec3 point, float pressure)
|
||||
@@ -2375,7 +2339,7 @@ void Canvas::draw_objects(std::function<void(const glm::mat4& camera, const glm:
|
||||
// save history
|
||||
action->m_layer_idx = m_current_layer_idx;
|
||||
action->m_canvas = this;
|
||||
action->m_stroke = std::move(m_current_stroke);
|
||||
//action->m_stroke = std::move(m_current_stroke);
|
||||
ActionManager::add(action);
|
||||
|
||||
glDeleteRenderbuffers(1, &rboID);
|
||||
@@ -2824,3 +2788,94 @@ void Layer::resize(int width, int height)
|
||||
//m_dirty_face[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void ActionStroke::undo()
|
||||
{
|
||||
if (clear_layer)
|
||||
m_canvas->m_layers[m_layer_idx].clear({ 0, 0, 0, 0 });
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
// empty data
|
||||
if (!m_image[i])
|
||||
continue;
|
||||
|
||||
LOG("undo box %d dirty=%s [%d,%d,%d,%d] to dirty=%s [%d,%d,%d,%d]",
|
||||
i,
|
||||
m_canvas->m_layers[m_layer_idx].m_dirty_face[i] ? "true" : "false",
|
||||
(int)m_canvas->m_layers[m_layer_idx].m_dirty_box[i].x,
|
||||
(int)m_canvas->m_layers[m_layer_idx].m_dirty_box[i].y,
|
||||
(int)m_canvas->m_layers[m_layer_idx].m_dirty_box[i].z,
|
||||
(int)m_canvas->m_layers[m_layer_idx].m_dirty_box[i].w,
|
||||
m_old_dirty[i] ? "true" : "false",
|
||||
(int)m_old_box[i].x,
|
||||
(int)m_old_box[i].y,
|
||||
(int)m_old_box[i].z,
|
||||
(int)m_old_box[i].w);
|
||||
m_canvas->m_layers[m_layer_idx].m_dirty_box[i] = m_old_box[i];
|
||||
m_canvas->m_layers[m_layer_idx].m_dirty_face[i] = m_old_dirty[i];
|
||||
|
||||
|
||||
glm::vec2 box_sz = zw(m_box[i]) - xy(m_box[i]);
|
||||
if (box_sz.x > 0 && box_sz.y > 0 && box_sz.x <= m_canvas->m_layers[m_layer_idx].w && box_sz.y <= m_canvas->m_layers[m_layer_idx].h)
|
||||
{
|
||||
m_canvas->m_layers[m_layer_idx].m_rtt[i].bindTexture();
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, (int)m_box[i].x, (int)m_box[i].y, (int)box_sz.x, (int)box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, m_image[i].get());
|
||||
m_canvas->m_layers[m_layer_idx].m_rtt[i].unbindTexture();
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG("undo invalid box size (%d, %d)", (int)box_sz.x, (int)box_sz.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t ActionStroke::memory()
|
||||
{
|
||||
size_t mem = 0;
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
glm::ivec2 sz = zw(m_box[i]) - xy(m_box[i]);
|
||||
mem += sz.x * sz.y * 4 + sizeof(*this);
|
||||
}
|
||||
return mem;
|
||||
}
|
||||
|
||||
Action* ActionStroke::get_redo()
|
||||
{
|
||||
auto action = new ActionStroke;
|
||||
auto& layer = m_canvas->m_layers[m_layer_idx];
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (!layer.m_dirty_face[i] && !m_image[i])
|
||||
continue; // no stroke on this face, skip it
|
||||
|
||||
layer.m_rtt[i].bindFramebuffer();
|
||||
|
||||
// save image before commit
|
||||
glm::vec2 box_or = xy(m_box[i]);
|
||||
glm::vec2 box_sz = zw(m_box[i]) - xy(m_box[i]);
|
||||
if (box_sz.x > 0 && box_sz.y > 0 && box_sz.x <= layer.w && box_sz.y <= layer.h)
|
||||
{
|
||||
action->m_image[i] = std::make_unique<uint8_t[]>(box_sz.x * box_sz.y * 4);
|
||||
glReadPixels(box_or.x, box_or.y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, action->m_image[i].get());
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG("create_action invalid box size (%d, %d)", (int)box_sz.x, (int)box_sz.y);
|
||||
}
|
||||
|
||||
action->m_box[i] = m_box[i];
|
||||
action->m_old_box[i] = layer.m_dirty_box[i];
|
||||
action->m_old_dirty[i] = layer.m_dirty_face[i];
|
||||
|
||||
layer.m_rtt[i].unbindFramebuffer();
|
||||
}
|
||||
// save history
|
||||
action->m_layer_idx = m_layer_idx;
|
||||
action->m_canvas = m_canvas;
|
||||
//action->m_stroke = std::move(m_stroke);
|
||||
action->clear_layer = false;
|
||||
return action;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user