bone_map.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**************************************************************************/
  2. /* bone_map.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_map.h"
  31. bool BoneMap::_set(const StringName &p_path, const Variant &p_value) {
  32. String path = p_path;
  33. if (path.begins_with("bone_map/")) {
  34. String which = path.get_slicec('/', 1);
  35. set_skeleton_bone_name(which, p_value);
  36. return true;
  37. }
  38. return false;
  39. }
  40. bool BoneMap::_get(const StringName &p_path, Variant &r_ret) const {
  41. String path = p_path;
  42. if (path.begins_with("bone_map/")) {
  43. String which = path.get_slicec('/', 1);
  44. r_ret = get_skeleton_bone_name(which);
  45. return true;
  46. }
  47. return false;
  48. }
  49. void BoneMap::_get_property_list(List<PropertyInfo> *p_list) const {
  50. HashMap<StringName, StringName>::ConstIterator E = bone_map.begin();
  51. while (E) {
  52. p_list->push_back(PropertyInfo(Variant::STRING_NAME, "bone_map/" + E->key, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  53. ++E;
  54. }
  55. }
  56. Ref<SkeletonProfile> BoneMap::get_profile() const {
  57. return profile;
  58. }
  59. void BoneMap::set_profile(const Ref<SkeletonProfile> &p_profile) {
  60. bool is_changed = profile != p_profile;
  61. if (is_changed) {
  62. if (!profile.is_null() && profile->is_connected("profile_updated", callable_mp(this, &BoneMap::_update_profile))) {
  63. profile->disconnect("profile_updated", callable_mp(this, &BoneMap::_update_profile));
  64. }
  65. profile = p_profile;
  66. if (!profile.is_null()) {
  67. profile->connect("profile_updated", callable_mp(this, &BoneMap::_update_profile));
  68. }
  69. _update_profile();
  70. }
  71. notify_property_list_changed();
  72. }
  73. StringName BoneMap::get_skeleton_bone_name(const StringName &p_profile_bone_name) const {
  74. ERR_FAIL_COND_V(!bone_map.has(p_profile_bone_name), StringName());
  75. return bone_map.get(p_profile_bone_name);
  76. }
  77. void BoneMap::_set_skeleton_bone_name(const StringName &p_profile_bone_name, const StringName &p_skeleton_bone_name) {
  78. ERR_FAIL_COND(!bone_map.has(p_profile_bone_name));
  79. bone_map.insert(p_profile_bone_name, p_skeleton_bone_name);
  80. }
  81. void BoneMap::set_skeleton_bone_name(const StringName &p_profile_bone_name, const StringName &p_skeleton_bone_name) {
  82. _set_skeleton_bone_name(p_profile_bone_name, p_skeleton_bone_name);
  83. emit_signal("bone_map_updated");
  84. }
  85. StringName BoneMap::find_profile_bone_name(const StringName &p_skeleton_bone_name) const {
  86. StringName profile_bone_name;
  87. HashMap<StringName, StringName>::ConstIterator E = bone_map.begin();
  88. while (E) {
  89. if (E->value == p_skeleton_bone_name) {
  90. profile_bone_name = E->key;
  91. break;
  92. }
  93. ++E;
  94. }
  95. return profile_bone_name;
  96. }
  97. int BoneMap::get_skeleton_bone_name_count(const StringName &p_skeleton_bone_name) const {
  98. int count = 0;
  99. HashMap<StringName, StringName>::ConstIterator E = bone_map.begin();
  100. while (E) {
  101. if (E->value == p_skeleton_bone_name) {
  102. ++count;
  103. }
  104. ++E;
  105. }
  106. return count;
  107. }
  108. void BoneMap::_update_profile() {
  109. _validate_bone_map();
  110. emit_signal("profile_updated");
  111. }
  112. void BoneMap::_validate_bone_map() {
  113. Ref<SkeletonProfile> current_profile = get_profile();
  114. if (current_profile.is_valid()) {
  115. // Insert missing profile bones into bone map.
  116. int len = current_profile->get_bone_size();
  117. StringName profile_bone_name;
  118. for (int i = 0; i < len; i++) {
  119. profile_bone_name = current_profile->get_bone_name(i);
  120. if (!bone_map.has(profile_bone_name)) {
  121. bone_map.insert(profile_bone_name, StringName());
  122. }
  123. }
  124. // Remove bones that do not exist in the profile from the map.
  125. Vector<StringName> delete_bones;
  126. StringName k;
  127. HashMap<StringName, StringName>::ConstIterator E = bone_map.begin();
  128. while (E) {
  129. k = E->key;
  130. if (!current_profile->has_bone(k)) {
  131. delete_bones.push_back(k);
  132. }
  133. ++E;
  134. }
  135. len = delete_bones.size();
  136. for (int i = 0; i < len; i++) {
  137. bone_map.erase(delete_bones[i]);
  138. }
  139. } else {
  140. bone_map.clear();
  141. }
  142. }
  143. void BoneMap::_bind_methods() {
  144. ClassDB::bind_method(D_METHOD("get_profile"), &BoneMap::get_profile);
  145. ClassDB::bind_method(D_METHOD("set_profile", "profile"), &BoneMap::set_profile);
  146. ClassDB::bind_method(D_METHOD("get_skeleton_bone_name", "profile_bone_name"), &BoneMap::get_skeleton_bone_name);
  147. ClassDB::bind_method(D_METHOD("set_skeleton_bone_name", "profile_bone_name", "skeleton_bone_name"), &BoneMap::set_skeleton_bone_name);
  148. ClassDB::bind_method(D_METHOD("find_profile_bone_name", "skeleton_bone_name"), &BoneMap::find_profile_bone_name);
  149. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "profile", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonProfile"), "set_profile", "get_profile");
  150. ADD_ARRAY("bonemap", "bonemap");
  151. ADD_SIGNAL(MethodInfo("bone_map_updated"));
  152. ADD_SIGNAL(MethodInfo("profile_updated"));
  153. }
  154. void BoneMap::_validate_property(PropertyInfo &property) const {
  155. if (property.name == "bonemap" || property.name == "profile") {
  156. property.usage = PROPERTY_USAGE_NO_EDITOR;
  157. }
  158. }
  159. BoneMap::BoneMap() {
  160. _validate_bone_map();
  161. }
  162. BoneMap::~BoneMap() {
  163. }
  164. //////////////////////////////////////