animation_player_editor_plugin.cpp 88 KB

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