animation_player_editor_plugin.cpp 88 KB

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