From bafde8e9db4d7c394144fafaba27294a4343ee9d Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 20 Jun 2022 23:22:52 +0200 Subject: [PATCH] Ported LineAction. --- modules/paint/actions/line_action.cpp | 85 ++++++++++++++++----------- modules/paint/actions/line_action.h | 1 + 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/modules/paint/actions/line_action.cpp b/modules/paint/actions/line_action.cpp index 6c903513d..e90777fe9 100644 --- a/modules/paint/actions/line_action.cpp +++ b/modules/paint/actions/line_action.cpp @@ -31,46 +31,61 @@ SOFTWARE. void LineAction::do_action(PaintCanvas *canvas, const Array &data) { PaintAction::do_action(canvas, data); - /* - .do_action(canvas, data) - - if mouse_start_pos == null: - mouse_start_pos = data[0] - - action_data.preview.cells.clear() - action_data.preview.colors.clear() - canvas.clear_preview_layer() - - var pixels = GEUtils.get_pixels_in_line(data[0], mouse_start_pos) - for pixel in pixels: - if canvas.is_alpha_locked() and canvas.get_pixel_v(pixel) == Color.transparent: - continue - - canvas.set_preview_pixel_v(pixel, data[2]) - action_data.preview.cells.append(pixel) - action_data.preview.colors.append(data[2]) - */ + if (!mouse_start_pos_set) { + mouse_start_pos = data[0]; + mouse_start_pos_set = true; + } + + preview_cells.resize(0); + preview_colors.resize(0); + canvas->clear_preview_layer(); + + PoolVector2iArray pixels = PaintUtilities::get_pixels_in_line(data[0], mouse_start_pos); + + for (int i = 0; i < pixels.size(); ++i) { + Vector2i pixel = pixels[i]; + + Color col = canvas->get_pixel_v(pixel); + + if (canvas->is_alpha_locked() && col.a < 0.00001) { + continue; + } + + Color nc = data[2]; + + canvas->set_preview_pixel_v(pixel, nc); + + preview_cells.append(pixel); + preview_colors.append(nc); + } } + void LineAction::commit_action(PaintCanvas *canvas) { - /* - canvas.clear_preview_layer() - var cells = action_data.preview.cells - var colors = action_data.preview.colors - for idx in range(cells.size()): - if canvas.get_pixel_v(cells[idx]) == null: - continue - action_data.undo.cells.append(cells[idx]) - action_data.undo.colors.append(canvas.get_pixel_v(cells[idx])) - - canvas.set_pixel_v(cells[idx], colors[idx]) - - action_data.redo.cells.append(cells[idx]) - action_data.redo.colors.append(colors[idx]) - mouse_start_pos = null - */ + canvas->clear_preview_layer(); + + for (int i = 0; i < preview_cells.size(); ++i) { + Vector2i pc = preview_cells[i]; + + if (!canvas->validate_pixel_v(pc)) { + continue; + } + + Color pcol = preview_colors[i]; + + undo_cells.append(pc); + undo_colors.append(canvas->get_pixel_v(pc)); + + canvas->set_pixel_v(pc, pcol); + + redo_cells.append(pc); + redo_colors.append(pcol); + } + + mouse_start_pos_set = false; } LineAction::LineAction() { + mouse_start_pos_set = false; } LineAction::~LineAction() { diff --git a/modules/paint/actions/line_action.h b/modules/paint/actions/line_action.h index 16dd90bcf..5a2347708 100644 --- a/modules/paint/actions/line_action.h +++ b/modules/paint/actions/line_action.h @@ -39,6 +39,7 @@ public: LineAction(); ~LineAction(); + bool mouse_start_pos_set; Vector2 mouse_start_pos; protected: