bone_attachment_3d.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*************************************************************************/
  2. /* bone_attachment_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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("override_pose")) {
  60. set_override_pose(p_value);
  61. } else if (p_path == SNAME("override_mode")) {
  62. set_override_mode(p_value);
  63. } else if (p_path == SNAME("use_external_skeleton")) {
  64. set_use_external_skeleton(p_value);
  65. } else if (p_path == SNAME("external_skeleton")) {
  66. set_external_skeleton(p_value);
  67. }
  68. return true;
  69. }
  70. bool BoneAttachment3D::_get(const StringName &p_path, Variant &r_ret) const {
  71. if (p_path == SNAME("override_pose")) {
  72. r_ret = get_override_pose();
  73. } else if (p_path == SNAME("override_mode")) {
  74. r_ret = get_override_mode();
  75. } else if (p_path == SNAME("use_external_skeleton")) {
  76. r_ret = get_use_external_skeleton();
  77. } else if (p_path == SNAME("external_skeleton")) {
  78. r_ret = get_external_skeleton();
  79. }
  80. return true;
  81. }
  82. void BoneAttachment3D::_get_property_list(List<PropertyInfo> *p_list) const {
  83. p_list->push_back(PropertyInfo(Variant::BOOL, "override_pose", PROPERTY_HINT_NONE, ""));
  84. if (override_pose) {
  85. p_list->push_back(PropertyInfo(Variant::INT, "override_mode", PROPERTY_HINT_ENUM, "Global Pose Override, Local Pose Override, Custom Pose"));
  86. }
  87. p_list->push_back(PropertyInfo(Variant::BOOL, "use_external_skeleton", PROPERTY_HINT_NONE, ""));
  88. if (use_external_skeleton) {
  89. p_list->push_back(PropertyInfo(Variant::NODE_PATH, "external_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"));
  90. }
  91. }
  92. TypedArray<String> BoneAttachment3D::get_configuration_warnings() const {
  93. TypedArray<String> warnings = Node3D::get_configuration_warnings();
  94. if (use_external_skeleton) {
  95. if (external_skeleton_node_cache.is_null()) {
  96. warnings.push_back(RTR("External Skeleton3D node not set! Please set a path to an external Skeleton3D node."));
  97. }
  98. } else {
  99. Skeleton3D *parent = Object::cast_to<Skeleton3D>(get_parent());
  100. if (!parent) {
  101. 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."));
  102. }
  103. }
  104. if (bone_idx == -1) {
  105. warnings.push_back(RTR("BoneAttachment3D node is not bound to any bones! Please select a bone to attach this node."));
  106. }
  107. return warnings;
  108. }
  109. void BoneAttachment3D::_update_external_skeleton_cache() {
  110. external_skeleton_node_cache = ObjectID();
  111. if (has_node(external_skeleton_node)) {
  112. Node *node = get_node(external_skeleton_node);
  113. ERR_FAIL_COND_MSG(!node, "Cannot update external skeleton cache: Node cannot be found!");
  114. // Make sure it's a skeleton3D
  115. Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
  116. ERR_FAIL_COND_MSG(!sk, "Cannot update external skeleton cache: Skeleton3D Nodepath does not point to a Skeleton3D node!");
  117. external_skeleton_node_cache = node->get_instance_id();
  118. } else {
  119. if (external_skeleton_node.is_empty()) {
  120. BoneAttachment3D *parent_attachment = Object::cast_to<BoneAttachment3D>(get_parent());
  121. if (parent_attachment) {
  122. parent_attachment->_update_external_skeleton_cache();
  123. if (parent_attachment->has_node(parent_attachment->external_skeleton_node)) {
  124. Node *node = parent_attachment->get_node(parent_attachment->external_skeleton_node);
  125. ERR_FAIL_COND_MSG(!node, "Cannot update external skeleton cache: Parent's Skeleton3D node cannot be found!");
  126. // Make sure it's a skeleton3D
  127. Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
  128. ERR_FAIL_COND_MSG(!sk, "Cannot update external skeleton cache: Parent Skeleton3D Nodepath does not point to a Skeleton3D node!");
  129. external_skeleton_node_cache = node->get_instance_id();
  130. external_skeleton_node = get_path_to(node);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. void BoneAttachment3D::_check_bind() {
  137. Skeleton3D *sk = _get_skeleton3d();
  138. if (sk && !bound) {
  139. if (bone_idx <= -1) {
  140. bone_idx = sk->find_bone(bone_name);
  141. }
  142. if (bone_idx != -1) {
  143. sk->call_deferred(SNAME("connect"), "bone_pose_changed", callable_mp(this, &BoneAttachment3D::on_bone_pose_update));
  144. bound = true;
  145. call_deferred(SNAME("on_bone_pose_update"), bone_idx);
  146. }
  147. }
  148. }
  149. Skeleton3D *BoneAttachment3D::_get_skeleton3d() {
  150. if (use_external_skeleton) {
  151. if (external_skeleton_node_cache.is_valid()) {
  152. return Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache));
  153. } else {
  154. _update_external_skeleton_cache();
  155. if (external_skeleton_node_cache.is_valid()) {
  156. return Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache));
  157. }
  158. }
  159. } else {
  160. return Object::cast_to<Skeleton3D>(get_parent());
  161. }
  162. return nullptr;
  163. }
  164. void BoneAttachment3D::_check_unbind() {
  165. if (bound) {
  166. Skeleton3D *sk = _get_skeleton3d();
  167. if (sk) {
  168. sk->disconnect(SNAME("bone_pose_changed"), callable_mp(this, &BoneAttachment3D::on_bone_pose_update));
  169. }
  170. bound = false;
  171. }
  172. }
  173. void BoneAttachment3D::_transform_changed() {
  174. if (!is_inside_tree()) {
  175. return;
  176. }
  177. if (override_pose) {
  178. Skeleton3D *sk = _get_skeleton3d();
  179. ERR_FAIL_COND_MSG(!sk, "Cannot override pose: Skeleton not found!");
  180. ERR_FAIL_INDEX_MSG(bone_idx, sk->get_bone_count(), "Cannot override pose: Bone index is out of range!");
  181. Transform3D our_trans = get_transform();
  182. if (use_external_skeleton) {
  183. our_trans = sk->world_transform_to_global_pose(get_global_transform());
  184. }
  185. if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) {
  186. sk->set_bone_global_pose_override(bone_idx, our_trans, 1.0, true);
  187. } else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) {
  188. sk->set_bone_local_pose_override(bone_idx, sk->global_pose_to_local_pose(bone_idx, our_trans), 1.0, true);
  189. }
  190. }
  191. }
  192. void BoneAttachment3D::set_bone_name(const String &p_name) {
  193. bone_name = p_name;
  194. Skeleton3D *sk = _get_skeleton3d();
  195. if (sk) {
  196. set_bone_idx(sk->find_bone(bone_name));
  197. }
  198. }
  199. String BoneAttachment3D::get_bone_name() const {
  200. return bone_name;
  201. }
  202. void BoneAttachment3D::set_bone_idx(const int &p_idx) {
  203. if (is_inside_tree()) {
  204. _check_unbind();
  205. }
  206. bone_idx = p_idx;
  207. Skeleton3D *sk = _get_skeleton3d();
  208. if (sk) {
  209. if (bone_idx <= -1 || bone_idx >= sk->get_bone_count()) {
  210. WARN_PRINT("Bone index out of range! Cannot connect BoneAttachment to node!");
  211. bone_idx = -1;
  212. } else {
  213. bone_name = sk->get_bone_name(bone_idx);
  214. }
  215. }
  216. if (is_inside_tree()) {
  217. _check_bind();
  218. }
  219. notify_property_list_changed();
  220. }
  221. int BoneAttachment3D::get_bone_idx() const {
  222. return bone_idx;
  223. }
  224. void BoneAttachment3D::set_override_pose(bool p_override) {
  225. override_pose = p_override;
  226. set_notify_local_transform(override_pose);
  227. set_process_internal(override_pose);
  228. if (!override_pose) {
  229. Skeleton3D *sk = _get_skeleton3d();
  230. if (sk) {
  231. if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) {
  232. sk->set_bone_global_pose_override(bone_idx, Transform3D(), 0.0, false);
  233. } else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) {
  234. sk->set_bone_local_pose_override(bone_idx, Transform3D(), 0.0, false);
  235. }
  236. }
  237. _transform_changed();
  238. }
  239. notify_property_list_changed();
  240. }
  241. bool BoneAttachment3D::get_override_pose() const {
  242. return override_pose;
  243. }
  244. void BoneAttachment3D::set_override_mode(int p_mode) {
  245. if (override_pose) {
  246. Skeleton3D *sk = _get_skeleton3d();
  247. if (sk) {
  248. if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) {
  249. sk->set_bone_global_pose_override(bone_idx, Transform3D(), 0.0, false);
  250. } else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) {
  251. sk->set_bone_local_pose_override(bone_idx, Transform3D(), 0.0, false);
  252. }
  253. }
  254. override_mode = p_mode;
  255. _transform_changed();
  256. return;
  257. }
  258. override_mode = p_mode;
  259. }
  260. int BoneAttachment3D::get_override_mode() const {
  261. return override_mode;
  262. }
  263. void BoneAttachment3D::set_use_external_skeleton(bool p_use_external) {
  264. use_external_skeleton = p_use_external;
  265. if (use_external_skeleton) {
  266. _check_unbind();
  267. _update_external_skeleton_cache();
  268. _check_bind();
  269. _transform_changed();
  270. }
  271. notify_property_list_changed();
  272. }
  273. bool BoneAttachment3D::get_use_external_skeleton() const {
  274. return use_external_skeleton;
  275. }
  276. void BoneAttachment3D::set_external_skeleton(NodePath p_path) {
  277. external_skeleton_node = p_path;
  278. _update_external_skeleton_cache();
  279. notify_property_list_changed();
  280. }
  281. NodePath BoneAttachment3D::get_external_skeleton() const {
  282. return external_skeleton_node;
  283. }
  284. void BoneAttachment3D::_notification(int p_what) {
  285. switch (p_what) {
  286. case NOTIFICATION_ENTER_TREE: {
  287. if (use_external_skeleton) {
  288. _update_external_skeleton_cache();
  289. }
  290. _check_bind();
  291. } break;
  292. case NOTIFICATION_EXIT_TREE: {
  293. _check_unbind();
  294. } break;
  295. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  296. _transform_changed();
  297. } break;
  298. case NOTIFICATION_INTERNAL_PROCESS: {
  299. if (_override_dirty) {
  300. _override_dirty = false;
  301. }
  302. } break;
  303. }
  304. }
  305. void BoneAttachment3D::on_bone_pose_update(int p_bone_index) {
  306. if (bone_idx == p_bone_index) {
  307. Skeleton3D *sk = _get_skeleton3d();
  308. if (sk) {
  309. if (!override_pose) {
  310. if (use_external_skeleton) {
  311. set_global_transform(sk->global_pose_to_world_transform(sk->get_bone_global_pose(bone_idx)));
  312. } else {
  313. set_transform(sk->get_bone_global_pose(bone_idx));
  314. }
  315. } else {
  316. if (!_override_dirty) {
  317. _transform_changed();
  318. _override_dirty = true;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. #ifdef TOOLS_ENABLED
  325. void BoneAttachment3D::_notify_skeleton_bones_renamed(Node *p_base_scene, Skeleton3D *p_skeleton, Ref<BoneMap> p_bone_map) {
  326. const Skeleton3D *parent = nullptr;
  327. if (use_external_skeleton) {
  328. if (external_skeleton_node_cache.is_valid()) {
  329. parent = Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache));
  330. }
  331. } else {
  332. parent = Object::cast_to<Skeleton3D>(get_parent());
  333. }
  334. if (parent && parent == p_skeleton) {
  335. StringName bn = p_bone_map->find_profile_bone_name(bone_name);
  336. if (bn) {
  337. set_bone_name(bn);
  338. }
  339. }
  340. }
  341. #endif // TOOLS_ENABLED
  342. BoneAttachment3D::BoneAttachment3D() {
  343. }
  344. void BoneAttachment3D::_bind_methods() {
  345. ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment3D::set_bone_name);
  346. ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment3D::get_bone_name);
  347. ClassDB::bind_method(D_METHOD("set_bone_idx", "bone_idx"), &BoneAttachment3D::set_bone_idx);
  348. ClassDB::bind_method(D_METHOD("get_bone_idx"), &BoneAttachment3D::get_bone_idx);
  349. ClassDB::bind_method(D_METHOD("on_bone_pose_update", "bone_index"), &BoneAttachment3D::on_bone_pose_update);
  350. ClassDB::bind_method(D_METHOD("set_override_pose", "override_pose"), &BoneAttachment3D::set_override_pose);
  351. ClassDB::bind_method(D_METHOD("get_override_pose"), &BoneAttachment3D::get_override_pose);
  352. ClassDB::bind_method(D_METHOD("set_override_mode", "override_mode"), &BoneAttachment3D::set_override_mode);
  353. ClassDB::bind_method(D_METHOD("get_override_mode"), &BoneAttachment3D::get_override_mode);
  354. ClassDB::bind_method(D_METHOD("set_use_external_skeleton", "use_external_skeleton"), &BoneAttachment3D::set_use_external_skeleton);
  355. ClassDB::bind_method(D_METHOD("get_use_external_skeleton"), &BoneAttachment3D::get_use_external_skeleton);
  356. ClassDB::bind_method(D_METHOD("set_external_skeleton", "external_skeleton"), &BoneAttachment3D::set_external_skeleton);
  357. ClassDB::bind_method(D_METHOD("get_external_skeleton"), &BoneAttachment3D::get_external_skeleton);
  358. #ifdef TOOLS_ENABLED
  359. ClassDB::bind_method(D_METHOD("_notify_skeleton_bones_renamed"), &BoneAttachment3D::_notify_skeleton_bones_renamed);
  360. #endif // TOOLS_ENABLED
  361. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
  362. ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_idx"), "set_bone_idx", "get_bone_idx");
  363. }