improving actions history

This commit is contained in:
2019-06-14 18:46:11 +02:00
parent ac47915658
commit c48a6da8a6
14 changed files with 388 additions and 214 deletions

View File

@@ -3,6 +3,7 @@
#include "node_panel_layer.h"
#include "canvas.h"
#include "node_combobox.h"
#include "app.h"
Node* NodeLayer::clone_instantiate() const
{
@@ -140,7 +141,7 @@ void NodePanelLayer::init()
btn_add->on_click = [this](Node*) {
add_layer();
if (on_layer_add)
on_layer_add(this);
on_layer_add(this, nullptr, 0);
if (on_layer_change)
on_layer_change(this, -1, m_layers_container->get_child_index(m_current_layer));
update_attributes();
@@ -182,10 +183,13 @@ void NodePanelLayer::init()
};
}
NodeLayer* NodePanelLayer::add_layer(const char* name)
NodeLayer* NodePanelLayer::add_layer(const char* name, bool add_history /*= true*/, std::unique_ptr<class Layer> layer /*= nullptr*/, int index /*= 0*/)
{
NodeLayer* l = new NodeLayer;
m_layers_container->add_child(l);
if (layer)
m_layers_container->add_child(l, index);
else
m_layers_container->add_child(l);
l->init();
l->create();
l->loaded();
@@ -199,7 +203,25 @@ NodeLayer* NodePanelLayer::add_layer(const char* name)
m_current_layer = l;
m_current_layer->m_selected = true;
m_layers.push_back(l);
update_attributes();
if (add_history)
{
auto a = new ActionLayerAdd;
a->m_panel = this;
a->m_layer_node = l->shared_from_this();
a->m_layer_order = m_layers_container->get_child_index(l);
ActionManager::add(a);
update_attributes();
}
else
{
if (on_layer_add)
on_layer_add(this, std::move(layer), index);
if (on_layer_change)
on_layer_change(this, -1, m_layers_container->get_child_index(m_current_layer));
update_attributes();
}
return l;
}
@@ -215,16 +237,28 @@ NodeLayer* NodePanelLayer::get_layer_at(int index)
return static_cast<NodeLayer*>(m_layers_container->get_child_at(index));
}
void NodePanelLayer::remove_layer(NodeLayer* layer)
void NodePanelLayer::remove_layer(NodeLayer* layer, bool add_history /*= true*/)
{
auto it = std::find(m_layers.begin(), m_layers.end(), m_current_layer);
auto i = m_layers_container->get_child_index(m_current_layer);
int old_idx = i;// (int)std::distance(m_layers.begin(), it);
auto copy = (*it)->shared_from_this();
m_layers_container->remove_child(m_current_layer);
m_layers.erase(it);
i = std::min<int>(i, (int)m_layers.size() - 1);
m_current_layer = (NodeLayer*)m_layers_container->get_child_at(i);
m_current_layer->m_selected = true;
if (add_history)
{
auto a = new ActionLayerRemove;
a->m_panel = this;
a->m_layer_node = copy;
a->m_layer = std::move(Canvas::I->m_layers[Canvas::I->m_order[old_idx]]);
a->m_layer_order = old_idx;
ActionManager::add(a);
}
if (on_layer_delete)
on_layer_delete(this, old_idx);
if (on_layer_change)
@@ -307,3 +341,64 @@ kEventResult NodePanelLayer::handle_event(Event* e)
}
return kEventResult::Available;
}
///////////////////////////////////////////////////////////////////////////////
Action* ActionLayerAdd::get_redo()
{
auto a = new ActionLayerRemove;
a->m_panel = m_panel;
a->m_layer_node = m_layer_node;
a->m_layer = std::move(Canvas::I->m_layers[Canvas::I->m_order[m_layer_order]]);
a->m_layer_order = m_layer_order;
return a;
}
void ActionLayerAdd::undo()
{
m_panel->remove_layer((NodeLayer*)m_layer_node.get(), false);
}
size_t ActionLayerAdd::memory()
{
return 0;
}
///////////////////////////////////////////////////////////////////////////////
Action* ActionLayerRemove::get_redo()
{
auto a = new ActionLayerAdd;
a->m_panel = m_panel;
a->m_layer_node = m_layer_node;
a->m_layer_order = m_layer_order;
return a;
}
void ActionLayerRemove::undo()
{
std::string name = m_layer->m_name;
m_panel->add_layer(name.c_str(), false, std::move(m_layer), m_layer_order);
}
size_t ActionLayerRemove::memory()
{
return 0;
}
///////////////////////////////////////////////////////////////////////////////
Action* ActionLayerMove::get_redo()
{
return nullptr;
}
void ActionLayerMove::undo()
{
}
size_t ActionLayerMove::memory()
{
return 0;
}