animation_player_editor_plugin.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_file_dialog.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/plugins/canvas_item_editor_plugin.h" // For onion skinning.
  41. #include "editor/plugins/node_3d_editor_plugin.h" // For onion skinning.
  42. #include "editor/scene_tree_dock.h"
  43. #include "scene/main/window.h"
  44. #include "scene/resources/animation.h"
  45. #include "scene/scene_string_names.h"
  46. #include "servers/rendering_server.h"
  47. ///////////////////////////////////
  48. void AnimationPlayerEditor::_node_removed(Node *p_node) {
  49. if (player && player == p_node) {
  50. player = nullptr;
  51. set_process(false);
  52. track_editor->set_animation(Ref<Animation>(), true);
  53. track_editor->set_root(nullptr);
  54. track_editor->show_select_node_warning(true);
  55. _update_player();
  56. }
  57. }
  58. void AnimationPlayerEditor::_notification(int p_what) {
  59. switch (p_what) {
  60. case NOTIFICATION_PROCESS: {
  61. if (!player) {
  62. return;
  63. }
  64. updating = true;
  65. if (player->is_playing()) {
  66. {
  67. String animname = player->get_assigned_animation();
  68. if (player->has_animation(animname)) {
  69. Ref<Animation> anim = player->get_animation(animname);
  70. if (!anim.is_null()) {
  71. frame->set_max((double)anim->get_length());
  72. }
  73. }
  74. }
  75. frame->set_value(player->get_current_animation_position());
  76. track_editor->set_anim_pos(player->get_current_animation_position());
  77. } else if (!player->is_valid()) {
  78. // Reset timeline when the player has been stopped externally
  79. frame->set_value(0);
  80. } else if (last_active) {
  81. // Need the last frame after it stopped.
  82. frame->set_value(player->get_current_animation_position());
  83. }
  84. last_active = player->is_playing();
  85. updating = false;
  86. } break;
  87. case NOTIFICATION_ENTER_TREE: {
  88. tool_anim->get_popup()->connect("id_pressed", callable_mp(this, &AnimationPlayerEditor::_animation_tool_menu));
  89. onion_skinning->get_popup()->connect("id_pressed", callable_mp(this, &AnimationPlayerEditor::_onion_skinning_menu));
  90. blend_editor.next->connect("item_selected", callable_mp(this, &AnimationPlayerEditor::_blend_editor_next_changed));
  91. get_tree()->connect("node_removed", callable_mp(this, &AnimationPlayerEditor::_node_removed));
  92. add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("panel"), SNAME("Panel")));
  93. } break;
  94. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  95. add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("panel"), SNAME("Panel")));
  96. } break;
  97. case NOTIFICATION_TRANSLATION_CHANGED:
  98. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  99. case NOTIFICATION_THEME_CHANGED: {
  100. autoplay->set_icon(get_theme_icon(SNAME("AutoPlay"), SNAME("EditorIcons")));
  101. play->set_icon(get_theme_icon(SNAME("PlayStart"), SNAME("EditorIcons")));
  102. play_from->set_icon(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")));
  103. play_bw->set_icon(get_theme_icon(SNAME("PlayStartBackwards"), SNAME("EditorIcons")));
  104. play_bw_from->set_icon(get_theme_icon(SNAME("PlayBackwards"), SNAME("EditorIcons")));
  105. autoplay_icon = get_theme_icon(SNAME("AutoPlay"), SNAME("EditorIcons"));
  106. reset_icon = get_theme_icon(SNAME("Reload"), SNAME("EditorIcons"));
  107. {
  108. Ref<Image> autoplay_img = autoplay_icon->get_image();
  109. Ref<Image> reset_img = reset_icon->get_image();
  110. Ref<Image> autoplay_reset_img;
  111. Size2 icon_size = autoplay_img->get_size();
  112. autoplay_reset_img.instantiate();
  113. autoplay_reset_img->create(icon_size.x * 2, icon_size.y, false, autoplay_img->get_format());
  114. autoplay_reset_img->blit_rect(autoplay_img, Rect2i(Point2i(), icon_size), Point2i());
  115. autoplay_reset_img->blit_rect(reset_img, Rect2i(Point2i(), icon_size), Point2i(icon_size.x, 0));
  116. autoplay_reset_icon.instantiate();
  117. autoplay_reset_icon->set_image(autoplay_reset_img);
  118. }
  119. stop->set_icon(get_theme_icon(SNAME("Stop"), SNAME("EditorIcons")));
  120. onion_toggle->set_icon(get_theme_icon(SNAME("Onion"), SNAME("EditorIcons")));
  121. onion_skinning->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  122. pin->set_icon(get_theme_icon(SNAME("Pin"), SNAME("EditorIcons")));
  123. tool_anim->add_theme_style_override("normal", get_theme_stylebox(SNAME("normal"), SNAME("Button")));
  124. track_editor->get_edit_menu()->add_theme_style_override("normal", get_theme_stylebox(SNAME("normal"), SNAME("Button")));
  125. #define ITEM_ICON(m_item, m_icon) tool_anim->get_popup()->set_item_icon(tool_anim->get_popup()->get_item_index(m_item), get_theme_icon(SNAME(m_icon), SNAME("EditorIcons")))
  126. ITEM_ICON(TOOL_NEW_ANIM, "New");
  127. ITEM_ICON(TOOL_ANIM_LIBRARY, "AnimationLibrary");
  128. ITEM_ICON(TOOL_DUPLICATE_ANIM, "Duplicate");
  129. ITEM_ICON(TOOL_RENAME_ANIM, "Rename");
  130. ITEM_ICON(TOOL_EDIT_TRANSITIONS, "Blend");
  131. ITEM_ICON(TOOL_EDIT_RESOURCE, "Edit");
  132. ITEM_ICON(TOOL_REMOVE_ANIM, "Remove");
  133. _update_animation_list_icons();
  134. } break;
  135. }
  136. }
  137. void AnimationPlayerEditor::_autoplay_pressed() {
  138. if (updating) {
  139. return;
  140. }
  141. if (animation->has_selectable_items() == 0) {
  142. return;
  143. }
  144. String current = animation->get_item_text(animation->get_selected());
  145. if (player->get_autoplay() == current) {
  146. //unset
  147. undo_redo->create_action(TTR("Toggle Autoplay"));
  148. undo_redo->add_do_method(player, "set_autoplay", "");
  149. undo_redo->add_undo_method(player, "set_autoplay", player->get_autoplay());
  150. undo_redo->add_do_method(this, "_animation_player_changed", player);
  151. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  152. undo_redo->commit_action();
  153. } else {
  154. //set
  155. undo_redo->create_action(TTR("Toggle Autoplay"));
  156. undo_redo->add_do_method(player, "set_autoplay", current);
  157. undo_redo->add_undo_method(player, "set_autoplay", player->get_autoplay());
  158. undo_redo->add_do_method(this, "_animation_player_changed", player);
  159. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  160. undo_redo->commit_action();
  161. }
  162. }
  163. void AnimationPlayerEditor::_play_pressed() {
  164. String current = _get_current();
  165. if (!current.is_empty()) {
  166. if (current == player->get_assigned_animation()) {
  167. player->stop(); //so it won't blend with itself
  168. }
  169. player->play(current);
  170. }
  171. //unstop
  172. stop->set_pressed(false);
  173. }
  174. void AnimationPlayerEditor::_play_from_pressed() {
  175. String current = _get_current();
  176. if (!current.is_empty()) {
  177. float time = player->get_current_animation_position();
  178. if (current == player->get_assigned_animation() && player->is_playing()) {
  179. player->stop(); //so it won't blend with itself
  180. }
  181. player->play(current);
  182. player->seek(time);
  183. }
  184. //unstop
  185. stop->set_pressed(false);
  186. }
  187. String AnimationPlayerEditor::_get_current() const {
  188. String current;
  189. if (animation->get_selected() >= 0 && animation->get_selected() < animation->get_item_count() && !animation->is_item_separator(animation->get_selected())) {
  190. current = animation->get_item_text(animation->get_selected());
  191. }
  192. return current;
  193. }
  194. void AnimationPlayerEditor::_play_bw_pressed() {
  195. String current = _get_current();
  196. if (!current.is_empty()) {
  197. if (current == player->get_assigned_animation()) {
  198. player->stop(); //so it won't blend with itself
  199. }
  200. player->play(current, -1, -1, true);
  201. }
  202. //unstop
  203. stop->set_pressed(false);
  204. }
  205. void AnimationPlayerEditor::_play_bw_from_pressed() {
  206. String current = _get_current();
  207. if (!current.is_empty()) {
  208. float time = player->get_current_animation_position();
  209. if (current == player->get_assigned_animation()) {
  210. player->stop(); //so it won't blend with itself
  211. }
  212. player->play(current, -1, -1, true);
  213. player->seek(time);
  214. }
  215. //unstop
  216. stop->set_pressed(false);
  217. }
  218. void AnimationPlayerEditor::_stop_pressed() {
  219. if (!player) {
  220. return;
  221. }
  222. player->stop(false);
  223. play->set_pressed(false);
  224. stop->set_pressed(true);
  225. }
  226. void AnimationPlayerEditor::_animation_selected(int p_which) {
  227. if (updating) {
  228. return;
  229. }
  230. // when selecting an animation, the idea is that the only interesting behavior
  231. // ui-wise is that it should play/blend the next one if currently playing
  232. String current = _get_current();
  233. if (!current.is_empty()) {
  234. player->set_assigned_animation(current);
  235. Ref<Animation> anim = player->get_animation(current);
  236. {
  237. bool animation_library_is_foreign = false;
  238. if (!anim->get_path().is_resource_file()) {
  239. int srpos = anim->get_path().find("::");
  240. if (srpos != -1) {
  241. String base = anim->get_path().substr(0, srpos);
  242. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  243. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  244. animation_library_is_foreign = true;
  245. }
  246. } else {
  247. if (FileAccess::exists(base + ".import")) {
  248. animation_library_is_foreign = true;
  249. }
  250. }
  251. }
  252. } else {
  253. if (FileAccess::exists(anim->get_path() + ".import")) {
  254. animation_library_is_foreign = true;
  255. }
  256. }
  257. track_editor->set_animation(anim, animation_library_is_foreign);
  258. Node *root = player->get_node(player->get_root());
  259. if (root) {
  260. track_editor->set_root(root);
  261. }
  262. }
  263. frame->set_max((double)anim->get_length());
  264. } else {
  265. track_editor->set_animation(Ref<Animation>(), true);
  266. track_editor->set_root(nullptr);
  267. }
  268. autoplay->set_pressed(current == player->get_autoplay());
  269. AnimationPlayerEditor::get_singleton()->get_track_editor()->update_keying();
  270. _animation_key_editor_seek(timeline_position, false);
  271. }
  272. void AnimationPlayerEditor::_animation_new() {
  273. int count = 1;
  274. String base = TTR("New Anim");
  275. String current_library_name = "";
  276. if (animation->has_selectable_items()) {
  277. String current_animation_name = animation->get_item_text(animation->get_selected());
  278. Ref<Animation> current_animation = player->get_animation(current_animation_name);
  279. if (current_animation.is_valid()) {
  280. current_library_name = player->find_animation_library(current_animation);
  281. }
  282. }
  283. String attempt_prefix = (current_library_name == "") ? "" : current_library_name + "/";
  284. while (true) {
  285. String attempt = base;
  286. if (count > 1) {
  287. attempt += " (" + itos(count) + ")";
  288. }
  289. if (player->has_animation(attempt_prefix + attempt)) {
  290. count++;
  291. continue;
  292. }
  293. base = attempt;
  294. break;
  295. }
  296. _update_name_dialog_library_dropdown();
  297. name_dialog_op = TOOL_NEW_ANIM;
  298. name_dialog->set_title(TTR("Create New Animation"));
  299. name_dialog->popup_centered(Size2(300, 90));
  300. name_title->set_text(TTR("New Animation Name:"));
  301. name->set_text(base);
  302. name->select_all();
  303. name->grab_focus();
  304. }
  305. void AnimationPlayerEditor::_animation_rename() {
  306. if (!animation->has_selectable_items()) {
  307. return;
  308. }
  309. int selected = animation->get_selected();
  310. String selected_name = animation->get_item_text(selected);
  311. // Remove library prefix if present.
  312. if (selected_name.contains("/")) {
  313. selected_name = selected_name.get_slice("/", 1);
  314. }
  315. name_dialog->set_title(TTR("Rename Animation"));
  316. name_title->set_text(TTR("Change Animation Name:"));
  317. name->set_text(selected_name);
  318. name_dialog_op = TOOL_RENAME_ANIM;
  319. name_dialog->popup_centered(Size2(300, 90));
  320. name->select_all();
  321. name->grab_focus();
  322. library->hide();
  323. }
  324. void AnimationPlayerEditor::_animation_remove() {
  325. if (!animation->has_selectable_items()) {
  326. return;
  327. }
  328. String current = animation->get_item_text(animation->get_selected());
  329. delete_dialog->set_text(vformat(TTR("Delete Animation '%s'?"), current));
  330. delete_dialog->popup_centered();
  331. }
  332. void AnimationPlayerEditor::_animation_remove_confirmed() {
  333. String current = animation->get_item_text(animation->get_selected());
  334. Ref<Animation> anim = player->get_animation(current);
  335. Ref<AnimationLibrary> al = player->get_animation_library(player->find_animation_library(anim));
  336. ERR_FAIL_COND(al.is_null());
  337. // For names of form lib_name/anim_name, remove library name prefix.
  338. if (current.contains("/")) {
  339. current = current.get_slice("/", 1);
  340. }
  341. undo_redo->create_action(TTR("Remove Animation"));
  342. if (player->get_autoplay() == current) {
  343. undo_redo->add_do_method(player, "set_autoplay", "");
  344. undo_redo->add_undo_method(player, "set_autoplay", current);
  345. // Avoid having the autoplay icon linger around if there is only one animation in the player.
  346. undo_redo->add_do_method(this, "_animation_player_changed", player);
  347. }
  348. undo_redo->add_do_method(al.ptr(), "remove_animation", current);
  349. undo_redo->add_undo_method(al.ptr(), "add_animation", current, anim);
  350. undo_redo->add_do_method(this, "_animation_player_changed", player);
  351. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  352. if (animation->has_selectable_items() && animation->get_selectable_item(false) == animation->get_selectable_item(true)) { // Last item remaining.
  353. undo_redo->add_do_method(this, "_stop_onion_skinning");
  354. undo_redo->add_undo_method(this, "_start_onion_skinning");
  355. }
  356. undo_redo->commit_action();
  357. }
  358. void AnimationPlayerEditor::_select_anim_by_name(const String &p_anim) {
  359. int idx = -1;
  360. for (int i = 0; i < animation->get_item_count(); i++) {
  361. if (animation->get_item_text(i) == p_anim) {
  362. idx = i;
  363. break;
  364. }
  365. }
  366. ERR_FAIL_COND(idx == -1);
  367. animation->select(idx);
  368. _animation_selected(idx);
  369. }
  370. double AnimationPlayerEditor::_get_editor_step() const {
  371. // Returns the effective snapping value depending on snapping modifiers, or 0 if snapping is disabled.
  372. if (track_editor->is_snap_enabled()) {
  373. const String current = player->get_assigned_animation();
  374. const Ref<Animation> anim = player->get_animation(current);
  375. ERR_FAIL_COND_V(!anim.is_valid(), 0.0);
  376. // Use more precise snapping when holding Shift
  377. return Input::get_singleton()->is_key_pressed(Key::SHIFT) ? anim->get_step() * 0.25 : anim->get_step();
  378. }
  379. return 0.0;
  380. }
  381. void AnimationPlayerEditor::_animation_name_edited() {
  382. player->stop();
  383. String new_name = name->get_text();
  384. if (!AnimationLibrary::is_valid_animation_name(new_name)) {
  385. error_dialog->set_text(TTR("Invalid animation name!"));
  386. error_dialog->popup_centered();
  387. return;
  388. }
  389. if (name_dialog_op == TOOL_RENAME_ANIM && animation->has_selectable_items() && animation->get_item_text(animation->get_selected()) == new_name) {
  390. name_dialog->hide();
  391. return;
  392. }
  393. String test_name_prefix = "";
  394. if (library->is_visible() && library->get_selected_id() != -1) {
  395. test_name_prefix = library->get_item_metadata(library->get_selected_id());
  396. test_name_prefix += (test_name_prefix != "") ? "/" : "";
  397. }
  398. if (player->has_animation(test_name_prefix + new_name)) {
  399. error_dialog->set_text(vformat(TTR("Animation '%s' already exists!"), test_name_prefix + new_name));
  400. error_dialog->popup_centered();
  401. return;
  402. }
  403. switch (name_dialog_op) {
  404. case TOOL_RENAME_ANIM: {
  405. String current = animation->get_item_text(animation->get_selected());
  406. Ref<Animation> anim = player->get_animation(current);
  407. Ref<AnimationLibrary> al = player->get_animation_library(player->find_animation_library(anim));
  408. ERR_FAIL_COND(al.is_null());
  409. // Extract library prefix if present.
  410. String new_library_prefix = "";
  411. if (current.contains("/")) {
  412. new_library_prefix = current.get_slice("/", 0) + "/";
  413. current = current.get_slice("/", 1);
  414. }
  415. undo_redo->create_action(TTR("Rename Animation"));
  416. undo_redo->add_do_method(al.ptr(), "rename_animation", current, new_name);
  417. undo_redo->add_do_method(anim.ptr(), "set_name", new_name);
  418. undo_redo->add_undo_method(al.ptr(), "rename_animation", new_name, current);
  419. undo_redo->add_undo_method(anim.ptr(), "set_name", current);
  420. undo_redo->add_do_method(this, "_animation_player_changed", player);
  421. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  422. undo_redo->commit_action();
  423. _select_anim_by_name(new_library_prefix + new_name);
  424. } break;
  425. case TOOL_NEW_ANIM: {
  426. Ref<Animation> new_anim = Ref<Animation>(memnew(Animation));
  427. new_anim->set_name(new_name);
  428. String library_name;
  429. Ref<AnimationLibrary> al;
  430. if (library->is_visible()) {
  431. library_name = library->get_item_metadata(library->get_selected());
  432. // It's possible that [Global] was selected, but doesn't exist yet.
  433. if (player->has_animation_library(library_name)) {
  434. al = player->get_animation_library(library_name);
  435. }
  436. } else {
  437. if (player->has_animation_library("")) {
  438. al = player->get_animation_library("");
  439. library_name = "";
  440. }
  441. }
  442. undo_redo->create_action(TTR("Add Animation"));
  443. bool lib_added = false;
  444. if (al.is_null()) {
  445. al.instantiate();
  446. lib_added = true;
  447. undo_redo->add_do_method(player, "add_animation_library", "", al);
  448. library_name = "";
  449. }
  450. undo_redo->add_do_method(al.ptr(), "add_animation", new_name, new_anim);
  451. undo_redo->add_undo_method(al.ptr(), "remove_animation", new_name);
  452. undo_redo->add_do_method(this, "_animation_player_changed", player);
  453. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  454. if (!animation->has_selectable_items()) {
  455. undo_redo->add_do_method(this, "_start_onion_skinning");
  456. undo_redo->add_undo_method(this, "_stop_onion_skinning");
  457. }
  458. if (lib_added) {
  459. undo_redo->add_undo_method(player, "remove_animation_library", "");
  460. }
  461. undo_redo->commit_action();
  462. if (library_name != "") {
  463. library_name = library_name + "/";
  464. }
  465. _select_anim_by_name(library_name + new_name);
  466. } break;
  467. case TOOL_DUPLICATE_ANIM: {
  468. String current = animation->get_item_text(animation->get_selected());
  469. Ref<Animation> anim = player->get_animation(current);
  470. Ref<Animation> new_anim = _animation_clone(anim);
  471. new_anim->set_name(new_name);
  472. String library_name;
  473. Ref<AnimationLibrary> al;
  474. if (library->is_visible()) {
  475. library_name = library->get_item_metadata(library->get_selected());
  476. // It's possible that [Global] was selected, but doesn't exist yet.
  477. if (player->has_animation_library(library_name)) {
  478. al = player->get_animation_library(library_name);
  479. }
  480. } else {
  481. if (player->has_animation_library("")) {
  482. al = player->get_animation_library("");
  483. library_name = "";
  484. }
  485. }
  486. undo_redo->create_action(TTR("Duplicate Animation"));
  487. bool lib_added = false;
  488. if (al.is_null()) {
  489. al.instantiate();
  490. lib_added = true;
  491. undo_redo->add_do_method(player, "add_animation_library", "", al);
  492. library_name = "";
  493. }
  494. undo_redo->add_do_method(al.ptr(), "add_animation", new_name, new_anim);
  495. undo_redo->add_undo_method(al.ptr(), "remove_animation", new_name);
  496. undo_redo->add_do_method(this, "_animation_player_changed", player);
  497. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  498. if (lib_added) {
  499. undo_redo->add_undo_method(player, "remove_animation_library", "");
  500. }
  501. undo_redo->commit_action();
  502. if (library_name != "") {
  503. library_name = library_name + "/";
  504. }
  505. _select_anim_by_name(library_name + new_name);
  506. } break;
  507. }
  508. name_dialog->hide();
  509. }
  510. void AnimationPlayerEditor::_blend_editor_next_changed(const int p_idx) {
  511. if (!animation->has_selectable_items()) {
  512. return;
  513. }
  514. String current = animation->get_item_text(animation->get_selected());
  515. undo_redo->create_action(TTR("Blend Next Changed"));
  516. undo_redo->add_do_method(player, "animation_set_next", current, blend_editor.next->get_item_text(p_idx));
  517. undo_redo->add_undo_method(player, "animation_set_next", current, player->animation_get_next(current));
  518. undo_redo->add_do_method(this, "_animation_player_changed", player);
  519. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  520. undo_redo->commit_action();
  521. }
  522. void AnimationPlayerEditor::_animation_blend() {
  523. if (updating_blends) {
  524. return;
  525. }
  526. blend_editor.tree->clear();
  527. if (!animation->has_selectable_items()) {
  528. return;
  529. }
  530. String current = animation->get_item_text(animation->get_selected());
  531. blend_editor.dialog->popup_centered(Size2(400, 400) * EDSCALE);
  532. blend_editor.tree->set_hide_root(true);
  533. blend_editor.tree->set_column_expand_ratio(0, 10);
  534. blend_editor.tree->set_column_clip_content(0, true);
  535. blend_editor.tree->set_column_expand_ratio(1, 3);
  536. blend_editor.tree->set_column_clip_content(1, true);
  537. List<StringName> anims;
  538. player->get_animation_list(&anims);
  539. TreeItem *root = blend_editor.tree->create_item();
  540. updating_blends = true;
  541. int i = 0;
  542. bool anim_found = false;
  543. blend_editor.next->clear();
  544. blend_editor.next->add_item("", i);
  545. for (const StringName &to : anims) {
  546. TreeItem *blend = blend_editor.tree->create_item(root);
  547. blend->set_editable(0, false);
  548. blend->set_editable(1, true);
  549. blend->set_text(0, to);
  550. blend->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  551. blend->set_range_config(1, 0, 3600, 0.001);
  552. blend->set_range(1, player->get_blend_time(current, to));
  553. i++;
  554. blend_editor.next->add_item(to, i);
  555. if (to == player->animation_get_next(current)) {
  556. blend_editor.next->select(i);
  557. anim_found = true;
  558. }
  559. }
  560. // make sure we reset it else it becomes out of sync and could contain a deleted animation
  561. if (!anim_found) {
  562. blend_editor.next->select(0);
  563. player->animation_set_next(current, blend_editor.next->get_item_text(0));
  564. }
  565. updating_blends = false;
  566. }
  567. void AnimationPlayerEditor::_blend_edited() {
  568. if (updating_blends) {
  569. return;
  570. }
  571. if (!animation->has_selectable_items()) {
  572. return;
  573. }
  574. String current = animation->get_item_text(animation->get_selected());
  575. TreeItem *selected = blend_editor.tree->get_edited();
  576. if (!selected) {
  577. return;
  578. }
  579. updating_blends = true;
  580. String to = selected->get_text(0);
  581. float blend_time = selected->get_range(1);
  582. float prev_blend_time = player->get_blend_time(current, to);
  583. undo_redo->create_action(TTR("Change Blend Time"));
  584. undo_redo->add_do_method(player, "set_blend_time", current, to, blend_time);
  585. undo_redo->add_undo_method(player, "set_blend_time", current, to, prev_blend_time);
  586. undo_redo->add_do_method(this, "_animation_player_changed", player);
  587. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  588. undo_redo->commit_action();
  589. updating_blends = false;
  590. }
  591. void AnimationPlayerEditor::ensure_visibility() {
  592. if (player && pin->is_pressed()) {
  593. return; // another player is pinned, don't reset
  594. }
  595. _animation_edit();
  596. }
  597. Dictionary AnimationPlayerEditor::get_state() const {
  598. Dictionary d;
  599. d["visible"] = is_visible_in_tree();
  600. if (EditorNode::get_singleton()->get_edited_scene() && is_visible_in_tree() && player) {
  601. d["player"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(player);
  602. d["animation"] = player->get_assigned_animation();
  603. d["track_editor_state"] = track_editor->get_state();
  604. }
  605. return d;
  606. }
  607. void AnimationPlayerEditor::set_state(const Dictionary &p_state) {
  608. if (!p_state.has("visible") || !p_state["visible"]) {
  609. return;
  610. }
  611. if (!EditorNode::get_singleton()->get_edited_scene()) {
  612. return;
  613. }
  614. if (p_state.has("player")) {
  615. Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
  616. if (Object::cast_to<AnimationPlayer>(n) && EditorNode::get_singleton()->get_editor_selection()->is_selected(n)) {
  617. player = Object::cast_to<AnimationPlayer>(n);
  618. _update_player();
  619. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  620. set_process(true);
  621. ensure_visibility();
  622. if (p_state.has("animation")) {
  623. String anim = p_state["animation"];
  624. if (!anim.is_empty() && player->has_animation(anim)) {
  625. _select_anim_by_name(anim);
  626. _animation_edit();
  627. }
  628. }
  629. }
  630. }
  631. if (p_state.has("track_editor_state")) {
  632. track_editor->set_state(p_state["track_editor_state"]);
  633. }
  634. }
  635. void AnimationPlayerEditor::_animation_resource_edit() {
  636. String current = _get_current();
  637. if (current != String()) {
  638. Ref<Animation> anim = player->get_animation(current);
  639. EditorNode::get_singleton()->edit_resource(anim);
  640. }
  641. }
  642. void AnimationPlayerEditor::_animation_edit() {
  643. String current = _get_current();
  644. if (current != String()) {
  645. Ref<Animation> anim = player->get_animation(current);
  646. bool animation_library_is_foreign = false;
  647. if (!anim->get_path().is_resource_file()) {
  648. int srpos = anim->get_path().find("::");
  649. if (srpos != -1) {
  650. String base = anim->get_path().substr(0, srpos);
  651. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  652. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  653. animation_library_is_foreign = true;
  654. }
  655. } else {
  656. if (FileAccess::exists(base + ".import")) {
  657. animation_library_is_foreign = true;
  658. }
  659. }
  660. }
  661. } else {
  662. if (FileAccess::exists(anim->get_path() + ".import")) {
  663. animation_library_is_foreign = true;
  664. }
  665. }
  666. track_editor->set_animation(anim, animation_library_is_foreign);
  667. Node *root = player->get_node(player->get_root());
  668. if (root) {
  669. track_editor->set_root(root);
  670. }
  671. } else {
  672. track_editor->set_animation(Ref<Animation>(), true);
  673. track_editor->set_root(nullptr);
  674. }
  675. }
  676. void AnimationPlayerEditor::_scale_changed(const String &p_scale) {
  677. player->set_speed_scale(p_scale.to_float());
  678. }
  679. void AnimationPlayerEditor::_update_animation() {
  680. // the purpose of _update_animation is to reflect the current state
  681. // of the animation player in the current editor..
  682. updating = true;
  683. if (player->is_playing()) {
  684. play->set_pressed(true);
  685. stop->set_pressed(false);
  686. } else {
  687. play->set_pressed(false);
  688. stop->set_pressed(true);
  689. }
  690. scale->set_text(String::num(player->get_speed_scale(), 2));
  691. String current = player->get_assigned_animation();
  692. for (int i = 0; i < animation->get_item_count(); i++) {
  693. if (animation->get_item_text(i) == current) {
  694. animation->select(i);
  695. break;
  696. }
  697. }
  698. updating = false;
  699. }
  700. void AnimationPlayerEditor::_update_player() {
  701. updating = true;
  702. animation->clear();
  703. tool_anim->set_disabled(player == nullptr);
  704. pin->set_disabled(player == nullptr);
  705. if (!player) {
  706. AnimationPlayerEditor::get_singleton()->get_track_editor()->update_keying();
  707. return;
  708. }
  709. List<StringName> libraries;
  710. player->get_animation_library_list(&libraries);
  711. int active_idx = -1;
  712. bool no_anims_found = true;
  713. bool foreign_global_anim_lib = false;
  714. for (const StringName &K : libraries) {
  715. if (K != StringName()) {
  716. animation->add_separator(K);
  717. }
  718. // Check if the global library is foreign since we want to disable options for adding/remove/renaming animations if it is.
  719. Ref<AnimationLibrary> library = player->get_animation_library(K);
  720. if (K == "") {
  721. if (!library->get_path().is_resource_file()) {
  722. int srpos = library->get_path().find("::");
  723. if (srpos != -1) {
  724. String base = library->get_path().substr(0, srpos);
  725. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  726. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  727. foreign_global_anim_lib = true;
  728. }
  729. } else {
  730. if (FileAccess::exists(base + ".import")) {
  731. foreign_global_anim_lib = true;
  732. }
  733. }
  734. }
  735. } else {
  736. if (FileAccess::exists(library->get_path() + ".import")) {
  737. foreign_global_anim_lib = true;
  738. }
  739. }
  740. }
  741. List<StringName> animlist;
  742. library->get_animation_list(&animlist);
  743. for (const StringName &E : animlist) {
  744. String path = K;
  745. if (path != "") {
  746. path += "/";
  747. }
  748. path += E;
  749. animation->add_item(path);
  750. if (player->get_assigned_animation() == path) {
  751. active_idx = animation->get_selectable_item(true);
  752. }
  753. no_anims_found = false;
  754. }
  755. }
  756. #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), foreign_global_anim_lib)
  757. ITEM_CHECK_DISABLED(TOOL_NEW_ANIM);
  758. #undef ITEM_CHECK_DISABLED
  759. #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), no_anims_found || foreign_global_anim_lib)
  760. ITEM_CHECK_DISABLED(TOOL_DUPLICATE_ANIM);
  761. ITEM_CHECK_DISABLED(TOOL_RENAME_ANIM);
  762. ITEM_CHECK_DISABLED(TOOL_EDIT_TRANSITIONS);
  763. ITEM_CHECK_DISABLED(TOOL_REMOVE_ANIM);
  764. ITEM_CHECK_DISABLED(TOOL_EDIT_RESOURCE);
  765. #undef ITEM_CHECK_DISABLED
  766. stop->set_disabled(no_anims_found);
  767. play->set_disabled(no_anims_found);
  768. play_bw->set_disabled(no_anims_found);
  769. play_bw_from->set_disabled(no_anims_found);
  770. play_from->set_disabled(no_anims_found);
  771. frame->set_editable(!no_anims_found);
  772. animation->set_disabled(no_anims_found);
  773. autoplay->set_disabled(no_anims_found);
  774. onion_toggle->set_disabled(no_anims_found);
  775. onion_skinning->set_disabled(no_anims_found);
  776. _update_animation_list_icons();
  777. updating = false;
  778. if (active_idx != -1) {
  779. animation->select(active_idx);
  780. autoplay->set_pressed(animation->get_item_text(active_idx) == player->get_autoplay());
  781. _animation_selected(active_idx);
  782. } else if (animation->has_selectable_items()) {
  783. int item = animation->get_selectable_item();
  784. animation->select(item);
  785. autoplay->set_pressed(animation->get_item_text(item) == player->get_autoplay());
  786. _animation_selected(item);
  787. } else {
  788. _animation_selected(0);
  789. }
  790. if (!no_anims_found) {
  791. String current = animation->get_item_text(animation->get_selected());
  792. Ref<Animation> anim = player->get_animation(current);
  793. bool animation_library_is_foreign = false;
  794. if (!anim->get_path().is_resource_file()) {
  795. int srpos = anim->get_path().find("::");
  796. if (srpos != -1) {
  797. String base = anim->get_path().substr(0, srpos);
  798. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  799. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  800. animation_library_is_foreign = true;
  801. }
  802. } else {
  803. if (FileAccess::exists(base + ".import")) {
  804. animation_library_is_foreign = true;
  805. }
  806. }
  807. }
  808. } else {
  809. if (FileAccess::exists(anim->get_path() + ".import")) {
  810. animation_library_is_foreign = true;
  811. }
  812. }
  813. track_editor->set_animation(anim, animation_library_is_foreign);
  814. Node *root = player->get_node(player->get_root());
  815. if (root) {
  816. track_editor->set_root(root);
  817. }
  818. }
  819. _update_animation();
  820. }
  821. void AnimationPlayerEditor::_update_animation_list_icons() {
  822. for (int i = 0; i < animation->get_item_count(); i++) {
  823. String name = animation->get_item_text(i);
  824. if (animation->is_item_disabled(i) || animation->is_item_separator(i)) {
  825. continue;
  826. }
  827. Ref<Texture2D> icon;
  828. if (name == player->get_autoplay()) {
  829. if (name == SceneStringNames::get_singleton()->RESET) {
  830. icon = autoplay_reset_icon;
  831. } else {
  832. icon = autoplay_icon;
  833. }
  834. } else if (name == SceneStringNames::get_singleton()->RESET) {
  835. icon = reset_icon;
  836. }
  837. animation->set_item_icon(i, icon);
  838. }
  839. }
  840. void AnimationPlayerEditor::_update_name_dialog_library_dropdown() {
  841. StringName current_library_name = StringName();
  842. if (animation->has_selectable_items()) {
  843. String current_animation_name = animation->get_item_text(animation->get_selected());
  844. Ref<Animation> current_animation = player->get_animation(current_animation_name);
  845. if (current_animation.is_valid()) {
  846. current_library_name = player->find_animation_library(current_animation);
  847. }
  848. }
  849. List<StringName> libraries;
  850. player->get_animation_library_list(&libraries);
  851. library->clear();
  852. // When [Global] isn't present, but other libraries are, add option of creating [Global].
  853. int index_offset = 0;
  854. if (!player->has_animation_library(StringName())) {
  855. library->add_item(String(TTR("[Global] (create)")));
  856. library->set_item_metadata(0, "");
  857. index_offset = 1;
  858. }
  859. int current_lib_id = index_offset; // Don't default to [Global] if it doesn't exist yet.
  860. for (int i = 0; i < libraries.size(); i++) {
  861. StringName library_name = libraries[i];
  862. library->add_item((library_name == StringName()) ? String(TTR("[Global]")) : String(library_name));
  863. library->set_item_metadata(i + index_offset, String(library_name));
  864. // Default to duplicating into same library.
  865. if (library_name == current_library_name) {
  866. current_lib_id = i + index_offset;
  867. }
  868. }
  869. if (library->get_item_count() > 1) {
  870. library->select(current_lib_id);
  871. library->show();
  872. } else {
  873. library->hide();
  874. }
  875. }
  876. void AnimationPlayerEditor::set_undo_redo(Ref<EditorUndoRedoManager> p_undo_redo) {
  877. undo_redo = p_undo_redo;
  878. }
  879. void AnimationPlayerEditor::edit(AnimationPlayer *p_player) {
  880. if (player && pin->is_pressed()) {
  881. return; // Ignore, pinned.
  882. }
  883. player = p_player;
  884. if (player) {
  885. _update_player();
  886. if (onion.enabled) {
  887. if (animation->has_selectable_items()) {
  888. _start_onion_skinning();
  889. } else {
  890. _stop_onion_skinning();
  891. }
  892. }
  893. track_editor->show_select_node_warning(false);
  894. } else {
  895. if (onion.enabled) {
  896. _stop_onion_skinning();
  897. }
  898. track_editor->show_select_node_warning(true);
  899. }
  900. library_editor->set_animation_player(player);
  901. }
  902. void AnimationPlayerEditor::forward_force_draw_over_viewport(Control *p_overlay) {
  903. if (!onion.can_overlay) {
  904. return;
  905. }
  906. // Can happen on viewport resize, at least.
  907. if (!_are_onion_layers_valid()) {
  908. return;
  909. }
  910. RID ci = p_overlay->get_canvas_item();
  911. Rect2 src_rect = p_overlay->get_global_rect();
  912. // Re-flip since captures are already flipped.
  913. src_rect.position.y = onion.capture_size.y - (src_rect.position.y + src_rect.size.y);
  914. src_rect.size.y *= -1;
  915. Rect2 dst_rect = Rect2(Point2(), p_overlay->get_size());
  916. float alpha_step = 1.0 / (onion.steps + 1);
  917. int cidx = 0;
  918. if (onion.past) {
  919. float alpha = 0;
  920. do {
  921. alpha += alpha_step;
  922. if (onion.captures_valid[cidx]) {
  923. RS::get_singleton()->canvas_item_add_texture_rect_region(
  924. ci, dst_rect, RS::get_singleton()->viewport_get_texture(onion.captures[cidx]), src_rect, Color(1, 1, 1, alpha));
  925. }
  926. cidx++;
  927. } while (cidx < onion.steps);
  928. }
  929. if (onion.future) {
  930. float alpha = 1;
  931. int base_cidx = cidx;
  932. do {
  933. alpha -= alpha_step;
  934. if (onion.captures_valid[cidx]) {
  935. RS::get_singleton()->canvas_item_add_texture_rect_region(
  936. ci, dst_rect, RS::get_singleton()->viewport_get_texture(onion.captures[cidx]), src_rect, Color(1, 1, 1, alpha));
  937. }
  938. cidx++;
  939. } while (cidx < base_cidx + onion.steps); // In case there's the present capture at the end, skip it.
  940. }
  941. }
  942. void AnimationPlayerEditor::_animation_duplicate() {
  943. if (!animation->has_selectable_items()) {
  944. return;
  945. }
  946. String current = animation->get_item_text(animation->get_selected());
  947. Ref<Animation> anim = player->get_animation(current);
  948. if (!anim.is_valid()) {
  949. return;
  950. }
  951. String new_name = current;
  952. while (player->has_animation(new_name)) {
  953. new_name = new_name + " (copy)";
  954. }
  955. if (new_name.contains("/")) {
  956. // Discard library prefix.
  957. new_name = new_name.get_slice("/", 1);
  958. }
  959. _update_name_dialog_library_dropdown();
  960. name_dialog_op = TOOL_DUPLICATE_ANIM;
  961. name_dialog->set_title(TTR("Duplicate Animation"));
  962. name_title->set_text(TTR("Duplicated Animation Name:"));
  963. name->set_text(new_name);
  964. name_dialog->popup_centered(Size2(300, 90));
  965. name->select_all();
  966. name->grab_focus();
  967. }
  968. Ref<Animation> AnimationPlayerEditor::_animation_clone(Ref<Animation> p_anim) {
  969. Ref<Animation> new_anim = memnew(Animation);
  970. List<PropertyInfo> plist;
  971. p_anim->get_property_list(&plist);
  972. for (const PropertyInfo &E : plist) {
  973. if (E.usage & PROPERTY_USAGE_STORAGE) {
  974. new_anim->set(E.name, p_anim->get(E.name));
  975. }
  976. }
  977. new_anim->set_path("");
  978. return new_anim;
  979. }
  980. void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_set, bool p_timeline_only) {
  981. if (updating || !player || player->is_playing()) {
  982. return;
  983. };
  984. updating = true;
  985. String current = player->get_assigned_animation();
  986. if (current.is_empty() || !player->has_animation(current)) {
  987. updating = false;
  988. current = "";
  989. return;
  990. };
  991. Ref<Animation> anim;
  992. anim = player->get_animation(current);
  993. float pos = CLAMP((double)anim->get_length() * (p_value / frame->get_max()), 0, (double)anim->get_length());
  994. if (track_editor->is_snap_enabled()) {
  995. pos = Math::snapped(pos, _get_editor_step());
  996. }
  997. if (!p_timeline_only) {
  998. if (player->is_valid() && !p_set) {
  999. float cpos = player->get_current_animation_position();
  1000. player->seek_delta(pos, pos - cpos);
  1001. } else {
  1002. player->stop(true);
  1003. player->seek(pos, true);
  1004. }
  1005. }
  1006. track_editor->set_anim_pos(pos);
  1007. };
  1008. void AnimationPlayerEditor::_animation_player_changed(Object *p_pl) {
  1009. if (player == p_pl && is_visible_in_tree()) {
  1010. _update_player();
  1011. if (blend_editor.dialog->is_visible()) {
  1012. _animation_blend(); // Update.
  1013. }
  1014. if (library_editor->is_visible()) {
  1015. library_editor->update_tree();
  1016. }
  1017. }
  1018. }
  1019. void AnimationPlayerEditor::_list_changed() {
  1020. if (is_visible_in_tree()) {
  1021. _update_player();
  1022. }
  1023. }
  1024. void AnimationPlayerEditor::_animation_key_editor_anim_len_changed(float p_len) {
  1025. frame->set_max(p_len);
  1026. }
  1027. void AnimationPlayerEditor::_animation_key_editor_seek(float p_pos, bool p_drag, bool p_timeline_only) {
  1028. timeline_position = p_pos;
  1029. if (!is_visible_in_tree()) {
  1030. return;
  1031. }
  1032. if (!player) {
  1033. return;
  1034. }
  1035. if (player->is_playing()) {
  1036. return;
  1037. }
  1038. if (!player->has_animation(player->get_assigned_animation())) {
  1039. return;
  1040. }
  1041. updating = true;
  1042. frame->set_value(Math::snapped(p_pos, _get_editor_step()));
  1043. updating = false;
  1044. _seek_value_changed(p_pos, !p_drag, p_timeline_only);
  1045. }
  1046. void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
  1047. String current = _get_current();
  1048. Ref<Animation> anim;
  1049. if (!current.is_empty()) {
  1050. anim = player->get_animation(current);
  1051. }
  1052. switch (p_option) {
  1053. case TOOL_NEW_ANIM: {
  1054. _animation_new();
  1055. } break;
  1056. case TOOL_ANIM_LIBRARY: {
  1057. library_editor->set_animation_player(player);
  1058. library_editor->show_dialog();
  1059. } break;
  1060. case TOOL_DUPLICATE_ANIM: {
  1061. _animation_duplicate();
  1062. } break;
  1063. case TOOL_RENAME_ANIM: {
  1064. _animation_rename();
  1065. } break;
  1066. case TOOL_EDIT_TRANSITIONS: {
  1067. _animation_blend();
  1068. } break;
  1069. case TOOL_REMOVE_ANIM: {
  1070. _animation_remove();
  1071. } break;
  1072. case TOOL_EDIT_RESOURCE: {
  1073. if (anim.is_valid()) {
  1074. EditorNode::get_singleton()->edit_resource(anim);
  1075. }
  1076. } break;
  1077. }
  1078. }
  1079. void AnimationPlayerEditor::_onion_skinning_menu(int p_option) {
  1080. PopupMenu *menu = onion_skinning->get_popup();
  1081. int idx = menu->get_item_index(p_option);
  1082. switch (p_option) {
  1083. case ONION_SKINNING_ENABLE: {
  1084. onion.enabled = !onion.enabled;
  1085. if (onion.enabled) {
  1086. _start_onion_skinning();
  1087. } else {
  1088. _stop_onion_skinning();
  1089. }
  1090. } break;
  1091. case ONION_SKINNING_PAST: {
  1092. // Ensure at least one of past/future is checked.
  1093. onion.past = onion.future ? !onion.past : true;
  1094. menu->set_item_checked(idx, onion.past);
  1095. } break;
  1096. case ONION_SKINNING_FUTURE: {
  1097. // Ensure at least one of past/future is checked.
  1098. onion.future = onion.past ? !onion.future : true;
  1099. menu->set_item_checked(idx, onion.future);
  1100. } break;
  1101. case ONION_SKINNING_1_STEP: // Fall-through.
  1102. case ONION_SKINNING_2_STEPS:
  1103. case ONION_SKINNING_3_STEPS: {
  1104. onion.steps = (p_option - ONION_SKINNING_1_STEP) + 1;
  1105. int one_frame_idx = menu->get_item_index(ONION_SKINNING_1_STEP);
  1106. for (int i = 0; i <= ONION_SKINNING_LAST_STEPS_OPTION - ONION_SKINNING_1_STEP; i++) {
  1107. menu->set_item_checked(one_frame_idx + i, onion.steps == i + 1);
  1108. }
  1109. } break;
  1110. case ONION_SKINNING_DIFFERENCES_ONLY: {
  1111. onion.differences_only = !onion.differences_only;
  1112. menu->set_item_checked(idx, onion.differences_only);
  1113. } break;
  1114. case ONION_SKINNING_FORCE_WHITE_MODULATE: {
  1115. onion.force_white_modulate = !onion.force_white_modulate;
  1116. menu->set_item_checked(idx, onion.force_white_modulate);
  1117. } break;
  1118. case ONION_SKINNING_INCLUDE_GIZMOS: {
  1119. onion.include_gizmos = !onion.include_gizmos;
  1120. menu->set_item_checked(idx, onion.include_gizmos);
  1121. } break;
  1122. }
  1123. }
  1124. void AnimationPlayerEditor::shortcut_input(const Ref<InputEvent> &p_ev) {
  1125. ERR_FAIL_COND(p_ev.is_null());
  1126. Ref<InputEventKey> k = p_ev;
  1127. 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()) {
  1128. switch (k->get_keycode()) {
  1129. case Key::A: {
  1130. if (!k->is_shift_pressed()) {
  1131. _play_bw_from_pressed();
  1132. } else {
  1133. _play_bw_pressed();
  1134. }
  1135. accept_event();
  1136. } break;
  1137. case Key::S: {
  1138. _stop_pressed();
  1139. accept_event();
  1140. } break;
  1141. case Key::D: {
  1142. if (!k->is_shift_pressed()) {
  1143. _play_from_pressed();
  1144. } else {
  1145. _play_pressed();
  1146. }
  1147. accept_event();
  1148. } break;
  1149. default:
  1150. break;
  1151. }
  1152. }
  1153. }
  1154. void AnimationPlayerEditor::_editor_visibility_changed() {
  1155. if (is_visible() && animation->has_selectable_items()) {
  1156. _start_onion_skinning();
  1157. }
  1158. }
  1159. bool AnimationPlayerEditor::_are_onion_layers_valid() {
  1160. ERR_FAIL_COND_V(!onion.past && !onion.future, false);
  1161. Point2 capture_size = get_tree()->get_root()->get_size();
  1162. return onion.captures.size() == onion.get_needed_capture_count() && onion.capture_size == capture_size;
  1163. }
  1164. void AnimationPlayerEditor::_allocate_onion_layers() {
  1165. _free_onion_layers();
  1166. int captures = onion.get_needed_capture_count();
  1167. Point2 capture_size = get_tree()->get_root()->get_size();
  1168. onion.captures.resize(captures);
  1169. onion.captures_valid.resize(captures);
  1170. for (int i = 0; i < captures; i++) {
  1171. bool is_present = onion.differences_only && i == captures - 1;
  1172. // Each capture is a viewport with a canvas item attached that renders a full-size rect with the contents of the main viewport.
  1173. onion.captures.write[i] = RS::get_singleton()->viewport_create();
  1174. RS::get_singleton()->viewport_set_size(onion.captures[i], capture_size.width, capture_size.height);
  1175. RS::get_singleton()->viewport_set_update_mode(onion.captures[i], RS::VIEWPORT_UPDATE_ALWAYS);
  1176. RS::get_singleton()->viewport_set_transparent_background(onion.captures[i], !is_present);
  1177. RS::get_singleton()->viewport_attach_canvas(onion.captures[i], onion.capture.canvas);
  1178. }
  1179. // Reset the capture canvas item to the current root viewport texture (defensive).
  1180. RS::get_singleton()->canvas_item_clear(onion.capture.canvas_item);
  1181. RS::get_singleton()->canvas_item_add_texture_rect(onion.capture.canvas_item, Rect2(Point2(), capture_size), get_tree()->get_root()->get_texture()->get_rid());
  1182. onion.capture_size = capture_size;
  1183. }
  1184. void AnimationPlayerEditor::_free_onion_layers() {
  1185. for (int i = 0; i < onion.captures.size(); i++) {
  1186. if (onion.captures[i].is_valid()) {
  1187. RS::get_singleton()->free(onion.captures[i]);
  1188. }
  1189. }
  1190. onion.captures.clear();
  1191. onion.captures_valid.clear();
  1192. }
  1193. void AnimationPlayerEditor::_prepare_onion_layers_1() {
  1194. // This would be called per viewport and we want to act once only.
  1195. int64_t frame = get_tree()->get_frame();
  1196. if (frame == onion.last_frame) {
  1197. return;
  1198. }
  1199. if (!onion.enabled || !is_processing() || !is_visible() || !get_player()) {
  1200. _stop_onion_skinning();
  1201. return;
  1202. }
  1203. onion.last_frame = frame;
  1204. // Refresh viewports with no onion layers overlaid.
  1205. onion.can_overlay = false;
  1206. plugin->update_overlays();
  1207. if (player->is_playing()) {
  1208. return;
  1209. }
  1210. // And go to next step afterwards.
  1211. call_deferred(SNAME("_prepare_onion_layers_2"));
  1212. }
  1213. void AnimationPlayerEditor::_prepare_onion_layers_1_deferred() {
  1214. call_deferred(SNAME("_prepare_onion_layers_1"));
  1215. }
  1216. void AnimationPlayerEditor::_prepare_onion_layers_2() {
  1217. Ref<Animation> anim = player->get_animation(player->get_assigned_animation());
  1218. if (!anim.is_valid()) {
  1219. return;
  1220. }
  1221. if (!_are_onion_layers_valid()) {
  1222. _allocate_onion_layers();
  1223. }
  1224. // Hide superfluous elements that would make the overlay unnecessary cluttered.
  1225. Dictionary canvas_edit_state;
  1226. Dictionary spatial_edit_state;
  1227. if (Node3DEditor::get_singleton()->is_visible()) {
  1228. // 3D
  1229. spatial_edit_state = Node3DEditor::get_singleton()->get_state();
  1230. Dictionary new_state = spatial_edit_state.duplicate();
  1231. new_state["show_grid"] = false;
  1232. new_state["show_origin"] = false;
  1233. Array orig_vp = spatial_edit_state["viewports"];
  1234. Array vp;
  1235. vp.resize(4);
  1236. for (int i = 0; i < vp.size(); i++) {
  1237. Dictionary d = ((Dictionary)orig_vp[i]).duplicate();
  1238. d["use_environment"] = false;
  1239. d["doppler"] = false;
  1240. d["gizmos"] = onion.include_gizmos ? d["gizmos"] : Variant(false);
  1241. d["information"] = false;
  1242. vp[i] = d;
  1243. }
  1244. new_state["viewports"] = vp;
  1245. // TODO: Save/restore only affected entries.
  1246. Node3DEditor::get_singleton()->set_state(new_state);
  1247. } else { // CanvasItemEditor
  1248. // 2D
  1249. canvas_edit_state = CanvasItemEditor::get_singleton()->get_state();
  1250. Dictionary new_state = canvas_edit_state.duplicate();
  1251. new_state["show_grid"] = false;
  1252. new_state["show_rulers"] = false;
  1253. new_state["show_guides"] = false;
  1254. new_state["show_helpers"] = false;
  1255. new_state["show_zoom_control"] = false;
  1256. // TODO: Save/restore only affected entries.
  1257. CanvasItemEditor::get_singleton()->set_state(new_state);
  1258. }
  1259. // Tweak the root viewport to ensure it's rendered before our target.
  1260. RID root_vp = get_tree()->get_root()->get_viewport_rid();
  1261. Rect2 root_vp_screen_rect = Rect2(Vector2(), get_tree()->get_root()->get_size());
  1262. RS::get_singleton()->viewport_attach_to_screen(root_vp, Rect2());
  1263. RS::get_singleton()->viewport_set_update_mode(root_vp, RS::VIEWPORT_UPDATE_ALWAYS);
  1264. RID present_rid;
  1265. if (onion.differences_only) {
  1266. // Capture present scene as it is.
  1267. RS::get_singleton()->canvas_item_set_material(onion.capture.canvas_item, RID());
  1268. present_rid = onion.captures[onion.captures.size() - 1];
  1269. RS::get_singleton()->viewport_set_active(present_rid, true);
  1270. RS::get_singleton()->viewport_set_parent_viewport(root_vp, present_rid);
  1271. RS::get_singleton()->draw(false);
  1272. RS::get_singleton()->viewport_set_active(present_rid, false);
  1273. }
  1274. // Backup current animation state.
  1275. Ref<AnimatedValuesBackup> values_backup = player->backup_animated_values();
  1276. float cpos = player->get_current_animation_position();
  1277. // Render every past/future step with the capture shader.
  1278. RS::get_singleton()->canvas_item_set_material(onion.capture.canvas_item, onion.capture.material->get_rid());
  1279. onion.capture.material->set_shader_uniform("bkg_color", GLOBAL_GET("rendering/environment/defaults/default_clear_color"));
  1280. onion.capture.material->set_shader_uniform("differences_only", onion.differences_only);
  1281. onion.capture.material->set_shader_uniform("present", onion.differences_only ? RS::get_singleton()->viewport_get_texture(present_rid) : RID());
  1282. int step_off_a = onion.past ? -onion.steps : 0;
  1283. int step_off_b = onion.future ? onion.steps : 0;
  1284. int cidx = 0;
  1285. onion.capture.material->set_shader_uniform("dir_color", onion.force_white_modulate ? Color(1, 1, 1) : Color(EDITOR_GET("editors/animation/onion_layers_past_color")));
  1286. for (int step_off = step_off_a; step_off <= step_off_b; step_off++) {
  1287. if (step_off == 0) {
  1288. // Skip present step and switch to the color of future.
  1289. if (!onion.force_white_modulate) {
  1290. onion.capture.material->set_shader_uniform("dir_color", EDITOR_GET("editors/animation/onion_layers_future_color"));
  1291. }
  1292. continue;
  1293. }
  1294. float pos = cpos + step_off * anim->get_step();
  1295. bool valid = anim->get_loop_mode() != Animation::LOOP_NONE || (pos >= 0 && pos <= anim->get_length());
  1296. onion.captures_valid.write[cidx] = valid;
  1297. if (valid) {
  1298. player->seek(pos, true);
  1299. get_tree()->flush_transform_notifications(); // Needed for transforms of Node3Ds.
  1300. values_backup->update_skeletons(); // Needed for Skeletons (2D & 3D).
  1301. RS::get_singleton()->viewport_set_active(onion.captures[cidx], true);
  1302. RS::get_singleton()->viewport_set_parent_viewport(root_vp, onion.captures[cidx]);
  1303. RS::get_singleton()->draw(false);
  1304. RS::get_singleton()->viewport_set_active(onion.captures[cidx], false);
  1305. }
  1306. cidx++;
  1307. }
  1308. // Restore root viewport.
  1309. RS::get_singleton()->viewport_set_parent_viewport(root_vp, RID());
  1310. RS::get_singleton()->viewport_attach_to_screen(root_vp, root_vp_screen_rect);
  1311. RS::get_singleton()->viewport_set_update_mode(root_vp, RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  1312. // Restore animation state
  1313. // (Seeking with update=true wouldn't do the trick because the current value of the properties
  1314. // may not match their value for the current point in the animation).
  1315. player->seek(cpos, false);
  1316. values_backup->restore();
  1317. // Restore state of main editors.
  1318. if (Node3DEditor::get_singleton()->is_visible()) {
  1319. // 3D
  1320. Node3DEditor::get_singleton()->set_state(spatial_edit_state);
  1321. } else { // CanvasItemEditor
  1322. // 2D
  1323. CanvasItemEditor::get_singleton()->set_state(canvas_edit_state);
  1324. }
  1325. // Update viewports with skin layers overlaid for the actual engine loop render.
  1326. onion.can_overlay = true;
  1327. plugin->update_overlays();
  1328. }
  1329. void AnimationPlayerEditor::_start_onion_skinning() {
  1330. // FIXME: Using "process_frame" makes onion layers update one frame behind the current.
  1331. if (!get_tree()->is_connected("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) {
  1332. get_tree()->connect("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred));
  1333. }
  1334. }
  1335. void AnimationPlayerEditor::_stop_onion_skinning() {
  1336. if (get_tree()->is_connected("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) {
  1337. get_tree()->disconnect("process_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred));
  1338. _free_onion_layers();
  1339. // Clean up the overlay.
  1340. onion.can_overlay = false;
  1341. plugin->update_overlays();
  1342. }
  1343. }
  1344. void AnimationPlayerEditor::_pin_pressed() {
  1345. SceneTreeDock::get_singleton()->get_tree_editor()->update_tree();
  1346. }
  1347. void AnimationPlayerEditor::_bind_methods() {
  1348. ClassDB::bind_method(D_METHOD("_animation_new"), &AnimationPlayerEditor::_animation_new);
  1349. ClassDB::bind_method(D_METHOD("_animation_rename"), &AnimationPlayerEditor::_animation_rename);
  1350. ClassDB::bind_method(D_METHOD("_animation_remove"), &AnimationPlayerEditor::_animation_remove);
  1351. ClassDB::bind_method(D_METHOD("_animation_blend"), &AnimationPlayerEditor::_animation_blend);
  1352. ClassDB::bind_method(D_METHOD("_animation_edit"), &AnimationPlayerEditor::_animation_edit);
  1353. ClassDB::bind_method(D_METHOD("_animation_resource_edit"), &AnimationPlayerEditor::_animation_resource_edit);
  1354. ClassDB::bind_method(D_METHOD("_animation_player_changed"), &AnimationPlayerEditor::_animation_player_changed);
  1355. ClassDB::bind_method(D_METHOD("_list_changed"), &AnimationPlayerEditor::_list_changed);
  1356. ClassDB::bind_method(D_METHOD("_animation_duplicate"), &AnimationPlayerEditor::_animation_duplicate);
  1357. ClassDB::bind_method(D_METHOD("_prepare_onion_layers_1"), &AnimationPlayerEditor::_prepare_onion_layers_1);
  1358. ClassDB::bind_method(D_METHOD("_prepare_onion_layers_2"), &AnimationPlayerEditor::_prepare_onion_layers_2);
  1359. ClassDB::bind_method(D_METHOD("_start_onion_skinning"), &AnimationPlayerEditor::_start_onion_skinning);
  1360. ClassDB::bind_method(D_METHOD("_stop_onion_skinning"), &AnimationPlayerEditor::_stop_onion_skinning);
  1361. }
  1362. AnimationPlayerEditor *AnimationPlayerEditor::singleton = nullptr;
  1363. AnimationPlayer *AnimationPlayerEditor::get_player() const {
  1364. return player;
  1365. }
  1366. AnimationPlayerEditor::AnimationPlayerEditor(AnimationPlayerEditorPlugin *p_plugin) {
  1367. plugin = p_plugin;
  1368. singleton = this;
  1369. updating = false;
  1370. set_focus_mode(FOCUS_ALL);
  1371. player = nullptr;
  1372. HBoxContainer *hb = memnew(HBoxContainer);
  1373. add_child(hb);
  1374. play_bw_from = memnew(Button);
  1375. play_bw_from->set_flat(true);
  1376. play_bw_from->set_tooltip_text(TTR("Play selected animation backwards from current pos. (A)"));
  1377. hb->add_child(play_bw_from);
  1378. play_bw = memnew(Button);
  1379. play_bw->set_flat(true);
  1380. play_bw->set_tooltip_text(TTR("Play selected animation backwards from end. (Shift+A)"));
  1381. hb->add_child(play_bw);
  1382. stop = memnew(Button);
  1383. stop->set_flat(true);
  1384. stop->set_toggle_mode(true);
  1385. hb->add_child(stop);
  1386. stop->set_tooltip_text(TTR("Stop animation playback. (S)"));
  1387. play = memnew(Button);
  1388. play->set_flat(true);
  1389. play->set_tooltip_text(TTR("Play selected animation from start. (Shift+D)"));
  1390. hb->add_child(play);
  1391. play_from = memnew(Button);
  1392. play_from->set_flat(true);
  1393. play_from->set_tooltip_text(TTR("Play selected animation from current pos. (D)"));
  1394. hb->add_child(play_from);
  1395. frame = memnew(SpinBox);
  1396. hb->add_child(frame);
  1397. frame->set_custom_minimum_size(Size2(80, 0) * EDSCALE);
  1398. frame->set_stretch_ratio(2);
  1399. frame->set_step(0.0001);
  1400. frame->set_tooltip_text(TTR("Animation position (in seconds)."));
  1401. hb->add_child(memnew(VSeparator));
  1402. scale = memnew(LineEdit);
  1403. hb->add_child(scale);
  1404. scale->set_h_size_flags(SIZE_EXPAND_FILL);
  1405. scale->set_stretch_ratio(1);
  1406. scale->set_tooltip_text(TTR("Scale animation playback globally for the node."));
  1407. scale->hide();
  1408. delete_dialog = memnew(ConfirmationDialog);
  1409. add_child(delete_dialog);
  1410. delete_dialog->connect("confirmed", callable_mp(this, &AnimationPlayerEditor::_animation_remove_confirmed));
  1411. tool_anim = memnew(MenuButton);
  1412. tool_anim->set_shortcut_context(this);
  1413. tool_anim->set_flat(false);
  1414. tool_anim->set_tooltip_text(TTR("Animation Tools"));
  1415. tool_anim->set_text(TTR("Animation"));
  1416. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/new_animation", TTR("New")), TOOL_NEW_ANIM);
  1417. tool_anim->get_popup()->add_separator();
  1418. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/animation_libraries", TTR("Manage Animations...")), TOOL_ANIM_LIBRARY);
  1419. tool_anim->get_popup()->add_separator();
  1420. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/duplicate_animation", TTR("Duplicate...")), TOOL_DUPLICATE_ANIM);
  1421. tool_anim->get_popup()->add_separator();
  1422. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/rename_animation", TTR("Rename...")), TOOL_RENAME_ANIM);
  1423. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/edit_transitions", TTR("Edit Transitions...")), TOOL_EDIT_TRANSITIONS);
  1424. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/open_animation_in_inspector", TTR("Open in Inspector")), TOOL_EDIT_RESOURCE);
  1425. tool_anim->get_popup()->add_separator();
  1426. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/remove_animation", TTR("Remove")), TOOL_REMOVE_ANIM);
  1427. tool_anim->set_disabled(true);
  1428. hb->add_child(tool_anim);
  1429. animation = memnew(OptionButton);
  1430. hb->add_child(animation);
  1431. animation->set_h_size_flags(SIZE_EXPAND_FILL);
  1432. animation->set_tooltip_text(TTR("Display list of animations in player."));
  1433. animation->set_clip_text(true);
  1434. autoplay = memnew(Button);
  1435. autoplay->set_flat(true);
  1436. hb->add_child(autoplay);
  1437. autoplay->set_tooltip_text(TTR("Autoplay on Load"));
  1438. hb->add_child(memnew(VSeparator));
  1439. track_editor = memnew(AnimationTrackEditor);
  1440. hb->add_child(track_editor->get_edit_menu());
  1441. hb->add_child(memnew(VSeparator));
  1442. onion_toggle = memnew(Button);
  1443. onion_toggle->set_flat(true);
  1444. onion_toggle->set_toggle_mode(true);
  1445. onion_toggle->set_tooltip_text(TTR("Enable Onion Skinning"));
  1446. onion_toggle->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_onion_skinning_menu).bind(ONION_SKINNING_ENABLE));
  1447. hb->add_child(onion_toggle);
  1448. onion_skinning = memnew(MenuButton);
  1449. onion_skinning->set_tooltip_text(TTR("Onion Skinning Options"));
  1450. onion_skinning->get_popup()->add_separator(TTR("Directions"));
  1451. // TRANSLATORS: Opposite of "Future", refers to a direction in animation onion skinning.
  1452. onion_skinning->get_popup()->add_check_item(TTR("Past"), ONION_SKINNING_PAST);
  1453. onion_skinning->get_popup()->set_item_checked(-1, true);
  1454. // TRANSLATORS: Opposite of "Past", refers to a direction in animation onion skinning.
  1455. onion_skinning->get_popup()->add_check_item(TTR("Future"), ONION_SKINNING_FUTURE);
  1456. onion_skinning->get_popup()->add_separator(TTR("Depth"));
  1457. onion_skinning->get_popup()->add_radio_check_item(TTR("1 step"), ONION_SKINNING_1_STEP);
  1458. onion_skinning->get_popup()->set_item_checked(-1, true);
  1459. onion_skinning->get_popup()->add_radio_check_item(TTR("2 steps"), ONION_SKINNING_2_STEPS);
  1460. onion_skinning->get_popup()->add_radio_check_item(TTR("3 steps"), ONION_SKINNING_3_STEPS);
  1461. onion_skinning->get_popup()->add_separator();
  1462. onion_skinning->get_popup()->add_check_item(TTR("Differences Only"), ONION_SKINNING_DIFFERENCES_ONLY);
  1463. onion_skinning->get_popup()->add_check_item(TTR("Force White Modulate"), ONION_SKINNING_FORCE_WHITE_MODULATE);
  1464. onion_skinning->get_popup()->add_check_item(TTR("Include Gizmos (3D)"), ONION_SKINNING_INCLUDE_GIZMOS);
  1465. hb->add_child(onion_skinning);
  1466. hb->add_child(memnew(VSeparator));
  1467. pin = memnew(Button);
  1468. pin->set_flat(true);
  1469. pin->set_toggle_mode(true);
  1470. pin->set_tooltip_text(TTR("Pin AnimationPlayer"));
  1471. hb->add_child(pin);
  1472. pin->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_pin_pressed));
  1473. file = memnew(EditorFileDialog);
  1474. add_child(file);
  1475. name_dialog = memnew(ConfirmationDialog);
  1476. name_dialog->set_title(TTR("Create New Animation"));
  1477. name_dialog->set_hide_on_ok(false);
  1478. add_child(name_dialog);
  1479. VBoxContainer *vb = memnew(VBoxContainer);
  1480. name_dialog->add_child(vb);
  1481. name_title = memnew(Label(TTR("Animation Name:")));
  1482. vb->add_child(name_title);
  1483. HBoxContainer *name_hb = memnew(HBoxContainer);
  1484. name = memnew(LineEdit);
  1485. name_hb->add_child(name);
  1486. name->set_h_size_flags(SIZE_EXPAND_FILL);
  1487. library = memnew(OptionButton);
  1488. name_hb->add_child(library);
  1489. library->hide();
  1490. vb->add_child(name_hb);
  1491. name_dialog->register_text_enter(name);
  1492. error_dialog = memnew(ConfirmationDialog);
  1493. error_dialog->set_ok_button_text(TTR("Close"));
  1494. error_dialog->set_title(TTR("Error!"));
  1495. add_child(error_dialog);
  1496. name_dialog->connect("confirmed", callable_mp(this, &AnimationPlayerEditor::_animation_name_edited));
  1497. blend_editor.dialog = memnew(AcceptDialog);
  1498. add_child(blend_editor.dialog);
  1499. blend_editor.dialog->set_ok_button_text(TTR("Close"));
  1500. blend_editor.dialog->set_hide_on_ok(true);
  1501. VBoxContainer *blend_vb = memnew(VBoxContainer);
  1502. blend_editor.dialog->add_child(blend_vb);
  1503. blend_editor.tree = memnew(Tree);
  1504. blend_editor.tree->set_columns(2);
  1505. blend_vb->add_margin_child(TTR("Blend Times:"), blend_editor.tree, true);
  1506. blend_editor.next = memnew(OptionButton);
  1507. blend_vb->add_margin_child(TTR("Next (Auto Queue):"), blend_editor.next);
  1508. blend_editor.dialog->set_title(TTR("Cross-Animation Blend Times"));
  1509. updating_blends = false;
  1510. blend_editor.tree->connect("item_edited", callable_mp(this, &AnimationPlayerEditor::_blend_edited));
  1511. autoplay->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_autoplay_pressed));
  1512. autoplay->set_toggle_mode(true);
  1513. play->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_pressed));
  1514. play_from->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_from_pressed));
  1515. play_bw->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_bw_pressed));
  1516. play_bw_from->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_bw_from_pressed));
  1517. stop->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_stop_pressed));
  1518. animation->connect("item_selected", callable_mp(this, &AnimationPlayerEditor::_animation_selected));
  1519. frame->connect("value_changed", callable_mp(this, &AnimationPlayerEditor::_seek_value_changed).bind(true, false));
  1520. scale->connect("text_submitted", callable_mp(this, &AnimationPlayerEditor::_scale_changed));
  1521. last_active = false;
  1522. timeline_position = 0;
  1523. set_process_shortcut_input(true);
  1524. add_child(track_editor);
  1525. track_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1526. track_editor->connect("timeline_changed", callable_mp(this, &AnimationPlayerEditor::_animation_key_editor_seek));
  1527. track_editor->connect("animation_len_changed", callable_mp(this, &AnimationPlayerEditor::_animation_key_editor_anim_len_changed));
  1528. _update_player();
  1529. library_editor = memnew(AnimationLibraryEditor);
  1530. add_child(library_editor);
  1531. library_editor->connect("update_editor", callable_mp(this, &AnimationPlayerEditor::_animation_player_changed));
  1532. // Onion skinning.
  1533. track_editor->connect("visibility_changed", callable_mp(this, &AnimationPlayerEditor::_editor_visibility_changed));
  1534. onion.enabled = false;
  1535. onion.past = true;
  1536. onion.future = false;
  1537. onion.steps = 1;
  1538. onion.differences_only = false;
  1539. onion.force_white_modulate = false;
  1540. onion.include_gizmos = false;
  1541. onion.last_frame = 0;
  1542. onion.can_overlay = false;
  1543. onion.capture_size = Size2();
  1544. onion.capture.canvas = RS::get_singleton()->canvas_create();
  1545. onion.capture.canvas_item = RS::get_singleton()->canvas_item_create();
  1546. RS::get_singleton()->canvas_item_set_parent(onion.capture.canvas_item, onion.capture.canvas);
  1547. onion.capture.material = Ref<ShaderMaterial>(memnew(ShaderMaterial));
  1548. onion.capture.shader = Ref<Shader>(memnew(Shader));
  1549. onion.capture.shader->set_code(R"(
  1550. // Animation editor onion skinning shader.
  1551. shader_type canvas_item;
  1552. uniform vec4 bkg_color;
  1553. uniform vec4 dir_color;
  1554. uniform bool differences_only;
  1555. uniform sampler2D present;
  1556. float zero_if_equal(vec4 a, vec4 b) {
  1557. return smoothstep(0.0, 0.005, length(a.rgb - b.rgb) / sqrt(3.0));
  1558. }
  1559. void fragment() {
  1560. vec4 capture_samp = texture(TEXTURE, UV);
  1561. vec4 present_samp = texture(present, UV);
  1562. float bkg_mask = zero_if_equal(capture_samp, bkg_color);
  1563. float diff_mask = 1.0 - zero_if_equal(present_samp, bkg_color);
  1564. diff_mask = min(1.0, diff_mask + float(!differences_only));
  1565. COLOR = vec4(capture_samp.rgb * dir_color.rgb, bkg_mask * diff_mask);
  1566. }
  1567. )");
  1568. RS::get_singleton()->material_set_shader(onion.capture.material->get_rid(), onion.capture.shader->get_rid());
  1569. }
  1570. AnimationPlayerEditor::~AnimationPlayerEditor() {
  1571. _free_onion_layers();
  1572. RS::get_singleton()->free(onion.capture.canvas);
  1573. RS::get_singleton()->free(onion.capture.canvas_item);
  1574. }
  1575. void AnimationPlayerEditorPlugin::_notification(int p_what) {
  1576. switch (p_what) {
  1577. case NOTIFICATION_ENTER_TREE: {
  1578. Node3DEditor::get_singleton()->connect("transform_key_request", callable_mp(this, &AnimationPlayerEditorPlugin::_transform_key_request));
  1579. InspectorDock::get_inspector_singleton()->connect("property_keyed", callable_mp(this, &AnimationPlayerEditorPlugin::_property_keyed));
  1580. anim_editor->get_track_editor()->connect("keying_changed", callable_mp(this, &AnimationPlayerEditorPlugin::_update_keying));
  1581. InspectorDock::get_inspector_singleton()->connect("edited_object_changed", callable_mp(anim_editor->get_track_editor(), &AnimationTrackEditor::update_keying));
  1582. set_force_draw_over_forwarding_enabled();
  1583. } break;
  1584. }
  1585. }
  1586. void AnimationPlayerEditorPlugin::_property_keyed(const String &p_keyed, const Variant &p_value, bool p_advance) {
  1587. if (!anim_editor->get_track_editor()->has_keying()) {
  1588. return;
  1589. }
  1590. anim_editor->get_track_editor()->insert_value_key(p_keyed, p_value, p_advance);
  1591. }
  1592. void AnimationPlayerEditorPlugin::_transform_key_request(Object *sp, const String &p_sub, const Transform3D &p_key) {
  1593. if (!anim_editor->get_track_editor()->has_keying()) {
  1594. return;
  1595. }
  1596. Node3D *s = Object::cast_to<Node3D>(sp);
  1597. if (!s) {
  1598. return;
  1599. }
  1600. anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_POSITION_3D, p_key.origin);
  1601. anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_ROTATION_3D, p_key.basis.get_rotation_quaternion());
  1602. anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_SCALE_3D, p_key.basis.get_scale());
  1603. }
  1604. void AnimationPlayerEditorPlugin::_update_keying() {
  1605. InspectorDock::get_inspector_singleton()->set_keying(anim_editor->get_track_editor()->has_keying());
  1606. }
  1607. void AnimationPlayerEditorPlugin::edit(Object *p_object) {
  1608. anim_editor->set_undo_redo(get_undo_redo());
  1609. if (!p_object) {
  1610. return;
  1611. }
  1612. anim_editor->edit(Object::cast_to<AnimationPlayer>(p_object));
  1613. }
  1614. bool AnimationPlayerEditorPlugin::handles(Object *p_object) const {
  1615. return p_object->is_class("AnimationPlayer");
  1616. }
  1617. void AnimationPlayerEditorPlugin::make_visible(bool p_visible) {
  1618. if (p_visible) {
  1619. EditorNode::get_singleton()->make_bottom_panel_item_visible(anim_editor);
  1620. anim_editor->set_process(true);
  1621. anim_editor->ensure_visibility();
  1622. }
  1623. }
  1624. AnimationPlayerEditorPlugin::AnimationPlayerEditorPlugin() {
  1625. anim_editor = memnew(AnimationPlayerEditor(this));
  1626. anim_editor->set_undo_redo(EditorNode::get_undo_redo());
  1627. EditorNode::get_singleton()->add_bottom_panel_item(TTR("Animation"), anim_editor);
  1628. }
  1629. AnimationPlayerEditorPlugin::~AnimationPlayerEditorPlugin() {
  1630. }