bone_attachment_3d.cpp 13 KB

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