diff --git a/modules/paint/ui/property_inspectors/paint_project_property_inspector.cpp b/modules/paint/ui/property_inspectors/paint_project_property_inspector.cpp index fa04f3dbe..9b8e1ca38 100644 --- a/modules/paint/ui/property_inspectors/paint_project_property_inspector.cpp +++ b/modules/paint/ui/property_inspectors/paint_project_property_inspector.cpp @@ -31,7 +31,7 @@ SOFTWARE. #include "../../nodes/paint_node.h" #include "../../nodes/paint_project.h" -void PaintProjectPropertyInspector::add_grid_button(const Color &color) { +void PaintProjectPropertyInspector::add_grid_button(const Color &color, const int color_index) { ColorSelectorButton *color_selector_button = memnew(ColorSelectorButton); color_selector_button->set_custom_minimum_size(Size2(35, 30)); @@ -39,8 +39,29 @@ void PaintProjectPropertyInspector::add_grid_button(const Color &color) { _color_grid->add_child(color_selector_button); color_selector_button->set_pick_color(color); - color_selector_button->connect("color_changed", this, "_on_grid_color_button_changed", varray(_color_grid->get_child_count() - 1)); - color_selector_button->connect("pressed", this, "_on_grid_color_button_pressed", varray(_color_grid->get_child_count() - 1)); + color_selector_button->set_meta("color_index", color_index); + color_selector_button->connect("color_changed", this, "_on_grid_color_button_changed", varray(color_selector_button)); + color_selector_button->connect("pressed", this, "_on_grid_color_button_pressed", varray(color_selector_button)); +} + +void PaintProjectPropertyInspector::create_grid_buttons() { + PaintProject *proj = Object::cast_to(ObjectDB::get_instance(_current_paint_project)); + + if (!proj) { + return; + } + + PoolColorArray colors = proj->get_color_presets(); + + for (int i = 0; i < colors.size(); ++i) { + add_grid_button(colors[i], i); + } +} + +void PaintProjectPropertyInspector::clear_grid_buttons() { + for (int i = 0; i < _color_grid->get_child_count(); ++i) { + _color_grid->get_child(i)->queue_delete(); + } } void PaintProjectPropertyInspector::_on_paint_node_selected(Node *p_paint_node) { @@ -60,26 +81,34 @@ void PaintProjectPropertyInspector::_on_paint_node_selected(Node *p_paint_node) if (!proj) { return; } - + _current_paint_project = proj->get_instance_id(); _main_color_button->set_pick_color(proj->get_current_color()); + + create_grid_buttons(); } -void PaintProjectPropertyInspector::_on_grid_color_button_changed(const Color &color, const int index) { +void PaintProjectPropertyInspector::_on_grid_color_button_changed(const Color &color, Node *p_button) { PaintProject *proj = Object::cast_to(ObjectDB::get_instance(_current_paint_project)); - ColorSelectorButton *button = Object::cast_to(_color_grid->get_child(index)); + ColorSelectorButton *button = Object::cast_to(p_button); ERR_FAIL_COND(!button); if (proj) { - //store + _ignore_preset_changed_event = true; + + int color_index = button->get_meta("color_index"); + + proj->set_preset_color(color_index, color); + + _ignore_preset_changed_event = false; } } -void PaintProjectPropertyInspector::_on_grid_color_button_pressed(const int index) { +void PaintProjectPropertyInspector::_on_grid_color_button_pressed(Node *p_button) { PaintProject *proj = Object::cast_to(ObjectDB::get_instance(_current_paint_project)); - ColorSelectorButton *button = Object::cast_to(_color_grid->get_child(index)); + ColorSelectorButton *button = Object::cast_to(p_button); ERR_FAIL_COND(!button); if (proj) { @@ -103,9 +132,35 @@ void PaintProjectPropertyInspector::_on_main_color_selected() { } } +void PaintProjectPropertyInspector::_on_add_color_button_pressed() { + PaintProject *proj = Object::cast_to(ObjectDB::get_instance(_current_paint_project)); + + if (proj) { + _ignore_preset_changed_event = true; + Color c = _main_color_button->get_pick_color(); + proj->add_preset_color(c); + add_grid_button(c, proj->get_preset_color_count() - 1); + _ignore_preset_changed_event = false; + } +} + +void PaintProjectPropertyInspector::_on_project_color_preset_changed() { + if (_ignore_preset_changed_event) { + return; + } + + PaintProject *proj = Object::cast_to(ObjectDB::get_instance(_current_paint_project)); + + if (proj) { + clear_grid_buttons(); + create_grid_buttons(); + } +} + PaintProjectPropertyInspector::PaintProjectPropertyInspector() { _current_paint_node = 0; _current_paint_project = 0; + _ignore_preset_changed_event = false; VBoxContainer *main_container = memnew(VBoxContainer); add_child(main_container); @@ -131,19 +186,27 @@ PaintProjectPropertyInspector::PaintProjectPropertyInspector() { _main_color_button->connect("color_changed", this, "_on_main_color_changed"); _main_color_button->connect("pressed", this, "_on_main_color_selected"); - //TODO add button - for (int i = 0; i < 30; ++i) { - add_grid_button(Color(Math::randf(), Math::randf(), Math::randf())); - } + _add_color_button = memnew(Button); + row_container->add_child(_add_color_button); + _add_color_button->set_tooltip("Save color."); + _add_color_button->connect("pressed", this, "_on_add_color_button_pressed"); } PaintProjectPropertyInspector::~PaintProjectPropertyInspector() { } +void PaintProjectPropertyInspector::_notification(int p_what) { + if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) { + _add_color_button->set_icon(get_theme_icon("Add", "EditorIcons")); + } +} + void PaintProjectPropertyInspector::_bind_methods() { ClassDB::bind_method(D_METHOD("_on_grid_color_button_changed"), &PaintProjectPropertyInspector::_on_grid_color_button_changed); ClassDB::bind_method(D_METHOD("_on_grid_color_button_pressed"), &PaintProjectPropertyInspector::_on_grid_color_button_pressed); ClassDB::bind_method(D_METHOD("_on_main_color_changed"), &PaintProjectPropertyInspector::_on_main_color_changed); ClassDB::bind_method(D_METHOD("_on_main_color_selected"), &PaintProjectPropertyInspector::_on_main_color_selected); + + ClassDB::bind_method(D_METHOD("_on_add_color_button_pressed"), &PaintProjectPropertyInspector::_on_add_color_button_pressed); } diff --git a/modules/paint/ui/property_inspectors/paint_project_property_inspector.h b/modules/paint/ui/property_inspectors/paint_project_property_inspector.h index b4d27003d..1eb06f2ed 100644 --- a/modules/paint/ui/property_inspectors/paint_project_property_inspector.h +++ b/modules/paint/ui/property_inspectors/paint_project_property_inspector.h @@ -32,12 +32,15 @@ class PaintNode; class PaintProject; class HFlowContainer; class ColorSelectorButton; +class Button; class PaintProjectPropertyInspector : public PaintCustomPropertyInspector { GDCLASS(PaintProjectPropertyInspector, PaintCustomPropertyInspector); public: - void add_grid_button(const Color &color); + void add_grid_button(const Color &color, const int color_index); + void create_grid_buttons(); + void clear_grid_buttons(); void _on_paint_node_selected(Node *paint_node); @@ -45,18 +48,25 @@ public: ~PaintProjectPropertyInspector(); protected: - void _on_grid_color_button_changed(const Color &color, const int index); - void _on_grid_color_button_pressed(const int index); + void _on_grid_color_button_changed(const Color &color, Node *button); + void _on_grid_color_button_pressed(Node *button); void _on_main_color_changed(const Color &color); void _on_main_color_selected(); + void _on_add_color_button_pressed(); + void _on_project_color_preset_changed(); + + void _notification(int p_what); static void _bind_methods(); HFlowContainer *_color_grid; ColorSelectorButton *_main_color_button; + Button *_add_color_button; ObjectID _current_paint_node; ObjectID _current_paint_project; + + bool _ignore_preset_changed_event; }; #endif