bone_attachment_3d.cpp 13 KB

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