1 /*************************************************************************/
2 /*  skeleton_2d_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 "skeleton_2d_editor_plugin.h"
32 
33 #include "canvas_item_editor_plugin.h"
34 #include "scene/2d/mesh_instance_2d.h"
35 #include "scene/gui/box_container.h"
36 #include "thirdparty/misc/clipper.hpp"
37 
_node_removed(Node * p_node)38 void Skeleton2DEditor::_node_removed(Node *p_node) {
39 
40 	if (p_node == node) {
41 		node = NULL;
42 		options->hide();
43 	}
44 }
45 
edit(Skeleton2D * p_sprite)46 void Skeleton2DEditor::edit(Skeleton2D *p_sprite) {
47 
48 	node = p_sprite;
49 }
50 
_menu_option(int p_option)51 void Skeleton2DEditor::_menu_option(int p_option) {
52 
53 	if (!node) {
54 		return;
55 	}
56 
57 	switch (p_option) {
58 		case MENU_OPTION_MAKE_REST: {
59 
60 			if (node->get_bone_count() == 0) {
61 				err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes."));
62 				err_dialog->popup_centered_minsize();
63 				return;
64 			}
65 			UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
66 			ur->create_action(TTR("Create Rest Pose from Bones"));
67 			for (int i = 0; i < node->get_bone_count(); i++) {
68 				Bone2D *bone = node->get_bone(i);
69 				ur->add_do_method(bone, "set_rest", bone->get_transform());
70 				ur->add_undo_method(bone, "set_rest", bone->get_rest());
71 			}
72 			ur->commit_action();
73 
74 		} break;
75 		case MENU_OPTION_SET_REST: {
76 			if (node->get_bone_count() == 0) {
77 				err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes."));
78 				err_dialog->popup_centered_minsize();
79 				return;
80 			}
81 			UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
82 			ur->create_action(TTR("Set Rest Pose to Bones"));
83 			for (int i = 0; i < node->get_bone_count(); i++) {
84 				Bone2D *bone = node->get_bone(i);
85 				ur->add_do_method(bone, "set_transform", bone->get_rest());
86 				ur->add_undo_method(bone, "set_transform", bone->get_transform());
87 			}
88 			ur->commit_action();
89 
90 		} break;
91 	}
92 }
93 
_bind_methods()94 void Skeleton2DEditor::_bind_methods() {
95 
96 	ClassDB::bind_method("_menu_option", &Skeleton2DEditor::_menu_option);
97 }
98 
Skeleton2DEditor()99 Skeleton2DEditor::Skeleton2DEditor() {
100 
101 	options = memnew(MenuButton);
102 
103 	CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
104 
105 	options->set_text(TTR("Skeleton2D"));
106 	options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Skeleton2D", "EditorIcons"));
107 
108 	options->get_popup()->add_item(TTR("Make Rest Pose (From Bones)"), MENU_OPTION_MAKE_REST);
109 	options->get_popup()->add_separator();
110 	options->get_popup()->add_item(TTR("Set Bones to Rest Pose"), MENU_OPTION_SET_REST);
111 	options->set_switch_on_hover(true);
112 
113 	options->get_popup()->connect("id_pressed", this, "_menu_option");
114 
115 	err_dialog = memnew(AcceptDialog);
116 	add_child(err_dialog);
117 }
118 
edit(Object * p_object)119 void Skeleton2DEditorPlugin::edit(Object *p_object) {
120 
121 	sprite_editor->edit(Object::cast_to<Skeleton2D>(p_object));
122 }
123 
handles(Object * p_object) const124 bool Skeleton2DEditorPlugin::handles(Object *p_object) const {
125 
126 	return p_object->is_class("Skeleton2D");
127 }
128 
make_visible(bool p_visible)129 void Skeleton2DEditorPlugin::make_visible(bool p_visible) {
130 
131 	if (p_visible) {
132 		sprite_editor->options->show();
133 	} else {
134 
135 		sprite_editor->options->hide();
136 		sprite_editor->edit(NULL);
137 	}
138 }
139 
Skeleton2DEditorPlugin(EditorNode * p_node)140 Skeleton2DEditorPlugin::Skeleton2DEditorPlugin(EditorNode *p_node) {
141 
142 	editor = p_node;
143 	sprite_editor = memnew(Skeleton2DEditor);
144 	editor->get_viewport()->add_child(sprite_editor);
145 	make_visible(false);
146 
147 	//sprite_editor->options->hide();
148 }
149 
~Skeleton2DEditorPlugin()150 Skeleton2DEditorPlugin::~Skeleton2DEditorPlugin() {
151 }
152