diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt
index 2599bcd..38b70c7 100644
--- a/android/CMakeLists.txt
+++ b/android/CMakeLists.txt
@@ -40,6 +40,7 @@ add_library(
../engine/brush.cpp
../engine/canvas.cpp
../engine/log.cpp
+ ../engine/action.cpp
)
target_include_directories(native-lib PRIVATE
diff --git a/android/src/main/cpp/main.cpp b/android/src/main/cpp/main.cpp
index b8e9406..8bedff9 100755
--- a/android/src/main/cpp/main.cpp
+++ b/android/src/main/cpp/main.cpp
@@ -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,
diff --git a/data/layout.xml b/data/layout.xml
index fa9c48c..40d2143 100644
--- a/data/layout.xml
+++ b/data/layout.xml
@@ -371,6 +371,7 @@
-->
+
diff --git a/engine.xcodeproj/project.pbxproj b/engine.xcodeproj/project.pbxproj
index 6b077b8..ea7fbfe 100644
--- a/engine.xcodeproj/project.pbxproj
+++ b/engine.xcodeproj/project.pbxproj
@@ -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 = ""; };
+ AD29CC611EA2B214008C8BFA /* action.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = action.h; sourceTree = ""; };
AD3B1EBE1E3B8B7600E918E3 /* layout.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = layout.cpp; sourceTree = ""; };
AD3B1EBF1E3B8B7600E918E3 /* layout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = layout.h; sourceTree = ""; };
AD4C08CF1E89BD0F0051D85F /* asset.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = asset.cpp; sourceTree = ""; };
@@ -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 */,
diff --git a/engine/action.cpp b/engine/action.cpp
new file mode 100644
index 0000000..084943c
--- /dev/null
+++ b/engine/action.cpp
@@ -0,0 +1,4 @@
+#include "pch.h"
+#include "action.h"
+
+ActionManager ActionManager::I;
diff --git a/engine/action.h b/engine/action.h
new file mode 100644
index 0000000..b8e902f
--- /dev/null
+++ b/engine/action.h
@@ -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> m_actions;
+ static void add(Action* action)
+ {
+ I.m_actions.emplace(action);
+ }
+ static void undo()
+ {
+ I.m_actions.top()->undo();
+ I.m_actions.pop();
+ }
+};
diff --git a/engine/app.cpp b/engine/app.cpp
index 3faa1af..f0bc32d 100644
--- a/engine/app.cpp
+++ b/engine/app.cpp
@@ -420,6 +420,12 @@ void App::initLayout()
};
button->m_text->set_text("NORM");
}
+ if (auto* button = layout[main_id]->find("btn-undo"))
+ {
+ button->on_click = [this,button](Node*) {
+ ActionManager::undo();
+ };
+ }
if (auto* button = layout[main_id]->find("btn-close"))
{
button->on_click = [this](Node*) {
diff --git a/engine/canvas.cpp b/engine/canvas.cpp
index e45c1d5..1e75c91 100644
--- a/engine/canvas.cpp
+++ b/engine/canvas.cpp
@@ -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(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();
+ 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)
{
diff --git a/engine/canvas.h b/engine/canvas.h
index ac4c6f5..27cf9db 100644
--- a/engine/canvas.h
+++ b/engine/canvas.h
@@ -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 m_current_stroke;
bool m_show_tmp = false;
std::vector m_layers;
- std::vector m_strokes;
std::vector 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 m_stroke;
+ std::unique_ptr 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
diff --git a/engine/pch.h b/engine/pch.h
index d922bfb..7215e66 100644
--- a/engine/pch.h
+++ b/engine/pch.h
@@ -46,6 +46,7 @@
#include
#include
#include
+#include
#include
#include
#include