implement simple stroke undo based on actions history
This commit is contained in:
@@ -40,6 +40,7 @@ add_library(
|
||||
../engine/brush.cpp
|
||||
../engine/canvas.cpp
|
||||
../engine/log.cpp
|
||||
../engine/action.cpp
|
||||
)
|
||||
|
||||
target_include_directories(native-lib PRIVATE
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
#include "pch.h"
|
||||
#include "app.h"
|
||||
#include "..\..\..\..\engine\asset.h"
|
||||
#include "../../../../engine/asset.h"
|
||||
|
||||
typedef void (*GLDEBUGPROC)(GLenum source,
|
||||
GLenum type,
|
||||
|
||||
@@ -371,6 +371,7 @@
|
||||
</button-custom>-->
|
||||
|
||||
<button id="btn-switch" width="50" height="50" margin="0 0 5 0" text="Switch"/>
|
||||
<button id="btn-undo" width="50" height="50" margin="0 0 5 0" text="Undo"/>
|
||||
<button id="btn-close" width="50" height="50" margin="0 0 5 0" text="Clear"/>
|
||||
<button-custom id="btn-bucket" width="50" height="50" margin="0 0 5 0" thickness="1" border-color=".1" pad="2">
|
||||
<image path="data/ui/bucket.png" width="100%" height="100%" align="center" justify="flex-end"/>
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
AD29CC621EA2B214008C8BFA /* action.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD29CC601EA2B214008C8BFA /* action.cpp */; };
|
||||
AD3B1EC01E3B8B7600E918E3 /* layout.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD3B1EBE1E3B8B7600E918E3 /* layout.cpp */; };
|
||||
AD4C08D91E89BD0F0051D85F /* asset.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD4C08CF1E89BD0F0051D85F /* asset.cpp */; };
|
||||
AD4C08DA1E89BD0F0051D85F /* bezier.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD4C08D11E89BD0F0051D85F /* bezier.cpp */; };
|
||||
@@ -48,6 +49,8 @@
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
AD29CC601EA2B214008C8BFA /* action.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = action.cpp; sourceTree = "<group>"; };
|
||||
AD29CC611EA2B214008C8BFA /* action.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = action.h; sourceTree = "<group>"; };
|
||||
AD3B1EBE1E3B8B7600E918E3 /* layout.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = layout.cpp; sourceTree = "<group>"; };
|
||||
AD3B1EBF1E3B8B7600E918E3 /* layout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = layout.h; sourceTree = "<group>"; };
|
||||
AD4C08CF1E89BD0F0051D85F /* asset.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = asset.cpp; sourceTree = "<group>"; };
|
||||
@@ -133,6 +136,8 @@
|
||||
AD58E0511E107411006ACC15 /* engine */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
AD29CC601EA2B214008C8BFA /* action.cpp */,
|
||||
AD29CC611EA2B214008C8BFA /* action.h */,
|
||||
AD8CF71F1E913F0500083FFD /* log.cpp */,
|
||||
AD8CF7201E913F0500083FFD /* log.h */,
|
||||
AD4C08CF1E89BD0F0051D85F /* asset.cpp */,
|
||||
@@ -235,6 +240,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
AD58E0791E342205006ACC15 /* tinyxml2.cpp in Sources */,
|
||||
AD29CC621EA2B214008C8BFA /* action.cpp in Sources */,
|
||||
AD58E06F1E2A80BC006ACC15 /* shape.cpp in Sources */,
|
||||
AD58E0651E2A76FD006ACC15 /* shader.cpp in Sources */,
|
||||
AD4C08DA1E89BD0F0051D85F /* bezier.cpp in Sources */,
|
||||
|
||||
4
engine/action.cpp
Normal file
4
engine/action.cpp
Normal file
@@ -0,0 +1,4 @@
|
||||
#include "pch.h"
|
||||
#include "action.h"
|
||||
|
||||
ActionManager ActionManager::I;
|
||||
24
engine/action.h
Normal file
24
engine/action.h
Normal 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();
|
||||
}
|
||||
};
|
||||
@@ -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*) {
|
||||
|
||||
@@ -109,6 +109,9 @@ void ui::Canvas::stroke_draw()
|
||||
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);
|
||||
//m_plane.update_vertices(P);
|
||||
@@ -137,6 +140,11 @@ void ui::Canvas::stroke_commit()
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
#include <stack>
|
||||
#include <regex>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
Reference in New Issue
Block a user