| 
									
										
										
										
											2018-05-16 14:19:33 -03:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  skeleton_2d_editor_plugin.cpp                                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* 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.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | #include "skeleton_2d_editor_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 02:46:22 +01:00
										 |  |  | #include "editor/editor_node.h"
 | 
					
						
							| 
									
										
										
										
											2023-09-13 13:14:07 +02:00
										 |  |  | #include "editor/editor_string_names.h"
 | 
					
						
							| 
									
										
										
										
											2022-03-25 18:06:46 +01:00
										 |  |  | #include "editor/editor_undo_redo_manager.h"
 | 
					
						
							| 
									
										
										
										
											2023-04-07 18:59:49 +02:00
										 |  |  | #include "editor/plugins/canvas_item_editor_plugin.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | #include "scene/2d/mesh_instance_2d.h"
 | 
					
						
							| 
									
										
										
										
											2023-04-07 18:59:49 +02:00
										 |  |  | #include "scene/gui/dialogs.h"
 | 
					
						
							| 
									
										
										
										
											2022-11-19 12:45:49 +01:00
										 |  |  | #include "scene/gui/menu_button.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void Skeleton2DEditor::_node_removed(Node *p_node) { | 
					
						
							|  |  |  | 	if (p_node == node) { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		node = nullptr; | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 		options->hide(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Skeleton2DEditor::edit(Skeleton2D *p_sprite) { | 
					
						
							|  |  |  | 	node = p_sprite; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Skeleton2DEditor::_menu_option(int p_option) { | 
					
						
							| 
									
										
										
										
											2018-04-13 12:07:47 +03:00
										 |  |  | 	if (!node) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 	switch (p_option) { | 
					
						
							| 
									
										
										
										
											2021-11-10 21:08:56 +01:00
										 |  |  | 		case MENU_OPTION_SET_REST: { | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 			if (node->get_bone_count() == 0) { | 
					
						
							|  |  |  | 				err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes.")); | 
					
						
							| 
									
										
										
										
											2020-03-06 14:00:16 -03:00
										 |  |  | 				err_dialog->popup_centered(); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-12-23 23:53:16 +01:00
										 |  |  | 			EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); | 
					
						
							| 
									
										
										
										
											2021-11-10 21:08:56 +01:00
										 |  |  | 			ur->create_action(TTR("Set Rest Pose to Bones")); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 			for (int i = 0; i < node->get_bone_count(); i++) { | 
					
						
							|  |  |  | 				Bone2D *bone = node->get_bone(i); | 
					
						
							| 
									
										
										
										
											2021-11-10 21:08:56 +01:00
										 |  |  | 				ur->add_do_method(bone, "set_transform", bone->get_rest()); | 
					
						
							|  |  |  | 				ur->add_undo_method(bone, "set_transform", bone->get_transform()); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			ur->commit_action(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2021-11-10 21:08:56 +01:00
										 |  |  | 		case MENU_OPTION_MAKE_REST: { | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 			if (node->get_bone_count() == 0) { | 
					
						
							|  |  |  | 				err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes.")); | 
					
						
							| 
									
										
										
										
											2020-03-06 14:00:16 -03:00
										 |  |  | 				err_dialog->popup_centered(); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-12-23 23:53:16 +01:00
										 |  |  | 			EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); | 
					
						
							| 
									
										
										
										
											2021-11-10 21:08:56 +01:00
										 |  |  | 			ur->create_action(TTR("Create Rest Pose from Bones")); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 			for (int i = 0; i < node->get_bone_count(); i++) { | 
					
						
							|  |  |  | 				Bone2D *bone = node->get_bone(i); | 
					
						
							| 
									
										
										
										
											2021-11-10 21:08:56 +01:00
										 |  |  | 				ur->add_do_method(bone, "set_rest", bone->get_transform()); | 
					
						
							|  |  |  | 				ur->add_undo_method(bone, "set_rest", bone->get_rest()); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			ur->commit_action(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Skeleton2DEditor::Skeleton2DEditor() { | 
					
						
							|  |  |  | 	options = memnew(MenuButton); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	options->set_text(TTR("Skeleton2D")); | 
					
						
							| 
									
										
										
										
											2023-09-13 13:14:07 +02:00
										 |  |  | 	options->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Skeleton2D"), EditorStringName(EditorIcons))); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 21:08:56 +01:00
										 |  |  | 	options->get_popup()->add_item(TTR("Reset to Rest Pose"), MENU_OPTION_SET_REST); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 	options->get_popup()->add_separator(); | 
					
						
							| 
									
										
										
										
											2021-09-01 17:49:57 +02:00
										 |  |  | 	// Use the "Overwrite" word to highlight that this is a destructive operation.
 | 
					
						
							| 
									
										
										
										
											2021-11-10 21:08:56 +01:00
										 |  |  | 	options->get_popup()->add_item(TTR("Overwrite Rest Pose"), MENU_OPTION_MAKE_REST); | 
					
						
							| 
									
										
										
										
											2019-04-20 15:52:01 -03:00
										 |  |  | 	options->set_switch_on_hover(true); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-14 14:13:31 +02:00
										 |  |  | 	options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Skeleton2DEditor::_menu_option)); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err_dialog = memnew(AcceptDialog); | 
					
						
							|  |  |  | 	add_child(err_dialog); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Skeleton2DEditorPlugin::edit(Object *p_object) { | 
					
						
							|  |  |  | 	sprite_editor->edit(Object::cast_to<Skeleton2D>(p_object)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Skeleton2DEditorPlugin::handles(Object *p_object) const { | 
					
						
							|  |  |  | 	return p_object->is_class("Skeleton2D"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Skeleton2DEditorPlugin::make_visible(bool p_visible) { | 
					
						
							|  |  |  | 	if (p_visible) { | 
					
						
							|  |  |  | 		sprite_editor->options->show(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		sprite_editor->options->hide(); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		sprite_editor->edit(nullptr); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 10:36:51 +01:00
										 |  |  | Skeleton2DEditorPlugin::Skeleton2DEditorPlugin() { | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 	sprite_editor = memnew(Skeleton2DEditor); | 
					
						
							| 
									
										
										
										
											2024-08-19 18:08:31 -04:00
										 |  |  | 	EditorNode::get_singleton()->get_gui_base()->add_child(sprite_editor); | 
					
						
							| 
									
										
										
										
											2018-04-13 12:07:47 +03:00
										 |  |  | 	make_visible(false); | 
					
						
							| 
									
										
										
										
											2018-02-21 17:23:27 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//sprite_editor->options->hide();
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Skeleton2DEditorPlugin::~Skeleton2DEditorPlugin() { | 
					
						
							|  |  |  | } |