skeleton_2d.cpp 26 KB

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