From a3fc87a5767445c0ae987defc660057448f56630 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 17 Apr 2022 20:04:30 +0200 Subject: [PATCH] Cleaned up BrushPrefabs. Also started cleaning up actions. --- modules/paint/actions/brighten_action.cpp | 82 +++++++----- modules/paint/actions/paint_action.cpp | 17 ++- modules/paint/bush_prefabs.cpp | 150 +++++++++++++--------- modules/paint/bush_prefabs.h | 2 +- 4 files changed, 148 insertions(+), 103 deletions(-) diff --git a/modules/paint/actions/brighten_action.cpp b/modules/paint/actions/brighten_action.cpp index 5aa60846a..bd5ee2ab8 100644 --- a/modules/paint/actions/brighten_action.cpp +++ b/modules/paint/actions/brighten_action.cpp @@ -24,33 +24,53 @@ SOFTWARE. #include "brighten_action.h" +#include "../paint_canvas_layer.h" +#include "../paint_canvas.h" +#include "../paint_utilities.h" + void BrightenAction::do_action(PaintCanvas *canvas, const Array &data) { - /* - .do_action(canvas, data) + PaintAction::do_action(canvas, data); +/* + PoolVector2iArray undo_cells = action_data_undo["cells"]; + PoolColorArray undo_colors = action_data_undo["colors"]; + PoolVector2iArray redo_cells = action_data_redo["cells"]; + PoolColorArray redo_colors = action_data_redo["colors"]; - var pixels = GEUtils.get_pixels_in_line(data[0], data[1]) - for pixel in pixels: - if canvas.get_pixel_v(pixel) == null: - continue + PoolVector2iArray pixels = PaintUtilities::get_pixels_in_line(data[0], data[1]); - if canvas.is_alpha_locked() and canvas.get_pixel_v(pixel) == Color.transparent: - continue + 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; + } + + if pixel in action_data.undo.cells: - var brightened_color = canvas.get_pixel_v(pixel).lightened(0.1) - canvas.set_pixel_v(pixel, brightened_color) + var brightened_color = canvas.get_pixel_v(pixel).lightened(0.1); + canvas.set_pixel_v(pixel, brightened_color); - action_data.redo.cells.append(pixel) - action_data.redo.colors.append(brightened_color) - continue + action_data.redo.cells.append(pixel); + action_data.redo.colors.append(brightened_color); + continue; - action_data.undo.colors.append(canvas.get_pixel_v(pixel)) - action_data.undo.cells.append(pixel) - var brightened_color = canvas.get_pixel_v(pixel).lightened(0.1) - canvas.set_pixel_v(pixel, brightened_color) + action_data.undo.colors.append(canvas.get_pixel_v(pixel)); + action_data.undo.cells.append(pixel); + Color brightened_color = canvas.get_pixel_v(pixel).lightened(0.1); + canvas.set_pixel_v(pixel, brightened_color); + + action_data.redo.cells.append(pixel); + action_data.redo.colors.append(brightened_color); + } + + PoolVector2iArray undo_cells = action_data_undo["cells"] = undo_cells; + PoolColorArray undo_colors = action_data_undo["colors"] = undo_colors; + PoolVector2iArray redo_cells = action_data_redo["cells"] = redo_cells; + PoolColorArray redo_colors = action_data_redo["colors"] = redo_colors; - action_data.redo.cells.append(pixel) - action_data.redo.colors.append(brightened_color) */ } void BrightenAction::commit_action(PaintCanvas *canvas) { @@ -61,20 +81,20 @@ void BrightenAction::commit_action(PaintCanvas *canvas) { } void BrightenAction::undo_action(PaintCanvas *canvas) { - /* - var cells = action_data.undo.cells - var colors = action_data.undo.colors - for idx in range(cells.size()): - canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx]) - */ + PoolVector2iArray cells = action_data_undo["cells"]; + PoolColorArray colors = action_data_undo["colors"]; + + for (int idx = 0; idx < cells.size(); ++idx) { + canvas->_set_pixel_v(action_data["layer"], cells[idx], colors[idx]); + } } void BrightenAction::redo_action(PaintCanvas *canvas) { - /* - var cells = action_data.redo.cells - var colors = action_data.redo.colors - for idx in range(cells.size()): - canvas._set_pixel_v(action_data.layer, cells[idx], colors[idx]) - */ + PoolVector2iArray cells = action_data_redo["cells"]; + PoolColorArray colors = action_data_redo["colors"]; + + for (int idx = 0; idx < cells.size(); ++idx) { + canvas->_set_pixel_v(action_data["layer"], cells[idx], colors[idx]); + } } BrightenAction::BrightenAction() { diff --git a/modules/paint/actions/paint_action.cpp b/modules/paint/actions/paint_action.cpp index 019fed14b..a7631f531 100644 --- a/modules/paint/actions/paint_action.cpp +++ b/modules/paint/actions/paint_action.cpp @@ -24,24 +24,27 @@ SOFTWARE. #include "paint_action.h" +#include "../paint_canvas.h" +#include "../paint_canvas_layer.h" + void PaintAction::do_action(PaintCanvas *canvas, const Array &data) { if (!action_data_redo.has("cells")) { - action_data_redo["cells"] = Array(); - action_data_redo["colors"] = Array(); + action_data_redo["cells"] = PoolVector2iArray(); + action_data_redo["colors"] = PoolColorArray(); } if (!action_data_undo.has("cells")) { - action_data_undo["cells"] = Array(); - action_data_undo["colors"] = Array(); + action_data_undo["cells"] = PoolVector2iArray(); + action_data_undo["colors"] = PoolColorArray(); } if (!action_data_preview.has("cells")) { - action_data_preview["cells"] = Array(); - action_data_preview["colors"] = Array(); + action_data_preview["cells"] = PoolVector2iArray(); + action_data_preview["colors"] = PoolColorArray(); } if (!action_data.has("layer")) { - //action_data["layer"] = canvas->get_active_layer(); + action_data["layer"] = canvas->get_active_layer(); } } void PaintAction::commit_action(PaintCanvas *canvas) { diff --git a/modules/paint/bush_prefabs.cpp b/modules/paint/bush_prefabs.cpp index d511aeafd..ba4955e56 100644 --- a/modules/paint/bush_prefabs.cpp +++ b/modules/paint/bush_prefabs.cpp @@ -44,85 +44,107 @@ const list = [ ] */ -PoolVector2iArray BrushPrefabs::get_brush(const BrushPrefabs::Type type, const int size) { - /* - var pixels = [] - if size < 1: - size = 1 +PoolVector2iArray BrushPrefabs::get_brush(const BrushPrefabs::Type type, int size) { + PoolVector2iArray pixels; - match type: - Type.CIRCLE: - size += 1 - var center = Vector2.ZERO - var last = center - var radius = size / 2.0 - for x in range(size): - for y in range(size): - if Vector2(x - radius, y - radius).length() < size / 3.0: - pixels.append(Vector2(x, y)) + if (size < 1) { + size = 1; + } - var avg = Vector2(size / 2, size / 2) - avg = Vector2(floor(avg.x), floor(avg.y)) + switch (type) { + case CIRCLE: { + size += 1; + Vector2 center; + int radius = size / 2.0; - for i in range(pixels.size()): - pixels[i] -= avg + for (int x = 0; x < size; ++x) { + for (int y = 0; y < size; ++y) { + if (Vector2(x - radius, y - radius).length() < size / 3.0) { + pixels.append(Vector2(x, y)); + } + } + } - Type.RECT: - var center = Vector2.ZERO - var last = center - for x in range(size): - for y in range(size): - pixels.append(Vector2(x, y)) + int av = static_cast(size / 2); + Vector2i avg = Vector2i(av, av); - var avg = Vector2.ZERO - for cell in pixels: - avg += cell + for (int i = 0; i < pixels.size(); ++i) { + Vector2i p = pixels[i]; + p -= avg; + pixels.set(i, p); + } + } break; + case RECT: { + for (int x = 0; x < size; ++x) { + for (int y = 0; y < size; ++y) { + pixels.append(Vector2(x, y)); + } + } - avg.x /= pixels.size() - avg.y /= pixels.size() + Vector2i avg; + for (int i = 0; i < pixels.size(); ++i) { + Vector2i p = pixels[i]; + avg += p; + } - avg = Vector2(floor(avg.x), floor(avg.y)) + avg.x /= pixels.size(); + avg.y /= pixels.size(); - for i in range(pixels.size()): - pixels[i] -= avg + for (int i = 0; i < pixels.size(); ++i) { + Vector2i p = pixels[i]; + p -= avg; + pixels.set(i, p); + } + } break; + case V_LINE: { + Vector2i center; + Vector2i last = center; + pixels.append(Vector2i()); - Type.V_LINE: - var center = Vector2.ZERO - var last = center - pixels.append(Vector2.ZERO) + for (int i = 0; i < size - 1; ++i) { + int sig = SGN(last.y); - for i in range(size - 1): - var sig = sign(last.y) - if sig == 0: - sig = 1 + if (sig == 0) { + sig = 1; + } - if last.y < 0: - center.y = abs(last.y) * -sig - else: - center.y = abs(last.y+1) * -sig - last = center - pixels.append(center) - Type.H_LINE: - var center = Vector2.ZERO - var last = center - pixels.append(Vector2.ZERO) + if (last.y < 0) { + center.y = abs(last.y) * -sig; + } else { + center.y = abs(last.y + 1) * -sig; + } - for i in range(size - 1): - var sig = sign(last.x) - if sig == 0: - sig = 1 + last = center; + pixels.append(center); + } - if last.x < 0: - center.x = abs(last.x) * -sig - else: - center.x = abs(last.x+1) * -sig - last = center - pixels.append(center) + } break; + case H_LINE: { + Vector2i center; + Vector2i last = center; + pixels.append(Vector2i()); - return pixels - */ + for (int i = 0; i < size - 1; ++i) { + int sig = SGN(last.x); - return PoolVector2iArray(); + if (sig == 0) { + sig = 1; + } + + if (last.x < 0) { + center.x = abs(last.x) * -sig; + } else { + center.x = abs(last.x + 1) * -sig; + } + + last = center; + pixels.append(center); + } + + } break; + } + + return pixels; } BrushPrefabs::BrushPrefabs() { diff --git a/modules/paint/bush_prefabs.h b/modules/paint/bush_prefabs.h index 6310a21c8..9f2695091 100644 --- a/modules/paint/bush_prefabs.h +++ b/modules/paint/bush_prefabs.h @@ -39,7 +39,7 @@ public: CIRCLE, }; - static PoolVector2iArray get_brush(const Type type, const int size); + static PoolVector2iArray get_brush(const Type type, int size); BrushPrefabs(); ~BrushPrefabs();