1 /*************************************************************************/
2 /*  color_ramp_editor_plugin.cpp                                         */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #include "color_ramp_editor_plugin.h"
31 #include "canvas_item_editor_plugin.h"
32 #include "spatial_editor_plugin.h"
33 
ColorRampEditorPlugin(EditorNode * p_node)34 ColorRampEditorPlugin::ColorRampEditorPlugin(EditorNode *p_node) {
35 
36 	editor = p_node;
37 	ramp_editor = memnew(ColorRampEdit);
38 
39 	add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, ramp_editor);
40 
41 	ramp_editor->set_custom_minimum_size(Size2(100, 48));
42 	ramp_editor->hide();
43 	ramp_editor->connect("ramp_changed", this, "ramp_changed");
44 }
45 
edit(Object * p_object)46 void ColorRampEditorPlugin::edit(Object *p_object) {
47 
48 	ColorRamp *color_ramp = p_object->cast_to<ColorRamp>();
49 	if (!color_ramp)
50 		return;
51 	color_ramp_ref = Ref<ColorRamp>(color_ramp);
52 	ramp_editor->set_points(color_ramp_ref->get_points());
53 }
54 
handles(Object * p_object) const55 bool ColorRampEditorPlugin::handles(Object *p_object) const {
56 
57 	return p_object->is_type("ColorRamp");
58 }
59 
make_visible(bool p_visible)60 void ColorRampEditorPlugin::make_visible(bool p_visible) {
61 
62 	if (p_visible) {
63 		ramp_editor->show();
64 	} else {
65 		ramp_editor->hide();
66 	}
67 }
68 
_ramp_changed()69 void ColorRampEditorPlugin::_ramp_changed() {
70 
71 	if (color_ramp_ref.is_valid()) {
72 
73 		UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
74 
75 		//Not sure if I should convert this data to DVector
76 		Vector<float> new_offsets = ramp_editor->get_offsets();
77 		Vector<Color> new_colors = ramp_editor->get_colors();
78 		Vector<float> old_offsets = color_ramp_ref->get_offsets();
79 		Vector<Color> old_colors = color_ramp_ref->get_colors();
80 
81 		if (old_offsets.size() != new_offsets.size())
82 			ur->create_action(TTR("Add/Remove Color Ramp Point"));
83 		else
84 			ur->create_action(TTR("Modify Color Ramp"), UndoRedo::MERGE_ENDS);
85 		ur->add_do_method(this, "undo_redo_color_ramp", new_offsets, new_colors);
86 		ur->add_undo_method(this, "undo_redo_color_ramp", old_offsets, old_colors);
87 		ur->commit_action();
88 
89 		//color_ramp_ref->set_points(ramp_editor->get_points());
90 	}
91 }
92 
_undo_redo_color_ramp(const Vector<float> & offsets,const Vector<Color> & colors)93 void ColorRampEditorPlugin::_undo_redo_color_ramp(const Vector<float> &offsets,
94 		const Vector<Color> &colors) {
95 
96 	color_ramp_ref->set_offsets(offsets);
97 	color_ramp_ref->set_colors(colors);
98 	ramp_editor->set_points(color_ramp_ref->get_points());
99 	ramp_editor->update();
100 }
101 
~ColorRampEditorPlugin()102 ColorRampEditorPlugin::~ColorRampEditorPlugin() {
103 }
104 
_bind_methods()105 void ColorRampEditorPlugin::_bind_methods() {
106 	ObjectTypeDB::bind_method(_MD("ramp_changed"), &ColorRampEditorPlugin::_ramp_changed);
107 	ObjectTypeDB::bind_method(_MD("undo_redo_color_ramp", "offsets", "colors"), &ColorRampEditorPlugin::_undo_redo_color_ramp);
108 }
109