2
0

animation_player_editor_plugin.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. /*************************************************************************/
  2. /* animation_player_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "animation_player_editor_plugin.h"
  30. #include "globals.h"
  31. #include "io/resource_loader.h"
  32. #include "io/resource_saver.h"
  33. #include "os/keyboard.h"
  34. #include "tools/editor/editor_settings.h"
  35. #include "tools/editor/animation_editor.h"
  36. void AnimationPlayerEditor::_node_removed(Node *p_node) {
  37. if (player && player == p_node) {
  38. player=NULL;
  39. set_process(false);
  40. key_editor->set_animation(Ref<Animation>());
  41. key_editor->set_root(NULL);
  42. _update_player();
  43. //editor->animation_editor_make_visible(false);
  44. }
  45. }
  46. void AnimationPlayerEditor::_input_event(InputEvent p_event) {
  47. }
  48. void AnimationPlayerEditor::_notification(int p_what) {
  49. if (p_what==NOTIFICATION_PROCESS) {
  50. if (!player)
  51. return;
  52. updating = true;
  53. if (player->is_playing()) {
  54. {
  55. String animname=player->get_current_animation();
  56. if (player->has_animation(animname)) {
  57. Ref<Animation> anim = player->get_animation(animname);
  58. if (!anim.is_null()) {
  59. frame->set_max(anim->get_length());
  60. }
  61. }
  62. }
  63. frame->set_val(player->get_current_animation_pos());
  64. key_editor->set_anim_pos(player->get_current_animation_pos());
  65. EditorNode::get_singleton()->get_property_editor()->refresh();
  66. } else if (last_active) {
  67. //need the last frame after it stopped
  68. frame->set_val(player->get_current_animation_pos());
  69. }
  70. last_active=player->is_playing();
  71. //seek->set_val(player->get_pos());
  72. updating = false;
  73. }
  74. if (p_what==NOTIFICATION_ENTER_TREE) {
  75. // editor->connect("hide_animation_player_editors",this,"_hide_anim_editors");
  76. add_anim->set_icon( get_icon("New","EditorIcons") );
  77. rename_anim->set_icon( get_icon("Rename","EditorIcons") );
  78. duplicate_anim->set_icon( get_icon("Duplicate","EditorIcons") );
  79. autoplay->set_icon( get_icon("AutoPlay","EditorIcons") );
  80. load_anim->set_icon( get_icon("Folder","EditorIcons") );
  81. save_anim->set_icon(get_icon("Save", "EditorIcons"));
  82. save_anim->get_popup()->connect("item_pressed", this, "_animation_save_menu");
  83. remove_anim->set_icon( get_icon("Remove","EditorIcons") );
  84. blend_anim->set_icon( get_icon("Blend","EditorIcons") );
  85. play->set_icon( get_icon("PlayStart","EditorIcons") );
  86. play_from->set_icon( get_icon("Play","EditorIcons") );
  87. play_bw->set_icon( get_icon("PlayStartBackwards","EditorIcons") );
  88. play_bw_from->set_icon( get_icon("PlayBackwards","EditorIcons") );
  89. autoplay_icon=get_icon("AutoPlay","EditorIcons");
  90. stop->set_icon( get_icon("Stop","EditorIcons") );
  91. resource_edit_anim->set_icon( get_icon("EditResource","EditorIcons") );
  92. pin->set_icon(get_icon("Pin","EditorIcons") );
  93. tool_anim->set_icon(get_icon("Tools","EditorIcons"));
  94. tool_anim->get_popup()->connect("item_pressed",this,"_animation_tool_menu");
  95. blend_editor.next->connect("item_selected", this, "_blend_editor_next_changed");
  96. nodename->set_icon(get_icon("AnimationPlayer","EditorIcons"));
  97. /*
  98. anim_editor_load->set_normal_texture( get_icon("AnimGet","EditorIcons"));
  99. anim_editor_store->set_normal_texture( get_icon("AnimSet","EditorIcons"));
  100. anim_editor_load->set_pressed_texture( get_icon("AnimGet","EditorIcons"));
  101. anim_editor_store->set_pressed_texture( get_icon("AnimSet","EditorIcons"));
  102. anim_editor_load->set_hover_texture( get_icon("AnimGetHl","EditorIcons"));
  103. anim_editor_store->set_hover_texture( get_icon("AnimSetHl","EditorIcons"));
  104. */
  105. get_tree()->connect("node_removed",this,"_node_removed");
  106. }
  107. }
  108. void AnimationPlayerEditor::_autoplay_pressed() {
  109. if (updating)
  110. return;
  111. if (animation->get_item_count()==0) {
  112. return;
  113. }
  114. String current = animation->get_item_text( animation->get_selected() );
  115. if (player->get_autoplay()==current) {
  116. //unset
  117. undo_redo->create_action(TTR("Toggle Autoplay"));
  118. undo_redo->add_do_method(player,"set_autoplay","");
  119. undo_redo->add_undo_method(player,"set_autoplay",player->get_autoplay());
  120. undo_redo->add_do_method(this,"_animation_player_changed",player);
  121. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  122. undo_redo->commit_action();
  123. } else {
  124. //set
  125. undo_redo->create_action(TTR("Toggle Autoplay"));
  126. undo_redo->add_do_method(player,"set_autoplay",current);
  127. undo_redo->add_undo_method(player,"set_autoplay",player->get_autoplay());
  128. undo_redo->add_do_method(this,"_animation_player_changed",player);
  129. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  130. undo_redo->commit_action();
  131. }
  132. }
  133. void AnimationPlayerEditor::_play_pressed() {
  134. String current;
  135. if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
  136. current = animation->get_item_text( animation->get_selected() );
  137. }
  138. if (current!="") {
  139. if (current==player->get_current_animation())
  140. player->stop(); //so it wont blend with itself
  141. player->play(current );
  142. }
  143. //unstop
  144. stop->set_pressed(false);
  145. //unpause
  146. //pause->set_pressed(false);
  147. }
  148. void AnimationPlayerEditor::_play_from_pressed() {
  149. String current;
  150. if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
  151. current = animation->get_item_text( animation->get_selected() );
  152. }
  153. if (current!="") {
  154. float time = player->get_current_animation_pos();
  155. if (current==player->get_current_animation() && player->is_playing()) {
  156. player->stop(); //so it wont blend with itself
  157. }
  158. player->play( current );
  159. player->seek(time);
  160. }
  161. //unstop
  162. stop->set_pressed(false);
  163. //unpause
  164. //pause->set_pressed(false);
  165. }
  166. void AnimationPlayerEditor::_play_bw_pressed() {
  167. String current;
  168. if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
  169. current = animation->get_item_text( animation->get_selected() );
  170. }
  171. if (current!="") {
  172. if (current==player->get_current_animation())
  173. player->stop(); //so it wont blend with itself
  174. player->play(current,-1,-1,true);
  175. }
  176. //unstop
  177. stop->set_pressed(false);
  178. //unpause
  179. //pause->set_pressed(false);
  180. }
  181. void AnimationPlayerEditor::_play_bw_from_pressed() {
  182. String current;
  183. if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
  184. current = animation->get_item_text( animation->get_selected() );
  185. }
  186. if (current!="") {
  187. float time = player->get_current_animation_pos();
  188. if (current==player->get_current_animation())
  189. player->stop(); //so it wont blend with itself
  190. player->play(current,-1,-1,true);
  191. player->seek(time);
  192. }
  193. //unstop
  194. stop->set_pressed(false);
  195. //unpause
  196. //pause->set_pressed(false);
  197. }
  198. void AnimationPlayerEditor::_stop_pressed() {
  199. player->stop(false);
  200. play->set_pressed(false);
  201. stop->set_pressed(true);
  202. //pause->set_pressed(false);
  203. //player->set_pause(false);
  204. }
  205. void AnimationPlayerEditor::_pause_pressed() {
  206. //player->set_pause( pause->is_pressed() );
  207. }
  208. void AnimationPlayerEditor::_animation_selected(int p_which) {
  209. if (updating)
  210. return;
  211. // when selecting an animation, the idea is that the only interesting behavior
  212. // ui-wise is that it should play/blend the next one if currently playing
  213. String current;
  214. if (animation->get_selected()>=0 && animation->get_selected()<animation->get_item_count()) {
  215. current = animation->get_item_text( animation->get_selected() );
  216. }
  217. if (current!="") {
  218. player->set_current_animation( current );
  219. Ref<Animation> anim = player->get_animation(current);
  220. {
  221. key_editor->set_animation(anim);
  222. Node *root = player->get_node(player->get_root());
  223. if (root) {
  224. key_editor->set_root(root);
  225. }
  226. }
  227. frame->set_max(anim->get_length());
  228. if (anim->get_step())
  229. frame->set_step(anim->get_step());
  230. else
  231. frame->set_step(0.00001);
  232. } else {
  233. key_editor->set_animation(Ref<Animation>());
  234. key_editor->set_root(NULL);
  235. }
  236. autoplay->set_pressed(current==player->get_autoplay());
  237. }
  238. void AnimationPlayerEditor::_animation_new() {
  239. renaming=false;
  240. name_title->set_text(TTR("New Animation Name:"));
  241. int count=1;
  242. String base=TTR("New Anim");
  243. while(true) {
  244. String attempt = base;
  245. if (count>1)
  246. attempt+=" ("+itos(count)+")";
  247. if (player->has_animation(attempt)) {
  248. count++;
  249. continue;
  250. }
  251. base=attempt;
  252. break;
  253. }
  254. name->set_text(base);
  255. name_dialog->popup_centered(Size2(300,90));
  256. name->select_all();
  257. name->grab_focus();
  258. }
  259. void AnimationPlayerEditor::_animation_rename() {
  260. if (animation->get_item_count()==0)
  261. return;
  262. int selected = animation->get_selected();
  263. String selected_name = animation->get_item_text(selected);
  264. name_title->set_text(TTR("Change Animation Name:"));
  265. name->set_text(selected_name);
  266. renaming=true;
  267. name_dialog->popup_centered(Size2(300,90));
  268. name->select_all();
  269. name->grab_focus();
  270. }
  271. void AnimationPlayerEditor::_animation_load() {
  272. ERR_FAIL_COND(!player);
  273. file->set_mode( EditorFileDialog::MODE_OPEN_FILE );
  274. file->clear_filters();
  275. List<String> extensions;
  276. ResourceLoader::get_recognized_extensions_for_type("Animation",&extensions);
  277. for (List<String>::Element *E=extensions.front();E;E=E->next()) {
  278. file->add_filter("*."+E->get()+" ; "+E->get().to_upper() );
  279. }
  280. file->popup_centered_ratio();
  281. current_option = RESOURCE_LOAD;
  282. }
  283. void AnimationPlayerEditor::_animation_save_in_path(const Ref<Resource>& p_resource, const String& p_path) {
  284. int flg = 0;
  285. if (EditorSettings::get_singleton()->get("on_save/compress_binary_resources"))
  286. flg |= ResourceSaver::FLAG_COMPRESS;
  287. if (EditorSettings::get_singleton()->get("on_save/save_paths_as_relative"))
  288. flg |= ResourceSaver::FLAG_RELATIVE_PATHS;
  289. String path = Globals::get_singleton()->localize_path(p_path);
  290. Error err = ResourceSaver::save(path, p_resource, flg | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS);
  291. if (err != OK) {
  292. accept->set_text(TTR("Error saving resource!"));
  293. accept->popup_centered_minsize();
  294. return;
  295. }
  296. // EditorFileSystem::get_singleton()->update_file(path,p_resource->get_type());
  297. ((Resource*)p_resource.ptr())->set_path(path);
  298. editor->emit_signal("resource_saved", p_resource);
  299. }
  300. void AnimationPlayerEditor::_animation_save(const Ref<Resource>& p_resource) {
  301. if (p_resource->get_path().is_resource_file()) {
  302. _animation_save_in_path(p_resource, p_resource->get_path());
  303. }
  304. else {
  305. _animation_save_as(p_resource);
  306. }
  307. }
  308. void AnimationPlayerEditor::_animation_save_as(const Ref<Resource>& p_resource) {
  309. file->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  310. bool relpaths = (p_resource->has_meta("__editor_relpaths__") && p_resource->get_meta("__editor_relpaths__").operator bool());
  311. List<String> extensions;
  312. ResourceSaver::get_recognized_extensions(p_resource, &extensions);
  313. file->clear_filters();
  314. for (int i = 0; i<extensions.size(); i++) {
  315. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  316. }
  317. //file->set_current_path(current_path);
  318. if (p_resource->get_path() != "") {
  319. file->set_current_path(p_resource->get_path());
  320. if (extensions.size()) {
  321. String ext = p_resource->get_path().extension().to_lower();
  322. if (extensions.find(ext) == NULL) {
  323. file->set_current_path(p_resource->get_path().replacen("." + ext, "." + extensions.front()->get()));
  324. }
  325. }
  326. }
  327. else {
  328. String existing;
  329. if (extensions.size()) {
  330. existing = "new_" + p_resource->get_type().to_lower() + "." + extensions.front()->get().to_lower();
  331. }
  332. file->set_current_path(existing);
  333. }
  334. file->popup_centered_ratio();
  335. file->set_title(TTR("Save Resource As.."));
  336. current_option = RESOURCE_SAVE;
  337. }
  338. void AnimationPlayerEditor::_animation_remove() {
  339. if (animation->get_item_count()==0)
  340. return;
  341. String current = animation->get_item_text(animation->get_selected());
  342. Ref<Animation> anim = player->get_animation(current);
  343. undo_redo->create_action(TTR("Remove Animation"));
  344. undo_redo->add_do_method(player,"remove_animation",current);
  345. undo_redo->add_undo_method(player,"add_animation",current,anim);
  346. undo_redo->add_do_method(this,"_animation_player_changed",player);
  347. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  348. undo_redo->commit_action();
  349. }
  350. void AnimationPlayerEditor::_select_anim_by_name(const String& p_anim) {
  351. int idx=-1;
  352. for(int i=0;i<animation->get_item_count();i++) {
  353. if (animation->get_item_text(i)==p_anim) {
  354. idx=i;
  355. break;
  356. }
  357. }
  358. ERR_FAIL_COND(idx==-1);
  359. animation->select(idx);
  360. _animation_selected(idx);
  361. }
  362. void AnimationPlayerEditor::_animation_name_edited() {
  363. player->stop();
  364. String new_name = name->get_text();
  365. if (new_name=="" || new_name.find(":")!=-1 || new_name.find("/")!=-1) {
  366. error_dialog->set_text(TTR("ERROR: Invalid animation name!"));
  367. error_dialog->popup_centered_minsize();
  368. return;
  369. }
  370. if (renaming && animation->get_item_count()>0 && animation->get_item_text(animation->get_selected())==new_name) {
  371. name_dialog->hide();
  372. return;
  373. }
  374. if (player->has_animation(new_name)) {
  375. error_dialog->set_text(TTR("ERROR: Animation name already exists!"));
  376. error_dialog->popup_centered_minsize();
  377. return;
  378. }
  379. if (renaming) {
  380. String current = animation->get_item_text(animation->get_selected());
  381. Ref<Animation> anim = player->get_animation(current);
  382. undo_redo->create_action(TTR("Rename Animation"));
  383. undo_redo->add_do_method(player,"rename_animation",current,new_name);
  384. undo_redo->add_do_method(anim.ptr(),"set_name",new_name);
  385. undo_redo->add_undo_method(player,"rename_animation",new_name,current);
  386. undo_redo->add_undo_method(anim.ptr(),"set_name",current);
  387. undo_redo->add_do_method(this,"_animation_player_changed",player);
  388. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  389. undo_redo->commit_action();
  390. _select_anim_by_name(new_name);
  391. } else {
  392. Ref<Animation> new_anim = Ref<Animation>(memnew( Animation ));
  393. new_anim->set_name(new_name);
  394. undo_redo->create_action(TTR("Add Animation"));
  395. undo_redo->add_do_method(player,"add_animation",new_name,new_anim);
  396. undo_redo->add_undo_method(player,"remove_animation",new_name);
  397. undo_redo->add_do_method(this,"_animation_player_changed",player);
  398. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  399. undo_redo->commit_action();
  400. _select_anim_by_name(new_name);
  401. }
  402. name_dialog->hide();
  403. }
  404. void AnimationPlayerEditor::_blend_editor_next_changed(const int p_idx) {
  405. if (animation->get_item_count()==0)
  406. return;
  407. String current = animation->get_item_text(animation->get_selected());
  408. undo_redo->create_action(TTR("Blend Next Changed"));
  409. undo_redo->add_do_method(player,"animation_set_next",current,blend_editor.next->get_item_text(p_idx));
  410. undo_redo->add_undo_method(player,"animation_set_next",current,player->animation_get_next(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. }
  415. void AnimationPlayerEditor::_animation_blend() {
  416. if (updating_blends)
  417. return;
  418. blend_editor.tree->clear();
  419. if (animation->get_item_count()==0)
  420. return;
  421. String current = animation->get_item_text(animation->get_selected());
  422. blend_editor.dialog->popup_centered(Size2(400,400));
  423. blend_editor.tree->set_hide_root(true);
  424. blend_editor.tree->set_column_min_width(0,10);
  425. blend_editor.tree->set_column_min_width(1,3);
  426. List<StringName> anims;
  427. player->get_animation_list(&anims);
  428. TreeItem *root = blend_editor.tree->create_item();
  429. updating_blends=true;
  430. int i = 0;
  431. bool anim_found = false;
  432. blend_editor.next->clear();
  433. blend_editor.next->add_item("", i);
  434. for(List<StringName>::Element *E=anims.front();E;E=E->next()) {
  435. String to=E->get();
  436. TreeItem *blend=blend_editor.tree->create_item(root);
  437. blend->set_editable(0,false);
  438. blend->set_editable(1,true);
  439. blend->set_text(0,to);
  440. blend->set_cell_mode(1,TreeItem::CELL_MODE_RANGE);
  441. blend->set_range_config(1,0,3600,0.001);
  442. blend->set_range(1,player->get_blend_time(current,to));
  443. i++;
  444. blend_editor.next->add_item(to, i);
  445. if (to == player->animation_get_next(current)) {
  446. blend_editor.next->select(i);
  447. anim_found = true;
  448. }
  449. }
  450. // make sure we reset it else it becomes out of sync and could contain a deleted animation
  451. if (!anim_found) {
  452. blend_editor.next->select(0);
  453. player->animation_set_next(current, blend_editor.next->get_item_text(0));
  454. }
  455. updating_blends=false;
  456. }
  457. void AnimationPlayerEditor::_blend_edited() {
  458. if (updating_blends)
  459. return;
  460. if (animation->get_item_count()==0)
  461. return;
  462. String current = animation->get_item_text(animation->get_selected());
  463. TreeItem *selected = blend_editor.tree->get_edited();
  464. if (!selected)
  465. return;
  466. updating_blends=true;
  467. String to=selected->get_text(0);
  468. float blend_time = selected->get_range(1);
  469. float prev_blend_time = player->get_blend_time(current,to);
  470. undo_redo->create_action(TTR("Change Blend Time"));
  471. undo_redo->add_do_method(player,"set_blend_time",current,to,blend_time);
  472. undo_redo->add_undo_method(player,"set_blend_time",current,to,prev_blend_time);
  473. undo_redo->add_do_method(this,"_animation_player_changed",player);
  474. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  475. undo_redo->commit_action();
  476. updating_blends=false;
  477. }
  478. void AnimationPlayerEditor::ensure_visibility() {
  479. if (player && pin->is_pressed())
  480. return; // another player is pinned, don't reset
  481. _animation_edit();
  482. }
  483. Dictionary AnimationPlayerEditor::get_state() const {
  484. Dictionary d;
  485. d["visible"]=is_visible();
  486. if (EditorNode::get_singleton()->get_edited_scene() && is_visible() && player) {
  487. d["player"]=EditorNode::get_singleton()->get_edited_scene()->get_path_to(player);
  488. d["animation"]=player->get_current_animation();
  489. }
  490. return d;
  491. }
  492. void AnimationPlayerEditor::set_state(const Dictionary& p_state) {
  493. if (p_state.has("visible") && p_state["visible"]) {
  494. if (!EditorNode::get_singleton()->get_edited_scene())
  495. return;
  496. Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
  497. if (n && n->cast_to<AnimationPlayer>()) {
  498. player=n->cast_to<AnimationPlayer>();
  499. _update_player();
  500. show();
  501. set_process(true);
  502. ensure_visibility();
  503. // EditorNode::get_singleton()->animation_panel_make_visible(true);
  504. if (p_state.has("animation")) {
  505. String anim = p_state["animation"];
  506. _select_anim_by_name(anim);
  507. _animation_edit();
  508. }
  509. }
  510. }
  511. }
  512. void AnimationPlayerEditor::_animation_resource_edit() {
  513. if (animation->get_item_count()) {
  514. String current = animation->get_item_text(animation->get_selected());
  515. Ref<Animation> anim = player->get_animation(current);
  516. editor->edit_resource(anim);
  517. }
  518. }
  519. void AnimationPlayerEditor::_animation_edit() {
  520. if (animation->get_item_count()) {
  521. String current = animation->get_item_text(animation->get_selected());
  522. Ref<Animation> anim = player->get_animation(current);
  523. key_editor->set_animation(anim);
  524. Node *root = player->get_node(player->get_root());
  525. if (root) {
  526. key_editor->set_root(root);
  527. }
  528. } else {
  529. key_editor->set_animation(Ref<Animation>());
  530. key_editor->set_root(NULL);
  531. }
  532. }
  533. void AnimationPlayerEditor::_dialog_action(String p_file) {
  534. switch (current_option) {
  535. case RESOURCE_LOAD: {
  536. ERR_FAIL_COND(!player);
  537. Ref<Resource> res = ResourceLoader::load(p_file, "Animation");
  538. ERR_FAIL_COND(res.is_null());
  539. ERR_FAIL_COND(!res->is_type("Animation"));
  540. if (p_file.find_last("/") != -1) {
  541. p_file = p_file.substr(p_file.find_last("/") + 1, p_file.length());
  542. }
  543. if (p_file.find_last("\\") != -1) {
  544. p_file = p_file.substr(p_file.find_last("\\") + 1, p_file.length());
  545. }
  546. if (p_file.find(".") != -1)
  547. p_file = p_file.substr(0, p_file.find("."));
  548. undo_redo->create_action(TTR("Load Animation"));
  549. undo_redo->add_do_method(player, "add_animation", p_file, res);
  550. undo_redo->add_undo_method(player, "remove_animation", p_file);
  551. if (player->has_animation(p_file)) {
  552. undo_redo->add_undo_method(player, "add_animation", p_file, player->get_animation(p_file));
  553. }
  554. undo_redo->add_do_method(this, "_animation_player_changed", player);
  555. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  556. undo_redo->commit_action();
  557. break;
  558. }
  559. case RESOURCE_SAVE: {
  560. String current = animation->get_item_text(animation->get_selected());
  561. if (current != "") {
  562. Ref<Animation> anim = player->get_animation(current);
  563. ERR_FAIL_COND(!anim->cast_to<Resource>())
  564. RES current_res = RES(anim->cast_to<Resource>());
  565. _animation_save_in_path(current_res, p_file);
  566. }
  567. }
  568. }
  569. }
  570. void AnimationPlayerEditor::_scale_changed(const String& p_scale) {
  571. player->set_speed(p_scale.to_double());
  572. }
  573. void AnimationPlayerEditor::_update_animation() {
  574. // the purpose of _update_animation is to reflect the current state
  575. // of the animation player in the current editor..
  576. updating=true;
  577. if (player->is_playing()) {
  578. play->set_pressed(true);
  579. stop->set_pressed(false);
  580. } else {
  581. play->set_pressed(false);
  582. stop->set_pressed(true);
  583. }
  584. scale->set_text( String::num(player->get_speed(),2) );
  585. String current=player->get_current_animation();
  586. for (int i=0;i<animation->get_item_count();i++) {
  587. if (animation->get_item_text(i)==current) {
  588. animation->select(i);
  589. break;
  590. }
  591. }
  592. updating=false;
  593. }
  594. void AnimationPlayerEditor::_update_player() {
  595. updating=true;
  596. List<StringName> animlist;
  597. if (player)
  598. player->get_animation_list(&animlist);
  599. animation->clear();
  600. if (player)
  601. nodename->set_text(player->get_name());
  602. else
  603. nodename->set_text("<empty>");
  604. add_anim->set_disabled(player==NULL);
  605. load_anim->set_disabled(player==NULL);
  606. stop->set_disabled(animlist.size()==0);
  607. play->set_disabled(animlist.size()==0);
  608. play_bw->set_disabled(animlist.size()==0);
  609. play_bw_from->set_disabled(animlist.size()==0);
  610. play_from->set_disabled(animlist.size()==0);
  611. autoplay->set_disabled(animlist.size()==0);
  612. duplicate_anim->set_disabled(animlist.size()==0);
  613. rename_anim->set_disabled(animlist.size()==0);
  614. blend_anim->set_disabled(animlist.size()==0);
  615. remove_anim->set_disabled(animlist.size()==0);
  616. resource_edit_anim->set_disabled(animlist.size()==0);
  617. save_anim->set_disabled(animlist.size() == 0);
  618. int active_idx=-1;
  619. for (List<StringName>::Element *E=animlist.front();E;E=E->next()) {
  620. if (player->get_autoplay()==E->get())
  621. animation->add_icon_item(autoplay_icon,E->get());
  622. else
  623. animation->add_item(E->get());
  624. if (player->get_current_animation()==E->get())
  625. active_idx=animation->get_item_count()-1;
  626. }
  627. if (!player)
  628. return;
  629. updating=false;
  630. if (active_idx!=-1) {
  631. animation->select(active_idx);
  632. autoplay->set_pressed(animation->get_item_text(active_idx)==player->get_autoplay());
  633. _animation_selected(active_idx);
  634. } else if (animation->get_item_count()>0){
  635. animation->select(0);
  636. autoplay->set_pressed(animation->get_item_text(0)==player->get_autoplay());
  637. _animation_selected(0);
  638. }
  639. //pause->set_pressed(player->is_paused());
  640. if (animation->get_item_count()) {
  641. String current = animation->get_item_text(animation->get_selected());
  642. Ref<Animation> anim = player->get_animation(current);
  643. key_editor->set_animation(anim);
  644. Node *root = player->get_node(player->get_root());
  645. if (root) {
  646. key_editor->set_root(root);
  647. }
  648. }
  649. _update_animation();
  650. }
  651. void AnimationPlayerEditor::edit(AnimationPlayer *p_player) {
  652. if (player && pin->is_pressed())
  653. return; //ignore, pinned
  654. player=p_player;
  655. if (player)
  656. _update_player();
  657. else {
  658. // hide();
  659. }
  660. }
  661. void AnimationPlayerEditor::_animation_duplicate() {
  662. if (!animation->get_item_count())
  663. return;
  664. String current = animation->get_item_text(animation->get_selected());
  665. Ref<Animation> anim = player->get_animation(current);
  666. if (!anim.is_valid())
  667. return;
  668. Ref<Animation> new_anim = memnew( Animation );
  669. List<PropertyInfo> plist;
  670. anim->get_property_list(&plist);
  671. for (List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  672. if (E->get().usage&PROPERTY_USAGE_STORAGE) {
  673. new_anim->set(E->get().name, anim->get(E->get().name));
  674. }
  675. }
  676. new_anim->set_path("");
  677. String new_name = current;
  678. while(player->has_animation(new_name)) {
  679. new_name=new_name+" (copy)";
  680. }
  681. undo_redo->create_action(TTR("Duplicate Animation"));
  682. undo_redo->add_do_method(player,"add_animation",new_name,new_anim);
  683. undo_redo->add_undo_method(player,"remove_animation",new_name);
  684. undo_redo->add_do_method(player,"animation_set_next",new_name,player->animation_get_next(current));
  685. undo_redo->add_do_method(this,"_animation_player_changed",player);
  686. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  687. undo_redo->commit_action();
  688. for(int i=0;i<animation->get_item_count();i++) {
  689. if (animation->get_item_text(i)==new_name) {
  690. animation->select(i);
  691. _animation_selected(i);
  692. return;
  693. }
  694. }
  695. }
  696. void AnimationPlayerEditor::_seek_value_changed(float p_value,bool p_set) {
  697. if (updating || !player || player->is_playing()) {
  698. return;
  699. };
  700. updating=true;
  701. String current=player->get_current_animation(); //animation->get_item_text( animation->get_selected() );
  702. if (current == "" || !player->has_animation(current)) {
  703. updating=false;
  704. current="";
  705. return;
  706. };
  707. Ref<Animation> anim;
  708. anim=player->get_animation(current);
  709. float pos = anim->get_length() * (p_value / frame->get_max());
  710. float step = anim->get_step();
  711. if (step) {
  712. pos=Math::stepify(pos, step);
  713. if (pos<0)
  714. pos=0;
  715. if (pos>=anim->get_length())
  716. pos=anim->get_length();
  717. }
  718. if (player->is_valid() && !p_set) {
  719. float cpos = player->get_current_animation_pos();
  720. player->seek_delta(pos,pos-cpos);
  721. } else {
  722. player->seek(pos,true);
  723. }
  724. key_editor->set_anim_pos(pos);
  725. updating=true;
  726. };
  727. void AnimationPlayerEditor::_animation_player_changed(Object *p_pl) {
  728. if (player==p_pl && is_visible()) {
  729. _update_player();
  730. if (blend_editor.dialog->is_visible())
  731. _animation_blend(); //update
  732. }
  733. }
  734. void AnimationPlayerEditor::_list_changed() {
  735. if(is_visible())
  736. _update_player();
  737. }
  738. #if 0
  739. void AnimationPlayerEditor::_editor_store() {
  740. if (animation->get_item_count()==0)
  741. return;
  742. String current = animation->get_item_text(animation->get_selected());
  743. Ref<Animation> anim = player->get_animation(current);
  744. if (key_editor->get_current_animation()==anim)
  745. return; //already there
  746. undo_redo->create_action("Store anim in editor");
  747. undo_redo->add_do_method(key_editor,"set_animation",anim);
  748. undo_redo->add_undo_method(key_editor,"remove_animation",anim);
  749. undo_redo->commit_action();
  750. }
  751. void AnimationPlayerEditor::_editor_load(){
  752. Ref<Animation> anim = key_editor->get_current_animation();
  753. if (anim.is_null())
  754. return;
  755. String existing = player->find_animation(anim);
  756. if (existing!="") {
  757. _select_anim_by_name(existing);
  758. return; //already has
  759. }
  760. int count=1;
  761. String base=anim->get_name();
  762. bool noname=false;
  763. if (base=="") {
  764. base="New Anim";
  765. noname=true;
  766. }
  767. while(true) {
  768. String attempt = base;
  769. if (count>1)
  770. attempt+=" ("+itos(count)+")";
  771. if (player->has_animation(attempt)) {
  772. count++;
  773. continue;
  774. }
  775. base=attempt;
  776. break;
  777. }
  778. if (noname)
  779. anim->set_name(base);
  780. undo_redo->create_action("Add Animation From Editor");
  781. undo_redo->add_do_method(player,"add_animation",base,anim);
  782. undo_redo->add_undo_method(player,"remove_animation",base);
  783. undo_redo->add_do_method(this,"_animation_player_changed",player);
  784. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  785. undo_redo->commit_action();
  786. _select_anim_by_name(base);
  787. }
  788. #endif
  789. void AnimationPlayerEditor::_animation_key_editor_anim_len_changed(float p_len) {
  790. frame->set_max(p_len);
  791. }
  792. void AnimationPlayerEditor::_animation_key_editor_anim_step_changed(float p_len) {
  793. if (p_len)
  794. frame->set_step(p_len);
  795. else
  796. frame->set_step(0.00001);
  797. }
  798. void AnimationPlayerEditor::_animation_key_editor_seek(float p_pos,bool p_drag) {
  799. if (!is_visible())
  800. return;
  801. if (!player)
  802. return;
  803. if (player->is_playing() )
  804. return;
  805. updating=true;
  806. frame->set_val(p_pos);
  807. updating=false;
  808. _seek_value_changed(p_pos,!p_drag);
  809. EditorNode::get_singleton()->get_property_editor()->refresh();
  810. //seekit
  811. }
  812. void AnimationPlayerEditor::_hide_anim_editors() {
  813. player=NULL;
  814. hide();
  815. set_process(false);
  816. key_editor->set_animation(Ref<Animation>());
  817. key_editor->set_root(NULL);
  818. // editor->animation_editor_make_visible(false);
  819. }
  820. void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
  821. switch(p_option) {
  822. case TOOL_COPY_ANIM: {
  823. if (!animation->get_item_count()) {
  824. error_dialog->set_text(TTR("ERROR: No animation to copy!"));
  825. error_dialog->popup_centered_minsize();
  826. return;
  827. }
  828. String current = animation->get_item_text(animation->get_selected());
  829. Ref<Animation> anim = player->get_animation(current);
  830. //editor->edit_resource(anim);
  831. EditorSettings::get_singleton()->set_resource_clipboard(anim);
  832. } break;
  833. case TOOL_PASTE_ANIM: {
  834. Ref<Animation> anim = EditorSettings::get_singleton()->get_resource_clipboard();
  835. if (!anim.is_valid()) {
  836. error_dialog->set_text(TTR("ERROR: No animation resource on clipboard!"));
  837. error_dialog->popup_centered_minsize();
  838. return;
  839. }
  840. String name = anim->get_name();
  841. if (name=="") {
  842. name=TTR("Pasted Animation");
  843. }
  844. int idx=1;
  845. String base = name;
  846. while (player->has_animation(name)) {
  847. idx++;
  848. name=base+" "+itos(idx);
  849. }
  850. undo_redo->create_action(TTR("Paste Animation"));
  851. undo_redo->add_do_method(player,"add_animation",name,anim);
  852. undo_redo->add_undo_method(player,"remove_animation",name);
  853. undo_redo->add_do_method(this,"_animation_player_changed",player);
  854. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  855. undo_redo->commit_action();
  856. _select_anim_by_name(name);
  857. } break;
  858. case TOOL_EDIT_RESOURCE: {
  859. if (!animation->get_item_count()) {
  860. error_dialog->set_text(TTR("ERROR: No animation to edit!"));
  861. error_dialog->popup_centered_minsize();
  862. return;
  863. }
  864. String current = animation->get_item_text(animation->get_selected());
  865. Ref<Animation> anim = player->get_animation(current);
  866. editor->edit_resource(anim);
  867. } break;
  868. }
  869. }
  870. void AnimationPlayerEditor::_animation_save_menu(int p_option) {
  871. String current = animation->get_item_text(animation->get_selected());
  872. if (current != "") {
  873. Ref<Animation> anim = player->get_animation(current);
  874. switch (p_option) {
  875. case ANIM_SAVE:
  876. _animation_save(anim);
  877. break;
  878. case ANIM_SAVE_AS:
  879. _animation_save_as(anim);
  880. break;
  881. }
  882. }
  883. }
  884. void AnimationPlayerEditor::_unhandled_key_input(const InputEvent& p_ev) {
  885. if (is_visible() && p_ev.type==InputEvent::KEY && p_ev.key.pressed && !p_ev.key.echo && !p_ev.key.mod.alt && !p_ev.key.mod.control && !p_ev.key.mod.meta) {
  886. switch(p_ev.key.scancode) {
  887. case KEY_A: {
  888. if (!p_ev.key.mod.shift)
  889. _play_bw_from_pressed();
  890. else
  891. _play_bw_pressed();
  892. } break;
  893. case KEY_S: {
  894. _stop_pressed();
  895. } break;
  896. case KEY_D: {
  897. if (!p_ev.key.mod.shift)
  898. _play_from_pressed();
  899. else
  900. _play_pressed();
  901. } break;
  902. }
  903. }
  904. }
  905. void AnimationPlayerEditor::_bind_methods() {
  906. ObjectTypeDB::bind_method(_MD("_input_event"),&AnimationPlayerEditor::_input_event);
  907. ObjectTypeDB::bind_method(_MD("_node_removed"),&AnimationPlayerEditor::_node_removed);
  908. ObjectTypeDB::bind_method(_MD("_play_pressed"),&AnimationPlayerEditor::_play_pressed);
  909. ObjectTypeDB::bind_method(_MD("_play_from_pressed"),&AnimationPlayerEditor::_play_from_pressed);
  910. ObjectTypeDB::bind_method(_MD("_play_bw_pressed"),&AnimationPlayerEditor::_play_bw_pressed);
  911. ObjectTypeDB::bind_method(_MD("_play_bw_from_pressed"),&AnimationPlayerEditor::_play_bw_from_pressed);
  912. ObjectTypeDB::bind_method(_MD("_stop_pressed"),&AnimationPlayerEditor::_stop_pressed);
  913. ObjectTypeDB::bind_method(_MD("_autoplay_pressed"),&AnimationPlayerEditor::_autoplay_pressed);
  914. ObjectTypeDB::bind_method(_MD("_pause_pressed"),&AnimationPlayerEditor::_pause_pressed);
  915. ObjectTypeDB::bind_method(_MD("_animation_selected"),&AnimationPlayerEditor::_animation_selected);
  916. ObjectTypeDB::bind_method(_MD("_animation_name_edited"),&AnimationPlayerEditor::_animation_name_edited);
  917. ObjectTypeDB::bind_method(_MD("_animation_new"),&AnimationPlayerEditor::_animation_new);
  918. ObjectTypeDB::bind_method(_MD("_animation_rename"),&AnimationPlayerEditor::_animation_rename);
  919. ObjectTypeDB::bind_method(_MD("_animation_load"),&AnimationPlayerEditor::_animation_load);
  920. ObjectTypeDB::bind_method(_MD("_animation_remove"),&AnimationPlayerEditor::_animation_remove);
  921. ObjectTypeDB::bind_method(_MD("_animation_blend"),&AnimationPlayerEditor::_animation_blend);
  922. ObjectTypeDB::bind_method(_MD("_animation_edit"),&AnimationPlayerEditor::_animation_edit);
  923. ObjectTypeDB::bind_method(_MD("_animation_resource_edit"),&AnimationPlayerEditor::_animation_resource_edit);
  924. ObjectTypeDB::bind_method(_MD("_dialog_action"),&AnimationPlayerEditor::_dialog_action);
  925. ObjectTypeDB::bind_method(_MD("_seek_value_changed"),&AnimationPlayerEditor::_seek_value_changed,DEFVAL(true));
  926. ObjectTypeDB::bind_method(_MD("_animation_player_changed"),&AnimationPlayerEditor::_animation_player_changed);
  927. ObjectTypeDB::bind_method(_MD("_blend_edited"),&AnimationPlayerEditor::_blend_edited);
  928. // ObjectTypeDB::bind_method(_MD("_seek_frame_changed"),&AnimationPlayerEditor::_seek_frame_changed);
  929. ObjectTypeDB::bind_method(_MD("_scale_changed"),&AnimationPlayerEditor::_scale_changed);
  930. //ObjectTypeDB::bind_method(_MD("_editor_store_all"),&AnimationPlayerEditor::_editor_store_all);
  931. ///jectTypeDB::bind_method(_MD("_editor_load_all"),&AnimationPlayerEditor::_editor_load_all);
  932. ObjectTypeDB::bind_method(_MD("_list_changed"),&AnimationPlayerEditor::_list_changed);
  933. ObjectTypeDB::bind_method(_MD("_animation_key_editor_seek"),&AnimationPlayerEditor::_animation_key_editor_seek);
  934. ObjectTypeDB::bind_method(_MD("_animation_key_editor_anim_len_changed"),&AnimationPlayerEditor::_animation_key_editor_anim_len_changed);
  935. ObjectTypeDB::bind_method(_MD("_animation_key_editor_anim_step_changed"),&AnimationPlayerEditor::_animation_key_editor_anim_step_changed);
  936. ObjectTypeDB::bind_method(_MD("_hide_anim_editors"),&AnimationPlayerEditor::_hide_anim_editors);
  937. ObjectTypeDB::bind_method(_MD("_animation_duplicate"),&AnimationPlayerEditor::_animation_duplicate);
  938. ObjectTypeDB::bind_method(_MD("_blend_editor_next_changed"),&AnimationPlayerEditor::_blend_editor_next_changed);
  939. ObjectTypeDB::bind_method(_MD("_unhandled_key_input"),&AnimationPlayerEditor::_unhandled_key_input);
  940. ObjectTypeDB::bind_method(_MD("_animation_tool_menu"),&AnimationPlayerEditor::_animation_tool_menu);
  941. ObjectTypeDB::bind_method(_MD("_animation_save_menu"), &AnimationPlayerEditor::_animation_save_menu);
  942. }
  943. AnimationPlayerEditor *AnimationPlayerEditor::singleton=NULL;
  944. AnimationPlayer *AnimationPlayerEditor::get_player() const {
  945. return player;
  946. }
  947. AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor) {
  948. editor=p_editor;
  949. singleton=this;
  950. updating=false;
  951. set_focus_mode(FOCUS_ALL);
  952. player=NULL;
  953. add_style_override("panel", get_stylebox("panel","Panel"));
  954. Label * l;
  955. /*l= memnew( Label );
  956. l->set_text("Animation Player:");
  957. add_child(l);*/
  958. HBoxContainer *hb = memnew( HBoxContainer );
  959. add_child(hb);
  960. play_bw_from = memnew( ToolButton );
  961. play_bw_from->set_tooltip(TTR("Play selected animation backwards from current pos. (A)"));
  962. hb->add_child(play_bw_from);
  963. play_bw = memnew( ToolButton );
  964. play_bw->set_tooltip(TTR("Play selected animation backwards from end. (Shift+A)"));
  965. hb->add_child(play_bw);
  966. stop = memnew( ToolButton );
  967. stop->set_toggle_mode(true);
  968. hb->add_child(stop);
  969. stop->set_tooltip(TTR("Stop animation playback. (S)"));
  970. play = memnew( ToolButton );
  971. play->set_tooltip(TTR("Play selected animation from start. (Shift+D)"));
  972. hb->add_child(play);
  973. play_from = memnew( ToolButton );
  974. play_from->set_tooltip(TTR("Play selected animation from current pos. (D)"));
  975. hb->add_child(play_from);
  976. //pause = memnew( Button );
  977. //pause->set_toggle_mode(true);
  978. //hb->add_child(pause);
  979. frame = memnew( SpinBox );
  980. hb->add_child(frame);
  981. frame->set_custom_minimum_size(Size2(60,0));
  982. frame->set_stretch_ratio(2);
  983. frame->set_tooltip(TTR("Animation position (in seconds)."));
  984. hb->add_child( memnew( VSeparator));
  985. scale = memnew( LineEdit );
  986. hb->add_child(scale);
  987. scale->set_h_size_flags(SIZE_EXPAND_FILL);
  988. scale->set_stretch_ratio(1);
  989. scale->set_tooltip(TTR("Scale animation playback globally for the node."));
  990. scale->hide();
  991. add_anim = memnew( ToolButton );
  992. ED_SHORTCUT("animation_player_editor/add_animation", TTR("Create new animation in player."));
  993. add_anim->set_shortcut(ED_GET_SHORTCUT("animation_player_editor/add_animation"));
  994. add_anim->set_tooltip(TTR("Create new animation in player."));
  995. hb->add_child(add_anim);
  996. load_anim = memnew( ToolButton );
  997. ED_SHORTCUT("animation_player_editor/load_from_disk", TTR("Load animation from disk."));
  998. add_anim->set_shortcut(ED_GET_SHORTCUT("animation_player_editor/load_from_disk"));
  999. load_anim->set_tooltip(TTR("Load an animation from disk."));
  1000. hb->add_child(load_anim);
  1001. save_anim = memnew(MenuButton);
  1002. save_anim->set_tooltip(TTR("Save the current animation"));
  1003. save_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/save", TTR("Save")), ANIM_SAVE);
  1004. save_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/save_as", TTR("Save As")), ANIM_SAVE_AS);
  1005. save_anim->set_focus_mode(Control::FOCUS_NONE);
  1006. hb->add_child(save_anim);
  1007. accept = memnew(AcceptDialog);
  1008. add_child(accept);
  1009. accept->connect("confirmed", this, "_menu_confirm_current");
  1010. duplicate_anim = memnew( ToolButton );
  1011. hb->add_child(duplicate_anim);
  1012. ED_SHORTCUT("animation_player_editor/duplicate_animation", TTR("Duplicate Animation"));
  1013. duplicate_anim->set_shortcut(ED_GET_SHORTCUT("animation_player_editor/duplicate_animation"));
  1014. duplicate_anim->set_tooltip(TTR("Duplicate Animation"));
  1015. rename_anim = memnew( ToolButton );
  1016. hb->add_child(rename_anim);
  1017. ED_SHORTCUT("animation_player_editor/rename_animation", TTR("Rename Animation"));
  1018. rename_anim->set_shortcut(ED_GET_SHORTCUT("animation_player_editor/rename_animation"));
  1019. rename_anim->set_tooltip(TTR("Rename Animation"));
  1020. remove_anim = memnew( ToolButton );
  1021. hb->add_child(remove_anim);
  1022. ED_SHORTCUT("animation_player_editor/remove_animation", TTR("Remove Animation"));
  1023. remove_anim->set_shortcut(ED_GET_SHORTCUT("animation_player_editor/remove_animation"));
  1024. remove_anim->set_tooltip(TTR("Remove Animation"));
  1025. animation = memnew( OptionButton );
  1026. hb->add_child(animation);
  1027. animation->set_h_size_flags(SIZE_EXPAND_FILL);
  1028. animation->set_tooltip(TTR("Display list of animations in player."));
  1029. animation->set_clip_text(true);
  1030. autoplay = memnew( ToolButton );
  1031. hb->add_child(autoplay);
  1032. autoplay->set_tooltip(TTR("Autoplay on Load"));
  1033. blend_anim = memnew( ToolButton );
  1034. hb->add_child(blend_anim);
  1035. blend_anim->set_tooltip(TTR("Edit Target Blend Times"));
  1036. tool_anim = memnew( MenuButton);
  1037. //tool_anim->set_flat(false);
  1038. tool_anim->set_tooltip(TTR("Animation Tools"));
  1039. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/copy_animation", TTR("Copy Animation")),TOOL_COPY_ANIM);
  1040. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/paste_animation", TTR("Paste Animation")),TOOL_PASTE_ANIM);
  1041. //tool_anim->get_popup()->add_separator();
  1042. //tool_anim->get_popup()->add_item("Edit Anim Resource",TOOL_PASTE_ANIM);
  1043. hb->add_child(tool_anim);
  1044. nodename = memnew( Button );
  1045. hb->add_child(nodename);
  1046. pin = memnew( ToolButton );
  1047. pin->set_toggle_mode(true);
  1048. hb->add_child(pin);
  1049. resource_edit_anim= memnew( Button );
  1050. hb->add_child(resource_edit_anim);
  1051. resource_edit_anim->hide();
  1052. file = memnew(EditorFileDialog);
  1053. add_child(file);
  1054. name_dialog = memnew( ConfirmationDialog );
  1055. name_dialog->set_title(TTR("Create New Animation"));
  1056. name_dialog->set_hide_on_ok(false);
  1057. add_child(name_dialog);
  1058. name = memnew( LineEdit );
  1059. name_dialog->add_child(name);
  1060. name->set_pos(Point2(18,30));
  1061. name->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,10);
  1062. name_dialog->register_text_enter(name);
  1063. l = memnew( Label );
  1064. l->set_text(TTR("Animation Name:"));
  1065. l->set_pos( Point2(10,10) );
  1066. name_dialog->add_child(l);
  1067. name_title=l;
  1068. error_dialog = memnew( ConfirmationDialog );
  1069. error_dialog->get_ok()->set_text(TTR("Close"));
  1070. //error_dialog->get_cancel()->set_text("Close");
  1071. error_dialog->set_text(TTR("Error!"));
  1072. add_child(error_dialog);
  1073. name_dialog->connect("confirmed", this,"_animation_name_edited");
  1074. blend_editor.dialog = memnew( AcceptDialog );
  1075. add_child(blend_editor.dialog);
  1076. blend_editor.dialog->get_ok()->set_text(TTR("Close"));
  1077. blend_editor.dialog->set_hide_on_ok(true);
  1078. VBoxContainer *blend_vb = memnew( VBoxContainer);
  1079. blend_editor.dialog->add_child(blend_vb);
  1080. blend_editor.dialog->set_child_rect(blend_vb);
  1081. blend_editor.tree = memnew( Tree );
  1082. blend_editor.tree->set_columns(2);
  1083. blend_vb->add_margin_child(TTR("Blend Times:"),blend_editor.tree,true);
  1084. blend_editor.next = memnew( OptionButton );
  1085. blend_vb->add_margin_child(TTR("Next (Auto Queue):"),blend_editor.next);
  1086. blend_editor.dialog->set_title(TTR("Cross-Animation Blend Times"));
  1087. updating_blends=false;
  1088. blend_editor.tree->connect("item_edited",this,"_blend_edited");
  1089. autoplay->connect("pressed", this,"_autoplay_pressed");
  1090. autoplay->set_toggle_mode(true);
  1091. play->connect("pressed", this,"_play_pressed");
  1092. play_from->connect("pressed", this,"_play_from_pressed");
  1093. play_bw->connect("pressed", this,"_play_bw_pressed");
  1094. play_bw_from->connect("pressed", this,"_play_bw_from_pressed");
  1095. stop->connect("pressed", this,"_stop_pressed");
  1096. //pause->connect("pressed", this,"_pause_pressed");
  1097. add_anim->connect("pressed", this,"_animation_new");
  1098. rename_anim->connect("pressed", this,"_animation_rename");
  1099. load_anim->connect("pressed", this,"_animation_load");
  1100. duplicate_anim->connect("pressed", this,"_animation_duplicate");
  1101. //frame->connect("text_entered", this,"_seek_frame_changed");
  1102. blend_anim->connect("pressed", this,"_animation_blend");
  1103. remove_anim->connect("pressed", this,"_animation_remove");
  1104. animation->connect("item_selected", this,"_animation_selected",Vector<Variant>(),true);
  1105. resource_edit_anim->connect("pressed", this,"_animation_resource_edit");
  1106. file->connect("file_selected", this,"_dialog_action");
  1107. frame->connect("value_changed", this, "_seek_value_changed",Vector<Variant>(),true);
  1108. scale->connect("text_entered", this, "_scale_changed",Vector<Variant>(),true);
  1109. renaming=false;
  1110. last_active=false;
  1111. set_process_unhandled_key_input(true);
  1112. key_editor = memnew( AnimationKeyEditor);
  1113. add_child(key_editor);
  1114. add_constant_override("separation",get_constant("separation","VBoxContainer"));
  1115. key_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1116. key_editor->connect("timeline_changed",this,"_animation_key_editor_seek");
  1117. key_editor->connect("animation_len_changed",this,"_animation_key_editor_anim_len_changed");
  1118. key_editor->connect("animation_step_changed",this,"_animation_key_editor_anim_step_changed");
  1119. _update_player();
  1120. }
  1121. void AnimationPlayerEditorPlugin::edit(Object *p_object) {
  1122. anim_editor->set_undo_redo(&get_undo_redo());
  1123. if (!p_object)
  1124. return;
  1125. anim_editor->edit(p_object->cast_to<AnimationPlayer>());
  1126. }
  1127. bool AnimationPlayerEditorPlugin::handles(Object *p_object) const {
  1128. return p_object->is_type("AnimationPlayer");
  1129. }
  1130. void AnimationPlayerEditorPlugin::make_visible(bool p_visible) {
  1131. if (p_visible) {
  1132. editor->make_bottom_panel_item_visible(anim_editor);
  1133. anim_editor->set_process(true);
  1134. anim_editor->ensure_visibility();
  1135. // editor->animation_panel_make_visible(true);
  1136. } else {
  1137. // anim_editor->hide();
  1138. // anim_editor->set_idle_process(false);
  1139. }
  1140. }
  1141. AnimationPlayerEditorPlugin::AnimationPlayerEditorPlugin(EditorNode *p_node) {
  1142. editor=p_node;
  1143. anim_editor = memnew( AnimationPlayerEditor(editor) );
  1144. anim_editor->set_undo_redo(editor->get_undo_redo());
  1145. editor->add_bottom_panel_item(TTR("Animation"),anim_editor);
  1146. /*
  1147. editor->get_viewport()->add_child(anim_editor);
  1148. anim_editor->set_area_as_parent_rect();
  1149. anim_editor->set_anchor( MARGIN_TOP, Control::ANCHOR_END);
  1150. anim_editor->set_margin( MARGIN_TOP, 75 );
  1151. anim_editor->set_anchor( MARGIN_RIGHT, Control::ANCHOR_END);
  1152. anim_editor->set_margin( MARGIN_RIGHT, 0 );*/
  1153. anim_editor->hide();
  1154. }
  1155. AnimationPlayerEditorPlugin::~AnimationPlayerEditorPlugin()
  1156. {
  1157. }