From 526800fcf9e6ad96ac2041229ca2296de14c76fa Mon Sep 17 00:00:00 2001
From: Relintai <relintai@protonmail.com>
Date: Thu, 17 Nov 2022 11:56:03 +0100
Subject: [PATCH] Added a new PaintToolsPropertyInspector.

---
 modules/paint/SCsub                           |   2 +
 modules/paint/register_types.cpp              |   2 +
 .../paint_tools_property_inspector.cpp        | 138 ++++++++++++++++++
 .../paint_tools_property_inspector.h          |  60 ++++++++
 4 files changed, 202 insertions(+)
 create mode 100644 modules/paint/ui/property_inspectors/paint_tools_property_inspector.cpp
 create mode 100644 modules/paint/ui/property_inspectors/paint_tools_property_inspector.h

diff --git a/modules/paint/SCsub b/modules/paint/SCsub
index cea733e57..6187c19a6 100644
--- a/modules/paint/SCsub
+++ b/modules/paint/SCsub
@@ -28,8 +28,10 @@ module_env.add_source_files(env.modules_sources,"ui/paint_canvas_outline.cpp")
 module_env.add_source_files(env.modules_sources,"ui/paint_canvas_background.cpp")
 module_env.add_source_files(env.modules_sources,"ui/paint_selection_box.cpp")
 module_env.add_source_files(env.modules_sources,"ui/paint_visual_grid.cpp")
+
 module_env.add_source_files(env.modules_sources,"ui/property_inspectors/paint_color_grid.cpp")
 module_env.add_source_files(env.modules_sources,"ui/property_inspectors/paint_custom_property_inspector.cpp")
+module_env.add_source_files(env.modules_sources,"ui/property_inspectors/paint_tools_property_inspector.cpp")
 
 module_env.add_source_files(env.modules_sources,"ui/paint_sidebar.cpp")
 module_env.add_source_files(env.modules_sources,"ui/paint_sidebar_module.cpp")
diff --git a/modules/paint/register_types.cpp b/modules/paint/register_types.cpp
index 6e1c40f00..c0f9caff5 100644
--- a/modules/paint/register_types.cpp
+++ b/modules/paint/register_types.cpp
@@ -45,6 +45,7 @@ SOFTWARE.
 
 #include "ui/property_inspectors/paint_color_grid.h"
 #include "ui/property_inspectors/paint_custom_property_inspector.h"
+#include "ui/property_inspectors/paint_tools_property_inspector.h"
 
 #include "nodes/paint_canvas.h"
 #include "nodes/paint_node.h"
@@ -79,6 +80,7 @@ void register_paint_types() {
 
 	ClassDB::register_class<PaintColorGrid>();
 	ClassDB::register_class<PaintCustomPropertyInspector>();
+	ClassDB::register_class<PaintToolsPropertyInspector>();
 
 	ClassDB::register_class<PaintNode>();
 	ClassDB::register_class<PaintCanvas>();
diff --git a/modules/paint/ui/property_inspectors/paint_tools_property_inspector.cpp b/modules/paint/ui/property_inspectors/paint_tools_property_inspector.cpp
new file mode 100644
index 000000000..5a7e85e0f
--- /dev/null
+++ b/modules/paint/ui/property_inspectors/paint_tools_property_inspector.cpp
@@ -0,0 +1,138 @@
+/*
+Copyright (c) 2022 Péter Magyar
+
+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.
+*/
+
+#include "paint_tools_property_inspector.h"
+
+#include "editor/editor_node.h"
+#include "scene/gui/box_container.h"
+#include "scene/gui/button.h"
+#include "scene/gui/flow_container.h"
+#include "scene/resources/style_box.h"
+#include "scene/resources/texture.h"
+
+#include "../../nodes/paint_canvas.h"
+#include "../../nodes/paint_node.h"
+#include "../../nodes/paint_project.h"
+
+void PaintToolsPropertyInspector::add_button(int id, const String &hint, const String &icon, const String &theme_type) {
+	Button *button = memnew(Button);
+
+	Ref<Texture> icon_tex;
+
+	if (EditorNode::get_singleton()) {
+		icon_tex = EditorNode::get_singleton()->get_gui_base()->get_theme_icon(icon, theme_type);
+	} else {
+		icon_tex = get_theme_icon(icon, theme_type);
+	}
+
+	button->set_icon(icon_tex);
+	button->set_tooltip(hint);
+	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));
+
+	_grid->add_child(button);
+}
+
+void PaintToolsPropertyInspector::_on_paint_node_selected(Node *p_paint_node) {
+	PaintCanvas *paint_canvas = Object::cast_to<PaintCanvas>(p_paint_node);
+
+	_paint_canvas = 0;
+
+	ERR_FAIL_COND(!paint_canvas);
+
+	_paint_canvas = paint_canvas->get_instance_id();
+
+	_on_tool_changed();
+
+	paint_canvas->connect("current_tool_changed", this, "_on_tool_changed");
+}
+
+PaintToolsPropertyInspector::PaintToolsPropertyInspector() {
+	_paint_canvas = 0;
+	_group.instance();
+
+	VBoxContainer *box_container = memnew(VBoxContainer);
+	add_child(box_container);
+
+	_grid = memnew(HFlowContainer);
+	box_container->add_child(_grid);
+	//_grid->set_custom_minimum_size(Size2(0, 145));
+	_grid->set_h_size_flags(SIZE_EXPAND_FILL);
+	_grid->set_v_size_flags(SIZE_EXPAND_FILL);
+
+	add_button(PaintCanvas::TOOL_PENCIL, "Pencil", "Edit", "EditorIcons");
+	add_button(PaintCanvas::TOOL_BRUSH, "Brush", "CanvasItem", "EditorIcons");
+	add_button(PaintCanvas::TOOL_BUCKET, "Bucket", "Bucket", "EditorIcons");
+	add_button(PaintCanvas::TOOL_RAINBOW, "Rainbow", "StyleBoxLine", "EditorIcons");
+	add_button(PaintCanvas::TOOL_LINE, "Line", "CurveLinear", "EditorIcons");
+	add_button(PaintCanvas::TOOL_RECT, "Rect", "Panels1", "EditorIcons");
+	add_button(PaintCanvas::TOOL_DARKEN, "Darken", "ArrowDown", "EditorIcons");
+	add_button(PaintCanvas::TOOL_BRIGHTEN, "Brighten", "ArrowUp", "EditorIcons");
+	add_button(PaintCanvas::TOOL_COLORPICKER, "Colorpicker", "ColorPick", "EditorIcons");
+	add_button(PaintCanvas::TOOL_CUT, "Cut", "ActionCut", "EditorIcons");
+	add_button(PaintCanvas::TOOL_PASTECUT, "Pastecut", "ActionCopy", "EditorIcons");
+}
+
+PaintToolsPropertyInspector::~PaintToolsPropertyInspector() {
+}
+
+void PaintToolsPropertyInspector::on_button_toggled(bool on, int id) {
+	if (!on) {
+		return;
+	}
+
+	PaintCanvas *paint_canvas = Object::cast_to<PaintCanvas>(ObjectDB::get_instance(_paint_canvas));
+
+	ERR_FAIL_COND(!paint_canvas);
+
+	paint_canvas->set_current_tool(id);
+}
+
+void PaintToolsPropertyInspector::_on_tool_changed() {
+	PaintCanvas *paint_canvas = Object::cast_to<PaintCanvas>(ObjectDB::get_instance(_paint_canvas));
+
+	ERR_FAIL_COND(!paint_canvas);
+
+	int tool_id = paint_canvas->get_current_tool();
+
+	for (int i = 0; i < _grid->get_child_count(); ++i) {
+		Button *b = Object::cast_to<Button>(_grid->get_child(i));
+
+		if (!b) {
+			continue;
+		}
+
+		int bid = b->get_meta("button_id");
+
+		if (bid == tool_id) {
+			b->set_pressed(true);
+			return;
+		}
+	}
+}
+
+void PaintToolsPropertyInspector::_bind_methods() {
+	ClassDB::bind_method(D_METHOD("on_button_toggled"), &PaintToolsPropertyInspector::on_button_toggled);
+	ClassDB::bind_method(D_METHOD("_on_tool_changed"), &PaintToolsPropertyInspector::_on_tool_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
new file mode 100644
index 000000000..9ed735831
--- /dev/null
+++ b/modules/paint/ui/property_inspectors/paint_tools_property_inspector.h
@@ -0,0 +1,60 @@
+#ifndef PAINT_TOOLS_PROPERTY_INSPECTOR_H
+#define PAINT_TOOLS_PROPERTY_INSPECTOR_H
+
+/*
+Copyright (c) 2022 Péter Magyar
+
+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.
+*/
+
+#include "paint_custom_property_inspector.h"
+#include "core/object/object_id.h"
+#include "core/object/reference.h"
+
+class HFlowContainer;
+class PaintNode;
+class PaintCanvas;
+class PaintProject;
+class ButtonGroup;
+
+class PaintToolsPropertyInspector : public PaintCustomPropertyInspector {
+	GDCLASS(PaintToolsPropertyInspector, PaintCustomPropertyInspector);
+
+public:
+	void add_button(int id, const String &hint, const String &icon, const String &theme_type);
+
+	void _on_paint_node_selected(Node *paint_node);
+
+	PaintToolsPropertyInspector();
+	~PaintToolsPropertyInspector();
+
+protected:
+	void on_button_toggled(bool on, int id);
+	void _on_tool_changed();
+
+	static void _bind_methods();
+
+	HFlowContainer *_grid;
+
+	ObjectID _paint_canvas;
+
+	Ref<ButtonGroup> _group;
+};
+
+#endif