1 /*************************************************************************/
2 /*  gradient_editor_plugin.cpp                                           */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "gradient_editor_plugin.h"
32 
33 #include "canvas_item_editor_plugin.h"
34 #include "editor/editor_scale.h"
35 #include "spatial_editor_plugin.h"
36 
get_minimum_size() const37 Size2 GradientEditor::get_minimum_size() const {
38 	return Size2(0, 60) * EDSCALE;
39 }
_gradient_changed()40 void GradientEditor::_gradient_changed() {
41 
42 	if (editing)
43 		return;
44 
45 	editing = true;
46 	Vector<Gradient::Point> points = gradient->get_points();
47 	set_points(points);
48 	editing = false;
49 }
50 
_ramp_changed()51 void GradientEditor::_ramp_changed() {
52 
53 	editing = true;
54 	UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
55 	undo_redo->create_action(TTR("Gradient Edited"));
56 	undo_redo->add_do_method(gradient.ptr(), "set_offsets", get_offsets());
57 	undo_redo->add_do_method(gradient.ptr(), "set_colors", get_colors());
58 	undo_redo->add_undo_method(gradient.ptr(), "set_offsets", gradient->get_offsets());
59 	undo_redo->add_undo_method(gradient.ptr(), "set_colors", gradient->get_colors());
60 	undo_redo->commit_action();
61 	editing = false;
62 }
63 
_bind_methods()64 void GradientEditor::_bind_methods() {
65 
66 	ClassDB::bind_method("_gradient_changed", &GradientEditor::_gradient_changed);
67 	ClassDB::bind_method("_ramp_changed", &GradientEditor::_ramp_changed);
68 }
69 
set_gradient(const Ref<Gradient> & p_gradient)70 void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {
71 	gradient = p_gradient;
72 	connect("ramp_changed", this, "_ramp_changed");
73 	gradient->connect("changed", this, "_gradient_changed");
74 	set_points(gradient->get_points());
75 }
76 
GradientEditor()77 GradientEditor::GradientEditor() {
78 	editing = false;
79 }
80 
81 ///////////////////////
82 
can_handle(Object * p_object)83 bool EditorInspectorPluginGradient::can_handle(Object *p_object) {
84 
85 	return Object::cast_to<Gradient>(p_object) != NULL;
86 }
87 
parse_begin(Object * p_object)88 void EditorInspectorPluginGradient::parse_begin(Object *p_object) {
89 
90 	Gradient *gradient = Object::cast_to<Gradient>(p_object);
91 	Ref<Gradient> g(gradient);
92 
93 	GradientEditor *editor = memnew(GradientEditor);
94 	editor->set_gradient(g);
95 	add_custom_control(editor);
96 }
97 
GradientEditorPlugin(EditorNode * p_node)98 GradientEditorPlugin::GradientEditorPlugin(EditorNode *p_node) {
99 
100 	Ref<EditorInspectorPluginGradient> plugin;
101 	plugin.instance();
102 	add_inspector_plugin(plugin);
103 }
104