pandemonium_engine/modules/paint/nodes/paint_project.cpp

178 lines
7.0 KiB
C++
Raw Normal View History

2023-12-18 00:18:53 +01:00
/*************************************************************************/
/* paint_project.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/*************************************************************************/
2022-11-15 13:17:37 +01:00
#include "paint_project.h"
#include "../ui/paint_canvas_background.h"
#include "../ui/paint_visual_grid.h"
#include "core/config/project_settings.h"
#include "core/config/engine.h"
String PaintProject::get_save_file_name() {
return _save_file_name;
}
void PaintProject::set_save_file_name(const String &fn) {
_save_file_name = fn;
}
Color PaintProject::get_current_color() {
return _current_color;
}
void PaintProject::set_current_color(const Color &color) {
_current_color = color;
emit_signal("current_color_changed", _current_color);
}
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();
}
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);
}
}
void PaintProject::save_image_to_file() {
ERR_FAIL_COND(_save_file_name.empty());
Ref<Image> img = render_image();
ERR_FAIL_COND(!img.is_valid());
img->save_png("res://" + _save_file_name);
}
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());
}
}
bool PaintProject::_render_should_evaluate_children() {
return false;
}
2022-11-15 13:17:37 +01:00
PaintProject::PaintProject() {
_current_color = Color(1, 1, 1, 1);
2022-11-15 13:17:37 +01:00
}
PaintProject::~PaintProject() {
}
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() {
ADD_SIGNAL(MethodInfo("current_color_changed", PropertyInfo(Variant::COLOR, "color")));
ADD_SIGNAL(MethodInfo("color_presets_changed"));
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");
ClassDB::bind_method(D_METHOD("save_image_to_file"), &PaintProject::save_image_to_file);
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");
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);
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);
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);
2022-11-15 13:17:37 +01:00
}