bone_attachment_3d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**************************************************************************/
  2. /* bone_attachment_3d.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_attachment_3d.h"
  31. #include "bone_attachment_3d.compat.inc"
  32. void BoneAttachment3D::_validate_property(PropertyInfo &p_property) const {
  33. if (p_property.name == "bone_name") {
  34. // Because it is a constant function, we cannot use the get_skeleton function.
  35. const Skeleton3D *parent = nullptr;
  36. if (use_external_skeleton) {
  37. if (external_skeleton_node_cache.is_valid()) {
  38. parent = ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
  39. }
  40. } else {
  41. parent = Object::cast_to<Skeleton3D>(get_parent());
  42. }
  43. if (parent) {
  44. p_property.hint = PROPERTY_HINT_ENUM;
  45. p_property.hint_string = parent->get_concatenated_bone_names();
  46. } else {
  47. p_property.hint = PROPERTY_HINT_NONE;
  48. p_property.hint_string = "";
  49. }
  50. }
  51. if (p_property.name == "external_skeleton" && !use_external_skeleton) {
  52. p_property.usage = PROPERTY_USAGE_NONE;
  53. }
  54. }
  55. PackedStringArray BoneAttachment3D::get_configuration_warnings() const {
  56. PackedStringArray warnings = Node3D::get_configuration_warnings();
  57. if (use_external_skeleton) {
  58. if (external_skeleton_node_cache.is_null()) {
  59. warnings.push_back(RTR("External Skeleton3D node not set! Please set a path to an external Skeleton3D node."));
  60. }
  61. } else {
  62. Skeleton3D *parent = Object::cast_to<Skeleton3D>(get_parent());
  63. if (!parent) {
  64. warnings.push_back(RTR("Parent node is not a Skeleton3D node! Please use an external Skeleton3D if you intend to use the BoneAttachment3D without it being a child of a Skeleton3D node."));
  65. }
  66. }
  67. if (bone_idx == -1) {
  68. warnings.push_back(RTR("BoneAttachment3D node is not bound to any bones! Please select a bone to attach this node."));
  69. }
  70. return warnings;
  71. }
  72. void BoneAttachment3D::_update_external_skeleton_cache() {
  73. external_skeleton_node_cache = ObjectID();
  74. if (has_node(external_skeleton_node)) {
  75. Node *node = get_node(external_skeleton_node);
  76. ERR_FAIL_NULL_MSG(node, "Cannot update external skeleton cache: Node cannot be found!");
  77. // Make sure it's a Skeleton3D.
  78. Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
  79. ERR_FAIL_NULL_MSG(sk, "Cannot update external skeleton cache: Skeleton3D Nodepath does not point to a Skeleton3D node!");
  80. external_skeleton_node_cache = node->get_instance_id();
  81. } else {
  82. if (external_skeleton_node.is_empty()) {
  83. BoneAttachment3D *parent_attachment = Object::cast_to<BoneAttachment3D>(get_parent());
  84. if (parent_attachment) {
  85. parent_attachment->_update_external_skeleton_cache();
  86. if (parent_attachment->has_node(parent_attachment->external_skeleton_node)) {
  87. Node *node = parent_attachment->get_node(parent_attachment->external_skeleton_node);
  88. ERR_FAIL_NULL_MSG(node, "Cannot update external skeleton cache: Parent's Skeleton3D node cannot be found!");
  89. // Make sure it's a Skeleton3D.
  90. Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
  91. ERR_FAIL_NULL_MSG(sk, "Cannot update external skeleton cache: Parent Skeleton3D Nodepath does not point to a Skeleton3D node!");
  92. external_skeleton_node_cache = node->get_instance_id();
  93. external_skeleton_node = get_path_to(node);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. void BoneAttachment3D::_check_bind() {
  100. Skeleton3D *sk = get_skeleton();
  101. if (sk && !bound) {
  102. if (bone_idx <= -1) {
  103. bone_idx = sk->find_bone(bone_name);
  104. }
  105. if (bone_idx != -1) {
  106. sk->connect(SceneStringName(skeleton_updated), callable_mp(this, &BoneAttachment3D::on_skeleton_update));
  107. bound = true;
  108. on_skeleton_update();
  109. }
  110. }
  111. }
  112. Skeleton3D *BoneAttachment3D::get_skeleton() {
  113. if (use_external_skeleton) {
  114. if (external_skeleton_node_cache.is_valid()) {
  115. return ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
  116. } else {
  117. _update_external_skeleton_cache();
  118. if (external_skeleton_node_cache.is_valid()) {
  119. return ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
  120. }
  121. }
  122. } else {
  123. return Object::cast_to<Skeleton3D>(get_parent());
  124. }
  125. return nullptr;
  126. }
  127. void BoneAttachment3D::_check_unbind() {
  128. if (bound) {
  129. Skeleton3D *sk = get_skeleton();
  130. if (sk) {
  131. sk->disconnect(SceneStringName(skeleton_updated), callable_mp(this, &BoneAttachment3D::on_skeleton_update));
  132. }
  133. bound = false;
  134. }
  135. }
  136. void BoneAttachment3D::_transform_changed() {
  137. if (!is_inside_tree()) {
  138. return;
  139. }
  140. if (override_pose && !overriding) {
  141. Skeleton3D *sk = get_skeleton();
  142. ERR_FAIL_NULL_MSG(sk, "Cannot override pose: Skeleton not found!");
  143. ERR_FAIL_INDEX_MSG(bone_idx, sk->get_bone_count(), "Cannot override pose: Bone index is out of range!");
  144. Transform3D our_trans = get_transform();
  145. if (use_external_skeleton) {
  146. our_trans = sk->get_global_transform().affine_inverse() * get_global_transform();
  147. }
  148. overriding = true;
  149. sk->set_bone_global_pose(bone_idx, our_trans);
  150. sk->force_update_all_dirty_bones();
  151. }
  152. overriding = false;
  153. }
  154. void BoneAttachment3D::set_bone_name(const String &p_name) {
  155. bone_name = p_name;
  156. Skeleton3D *sk = get_skeleton();
  157. if (sk) {
  158. set_bone_idx(sk->find_bone(bone_name));
  159. }
  160. }
  161. String BoneAttachment3D::get_bone_name() const {
  162. return bone_name;
  163. }
  164. void BoneAttachment3D::set_bone_idx(const int &p_idx) {
  165. if (is_inside_tree()) {
  166. _check_unbind();
  167. }
  168. bone_idx = p_idx;
  169. Skeleton3D *sk = get_skeleton();
  170. if (sk) {
  171. if (bone_idx <= -1 || bone_idx >= sk->get_bone_count()) {
  172. WARN_PRINT("Bone index " + itos(bone_idx) + " out of range! Cannot connect BoneAttachment to node!");
  173. bone_idx = -1;
  174. } else {
  175. bone_name = sk->get_bone_name(bone_idx);
  176. }
  177. }
  178. if (is_inside_tree()) {
  179. _check_bind();
  180. } else {
  181. on_skeleton_update();
  182. }
  183. notify_property_list_changed();
  184. }
  185. int BoneAttachment3D::get_bone_idx() const {
  186. return bone_idx;
  187. }
  188. void BoneAttachment3D::set_override_pose(bool p_override_pose) {
  189. if (override_pose == p_override_pose) {
  190. return;
  191. }
  192. override_pose = p_override_pose;
  193. set_notify_transform(override_pose);
  194. set_process_internal(override_pose);
  195. if (!override_pose && bone_idx >= 0) {
  196. Skeleton3D *sk = get_skeleton();
  197. if (sk) {
  198. sk->reset_bone_pose(bone_idx);
  199. }
  200. }
  201. notify_property_list_changed();
  202. }
  203. bool BoneAttachment3D::get_override_pose() const {
  204. return override_pose;
  205. }
  206. void BoneAttachment3D::set_use_external_skeleton(bool p_use_external_skeleton) {
  207. use_external_skeleton = p_use_external_skeleton;
  208. if (use_external_skeleton) {
  209. _check_unbind();
  210. _update_external_skeleton_cache();
  211. _check_bind();
  212. _transform_changed();
  213. }
  214. notify_property_list_changed();
  215. }
  216. bool BoneAttachment3D::get_use_external_skeleton() const {
  217. return use_external_skeleton;
  218. }
  219. void BoneAttachment3D::set_external_skeleton(NodePath p_external_skeleton) {
  220. external_skeleton_node = p_external_skeleton;
  221. _update_external_skeleton_cache();
  222. notify_property_list_changed();
  223. }
  224. NodePath BoneAttachment3D::get_external_skeleton() const {
  225. return external_skeleton_node;
  226. }
  227. void BoneAttachment3D::_notification(int p_what) {
  228. switch (p_what) {
  229. case NOTIFICATION_ENTER_TREE: {
  230. if (use_external_skeleton) {
  231. _update_external_skeleton_cache();
  232. }
  233. _check_bind();
  234. } break;
  235. case NOTIFICATION_EXIT_TREE: {
  236. _check_unbind();
  237. } break;
  238. case NOTIFICATION_TRANSFORM_CHANGED: {
  239. _transform_changed();
  240. } break;
  241. case NOTIFICATION_INTERNAL_PROCESS: {
  242. if (_override_dirty) {
  243. _override_dirty = false;
  244. }
  245. } break;
  246. }
  247. }
  248. void BoneAttachment3D::on_skeleton_update() {
  249. if (updating) {
  250. return;
  251. }
  252. updating = true;
  253. if (bone_idx >= 0) {
  254. Skeleton3D *sk = get_skeleton();
  255. if (sk) {
  256. if (!override_pose) {
  257. if (use_external_skeleton) {
  258. set_global_transform(sk->get_global_transform() * sk->get_bone_global_pose(bone_idx));
  259. } else {
  260. set_transform(sk->get_bone_global_pose(bone_idx));
  261. }
  262. } else {
  263. if (!_override_dirty) {
  264. _transform_changed();
  265. _override_dirty = true;
  266. }
  267. }
  268. }
  269. }
  270. updating = false;
  271. }
  272. #ifdef TOOLS_ENABLED
  273. void BoneAttachment3D::notify_skeleton_bones_renamed(Node *p_base_scene, Skeleton3D *p_skeleton, Dictionary p_rename_map) {
  274. const Skeleton3D *parent = nullptr;
  275. if (use_external_skeleton) {
  276. if (external_skeleton_node_cache.is_valid()) {
  277. parent = ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
  278. }
  279. } else {
  280. parent = Object::cast_to<Skeleton3D>(get_parent());
  281. }
  282. if (parent && parent == p_skeleton) {
  283. StringName bn = p_rename_map[bone_name];
  284. if (bn) {
  285. set_bone_name(bn);
  286. }
  287. }
  288. }
  289. void BoneAttachment3D::notify_rebind_required() {
  290. // Ensures bindings are properly updated after a scene reload.
  291. _check_unbind();
  292. if (use_external_skeleton) {
  293. _update_external_skeleton_cache();
  294. }
  295. bone_idx = -1;
  296. _check_bind();
  297. }
  298. #endif // TOOLS_ENABLED
  299. BoneAttachment3D::BoneAttachment3D() {
  300. set_physics_interpolation_mode(PHYSICS_INTERPOLATION_MODE_OFF);
  301. }
  302. void BoneAttachment3D::_bind_methods() {
  303. ClassDB::bind_method(D_METHOD("get_skeleton"), &BoneAttachment3D::get_skeleton);
  304. ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment3D::set_bone_name);
  305. ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment3D::get_bone_name);
  306. ClassDB::bind_method(D_METHOD("set_bone_idx", "bone_idx"), &BoneAttachment3D::set_bone_idx);
  307. ClassDB::bind_method(D_METHOD("get_bone_idx"), &BoneAttachment3D::get_bone_idx);
  308. ClassDB::bind_method(D_METHOD("on_skeleton_update"), &BoneAttachment3D::on_skeleton_update);
  309. ClassDB::bind_method(D_METHOD("set_override_pose", "override_pose"), &BoneAttachment3D::set_override_pose);
  310. ClassDB::bind_method(D_METHOD("get_override_pose"), &BoneAttachment3D::get_override_pose);
  311. ClassDB::bind_method(D_METHOD("set_use_external_skeleton", "use_external_skeleton"), &BoneAttachment3D::set_use_external_skeleton);
  312. ClassDB::bind_method(D_METHOD("get_use_external_skeleton"), &BoneAttachment3D::get_use_external_skeleton);
  313. ClassDB::bind_method(D_METHOD("set_external_skeleton", "external_skeleton"), &BoneAttachment3D::set_external_skeleton);
  314. ClassDB::bind_method(D_METHOD("get_external_skeleton"), &BoneAttachment3D::get_external_skeleton);
  315. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
  316. ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_idx"), "set_bone_idx", "get_bone_idx");
  317. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_pose"), "set_override_pose", "get_override_pose");
  318. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_external_skeleton"), "set_use_external_skeleton", "get_use_external_skeleton");
  319. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "external_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_external_skeleton", "get_external_skeleton");
  320. }