animation_player_editor_plugin.cpp 81 KB

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