bone_attachment.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*************************************************************************/
  2. /* bone_attachment.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "bone_attachment.h"
  30. bool BoneAttachment::_get(const StringName& p_name,Variant &r_ret) const {
  31. if (String(p_name)=="bone_name") {
  32. r_ret=get_bone_name();
  33. return true;
  34. }
  35. return false;
  36. }
  37. bool BoneAttachment::_set(const StringName& p_name, const Variant& p_value){
  38. if (String(p_name)=="bone_name") {
  39. set_bone_name(p_value);
  40. return true;
  41. }
  42. return false;
  43. }
  44. void BoneAttachment::_get_property_list( List<PropertyInfo>* p_list ) const{
  45. Skeleton *parent=NULL;
  46. if(get_parent())
  47. parent=get_parent()->cast_to<Skeleton>();
  48. if (parent) {
  49. String names;
  50. for(int i=0;i<parent->get_bone_count();i++) {
  51. if(i>0)
  52. names+=",";
  53. names+=parent->get_bone_name(i);
  54. }
  55. p_list->push_back(PropertyInfo(Variant::STRING,"bone_name",PROPERTY_HINT_ENUM,names));
  56. } else {
  57. p_list->push_back(PropertyInfo(Variant::STRING,"bone_name"));
  58. }
  59. }
  60. void BoneAttachment::_check_bind() {
  61. if (get_parent() && get_parent()->cast_to<Skeleton>()) {
  62. Skeleton *sk = get_parent()->cast_to<Skeleton>();
  63. int idx = sk->find_bone(bone_name);
  64. if (idx!=-1) {
  65. sk->bind_child_node_to_bone(idx,this);;
  66. set_transform(sk->get_bone_global_pose(idx));
  67. bound=true;
  68. }
  69. }
  70. }
  71. void BoneAttachment::_check_unbind() {
  72. if (bound) {
  73. if (get_parent() && get_parent()->cast_to<Skeleton>()) {
  74. Skeleton *sk = get_parent()->cast_to<Skeleton>();
  75. int idx = sk->find_bone(bone_name);
  76. if (idx!=-1) {
  77. sk->unbind_child_node_from_bone(idx,this);;
  78. }
  79. }
  80. bound=false;
  81. }
  82. }
  83. void BoneAttachment::set_bone_name(const String& p_name) {
  84. if (is_inside_tree())
  85. _check_unbind();
  86. bone_name=p_name;
  87. if (is_inside_tree())
  88. _check_bind();
  89. }
  90. String BoneAttachment::get_bone_name() const{
  91. return bone_name;
  92. }
  93. void BoneAttachment::_notification(int p_what) {
  94. switch(p_what) {
  95. case NOTIFICATION_ENTER_TREE: {
  96. _check_bind();
  97. } break;
  98. case NOTIFICATION_EXIT_TREE: {
  99. _check_unbind();
  100. } break;
  101. }
  102. }
  103. BoneAttachment::BoneAttachment()
  104. {
  105. bound=false;
  106. }