animation_player_editor_plugin.cpp 61 KB

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