animation_player_editor_plugin.cpp 61 KB

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