skeleton_2d_editor_plugin.cpp 5.4 KB

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