pandemonium_engine/modules/paint/actions/line_action.cpp

121 lines
3.5 KiB
C++
Raw Normal View History

/*
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
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:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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 "line_action.h"
2022-04-17 21:10:43 +02:00
#include "../paint_canvas.h"
#include "../paint_canvas_layer.h"
#include "../paint_utilities.h"
2022-06-21 18:47:22 +02:00
Vector2i LineAction::get_mouse_start_pos() {
return mouse_start_pos;
}
void LineAction::set_mouse_start_pos(const Vector2i &val) {
mouse_start_pos = val;
}
bool LineAction::get_mouse_start_pos_set() {
return mouse_start_pos_set;
}
void LineAction::set_mouse_start_pos_set(const bool val) {
mouse_start_pos_set = val;
}
void LineAction::do_action(PaintCanvas *canvas, const Array &data) {
2022-04-17 21:10:43 +02:00
PaintAction::do_action(canvas, data);
2022-06-20 23:22:52 +02:00
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);
}
}
2022-06-20 23:22:52 +02:00
void LineAction::commit_action(PaintCanvas *canvas) {
2022-06-20 23:22:52 +02:00
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;
}
2022-06-21 16:35:19 +02:00
bool LineAction::can_commit() {
return true;
}
LineAction::LineAction() {
2022-06-20 23:22:52 +02:00
mouse_start_pos_set = false;
}
LineAction::~LineAction() {
}
void LineAction::_bind_methods() {
2022-06-21 18:47:22 +02:00
ClassDB::bind_method(D_METHOD("get_mouse_start_pos"), &LineAction::get_mouse_start_pos);
ClassDB::bind_method(D_METHOD("set_mouse_start_pos", "value"), &LineAction::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"), &LineAction::get_mouse_start_pos_set);
ClassDB::bind_method(D_METHOD("set_mouse_start_pos_set", "value"), &LineAction::set_mouse_start_pos_set);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mouse_start_pos_set"), "set_mouse_start_pos_set", "get_mouse_start_pos_set");
}