skeleton_ik_editor_plugin.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**************************************************************************/
  2. /* skeleton_ik_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. #include "skeleton_ik_editor_plugin.h"
  31. #include "scene/animation/skeleton_ik.h"
  32. void SkeletonIKEditorPlugin::_play() {
  33. if (!skeleton_ik) {
  34. return;
  35. }
  36. if (!skeleton_ik->get_parent_skeleton()) {
  37. return;
  38. }
  39. if (play_btn->is_pressed()) {
  40. skeleton_ik->start();
  41. } else {
  42. skeleton_ik->stop();
  43. skeleton_ik->get_parent_skeleton()->clear_bones_global_pose_override();
  44. }
  45. }
  46. void SkeletonIKEditorPlugin::edit(Object *p_object) {
  47. if (p_object != skeleton_ik) {
  48. if (skeleton_ik) {
  49. play_btn->set_pressed(false);
  50. _play();
  51. }
  52. }
  53. SkeletonIK *s = Object::cast_to<SkeletonIK>(p_object);
  54. if (!s) {
  55. return;
  56. }
  57. skeleton_ik = s;
  58. }
  59. bool SkeletonIKEditorPlugin::handles(Object *p_object) const {
  60. return p_object->is_class("SkeletonIK");
  61. }
  62. void SkeletonIKEditorPlugin::make_visible(bool p_visible) {
  63. if (p_visible) {
  64. play_btn->show();
  65. } else {
  66. play_btn->hide();
  67. }
  68. }
  69. void SkeletonIKEditorPlugin::_bind_methods() {
  70. ClassDB::bind_method("_play", &SkeletonIKEditorPlugin::_play);
  71. }
  72. SkeletonIKEditorPlugin::SkeletonIKEditorPlugin(EditorNode *p_node) {
  73. editor = p_node;
  74. play_btn = memnew(Button);
  75. play_btn->set_icon(editor->get_gui_base()->get_icon("Play", "EditorIcons"));
  76. play_btn->set_text(TTR("Play IK"));
  77. play_btn->set_toggle_mode(true);
  78. play_btn->hide();
  79. play_btn->connect("pressed", this, "_play");
  80. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, play_btn);
  81. skeleton_ik = nullptr;
  82. }
  83. SkeletonIKEditorPlugin::~SkeletonIKEditorPlugin() {}