add undo action for bucket fill
This commit is contained in:
@@ -1549,11 +1549,6 @@ void CanvasModeTransform::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
}
|
||||
}
|
||||
|
||||
void CanvasModeFloodFill::init()
|
||||
{
|
||||
TextureManager::load(m_cursor_path.c_str());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
void CanvasModeFloodFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
@@ -1566,6 +1561,48 @@ void CanvasModeFloodFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
if (Canvas::I->m_touch_lock && me->m_source == kEventSource::Touch)
|
||||
return;
|
||||
|
||||
struct ActionFloodFill : public Action
|
||||
{
|
||||
std::shared_ptr<Layer> m_layer;
|
||||
std::shared_ptr<Layer::Snapshot> m_snap;
|
||||
glm::ivec2 m_pos;
|
||||
glm::vec4 m_color;
|
||||
float m_threshold;
|
||||
int m_layer_index;
|
||||
int m_plane;
|
||||
virtual void run() override { }
|
||||
virtual size_t memory() override { return m_snap->memsize(); }
|
||||
virtual Action* get_redo() override
|
||||
{
|
||||
auto a = new ActionFloodFill;
|
||||
a->m_direction = (Direction)(1 - (int)m_direction);
|
||||
a->m_layer = m_layer;
|
||||
a->m_snap = m_snap;
|
||||
a->m_pos = m_pos;
|
||||
a->m_color = m_color;
|
||||
a->m_layer_index = m_layer_index;
|
||||
a->m_threshold = m_threshold;
|
||||
a->m_plane = m_plane;
|
||||
return a;
|
||||
}
|
||||
virtual void undo() override
|
||||
{
|
||||
if (m_direction == Direction::Undo)
|
||||
{
|
||||
m_layer->restore(*m_snap);
|
||||
}
|
||||
else
|
||||
{
|
||||
Canvas::FloodData plane_data;
|
||||
std::unique_ptr<glm::vec4> color;
|
||||
Canvas::I->flood_fill(m_layer_index, m_plane, { m_pos },
|
||||
plane_data, m_threshold, m_color, color);
|
||||
plane_data.apply();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
switch (me->m_type)
|
||||
{
|
||||
case kEventType::MouseUpL:
|
||||
@@ -1574,10 +1611,23 @@ void CanvasModeFloodFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
int plane;
|
||||
if (Canvas::I->point_trace(loc, ro, rd, hit, pos, n, plane))
|
||||
{
|
||||
std::map<int, std::unique_ptr<bool[]>> plane_mask;
|
||||
Canvas::FloodData plane_data;
|
||||
std::unique_ptr<glm::vec4> color;
|
||||
Canvas::I->flood_fill(Canvas::I->m_current_layer_idx, plane, { (glm::ivec2)pos },
|
||||
plane_mask, m_tool->get_threshold(), Canvas::I->m_current_brush->m_tip_color, color);
|
||||
plane_data, m_tool->get_threshold(), Canvas::I->m_current_brush->m_tip_color, color);
|
||||
|
||||
auto a = new ActionFloodFill;
|
||||
a->m_direction = Action::Direction::Undo;
|
||||
a->m_layer = plane_data.layer;
|
||||
a->m_snap = std::make_shared<Layer::Snapshot>(plane_data.layer->snapshot());
|
||||
a->m_pos = (glm::ivec2)pos;
|
||||
a->m_color = Canvas::I->m_current_brush->m_tip_color;
|
||||
a->m_layer_index = Canvas::I->m_current_layer_idx;
|
||||
a->m_threshold = m_tool->get_threshold();
|
||||
a->m_plane = plane;
|
||||
ActionManager::add(a);
|
||||
|
||||
plane_data.apply();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -1585,6 +1635,11 @@ void CanvasModeFloodFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
}
|
||||
}
|
||||
|
||||
void CanvasModeFloodFill::init()
|
||||
{
|
||||
TextureManager::load(m_cursor_path.c_str());
|
||||
}
|
||||
|
||||
void CanvasModeFloodFill::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera)
|
||||
{
|
||||
if (m_draw_tip)
|
||||
|
||||
Reference in New Issue
Block a user