From 12193b03f40b62093156dfb793996f2807f19734 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 18 Nov 2022 01:09:25 +0100 Subject: [PATCH] Added a slider for the brush size property into PaintToolsPropertyInspector. --- .../paint_tools_property_inspector.cpp | 64 +++++++++++++++++-- .../paint_tools_property_inspector.h | 10 ++- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/modules/paint/ui/property_inspectors/paint_tools_property_inspector.cpp b/modules/paint/ui/property_inspectors/paint_tools_property_inspector.cpp index 89acb168e..f8ee7318f 100644 --- a/modules/paint/ui/property_inspectors/paint_tools_property_inspector.cpp +++ b/modules/paint/ui/property_inspectors/paint_tools_property_inspector.cpp @@ -25,20 +25,22 @@ SOFTWARE. #include "scene/gui/box_container.h" #include "scene/gui/button.h" #include "scene/gui/flow_container.h" +#include "scene/gui/label.h" +#include "scene/gui/slider.h" #include "scene/gui/texture_button.h" +#include "../../bush_prefabs.h" +#include "../../paint_icons/icons.h" #include "scene/resources/style_box.h" #include "scene/resources/texture.h" -#include "../../paint_icons/icons.h" -#include "../../bush_prefabs.h" #include "../../nodes/paint_canvas.h" #include "../../nodes/paint_node.h" #include "../../nodes/paint_project.h" #ifdef TOOLS_ENABLED -#include "editor/editor_node.h" #include "core/config/engine.h" +#include "editor/editor_node.h" #endif void PaintToolsPropertyInspector::add_button(int id, const String &hint, const String &icon, const String &theme_type) { @@ -49,7 +51,7 @@ void PaintToolsPropertyInspector::add_button(int id, const String &hint, const S #ifdef TOOLS_ENABLED if (EditorNode::get_singleton() && Engine::get_singleton()->is_editor_hint()) { icon_tex = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(icon, theme_type); - } else + } else #endif { icon_tex = get_theme_icon(icon, theme_type); @@ -60,7 +62,7 @@ void PaintToolsPropertyInspector::add_button(int id, const String &hint, const S button->set_toggle_mode(true); button->set_button_group(_group); button->set_meta("button_id", id); - button->connect("toggled", this, "on_button_toggled", varray(id)); + button->connect("toggled", this, "_on_button_toggled", varray(id)); _grid->add_child(button); } @@ -84,11 +86,14 @@ void PaintToolsPropertyInspector::_on_paint_node_selected(Node *p_paint_node) { _paint_canvas = paint_canvas->get_instance_id(); _on_tool_changed(); + _on_brush_size_changed(); paint_canvas->connect("current_tool_changed", this, "_on_tool_changed"); + paint_canvas->connect("brush_size_changed", this, "_on_brush_size_changed"); } PaintToolsPropertyInspector::PaintToolsPropertyInspector() { + _ignore_signal = false; _paint_canvas = 0; _group.instance(); @@ -120,12 +125,30 @@ PaintToolsPropertyInspector::PaintToolsPropertyInspector() { add_brush_prefab(BrushPrefabs::CIRCLE, PaintIcons::make_icon_brush_circle_png(), PaintIcons::make_icon_brush_circle_hovered_png()); add_brush_prefab(BrushPrefabs::V_LINE, PaintIcons::make_icon_brush_v_line_png(), PaintIcons::make_icon_brush_v_line_hovered_png()); add_brush_prefab(BrushPrefabs::H_LINE, PaintIcons::make_icon_brush_h_line_png(), PaintIcons::make_icon_brush_h_line_hovered_png()); + + HBoxContainer *brush_size_container = memnew(HBoxContainer); + box_container->add_child(brush_size_container); + brush_size_container->set_h_size_flags(SIZE_EXPAND_FILL); + + _brush_size_label = memnew(Label); + brush_size_container->add_child(_brush_size_label); + _brush_size_label->set_text("1"); + + _brush_size_slider = memnew(HSlider); + brush_size_container->add_child(_brush_size_slider); + _brush_size_slider->set_min(1); + _brush_size_slider->set_max(100); + _brush_size_slider->set_use_rounded_values(true); + _brush_size_slider->set_value(1); + _brush_size_slider->set_allow_greater(true); + _brush_size_slider->connect("value_changed", this, "_on_brush_size_slider_value_changed"); + _brush_size_slider->set_h_size_flags(SIZE_EXPAND_FILL); } PaintToolsPropertyInspector::~PaintToolsPropertyInspector() { } -void PaintToolsPropertyInspector::on_button_toggled(bool on, int id) { +void PaintToolsPropertyInspector::_on_button_toggled(bool on, int id) { if (!on) { return; } @@ -168,8 +191,35 @@ void PaintToolsPropertyInspector::_on_brush_prefab_button_pressed(const int id) paint_canvas->set_brush_prefab(id); } +void PaintToolsPropertyInspector::_on_brush_size_slider_value_changed(const float value) { + PaintCanvas *paint_canvas = Object::cast_to(ObjectDB::get_instance(_paint_canvas)); + + ERR_FAIL_COND(!paint_canvas); + + _ignore_signal = true; + paint_canvas->set_brush_size(static_cast(value)); + _ignore_signal = false; + + _brush_size_label->set_text(itos(paint_canvas->get_brush_size())); +} + +void PaintToolsPropertyInspector::_on_brush_size_changed() { + if (_ignore_signal) { + return; + } + + PaintCanvas *paint_canvas = Object::cast_to(ObjectDB::get_instance(_paint_canvas)); + + ERR_FAIL_COND(!paint_canvas); + + _brush_size_slider->set_value(paint_canvas->get_brush_size()); + _brush_size_label->set_text(itos(paint_canvas->get_brush_size())); +} + void PaintToolsPropertyInspector::_bind_methods() { - ClassDB::bind_method(D_METHOD("on_button_toggled"), &PaintToolsPropertyInspector::on_button_toggled); + ClassDB::bind_method(D_METHOD("_on_button_toggled"), &PaintToolsPropertyInspector::_on_button_toggled); ClassDB::bind_method(D_METHOD("_on_tool_changed"), &PaintToolsPropertyInspector::_on_tool_changed); ClassDB::bind_method(D_METHOD("_on_brush_prefab_button_pressed"), &PaintToolsPropertyInspector::_on_brush_prefab_button_pressed); + ClassDB::bind_method(D_METHOD("_on_brush_size_slider_value_changed"), &PaintToolsPropertyInspector::_on_brush_size_slider_value_changed); + ClassDB::bind_method(D_METHOD("_on_brush_size_changed"), &PaintToolsPropertyInspector::_on_brush_size_changed); } diff --git a/modules/paint/ui/property_inspectors/paint_tools_property_inspector.h b/modules/paint/ui/property_inspectors/paint_tools_property_inspector.h index bbeed5cfe..780d089dc 100644 --- a/modules/paint/ui/property_inspectors/paint_tools_property_inspector.h +++ b/modules/paint/ui/property_inspectors/paint_tools_property_inspector.h @@ -32,6 +32,8 @@ class PaintNode; class PaintCanvas; class PaintProject; class ButtonGroup; +class HSlider; +class Label; class PaintToolsPropertyInspector : public PaintCustomPropertyInspector { GDCLASS(PaintToolsPropertyInspector, PaintCustomPropertyInspector); @@ -46,18 +48,24 @@ public: ~PaintToolsPropertyInspector(); protected: - void on_button_toggled(bool on, int id); + void _on_button_toggled(bool on, int id); void _on_tool_changed(); void _on_brush_prefab_button_pressed(const int id); + void _on_brush_size_slider_value_changed(const float value); + void _on_brush_size_changed(); static void _bind_methods(); HFlowContainer *_grid; HFlowContainer *_brush_prefabs; + HSlider *_brush_size_slider; + Label *_brush_size_label; ObjectID _paint_canvas; Ref _group; + + bool _ignore_signal; }; #endif