2022-04-15 19:40:12 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019 Flairieve
|
|
|
|
Copyright (c) 2020-2022 cobrapitz
|
|
|
|
Copyright (c) 2022 Péter Magyar
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
2022-04-10 21:14:56 +02:00
|
|
|
|
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,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rect_action.h"
|
|
|
|
|
2022-11-14 17:31:45 +01:00
|
|
|
#include "../deprecated/paint_canvas.h"
|
|
|
|
#include "../deprecated/paint_canvas_layer.h"
|
2022-04-17 21:10:43 +02:00
|
|
|
#include "../paint_utilities.h"
|
|
|
|
|
2022-06-21 18:47:22 +02:00
|
|
|
Vector2i RectAction::get_mouse_start_pos() {
|
|
|
|
return mouse_start_pos;
|
|
|
|
}
|
|
|
|
void RectAction::set_mouse_start_pos(const Vector2i &val) {
|
|
|
|
mouse_start_pos = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RectAction::get_mouse_start_pos_set() {
|
|
|
|
return mouse_start_pos_set;
|
|
|
|
}
|
|
|
|
void RectAction::set_mouse_start_pos_set(const bool val) {
|
|
|
|
mouse_start_pos_set = val;
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:49:03 +01:00
|
|
|
void RectAction::do_action_old(PaintCanvasOld *canvas, const Array &data) {
|
|
|
|
PaintAction::do_action_old(canvas, data);
|
2022-04-17 21:10:43 +02:00
|
|
|
|
2022-06-21 10:08:41 +02:00
|
|
|
if (!mouse_start_pos_set) {
|
|
|
|
mouse_start_pos = data[0];
|
|
|
|
//print("init:", mouse_start_pos)
|
|
|
|
mouse_start_pos_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
undo_cells.clear();
|
|
|
|
undo_colors.clear();
|
|
|
|
preview_cells.clear();
|
|
|
|
preview_colors.clear();
|
|
|
|
|
|
|
|
canvas->clear_preview_layer();
|
|
|
|
|
|
|
|
Vector2i p = mouse_start_pos;
|
|
|
|
Vector2i current_mouse_pos = data[0];
|
|
|
|
Vector2i s = current_mouse_pos - mouse_start_pos;
|
|
|
|
|
|
|
|
PoolVector2iArray pixels = PaintUtilities::get_pixels_in_line(p, p + Vector2i(s.x, 0));
|
|
|
|
pixels.append_array(PaintUtilities::get_pixels_in_line(p, p + Vector2(0, s.y)));
|
|
|
|
pixels.append_array(PaintUtilities::get_pixels_in_line(p + s, p + s + Vector2(0, -s.y)));
|
|
|
|
pixels.append_array(PaintUtilities::get_pixels_in_line(p + s, p + s + Vector2(-s.x, 0)));
|
|
|
|
|
|
|
|
for (int i = 0; i < pixels.size(); ++i) {
|
|
|
|
Vector2i pixel = pixels[i];
|
|
|
|
|
|
|
|
if (!canvas->validate_pixel_v(pixel)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color col = canvas->get_pixel_v(pixel);
|
|
|
|
|
|
|
|
if (canvas->is_alpha_locked() && col.a < 0.00001) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color tc = data[2];
|
|
|
|
|
|
|
|
canvas->set_preview_pixel_v(pixel, tc);
|
|
|
|
undo_cells.append(pixel);
|
|
|
|
undo_colors.append(col);
|
|
|
|
preview_cells.append(pixel);
|
|
|
|
preview_colors.append(tc);
|
|
|
|
}
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-11-15 11:49:03 +01:00
|
|
|
void RectAction::commit_action_old(PaintCanvasOld *canvas) {
|
2022-06-21 10:08:41 +02:00
|
|
|
canvas->clear_preview_layer();
|
|
|
|
|
|
|
|
for (int idx = 0; idx < preview_cells.size(); ++idx) {
|
|
|
|
Vector2i pcell = preview_cells[idx];
|
|
|
|
Color pcolor = preview_colors[idx];
|
|
|
|
|
|
|
|
canvas->set_pixel_v(pcell, pcolor);
|
|
|
|
|
|
|
|
redo_cells.append(pcell);
|
|
|
|
redo_colors.append(pcolor);
|
|
|
|
}
|
|
|
|
|
|
|
|
mouse_start_pos_set = false;
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-11-15 12:15:32 +01:00
|
|
|
bool RectAction::_can_commit() {
|
2022-06-21 16:35:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
RectAction::RectAction() {
|
2022-06-21 10:08:41 +02:00
|
|
|
mouse_start_pos_set = false;
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
RectAction::~RectAction() {
|
|
|
|
}
|
2022-04-10 21:14:56 +02:00
|
|
|
|
2022-04-15 19:40:12 +02:00
|
|
|
void RectAction::_bind_methods() {
|
2022-06-21 18:47:22 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_mouse_start_pos"), &RectAction::get_mouse_start_pos);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mouse_start_pos", "value"), &RectAction::set_mouse_start_pos);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "mouse_start_pos"), "set_mouse_start_pos", "get_mouse_start_pos");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mouse_start_pos_set"), &RectAction::get_mouse_start_pos_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mouse_start_pos_set", "value"), &RectAction::set_mouse_start_pos_set);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mouse_start_pos_set"), "set_mouse_start_pos_set", "get_mouse_start_pos_set");
|
2022-04-15 19:40:12 +02:00
|
|
|
}
|