2022-04-15 19:40:12 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019 Flairieve
|
|
|
|
Copyright (c) 2020-2022 cobrapitz
|
|
|
|
Copyright (c) 2022 Péter Magyar
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
2022-06-20 00:10:31 +02:00
|
|
|
of this software && associated documentation files (the "Software"), to deal
|
2022-04-15 19:40:12 +02:00
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
2022-06-20 00:10:31 +02:00
|
|
|
copies of the Software, && to permit persons to whom the Software is
|
2022-04-15 19:40:12 +02:00
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
2022-06-20 00:10:31 +02:00
|
|
|
The above copyright notice && this permission notice shall be included in all
|
|
|
|
copies || substantial portions of the Software.
|
2022-04-15 19:40:12 +02:00
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
2022-06-20 00:10:31 +02:00
|
|
|
FITNESS FOR A PARTICULAR PURPOSE && NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS || COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES || OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT || OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF || IN CONNECTION WITH THE SOFTWARE || THE USE || OTHER DEALINGS IN THE
|
2022-04-15 19:40:12 +02:00
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "paint_action.h"
|
|
|
|
|
2022-11-14 17:31:45 +01:00
|
|
|
#include "../deprecated/paint_canvas.h"
|
|
|
|
#include "../deprecated/paint_canvas_layer.h"
|
2022-08-17 13:45:14 +02:00
|
|
|
#include "core/object/object.h"
|
2022-06-21 18:04:31 +02:00
|
|
|
|
|
|
|
Dictionary PaintAction::get_action_data_undo() {
|
|
|
|
return action_data_undo;
|
|
|
|
}
|
|
|
|
void PaintAction::set_action_data_undo(const Dictionary &val) {
|
|
|
|
action_data_undo = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary PaintAction::get_action_data_redo() {
|
|
|
|
return action_data_redo;
|
|
|
|
}
|
|
|
|
void PaintAction::set_action_data_redo(const Dictionary &val) {
|
|
|
|
action_data_redo = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary PaintAction::get_action_data_preview() {
|
|
|
|
return action_data_preview;
|
|
|
|
}
|
|
|
|
void PaintAction::set_action_data_preview(const Dictionary &val) {
|
|
|
|
action_data_preview = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolVector2iArray PaintAction::get_undo_cells() {
|
|
|
|
return undo_cells;
|
|
|
|
}
|
|
|
|
void PaintAction::set_undo_cells(const PoolVector2iArray &val) {
|
|
|
|
undo_cells = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolColorArray PaintAction::get_undo_colors() {
|
|
|
|
return undo_colors;
|
|
|
|
}
|
|
|
|
void PaintAction::set_undo_colors(const PoolColorArray &val) {
|
|
|
|
undo_colors = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolVector2iArray PaintAction::get_redo_cells() {
|
|
|
|
return redo_cells;
|
|
|
|
}
|
|
|
|
void PaintAction::set_redo_cells(const PoolVector2iArray &val) {
|
|
|
|
redo_cells = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolColorArray PaintAction::get_redo_colors() {
|
|
|
|
return redo_colors;
|
|
|
|
}
|
|
|
|
void PaintAction::set_redo_colors(const PoolColorArray &val) {
|
|
|
|
redo_colors = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolVector2iArray PaintAction::get_preview_cells() {
|
|
|
|
return preview_cells;
|
|
|
|
}
|
|
|
|
void PaintAction::set_preview_cells(const PoolVector2iArray &val) {
|
|
|
|
preview_cells = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolColorArray PaintAction::get_preview_colors() {
|
|
|
|
return preview_colors;
|
|
|
|
}
|
|
|
|
void PaintAction::set_preview_colors(const PoolColorArray &val) {
|
|
|
|
preview_colors = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<PaintCanvasLayer> PaintAction::get_layer() {
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
void PaintAction::set_layer(const Ref<PaintCanvasLayer> &val) {
|
|
|
|
layer = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary PaintAction::get_action_data() {
|
|
|
|
return action_data;
|
|
|
|
}
|
|
|
|
void PaintAction::set_action_data(const Dictionary &val) {
|
|
|
|
action_data = val;
|
|
|
|
}
|
2022-04-17 20:04:30 +02:00
|
|
|
|
2022-11-15 11:49:03 +01:00
|
|
|
void PaintAction::do_action_old(PaintCanvasOld *canvas, const Array &data) {
|
2022-04-17 21:10:43 +02:00
|
|
|
layer = canvas->get_active_layer();
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-11-15 11:49:03 +01:00
|
|
|
void PaintAction::commit_action_old(PaintCanvasOld *canvas) {
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-11-15 11:49:03 +01:00
|
|
|
void PaintAction::undo_action_old(PaintCanvasOld *canvas) {
|
2022-04-17 21:10:43 +02:00
|
|
|
for (int idx = 0; idx < undo_cells.size(); ++idx) {
|
|
|
|
canvas->_set_pixel_v(layer, undo_cells[idx], undo_colors[idx]);
|
|
|
|
}
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-11-15 11:49:03 +01:00
|
|
|
void PaintAction::redo_action_old(PaintCanvasOld *canvas) {
|
2022-04-17 21:10:43 +02:00
|
|
|
for (int idx = 0; idx < redo_cells.size(); ++idx) {
|
|
|
|
canvas->_set_pixel_v(layer, redo_cells[idx], redo_colors[idx]);
|
|
|
|
}
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-04-17 21:10:43 +02:00
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
bool PaintAction::can_commit() {
|
2022-04-17 21:10:43 +02:00
|
|
|
return !redo_cells.empty();
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-04-16 02:35:03 +02:00
|
|
|
PoolVector2iArray PaintAction::get_x_sym_points(const int canvas_width, const Vector2i &pixel) {
|
2022-04-16 01:25:02 +02:00
|
|
|
int p = canvas_width - pixel.x;
|
2022-04-15 19:40:12 +02:00
|
|
|
|
2022-04-16 01:25:02 +02:00
|
|
|
PoolVector2iArray points;
|
|
|
|
points.append(pixel);
|
|
|
|
points.append(Vector2i(p - 1, pixel.y));
|
2022-04-15 19:40:12 +02:00
|
|
|
|
2022-04-16 01:25:02 +02:00
|
|
|
return points;
|
|
|
|
}
|
2022-04-16 02:35:03 +02:00
|
|
|
PoolVector2iArray PaintAction::get_y_sym_points(const int canvas_height, const Vector2i &pixel) {
|
2022-04-16 01:25:02 +02:00
|
|
|
int p = canvas_height - pixel.y;
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-04-16 01:25:02 +02:00
|
|
|
PoolVector2iArray points;
|
|
|
|
points.append(pixel);
|
|
|
|
points.append(Vector2i(pixel.x, p - 1));
|
|
|
|
|
|
|
|
return points;
|
|
|
|
}
|
2022-04-16 02:35:03 +02:00
|
|
|
PoolVector2iArray PaintAction::get_xy_sym_points(const int canvas_width, const int canvas_height, const Vector2i &pixel) {
|
2022-04-16 01:25:02 +02:00
|
|
|
PoolVector2iArray all_points;
|
|
|
|
PoolVector2iArray xpoints = get_x_sym_points(canvas_width, pixel);
|
|
|
|
|
|
|
|
all_points.append_array(get_y_sym_points(canvas_height, xpoints[0]));
|
|
|
|
all_points.append_array(get_y_sym_points(canvas_height, xpoints[1]));
|
|
|
|
|
|
|
|
PoolVector2iArray points;
|
|
|
|
for (int i = 0; i < all_points.size(); ++i) { //point in all_points:
|
|
|
|
Vector2i point = all_points[i];
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (int j = 0; j < points.size(); ++j) {
|
|
|
|
if (points[j] == point) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
points.append(point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-06-20 00:10:31 +02:00
|
|
|
|
2022-11-14 17:46:37 +01:00
|
|
|
PoolVector2iArray PaintAction::get_points(PaintCanvasOld *canvas, const Vector2i &pixel) {
|
2022-04-16 01:25:02 +02:00
|
|
|
PoolVector2iArray points;
|
2022-04-15 19:40:12 +02:00
|
|
|
|
2022-06-20 00:10:31 +02:00
|
|
|
if (canvas->symmetry_x && canvas->symmetry_y) {
|
|
|
|
PoolVector2iArray sym_points = get_xy_sym_points(canvas->get_canvas_width(), canvas->get_canvas_height(), pixel);
|
|
|
|
|
|
|
|
for (int i = 0; i < sym_points.size(); ++i) {
|
|
|
|
Vector2i point = sym_points[i];
|
|
|
|
|
|
|
|
if (undo_cells.contains(point) || !canvas->validate_pixel_v(point)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canvas->is_alpha_locked() && canvas->get_pixel_v(pixel) == Color(0, 0, 0, 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
points.append(point);
|
|
|
|
}
|
|
|
|
} else if (canvas->symmetry_y) {
|
|
|
|
PoolVector2iArray sym_points = get_y_sym_points(canvas->get_canvas_height(), pixel);
|
|
|
|
|
|
|
|
for (int i = 0; i < sym_points.size(); ++i) {
|
|
|
|
Vector2i point = sym_points[i];
|
|
|
|
|
|
|
|
if (undo_cells.contains(point) || !canvas->validate_pixel_v(point)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canvas->is_alpha_locked() && canvas->get_pixel_v(pixel) == Color(0, 0, 0, 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
points.append(point);
|
|
|
|
}
|
|
|
|
} else if (canvas->symmetry_x) {
|
|
|
|
PoolVector2iArray sym_points = get_x_sym_points(canvas->get_canvas_width(), pixel);
|
|
|
|
|
|
|
|
for (int i = 0; i < sym_points.size(); ++i) {
|
|
|
|
Vector2i point = sym_points[i];
|
|
|
|
|
|
|
|
if (undo_cells.contains(point) || !canvas->validate_pixel_v(point)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canvas->is_alpha_locked() && canvas->get_pixel_v(pixel) == Color(0, 0, 0, 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
points.append(point);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (undo_cells.contains(pixel) || !canvas->validate_pixel_v(pixel)) {
|
|
|
|
//empty
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canvas->is_alpha_locked() && canvas->get_pixel_v(pixel) == Color(0, 0, 0, 0)) {
|
|
|
|
//empty
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
|
|
|
points.append(pixel);
|
|
|
|
}
|
2022-04-16 01:25:02 +02:00
|
|
|
|
2022-06-20 00:10:31 +02:00
|
|
|
return points;
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
|
|
|
|
2022-11-14 17:46:37 +01:00
|
|
|
void PaintAction::draw_points(PaintCanvasOld *canvas, const PoolVector2iArray &point_arr, const PoolColorArray &color_arr) {
|
2022-04-17 21:10:43 +02:00
|
|
|
for (int i = 0; i < point_arr.size(); ++i) {
|
|
|
|
Vector2i pixel = point_arr[i];
|
|
|
|
|
|
|
|
Color col = color_arr[i];
|
|
|
|
|
|
|
|
if (canvas->is_alpha_locked() && col.a < 0.00001) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_cells.append(pixel);
|
|
|
|
undo_colors.append(col);
|
|
|
|
|
|
|
|
canvas->set_pixel_v(pixel, col);
|
|
|
|
|
|
|
|
redo_cells.append(pixel);
|
|
|
|
redo_colors.append(col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
PaintAction::PaintAction() {
|
|
|
|
}
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
PaintAction::~PaintAction() {
|
|
|
|
}
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
void PaintAction::_bind_methods() {
|
2022-06-21 18:04:31 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_action_data_undo"), &PaintAction::get_action_data_undo);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_action_data_undo", "value"), &PaintAction::set_action_data_undo);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "action_data_undo"), "set_action_data_undo", "get_action_data_undo");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_action_data_redo"), &PaintAction::get_action_data_redo);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_action_data_redo", "value"), &PaintAction::set_action_data_redo);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "action_data_redo"), "set_action_data_redo", "get_action_data_redo");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_action_data_preview"), &PaintAction::get_action_data_preview);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_action_data_preview", "value"), &PaintAction::set_action_data_preview);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "action_data_preview"), "set_action_data_preview", "get_action_data_preview");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_undo_cells"), &PaintAction::get_undo_cells);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_undo_cells", "value"), &PaintAction::set_undo_cells);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2I_ARRAY, "undo_cells"), "set_undo_cells", "get_undo_cells");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_undo_colors"), &PaintAction::get_undo_colors);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_undo_colors", "value"), &PaintAction::set_undo_colors);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "undo_colors"), "set_undo_colors", "get_undo_colors");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_redo_cells"), &PaintAction::get_redo_cells);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_redo_cells", "value"), &PaintAction::set_redo_cells);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2I_ARRAY, "redo_cells"), "set_redo_cells", "get_redo_cells");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_redo_colors"), &PaintAction::get_redo_colors);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_redo_colors", "value"), &PaintAction::set_redo_colors);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "redo_colors"), "set_redo_colors", "get_redo_colors");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_preview_cells"), &PaintAction::get_preview_cells);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_preview_cells", "value"), &PaintAction::set_preview_cells);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2I_ARRAY, "preview_cells"), "set_preview_cells", "get_preview_cells");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_preview_colors"), &PaintAction::get_preview_colors);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_preview_colors", "value"), &PaintAction::set_preview_colors);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "preview_colors"), "set_preview_colors", "get_preview_colors");
|
|
|
|
|
|
|
|
//ClassDB::bind_method(D_METHOD("get_layer"), &PaintAction::get_layer);
|
|
|
|
//ClassDB::bind_method(D_METHOD("set_layer", "value"), &PaintAction::set_layer);
|
|
|
|
//ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "layer", PROPERTY_HINT_RESOURCE_TYPE, "PaintCanvasLayer", 0), "set_layer", "get_layer");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_action_data"), &PaintAction::get_action_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_action_data", "value"), &PaintAction::set_action_data);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "action_data"), "set_action_data", "get_action_data");
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|