skeleton_ik_editor_plugin.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_ik_editor_plugin.h"
  31. #include "scene/animation/skeleton_ik.h"
  32. void SkeletonIKEditorPlugin::_play() {
  33. if (!skeleton_ik)
  34. return;
  35. if (!skeleton_ik->get_parent_skeleton())
  36. return;
  37. if (play_btn->is_pressed()) {
  38. initial_bone_poses.resize(skeleton_ik->get_parent_skeleton()->get_bone_count());
  39. for (int i = 0; i < skeleton_ik->get_parent_skeleton()->get_bone_count(); ++i) {
  40. initial_bone_poses.write[i] = skeleton_ik->get_parent_skeleton()->get_bone_pose(i);
  41. }
  42. skeleton_ik->start();
  43. } else {
  44. skeleton_ik->stop();
  45. if (initial_bone_poses.size() != skeleton_ik->get_parent_skeleton()->get_bone_count())
  46. return;
  47. for (int i = 0; i < skeleton_ik->get_parent_skeleton()->get_bone_count(); ++i) {
  48. skeleton_ik->get_parent_skeleton()->set_bone_pose(i, initial_bone_poses[i]);
  49. }
  50. }
  51. }
  52. void SkeletonIKEditorPlugin::edit(Object *p_object) {
  53. if (p_object != skeleton_ik) {
  54. if (skeleton_ik) {
  55. play_btn->set_pressed(false);
  56. _play();
  57. }
  58. }
  59. SkeletonIK *s = Object::cast_to<SkeletonIK>(p_object);
  60. if (!s)
  61. return;
  62. skeleton_ik = s;
  63. }
  64. bool SkeletonIKEditorPlugin::handles(Object *p_object) const {
  65. return p_object->is_class("SkeletonIK");
  66. }
  67. void SkeletonIKEditorPlugin::make_visible(bool p_visible) {
  68. if (p_visible)
  69. play_btn->show();
  70. else
  71. play_btn->hide();
  72. }
  73. void SkeletonIKEditorPlugin::_bind_methods() {
  74. ClassDB::bind_method("_play", &SkeletonIKEditorPlugin::_play);
  75. }
  76. SkeletonIKEditorPlugin::SkeletonIKEditorPlugin(EditorNode *p_node) {
  77. editor = p_node;
  78. play_btn = memnew(Button);
  79. play_btn->set_icon(editor->get_gui_base()->get_icon("Play", "EditorIcons"));
  80. play_btn->set_text(TTR("Play IK"));
  81. play_btn->set_toggle_mode(true);
  82. play_btn->hide();
  83. play_btn->connect("pressed", this, "_play");
  84. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, play_btn);
  85. skeleton_ik = NULL;
  86. }
  87. SkeletonIKEditorPlugin::~SkeletonIKEditorPlugin() {}