animation_player_editor_plugin.cpp 60 KB

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