animation_player_editor_plugin.cpp 60 KB

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