skeleton_ik_editor_plugin.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. skeleton_ik->start();
  39. } else {
  40. skeleton_ik->stop();
  41. skeleton_ik->get_parent_skeleton()->clear_bones_global_pose_override();
  42. }
  43. }
  44. void SkeletonIKEditorPlugin::edit(Object *p_object) {
  45. if (p_object != skeleton_ik) {
  46. if (skeleton_ik) {
  47. play_btn->set_pressed(false);
  48. _play();
  49. }
  50. }
  51. SkeletonIK *s = Object::cast_to<SkeletonIK>(p_object);
  52. if (!s)
  53. return;
  54. skeleton_ik = s;
  55. }
  56. bool SkeletonIKEditorPlugin::handles(Object *p_object) const {
  57. return p_object->is_class("SkeletonIK");
  58. }
  59. void SkeletonIKEditorPlugin::make_visible(bool p_visible) {
  60. if (p_visible)
  61. play_btn->show();
  62. else
  63. play_btn->hide();
  64. }
  65. void SkeletonIKEditorPlugin::_bind_methods() {
  66. ClassDB::bind_method("_play", &SkeletonIKEditorPlugin::_play);
  67. }
  68. SkeletonIKEditorPlugin::SkeletonIKEditorPlugin(EditorNode *p_node) {
  69. editor = p_node;
  70. play_btn = memnew(Button);
  71. play_btn->set_icon(editor->get_gui_base()->get_icon("Play", "EditorIcons"));
  72. play_btn->set_text(TTR("Play IK"));
  73. play_btn->set_toggle_mode(true);
  74. play_btn->hide();
  75. play_btn->connect("pressed", this, "_play");
  76. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, play_btn);
  77. skeleton_ik = NULL;
  78. }
  79. SkeletonIKEditorPlugin::~SkeletonIKEditorPlugin() {}