animation_player_editor_plugin.cpp 44 KB

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