animation_player_editor_plugin.cpp 88 KB

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