animation_player_editor_plugin.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. /*************************************************************************/
  2. /* animation_player_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_player_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/io/resource_saver.h"
  35. #include "core/os/keyboard.h"
  36. #include "editor/editor_file_dialog.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/plugins/canvas_item_editor_plugin.h" // For onion skinning.
  41. #include "editor/plugins/node_3d_editor_plugin.h" // For onion skinning.
  42. #include "editor/scene_tree_dock.h"
  43. #include "scene/main/window.h"
  44. #include "scene/resources/animation.h"
  45. #include "scene/scene_string_names.h"
  46. #include "servers/rendering_server.h"
  47. ///////////////////////////////////
  48. void AnimationPlayerEditor::_node_removed(Node *p_node) {
  49. if (player && player == p_node) {
  50. player = nullptr;
  51. set_process(false);
  52. track_editor->set_animation(Ref<Animation>(), true);
  53. track_editor->set_root(nullptr);
  54. track_editor->show_select_node_warning(true);
  55. _update_player();
  56. }
  57. }
  58. void AnimationPlayerEditor::_notification(int p_what) {
  59. switch (p_what) {
  60. case NOTIFICATION_PROCESS: {
  61. if (!player) {
  62. return;
  63. }
  64. updating = true;
  65. if (player->is_playing()) {
  66. {
  67. String animname = player->get_assigned_animation();
  68. if (player->has_animation(animname)) {
  69. Ref<Animation> anim = player->get_animation(animname);
  70. if (!anim.is_null()) {
  71. frame->set_max((double)anim->get_length());
  72. }
  73. }
  74. }
  75. frame->set_value(player->get_current_animation_position());
  76. track_editor->set_anim_pos(player->get_current_animation_position());
  77. } else if (!player->is_valid()) {
  78. // Reset timeline when the player has been stopped externally
  79. frame->set_value(0);
  80. } else if (last_active) {
  81. // Need the last frame after it stopped.
  82. frame->set_value(player->get_current_animation_position());
  83. }
  84. last_active = player->is_playing();
  85. updating = false;
  86. } break;
  87. case NOTIFICATION_ENTER_TREE: {
  88. tool_anim->get_popup()->connect("id_pressed", callable_mp(this, &AnimationPlayerEditor::_animation_tool_menu));
  89. onion_skinning->get_popup()->connect("id_pressed", callable_mp(this, &AnimationPlayerEditor::_onion_skinning_menu));
  90. blend_editor.next->connect("item_selected", callable_mp(this, &AnimationPlayerEditor::_blend_editor_next_changed));
  91. get_tree()->connect("node_removed", callable_mp(this, &AnimationPlayerEditor::_node_removed));
  92. add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("panel"), SNAME("Panel")));
  93. } break;
  94. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  95. add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("panel"), SNAME("Panel")));
  96. } break;
  97. case NOTIFICATION_TRANSLATION_CHANGED:
  98. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  99. case NOTIFICATION_THEME_CHANGED: {
  100. autoplay->set_icon(get_theme_icon(SNAME("AutoPlay"), SNAME("EditorIcons")));
  101. play->set_icon(get_theme_icon(SNAME("PlayStart"), SNAME("EditorIcons")));
  102. play_from->set_icon(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")));
  103. play_bw->set_icon(get_theme_icon(SNAME("PlayStartBackwards"), SNAME("EditorIcons")));
  104. play_bw_from->set_icon(get_theme_icon(SNAME("PlayBackwards"), SNAME("EditorIcons")));
  105. autoplay_icon = get_theme_icon(SNAME("AutoPlay"), SNAME("EditorIcons"));
  106. reset_icon = get_theme_icon(SNAME("Reload"), SNAME("EditorIcons"));
  107. {
  108. Ref<Image> autoplay_img = autoplay_icon->get_image();
  109. Ref<Image> reset_img = reset_icon->get_image();
  110. Ref<Image> autoplay_reset_img;
  111. Size2 icon_size = autoplay_img->get_size();
  112. autoplay_reset_img.instantiate();
  113. autoplay_reset_img->create(icon_size.x * 2, icon_size.y, false, autoplay_img->get_format());
  114. autoplay_reset_img->blit_rect(autoplay_img, Rect2i(Point2i(), icon_size), Point2i());
  115. autoplay_reset_img->blit_rect(reset_img, Rect2i(Point2i(), icon_size), Point2i(icon_size.x, 0));
  116. autoplay_reset_icon.instantiate();
  117. autoplay_reset_icon->set_image(autoplay_reset_img);
  118. }
  119. stop->set_icon(get_theme_icon(SNAME("Stop"), SNAME("EditorIcons")));
  120. onion_toggle->set_icon(get_theme_icon(SNAME("Onion"), SNAME("EditorIcons")));
  121. onion_skinning->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  122. pin->set_icon(get_theme_icon(SNAME("Pin"), SNAME("EditorIcons")));
  123. tool_anim->add_theme_style_override("normal", get_theme_stylebox(SNAME("normal"), SNAME("Button")));
  124. track_editor->get_edit_menu()->add_theme_style_override("normal", get_theme_stylebox(SNAME("normal"), SNAME("Button")));
  125. #define ITEM_ICON(m_item, m_icon) tool_anim->get_popup()->set_item_icon(tool_anim->get_popup()->get_item_index(m_item), get_theme_icon(SNAME(m_icon), SNAME("EditorIcons")))
  126. ITEM_ICON(TOOL_NEW_ANIM, "New");
  127. ITEM_ICON(TOOL_ANIM_LIBRARY, "AnimationLibrary");
  128. ITEM_ICON(TOOL_DUPLICATE_ANIM, "Duplicate");
  129. ITEM_ICON(TOOL_RENAME_ANIM, "Rename");
  130. ITEM_ICON(TOOL_EDIT_TRANSITIONS, "Blend");
  131. ITEM_ICON(TOOL_EDIT_RESOURCE, "Edit");
  132. ITEM_ICON(TOOL_REMOVE_ANIM, "Remove");
  133. _update_animation_list_icons();
  134. } break;
  135. }
  136. }
  137. void AnimationPlayerEditor::_autoplay_pressed() {
  138. if (updating) {
  139. return;
  140. }
  141. if (animation->has_selectable_items() == 0) {
  142. return;
  143. }
  144. String current = animation->get_item_text(animation->get_selected());
  145. if (player->get_autoplay() == current) {
  146. //unset
  147. undo_redo->create_action(TTR("Toggle Autoplay"));
  148. undo_redo->add_do_method(player, "set_autoplay", "");
  149. undo_redo->add_undo_method(player, "set_autoplay", player->get_autoplay());
  150. undo_redo->add_do_method(this, "_animation_player_changed", player);
  151. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  152. undo_redo->commit_action();
  153. } else {
  154. //set
  155. undo_redo->create_action(TTR("Toggle Autoplay"));
  156. undo_redo->add_do_method(player, "set_autoplay", current);
  157. undo_redo->add_undo_method(player, "set_autoplay", player->get_autoplay());
  158. undo_redo->add_do_method(this, "_animation_player_changed", player);
  159. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  160. undo_redo->commit_action();
  161. }
  162. }
  163. void AnimationPlayerEditor::_play_pressed() {
  164. String current = _get_current();
  165. if (!current.is_empty()) {
  166. if (current == player->get_assigned_animation()) {
  167. player->stop(); //so it won't blend with itself
  168. }
  169. player->play(current);
  170. }
  171. //unstop
  172. stop->set_pressed(false);
  173. }
  174. void AnimationPlayerEditor::_play_from_pressed() {
  175. String current = _get_current();
  176. if (!current.is_empty()) {
  177. float time = player->get_current_animation_position();
  178. if (current == player->get_assigned_animation() && player->is_playing()) {
  179. player->stop(); //so it won't blend with itself
  180. }
  181. player->play(current);
  182. player->seek(time);
  183. }
  184. //unstop
  185. stop->set_pressed(false);
  186. }
  187. String AnimationPlayerEditor::_get_current() const {
  188. String current;
  189. if (animation->get_selected() >= 0 && animation->get_selected() < animation->get_item_count() && !animation->is_item_separator(animation->get_selected())) {
  190. current = animation->get_item_text(animation->get_selected());
  191. }
  192. return current;
  193. }
  194. void AnimationPlayerEditor::_play_bw_pressed() {
  195. String current = _get_current();
  196. if (!current.is_empty()) {
  197. if (current == player->get_assigned_animation()) {
  198. player->stop(); //so it won't blend with itself
  199. }
  200. player->play(current, -1, -1, true);
  201. }
  202. //unstop
  203. stop->set_pressed(false);
  204. }
  205. void AnimationPlayerEditor::_play_bw_from_pressed() {
  206. String current = _get_current();
  207. if (!current.is_empty()) {
  208. float time = player->get_current_animation_position();
  209. if (current == player->get_assigned_animation()) {
  210. player->stop(); //so it won't blend with itself
  211. }
  212. player->play(current, -1, -1, true);
  213. player->seek(time);
  214. }
  215. //unstop
  216. stop->set_pressed(false);
  217. }
  218. void AnimationPlayerEditor::_stop_pressed() {
  219. if (!player) {
  220. return;
  221. }
  222. player->stop(false);
  223. play->set_pressed(false);
  224. stop->set_pressed(true);
  225. }
  226. void AnimationPlayerEditor::_animation_selected(int p_which) {
  227. if (updating) {
  228. return;
  229. }
  230. // when selecting an animation, the idea is that the only interesting behavior
  231. // ui-wise is that it should play/blend the next one if currently playing
  232. String current = _get_current();
  233. if (!current.is_empty()) {
  234. player->set_assigned_animation(current);
  235. Ref<Animation> anim = player->get_animation(current);
  236. {
  237. bool animation_library_is_foreign = EditorNode::get_singleton()->is_resource_read_only(anim);
  238. track_editor->set_animation(anim, animation_library_is_foreign);
  239. Node *root = player->get_node(player->get_root());
  240. if (root) {
  241. track_editor->set_root(root);
  242. }
  243. }
  244. frame->set_max((double)anim->get_length());
  245. } else {
  246. track_editor->set_animation(Ref<Animation>(), true);
  247. track_editor->set_root(nullptr);
  248. }
  249. autoplay->set_pressed(current == player->get_autoplay());
  250. AnimationPlayerEditor::get_singleton()->get_track_editor()->update_keying();
  251. _animation_key_editor_seek(timeline_position, false);
  252. }
  253. void AnimationPlayerEditor::_animation_new() {
  254. int count = 1;
  255. String base = TTR("New Anim");
  256. String current_library_name = "";
  257. if (animation->has_selectable_items()) {
  258. String current_animation_name = animation->get_item_text(animation->get_selected());
  259. Ref<Animation> current_animation = player->get_animation(current_animation_name);
  260. if (current_animation.is_valid()) {
  261. current_library_name = player->find_animation_library(current_animation);
  262. }
  263. }
  264. String attempt_prefix = (current_library_name == "") ? "" : current_library_name + "/";
  265. while (true) {
  266. String attempt = base;
  267. if (count > 1) {
  268. attempt += " (" + itos(count) + ")";
  269. }
  270. if (player->has_animation(attempt_prefix + attempt)) {
  271. count++;
  272. continue;
  273. }
  274. base = attempt;
  275. break;
  276. }
  277. _update_name_dialog_library_dropdown();
  278. name_dialog_op = TOOL_NEW_ANIM;
  279. name_dialog->set_title(TTR("Create New Animation"));
  280. name_dialog->popup_centered(Size2(300, 90));
  281. name_title->set_text(TTR("New Animation Name:"));
  282. name->set_text(base);
  283. name->select_all();
  284. name->grab_focus();
  285. }
  286. void AnimationPlayerEditor::_animation_rename() {
  287. if (!animation->has_selectable_items()) {
  288. return;
  289. }
  290. int selected = animation->get_selected();
  291. String selected_name = animation->get_item_text(selected);
  292. // Remove library prefix if present.
  293. if (selected_name.contains("/")) {
  294. selected_name = selected_name.get_slice("/", 1);
  295. }
  296. name_dialog->set_title(TTR("Rename Animation"));
  297. name_title->set_text(TTR("Change Animation Name:"));
  298. name->set_text(selected_name);
  299. name_dialog_op = TOOL_RENAME_ANIM;
  300. name_dialog->popup_centered(Size2(300, 90));
  301. name->select_all();
  302. name->grab_focus();
  303. library->hide();
  304. }
  305. void AnimationPlayerEditor::_animation_remove() {
  306. if (!animation->has_selectable_items()) {
  307. return;
  308. }
  309. String current = animation->get_item_text(animation->get_selected());
  310. delete_dialog->set_text(vformat(TTR("Delete Animation '%s'?"), current));
  311. delete_dialog->popup_centered();
  312. }
  313. void AnimationPlayerEditor::_animation_remove_confirmed() {
  314. String current = animation->get_item_text(animation->get_selected());
  315. Ref<Animation> anim = player->get_animation(current);
  316. Ref<AnimationLibrary> al = player->get_animation_library(player->find_animation_library(anim));
  317. ERR_FAIL_COND(al.is_null());
  318. // For names of form lib_name/anim_name, remove library name prefix.
  319. if (current.contains("/")) {
  320. current = current.get_slice("/", 1);
  321. }
  322. undo_redo->create_action(TTR("Remove Animation"));
  323. if (player->get_autoplay() == current) {
  324. undo_redo->add_do_method(player, "set_autoplay", "");
  325. undo_redo->add_undo_method(player, "set_autoplay", current);
  326. // Avoid having the autoplay icon linger around if there is only one animation in the player.
  327. undo_redo->add_do_method(this, "_animation_player_changed", player);
  328. }
  329. undo_redo->add_do_method(al.ptr(), "remove_animation", current);
  330. undo_redo->add_undo_method(al.ptr(), "add_animation", current, anim);
  331. undo_redo->add_do_method(this, "_animation_player_changed", player);
  332. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  333. if (animation->has_selectable_items() && animation->get_selectable_item(false) == animation->get_selectable_item(true)) { // Last item remaining.
  334. undo_redo->add_do_method(this, "_stop_onion_skinning");
  335. undo_redo->add_undo_method(this, "_start_onion_skinning");
  336. }
  337. undo_redo->commit_action();
  338. }
  339. void AnimationPlayerEditor::_select_anim_by_name(const String &p_anim) {
  340. int idx = -1;
  341. for (int i = 0; i < animation->get_item_count(); i++) {
  342. if (animation->get_item_text(i) == p_anim) {
  343. idx = i;
  344. break;
  345. }
  346. }
  347. ERR_FAIL_COND(idx == -1);
  348. animation->select(idx);
  349. _animation_selected(idx);
  350. }
  351. double AnimationPlayerEditor::_get_editor_step() const {
  352. // Returns the effective snapping value depending on snapping modifiers, or 0 if snapping is disabled.
  353. if (track_editor->is_snap_enabled()) {
  354. const String current = player->get_assigned_animation();
  355. const Ref<Animation> anim = player->get_animation(current);
  356. ERR_FAIL_COND_V(!anim.is_valid(), 0.0);
  357. // Use more precise snapping when holding Shift
  358. return Input::get_singleton()->is_key_pressed(Key::SHIFT) ? anim->get_step() * 0.25 : anim->get_step();
  359. }
  360. return 0.0;
  361. }
  362. void AnimationPlayerEditor::_animation_name_edited() {
  363. player->stop();
  364. String new_name = name->get_text();
  365. if (!AnimationLibrary::is_valid_animation_name(new_name)) {
  366. error_dialog->set_text(TTR("Invalid animation name!"));
  367. error_dialog->popup_centered();
  368. return;
  369. }
  370. if (name_dialog_op == TOOL_RENAME_ANIM && animation->has_selectable_items() && animation->get_item_text(animation->get_selected()) == new_name) {
  371. name_dialog->hide();
  372. return;
  373. }
  374. String test_name_prefix = "";
  375. if (library->is_visible() && library->get_selected_id() != -1) {
  376. test_name_prefix = library->get_item_metadata(library->get_selected_id());
  377. test_name_prefix += (test_name_prefix != "") ? "/" : "";
  378. }
  379. if (player->has_animation(test_name_prefix + new_name)) {
  380. error_dialog->set_text(vformat(TTR("Animation '%s' already exists!"), test_name_prefix + new_name));
  381. error_dialog->popup_centered();
  382. return;
  383. }
  384. switch (name_dialog_op) {
  385. case TOOL_RENAME_ANIM: {
  386. String current = animation->get_item_text(animation->get_selected());
  387. Ref<Animation> anim = player->get_animation(current);
  388. Ref<AnimationLibrary> al = player->get_animation_library(player->find_animation_library(anim));
  389. ERR_FAIL_COND(al.is_null());
  390. // Extract library prefix if present.
  391. String new_library_prefix = "";
  392. if (current.contains("/")) {
  393. new_library_prefix = current.get_slice("/", 0) + "/";
  394. current = current.get_slice("/", 1);
  395. }
  396. undo_redo->create_action(TTR("Rename Animation"));
  397. undo_redo->add_do_method(al.ptr(), "rename_animation", current, new_name);
  398. undo_redo->add_do_method(anim.ptr(), "set_name", new_name);
  399. undo_redo->add_undo_method(al.ptr(), "rename_animation", new_name, current);
  400. undo_redo->add_undo_method(anim.ptr(), "set_name", current);
  401. undo_redo->add_do_method(this, "_animation_player_changed", player);
  402. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  403. undo_redo->commit_action();
  404. _select_anim_by_name(new_library_prefix + new_name);
  405. } break;
  406. case TOOL_NEW_ANIM: {
  407. Ref<Animation> new_anim = Ref<Animation>(memnew(Animation));
  408. new_anim->set_name(new_name);
  409. String library_name;
  410. Ref<AnimationLibrary> al;
  411. if (library->is_visible()) {
  412. library_name = library->get_item_metadata(library->get_selected());
  413. // It's possible that [Global] was selected, but doesn't exist yet.
  414. if (player->has_animation_library(library_name)) {
  415. al = player->get_animation_library(library_name);
  416. }
  417. } else {
  418. if (player->has_animation_library("")) {
  419. al = player->get_animation_library("");
  420. library_name = "";
  421. }
  422. }
  423. undo_redo->create_action(TTR("Add Animation"));
  424. bool lib_added = false;
  425. if (al.is_null()) {
  426. al.instantiate();
  427. lib_added = true;
  428. undo_redo->add_do_method(player, "add_animation_library", "", al);
  429. library_name = "";
  430. }
  431. undo_redo->add_do_method(al.ptr(), "add_animation", new_name, new_anim);
  432. undo_redo->add_undo_method(al.ptr(), "remove_animation", new_name);
  433. undo_redo->add_do_method(this, "_animation_player_changed", player);
  434. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  435. if (!animation->has_selectable_items()) {
  436. undo_redo->add_do_method(this, "_start_onion_skinning");
  437. undo_redo->add_undo_method(this, "_stop_onion_skinning");
  438. }
  439. if (lib_added) {
  440. undo_redo->add_undo_method(player, "remove_animation_library", "");
  441. }
  442. undo_redo->commit_action();
  443. if (library_name != "") {
  444. library_name = library_name + "/";
  445. }
  446. _select_anim_by_name(library_name + new_name);
  447. } break;
  448. case TOOL_DUPLICATE_ANIM: {
  449. String current = animation->get_item_text(animation->get_selected());
  450. Ref<Animation> anim = player->get_animation(current);
  451. Ref<Animation> new_anim = _animation_clone(anim);
  452. new_anim->set_name(new_name);
  453. String library_name;
  454. Ref<AnimationLibrary> al;
  455. if (library->is_visible()) {
  456. library_name = library->get_item_metadata(library->get_selected());
  457. // It's possible that [Global] was selected, but doesn't exist yet.
  458. if (player->has_animation_library(library_name)) {
  459. al = player->get_animation_library(library_name);
  460. }
  461. } else {
  462. if (player->has_animation_library("")) {
  463. al = player->get_animation_library("");
  464. library_name = "";
  465. }
  466. }
  467. undo_redo->create_action(TTR("Duplicate Animation"));
  468. bool lib_added = false;
  469. if (al.is_null()) {
  470. al.instantiate();
  471. lib_added = true;
  472. undo_redo->add_do_method(player, "add_animation_library", "", al);
  473. library_name = "";
  474. }
  475. undo_redo->add_do_method(al.ptr(), "add_animation", new_name, new_anim);
  476. undo_redo->add_undo_method(al.ptr(), "remove_animation", new_name);
  477. undo_redo->add_do_method(this, "_animation_player_changed", player);
  478. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  479. if (lib_added) {
  480. undo_redo->add_undo_method(player, "remove_animation_library", "");
  481. }
  482. undo_redo->commit_action();
  483. if (library_name != "") {
  484. library_name = library_name + "/";
  485. }
  486. _select_anim_by_name(library_name + new_name);
  487. } break;
  488. }
  489. name_dialog->hide();
  490. }
  491. void AnimationPlayerEditor::_blend_editor_next_changed(const int p_idx) {
  492. if (!animation->has_selectable_items()) {
  493. return;
  494. }
  495. String current = animation->get_item_text(animation->get_selected());
  496. undo_redo->create_action(TTR("Blend Next Changed"));
  497. undo_redo->add_do_method(player, "animation_set_next", current, blend_editor.next->get_item_text(p_idx));
  498. undo_redo->add_undo_method(player, "animation_set_next", current, player->animation_get_next(current));
  499. undo_redo->add_do_method(this, "_animation_player_changed", player);
  500. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  501. undo_redo->commit_action();
  502. }
  503. void AnimationPlayerEditor::_animation_blend() {
  504. if (updating_blends) {
  505. return;
  506. }
  507. blend_editor.tree->clear();
  508. if (!animation->has_selectable_items()) {
  509. return;
  510. }
  511. String current = animation->get_item_text(animation->get_selected());
  512. blend_editor.dialog->popup_centered(Size2(400, 400) * EDSCALE);
  513. blend_editor.tree->set_hide_root(true);
  514. blend_editor.tree->set_column_expand_ratio(0, 10);
  515. blend_editor.tree->set_column_clip_content(0, true);
  516. blend_editor.tree->set_column_expand_ratio(1, 3);
  517. blend_editor.tree->set_column_clip_content(1, true);
  518. List<StringName> anims;
  519. player->get_animation_list(&anims);
  520. TreeItem *root = blend_editor.tree->create_item();
  521. updating_blends = true;
  522. int i = 0;
  523. bool anim_found = false;
  524. blend_editor.next->clear();
  525. blend_editor.next->add_item("", i);
  526. for (const StringName &to : anims) {
  527. TreeItem *blend = blend_editor.tree->create_item(root);
  528. blend->set_editable(0, false);
  529. blend->set_editable(1, true);
  530. blend->set_text(0, to);
  531. blend->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  532. blend->set_range_config(1, 0, 3600, 0.001);
  533. blend->set_range(1, player->get_blend_time(current, to));
  534. i++;
  535. blend_editor.next->add_item(to, i);
  536. if (to == player->animation_get_next(current)) {
  537. blend_editor.next->select(i);
  538. anim_found = true;
  539. }
  540. }
  541. // make sure we reset it else it becomes out of sync and could contain a deleted animation
  542. if (!anim_found) {
  543. blend_editor.next->select(0);
  544. player->animation_set_next(current, blend_editor.next->get_item_text(0));
  545. }
  546. updating_blends = false;
  547. }
  548. void AnimationPlayerEditor::_blend_edited() {
  549. if (updating_blends) {
  550. return;
  551. }
  552. if (!animation->has_selectable_items()) {
  553. return;
  554. }
  555. String current = animation->get_item_text(animation->get_selected());
  556. TreeItem *selected = blend_editor.tree->get_edited();
  557. if (!selected) {
  558. return;
  559. }
  560. updating_blends = true;
  561. String to = selected->get_text(0);
  562. float blend_time = selected->get_range(1);
  563. float prev_blend_time = player->get_blend_time(current, to);
  564. undo_redo->create_action(TTR("Change Blend Time"));
  565. undo_redo->add_do_method(player, "set_blend_time", current, to, blend_time);
  566. undo_redo->add_undo_method(player, "set_blend_time", current, to, prev_blend_time);
  567. undo_redo->add_do_method(this, "_animation_player_changed", player);
  568. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  569. undo_redo->commit_action();
  570. updating_blends = false;
  571. }
  572. void AnimationPlayerEditor::ensure_visibility() {
  573. if (player && pin->is_pressed()) {
  574. return; // another player is pinned, don't reset
  575. }
  576. _animation_edit();
  577. }
  578. Dictionary AnimationPlayerEditor::get_state() const {
  579. Dictionary d;
  580. d["visible"] = is_visible_in_tree();
  581. if (EditorNode::get_singleton()->get_edited_scene() && is_visible_in_tree() && player) {
  582. d["player"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(player);
  583. d["animation"] = player->get_assigned_animation();
  584. d["track_editor_state"] = track_editor->get_state();
  585. }
  586. return d;
  587. }
  588. void AnimationPlayerEditor::set_state(const Dictionary &p_state) {
  589. if (!p_state.has("visible") || !p_state["visible"]) {
  590. return;
  591. }
  592. if (!EditorNode::get_singleton()->get_edited_scene()) {
  593. return;
  594. }
  595. if (p_state.has("player")) {
  596. Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
  597. if (Object::cast_to<AnimationPlayer>(n) && EditorNode::get_singleton()->get_editor_selection()->is_selected(n)) {
  598. player = Object::cast_to<AnimationPlayer>(n);
  599. _update_player();
  600. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  601. set_process(true);
  602. ensure_visibility();
  603. if (p_state.has("animation")) {
  604. String anim = p_state["animation"];
  605. if (!anim.is_empty() && player->has_animation(anim)) {
  606. _select_anim_by_name(anim);
  607. _animation_edit();
  608. }
  609. }
  610. }
  611. }
  612. if (p_state.has("track_editor_state")) {
  613. track_editor->set_state(p_state["track_editor_state"]);
  614. }
  615. }
  616. void AnimationPlayerEditor::_animation_resource_edit() {
  617. String current = _get_current();
  618. if (current != String()) {
  619. Ref<Animation> anim = player->get_animation(current);
  620. EditorNode::get_singleton()->edit_resource(anim);
  621. }
  622. }
  623. void AnimationPlayerEditor::_animation_edit() {
  624. String current = _get_current();
  625. if (current != String()) {
  626. Ref<Animation> anim = player->get_animation(current);
  627. bool animation_library_is_foreign = EditorNode::get_singleton()->is_resource_read_only(anim);
  628. track_editor->set_animation(anim, animation_library_is_foreign);
  629. Node *root = player->get_node(player->get_root());
  630. if (root) {
  631. track_editor->set_root(root);
  632. }
  633. } else {
  634. track_editor->set_animation(Ref<Animation>(), true);
  635. track_editor->set_root(nullptr);
  636. }
  637. }
  638. void AnimationPlayerEditor::_scale_changed(const String &p_scale) {
  639. player->set_speed_scale(p_scale.to_float());
  640. }
  641. void AnimationPlayerEditor::_update_animation() {
  642. // the purpose of _update_animation is to reflect the current state
  643. // of the animation player in the current editor..
  644. updating = true;
  645. if (player->is_playing()) {
  646. play->set_pressed(true);
  647. stop->set_pressed(false);
  648. } else {
  649. play->set_pressed(false);
  650. stop->set_pressed(true);
  651. }
  652. scale->set_text(String::num(player->get_speed_scale(), 2));
  653. String current = player->get_assigned_animation();
  654. for (int i = 0; i < animation->get_item_count(); i++) {
  655. if (animation->get_item_text(i) == current) {
  656. animation->select(i);
  657. break;
  658. }
  659. }
  660. updating = false;
  661. }
  662. void AnimationPlayerEditor::_update_player() {
  663. updating = true;
  664. animation->clear();
  665. tool_anim->set_disabled(player == nullptr);
  666. pin->set_disabled(player == nullptr);
  667. if (!player) {
  668. AnimationPlayerEditor::get_singleton()->get_track_editor()->update_keying();
  669. return;
  670. }
  671. List<StringName> libraries;
  672. player->get_animation_library_list(&libraries);
  673. int active_idx = -1;
  674. bool no_anims_found = true;
  675. bool foreign_global_anim_lib = false;
  676. for (const StringName &K : libraries) {
  677. if (K != StringName()) {
  678. animation->add_separator(K);
  679. }
  680. // Check if the global library is foreign since we want to disable options for adding/remove/renaming animations if it is.
  681. Ref<AnimationLibrary> anim_library = player->get_animation_library(K);
  682. if (K == "") {
  683. foreign_global_anim_lib = EditorNode::get_singleton()->is_resource_read_only(anim_library);
  684. }
  685. List<StringName> animlist;
  686. anim_library->get_animation_list(&animlist);
  687. for (const StringName &E : animlist) {
  688. String path = K;
  689. if (path != "") {
  690. path += "/";
  691. }
  692. path += E;
  693. animation->add_item(path);
  694. if (player->get_assigned_animation() == path) {
  695. active_idx = animation->get_selectable_item(true);
  696. }
  697. no_anims_found = false;
  698. }
  699. }
  700. #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), foreign_global_anim_lib)
  701. ITEM_CHECK_DISABLED(TOOL_NEW_ANIM);
  702. #undef ITEM_CHECK_DISABLED
  703. #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), no_anims_found || foreign_global_anim_lib)
  704. ITEM_CHECK_DISABLED(TOOL_DUPLICATE_ANIM);
  705. ITEM_CHECK_DISABLED(TOOL_RENAME_ANIM);
  706. ITEM_CHECK_DISABLED(TOOL_EDIT_TRANSITIONS);
  707. ITEM_CHECK_DISABLED(TOOL_REMOVE_ANIM);
  708. ITEM_CHECK_DISABLED(TOOL_EDIT_RESOURCE);
  709. #undef ITEM_CHECK_DISABLED
  710. stop->set_disabled(no_anims_found);
  711. play->set_disabled(no_anims_found);
  712. play_bw->set_disabled(no_anims_found);
  713. play_bw_from->set_disabled(no_anims_found);
  714. play_from->set_disabled(no_anims_found);
  715. frame->set_editable(!no_anims_found);
  716. animation->set_disabled(no_anims_found);
  717. autoplay->set_disabled(no_anims_found);
  718. onion_toggle->set_disabled(no_anims_found);
  719. onion_skinning->set_disabled(no_anims_found);
  720. if (hack_disable_onion_skinning) {
  721. onion_toggle->set_disabled(true);
  722. onion_skinning->set_disabled(true);
  723. }
  724. _update_animation_list_icons();
  725. updating = false;
  726. if (active_idx != -1) {
  727. animation->select(active_idx);
  728. autoplay->set_pressed(animation->get_item_text(active_idx) == player->get_autoplay());
  729. _animation_selected(active_idx);
  730. } else if (animation->has_selectable_items()) {
  731. int item = animation->get_selectable_item();
  732. animation->select(item);
  733. autoplay->set_pressed(animation->get_item_text(item) == player->get_autoplay());
  734. _animation_selected(item);
  735. } else {
  736. _animation_selected(0);
  737. }
  738. if (!no_anims_found) {
  739. String current = animation->get_item_text(animation->get_selected());
  740. Ref<Animation> anim = player->get_animation(current);
  741. bool animation_library_is_foreign = EditorNode::get_singleton()->is_resource_read_only(anim);
  742. track_editor->set_animation(anim, animation_library_is_foreign);
  743. Node *root = player->get_node(player->get_root());
  744. if (root) {
  745. track_editor->set_root(root);
  746. }
  747. }
  748. _update_animation();
  749. }
  750. void AnimationPlayerEditor::_update_animation_list_icons() {
  751. for (int i = 0; i < animation->get_item_count(); i++) {
  752. String anim_name = animation->get_item_text(i);
  753. if (animation->is_item_disabled(i) || animation->is_item_separator(i)) {
  754. continue;
  755. }
  756. Ref<Texture2D> icon;
  757. if (anim_name == player->get_autoplay()) {
  758. if (anim_name == SceneStringNames::get_singleton()->RESET) {
  759. icon = autoplay_reset_icon;
  760. } else {
  761. icon = autoplay_icon;
  762. }
  763. } else if (anim_name == SceneStringNames::get_singleton()->RESET) {
  764. icon = reset_icon;
  765. }
  766. animation->set_item_icon(i, icon);
  767. }
  768. }
  769. void AnimationPlayerEditor::_update_name_dialog_library_dropdown() {
  770. StringName current_library_name = StringName();
  771. if (animation->has_selectable_items()) {
  772. String current_animation_name = animation->get_item_text(animation->get_selected());
  773. Ref<Animation> current_animation = player->get_animation(current_animation_name);
  774. if (current_animation.is_valid()) {
  775. current_library_name = player->find_animation_library(current_animation);
  776. }
  777. }
  778. List<StringName> libraries;
  779. player->get_animation_library_list(&libraries);
  780. library->clear();
  781. // When [Global] isn't present, but other libraries are, add option of creating [Global].
  782. int index_offset = 0;
  783. if (!player->has_animation_library(StringName())) {
  784. library->add_item(String(TTR("[Global] (create)")));
  785. library->set_item_metadata(0, "");
  786. index_offset = 1;
  787. }
  788. int current_lib_id = index_offset; // Don't default to [Global] if it doesn't exist yet.
  789. for (int i = 0; i < libraries.size(); i++) {
  790. StringName library_name = libraries[i];
  791. library->add_item((library_name == StringName()) ? String(TTR("[Global]")) : String(library_name));
  792. library->set_item_metadata(i + index_offset, String(library_name));
  793. // Default to duplicating into same library.
  794. if (library_name == current_library_name) {
  795. current_lib_id = i + index_offset;
  796. }
  797. }
  798. if (library->get_item_count() > 1) {
  799. library->select(current_lib_id);
  800. library->show();
  801. } else {
  802. library->hide();
  803. }
  804. }
  805. void AnimationPlayerEditor::set_undo_redo(Ref<EditorUndoRedoManager> p_undo_redo) {
  806. undo_redo = p_undo_redo;
  807. }
  808. void AnimationPlayerEditor::edit(AnimationPlayer *p_player) {
  809. if (player && pin->is_pressed()) {
  810. return; // Ignore, pinned.
  811. }
  812. player = p_player;
  813. if (player) {
  814. _update_player();
  815. if (onion.enabled) {
  816. if (animation->has_selectable_items()) {
  817. _start_onion_skinning();
  818. } else {
  819. _stop_onion_skinning();
  820. }
  821. }
  822. track_editor->show_select_node_warning(false);
  823. } else {
  824. if (onion.enabled) {
  825. _stop_onion_skinning();
  826. }
  827. track_editor->show_select_node_warning(true);
  828. }
  829. library_editor->set_animation_player(player);
  830. }
  831. void AnimationPlayerEditor::forward_force_draw_over_viewport(Control *p_overlay) {
  832. if (!onion.can_overlay) {
  833. return;
  834. }
  835. // Can happen on viewport resize, at least.
  836. if (!_are_onion_layers_valid()) {
  837. return;
  838. }
  839. RID ci = p_overlay->get_canvas_item();
  840. Rect2 src_rect = p_overlay->get_global_rect();
  841. // Re-flip since captures are already flipped.
  842. src_rect.position.y = onion.capture_size.y - (src_rect.position.y + src_rect.size.y);
  843. src_rect.size.y *= -1;
  844. Rect2 dst_rect = Rect2(Point2(), p_overlay->get_size());
  845. float alpha_step = 1.0 / (onion.steps + 1);
  846. int cidx = 0;
  847. if (onion.past) {
  848. float alpha = 0;
  849. do {
  850. alpha += alpha_step;
  851. if (onion.captures_valid[cidx]) {
  852. RS::get_singleton()->canvas_item_add_texture_rect_region(
  853. ci, dst_rect, RS::get_singleton()->viewport_get_texture(onion.captures[cidx]), src_rect, Color(1, 1, 1, alpha));
  854. }
  855. cidx++;
  856. } while (cidx < onion.steps);
  857. }
  858. if (onion.future) {
  859. float alpha = 1;
  860. int base_cidx = cidx;
  861. do {
  862. alpha -= alpha_step;
  863. if (onion.captures_valid[cidx]) {
  864. RS::get_singleton()->canvas_item_add_texture_rect_region(
  865. ci, dst_rect, RS::get_singleton()->viewport_get_texture(onion.captures[cidx]), src_rect, Color(1, 1, 1, alpha));
  866. }
  867. cidx++;
  868. } while (cidx < base_cidx + onion.steps); // In case there's the present capture at the end, skip it.
  869. }
  870. }
  871. void AnimationPlayerEditor::_animation_duplicate() {
  872. if (!animation->has_selectable_items()) {
  873. return;
  874. }
  875. String current = animation->get_item_text(animation->get_selected());
  876. Ref<Animation> anim = player->get_animation(current);
  877. if (!anim.is_valid()) {
  878. return;
  879. }
  880. String new_name = current;
  881. while (player->has_animation(new_name)) {
  882. new_name = new_name + " (copy)";
  883. }
  884. if (new_name.contains("/")) {
  885. // Discard library prefix.
  886. new_name = new_name.get_slice("/", 1);
  887. }
  888. _update_name_dialog_library_dropdown();
  889. name_dialog_op = TOOL_DUPLICATE_ANIM;
  890. name_dialog->set_title(TTR("Duplicate Animation"));
  891. name_title->set_text(TTR("Duplicated Animation Name:"));
  892. name->set_text(new_name);
  893. name_dialog->popup_centered(Size2(300, 90));
  894. name->select_all();
  895. name->grab_focus();
  896. }
  897. Ref<Animation> AnimationPlayerEditor::_animation_clone(Ref<Animation> p_anim) {
  898. Ref<Animation> new_anim = memnew(Animation);
  899. List<PropertyInfo> plist;
  900. p_anim->get_property_list(&plist);
  901. for (const PropertyInfo &E : plist) {
  902. if (E.usage & PROPERTY_USAGE_STORAGE) {
  903. new_anim->set(E.name, p_anim->get(E.name));
  904. }
  905. }
  906. new_anim->set_path("");
  907. return new_anim;
  908. }
  909. void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_set, bool p_timeline_only) {
  910. if (updating || !player || player->is_playing()) {
  911. return;
  912. };
  913. updating = true;
  914. String current = player->get_assigned_animation();
  915. if (current.is_empty() || !player->has_animation(current)) {
  916. updating = false;
  917. current = "";
  918. return;
  919. };
  920. Ref<Animation> anim;
  921. anim = player->get_animation(current);
  922. float pos = CLAMP((double)anim->get_length() * (p_value / frame->get_max()), 0, (double)anim->get_length());
  923. if (track_editor->is_snap_enabled()) {
  924. pos = Math::snapped(pos, _get_editor_step());
  925. }
  926. if (!p_timeline_only) {
  927. if (player->is_valid() && !p_set) {
  928. float cpos = player->get_current_animation_position();
  929. player->seek_delta(pos, pos - cpos);
  930. } else {
  931. player->stop(true);
  932. player->seek(pos, true);
  933. }
  934. }
  935. track_editor->set_anim_pos(pos);
  936. };
  937. void AnimationPlayerEditor::_animation_player_changed(Object *p_pl) {
  938. if (player == p_pl && is_visible_in_tree()) {
  939. _update_player();
  940. if (blend_editor.dialog->is_visible()) {
  941. _animation_blend(); // Update.
  942. }
  943. if (library_editor->is_visible()) {
  944. library_editor->update_tree();
  945. }
  946. }
  947. }
  948. void AnimationPlayerEditor::_list_changed() {
  949. if (is_visible_in_tree()) {
  950. _update_player();
  951. }
  952. }
  953. void AnimationPlayerEditor::_animation_key_editor_anim_len_changed(float p_len) {
  954. frame->set_max(p_len);
  955. }
  956. void AnimationPlayerEditor::_animation_key_editor_seek(float p_pos, bool p_drag, bool p_timeline_only) {
  957. timeline_position = p_pos;
  958. if (!is_visible_in_tree()) {
  959. return;
  960. }
  961. if (!player) {
  962. return;
  963. }
  964. if (player->is_playing()) {
  965. return;
  966. }
  967. if (!player->has_animation(player->get_assigned_animation())) {
  968. return;
  969. }
  970. updating = true;
  971. frame->set_value(Math::snapped(p_pos, _get_editor_step()));
  972. updating = false;
  973. _seek_value_changed(p_pos, !p_drag, p_timeline_only);
  974. }
  975. void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
  976. String current = _get_current();
  977. Ref<Animation> anim;
  978. if (!current.is_empty()) {
  979. anim = player->get_animation(current);
  980. }
  981. switch (p_option) {
  982. case TOOL_NEW_ANIM: {
  983. _animation_new();
  984. } break;
  985. case TOOL_ANIM_LIBRARY: {
  986. library_editor->set_animation_player(player);
  987. library_editor->show_dialog();
  988. } break;
  989. case TOOL_DUPLICATE_ANIM: {
  990. _animation_duplicate();
  991. } break;
  992. case TOOL_RENAME_ANIM: {
  993. _animation_rename();
  994. } break;
  995. case TOOL_EDIT_TRANSITIONS: {
  996. _animation_blend();
  997. } break;
  998. case TOOL_REMOVE_ANIM: {
  999. _animation_remove();
  1000. } break;
  1001. case TOOL_EDIT_RESOURCE: {
  1002. if (anim.is_valid()) {
  1003. EditorNode::get_singleton()->edit_resource(anim);
  1004. }
  1005. } break;
  1006. }
  1007. }
  1008. void AnimationPlayerEditor::_onion_skinning_menu(int p_option) {
  1009. PopupMenu *menu = onion_skinning->get_popup();
  1010. int idx = menu->get_item_index(p_option);
  1011. switch (p_option) {
  1012. case ONION_SKINNING_ENABLE: {
  1013. onion.enabled = !onion.enabled;
  1014. if (onion.enabled) {
  1015. _start_onion_skinning();
  1016. } else {
  1017. _stop_onion_skinning();
  1018. }
  1019. } break;
  1020. case ONION_SKINNING_PAST: {
  1021. // Ensure at least one of past/future is checked.
  1022. onion.past = onion.future ? !onion.past : true;
  1023. menu->set_item_checked(idx, onion.past);
  1024. } break;
  1025. case ONION_SKINNING_FUTURE: {
  1026. // Ensure at least one of past/future is checked.
  1027. onion.future = onion.past ? !onion.future : true;
  1028. menu->set_item_checked(idx, onion.future);
  1029. } break;
  1030. case ONION_SKINNING_1_STEP: // Fall-through.
  1031. case ONION_SKINNING_2_STEPS:
  1032. case ONION_SKINNING_3_STEPS: {
  1033. onion.steps = (p_option - ONION_SKINNING_1_STEP) + 1;
  1034. int one_frame_idx = menu->get_item_index(ONION_SKINNING_1_STEP);
  1035. for (int i = 0; i <= ONION_SKINNING_LAST_STEPS_OPTION - ONION_SKINNING_1_STEP; i++) {
  1036. menu->set_item_checked(one_frame_idx + i, onion.steps == i + 1);
  1037. }
  1038. } break;
  1039. case ONION_SKINNING_DIFFERENCES_ONLY: {
  1040. onion.differences_only = !onion.differences_only;
  1041. menu->set_item_checked(idx, onion.differences_only);
  1042. } break;
  1043. case ONION_SKINNING_FORCE_WHITE_MODULATE: {
  1044. onion.force_white_modulate = !onion.force_white_modulate;
  1045. menu->set_item_checked(idx, onion.force_white_modulate);
  1046. } break;
  1047. case ONION_SKINNING_INCLUDE_GIZMOS: {
  1048. onion.include_gizmos = !onion.include_gizmos;
  1049. menu->set_item_checked(idx, onion.include_gizmos);
  1050. } break;
  1051. }
  1052. }
  1053. void AnimationPlayerEditor::shortcut_input(const Ref<InputEvent> &p_ev) {
  1054. ERR_FAIL_COND(p_ev.is_null());
  1055. Ref<InputEventKey> k = p_ev;
  1056. if (is_visible_in_tree() && k.is_valid() && k->is_pressed() && !k->is_echo() && !k->is_alt_pressed() && !k->is_ctrl_pressed() && !k->is_meta_pressed()) {
  1057. switch (k->get_keycode()) {
  1058. case Key::A: {
  1059. if (!k->is_shift_pressed()) {
  1060. _play_bw_from_pressed();
  1061. } else {
  1062. _play_bw_pressed();
  1063. }
  1064. accept_event();
  1065. } break;
  1066. case Key::S: {
  1067. _stop_pressed();
  1068. accept_event();
  1069. } break;
  1070. case Key::D: {
  1071. if (!k->is_shift_pressed()) {
  1072. _play_from_pressed();
  1073. } else {
  1074. _play_pressed();
  1075. }
  1076. accept_event();
  1077. } break;
  1078. default:
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. void AnimationPlayerEditor::_editor_visibility_changed() {
  1084. if (is_visible() && animation->has_selectable_items()) {
  1085. _start_onion_skinning();
  1086. }
  1087. }
  1088. bool AnimationPlayerEditor::_are_onion_layers_valid() {
  1089. ERR_FAIL_COND_V(!onion.past && !onion.future, false);
  1090. Point2 capture_size = get_tree()->get_root()->get_size();
  1091. return onion.captures.size() == onion.get_needed_capture_count() && onion.capture_size == capture_size;
  1092. }
  1093. void AnimationPlayerEditor::_allocate_onion_layers() {
  1094. _free_onion_layers();
  1095. int captures = onion.get_needed_capture_count();
  1096. Point2 capture_size = get_tree()->get_root()->get_size();
  1097. onion.captures.resize(captures);
  1098. onion.captures_valid.resize(captures);
  1099. for (int i = 0; i < captures; i++) {
  1100. bool is_present = onion.differences_only && i == captures - 1;
  1101. // Each capture is a viewport with a canvas item attached that renders a full-size rect with the contents of the main viewport.
  1102. onion.captures.write[i] = RS::get_singleton()->viewport_create();
  1103. RS::get_singleton()->viewport_set_size(onion.captures[i], capture_size.width, capture_size.height);
  1104. RS::get_singleton()->viewport_set_update_mode(onion.captures[i], RS::VIEWPORT_UPDATE_ALWAYS);
  1105. RS::get_singleton()->viewport_set_transparent_background(onion.captures[i], !is_present);
  1106. RS::get_singleton()->viewport_attach_canvas(onion.captures[i], onion.capture.canvas);
  1107. }
  1108. // Reset the capture canvas item to the current root viewport texture (defensive).
  1109. RS::get_singleton()->canvas_item_clear(onion.capture.canvas_item);
  1110. RS::get_singleton()->canvas_item_add_texture_rect(onion.capture.canvas_item, Rect2(Point2(), capture_size), get_tree()->get_root()->get_texture()->get_rid());
  1111. onion.capture_size = capture_size;
  1112. }
  1113. void AnimationPlayerEditor::_free_onion_layers() {
  1114. for (int i = 0; i < onion.captures.size(); i++) {
  1115. if (onion.captures[i].is_valid()) {
  1116. RS::get_singleton()->free(onion.captures[i]);
  1117. }
  1118. }
  1119. onion.captures.clear();
  1120. onion.captures_valid.clear();
  1121. }
  1122. void AnimationPlayerEditor::_prepare_onion_layers_1() {
  1123. // This would be called per viewport and we want to act once only.
  1124. int64_t cur_frame = get_tree()->get_frame();
  1125. if (cur_frame == onion.last_frame) {
  1126. return;
  1127. }
  1128. if (!onion.enabled || !is_processing() || !is_visible() || !get_player()) {
  1129. _stop_onion_skinning();
  1130. return;
  1131. }
  1132. onion.last_frame = cur_frame;
  1133. // Refresh viewports with no onion layers overlaid.
  1134. onion.can_overlay = false;
  1135. plugin->update_overlays();
  1136. if (player->is_playing()) {
  1137. return;
  1138. }
  1139. // And go to next step afterwards.
  1140. call_deferred(SNAME("_prepare_onion_layers_2"));
  1141. }
  1142. void AnimationPlayerEditor::_prepare_onion_layers_1_deferred() {
  1143. call_deferred(SNAME("_prepare_onion_layers_1"));
  1144. }
  1145. void AnimationPlayerEditor::_prepare_onion_layers_2() {
  1146. Ref<Animation> anim = player->get_animation(player->get_assigned_animation());
  1147. if (!anim.is_valid()) {
  1148. return;
  1149. }
  1150. if (!_are_onion_layers_valid()) {
  1151. _allocate_onion_layers();
  1152. }
  1153. // Hide superfluous elements that would make the overlay unnecessary cluttered.
  1154. Dictionary canvas_edit_state;
  1155. Dictionary spatial_edit_state;
  1156. if (Node3DEditor::get_singleton()->is_visible()) {
  1157. // 3D
  1158. spatial_edit_state = Node3DEditor::get_singleton()->get_state();
  1159. Dictionary new_state = spatial_edit_state.duplicate();
  1160. new_state["show_grid"] = false;
  1161. new_state["show_origin"] = false;
  1162. Array orig_vp = spatial_edit_state["viewports"];
  1163. Array vp;
  1164. vp.resize(4);
  1165. for (int i = 0; i < vp.size(); i++) {
  1166. Dictionary d = ((Dictionary)orig_vp[i]).duplicate();
  1167. d["use_environment"] = false;
  1168. d["doppler"] = false;
  1169. d["gizmos"] = onion.include_gizmos ? d["gizmos"] : Variant(false);
  1170. d["information"] = false;
  1171. vp[i] = d;
  1172. }
  1173. new_state["viewports"] = vp;
  1174. // TODO: Save/restore only affected entries.
  1175. Node3DEditor::get_singleton()->set_state(new_state);
  1176. } else { // CanvasItemEditor
  1177. // 2D
  1178. canvas_edit_state = CanvasItemEditor::get_singleton()->get_state();
  1179. Dictionary new_state = canvas_edit_state.duplicate();
  1180. new_state["show_grid"] = false;
  1181. new_state["show_rulers"] = false;
  1182. new_state["show_guides"] = false;
  1183. new_state["show_helpers"] = false;
  1184. new_state["show_zoom_control"] = false;
  1185. // TODO: Save/restore only affected entries.
  1186. CanvasItemEditor::get_singleton()->set_state(new_state);
  1187. }
  1188. // Tweak the root viewport to ensure it's rendered before our target.
  1189. RID root_vp = get_tree()->get_root()->get_viewport_rid();
  1190. Rect2 root_vp_screen_rect = Rect2(Vector2(), get_tree()->get_root()->get_size());
  1191. RS::get_singleton()->viewport_attach_to_screen(root_vp, Rect2());
  1192. RS::get_singleton()->viewport_set_update_mode(root_vp, RS::VIEWPORT_UPDATE_ALWAYS);
  1193. RID present_rid;
  1194. if (onion.differences_only) {
  1195. // Capture present scene as it is.
  1196. RS::get_singleton()->canvas_item_set_material(onion.capture.canvas_item, RID());
  1197. present_rid = onion.captures[onion.captures.size() - 1];
  1198. RS::get_singleton()->viewport_set_active(present_rid, true);
  1199. RS::get_singleton()->viewport_set_parent_viewport(root_vp, present_rid);
  1200. RS::get_singleton()->draw(false);
  1201. RS::get_singleton()->viewport_set_active(present_rid, false);
  1202. }
  1203. // Backup current animation state.
  1204. Ref<AnimatedValuesBackup> values_backup = player->backup_animated_values();
  1205. float cpos = player->get_current_animation_position();
  1206. // Render every past/future step with the capture shader.
  1207. RS::get_singleton()->canvas_item_set_material(onion.capture.canvas_item, onion.capture.material->get_rid());
  1208. onion.capture.material->set_shader_parameter("bkg_color", GLOBAL_GET("rendering/environment/defaults/default_clear_color"));
  1209. onion.capture.material->set_shader_parameter("differences_only", onion.differences_only);
  1210. onion.capture.material->set_shader_parameter("present", onion.differences_only ? RS::get_singleton()->viewport_get_texture(present_rid) : RID());
  1211. int step_off_a = onion.past ? -onion.steps : 0;
  1212. int step_off_b = onion.future ? onion.steps : 0;
  1213. int cidx = 0;
  1214. onion.capture.material->set_shader_parameter("dir_color", onion.force_white_modulate ? Color(1, 1, 1) : Color(EDITOR_GET("editors/animation/onion_layers_past_color")));
  1215. for (int step_off = step_off_a; step_off <= step_off_b; step_off++) {
  1216. if (step_off == 0) {
  1217. // Skip present step and switch to the color of future.
  1218. if (!onion.force_white_modulate) {
  1219. onion.capture.material->set_shader_parameter("dir_color", EDITOR_GET("editors/animation/onion_layers_future_color"));
  1220. }
  1221. continue;
  1222. }
  1223. float pos = cpos + step_off * anim->get_step();
  1224. bool valid = anim->get_loop_mode() != Animation::LOOP_NONE || (pos >= 0 && pos <= anim->get_length());
  1225. onion.captures_valid.write[cidx] = valid;
  1226. if (valid) {
  1227. player->seek(pos, true);
  1228. get_tree()->flush_transform_notifications(); // Needed for transforms of Node3Ds.
  1229. values_backup->update_skeletons(); // Needed for Skeletons (2D & 3D).
  1230. RS::get_singleton()->viewport_set_active(onion.captures[cidx], true);
  1231. RS::get_singleton()->viewport_set_parent_viewport(root_vp, onion.captures[cidx]);
  1232. RS::get_singleton()->draw(false);
  1233. RS::get_singleton()->viewport_set_active(onion.captures[cidx], false);
  1234. }
  1235. cidx++;
  1236. }
  1237. // Restore root viewport.
  1238. RS::get_singleton()->viewport_set_parent_viewport(root_vp, RID());
  1239. RS::get_singleton()->viewport_attach_to_screen(root_vp, root_vp_screen_rect);
  1240. RS::get_singleton()->viewport_set_update_mode(root_vp, RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  1241. // Restore animation state
  1242. // (Seeking with update=true wouldn't do the trick because the current value of the properties
  1243. // may not match their value for the current point in the animation).
  1244. player->seek(cpos, false);
  1245. values_backup->restore();
  1246. // Restore state of main editors.
  1247. if (Node3DEditor::get_singleton()->is_visible()) {
  1248. // 3D
  1249. Node3DEditor::get_singleton()->set_state(spatial_edit_state);
  1250. } else { // CanvasItemEditor
  1251. // 2D
  1252. CanvasItemEditor::get_singleton()->set_state(canvas_edit_state);
  1253. }
  1254. // Update viewports with skin layers overlaid for the actual engine loop render.
  1255. onion.can_overlay = true;
  1256. plugin->update_overlays();
  1257. }
  1258. void AnimationPlayerEditor::_start_onion_skinning() {
  1259. // FIXME: Using "process_frame" makes onion layers update one frame behind the current.
  1260. if (!get_tree()->is_connected("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) {
  1261. get_tree()->connect("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred));
  1262. }
  1263. }
  1264. void AnimationPlayerEditor::_stop_onion_skinning() {
  1265. if (get_tree()->is_connected("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) {
  1266. get_tree()->disconnect("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred));
  1267. _free_onion_layers();
  1268. // Clean up the overlay.
  1269. onion.can_overlay = false;
  1270. plugin->update_overlays();
  1271. }
  1272. }
  1273. void AnimationPlayerEditor::_pin_pressed() {
  1274. SceneTreeDock::get_singleton()->get_tree_editor()->update_tree();
  1275. }
  1276. void AnimationPlayerEditor::_bind_methods() {
  1277. ClassDB::bind_method(D_METHOD("_animation_new"), &AnimationPlayerEditor::_animation_new);
  1278. ClassDB::bind_method(D_METHOD("_animation_rename"), &AnimationPlayerEditor::_animation_rename);
  1279. ClassDB::bind_method(D_METHOD("_animation_remove"), &AnimationPlayerEditor::_animation_remove);
  1280. ClassDB::bind_method(D_METHOD("_animation_blend"), &AnimationPlayerEditor::_animation_blend);
  1281. ClassDB::bind_method(D_METHOD("_animation_edit"), &AnimationPlayerEditor::_animation_edit);
  1282. ClassDB::bind_method(D_METHOD("_animation_resource_edit"), &AnimationPlayerEditor::_animation_resource_edit);
  1283. ClassDB::bind_method(D_METHOD("_animation_player_changed"), &AnimationPlayerEditor::_animation_player_changed);
  1284. ClassDB::bind_method(D_METHOD("_list_changed"), &AnimationPlayerEditor::_list_changed);
  1285. ClassDB::bind_method(D_METHOD("_animation_duplicate"), &AnimationPlayerEditor::_animation_duplicate);
  1286. ClassDB::bind_method(D_METHOD("_prepare_onion_layers_1"), &AnimationPlayerEditor::_prepare_onion_layers_1);
  1287. ClassDB::bind_method(D_METHOD("_prepare_onion_layers_2"), &AnimationPlayerEditor::_prepare_onion_layers_2);
  1288. ClassDB::bind_method(D_METHOD("_start_onion_skinning"), &AnimationPlayerEditor::_start_onion_skinning);
  1289. ClassDB::bind_method(D_METHOD("_stop_onion_skinning"), &AnimationPlayerEditor::_stop_onion_skinning);
  1290. }
  1291. AnimationPlayerEditor *AnimationPlayerEditor::singleton = nullptr;
  1292. AnimationPlayer *AnimationPlayerEditor::get_player() const {
  1293. return player;
  1294. }
  1295. AnimationPlayerEditor::AnimationPlayerEditor(AnimationPlayerEditorPlugin *p_plugin) {
  1296. plugin = p_plugin;
  1297. singleton = this;
  1298. updating = false;
  1299. set_focus_mode(FOCUS_ALL);
  1300. player = nullptr;
  1301. HBoxContainer *hb = memnew(HBoxContainer);
  1302. add_child(hb);
  1303. play_bw_from = memnew(Button);
  1304. play_bw_from->set_flat(true);
  1305. play_bw_from->set_tooltip_text(TTR("Play selected animation backwards from current pos. (A)"));
  1306. hb->add_child(play_bw_from);
  1307. play_bw = memnew(Button);
  1308. play_bw->set_flat(true);
  1309. play_bw->set_tooltip_text(TTR("Play selected animation backwards from end. (Shift+A)"));
  1310. hb->add_child(play_bw);
  1311. stop = memnew(Button);
  1312. stop->set_flat(true);
  1313. stop->set_toggle_mode(true);
  1314. hb->add_child(stop);
  1315. stop->set_tooltip_text(TTR("Stop animation playback. (S)"));
  1316. play = memnew(Button);
  1317. play->set_flat(true);
  1318. play->set_tooltip_text(TTR("Play selected animation from start. (Shift+D)"));
  1319. hb->add_child(play);
  1320. play_from = memnew(Button);
  1321. play_from->set_flat(true);
  1322. play_from->set_tooltip_text(TTR("Play selected animation from current pos. (D)"));
  1323. hb->add_child(play_from);
  1324. frame = memnew(SpinBox);
  1325. hb->add_child(frame);
  1326. frame->set_custom_minimum_size(Size2(80, 0) * EDSCALE);
  1327. frame->set_stretch_ratio(2);
  1328. frame->set_step(0.0001);
  1329. frame->set_tooltip_text(TTR("Animation position (in seconds)."));
  1330. hb->add_child(memnew(VSeparator));
  1331. scale = memnew(LineEdit);
  1332. hb->add_child(scale);
  1333. scale->set_h_size_flags(SIZE_EXPAND_FILL);
  1334. scale->set_stretch_ratio(1);
  1335. scale->set_tooltip_text(TTR("Scale animation playback globally for the node."));
  1336. scale->hide();
  1337. delete_dialog = memnew(ConfirmationDialog);
  1338. add_child(delete_dialog);
  1339. delete_dialog->connect("confirmed", callable_mp(this, &AnimationPlayerEditor::_animation_remove_confirmed));
  1340. tool_anim = memnew(MenuButton);
  1341. tool_anim->set_shortcut_context(this);
  1342. tool_anim->set_flat(false);
  1343. tool_anim->set_tooltip_text(TTR("Animation Tools"));
  1344. tool_anim->set_text(TTR("Animation"));
  1345. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/new_animation", TTR("New")), TOOL_NEW_ANIM);
  1346. tool_anim->get_popup()->add_separator();
  1347. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/animation_libraries", TTR("Manage Animations...")), TOOL_ANIM_LIBRARY);
  1348. tool_anim->get_popup()->add_separator();
  1349. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/duplicate_animation", TTR("Duplicate...")), TOOL_DUPLICATE_ANIM);
  1350. tool_anim->get_popup()->add_separator();
  1351. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/rename_animation", TTR("Rename...")), TOOL_RENAME_ANIM);
  1352. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/edit_transitions", TTR("Edit Transitions...")), TOOL_EDIT_TRANSITIONS);
  1353. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/open_animation_in_inspector", TTR("Open in Inspector")), TOOL_EDIT_RESOURCE);
  1354. tool_anim->get_popup()->add_separator();
  1355. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/remove_animation", TTR("Remove")), TOOL_REMOVE_ANIM);
  1356. tool_anim->set_disabled(true);
  1357. hb->add_child(tool_anim);
  1358. animation = memnew(OptionButton);
  1359. hb->add_child(animation);
  1360. animation->set_h_size_flags(SIZE_EXPAND_FILL);
  1361. animation->set_tooltip_text(TTR("Display list of animations in player."));
  1362. animation->set_clip_text(true);
  1363. autoplay = memnew(Button);
  1364. autoplay->set_flat(true);
  1365. hb->add_child(autoplay);
  1366. autoplay->set_tooltip_text(TTR("Autoplay on Load"));
  1367. hb->add_child(memnew(VSeparator));
  1368. track_editor = memnew(AnimationTrackEditor);
  1369. hb->add_child(track_editor->get_edit_menu());
  1370. hb->add_child(memnew(VSeparator));
  1371. onion_toggle = memnew(Button);
  1372. onion_toggle->set_flat(true);
  1373. onion_toggle->set_toggle_mode(true);
  1374. onion_toggle->set_tooltip_text(TTR("Enable Onion Skinning"));
  1375. onion_toggle->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_onion_skinning_menu).bind(ONION_SKINNING_ENABLE));
  1376. hb->add_child(onion_toggle);
  1377. onion_skinning = memnew(MenuButton);
  1378. onion_skinning->set_tooltip_text(TTR("Onion Skinning Options"));
  1379. onion_skinning->get_popup()->add_separator(TTR("Directions"));
  1380. // TRANSLATORS: Opposite of "Future", refers to a direction in animation onion skinning.
  1381. onion_skinning->get_popup()->add_check_item(TTR("Past"), ONION_SKINNING_PAST);
  1382. onion_skinning->get_popup()->set_item_checked(-1, true);
  1383. // TRANSLATORS: Opposite of "Past", refers to a direction in animation onion skinning.
  1384. onion_skinning->get_popup()->add_check_item(TTR("Future"), ONION_SKINNING_FUTURE);
  1385. onion_skinning->get_popup()->add_separator(TTR("Depth"));
  1386. onion_skinning->get_popup()->add_radio_check_item(TTR("1 step"), ONION_SKINNING_1_STEP);
  1387. onion_skinning->get_popup()->set_item_checked(-1, true);
  1388. onion_skinning->get_popup()->add_radio_check_item(TTR("2 steps"), ONION_SKINNING_2_STEPS);
  1389. onion_skinning->get_popup()->add_radio_check_item(TTR("3 steps"), ONION_SKINNING_3_STEPS);
  1390. onion_skinning->get_popup()->add_separator();
  1391. onion_skinning->get_popup()->add_check_item(TTR("Differences Only"), ONION_SKINNING_DIFFERENCES_ONLY);
  1392. onion_skinning->get_popup()->add_check_item(TTR("Force White Modulate"), ONION_SKINNING_FORCE_WHITE_MODULATE);
  1393. onion_skinning->get_popup()->add_check_item(TTR("Include Gizmos (3D)"), ONION_SKINNING_INCLUDE_GIZMOS);
  1394. hb->add_child(onion_skinning);
  1395. // FIXME: Onion skinning disabled for now as it's broken and triggers fast
  1396. // flickering red/blue modulation (GH-53870).
  1397. if (hack_disable_onion_skinning) {
  1398. onion_toggle->set_disabled(true);
  1399. onion_toggle->set_tooltip_text(TTR("Onion Skinning temporarily disabled due to rendering bug."));
  1400. onion_skinning->set_disabled(true);
  1401. onion_skinning->set_tooltip_text(TTR("Onion Skinning temporarily disabled due to rendering bug."));
  1402. }
  1403. hb->add_child(memnew(VSeparator));
  1404. pin = memnew(Button);
  1405. pin->set_flat(true);
  1406. pin->set_toggle_mode(true);
  1407. pin->set_tooltip_text(TTR("Pin AnimationPlayer"));
  1408. hb->add_child(pin);
  1409. pin->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_pin_pressed));
  1410. file = memnew(EditorFileDialog);
  1411. add_child(file);
  1412. name_dialog = memnew(ConfirmationDialog);
  1413. name_dialog->set_title(TTR("Create New Animation"));
  1414. name_dialog->set_hide_on_ok(false);
  1415. add_child(name_dialog);
  1416. VBoxContainer *vb = memnew(VBoxContainer);
  1417. name_dialog->add_child(vb);
  1418. name_title = memnew(Label(TTR("Animation Name:")));
  1419. vb->add_child(name_title);
  1420. HBoxContainer *name_hb = memnew(HBoxContainer);
  1421. name = memnew(LineEdit);
  1422. name_hb->add_child(name);
  1423. name->set_h_size_flags(SIZE_EXPAND_FILL);
  1424. library = memnew(OptionButton);
  1425. name_hb->add_child(library);
  1426. library->hide();
  1427. vb->add_child(name_hb);
  1428. name_dialog->register_text_enter(name);
  1429. error_dialog = memnew(ConfirmationDialog);
  1430. error_dialog->set_ok_button_text(TTR("Close"));
  1431. error_dialog->set_title(TTR("Error!"));
  1432. add_child(error_dialog);
  1433. name_dialog->connect("confirmed", callable_mp(this, &AnimationPlayerEditor::_animation_name_edited));
  1434. blend_editor.dialog = memnew(AcceptDialog);
  1435. add_child(blend_editor.dialog);
  1436. blend_editor.dialog->set_ok_button_text(TTR("Close"));
  1437. blend_editor.dialog->set_hide_on_ok(true);
  1438. VBoxContainer *blend_vb = memnew(VBoxContainer);
  1439. blend_editor.dialog->add_child(blend_vb);
  1440. blend_editor.tree = memnew(Tree);
  1441. blend_editor.tree->set_columns(2);
  1442. blend_vb->add_margin_child(TTR("Blend Times:"), blend_editor.tree, true);
  1443. blend_editor.next = memnew(OptionButton);
  1444. blend_vb->add_margin_child(TTR("Next (Auto Queue):"), blend_editor.next);
  1445. blend_editor.dialog->set_title(TTR("Cross-Animation Blend Times"));
  1446. updating_blends = false;
  1447. blend_editor.tree->connect("item_edited", callable_mp(this, &AnimationPlayerEditor::_blend_edited));
  1448. autoplay->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_autoplay_pressed));
  1449. autoplay->set_toggle_mode(true);
  1450. play->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_pressed));
  1451. play_from->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_from_pressed));
  1452. play_bw->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_bw_pressed));
  1453. play_bw_from->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_bw_from_pressed));
  1454. stop->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_stop_pressed));
  1455. animation->connect("item_selected", callable_mp(this, &AnimationPlayerEditor::_animation_selected));
  1456. frame->connect("value_changed", callable_mp(this, &AnimationPlayerEditor::_seek_value_changed).bind(true, false));
  1457. scale->connect("text_submitted", callable_mp(this, &AnimationPlayerEditor::_scale_changed));
  1458. last_active = false;
  1459. timeline_position = 0;
  1460. set_process_shortcut_input(true);
  1461. add_child(track_editor);
  1462. track_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1463. track_editor->connect("timeline_changed", callable_mp(this, &AnimationPlayerEditor::_animation_key_editor_seek));
  1464. track_editor->connect("animation_len_changed", callable_mp(this, &AnimationPlayerEditor::_animation_key_editor_anim_len_changed));
  1465. _update_player();
  1466. library_editor = memnew(AnimationLibraryEditor);
  1467. add_child(library_editor);
  1468. library_editor->connect("update_editor", callable_mp(this, &AnimationPlayerEditor::_animation_player_changed));
  1469. // Onion skinning.
  1470. track_editor->connect("visibility_changed", callable_mp(this, &AnimationPlayerEditor::_editor_visibility_changed));
  1471. onion.enabled = false;
  1472. onion.past = true;
  1473. onion.future = false;
  1474. onion.steps = 1;
  1475. onion.differences_only = false;
  1476. onion.force_white_modulate = false;
  1477. onion.include_gizmos = false;
  1478. onion.last_frame = 0;
  1479. onion.can_overlay = false;
  1480. onion.capture_size = Size2();
  1481. onion.capture.canvas = RS::get_singleton()->canvas_create();
  1482. onion.capture.canvas_item = RS::get_singleton()->canvas_item_create();
  1483. RS::get_singleton()->canvas_item_set_parent(onion.capture.canvas_item, onion.capture.canvas);
  1484. onion.capture.material = Ref<ShaderMaterial>(memnew(ShaderMaterial));
  1485. onion.capture.shader = Ref<Shader>(memnew(Shader));
  1486. onion.capture.shader->set_code(R"(
  1487. // Animation editor onion skinning shader.
  1488. shader_type canvas_item;
  1489. uniform vec4 bkg_color;
  1490. uniform vec4 dir_color;
  1491. uniform bool differences_only;
  1492. uniform sampler2D present;
  1493. float zero_if_equal(vec4 a, vec4 b) {
  1494. return smoothstep(0.0, 0.005, length(a.rgb - b.rgb) / sqrt(3.0));
  1495. }
  1496. void fragment() {
  1497. vec4 capture_samp = texture(TEXTURE, UV);
  1498. vec4 present_samp = texture(present, UV);
  1499. float bkg_mask = zero_if_equal(capture_samp, bkg_color);
  1500. float diff_mask = 1.0 - zero_if_equal(present_samp, bkg_color);
  1501. diff_mask = min(1.0, diff_mask + float(!differences_only));
  1502. COLOR = vec4(capture_samp.rgb * dir_color.rgb, bkg_mask * diff_mask);
  1503. }
  1504. )");
  1505. RS::get_singleton()->material_set_shader(onion.capture.material->get_rid(), onion.capture.shader->get_rid());
  1506. }
  1507. AnimationPlayerEditor::~AnimationPlayerEditor() {
  1508. _free_onion_layers();
  1509. RS::get_singleton()->free(onion.capture.canvas);
  1510. RS::get_singleton()->free(onion.capture.canvas_item);
  1511. }
  1512. void AnimationPlayerEditorPlugin::_notification(int p_what) {
  1513. switch (p_what) {
  1514. case NOTIFICATION_ENTER_TREE: {
  1515. Node3DEditor::get_singleton()->connect("transform_key_request", callable_mp(this, &AnimationPlayerEditorPlugin::_transform_key_request));
  1516. InspectorDock::get_inspector_singleton()->connect("property_keyed", callable_mp(this, &AnimationPlayerEditorPlugin::_property_keyed));
  1517. anim_editor->get_track_editor()->connect("keying_changed", callable_mp(this, &AnimationPlayerEditorPlugin::_update_keying));
  1518. InspectorDock::get_inspector_singleton()->connect("edited_object_changed", callable_mp(anim_editor->get_track_editor(), &AnimationTrackEditor::update_keying));
  1519. set_force_draw_over_forwarding_enabled();
  1520. } break;
  1521. }
  1522. }
  1523. void AnimationPlayerEditorPlugin::_property_keyed(const String &p_keyed, const Variant &p_value, bool p_advance) {
  1524. if (!anim_editor->get_track_editor()->has_keying()) {
  1525. return;
  1526. }
  1527. anim_editor->get_track_editor()->insert_value_key(p_keyed, p_value, p_advance);
  1528. }
  1529. void AnimationPlayerEditorPlugin::_transform_key_request(Object *sp, const String &p_sub, const Transform3D &p_key) {
  1530. if (!anim_editor->get_track_editor()->has_keying()) {
  1531. return;
  1532. }
  1533. Node3D *s = Object::cast_to<Node3D>(sp);
  1534. if (!s) {
  1535. return;
  1536. }
  1537. anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_POSITION_3D, p_key.origin);
  1538. anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_ROTATION_3D, p_key.basis.get_rotation_quaternion());
  1539. anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_SCALE_3D, p_key.basis.get_scale());
  1540. }
  1541. void AnimationPlayerEditorPlugin::_update_keying() {
  1542. InspectorDock::get_inspector_singleton()->set_keying(anim_editor->get_track_editor()->has_keying());
  1543. }
  1544. void AnimationPlayerEditorPlugin::edit(Object *p_object) {
  1545. anim_editor->set_undo_redo(get_undo_redo());
  1546. if (!p_object) {
  1547. return;
  1548. }
  1549. anim_editor->edit(Object::cast_to<AnimationPlayer>(p_object));
  1550. }
  1551. bool AnimationPlayerEditorPlugin::handles(Object *p_object) const {
  1552. return p_object->is_class("AnimationPlayer");
  1553. }
  1554. void AnimationPlayerEditorPlugin::make_visible(bool p_visible) {
  1555. if (p_visible) {
  1556. EditorNode::get_singleton()->make_bottom_panel_item_visible(anim_editor);
  1557. anim_editor->set_process(true);
  1558. anim_editor->ensure_visibility();
  1559. }
  1560. }
  1561. AnimationPlayerEditorPlugin::AnimationPlayerEditorPlugin() {
  1562. anim_editor = memnew(AnimationPlayerEditor(this));
  1563. anim_editor->set_undo_redo(EditorNode::get_undo_redo());
  1564. EditorNode::get_singleton()->add_bottom_panel_item(TTR("Animation"), anim_editor);
  1565. }
  1566. AnimationPlayerEditorPlugin::~AnimationPlayerEditorPlugin() {
  1567. }