animation_player_editor_plugin.cpp 69 KB

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