From dabfedb089146dcdf201b770049b6eb1cc1d3011 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 19 Jun 2019 23:05:14 +0200 Subject: [PATCH] improve layer history, fix doc open --- src/canvas.cpp | 12 ++++----- src/node_panel_layer.cpp | 53 +++++++++++++++++++++++++++++++++++++++- src/node_panel_layer.h | 17 +++++++++++++ 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/canvas.cpp b/src/canvas.cpp index 6e9a7b1..921db02 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -2500,15 +2500,15 @@ bool Canvas::project_open_thread(std::string file_path) resize(m_width, m_height); App::I.async_end(); - std::vector> tmp_layers; + std::vector> tmp_layers(n_layers); for (int i = 0; i < n_layers; i++) { int n_order; fread(&n_order, sizeof(int), 1, fp); - tmp_layers.push_back(std::make_unique()); - auto& layer = tmp_layers.back(); + tmp_layers[n_order] = std::make_unique(); + auto& layer = tmp_layers[n_order]; fread(&layer->m_opacity, sizeof(float), 1, fp); @@ -2567,9 +2567,9 @@ bool Canvas::project_open_thread(std::string file_path) } App::I.async_start(); - tmp_layers.back()->create(m_width, m_height, name.c_str()); - tmp_layers.back()->clear({ 0, 0, 0, 0 }); - tmp_layers.back()->restore(snap); + layer->create(m_width, m_height, name.c_str()); + layer->clear({ 0, 0, 0, 0 }); + layer->restore(snap); App::I.async_end(); } diff --git a/src/node_panel_layer.cpp b/src/node_panel_layer.cpp index d70a391..24c3175 100644 --- a/src/node_panel_layer.cpp +++ b/src/node_panel_layer.cpp @@ -199,7 +199,7 @@ NodeLayer* NodePanelLayer::add_layer(const char* name, bool add_history /*= true std::shared_ptr layer /*= nullptr*/, std::shared_ptr layer_node /*= nullptr*/, int index /*= 0*/) { if (index == -1) - index = m_layers_container->m_children.size(); + index = (int)m_layers_container->m_children.size(); auto l = layer_node ? layer_node : std::make_shared(); m_layers_container->add_child(l, index); l->init(); @@ -302,18 +302,21 @@ void NodePanelLayer::handle_layer_highlight(NodeLayer* target, bool highlight) void NodePanelLayer::handle_layer_visibility(NodeLayer* target, bool visible) { + save_history(); if (on_layer_visibility_changed) on_layer_visibility_changed(this, m_layers_container->get_child_index(target), visible); } void NodePanelLayer::handle_layer_alpha_lock(NodeLayer* target, bool locked) { + save_history(); if (on_layer_alpha_lock_changed) on_layer_alpha_lock_changed(this, m_layers_container->get_child_index(target), locked); } void NodePanelLayer::handle_layer_blend_mode(NodeLayer* target, int mode) { + save_history(); if (on_layer_blend_mode_changed) on_layer_blend_mode_changed(this, m_layers_container->get_child_index(target), mode); } @@ -329,6 +332,21 @@ void NodePanelLayer::handle_layer_selected(NodeLayer* target) update_attributes(); } +void NodePanelLayer::save_history() +{ + auto a = new ActionLayerChange; + a->m_panel = this; + for (auto const& l : Canvas::I->m_layers) + { + ActionLayerChange::LayerState s; + s.alpha = l->m_opacity; + s.lock = l->m_alpha_locked; + s.visible = l->m_visible; + a->m_layers.push_back(s); + } + ActionManager::add(a); +} + void NodePanelLayer::clear() { m_layers_container->remove_all_children(); @@ -342,6 +360,10 @@ void NodePanelLayer::update_attributes() m_opacity->set_value(l->m_opacity); m_alpha_lock->set_value(l->m_alpha_locked); m_blend_mode->set_index(l->m_blend_mode); + for (int i = 0; i < Canvas::I->m_layers.size(); i++) + { + m_layers[i]->m_visibility->set_value(Canvas::I->m_layers[i]->m_visible); + } } kEventResult NodePanelLayer::handle_event(Event* e) @@ -487,3 +509,32 @@ Action* ActionLayerMerge::get_redo() a->m_direction = m_direction == Direction::Undo ? Direction::Redo : Direction::Undo; return a; } + +/////////////////////////////////////////////////////////////////////////////// + +void ActionLayerChange::undo() +{ + for (int i = 0; i < m_layers.size(); i++) + { + auto& l = Canvas::I->m_layers[i]; + l->m_opacity = m_layers[i].alpha; + l->m_alpha_locked = m_layers[i].lock; + l->m_visible = m_layers[i].visible; + } + m_panel->update_attributes(); +} + +Action* ActionLayerChange::get_redo() +{ + auto a = new ActionLayerChange; + a->m_panel = m_panel; + for (auto const& l : Canvas::I->m_layers) + { + LayerState s; + s.alpha = l->m_opacity; + s.lock = l->m_alpha_locked; + s.visible = l->m_visible; + a->m_layers.push_back(s); + } + return a; +} diff --git a/src/node_panel_layer.h b/src/node_panel_layer.h index 47762e3..6915db4 100644 --- a/src/node_panel_layer.h +++ b/src/node_panel_layer.h @@ -74,10 +74,27 @@ public: void handle_layer_highlight(NodeLayer* target, bool highlight); void handle_layer_blend_mode(NodeLayer* target, int mode); void handle_layer_selected(NodeLayer* target); + void save_history(); void clear(); void update_attributes(); }; +struct ActionLayerChange : public Action +{ + struct LayerState + { + bool visible; + float alpha; + bool lock; + }; + NodePanelLayer* m_panel; + std::vector m_layers; + virtual void run() override { } + virtual Action* get_redo() override; + virtual void undo() override; + virtual size_t memory() override { return 0; } +}; + class ActionLayerAdd : public Action { public: