animation_player_editor_plugin.cpp 46 KB

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