bone_attachment.cpp 4.2 KB

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