Files
panopainter/src/canvas_actions.cpp

147 lines
4.2 KiB
C++

#include "pch.h"
#include "log.h"
#include "app.h"
#include "canvas.h"
#include "canvas_actions.h"
#include "node_panel_layer.h"
void ActionStroke::undo()
{
if (clear_layer)
m_canvas->m_layers[m_layer_idx]->clear({ 0, 0, 0, 0 });
for (int i = 0; i < 6; i++)
{
// empty data
if (!m_image[i])
continue;
LOG("undo box %d dirty=%s [%d,%d,%d,%d] to dirty=%s [%d,%d,%d,%d]",
i,
m_canvas->m_layers[m_layer_idx]->m_dirty_face[i] ? "true" : "false",
(int)m_canvas->m_layers[m_layer_idx]->m_dirty_box[i].x,
(int)m_canvas->m_layers[m_layer_idx]->m_dirty_box[i].y,
(int)m_canvas->m_layers[m_layer_idx]->m_dirty_box[i].z,
(int)m_canvas->m_layers[m_layer_idx]->m_dirty_box[i].w,
m_old_dirty[i] ? "true" : "false",
(int)m_old_box[i].x,
(int)m_old_box[i].y,
(int)m_old_box[i].z,
(int)m_old_box[i].w);
m_canvas->m_layers[m_layer_idx]->m_dirty_box[i] = m_old_box[i];
m_canvas->m_layers[m_layer_idx]->m_dirty_face[i] = m_old_dirty[i];
glm::vec2 box_sz = zw(m_box[i]) - xy(m_box[i]);
if (box_sz.x > 0 && box_sz.y > 0 && box_sz.x <= m_canvas->m_layers[m_layer_idx]->w && box_sz.y <= m_canvas->m_layers[m_layer_idx]->h)
{
m_canvas->m_layers[m_layer_idx]->m_rtt[i].bindTexture();
glTexSubImage2D(GL_TEXTURE_2D, 0, (int)m_box[i].x, (int)m_box[i].y, (int)box_sz.x, (int)box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, m_image[i].get());
m_canvas->m_layers[m_layer_idx]->m_rtt[i].unbindTexture();
}
else
{
LOG("undo invalid box size (%d, %d)", (int)box_sz.x, (int)box_sz.y);
}
}
m_canvas->draw_merge();
}
size_t ActionStroke::memory()
{
size_t mem = 0;
for (int i = 0; i < 6; i++)
{
glm::ivec2 sz = zw(m_box[i]) - xy(m_box[i]);
mem += sz.x * sz.y * 4 + sizeof(*this);
}
return mem;
}
Action* ActionStroke::get_redo()
{
auto action = new ActionStroke;
auto& layer = m_canvas->m_layers[m_layer_idx];
for (int i = 0; i < 6; i++)
{
if (!layer->m_dirty_face[i] && !m_image[i])
continue; // no stroke on this face, skip it
layer->m_rtt[i].bindFramebuffer();
auto box = clear_layer ? glm::ivec4(layer->m_dirty_box[i]) : m_box[i];
// save image before commit
glm::vec2 box_or = xy(box);
glm::vec2 box_sz = zw(box) - xy(box);
if (box_sz.x > 0 && box_sz.y > 0 && box_sz.x <= layer->w && box_sz.y <= layer->h)
{
action->m_image[i] = std::make_unique<uint8_t[]>(box_sz.x * box_sz.y * 4);
glReadPixels(box_or.x, box_or.y, box_sz.x, box_sz.y, GL_RGBA, GL_UNSIGNED_BYTE, action->m_image[i].get());
}
else
{
LOG("create_action invalid box size (%d, %d)", (int)box_sz.x, (int)box_sz.y);
}
action->m_box[i] = box;
action->m_old_box[i] = layer->m_dirty_box[i];
action->m_old_dirty[i] = layer->m_dirty_face[i];
layer->m_rtt[i].unbindFramebuffer();
}
// save history
action->m_layer_idx = m_layer_idx;
action->m_canvas = m_canvas;
//action->m_stroke = std::move(m_stroke);
action->clear_layer = false;
return action;
}
//////////////////////////////////////////////////////////////////////////
Action* ActionLayerClear::get_redo()
{
auto a = new ActionLayerClear;
a->m_direction = reverse_direction();
a->m_snap = m_snap;
a->m_layer = m_layer;
a->m_color = m_color;
return a;
}
void ActionLayerClear::undo()
{
if (m_direction == Direction::Undo)
{
m_layer->restore(*m_snap);
}
else
{
m_layer->clear(m_color);
}
}
//////////////////////////////////////////////////////////////////////////
Action* ActionImportEquirect::get_redo()
{
auto a = new ActionImportEquirect;
a->m_direction = reverse_direction();
a->m_snap = m_snap;
a->m_layer = m_layer;
a->m_path = m_path;
return a;
}
void ActionImportEquirect::undo()
{
if (m_direction == Direction::Undo)
{
m_layer->restore(*m_snap);
}
else
{
Canvas::I->import_equirectangular_thread(m_path, m_layer);
}
}