2022-11-15 13:17:37 +01:00
|
|
|
#include "paint_project.h"
|
|
|
|
|
2022-11-19 12:22:10 +01:00
|
|
|
#include "../ui/paint_canvas_background.h"
|
|
|
|
#include "../ui/paint_visual_grid.h"
|
2022-11-19 13:17:30 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2022-11-19 12:22:10 +01:00
|
|
|
|
|
|
|
#include "core/config/engine.h"
|
2022-11-17 23:32:09 +01:00
|
|
|
|
2022-11-19 13:45:14 +01:00
|
|
|
String PaintProject::get_save_file_name() {
|
|
|
|
return _save_file_name;
|
|
|
|
}
|
|
|
|
void PaintProject::set_save_file_name(const String &fn) {
|
|
|
|
_save_file_name = fn;
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:11:38 +01:00
|
|
|
Color PaintProject::get_current_color() {
|
|
|
|
return _current_color;
|
|
|
|
}
|
|
|
|
void PaintProject::set_current_color(const Color &color) {
|
|
|
|
_current_color = color;
|
|
|
|
|
2022-11-17 23:32:09 +01:00
|
|
|
emit_signal("current_color_changed", _current_color);
|
|
|
|
}
|
|
|
|
|
2022-11-17 23:55:36 +01:00
|
|
|
void PaintProject::add_preset_color(const Color &color) {
|
|
|
|
_color_presets.push_back(color);
|
|
|
|
|
|
|
|
emit_signal("color_presets_changed");
|
|
|
|
}
|
|
|
|
void PaintProject::remove_preset_color(const int index) {
|
|
|
|
ERR_FAIL_INDEX(index, _color_presets.size());
|
|
|
|
|
|
|
|
_color_presets.remove(index);
|
|
|
|
|
|
|
|
emit_signal("color_presets_changed");
|
|
|
|
}
|
|
|
|
Color PaintProject::get_preset_color(const int index) {
|
|
|
|
ERR_FAIL_INDEX_V(index, _color_presets.size(), Color());
|
|
|
|
|
|
|
|
return _color_presets.get(index);
|
|
|
|
}
|
|
|
|
void PaintProject::set_preset_color(const int index, const Color &color) {
|
|
|
|
ERR_FAIL_INDEX(index, _color_presets.size());
|
|
|
|
|
|
|
|
_color_presets.set(index, color);
|
|
|
|
|
|
|
|
emit_signal("color_presets_changed");
|
|
|
|
}
|
|
|
|
int PaintProject::get_preset_color_count() {
|
|
|
|
return _color_presets.size();
|
|
|
|
}
|
|
|
|
|
2022-11-17 23:32:09 +01:00
|
|
|
PoolColorArray PaintProject::get_color_presets() {
|
|
|
|
return _color_presets;
|
|
|
|
}
|
|
|
|
void PaintProject::set_color_presets(const PoolColorArray &colors) {
|
|
|
|
_color_presets = colors;
|
|
|
|
|
|
|
|
emit_signal("color_presets_changed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaintProject::set_colors_as_default() {
|
|
|
|
if (ProjectSettings::get_singleton()) {
|
|
|
|
ProjectSettings::get_singleton()->set_setting("paint/color_presets/colors", _color_presets);
|
|
|
|
}
|
2022-11-15 23:11:38 +01:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:06:56 +01:00
|
|
|
void PaintProject::save_image_to_file() {
|
2022-11-19 13:45:14 +01:00
|
|
|
ERR_FAIL_COND(_save_file_name.empty());
|
|
|
|
|
2022-11-19 23:06:56 +01:00
|
|
|
Ref<Image> img = save_image();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!img.is_valid());
|
|
|
|
|
|
|
|
img->save_png("res://" + _save_file_name);
|
2022-11-19 13:45:14 +01:00
|
|
|
}
|
|
|
|
|
2022-11-19 12:22:10 +01:00
|
|
|
void PaintProject::add_paint_canvas_backgorund() {
|
|
|
|
PaintCanvasBackground *bg = memnew(PaintCanvasBackground);
|
|
|
|
add_child(bg);
|
|
|
|
move_child(bg, 0);
|
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
bg->set_owner(get_tree()->get_edited_scene_root());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void PaintProject::add_paint_visual_grid() {
|
|
|
|
PaintVisualGrid *grid = memnew(PaintVisualGrid);
|
|
|
|
add_child(grid);
|
|
|
|
move_child(grid, 0);
|
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
grid->set_owner(get_tree()->get_edited_scene_root());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 13:17:37 +01:00
|
|
|
PaintProject::PaintProject() {
|
2022-11-15 23:11:38 +01:00
|
|
|
_current_color = Color(1, 1, 1, 1);
|
2022-11-15 13:17:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PaintProject::~PaintProject() {
|
|
|
|
}
|
|
|
|
|
2022-11-17 23:32:09 +01:00
|
|
|
void PaintProject::_notification(int p_what) {
|
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
|
|
|
if (_color_presets.size() == 0) {
|
|
|
|
if (ProjectSettings::get_singleton()->has_setting("paint/color_presets/colors")) {
|
|
|
|
_color_presets = ProjectSettings::get_singleton()->get_setting("paint/color_presets/colors");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 13:17:37 +01:00
|
|
|
void PaintProject::_bind_methods() {
|
2022-11-17 23:32:09 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("current_color_changed", PropertyInfo(Variant::COLOR, "color")));
|
|
|
|
ADD_SIGNAL(MethodInfo("color_presets_changed"));
|
2022-11-15 23:11:38 +01:00
|
|
|
|
2022-11-19 13:45:14 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_save_file_name"), &PaintProject::get_save_file_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_save_file_name", "size"), &PaintProject::set_save_file_name);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "save_file_name"), "set_save_file_name", "get_save_file_name");
|
|
|
|
|
2022-11-19 23:06:56 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("save_image_to_file"), &PaintProject::save_image_to_file);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NIL, "run_save_image", PROPERTY_HINT_BUTTON, "save_image_to_file"), "", "");
|
2022-11-19 13:45:14 +01:00
|
|
|
|
2022-11-15 23:11:38 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_current_color"), &PaintProject::get_current_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_current_color", "size"), &PaintProject::set_current_color);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "current_color"), "set_current_color", "get_current_color");
|
2022-11-17 23:32:09 +01:00
|
|
|
|
2022-11-17 23:55:36 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("add_preset_color", "color"), &PaintProject::add_preset_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_preset_color", "index"), &PaintProject::remove_preset_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_preset_color", "index"), &PaintProject::get_preset_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_preset_color", "index", "color"), &PaintProject::set_preset_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_preset_color_count"), &PaintProject::get_preset_color_count);
|
|
|
|
|
2022-11-17 23:32:09 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_color_presets"), &PaintProject::get_color_presets);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_color_presets", "colors"), &PaintProject::set_color_presets);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "color_presets"), "set_color_presets", "get_color_presets");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_colors_as_default"), &PaintProject::set_colors_as_default);
|
2022-11-19 12:22:10 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NIL, "run_set_colors_as_default", PROPERTY_HINT_BUTTON, "set_colors_as_default"), "", "");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("add_paint_canvas_backgorund"), &PaintProject::add_paint_canvas_backgorund);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_paint_visual_grid"), &PaintProject::add_paint_visual_grid);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NIL, "run_add_paint_visual_grid", PROPERTY_HINT_BUTTON, "add_paint_visual_grid"), "", "");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NIL, "run_add_paint_canvas_backgorund", PROPERTY_HINT_BUTTON, "add_paint_canvas_backgorund"), "", "");
|
2022-11-15 13:17:37 +01:00
|
|
|
}
|