log history memory usage add fix virtual dtor for Action subclasses to be deleted from memory

This commit is contained in:
2017-05-10 13:41:27 +01:00
parent eb8f93af91
commit 6f785c1944
6 changed files with 50 additions and 21 deletions

View File

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

View File

@@ -5,6 +5,8 @@ class Action
public:
virtual void run() = 0;
virtual void undo() = 0;
virtual size_t memory() = 0;
virtual ~Action(){};
};
class ActionManager
@@ -12,13 +14,22 @@ class ActionManager
public:
static ActionManager I;
std::stack<std::unique_ptr<Action>> m_actions;
size_t m_memory = 0;
static void add(Action* action)
{
I.m_actions.emplace(action);
I.m_memory += action->memory();
LOG("History: %.2f KB", I.m_memory / 1024.f);
}
static void undo()
{
I.m_actions.top()->undo();
I.m_memory -= I.m_actions.top()->memory();
I.m_actions.pop();
LOG("History: %.2f KB", I.m_memory / 1024.f);
}
static bool empty()
{
return I.m_actions.empty();
}
};

View File

@@ -623,7 +623,8 @@ void App::initLayout()
if (auto* button = layout[main_id]->find<NodeButton>("btn-undo"))
{
button->on_click = [this,button](Node*) {
ActionManager::undo();
if (!ActionManager::empty())
ActionManager::undo();
};
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-clear"))

View File

@@ -157,6 +157,21 @@ public:
m_canvas->m_layers[m_layer_idx].m_rtt[i].unbindTexture();
}
}
virtual size_t memory() override
{
size_t mem = 0;
for (int i = 0; i < 6; i++)
{
glm::vec2 sz = m_box[i].zw() - m_box[i].xy();
mem += sz.x * sz.y * 4 + sizeof(*this);
}
return mem;
}
virtual ~ActionStroke()
{
LOG("ActionStroke destroyed: free %zu bytes", memory());
}
};
NS_END

View File

@@ -353,6 +353,7 @@ void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
case kEventType::MouseCancel:
m_dragging = false;
node->mouse_release();
m_points.clear();
break;
default:
break;