bone_attachment_3d.cpp 13 KB

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