2
0

animation_player_editor_plugin.cpp 60 KB

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