skeleton_2d.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /**************************************************************************/
  2. /* skeleton_2d.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 "skeleton_2d.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "editor/editor_data.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/plugins/canvas_item_editor_plugin.h"
  35. #endif //TOOLS_ENABLED
  36. bool Bone2D::_set(const StringName &p_path, const Variant &p_value) {
  37. String path = p_path;
  38. if (path.begins_with("auto_calculate_length_and_angle")) {
  39. set_autocalculate_length_and_angle(p_value);
  40. } else if (path.begins_with("length")) {
  41. set_length(p_value);
  42. } else if (path.begins_with("bone_angle")) {
  43. set_bone_angle(Math::deg_to_rad(real_t(p_value)));
  44. } else if (path.begins_with("default_length")) {
  45. set_length(p_value);
  46. }
  47. #ifdef TOOLS_ENABLED
  48. if (path.begins_with("editor_settings/show_bone_gizmo")) {
  49. _editor_set_show_bone_gizmo(p_value);
  50. }
  51. #endif // TOOLS_ENABLED
  52. return true;
  53. }
  54. bool Bone2D::_get(const StringName &p_path, Variant &r_ret) const {
  55. String path = p_path;
  56. if (path.begins_with("auto_calculate_length_and_angle")) {
  57. r_ret = get_autocalculate_length_and_angle();
  58. } else if (path.begins_with("length")) {
  59. r_ret = get_length();
  60. } else if (path.begins_with("bone_angle")) {
  61. r_ret = Math::rad_to_deg(get_bone_angle());
  62. } else if (path.begins_with("default_length")) {
  63. r_ret = get_length();
  64. }
  65. #ifdef TOOLS_ENABLED
  66. if (path.begins_with("editor_settings/show_bone_gizmo")) {
  67. r_ret = _editor_get_show_bone_gizmo();
  68. }
  69. #endif // TOOLS_ENABLED
  70. return true;
  71. }
  72. void Bone2D::_get_property_list(List<PropertyInfo> *p_list) const {
  73. p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("auto_calculate_length_and_angle"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  74. if (!autocalculate_length_and_angle) {
  75. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("length"), PROPERTY_HINT_RANGE, "1, 1024, 1", PROPERTY_USAGE_DEFAULT));
  76. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("bone_angle"), PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
  77. }
  78. #ifdef TOOLS_ENABLED
  79. p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("editor_settings/show_bone_gizmo"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  80. #endif // TOOLS_ENABLED
  81. }
  82. void Bone2D::_notification(int p_what) {
  83. switch (p_what) {
  84. case NOTIFICATION_ENTER_TREE: {
  85. Node *parent = get_parent();
  86. parent_bone = Object::cast_to<Bone2D>(parent);
  87. skeleton = nullptr;
  88. while (parent) {
  89. skeleton = Object::cast_to<Skeleton2D>(parent);
  90. if (skeleton) {
  91. break;
  92. }
  93. if (!Object::cast_to<Bone2D>(parent)) {
  94. break; //skeletons must be chained to Bone2Ds.
  95. }
  96. parent = parent->get_parent();
  97. }
  98. if (skeleton) {
  99. Skeleton2D::Bone bone;
  100. bone.bone = this;
  101. skeleton->bones.push_back(bone);
  102. skeleton->_make_bone_setup_dirty();
  103. get_parent()->connect(SNAME("child_order_changed"), callable_mp(skeleton, &Skeleton2D::_make_bone_setup_dirty), CONNECT_REFERENCE_COUNTED);
  104. }
  105. cache_transform = get_transform();
  106. copy_transform_to_cache = true;
  107. #ifdef TOOLS_ENABLED
  108. // Only draw the gizmo in the editor!
  109. if (Engine::get_singleton()->is_editor_hint() == false) {
  110. return;
  111. }
  112. queue_redraw();
  113. #endif // TOOLS_ENABLED
  114. } break;
  115. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  116. if (skeleton) {
  117. skeleton->_make_transform_dirty();
  118. }
  119. if (copy_transform_to_cache) {
  120. cache_transform = get_transform();
  121. }
  122. #ifdef TOOLS_ENABLED
  123. // Only draw the gizmo in the editor!
  124. if (Engine::get_singleton()->is_editor_hint() == false) {
  125. return;
  126. }
  127. queue_redraw();
  128. if (get_parent()) {
  129. Bone2D *p_bone = Object::cast_to<Bone2D>(get_parent());
  130. if (p_bone) {
  131. p_bone->queue_redraw();
  132. }
  133. }
  134. #endif // TOOLS_ENABLED
  135. } break;
  136. case NOTIFICATION_EXIT_TREE: {
  137. if (skeleton) {
  138. for (int i = 0; i < skeleton->bones.size(); i++) {
  139. if (skeleton->bones[i].bone == this) {
  140. skeleton->bones.remove_at(i);
  141. break;
  142. }
  143. }
  144. skeleton->_make_bone_setup_dirty();
  145. get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(skeleton, &Skeleton2D::_make_bone_setup_dirty));
  146. }
  147. parent_bone = nullptr;
  148. set_transform(cache_transform);
  149. } break;
  150. case NOTIFICATION_READY: {
  151. if (autocalculate_length_and_angle) {
  152. calculate_length_and_rotation();
  153. }
  154. } break;
  155. #ifdef TOOLS_ENABLED
  156. case NOTIFICATION_EDITOR_PRE_SAVE:
  157. case NOTIFICATION_EDITOR_POST_SAVE: {
  158. Transform2D tmp_trans = get_transform();
  159. set_transform(cache_transform);
  160. cache_transform = tmp_trans;
  161. } break;
  162. // Bone2D Editor gizmo drawing.
  163. // TODO: Bone2D gizmo drawing needs to be moved to an editor plugin.
  164. case NOTIFICATION_DRAW: {
  165. // Only draw the gizmo in the editor!
  166. if (Engine::get_singleton()->is_editor_hint() == false) {
  167. return;
  168. }
  169. if (editor_gizmo_rid.is_null()) {
  170. editor_gizmo_rid = RenderingServer::get_singleton()->canvas_item_create();
  171. RenderingServer::get_singleton()->canvas_item_set_parent(editor_gizmo_rid, get_canvas_item());
  172. RenderingServer::get_singleton()->canvas_item_set_z_as_relative_to_parent(editor_gizmo_rid, true);
  173. RenderingServer::get_singleton()->canvas_item_set_z_index(editor_gizmo_rid, 10);
  174. }
  175. RenderingServer::get_singleton()->canvas_item_clear(editor_gizmo_rid);
  176. if (!_editor_show_bone_gizmo) {
  177. return;
  178. }
  179. // Undo scaling
  180. Transform2D editor_gizmo_trans;
  181. editor_gizmo_trans.set_scale(Vector2(1, 1) / get_global_scale());
  182. RenderingServer::get_singleton()->canvas_item_set_transform(editor_gizmo_rid, editor_gizmo_trans);
  183. Color bone_color1 = EDITOR_GET("editors/2d/bone_color1");
  184. Color bone_color2 = EDITOR_GET("editors/2d/bone_color2");
  185. Color bone_ik_color = EDITOR_GET("editors/2d/bone_ik_color");
  186. Color bone_outline_color = EDITOR_GET("editors/2d/bone_outline_color");
  187. Color bone_selected_color = EDITOR_GET("editors/2d/bone_selected_color");
  188. bool Bone2D_found = false;
  189. for (int i = 0; i < get_child_count(); i++) {
  190. Bone2D *child_node = nullptr;
  191. child_node = Object::cast_to<Bone2D>(get_child(i));
  192. if (!child_node) {
  193. continue;
  194. }
  195. Bone2D_found = true;
  196. Vector<Vector2> bone_shape;
  197. Vector<Vector2> bone_shape_outline;
  198. _editor_get_bone_shape(&bone_shape, &bone_shape_outline, child_node);
  199. Vector<Color> colors;
  200. if (has_meta("_local_pose_override_enabled_")) {
  201. colors.push_back(bone_ik_color);
  202. colors.push_back(bone_ik_color);
  203. colors.push_back(bone_ik_color);
  204. colors.push_back(bone_ik_color);
  205. } else {
  206. colors.push_back(bone_color1);
  207. colors.push_back(bone_color2);
  208. colors.push_back(bone_color1);
  209. colors.push_back(bone_color2);
  210. }
  211. Vector<Color> outline_colors;
  212. if (CanvasItemEditor::get_singleton()->editor_selection->is_selected(this)) {
  213. outline_colors.push_back(bone_selected_color);
  214. outline_colors.push_back(bone_selected_color);
  215. outline_colors.push_back(bone_selected_color);
  216. outline_colors.push_back(bone_selected_color);
  217. outline_colors.push_back(bone_selected_color);
  218. outline_colors.push_back(bone_selected_color);
  219. } else {
  220. outline_colors.push_back(bone_outline_color);
  221. outline_colors.push_back(bone_outline_color);
  222. outline_colors.push_back(bone_outline_color);
  223. outline_colors.push_back(bone_outline_color);
  224. outline_colors.push_back(bone_outline_color);
  225. outline_colors.push_back(bone_outline_color);
  226. }
  227. RenderingServer::get_singleton()->canvas_item_add_polygon(editor_gizmo_rid, bone_shape_outline, outline_colors);
  228. RenderingServer::get_singleton()->canvas_item_add_polygon(editor_gizmo_rid, bone_shape, colors);
  229. }
  230. if (!Bone2D_found) {
  231. Vector<Vector2> bone_shape;
  232. Vector<Vector2> bone_shape_outline;
  233. _editor_get_bone_shape(&bone_shape, &bone_shape_outline, nullptr);
  234. Vector<Color> colors;
  235. if (has_meta("_local_pose_override_enabled_")) {
  236. colors.push_back(bone_ik_color);
  237. colors.push_back(bone_ik_color);
  238. colors.push_back(bone_ik_color);
  239. colors.push_back(bone_ik_color);
  240. } else {
  241. colors.push_back(bone_color1);
  242. colors.push_back(bone_color2);
  243. colors.push_back(bone_color1);
  244. colors.push_back(bone_color2);
  245. }
  246. Vector<Color> outline_colors;
  247. if (CanvasItemEditor::get_singleton()->editor_selection->is_selected(this)) {
  248. outline_colors.push_back(bone_selected_color);
  249. outline_colors.push_back(bone_selected_color);
  250. outline_colors.push_back(bone_selected_color);
  251. outline_colors.push_back(bone_selected_color);
  252. outline_colors.push_back(bone_selected_color);
  253. outline_colors.push_back(bone_selected_color);
  254. } else {
  255. outline_colors.push_back(bone_outline_color);
  256. outline_colors.push_back(bone_outline_color);
  257. outline_colors.push_back(bone_outline_color);
  258. outline_colors.push_back(bone_outline_color);
  259. outline_colors.push_back(bone_outline_color);
  260. outline_colors.push_back(bone_outline_color);
  261. }
  262. RenderingServer::get_singleton()->canvas_item_add_polygon(editor_gizmo_rid, bone_shape_outline, outline_colors);
  263. RenderingServer::get_singleton()->canvas_item_add_polygon(editor_gizmo_rid, bone_shape, colors);
  264. }
  265. } break;
  266. #endif // TOOLS_ENABLED
  267. }
  268. }
  269. #ifdef TOOLS_ENABLED
  270. bool Bone2D::_editor_get_bone_shape(Vector<Vector2> *p_shape, Vector<Vector2> *p_outline_shape, Bone2D *p_other_bone) {
  271. int bone_width = EDITOR_GET("editors/2d/bone_width");
  272. int bone_outline_width = EDITOR_GET("editors/2d/bone_outline_size");
  273. if (!is_inside_tree()) {
  274. return false; //may have been removed
  275. }
  276. if (!p_other_bone && length <= 0) {
  277. return false;
  278. }
  279. Vector2 rel;
  280. if (p_other_bone) {
  281. rel = (p_other_bone->get_global_position() - get_global_position());
  282. rel = rel.rotated(-get_global_rotation()); // Undo Bone2D node's rotation so its drawn correctly regardless of the node's rotation
  283. } else {
  284. real_t angle_to_use = get_rotation() + bone_angle;
  285. rel = Vector2(cos(angle_to_use), sin(angle_to_use)) * (length * MIN(get_global_scale().x, get_global_scale().y));
  286. rel = rel.rotated(-get_rotation()); // Undo Bone2D node's rotation so its drawn correctly regardless of the node's rotation
  287. }
  288. Vector2 relt = rel.rotated(Math_PI * 0.5).normalized() * bone_width;
  289. Vector2 reln = rel.normalized();
  290. Vector2 reltn = relt.normalized();
  291. if (p_shape) {
  292. p_shape->clear();
  293. p_shape->push_back(Vector2(0, 0));
  294. p_shape->push_back(rel * 0.2 + relt);
  295. p_shape->push_back(rel);
  296. p_shape->push_back(rel * 0.2 - relt);
  297. }
  298. if (p_outline_shape) {
  299. p_outline_shape->clear();
  300. p_outline_shape->push_back((-reln - reltn) * bone_outline_width);
  301. p_outline_shape->push_back((-reln + reltn) * bone_outline_width);
  302. p_outline_shape->push_back(rel * 0.2 + relt + reltn * bone_outline_width);
  303. p_outline_shape->push_back(rel + (reln + reltn) * bone_outline_width);
  304. p_outline_shape->push_back(rel + (reln - reltn) * bone_outline_width);
  305. p_outline_shape->push_back(rel * 0.2 - relt - reltn * bone_outline_width);
  306. }
  307. return true;
  308. }
  309. void Bone2D::_editor_set_show_bone_gizmo(bool p_show_gizmo) {
  310. _editor_show_bone_gizmo = p_show_gizmo;
  311. queue_redraw();
  312. }
  313. bool Bone2D::_editor_get_show_bone_gizmo() const {
  314. return _editor_show_bone_gizmo;
  315. }
  316. #endif // TOOLS_ENABLED
  317. void Bone2D::_bind_methods() {
  318. ClassDB::bind_method(D_METHOD("set_rest", "rest"), &Bone2D::set_rest);
  319. ClassDB::bind_method(D_METHOD("get_rest"), &Bone2D::get_rest);
  320. ClassDB::bind_method(D_METHOD("apply_rest"), &Bone2D::apply_rest);
  321. ClassDB::bind_method(D_METHOD("get_skeleton_rest"), &Bone2D::get_skeleton_rest);
  322. ClassDB::bind_method(D_METHOD("get_index_in_skeleton"), &Bone2D::get_index_in_skeleton);
  323. ClassDB::bind_method(D_METHOD("set_autocalculate_length_and_angle", "auto_calculate"), &Bone2D::set_autocalculate_length_and_angle);
  324. ClassDB::bind_method(D_METHOD("get_autocalculate_length_and_angle"), &Bone2D::get_autocalculate_length_and_angle);
  325. ClassDB::bind_method(D_METHOD("set_length", "length"), &Bone2D::set_length);
  326. ClassDB::bind_method(D_METHOD("get_length"), &Bone2D::get_length);
  327. ClassDB::bind_method(D_METHOD("set_bone_angle", "angle"), &Bone2D::set_bone_angle);
  328. ClassDB::bind_method(D_METHOD("get_bone_angle"), &Bone2D::get_bone_angle);
  329. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "rest", PROPERTY_HINT_NONE, "suffix:px"), "set_rest", "get_rest");
  330. }
  331. void Bone2D::set_rest(const Transform2D &p_rest) {
  332. rest = p_rest;
  333. if (skeleton) {
  334. skeleton->_make_bone_setup_dirty();
  335. }
  336. update_configuration_warnings();
  337. }
  338. Transform2D Bone2D::get_rest() const {
  339. return rest;
  340. }
  341. Transform2D Bone2D::get_skeleton_rest() const {
  342. if (parent_bone) {
  343. return parent_bone->get_skeleton_rest() * rest;
  344. } else {
  345. return rest;
  346. }
  347. }
  348. void Bone2D::apply_rest() {
  349. set_transform(rest);
  350. }
  351. int Bone2D::get_index_in_skeleton() const {
  352. ERR_FAIL_COND_V(!skeleton, -1);
  353. skeleton->_update_bone_setup();
  354. return skeleton_index;
  355. }
  356. PackedStringArray Bone2D::get_configuration_warnings() const {
  357. PackedStringArray warnings = Node::get_configuration_warnings();
  358. if (!skeleton) {
  359. if (parent_bone) {
  360. warnings.push_back(RTR("This Bone2D chain should end at a Skeleton2D node."));
  361. } else {
  362. warnings.push_back(RTR("A Bone2D only works with a Skeleton2D or another Bone2D as parent node."));
  363. }
  364. }
  365. if (rest == Transform2D(0, 0, 0, 0, 0, 0)) {
  366. warnings.push_back(RTR("This bone lacks a proper REST pose. Go to the Skeleton2D node and set one."));
  367. }
  368. return warnings;
  369. }
  370. void Bone2D::calculate_length_and_rotation() {
  371. // if there is at least a single child Bone2D node, we can calculate
  372. // the length and direction. We will always just use the first Bone2D for this.
  373. bool calculated = false;
  374. int child_count = get_child_count();
  375. if (child_count > 0) {
  376. for (int i = 0; i < child_count; i++) {
  377. Bone2D *child = Object::cast_to<Bone2D>(get_child(i));
  378. if (child) {
  379. Vector2 child_local_pos = to_local(child->get_global_position());
  380. length = child_local_pos.length();
  381. bone_angle = child_local_pos.normalized().angle();
  382. calculated = true;
  383. break;
  384. }
  385. }
  386. }
  387. if (calculated) {
  388. return; // Finished!
  389. } else {
  390. WARN_PRINT("No Bone2D children of node " + get_name() + ". Cannot calculate bone length or angle reliably.\nUsing transform rotation for bone angle");
  391. bone_angle = get_transform().get_rotation();
  392. return;
  393. }
  394. }
  395. void Bone2D::set_autocalculate_length_and_angle(bool p_autocalculate) {
  396. autocalculate_length_and_angle = p_autocalculate;
  397. if (autocalculate_length_and_angle) {
  398. calculate_length_and_rotation();
  399. }
  400. notify_property_list_changed();
  401. }
  402. bool Bone2D::get_autocalculate_length_and_angle() const {
  403. return autocalculate_length_and_angle;
  404. }
  405. void Bone2D::set_length(real_t p_length) {
  406. length = p_length;
  407. #ifdef TOOLS_ENABLED
  408. queue_redraw();
  409. #endif // TOOLS_ENABLED
  410. }
  411. real_t Bone2D::get_length() const {
  412. return length;
  413. }
  414. void Bone2D::set_bone_angle(real_t p_angle) {
  415. bone_angle = p_angle;
  416. #ifdef TOOLS_ENABLED
  417. queue_redraw();
  418. #endif // TOOLS_ENABLED
  419. }
  420. real_t Bone2D::get_bone_angle() const {
  421. return bone_angle;
  422. }
  423. Bone2D::Bone2D() {
  424. skeleton = nullptr;
  425. parent_bone = nullptr;
  426. skeleton_index = -1;
  427. length = 16;
  428. bone_angle = 0;
  429. autocalculate_length_and_angle = true;
  430. set_notify_local_transform(true);
  431. set_hide_clip_children(true);
  432. //this is a clever hack so the bone knows no rest has been set yet, allowing to show an error.
  433. for (int i = 0; i < 3; i++) {
  434. rest[i] = Vector2(0, 0);
  435. }
  436. copy_transform_to_cache = true;
  437. }
  438. Bone2D::~Bone2D() {
  439. #ifdef TOOLS_ENABLED
  440. if (!editor_gizmo_rid.is_null()) {
  441. ERR_FAIL_NULL(RenderingServer::get_singleton());
  442. RenderingServer::get_singleton()->free(editor_gizmo_rid);
  443. }
  444. #endif // TOOLS_ENABLED
  445. }
  446. //////////////////////////////////////
  447. bool Skeleton2D::_set(const StringName &p_path, const Variant &p_value) {
  448. String path = p_path;
  449. if (path.begins_with("modification_stack")) {
  450. set_modification_stack(p_value);
  451. return true;
  452. }
  453. return true;
  454. }
  455. bool Skeleton2D::_get(const StringName &p_path, Variant &r_ret) const {
  456. String path = p_path;
  457. if (path.begins_with("modification_stack")) {
  458. r_ret = get_modification_stack();
  459. return true;
  460. }
  461. return true;
  462. }
  463. void Skeleton2D::_get_property_list(List<PropertyInfo> *p_list) const {
  464. p_list->push_back(
  465. PropertyInfo(Variant::OBJECT, PNAME("modification_stack"),
  466. PROPERTY_HINT_RESOURCE_TYPE,
  467. "SkeletonModificationStack2D",
  468. PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE | PROPERTY_USAGE_ALWAYS_DUPLICATE));
  469. }
  470. void Skeleton2D::_make_bone_setup_dirty() {
  471. if (bone_setup_dirty) {
  472. return;
  473. }
  474. bone_setup_dirty = true;
  475. if (is_inside_tree()) {
  476. call_deferred(SNAME("_update_bone_setup"));
  477. }
  478. }
  479. void Skeleton2D::_update_bone_setup() {
  480. if (!bone_setup_dirty) {
  481. return;
  482. }
  483. bone_setup_dirty = false;
  484. RS::get_singleton()->skeleton_allocate_data(skeleton, bones.size(), true);
  485. bones.sort(); //sorting so that they are always in the same order/index
  486. for (int i = 0; i < bones.size(); i++) {
  487. bones.write[i].rest_inverse = bones[i].bone->get_skeleton_rest().affine_inverse(); //bind pose
  488. bones.write[i].bone->skeleton_index = i;
  489. Bone2D *parent_bone = Object::cast_to<Bone2D>(bones[i].bone->get_parent());
  490. if (parent_bone) {
  491. bones.write[i].parent_index = parent_bone->skeleton_index;
  492. } else {
  493. bones.write[i].parent_index = -1;
  494. }
  495. bones.write[i].local_pose_override = bones[i].bone->get_skeleton_rest();
  496. }
  497. transform_dirty = true;
  498. _update_transform();
  499. emit_signal(SNAME("bone_setup_changed"));
  500. }
  501. void Skeleton2D::_make_transform_dirty() {
  502. if (transform_dirty) {
  503. return;
  504. }
  505. transform_dirty = true;
  506. if (is_inside_tree()) {
  507. call_deferred(SNAME("_update_transform"));
  508. }
  509. }
  510. void Skeleton2D::_update_transform() {
  511. if (bone_setup_dirty) {
  512. _update_bone_setup();
  513. return; //above will update transform anyway
  514. }
  515. if (!transform_dirty) {
  516. return;
  517. }
  518. transform_dirty = false;
  519. for (int i = 0; i < bones.size(); i++) {
  520. ERR_CONTINUE(bones[i].parent_index >= i);
  521. if (bones[i].parent_index >= 0) {
  522. bones.write[i].accum_transform = bones[bones[i].parent_index].accum_transform * bones[i].bone->get_transform();
  523. } else {
  524. bones.write[i].accum_transform = bones[i].bone->get_transform();
  525. }
  526. }
  527. for (int i = 0; i < bones.size(); i++) {
  528. Transform2D final_xform = bones[i].accum_transform * bones[i].rest_inverse;
  529. RS::get_singleton()->skeleton_bone_set_transform_2d(skeleton, i, final_xform);
  530. }
  531. }
  532. int Skeleton2D::get_bone_count() const {
  533. ERR_FAIL_COND_V(!is_inside_tree(), 0);
  534. if (bone_setup_dirty) {
  535. const_cast<Skeleton2D *>(this)->_update_bone_setup();
  536. }
  537. return bones.size();
  538. }
  539. Bone2D *Skeleton2D::get_bone(int p_idx) {
  540. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  541. ERR_FAIL_INDEX_V(p_idx, bones.size(), nullptr);
  542. return bones[p_idx].bone;
  543. }
  544. void Skeleton2D::_notification(int p_what) {
  545. if (p_what == NOTIFICATION_READY) {
  546. if (bone_setup_dirty) {
  547. _update_bone_setup();
  548. }
  549. if (transform_dirty) {
  550. _update_transform();
  551. }
  552. request_ready();
  553. }
  554. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  555. RS::get_singleton()->skeleton_set_base_transform_2d(skeleton, get_global_transform());
  556. } else if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  557. if (modification_stack.is_valid()) {
  558. execute_modifications(get_process_delta_time(), SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_process);
  559. }
  560. } else if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  561. if (modification_stack.is_valid()) {
  562. execute_modifications(get_physics_process_delta_time(), SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_physics_process);
  563. }
  564. }
  565. #ifdef TOOLS_ENABLED
  566. else if (p_what == NOTIFICATION_DRAW) {
  567. if (Engine::get_singleton()->is_editor_hint()) {
  568. if (modification_stack.is_valid()) {
  569. modification_stack->draw_editor_gizmos();
  570. }
  571. }
  572. }
  573. #endif // TOOLS_ENABLED
  574. }
  575. RID Skeleton2D::get_skeleton() const {
  576. return skeleton;
  577. }
  578. void Skeleton2D::set_bone_local_pose_override(int p_bone_idx, Transform2D p_override, real_t p_amount, bool p_persistent) {
  579. ERR_FAIL_INDEX_MSG(p_bone_idx, bones.size(), "Bone index is out of range!");
  580. bones.write[p_bone_idx].local_pose_override = p_override;
  581. bones.write[p_bone_idx].local_pose_override_amount = p_amount;
  582. bones.write[p_bone_idx].local_pose_override_persistent = p_persistent;
  583. }
  584. Transform2D Skeleton2D::get_bone_local_pose_override(int p_bone_idx) {
  585. ERR_FAIL_INDEX_V_MSG(p_bone_idx, bones.size(), Transform2D(), "Bone index is out of range!");
  586. return bones[p_bone_idx].local_pose_override;
  587. }
  588. void Skeleton2D::set_modification_stack(Ref<SkeletonModificationStack2D> p_stack) {
  589. if (modification_stack.is_valid()) {
  590. modification_stack->is_setup = false;
  591. modification_stack->set_skeleton(nullptr);
  592. set_process_internal(false);
  593. set_physics_process_internal(false);
  594. }
  595. modification_stack = p_stack;
  596. if (modification_stack.is_valid()) {
  597. modification_stack->set_skeleton(this);
  598. modification_stack->setup();
  599. set_process_internal(true);
  600. set_physics_process_internal(true);
  601. #ifdef TOOLS_ENABLED
  602. modification_stack->set_editor_gizmos_dirty(true);
  603. #endif // TOOLS_ENABLED
  604. }
  605. }
  606. Ref<SkeletonModificationStack2D> Skeleton2D::get_modification_stack() const {
  607. return modification_stack;
  608. }
  609. void Skeleton2D::execute_modifications(real_t p_delta, int p_execution_mode) {
  610. if (!modification_stack.is_valid()) {
  611. return;
  612. }
  613. // Do not cache the transform changes caused by the modifications!
  614. for (int i = 0; i < bones.size(); i++) {
  615. bones[i].bone->copy_transform_to_cache = false;
  616. }
  617. if (modification_stack->skeleton != this) {
  618. modification_stack->set_skeleton(this);
  619. }
  620. modification_stack->execute(p_delta, p_execution_mode);
  621. // Only apply the local pose override on _process. Otherwise, just calculate the local_pose_override and reset the transform.
  622. if (p_execution_mode == SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_process) {
  623. for (int i = 0; i < bones.size(); i++) {
  624. if (bones[i].local_pose_override_amount > 0) {
  625. bones[i].bone->set_meta("_local_pose_override_enabled_", true);
  626. Transform2D final_trans = bones[i].bone->cache_transform;
  627. final_trans = final_trans.interpolate_with(bones[i].local_pose_override, bones[i].local_pose_override_amount);
  628. bones[i].bone->set_transform(final_trans);
  629. bones[i].bone->propagate_call("force_update_transform");
  630. if (bones[i].local_pose_override_persistent) {
  631. bones.write[i].local_pose_override_amount = 0.0;
  632. }
  633. } else {
  634. // TODO: see if there is a way to undo the override without having to resort to setting every bone's transform.
  635. bones[i].bone->remove_meta("_local_pose_override_enabled_");
  636. bones[i].bone->set_transform(bones[i].bone->cache_transform);
  637. }
  638. }
  639. }
  640. // Cache any future transform changes
  641. for (int i = 0; i < bones.size(); i++) {
  642. bones[i].bone->copy_transform_to_cache = true;
  643. }
  644. #ifdef TOOLS_ENABLED
  645. modification_stack->set_editor_gizmos_dirty(true);
  646. #endif // TOOLS_ENABLED
  647. }
  648. void Skeleton2D::_bind_methods() {
  649. ClassDB::bind_method(D_METHOD("_update_bone_setup"), &Skeleton2D::_update_bone_setup);
  650. ClassDB::bind_method(D_METHOD("_update_transform"), &Skeleton2D::_update_transform);
  651. ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton2D::get_bone_count);
  652. ClassDB::bind_method(D_METHOD("get_bone", "idx"), &Skeleton2D::get_bone);
  653. ClassDB::bind_method(D_METHOD("get_skeleton"), &Skeleton2D::get_skeleton);
  654. ClassDB::bind_method(D_METHOD("set_modification_stack", "modification_stack"), &Skeleton2D::set_modification_stack);
  655. ClassDB::bind_method(D_METHOD("get_modification_stack"), &Skeleton2D::get_modification_stack);
  656. ClassDB::bind_method(D_METHOD("execute_modifications", "delta", "execution_mode"), &Skeleton2D::execute_modifications);
  657. ClassDB::bind_method(D_METHOD("set_bone_local_pose_override", "bone_idx", "override_pose", "strength", "persistent"), &Skeleton2D::set_bone_local_pose_override);
  658. ClassDB::bind_method(D_METHOD("get_bone_local_pose_override", "bone_idx"), &Skeleton2D::get_bone_local_pose_override);
  659. ADD_SIGNAL(MethodInfo("bone_setup_changed"));
  660. }
  661. Skeleton2D::Skeleton2D() {
  662. skeleton = RS::get_singleton()->skeleton_create();
  663. set_notify_transform(true);
  664. set_hide_clip_children(true);
  665. }
  666. Skeleton2D::~Skeleton2D() {
  667. ERR_FAIL_NULL(RenderingServer::get_singleton());
  668. RS::get_singleton()->free(skeleton);
  669. }