animation_player_editor_plugin.cpp 89 KB

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