bone_attachment.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**************************************************************************/
  2. /* bone_attachment.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 "bone_attachment.h"
  31. void BoneAttachment::_validate_property(PropertyInfo &property) const {
  32. if (property.name == "bone_name") {
  33. Skeleton *parent = Object::cast_to<Skeleton>(get_parent());
  34. if (parent) {
  35. String names;
  36. for (int i = 0; i < parent->get_bone_count(); i++) {
  37. if (i > 0) {
  38. names += ",";
  39. }
  40. names += parent->get_bone_name(i);
  41. }
  42. property.hint = PROPERTY_HINT_ENUM;
  43. property.hint_string = names;
  44. } else {
  45. property.hint = PROPERTY_HINT_NONE;
  46. property.hint_string = "";
  47. }
  48. }
  49. }
  50. void BoneAttachment::_check_bind() {
  51. Skeleton *sk = Object::cast_to<Skeleton>(get_parent());
  52. if (sk) {
  53. int idx = sk->find_bone(bone_name);
  54. if (idx != -1) {
  55. sk->bind_child_node_to_bone(idx, this);
  56. set_transform(sk->get_bone_global_pose(idx));
  57. bound = true;
  58. }
  59. }
  60. }
  61. void BoneAttachment::_check_unbind() {
  62. if (bound) {
  63. Skeleton *sk = Object::cast_to<Skeleton>(get_parent());
  64. if (sk) {
  65. int idx = sk->find_bone(bone_name);
  66. if (idx != -1) {
  67. sk->unbind_child_node_from_bone(idx, this);
  68. }
  69. }
  70. bound = false;
  71. }
  72. }
  73. void BoneAttachment::set_bone_name(const String &p_name) {
  74. if (is_inside_tree()) {
  75. _check_unbind();
  76. }
  77. bone_name = p_name;
  78. if (is_inside_tree()) {
  79. _check_bind();
  80. }
  81. }
  82. String BoneAttachment::get_bone_name() const {
  83. return bone_name;
  84. }
  85. void BoneAttachment::_notification(int p_what) {
  86. switch (p_what) {
  87. case NOTIFICATION_ENTER_TREE: {
  88. _check_bind();
  89. } break;
  90. case NOTIFICATION_EXIT_TREE: {
  91. _check_unbind();
  92. } break;
  93. }
  94. }
  95. BoneAttachment::BoneAttachment() {
  96. bound = false;
  97. }
  98. void BoneAttachment::_bind_methods() {
  99. ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment::set_bone_name);
  100. ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment::get_bone_name);
  101. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bone_name"), "set_bone_name", "get_bone_name");
  102. }