bone_attachment.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*************************************************************************/
  2. /* bone_attachment.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "bone_attachment.h"
  31. bool BoneAttachment::_get(const StringName &p_name, Variant &r_ret) const {
  32. if (String(p_name) == "bone_name") {
  33. r_ret = get_bone_name();
  34. return true;
  35. }
  36. return false;
  37. }
  38. bool BoneAttachment::_set(const StringName &p_name, const Variant &p_value) {
  39. if (String(p_name) == "bone_name") {
  40. set_bone_name(p_value);
  41. return true;
  42. }
  43. return false;
  44. }
  45. void BoneAttachment::_get_property_list(List<PropertyInfo> *p_list) const {
  46. Skeleton *parent = NULL;
  47. if (get_parent())
  48. parent = get_parent()->cast_to<Skeleton>();
  49. if (parent) {
  50. String names;
  51. for (int i = 0; i < parent->get_bone_count(); i++) {
  52. if (i > 0)
  53. names += ",";
  54. names += parent->get_bone_name(i);
  55. }
  56. p_list->push_back(PropertyInfo(Variant::STRING, "bone_name", PROPERTY_HINT_ENUM, names));
  57. } else {
  58. p_list->push_back(PropertyInfo(Variant::STRING, "bone_name"));
  59. }
  60. }
  61. void BoneAttachment::_check_bind() {
  62. if (get_parent() && get_parent()->cast_to<Skeleton>()) {
  63. Skeleton *sk = get_parent()->cast_to<Skeleton>();
  64. int idx = sk->find_bone(bone_name);
  65. if (idx != -1) {
  66. sk->bind_child_node_to_bone(idx, this);
  67. set_transform(sk->get_bone_global_pose(idx));
  68. bound = true;
  69. }
  70. }
  71. }
  72. void BoneAttachment::_check_unbind() {
  73. if (bound) {
  74. if (get_parent() && get_parent()->cast_to<Skeleton>()) {
  75. Skeleton *sk = get_parent()->cast_to<Skeleton>();
  76. int idx = sk->find_bone(bone_name);
  77. if (idx != -1) {
  78. sk->unbind_child_node_from_bone(idx, this);
  79. }
  80. }
  81. bound = false;
  82. }
  83. }
  84. void BoneAttachment::set_bone_name(const String &p_name) {
  85. if (is_inside_tree())
  86. _check_unbind();
  87. bone_name = p_name;
  88. if (is_inside_tree())
  89. _check_bind();
  90. }
  91. String BoneAttachment::get_bone_name() const {
  92. return bone_name;
  93. }
  94. void BoneAttachment::_notification(int p_what) {
  95. switch (p_what) {
  96. case NOTIFICATION_ENTER_TREE: {
  97. _check_bind();
  98. } break;
  99. case NOTIFICATION_EXIT_TREE: {
  100. _check_unbind();
  101. } break;
  102. }
  103. }
  104. BoneAttachment::BoneAttachment() {
  105. bound = false;
  106. }
  107. void BoneAttachment::_bind_methods() {
  108. ObjectTypeDB::bind_method(_MD("set_bone_name", "bone_name"), &BoneAttachment::set_bone_name);
  109. ObjectTypeDB::bind_method(_MD("get_bone_name"), &BoneAttachment::get_bone_name);
  110. }