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) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
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 "editor/editor_node.h" |
34 | #include "editor/editor_string_names.h" |
35 | #include "editor/editor_undo_redo_manager.h" |
36 | #include "editor/plugins/canvas_item_editor_plugin.h" |
37 | #include "scene/2d/mesh_instance_2d.h" |
38 | #include "scene/gui/dialogs.h" |
39 | #include "scene/gui/menu_button.h" |
40 | |
41 | void Skeleton2DEditor::_node_removed(Node *p_node) { |
42 | if (p_node == node) { |
43 | node = nullptr; |
44 | options->hide(); |
45 | } |
46 | } |
47 | |
48 | void Skeleton2DEditor::edit(Skeleton2D *p_sprite) { |
49 | node = p_sprite; |
50 | } |
51 | |
52 | void Skeleton2DEditor::(int p_option) { |
53 | if (!node) { |
54 | return; |
55 | } |
56 | |
57 | switch (p_option) { |
58 | case MENU_OPTION_SET_REST: { |
59 | if (node->get_bone_count() == 0) { |
60 | err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes." )); |
61 | err_dialog->popup_centered(); |
62 | return; |
63 | } |
64 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
65 | ur->create_action(TTR("Set Rest Pose to Bones" )); |
66 | for (int i = 0; i < node->get_bone_count(); i++) { |
67 | Bone2D *bone = node->get_bone(i); |
68 | ur->add_do_method(bone, "set_transform" , bone->get_rest()); |
69 | ur->add_undo_method(bone, "set_transform" , bone->get_transform()); |
70 | } |
71 | ur->commit_action(); |
72 | |
73 | } break; |
74 | case MENU_OPTION_MAKE_REST: { |
75 | if (node->get_bone_count() == 0) { |
76 | err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes." )); |
77 | err_dialog->popup_centered(); |
78 | return; |
79 | } |
80 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
81 | ur->create_action(TTR("Create Rest Pose from Bones" )); |
82 | for (int i = 0; i < node->get_bone_count(); i++) { |
83 | Bone2D *bone = node->get_bone(i); |
84 | ur->add_do_method(bone, "set_rest" , bone->get_transform()); |
85 | ur->add_undo_method(bone, "set_rest" , bone->get_rest()); |
86 | } |
87 | ur->commit_action(); |
88 | |
89 | } break; |
90 | } |
91 | } |
92 | |
93 | void Skeleton2DEditor::_bind_methods() { |
94 | } |
95 | |
96 | Skeleton2DEditor::Skeleton2DEditor() { |
97 | options = memnew(MenuButton); |
98 | |
99 | CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options); |
100 | |
101 | options->set_text(TTR("Skeleton2D" )); |
102 | options->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Skeleton2D" ), EditorStringName(EditorIcons))); |
103 | |
104 | options->get_popup()->add_item(TTR("Reset to Rest Pose" ), MENU_OPTION_SET_REST); |
105 | options->get_popup()->add_separator(); |
106 | // Use the "Overwrite" word to highlight that this is a destructive operation. |
107 | options->get_popup()->add_item(TTR("Overwrite Rest Pose" ), MENU_OPTION_MAKE_REST); |
108 | options->set_switch_on_hover(true); |
109 | |
110 | options->get_popup()->connect("id_pressed" , callable_mp(this, &Skeleton2DEditor::_menu_option)); |
111 | |
112 | err_dialog = memnew(AcceptDialog); |
113 | add_child(err_dialog); |
114 | } |
115 | |
116 | void Skeleton2DEditorPlugin::edit(Object *p_object) { |
117 | sprite_editor->edit(Object::cast_to<Skeleton2D>(p_object)); |
118 | } |
119 | |
120 | bool Skeleton2DEditorPlugin::handles(Object *p_object) const { |
121 | return p_object->is_class("Skeleton2D" ); |
122 | } |
123 | |
124 | void Skeleton2DEditorPlugin::make_visible(bool p_visible) { |
125 | if (p_visible) { |
126 | sprite_editor->options->show(); |
127 | } else { |
128 | sprite_editor->options->hide(); |
129 | sprite_editor->edit(nullptr); |
130 | } |
131 | } |
132 | |
133 | Skeleton2DEditorPlugin::Skeleton2DEditorPlugin() { |
134 | sprite_editor = memnew(Skeleton2DEditor); |
135 | EditorNode::get_singleton()->get_main_screen_control()->add_child(sprite_editor); |
136 | make_visible(false); |
137 | |
138 | //sprite_editor->options->hide(); |
139 | } |
140 | |
141 | Skeleton2DEditorPlugin::~Skeleton2DEditorPlugin() { |
142 | } |
143 | |