fix undo and redo

This commit is contained in:
2019-02-01 17:47:32 +01:00
parent 91d6905238
commit e3f6d1cc44
5 changed files with 1672 additions and 1536 deletions

File diff suppressed because one or more lines are too long

View File

@@ -8,7 +8,7 @@ public:
virtual void undo() = 0;
virtual Action* get_redo() = 0;
virtual size_t memory() = 0;
virtual ~Action(){};
virtual ~Action() = default;
Action() = default;
};

View File

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

View File

@@ -236,7 +236,6 @@ public:
void snapshot_save();
void snapshot_restore();
void snap_history(const std::vector<int>& planes);
class ActionStroke* create_action(int layer);
void clear_context();
void import_equirectangular(std::string file_path);
void import_equirectangular_thread(std::string file_path);
@@ -278,75 +277,18 @@ public:
class ActionStroke : public Action
{
public:
std::unique_ptr<Stroke> m_stroke;
//std::unique_ptr<Stroke> m_stroke;
std::unique_ptr<uint8_t[]> m_image[6] = SIXPLETTE(nullptr);
glm::ivec4 m_old_box[6] = SIXPLETTE(glm::ivec4(0));
bool m_old_dirty[6] = SIXPLETTE(false);
glm::ivec4 m_box[6] = SIXPLETTE(glm::ivec4(0));
bool m_dirty[6] = SIXPLETTE(false);
bool clear_layer = false;
int m_layer_idx;
int m_layer_idx = 0;
Canvas* m_canvas;
ActionStroke() = default;
virtual void run() override
{
}
virtual Action* get_redo() override
{
auto redo = m_canvas->create_action(m_layer_idx);
return redo;
}
virtual void undo() override
{
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);
}
}
}
virtual size_t memory() override
{
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;
}
virtual ~ActionStroke()
{
}
virtual ~ActionStroke() = default;
virtual void run() override { }
virtual Action* get_redo() override;
virtual void undo() override;
virtual size_t memory() override;
};

View File

@@ -296,8 +296,25 @@ void NodeCanvas::draw()
m_blender_rtt.unbindTexture();
glActiveTexture(GL_TEXTURE0);
m_blender_rtt.unbindTexture();
}
#ifdef _DEBUG
// draw dirty area
{
auto bb = m_canvas->m_layers[layer_index].m_dirty_box[plane_index] / (float)m_canvas->m_layers[layer_index].w;
glm::vec2 bbmin = xy(bb);
glm::vec2 bbsz = zw(bb) - xy(bb);
ShaderManager::use(kShader::Color);
ShaderManager::u_vec4(kShaderUniform::Col, { 1, 0, 0, 1 });
ShaderManager::u_mat4(kShaderUniform::MVP, plane_mvp_z
* glm::translate(glm::vec3(bbmin * 2.f, 0))
* glm::translate(glm::vec3(-1, -1, 0))
* glm::scale(glm::vec3(bbsz, 1))
* glm::translate(glm::vec3(1, 1, 0))
);
m_face_plane.draw_stroke();
}
#endif
}
}
if (use_blend)