bone_map.cpp 7.0 KB

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