bone_attachment.cpp 4.0 KB

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