skin.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**************************************************************************/
  2. /* skin.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 "skin.h"
  31. void Skin::set_bind_count(int p_size) {
  32. ERR_FAIL_COND(p_size < 0);
  33. binds.resize(p_size);
  34. binds_ptr = binds.ptrw();
  35. bind_count = p_size;
  36. emit_changed();
  37. }
  38. void Skin::add_bind(int p_bone, const Transform3D &p_pose) {
  39. uint32_t index = bind_count;
  40. set_bind_count(bind_count + 1);
  41. set_bind_bone(index, p_bone);
  42. set_bind_pose(index, p_pose);
  43. }
  44. void Skin::add_named_bind(const String &p_name, const Transform3D &p_pose) {
  45. uint32_t index = bind_count;
  46. set_bind_count(bind_count + 1);
  47. set_bind_name(index, p_name);
  48. set_bind_pose(index, p_pose);
  49. }
  50. void Skin::set_bind_name(int p_index, const StringName &p_name) {
  51. ERR_FAIL_INDEX(p_index, bind_count);
  52. bool notify_change = (binds_ptr[p_index].name != StringName()) != (p_name != StringName());
  53. binds_ptr[p_index].name = p_name;
  54. emit_changed();
  55. if (notify_change) {
  56. notify_property_list_changed();
  57. }
  58. }
  59. void Skin::set_bind_bone(int p_index, int p_bone) {
  60. ERR_FAIL_INDEX(p_index, bind_count);
  61. binds_ptr[p_index].bone = p_bone;
  62. emit_changed();
  63. }
  64. void Skin::set_bind_pose(int p_index, const Transform3D &p_pose) {
  65. ERR_FAIL_INDEX(p_index, bind_count);
  66. binds_ptr[p_index].pose = p_pose;
  67. emit_changed();
  68. }
  69. void Skin::clear_binds() {
  70. binds.clear();
  71. binds_ptr = nullptr;
  72. bind_count = 0;
  73. emit_changed();
  74. }
  75. void Skin::reset_state() {
  76. clear_binds();
  77. }
  78. bool Skin::_set(const StringName &p_name, const Variant &p_value) {
  79. String prop_name = p_name;
  80. if (prop_name == "bind_count") {
  81. set_bind_count(p_value);
  82. return true;
  83. } else if (prop_name.begins_with("bind/")) {
  84. int index = prop_name.get_slicec('/', 1).to_int();
  85. String what = prop_name.get_slicec('/', 2);
  86. if (what == "bone") {
  87. set_bind_bone(index, p_value);
  88. return true;
  89. } else if (what == "name") {
  90. set_bind_name(index, p_value);
  91. return true;
  92. } else if (what == "pose") {
  93. set_bind_pose(index, p_value);
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. bool Skin::_get(const StringName &p_name, Variant &r_ret) const {
  100. String prop_name = p_name;
  101. if (prop_name == "bind_count") {
  102. r_ret = get_bind_count();
  103. return true;
  104. } else if (prop_name.begins_with("bind/")) {
  105. int index = prop_name.get_slicec('/', 1).to_int();
  106. String what = prop_name.get_slicec('/', 2);
  107. if (what == "bone") {
  108. r_ret = get_bind_bone(index);
  109. return true;
  110. } else if (what == "name") {
  111. r_ret = get_bind_name(index);
  112. return true;
  113. } else if (what == "pose") {
  114. r_ret = get_bind_pose(index);
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. void Skin::_get_property_list(List<PropertyInfo> *p_list) const {
  121. p_list->push_back(PropertyInfo(Variant::INT, PNAME("bind_count"), PROPERTY_HINT_RANGE, "0,16384,1,or_greater"));
  122. for (int i = 0; i < get_bind_count(); i++) {
  123. const String prefix = vformat("%s/%d/", PNAME("bind"), i);
  124. p_list->push_back(PropertyInfo(Variant::STRING_NAME, prefix + PNAME("name")));
  125. p_list->push_back(PropertyInfo(Variant::INT, prefix + PNAME("bone"), PROPERTY_HINT_RANGE, "0,16384,1,or_greater", get_bind_name(i) != StringName() ? PROPERTY_USAGE_NO_EDITOR : PROPERTY_USAGE_DEFAULT));
  126. p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prefix + PNAME("pose")));
  127. }
  128. }
  129. void Skin::_bind_methods() {
  130. ClassDB::bind_method(D_METHOD("set_bind_count", "bind_count"), &Skin::set_bind_count);
  131. ClassDB::bind_method(D_METHOD("get_bind_count"), &Skin::get_bind_count);
  132. ClassDB::bind_method(D_METHOD("add_bind", "bone", "pose"), &Skin::add_bind);
  133. ClassDB::bind_method(D_METHOD("add_named_bind", "name", "pose"), &Skin::add_named_bind);
  134. ClassDB::bind_method(D_METHOD("set_bind_pose", "bind_index", "pose"), &Skin::set_bind_pose);
  135. ClassDB::bind_method(D_METHOD("get_bind_pose", "bind_index"), &Skin::get_bind_pose);
  136. ClassDB::bind_method(D_METHOD("set_bind_name", "bind_index", "name"), &Skin::set_bind_name);
  137. ClassDB::bind_method(D_METHOD("get_bind_name", "bind_index"), &Skin::get_bind_name);
  138. ClassDB::bind_method(D_METHOD("set_bind_bone", "bind_index", "bone"), &Skin::set_bind_bone);
  139. ClassDB::bind_method(D_METHOD("get_bind_bone", "bind_index"), &Skin::get_bind_bone);
  140. ClassDB::bind_method(D_METHOD("clear_binds"), &Skin::clear_binds);
  141. }
  142. Skin::Skin() {
  143. }