animation_player_editor_plugin.cpp 91 KB

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