animation_player_editor_plugin.cpp 69 KB

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