2022-11-14 23:27:26 +01:00
|
|
|
#include "paint_node.h"
|
|
|
|
|
2022-11-16 00:26:33 +01:00
|
|
|
#include "../paint_utilities.h"
|
2022-11-15 23:11:55 +01:00
|
|
|
#include "paint_project.h"
|
|
|
|
|
2022-11-14 23:27:26 +01:00
|
|
|
Vector2i PaintNode::get_size() {
|
|
|
|
return _size;
|
|
|
|
}
|
|
|
|
void PaintNode::set_size(const Vector2i &size) {
|
2022-11-19 19:26:19 +01:00
|
|
|
if (size == _size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-14 23:27:26 +01:00
|
|
|
_size = size;
|
2022-11-19 01:23:33 +01:00
|
|
|
|
2022-11-19 19:26:19 +01:00
|
|
|
emit_signal("size_changed");
|
|
|
|
_propagate_notification_resized();
|
|
|
|
update();
|
2022-11-19 01:23:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PaintNode::get_draw_outline() {
|
|
|
|
return _draw_outline;
|
|
|
|
}
|
|
|
|
void PaintNode::set_draw_outline(const bool val) {
|
|
|
|
_draw_outline = val;
|
|
|
|
|
2022-11-19 19:26:19 +01:00
|
|
|
update();
|
2022-11-14 23:27:26 +01:00
|
|
|
}
|
|
|
|
|
2022-11-19 13:45:14 +01:00
|
|
|
void PaintNode::save_image() {
|
|
|
|
_propagate_notification_project_pre_save();
|
|
|
|
call("_save_image");
|
|
|
|
_propagate_notification_project_post_save();
|
2022-11-19 13:17:30 +01:00
|
|
|
}
|
2022-11-19 13:45:14 +01:00
|
|
|
void PaintNode::_save_image() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> PaintNode::get_save_image() {
|
|
|
|
return call("_get_save_image");
|
|
|
|
}
|
|
|
|
Ref<Image> PaintNode::_get_save_image() {
|
|
|
|
return Ref<Image>();
|
2022-11-19 13:17:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-16 00:26:33 +01:00
|
|
|
PoolVector2iArray PaintNode::util_get_pixels_in_line(const Vector2i &from, const Vector2i &to) {
|
|
|
|
return PaintUtilities::get_pixels_in_line(from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PaintNode::util_to_1d_v(const Vector2i &p, int w) {
|
|
|
|
return PaintUtilities::to_1D_v(p, w);
|
|
|
|
}
|
|
|
|
int PaintNode::util_to_1d(int x, int y, int w) {
|
|
|
|
return PaintUtilities::to_1D(x, y, w);
|
|
|
|
}
|
|
|
|
Vector2i PaintNode::util_to_2d(int idx, int w) {
|
|
|
|
return PaintUtilities::to_2D(idx, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color PaintNode::util_color_from_array(const PoolRealArray &color_array) {
|
|
|
|
return PaintUtilities::color_from_array(color_array);
|
|
|
|
}
|
|
|
|
Color PaintNode::util_random_color() {
|
|
|
|
return PaintUtilities::random_color();
|
|
|
|
}
|
|
|
|
Color PaintNode::util_random_color_alt() {
|
|
|
|
return PaintUtilities::random_color_alt();
|
|
|
|
}
|
|
|
|
|
2022-11-16 15:25:27 +01:00
|
|
|
bool PaintNode::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
|
|
|
|
return call("_forward_canvas_gui_input", p_event);
|
|
|
|
}
|
|
|
|
bool PaintNode::_forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:11:55 +01:00
|
|
|
PaintProject *PaintNode::get_paint_project() {
|
|
|
|
PaintNode *p = this;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
PaintProject *pp = Object::cast_to<PaintProject>(p);
|
|
|
|
|
|
|
|
if (pp) {
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = Object::cast_to<PaintNode>(p->get_parent());
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-19 19:26:19 +01:00
|
|
|
PaintNode *PaintNode::get_parent_paint_node() {
|
|
|
|
return Object::cast_to<PaintNode>(get_parent());
|
|
|
|
}
|
|
|
|
|
|
|
|
PaintNode *PaintNode::find_parent_paint_node() {
|
|
|
|
Node *p = get_parent();
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
PaintNode *pn = Object::cast_to<PaintNode>(p);
|
|
|
|
|
|
|
|
if (pn) {
|
|
|
|
return pn;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = Object::cast_to<PaintNode>(p->get_parent());
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:57:44 +01:00
|
|
|
String PaintNode::get_configuration_warning() const {
|
|
|
|
const PaintNode *p = this;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
const PaintProject *pp = Object::cast_to<PaintProject>(p);
|
|
|
|
|
|
|
|
if (pp) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
p = Object::cast_to<PaintNode>(p->get_parent());
|
|
|
|
}
|
|
|
|
|
|
|
|
return "This Node should be a child of a PaintProject!";
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:17:30 +01:00
|
|
|
void PaintNode::_propagate_notification_resized() {
|
2022-11-19 19:26:19 +01:00
|
|
|
//Only send it to children
|
|
|
|
//So if they don't resize themselves, the notification stops
|
2022-11-19 13:17:30 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); ++i) {
|
|
|
|
PaintNode *pn = Object::cast_to<PaintNode>(get_child(i));
|
|
|
|
|
|
|
|
if (pn) {
|
2022-11-19 19:26:19 +01:00
|
|
|
pn->notification(NOTIFICATION_PARENT_PAINT_NODE_RESIZED);
|
2022-11-19 13:17:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:45:14 +01:00
|
|
|
void PaintNode::_propagate_notification_project_pre_save() {
|
|
|
|
notification(NOTIFICATION_PAINT_PROJECT_PRE_SAVE);
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); ++i) {
|
|
|
|
PaintNode *pn = Object::cast_to<PaintNode>(get_child(i));
|
|
|
|
|
|
|
|
if (pn) {
|
|
|
|
pn->_propagate_notification_project_pre_save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void PaintNode::_propagate_notification_project_post_save() {
|
|
|
|
notification(NOTIFICATION_PAINT_PROJECT_POST_SAVE);
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); ++i) {
|
|
|
|
PaintNode *pn = Object::cast_to<PaintNode>(get_child(i));
|
|
|
|
|
|
|
|
if (pn) {
|
|
|
|
pn->_propagate_notification_project_post_save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 23:27:26 +01:00
|
|
|
PaintNode::PaintNode() {
|
2022-11-19 01:23:33 +01:00
|
|
|
_draw_outline = true;
|
2022-11-14 23:27:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PaintNode::~PaintNode() {
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:23:33 +01:00
|
|
|
void PaintNode::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
2022-11-19 19:26:19 +01:00
|
|
|
case NOTIFICATION_POST_ENTER_TREE: {
|
|
|
|
if (get_size() == Vector2i(0, 0)) {
|
|
|
|
PaintNode *pn = get_parent_paint_node();
|
|
|
|
|
|
|
|
if (pn) {
|
|
|
|
set_size(pn->get_size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
2022-11-19 01:23:33 +01:00
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
if (_draw_outline) {
|
|
|
|
draw_line(Point2(0, 0), Point2(_size.x, 0), Color(0, 0, 0, 1));
|
|
|
|
draw_line(Point2(0, _size.y), Point2(_size.x, _size.y), Color(0, 0, 0, 1));
|
|
|
|
draw_line(Point2(0, 0), Point2(0, _size.y), Color(0, 0, 0, 1));
|
|
|
|
draw_line(Point2(_size.x, 0), Point2(_size.x, _size.y), Color(0, 0, 0, 1));
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 23:27:26 +01:00
|
|
|
void PaintNode::_bind_methods() {
|
2022-11-19 19:26:19 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("size_changed"));
|
|
|
|
|
2022-11-14 23:27:26 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_size"), &PaintNode::get_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_size", "size"), &PaintNode::set_size);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size"), "set_size", "get_size");
|
2022-11-15 23:58:43 +01:00
|
|
|
|
2022-11-19 01:23:33 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_draw_outline"), &PaintNode::get_draw_outline);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_draw_outline", "val"), &PaintNode::set_draw_outline);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_outline"), "set_draw_outline", "get_draw_outline");
|
|
|
|
|
2022-11-19 13:45:14 +01:00
|
|
|
BIND_VMETHOD(MethodInfo("_save_image"));
|
|
|
|
ClassDB::bind_method(D_METHOD("save_image"), &PaintNode::save_image);
|
|
|
|
ClassDB::bind_method(D_METHOD("_save_image"), &PaintNode::_save_image);
|
|
|
|
|
|
|
|
BIND_VMETHOD(MethodInfo("_get_save_image"));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_save_image"), &PaintNode::get_save_image);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_save_image"), &PaintNode::_get_save_image);
|
2022-11-19 13:17:30 +01:00
|
|
|
|
2022-11-16 00:26:33 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("util_get_pixels_in_line", "from", "to"), &PaintNode::util_get_pixels_in_line);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("util_to_1d_v", "p", "w"), &PaintNode::util_to_1d_v);
|
|
|
|
ClassDB::bind_method(D_METHOD("util_to_1d", "x", "y", "w"), &PaintNode::util_to_1d);
|
|
|
|
ClassDB::bind_method(D_METHOD("util_to_2d", "idx", "w"), &PaintNode::util_to_2d);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("util_color_from_array", "color_array"), &PaintNode::util_color_from_array);
|
|
|
|
ClassDB::bind_method(D_METHOD("util_random_color"), &PaintNode::util_random_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("util_random_color_alt"), &PaintNode::util_random_color_alt);
|
|
|
|
|
2022-11-16 15:25:27 +01:00
|
|
|
BIND_VMETHOD(MethodInfo(Variant::BOOL, "_forward_canvas_gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
|
|
|
|
ClassDB::bind_method(D_METHOD("forward_canvas_gui_input", "event"), &PaintNode::forward_canvas_gui_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_forward_canvas_gui_input", "event"), &PaintNode::_forward_canvas_gui_input);
|
|
|
|
|
2022-11-15 23:58:43 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_paint_project"), &PaintNode::get_paint_project);
|
2022-11-19 19:26:19 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_parent_paint_node"), &PaintNode::get_parent_paint_node);
|
|
|
|
ClassDB::bind_method(D_METHOD("find_parent_paint_node"), &PaintNode::find_parent_paint_node);
|
2022-11-19 13:17:30 +01:00
|
|
|
|
2022-11-19 19:26:19 +01:00
|
|
|
BIND_CONSTANT(NOTIFICATION_PARENT_PAINT_NODE_RESIZED);
|
2022-11-19 13:17:30 +01:00
|
|
|
BIND_CONSTANT(NOTIFICATION_PAINT_PROJECT_PRE_SAVE);
|
|
|
|
BIND_CONSTANT(NOTIFICATION_PAINT_PROJECT_POST_SAVE);
|
2022-11-14 23:27:26 +01:00
|
|
|
}
|