skeleton_2d.cpp 26 KB

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