animation_player_editor_plugin.cpp 81 KB

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