animation_player_editor_plugin.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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("text_changed",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("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("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("New Animation Name:");
  241. int count=1;
  242. String base="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("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("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("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("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("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("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("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("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 String& p_string) {
  405. if (animation->get_item_count()==0)
  406. return;
  407. String current = animation->get_item_text(animation->get_selected());
  408. player->animation_set_next(current,p_string);
  409. }
  410. void AnimationPlayerEditor::_animation_blend() {
  411. if (updating_blends)
  412. return;
  413. blend_editor.tree->clear();
  414. if (animation->get_item_count()==0)
  415. return;
  416. String current = animation->get_item_text(animation->get_selected());
  417. blend_editor.dialog->popup_centered(Size2(400,400));
  418. blend_editor.tree->set_hide_root(true);
  419. blend_editor.tree->set_column_min_width(0,10);
  420. blend_editor.tree->set_column_min_width(1,3);
  421. List<StringName> anims;
  422. player->get_animation_list(&anims);
  423. TreeItem *root = blend_editor.tree->create_item();
  424. updating_blends=true;
  425. for(List<StringName>::Element *E=anims.front();E;E=E->next()) {
  426. String to=E->get();
  427. TreeItem *blend=blend_editor.tree->create_item(root);
  428. blend->set_editable(0,false);
  429. blend->set_editable(1,true);
  430. blend->set_text(0,to);
  431. blend->set_cell_mode(1,TreeItem::CELL_MODE_RANGE);
  432. blend->set_range_config(1,0,3600,0.001);
  433. blend->set_range(1,player->get_blend_time(current,to));
  434. }
  435. blend_editor.next->set_text( player->animation_get_next(current) );
  436. updating_blends=false;
  437. }
  438. void AnimationPlayerEditor::_blend_edited() {
  439. if (updating_blends)
  440. return;
  441. if (animation->get_item_count()==0)
  442. return;
  443. String current = animation->get_item_text(animation->get_selected());
  444. TreeItem *selected = blend_editor.tree->get_edited();
  445. if (!selected)
  446. return;
  447. updating_blends=true;
  448. String to=selected->get_text(0);
  449. float blend_time = selected->get_range(1);
  450. float prev_blend_time = player->get_blend_time(current,to);
  451. undo_redo->create_action("Change Blend Time");
  452. undo_redo->add_do_method(player,"set_blend_time",current,to,blend_time);
  453. undo_redo->add_undo_method(player,"set_blend_time",current,to,prev_blend_time);
  454. undo_redo->add_do_method(this,"_animation_player_changed",player);
  455. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  456. undo_redo->commit_action();
  457. updating_blends=false;
  458. }
  459. void AnimationPlayerEditor::ensure_visibility() {
  460. if (player && pin->is_pressed())
  461. return; // another player is pinned, don't reset
  462. _animation_edit();
  463. }
  464. Dictionary AnimationPlayerEditor::get_state() const {
  465. Dictionary d;
  466. d["visible"]=is_visible();
  467. if (EditorNode::get_singleton()->get_edited_scene() && is_visible() && player) {
  468. d["player"]=EditorNode::get_singleton()->get_edited_scene()->get_path_to(player);
  469. d["animation"]=player->get_current_animation();
  470. }
  471. return d;
  472. }
  473. void AnimationPlayerEditor::set_state(const Dictionary& p_state) {
  474. if (p_state.has("visible") && p_state["visible"]) {
  475. if (!EditorNode::get_singleton()->get_edited_scene())
  476. return;
  477. Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
  478. if (n && n->cast_to<AnimationPlayer>()) {
  479. player=n->cast_to<AnimationPlayer>();
  480. _update_player();
  481. show();
  482. set_process(true);
  483. ensure_visibility();
  484. // EditorNode::get_singleton()->animation_panel_make_visible(true);
  485. if (p_state.has("animation")) {
  486. String anim = p_state["animation"];
  487. _select_anim_by_name(anim);
  488. _animation_edit();
  489. }
  490. }
  491. }
  492. }
  493. void AnimationPlayerEditor::_animation_resource_edit() {
  494. if (animation->get_item_count()) {
  495. String current = animation->get_item_text(animation->get_selected());
  496. Ref<Animation> anim = player->get_animation(current);
  497. editor->edit_resource(anim);
  498. }
  499. }
  500. void AnimationPlayerEditor::_animation_edit() {
  501. if (animation->get_item_count()) {
  502. String current = animation->get_item_text(animation->get_selected());
  503. Ref<Animation> anim = player->get_animation(current);
  504. key_editor->set_animation(anim);
  505. Node *root = player->get_node(player->get_root());
  506. if (root) {
  507. key_editor->set_root(root);
  508. }
  509. } else {
  510. key_editor->set_animation(Ref<Animation>());
  511. key_editor->set_root(NULL);
  512. }
  513. }
  514. void AnimationPlayerEditor::_dialog_action(String p_file) {
  515. switch (current_option) {
  516. case RESOURCE_LOAD: {
  517. ERR_FAIL_COND(!player);
  518. Ref<Resource> res = ResourceLoader::load(p_file, "Animation");
  519. ERR_FAIL_COND(res.is_null());
  520. ERR_FAIL_COND(!res->is_type("Animation"));
  521. if (p_file.find_last("/") != -1) {
  522. p_file = p_file.substr(p_file.find_last("/") + 1, p_file.length());
  523. }
  524. if (p_file.find_last("\\") != -1) {
  525. p_file = p_file.substr(p_file.find_last("\\") + 1, p_file.length());
  526. }
  527. if (p_file.find(".") != -1)
  528. p_file = p_file.substr(0, p_file.find("."));
  529. undo_redo->create_action("Load Animation");
  530. undo_redo->add_do_method(player, "add_animation", p_file, res);
  531. undo_redo->add_undo_method(player, "remove_animation", p_file);
  532. if (player->has_animation(p_file)) {
  533. undo_redo->add_undo_method(player, "add_animation", p_file, player->get_animation(p_file));
  534. }
  535. undo_redo->add_do_method(this, "_animation_player_changed", player);
  536. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  537. undo_redo->commit_action();
  538. break;
  539. }
  540. case RESOURCE_SAVE: {
  541. String current = animation->get_item_text(animation->get_selected());
  542. if (current != "") {
  543. Ref<Animation> anim = player->get_animation(current);
  544. ERR_FAIL_COND(!anim->cast_to<Resource>())
  545. RES current_res = RES(anim->cast_to<Resource>());
  546. _animation_save_in_path(current_res, p_file);
  547. }
  548. }
  549. }
  550. }
  551. void AnimationPlayerEditor::_scale_changed(const String& p_scale) {
  552. player->set_speed(p_scale.to_double());
  553. }
  554. void AnimationPlayerEditor::_update_animation() {
  555. // the purpose of _update_animation is to reflect the current state
  556. // of the animation player in the current editor..
  557. updating=true;
  558. if (player->is_playing()) {
  559. play->set_pressed(true);
  560. stop->set_pressed(false);
  561. } else {
  562. play->set_pressed(false);
  563. stop->set_pressed(true);
  564. }
  565. scale->set_text( String::num(player->get_speed(),2) );
  566. String current=player->get_current_animation();
  567. for (int i=0;i<animation->get_item_count();i++) {
  568. if (animation->get_item_text(i)==current) {
  569. animation->select(i);
  570. break;
  571. }
  572. }
  573. updating=false;
  574. }
  575. void AnimationPlayerEditor::_update_player() {
  576. updating=true;
  577. List<StringName> animlist;
  578. if (player)
  579. player->get_animation_list(&animlist);
  580. animation->clear();
  581. if (player)
  582. nodename->set_text(player->get_name());
  583. else
  584. nodename->set_text("<empty>");
  585. add_anim->set_disabled(player==NULL);
  586. load_anim->set_disabled(player==NULL);
  587. stop->set_disabled(animlist.size()==0);
  588. play->set_disabled(animlist.size()==0);
  589. play_bw->set_disabled(animlist.size()==0);
  590. play_bw_from->set_disabled(animlist.size()==0);
  591. play_from->set_disabled(animlist.size()==0);
  592. autoplay->set_disabled(animlist.size()==0);
  593. duplicate_anim->set_disabled(animlist.size()==0);
  594. rename_anim->set_disabled(animlist.size()==0);
  595. blend_anim->set_disabled(animlist.size()==0);
  596. remove_anim->set_disabled(animlist.size()==0);
  597. resource_edit_anim->set_disabled(animlist.size()==0);
  598. save_anim->set_disabled(animlist.size() == 0);
  599. int active_idx=-1;
  600. for (List<StringName>::Element *E=animlist.front();E;E=E->next()) {
  601. if (player->get_autoplay()==E->get())
  602. animation->add_icon_item(autoplay_icon,E->get());
  603. else
  604. animation->add_item(E->get());
  605. if (player->get_current_animation()==E->get())
  606. active_idx=animation->get_item_count()-1;
  607. }
  608. if (!player)
  609. return;
  610. updating=false;
  611. if (active_idx!=-1) {
  612. animation->select(active_idx);
  613. autoplay->set_pressed(animation->get_item_text(active_idx)==player->get_autoplay());
  614. _animation_selected(active_idx);
  615. } else if (animation->get_item_count()>0){
  616. animation->select(0);
  617. autoplay->set_pressed(animation->get_item_text(0)==player->get_autoplay());
  618. _animation_selected(0);
  619. }
  620. //pause->set_pressed(player->is_paused());
  621. if (animation->get_item_count()) {
  622. String current = animation->get_item_text(animation->get_selected());
  623. Ref<Animation> anim = player->get_animation(current);
  624. key_editor->set_animation(anim);
  625. Node *root = player->get_node(player->get_root());
  626. if (root) {
  627. key_editor->set_root(root);
  628. }
  629. }
  630. _update_animation();
  631. }
  632. void AnimationPlayerEditor::edit(AnimationPlayer *p_player) {
  633. if (player && pin->is_pressed())
  634. return; //ignore, pinned
  635. player=p_player;
  636. if (player)
  637. _update_player();
  638. else {
  639. // hide();
  640. }
  641. }
  642. void AnimationPlayerEditor::_animation_duplicate() {
  643. if (!animation->get_item_count())
  644. return;
  645. String current = animation->get_item_text(animation->get_selected());
  646. Ref<Animation> anim = player->get_animation(current);
  647. if (!anim.is_valid())
  648. return;
  649. Ref<Animation> new_anim = memnew( Animation );
  650. List<PropertyInfo> plist;
  651. anim->get_property_list(&plist);
  652. for (List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  653. if (E->get().usage&PROPERTY_USAGE_STORAGE) {
  654. new_anim->set(E->get().name, anim->get(E->get().name));
  655. }
  656. }
  657. new_anim->set_path("");
  658. String new_name = current;
  659. while(player->has_animation(new_name)) {
  660. new_name=new_name+" (copy)";
  661. }
  662. undo_redo->create_action("Duplicate Animation");
  663. undo_redo->add_do_method(player,"add_animation",new_name,new_anim);
  664. undo_redo->add_undo_method(player,"remove_animation",new_name);
  665. undo_redo->add_do_method(this,"_animation_player_changed",player);
  666. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  667. undo_redo->commit_action();
  668. for(int i=0;i<animation->get_item_count();i++) {
  669. if (animation->get_item_text(i)==new_name) {
  670. animation->select(i);
  671. _animation_selected(i);
  672. return;
  673. }
  674. }
  675. }
  676. void AnimationPlayerEditor::_seek_value_changed(float p_value) {
  677. if (updating || !player || player->is_playing()) {
  678. return;
  679. };
  680. updating=true;
  681. String current=player->get_current_animation(); //animation->get_item_text( animation->get_selected() );
  682. if (current == "" || !player->has_animation(current)) {
  683. updating=false;
  684. current="";
  685. return;
  686. };
  687. Ref<Animation> anim;
  688. anim=player->get_animation(current);
  689. float pos = anim->get_length() * (p_value / frame->get_max());
  690. float step = anim->get_step();
  691. if (step) {
  692. pos=Math::stepify(pos, step);
  693. if (pos<0)
  694. pos=0;
  695. if (pos>=anim->get_length())
  696. pos=anim->get_length();
  697. }
  698. if (player->is_valid()) {
  699. float cpos = player->get_current_animation_pos();
  700. player->seek_delta(pos,pos-cpos);
  701. } else {
  702. player->seek(pos,true);
  703. }
  704. key_editor->set_anim_pos(pos);
  705. updating=true;
  706. };
  707. void AnimationPlayerEditor::_animation_player_changed(Object *p_pl) {
  708. if (player==p_pl && is_visible()) {
  709. _update_player();
  710. if (blend_editor.dialog->is_visible())
  711. _animation_blend(); //update
  712. }
  713. }
  714. void AnimationPlayerEditor::_list_changed() {
  715. if(is_visible())
  716. _update_player();
  717. }
  718. #if 0
  719. void AnimationPlayerEditor::_editor_store() {
  720. if (animation->get_item_count()==0)
  721. return;
  722. String current = animation->get_item_text(animation->get_selected());
  723. Ref<Animation> anim = player->get_animation(current);
  724. if (key_editor->get_current_animation()==anim)
  725. return; //already there
  726. undo_redo->create_action("Store anim in editor");
  727. undo_redo->add_do_method(key_editor,"set_animation",anim);
  728. undo_redo->add_undo_method(key_editor,"remove_animation",anim);
  729. undo_redo->commit_action();
  730. }
  731. void AnimationPlayerEditor::_editor_load(){
  732. Ref<Animation> anim = key_editor->get_current_animation();
  733. if (anim.is_null())
  734. return;
  735. String existing = player->find_animation(anim);
  736. if (existing!="") {
  737. _select_anim_by_name(existing);
  738. return; //already has
  739. }
  740. int count=1;
  741. String base=anim->get_name();
  742. bool noname=false;
  743. if (base=="") {
  744. base="New Anim";
  745. noname=true;
  746. }
  747. while(true) {
  748. String attempt = base;
  749. if (count>1)
  750. attempt+=" ("+itos(count)+")";
  751. if (player->has_animation(attempt)) {
  752. count++;
  753. continue;
  754. }
  755. base=attempt;
  756. break;
  757. }
  758. if (noname)
  759. anim->set_name(base);
  760. undo_redo->create_action("Add Animation From Editor");
  761. undo_redo->add_do_method(player,"add_animation",base,anim);
  762. undo_redo->add_undo_method(player,"remove_animation",base);
  763. undo_redo->add_do_method(this,"_animation_player_changed",player);
  764. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  765. undo_redo->commit_action();
  766. _select_anim_by_name(base);
  767. }
  768. #endif
  769. void AnimationPlayerEditor::_animation_key_editor_anim_len_changed(float p_len) {
  770. frame->set_max(p_len);
  771. }
  772. void AnimationPlayerEditor::_animation_key_editor_anim_step_changed(float p_len) {
  773. if (p_len)
  774. frame->set_step(p_len);
  775. else
  776. frame->set_step(0.00001);
  777. }
  778. void AnimationPlayerEditor::_animation_key_editor_seek(float p_pos) {
  779. if (!is_visible())
  780. return;
  781. if (!player)
  782. return;
  783. if (player->is_playing() )
  784. return;
  785. frame->set_val(p_pos);
  786. EditorNode::get_singleton()->get_property_editor()->refresh();
  787. //seekit
  788. }
  789. void AnimationPlayerEditor::_hide_anim_editors() {
  790. player=NULL;
  791. hide();
  792. set_process(false);
  793. key_editor->set_animation(Ref<Animation>());
  794. key_editor->set_root(NULL);
  795. // editor->animation_editor_make_visible(false);
  796. }
  797. void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
  798. switch(p_option) {
  799. case TOOL_COPY_ANIM: {
  800. if (!animation->get_item_count()) {
  801. error_dialog->set_text("ERROR: No animation to copy!");
  802. error_dialog->popup_centered_minsize();
  803. return;
  804. }
  805. String current = animation->get_item_text(animation->get_selected());
  806. Ref<Animation> anim = player->get_animation(current);
  807. //editor->edit_resource(anim);
  808. EditorSettings::get_singleton()->set_resource_clipboard(anim);
  809. } break;
  810. case TOOL_PASTE_ANIM: {
  811. Ref<Animation> anim = EditorSettings::get_singleton()->get_resource_clipboard();
  812. if (!anim.is_valid()) {
  813. error_dialog->set_text("ERROR: No animation resource on clipboard!");
  814. error_dialog->popup_centered_minsize();
  815. return;
  816. }
  817. String name = anim->get_name();
  818. if (name=="") {
  819. name="Pasted Animation";
  820. }
  821. int idx=1;
  822. String base = name;
  823. while (player->has_animation(name)) {
  824. idx++;
  825. name=base+" "+itos(idx);
  826. }
  827. undo_redo->create_action("Paste Animation");
  828. undo_redo->add_do_method(player,"add_animation",name,anim);
  829. undo_redo->add_undo_method(player,"remove_animation",name);
  830. undo_redo->add_do_method(this,"_animation_player_changed",player);
  831. undo_redo->add_undo_method(this,"_animation_player_changed",player);
  832. undo_redo->commit_action();
  833. _select_anim_by_name(name);
  834. } break;
  835. case TOOL_EDIT_RESOURCE: {
  836. if (!animation->get_item_count()) {
  837. error_dialog->set_text("ERROR: No animation to edit!");
  838. error_dialog->popup_centered_minsize();
  839. return;
  840. }
  841. String current = animation->get_item_text(animation->get_selected());
  842. Ref<Animation> anim = player->get_animation(current);
  843. editor->edit_resource(anim);
  844. } break;
  845. }
  846. }
  847. void AnimationPlayerEditor::_animation_save_menu(int p_option) {
  848. String current = animation->get_item_text(animation->get_selected());
  849. if (current != "") {
  850. Ref<Animation> anim = player->get_animation(current);
  851. switch (p_option) {
  852. case ANIM_SAVE:
  853. _animation_save(anim);
  854. break;
  855. case ANIM_SAVE_AS:
  856. _animation_save_as(anim);
  857. break;
  858. }
  859. }
  860. }
  861. void AnimationPlayerEditor::_unhandled_key_input(const InputEvent& p_ev) {
  862. 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) {
  863. switch(p_ev.key.scancode) {
  864. case KEY_A: {
  865. if (!p_ev.key.mod.shift)
  866. _play_bw_from_pressed();
  867. else
  868. _play_bw_pressed();
  869. } break;
  870. case KEY_S: {
  871. _stop_pressed();
  872. } break;
  873. case KEY_D: {
  874. if (!p_ev.key.mod.shift)
  875. _play_from_pressed();
  876. else
  877. _play_pressed();
  878. } break;
  879. }
  880. }
  881. }
  882. void AnimationPlayerEditor::_bind_methods() {
  883. ObjectTypeDB::bind_method(_MD("_input_event"),&AnimationPlayerEditor::_input_event);
  884. ObjectTypeDB::bind_method(_MD("_node_removed"),&AnimationPlayerEditor::_node_removed);
  885. ObjectTypeDB::bind_method(_MD("_play_pressed"),&AnimationPlayerEditor::_play_pressed);
  886. ObjectTypeDB::bind_method(_MD("_play_from_pressed"),&AnimationPlayerEditor::_play_from_pressed);
  887. ObjectTypeDB::bind_method(_MD("_play_bw_pressed"),&AnimationPlayerEditor::_play_bw_pressed);
  888. ObjectTypeDB::bind_method(_MD("_play_bw_from_pressed"),&AnimationPlayerEditor::_play_bw_from_pressed);
  889. ObjectTypeDB::bind_method(_MD("_stop_pressed"),&AnimationPlayerEditor::_stop_pressed);
  890. ObjectTypeDB::bind_method(_MD("_autoplay_pressed"),&AnimationPlayerEditor::_autoplay_pressed);
  891. ObjectTypeDB::bind_method(_MD("_pause_pressed"),&AnimationPlayerEditor::_pause_pressed);
  892. ObjectTypeDB::bind_method(_MD("_animation_selected"),&AnimationPlayerEditor::_animation_selected);
  893. ObjectTypeDB::bind_method(_MD("_animation_name_edited"),&AnimationPlayerEditor::_animation_name_edited);
  894. ObjectTypeDB::bind_method(_MD("_animation_new"),&AnimationPlayerEditor::_animation_new);
  895. ObjectTypeDB::bind_method(_MD("_animation_rename"),&AnimationPlayerEditor::_animation_rename);
  896. ObjectTypeDB::bind_method(_MD("_animation_load"),&AnimationPlayerEditor::_animation_load);
  897. ObjectTypeDB::bind_method(_MD("_animation_remove"),&AnimationPlayerEditor::_animation_remove);
  898. ObjectTypeDB::bind_method(_MD("_animation_blend"),&AnimationPlayerEditor::_animation_blend);
  899. ObjectTypeDB::bind_method(_MD("_animation_edit"),&AnimationPlayerEditor::_animation_edit);
  900. ObjectTypeDB::bind_method(_MD("_animation_resource_edit"),&AnimationPlayerEditor::_animation_resource_edit);
  901. ObjectTypeDB::bind_method(_MD("_dialog_action"),&AnimationPlayerEditor::_dialog_action);
  902. ObjectTypeDB::bind_method(_MD("_seek_value_changed"),&AnimationPlayerEditor::_seek_value_changed);
  903. ObjectTypeDB::bind_method(_MD("_animation_player_changed"),&AnimationPlayerEditor::_animation_player_changed);
  904. ObjectTypeDB::bind_method(_MD("_blend_edited"),&AnimationPlayerEditor::_blend_edited);
  905. // ObjectTypeDB::bind_method(_MD("_seek_frame_changed"),&AnimationPlayerEditor::_seek_frame_changed);
  906. ObjectTypeDB::bind_method(_MD("_scale_changed"),&AnimationPlayerEditor::_scale_changed);
  907. //ObjectTypeDB::bind_method(_MD("_editor_store_all"),&AnimationPlayerEditor::_editor_store_all);
  908. ///jectTypeDB::bind_method(_MD("_editor_load_all"),&AnimationPlayerEditor::_editor_load_all);
  909. ObjectTypeDB::bind_method(_MD("_list_changed"),&AnimationPlayerEditor::_list_changed);
  910. ObjectTypeDB::bind_method(_MD("_animation_key_editor_seek"),&AnimationPlayerEditor::_animation_key_editor_seek);
  911. ObjectTypeDB::bind_method(_MD("_animation_key_editor_anim_len_changed"),&AnimationPlayerEditor::_animation_key_editor_anim_len_changed);
  912. ObjectTypeDB::bind_method(_MD("_animation_key_editor_anim_step_changed"),&AnimationPlayerEditor::_animation_key_editor_anim_step_changed);
  913. ObjectTypeDB::bind_method(_MD("_hide_anim_editors"),&AnimationPlayerEditor::_hide_anim_editors);
  914. ObjectTypeDB::bind_method(_MD("_animation_duplicate"),&AnimationPlayerEditor::_animation_duplicate);
  915. ObjectTypeDB::bind_method(_MD("_blend_editor_next_changed"),&AnimationPlayerEditor::_blend_editor_next_changed);
  916. ObjectTypeDB::bind_method(_MD("_unhandled_key_input"),&AnimationPlayerEditor::_unhandled_key_input);
  917. ObjectTypeDB::bind_method(_MD("_animation_tool_menu"),&AnimationPlayerEditor::_animation_tool_menu);
  918. ObjectTypeDB::bind_method(_MD("_animation_save_menu"), &AnimationPlayerEditor::_animation_save_menu);
  919. }
  920. AnimationPlayerEditor *AnimationPlayerEditor::singleton=NULL;
  921. AnimationPlayer *AnimationPlayerEditor::get_player() const {
  922. return player;
  923. }
  924. AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor) {
  925. editor=p_editor;
  926. singleton=this;
  927. updating=false;
  928. set_focus_mode(FOCUS_ALL);
  929. player=NULL;
  930. add_style_override("panel", get_stylebox("panel","Panel"));
  931. Label * l;
  932. /*l= memnew( Label );
  933. l->set_text("Animation Player:");
  934. add_child(l);*/
  935. HBoxContainer *hb = memnew( HBoxContainer );
  936. add_child(hb);
  937. play_bw_from = memnew( ToolButton );
  938. play_bw_from->set_tooltip("Play backwards selected animation from current pos. (A)");
  939. hb->add_child(play_bw_from);
  940. play_bw = memnew( ToolButton );
  941. play_bw->set_tooltip("Play backwards selected animation from end. (Shift+A)");
  942. hb->add_child(play_bw);
  943. stop = memnew( ToolButton );
  944. stop->set_toggle_mode(true);
  945. hb->add_child(stop);
  946. stop->set_tooltip("Stop animation playback. (S)");
  947. play = memnew( ToolButton );
  948. play->set_tooltip("Play selected animation from start. (Shift+D)");
  949. hb->add_child(play);
  950. play_from = memnew( ToolButton );
  951. play_from->set_tooltip("Play selected animation from current pos. (D)");
  952. hb->add_child(play_from);
  953. //pause = memnew( Button );
  954. //pause->set_toggle_mode(true);
  955. //hb->add_child(pause);
  956. frame = memnew( SpinBox );
  957. hb->add_child(frame);
  958. frame->set_custom_minimum_size(Size2(60,0));
  959. frame->set_stretch_ratio(2);
  960. frame->set_tooltip("Animation position (in seconds).");
  961. hb->add_child( memnew( VSeparator));
  962. scale = memnew( LineEdit );
  963. hb->add_child(scale);
  964. scale->set_h_size_flags(SIZE_EXPAND_FILL);
  965. scale->set_stretch_ratio(1);
  966. scale->set_tooltip("Scale animation playback globally for the node.");
  967. scale->hide();
  968. add_anim = memnew( ToolButton );
  969. add_anim->set_tooltip("Create new animation in player.");
  970. hb->add_child(add_anim);
  971. load_anim = memnew( ToolButton );
  972. load_anim->set_tooltip("Load an animation from disk.");
  973. hb->add_child(load_anim);
  974. save_anim = memnew(MenuButton);
  975. save_anim->set_tooltip("Save the current animation");
  976. save_anim->get_popup()->add_item("Save", ANIM_SAVE);
  977. save_anim->get_popup()->add_item("Save As..", ANIM_SAVE_AS);
  978. save_anim->set_focus_mode(Control::FOCUS_NONE);
  979. hb->add_child(save_anim);
  980. accept = memnew(AcceptDialog);
  981. add_child(accept);
  982. accept->connect("confirmed", this, "_menu_confirm_current");
  983. duplicate_anim = memnew( ToolButton );
  984. hb->add_child(duplicate_anim);
  985. duplicate_anim->set_tooltip("Duplicate Animation");
  986. rename_anim = memnew( ToolButton );
  987. hb->add_child(rename_anim);
  988. rename_anim->set_tooltip("Rename Animation");
  989. remove_anim = memnew( ToolButton );
  990. hb->add_child(remove_anim);
  991. remove_anim->set_tooltip("Remove Animation");
  992. animation = memnew( OptionButton );
  993. hb->add_child(animation);
  994. animation->set_h_size_flags(SIZE_EXPAND_FILL);
  995. animation->set_tooltip("Display list of animations in player.");
  996. autoplay = memnew( ToolButton );
  997. hb->add_child(autoplay);
  998. autoplay->set_tooltip("Autoplay On Load");
  999. blend_anim = memnew( ToolButton );
  1000. hb->add_child(blend_anim);
  1001. blend_anim->set_tooltip("Edit Target Blend Times");
  1002. tool_anim = memnew( MenuButton);
  1003. //tool_anim->set_flat(false);
  1004. tool_anim->set_tooltip("Animation Tools");
  1005. tool_anim->get_popup()->add_item("Copy Animation",TOOL_COPY_ANIM);
  1006. tool_anim->get_popup()->add_item("Paste Animation",TOOL_PASTE_ANIM);
  1007. //tool_anim->get_popup()->add_separator();
  1008. //tool_anim->get_popup()->add_item("Edit Anim Resource",TOOL_PASTE_ANIM);
  1009. hb->add_child(tool_anim);
  1010. nodename = memnew( Button );
  1011. hb->add_child(nodename);
  1012. pin = memnew( ToolButton );
  1013. pin->set_toggle_mode(true);
  1014. hb->add_child(pin);
  1015. resource_edit_anim= memnew( Button );
  1016. hb->add_child(resource_edit_anim);
  1017. resource_edit_anim->hide();
  1018. file = memnew(EditorFileDialog);
  1019. add_child(file);
  1020. name_dialog = memnew( ConfirmationDialog );
  1021. name_dialog->set_title("Create New Animation");
  1022. name_dialog->set_hide_on_ok(false);
  1023. add_child(name_dialog);
  1024. name = memnew( LineEdit );
  1025. name_dialog->add_child(name);
  1026. name->set_pos(Point2(18,30));
  1027. name->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,10);
  1028. name_dialog->register_text_enter(name);
  1029. l = memnew( Label );
  1030. l->set_text("Animation Name:");
  1031. l->set_pos( Point2(10,10) );
  1032. name_dialog->add_child(l);
  1033. name_title=l;
  1034. error_dialog = memnew( ConfirmationDialog );
  1035. error_dialog->get_ok()->set_text("Close");
  1036. //error_dialog->get_cancel()->set_text("Close");
  1037. error_dialog->set_text("Error!");
  1038. add_child(error_dialog);
  1039. name_dialog->connect("confirmed", this,"_animation_name_edited");
  1040. blend_editor.dialog = memnew( AcceptDialog );
  1041. add_child(blend_editor.dialog);
  1042. blend_editor.dialog->get_ok()->set_text("Close");
  1043. blend_editor.dialog->set_hide_on_ok(true);
  1044. VBoxContainer *blend_vb = memnew( VBoxContainer);
  1045. blend_editor.dialog->add_child(blend_vb);
  1046. blend_editor.dialog->set_child_rect(blend_vb);
  1047. blend_editor.tree = memnew( Tree );
  1048. blend_editor.tree->set_columns(2);
  1049. blend_vb->add_margin_child("Blend Times: ",blend_editor.tree,true);
  1050. blend_editor.next = memnew( LineEdit );
  1051. blend_vb->add_margin_child("Next (Auto Queue):",blend_editor.next);
  1052. blend_editor.dialog->set_title("Cross-Animation Blend Times");
  1053. updating_blends=false;
  1054. blend_editor.tree->connect("item_edited",this,"_blend_edited");
  1055. autoplay->connect("pressed", this,"_autoplay_pressed");
  1056. autoplay->set_toggle_mode(true);
  1057. play->connect("pressed", this,"_play_pressed");
  1058. play_from->connect("pressed", this,"_play_from_pressed");
  1059. play_bw->connect("pressed", this,"_play_bw_pressed");
  1060. play_bw_from->connect("pressed", this,"_play_bw_from_pressed");
  1061. stop->connect("pressed", this,"_stop_pressed");
  1062. //pause->connect("pressed", this,"_pause_pressed");
  1063. add_anim->connect("pressed", this,"_animation_new");
  1064. rename_anim->connect("pressed", this,"_animation_rename");
  1065. load_anim->connect("pressed", this,"_animation_load");
  1066. duplicate_anim->connect("pressed", this,"_animation_duplicate");
  1067. //frame->connect("text_entered", this,"_seek_frame_changed");
  1068. blend_anim->connect("pressed", this,"_animation_blend");
  1069. remove_anim->connect("pressed", this,"_animation_remove");
  1070. animation->connect("item_selected", this,"_animation_selected",Vector<Variant>(),true);
  1071. resource_edit_anim->connect("pressed", this,"_animation_resource_edit");
  1072. file->connect("file_selected", this,"_dialog_action");
  1073. frame->connect("value_changed", this, "_seek_value_changed",Vector<Variant>(),true);
  1074. scale->connect("text_entered", this, "_scale_changed",Vector<Variant>(),true);
  1075. renaming=false;
  1076. last_active=false;
  1077. set_process_unhandled_key_input(true);
  1078. key_editor = memnew( AnimationKeyEditor);
  1079. add_child(key_editor);
  1080. add_constant_override("separation",get_constant("separation","VBoxContainer"));
  1081. key_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1082. key_editor->connect("timeline_changed",this,"_animation_key_editor_seek");
  1083. key_editor->connect("animation_len_changed",this,"_animation_key_editor_anim_len_changed");
  1084. key_editor->connect("animation_step_changed",this,"_animation_key_editor_anim_step_changed");
  1085. _update_player();
  1086. }
  1087. void AnimationPlayerEditorPlugin::edit(Object *p_object) {
  1088. anim_editor->set_undo_redo(&get_undo_redo());
  1089. if (!p_object)
  1090. return;
  1091. anim_editor->edit(p_object->cast_to<AnimationPlayer>());
  1092. }
  1093. bool AnimationPlayerEditorPlugin::handles(Object *p_object) const {
  1094. return p_object->is_type("AnimationPlayer");
  1095. }
  1096. void AnimationPlayerEditorPlugin::make_visible(bool p_visible) {
  1097. if (p_visible) {
  1098. editor->make_bottom_panel_item_visible(anim_editor);
  1099. anim_editor->set_process(true);
  1100. anim_editor->ensure_visibility();
  1101. // editor->animation_panel_make_visible(true);
  1102. } else {
  1103. // anim_editor->hide();
  1104. // anim_editor->set_idle_process(false);
  1105. }
  1106. }
  1107. AnimationPlayerEditorPlugin::AnimationPlayerEditorPlugin(EditorNode *p_node) {
  1108. editor=p_node;
  1109. anim_editor = memnew( AnimationPlayerEditor(editor) );
  1110. anim_editor->set_undo_redo(editor->get_undo_redo());
  1111. editor->add_bottom_panel_item("Animation",anim_editor);
  1112. /*
  1113. editor->get_viewport()->add_child(anim_editor);
  1114. anim_editor->set_area_as_parent_rect();
  1115. anim_editor->set_anchor( MARGIN_TOP, Control::ANCHOR_END);
  1116. anim_editor->set_margin( MARGIN_TOP, 75 );
  1117. anim_editor->set_anchor( MARGIN_RIGHT, Control::ANCHOR_END);
  1118. anim_editor->set_margin( MARGIN_RIGHT, 0 );*/
  1119. anim_editor->hide();
  1120. }
  1121. AnimationPlayerEditorPlugin::~AnimationPlayerEditorPlugin()
  1122. {
  1123. }