skin.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 Transform &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 Transform &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. _change_notify();
  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 Transform &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. bool Skin::_set(const StringName &p_name, const Variant &p_value) {
  76. String name = p_name;
  77. if (name == "bind_count") {
  78. set_bind_count(p_value);
  79. return true;
  80. } else if (name.begins_with("bind/")) {
  81. int index = name.get_slicec('/', 1).to_int();
  82. String what = name.get_slicec('/', 2);
  83. if (what == "bone") {
  84. set_bind_bone(index, p_value);
  85. return true;
  86. } else if (what == "name") {
  87. set_bind_name(index, p_value);
  88. return true;
  89. } else if (what == "pose") {
  90. set_bind_pose(index, p_value);
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. bool Skin::_get(const StringName &p_name, Variant &r_ret) const {
  97. String name = p_name;
  98. if (name == "bind_count") {
  99. r_ret = get_bind_count();
  100. return true;
  101. } else if (name.begins_with("bind/")) {
  102. int index = name.get_slicec('/', 1).to_int();
  103. String what = name.get_slicec('/', 2);
  104. if (what == "bone") {
  105. r_ret = get_bind_bone(index);
  106. return true;
  107. } else if (what == "name") {
  108. r_ret = get_bind_name(index);
  109. return true;
  110. } else if (what == "pose") {
  111. r_ret = get_bind_pose(index);
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. void Skin::_get_property_list(List<PropertyInfo> *p_list) const {
  118. p_list->push_back(PropertyInfo(Variant::INT, PNAME("bind_count"), PROPERTY_HINT_RANGE, "0,16384,1,or_greater"));
  119. for (int i = 0; i < get_bind_count(); i++) {
  120. const String prefix = vformat("%s/%d/", PNAME("bind"), i);
  121. p_list->push_back(PropertyInfo(Variant::STRING, prefix + PNAME("name")));
  122. 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_NOEDITOR : PROPERTY_USAGE_DEFAULT));
  123. p_list->push_back(PropertyInfo(Variant::TRANSFORM, prefix + PNAME("pose")));
  124. }
  125. }
  126. void Skin::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_bind_count", "bind_count"), &Skin::set_bind_count);
  128. ClassDB::bind_method(D_METHOD("get_bind_count"), &Skin::get_bind_count);
  129. ClassDB::bind_method(D_METHOD("add_bind", "bone", "pose"), &Skin::add_bind);
  130. ClassDB::bind_method(D_METHOD("set_bind_pose", "bind_index", "pose"), &Skin::set_bind_pose);
  131. ClassDB::bind_method(D_METHOD("get_bind_pose", "bind_index"), &Skin::get_bind_pose);
  132. ClassDB::bind_method(D_METHOD("set_bind_name", "bind_index", "name"), &Skin::set_bind_name);
  133. ClassDB::bind_method(D_METHOD("get_bind_name", "bind_index"), &Skin::get_bind_name);
  134. ClassDB::bind_method(D_METHOD("set_bind_bone", "bind_index", "bone"), &Skin::set_bind_bone);
  135. ClassDB::bind_method(D_METHOD("get_bind_bone", "bind_index"), &Skin::get_bind_bone);
  136. ClassDB::bind_method(D_METHOD("clear_binds"), &Skin::clear_binds);
  137. }
  138. Skin::Skin() {
  139. bind_count = 0;
  140. binds_ptr = nullptr;
  141. }