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

4
engine/action.cpp Normal file
View File

@@ -0,0 +1,4 @@
#include "pch.h"
#include "action.h"
ActionManager ActionManager::I;

24
engine/action.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
class Action
{
public:
virtual void run() = 0;
virtual void undo() = 0;
};
class ActionManager
{
public:
static ActionManager I;
std::stack<std::unique_ptr<Action>> m_actions;
static void add(Action* action)
{
I.m_actions.emplace(action);
}
static void undo()
{
I.m_actions.top()->undo();
I.m_actions.pop();
}
};

View File

@@ -420,6 +420,12 @@ void App::initLayout()
};
button->m_text->set_text("NORM");
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-undo"))
{
button->on_click = [this,button](Node*) {
ActionManager::undo();
};
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-close"))
{
button->on_click = [this](Node*) {

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)
{

View File

@@ -4,6 +4,7 @@
#include "shader.h"
#include "shape.h"
#include "brush.h"
#include "action.h"
NS_START
@@ -15,14 +16,14 @@ class Canvas
public:
bool m_erase = false;
glm::mat4 m_mvp;
glm::vec4 m_box;
int m_width;
int m_height;
bool m_use_instanced = false;
int m_current_layer_idx = 0;
Stroke* m_current_stroke = nullptr;
std::unique_ptr<Stroke> m_current_stroke;
bool m_show_tmp = false;
std::vector<Layer> m_layers;
std::vector<Stroke> m_strokes;
std::vector<int> m_order;
RTT m_tmp;
Texture2D m_tex;
@@ -42,4 +43,28 @@ public:
void clear(const glm::vec4& color = { 1, 1, 1, 1 });
};
class ActionStroke : public Action
{
public:
std::unique_ptr<Stroke> m_stroke;
std::unique_ptr<uint8_t[]> m_image;
glm::ivec4 m_box;
int m_layer_idx;
Canvas* m_canvas;
virtual void run() override
{
}
virtual void undo() override
{
m_canvas->m_layers[m_stroke->m_layer].m_rtt.bindTexture();
glm::vec2 box_sz = m_box.zw() - m_box.xy();
glTexSubImage2D(GL_TEXTURE_2D, 0, m_box.x, m_box.y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, m_image.get());
m_canvas->m_layers[m_stroke->m_layer].m_rtt.unbindTexture();
}
};
NS_END

View File

@@ -46,6 +46,7 @@
#include <stack>
#include <regex>
#include <mutex>
#include <queue>
#include <memory>
#include <string>
#include <vector>