animation_player_editor_plugin.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. /*************************************************************************/
  2. /* animation_player_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_player_editor_plugin.h"
  31. #include "core/input/input_filter.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/project_settings.h"
  36. #include "editor/animation_track_editor.h"
  37. #include "editor/editor_scale.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/plugins/canvas_item_editor_plugin.h" // For onion skinning.
  40. #include "editor/plugins/node_3d_editor_plugin.h" // For onion skinning.
  41. #include "scene/main/window.h"
  42. #include "servers/rendering_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", callable_mp(this, &AnimationPlayerEditor::_animation_tool_menu));
  84. onion_skinning->get_popup()->connect("id_pressed", callable_mp(this, &AnimationPlayerEditor::_onion_skinning_menu));
  85. blend_editor.next->connect("item_selected", callable_mp(this, &AnimationPlayerEditor::_blend_editor_next_changed));
  86. get_tree()->connect("node_removed", callable_mp(this, &AnimationPlayerEditor::_node_removed));
  87. add_theme_style_override("panel", editor->get_gui_base()->get_theme_stylebox("panel", "Panel"));
  88. } break;
  89. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  90. add_theme_style_override("panel", editor->get_gui_base()->get_theme_stylebox("panel", "Panel"));
  91. } break;
  92. case NOTIFICATION_THEME_CHANGED: {
  93. autoplay->set_icon(get_theme_icon("AutoPlay", "EditorIcons"));
  94. play->set_icon(get_theme_icon("PlayStart", "EditorIcons"));
  95. play_from->set_icon(get_theme_icon("Play", "EditorIcons"));
  96. play_bw->set_icon(get_theme_icon("PlayStartBackwards", "EditorIcons"));
  97. play_bw_from->set_icon(get_theme_icon("PlayBackwards", "EditorIcons"));
  98. autoplay_icon = get_theme_icon("AutoPlay", "EditorIcons");
  99. stop->set_icon(get_theme_icon("Stop", "EditorIcons"));
  100. onion_toggle->set_icon(get_theme_icon("Onion", "EditorIcons"));
  101. onion_skinning->set_icon(get_theme_icon("GuiTabMenu", "EditorIcons"));
  102. pin->set_icon(get_theme_icon("Pin", "EditorIcons"));
  103. tool_anim->add_theme_style_override("normal", get_theme_stylebox("normal", "Button"));
  104. track_editor->get_edit_menu()->add_theme_style_override("normal", get_theme_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_theme_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_file_mode(EditorFileDialog::FILE_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. EditorNode::get_singleton()->show_warning(TTR("Error saving resource!"));
  290. return;
  291. }
  292. ((Resource *)p_resource.ptr())->set_path(path);
  293. editor->emit_signal("resource_saved", p_resource);
  294. }
  295. void AnimationPlayerEditor::_animation_save(const Ref<Resource> &p_resource) {
  296. if (p_resource->get_path().is_resource_file()) {
  297. _animation_save_in_path(p_resource, p_resource->get_path());
  298. } else {
  299. _animation_save_as(p_resource);
  300. }
  301. }
  302. void AnimationPlayerEditor::_animation_save_as(const Ref<Resource> &p_resource) {
  303. file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  304. List<String> extensions;
  305. ResourceSaver::get_recognized_extensions(p_resource, &extensions);
  306. file->clear_filters();
  307. for (int i = 0; i < extensions.size(); i++) {
  308. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  309. }
  310. //file->set_current_path(current_path);
  311. if (p_resource->get_path() != "") {
  312. file->set_current_path(p_resource->get_path());
  313. if (extensions.size()) {
  314. String ext = p_resource->get_path().get_extension().to_lower();
  315. if (extensions.find(ext) == NULL) {
  316. file->set_current_path(p_resource->get_path().replacen("." + ext, "." + extensions.front()->get()));
  317. }
  318. }
  319. } else {
  320. String existing;
  321. if (extensions.size()) {
  322. if (p_resource->get_name() != "") {
  323. existing = p_resource->get_name() + "." + extensions.front()->get().to_lower();
  324. } else {
  325. existing = "new_" + p_resource->get_class().to_lower() + "." + extensions.front()->get().to_lower();
  326. }
  327. }
  328. file->set_current_path(existing);
  329. }
  330. file->popup_centered_ratio();
  331. file->set_title(TTR("Save Resource As..."));
  332. current_option = RESOURCE_SAVE;
  333. }
  334. void AnimationPlayerEditor::_animation_remove() {
  335. if (animation->get_item_count() == 0)
  336. return;
  337. delete_dialog->set_text(TTR("Delete Animation?"));
  338. delete_dialog->popup_centered();
  339. }
  340. void AnimationPlayerEditor::_animation_remove_confirmed() {
  341. String current = animation->get_item_text(animation->get_selected());
  342. Ref<Animation> anim = player->get_animation(current);
  343. undo_redo->create_action(TTR("Remove Animation"));
  344. if (player->get_autoplay() == current) {
  345. undo_redo->add_do_method(player, "set_autoplay", "");
  346. undo_redo->add_undo_method(player, "set_autoplay", current);
  347. // Avoid having the autoplay icon linger around if there is only one animation in the player.
  348. undo_redo->add_do_method(this, "_animation_player_changed", player);
  349. }
  350. undo_redo->add_do_method(player, "remove_animation", current);
  351. undo_redo->add_undo_method(player, "add_animation", current, anim);
  352. undo_redo->add_do_method(this, "_animation_player_changed", player);
  353. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  354. if (animation->get_item_count() == 1) {
  355. undo_redo->add_do_method(this, "_stop_onion_skinning");
  356. undo_redo->add_undo_method(this, "_start_onion_skinning");
  357. }
  358. undo_redo->commit_action();
  359. }
  360. void AnimationPlayerEditor::_select_anim_by_name(const String &p_anim) {
  361. int idx = -1;
  362. for (int i = 0; i < animation->get_item_count(); i++) {
  363. if (animation->get_item_text(i) == p_anim) {
  364. idx = i;
  365. break;
  366. }
  367. }
  368. ERR_FAIL_COND(idx == -1);
  369. animation->select(idx);
  370. _animation_selected(idx);
  371. }
  372. double AnimationPlayerEditor::_get_editor_step() const {
  373. // Returns the effective snapping value depending on snapping modifiers, or 0 if snapping is disabled.
  374. if (track_editor->is_snap_enabled()) {
  375. const String current = player->get_assigned_animation();
  376. const Ref<Animation> anim = player->get_animation(current);
  377. ERR_FAIL_COND_V(!anim.is_valid(), 0.0);
  378. // Use more precise snapping when holding Shift
  379. return InputFilter::get_singleton()->is_key_pressed(KEY_SHIFT) ? anim->get_step() * 0.25 : anim->get_step();
  380. }
  381. return 0.0;
  382. }
  383. void AnimationPlayerEditor::_animation_name_edited() {
  384. player->stop();
  385. String new_name = name->get_text();
  386. if (new_name == "" || new_name.find(":") != -1 || new_name.find("/") != -1) {
  387. error_dialog->set_text(TTR("Invalid animation name!"));
  388. error_dialog->popup_centered();
  389. return;
  390. }
  391. if (renaming && animation->get_item_count() > 0 && animation->get_item_text(animation->get_selected()) == new_name) {
  392. name_dialog->hide();
  393. return;
  394. }
  395. if (player->has_animation(new_name)) {
  396. error_dialog->set_text(TTR("Animation name already exists!"));
  397. error_dialog->popup_centered();
  398. return;
  399. }
  400. if (renaming) {
  401. String current = animation->get_item_text(animation->get_selected());
  402. Ref<Animation> anim = player->get_animation(current);
  403. undo_redo->create_action(TTR("Rename Animation"));
  404. undo_redo->add_do_method(player, "rename_animation", current, new_name);
  405. undo_redo->add_do_method(anim.ptr(), "set_name", new_name);
  406. undo_redo->add_undo_method(player, "rename_animation", new_name, current);
  407. undo_redo->add_undo_method(anim.ptr(), "set_name", current);
  408. undo_redo->add_do_method(this, "_animation_player_changed", player);
  409. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  410. undo_redo->commit_action();
  411. _select_anim_by_name(new_name);
  412. } else {
  413. Ref<Animation> new_anim = Ref<Animation>(memnew(Animation));
  414. new_anim->set_name(new_name);
  415. undo_redo->create_action(TTR("Add Animation"));
  416. undo_redo->add_do_method(player, "add_animation", new_name, new_anim);
  417. undo_redo->add_undo_method(player, "remove_animation", new_name);
  418. undo_redo->add_do_method(this, "_animation_player_changed", player);
  419. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  420. if (animation->get_item_count() == 0) {
  421. undo_redo->add_do_method(this, "_start_onion_skinning");
  422. undo_redo->add_undo_method(this, "_stop_onion_skinning");
  423. }
  424. undo_redo->commit_action();
  425. _select_anim_by_name(new_name);
  426. }
  427. name_dialog->hide();
  428. }
  429. void AnimationPlayerEditor::_blend_editor_next_changed(const int p_idx) {
  430. if (animation->get_item_count() == 0)
  431. return;
  432. String current = animation->get_item_text(animation->get_selected());
  433. undo_redo->create_action(TTR("Blend Next Changed"));
  434. undo_redo->add_do_method(player, "animation_set_next", current, blend_editor.next->get_item_text(p_idx));
  435. undo_redo->add_undo_method(player, "animation_set_next", current, player->animation_get_next(current));
  436. undo_redo->add_do_method(this, "_animation_player_changed", player);
  437. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  438. undo_redo->commit_action();
  439. }
  440. void AnimationPlayerEditor::_animation_blend() {
  441. if (updating_blends)
  442. return;
  443. blend_editor.tree->clear();
  444. if (animation->get_item_count() == 0)
  445. return;
  446. String current = animation->get_item_text(animation->get_selected());
  447. blend_editor.dialog->popup_centered(Size2(400, 400) * EDSCALE);
  448. blend_editor.tree->set_hide_root(true);
  449. blend_editor.tree->set_column_min_width(0, 10);
  450. blend_editor.tree->set_column_min_width(1, 3);
  451. List<StringName> anims;
  452. player->get_animation_list(&anims);
  453. TreeItem *root = blend_editor.tree->create_item();
  454. updating_blends = true;
  455. int i = 0;
  456. bool anim_found = false;
  457. blend_editor.next->clear();
  458. blend_editor.next->add_item("", i);
  459. for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
  460. String to = E->get();
  461. TreeItem *blend = blend_editor.tree->create_item(root);
  462. blend->set_editable(0, false);
  463. blend->set_editable(1, true);
  464. blend->set_text(0, to);
  465. blend->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  466. blend->set_range_config(1, 0, 3600, 0.001);
  467. blend->set_range(1, player->get_blend_time(current, to));
  468. i++;
  469. blend_editor.next->add_item(to, i);
  470. if (to == player->animation_get_next(current)) {
  471. blend_editor.next->select(i);
  472. anim_found = true;
  473. }
  474. }
  475. // make sure we reset it else it becomes out of sync and could contain a deleted animation
  476. if (!anim_found) {
  477. blend_editor.next->select(0);
  478. player->animation_set_next(current, blend_editor.next->get_item_text(0));
  479. }
  480. updating_blends = false;
  481. }
  482. void AnimationPlayerEditor::_blend_edited() {
  483. if (updating_blends)
  484. return;
  485. if (animation->get_item_count() == 0)
  486. return;
  487. String current = animation->get_item_text(animation->get_selected());
  488. TreeItem *selected = blend_editor.tree->get_edited();
  489. if (!selected)
  490. return;
  491. updating_blends = true;
  492. String to = selected->get_text(0);
  493. float blend_time = selected->get_range(1);
  494. float prev_blend_time = player->get_blend_time(current, to);
  495. undo_redo->create_action(TTR("Change Blend Time"));
  496. undo_redo->add_do_method(player, "set_blend_time", current, to, blend_time);
  497. undo_redo->add_undo_method(player, "set_blend_time", current, to, prev_blend_time);
  498. undo_redo->add_do_method(this, "_animation_player_changed", player);
  499. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  500. undo_redo->commit_action();
  501. updating_blends = false;
  502. }
  503. void AnimationPlayerEditor::ensure_visibility() {
  504. if (player && pin->is_pressed())
  505. return; // another player is pinned, don't reset
  506. _animation_edit();
  507. }
  508. Dictionary AnimationPlayerEditor::get_state() const {
  509. Dictionary d;
  510. d["visible"] = is_visible_in_tree();
  511. if (EditorNode::get_singleton()->get_edited_scene() && is_visible_in_tree() && player) {
  512. d["player"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(player);
  513. d["animation"] = player->get_assigned_animation();
  514. d["track_editor_state"] = track_editor->get_state();
  515. }
  516. return d;
  517. }
  518. void AnimationPlayerEditor::set_state(const Dictionary &p_state) {
  519. if (!p_state.has("visible") || !p_state["visible"]) {
  520. return;
  521. }
  522. if (!EditorNode::get_singleton()->get_edited_scene()) {
  523. return;
  524. }
  525. if (p_state.has("player")) {
  526. Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
  527. if (Object::cast_to<AnimationPlayer>(n) && EditorNode::get_singleton()->get_editor_selection()->is_selected(n)) {
  528. player = Object::cast_to<AnimationPlayer>(n);
  529. _update_player();
  530. editor->make_bottom_panel_item_visible(this);
  531. set_process(true);
  532. ensure_visibility();
  533. if (p_state.has("animation")) {
  534. String anim = p_state["animation"];
  535. if (!anim.empty() && player->has_animation(anim)) {
  536. _select_anim_by_name(anim);
  537. _animation_edit();
  538. }
  539. }
  540. }
  541. }
  542. if (p_state.has("track_editor_state")) {
  543. track_editor->set_state(p_state["track_editor_state"]);
  544. }
  545. }
  546. void AnimationPlayerEditor::_animation_resource_edit() {
  547. if (animation->get_item_count()) {
  548. String current = animation->get_item_text(animation->get_selected());
  549. Ref<Animation> anim = player->get_animation(current);
  550. editor->edit_resource(anim);
  551. }
  552. }
  553. void AnimationPlayerEditor::_animation_edit() {
  554. if (animation->get_item_count()) {
  555. String current = animation->get_item_text(animation->get_selected());
  556. Ref<Animation> anim = player->get_animation(current);
  557. track_editor->set_animation(anim);
  558. Node *root = player->get_node(player->get_root());
  559. if (root) {
  560. track_editor->set_root(root);
  561. }
  562. } else {
  563. track_editor->set_animation(Ref<Animation>());
  564. track_editor->set_root(NULL);
  565. }
  566. }
  567. void AnimationPlayerEditor::_dialog_action(String p_file) {
  568. switch (current_option) {
  569. case RESOURCE_LOAD: {
  570. ERR_FAIL_COND(!player);
  571. Ref<Resource> res = ResourceLoader::load(p_file, "Animation");
  572. ERR_FAIL_COND_MSG(res.is_null(), "Cannot load Animation from file '" + p_file + "'.");
  573. ERR_FAIL_COND_MSG(!res->is_class("Animation"), "Loaded resource from file '" + p_file + "' is not Animation.");
  574. if (p_file.find_last("/") != -1) {
  575. p_file = p_file.substr(p_file.find_last("/") + 1, p_file.length());
  576. }
  577. if (p_file.find_last("\\") != -1) {
  578. p_file = p_file.substr(p_file.find_last("\\") + 1, p_file.length());
  579. }
  580. if (p_file.find(".") != -1)
  581. p_file = p_file.substr(0, p_file.find("."));
  582. undo_redo->create_action(TTR("Load Animation"));
  583. undo_redo->add_do_method(player, "add_animation", p_file, res);
  584. undo_redo->add_undo_method(player, "remove_animation", p_file);
  585. if (player->has_animation(p_file)) {
  586. undo_redo->add_undo_method(player, "add_animation", p_file, player->get_animation(p_file));
  587. }
  588. undo_redo->add_do_method(this, "_animation_player_changed", player);
  589. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  590. undo_redo->commit_action();
  591. break;
  592. }
  593. case RESOURCE_SAVE: {
  594. String current = animation->get_item_text(animation->get_selected());
  595. if (current != "") {
  596. Ref<Animation> anim = player->get_animation(current);
  597. ERR_FAIL_COND(!Object::cast_to<Resource>(*anim));
  598. RES current_res = RES(Object::cast_to<Resource>(*anim));
  599. _animation_save_in_path(current_res, p_file);
  600. }
  601. }
  602. }
  603. }
  604. void AnimationPlayerEditor::_scale_changed(const String &p_scale) {
  605. player->set_speed_scale(p_scale.to_double());
  606. }
  607. void AnimationPlayerEditor::_update_animation() {
  608. // the purpose of _update_animation is to reflect the current state
  609. // of the animation player in the current editor..
  610. updating = true;
  611. if (player->is_playing()) {
  612. play->set_pressed(true);
  613. stop->set_pressed(false);
  614. } else {
  615. play->set_pressed(false);
  616. stop->set_pressed(true);
  617. }
  618. scale->set_text(String::num(player->get_speed_scale(), 2));
  619. String current = player->get_assigned_animation();
  620. for (int i = 0; i < animation->get_item_count(); i++) {
  621. if (animation->get_item_text(i) == current) {
  622. animation->select(i);
  623. break;
  624. }
  625. }
  626. updating = false;
  627. }
  628. void AnimationPlayerEditor::_update_player() {
  629. updating = true;
  630. List<StringName> animlist;
  631. if (player)
  632. player->get_animation_list(&animlist);
  633. animation->clear();
  634. #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)
  635. ITEM_DISABLED(TOOL_SAVE_ANIM, animlist.size() == 0);
  636. ITEM_DISABLED(TOOL_SAVE_AS_ANIM, animlist.size() == 0);
  637. ITEM_DISABLED(TOOL_DUPLICATE_ANIM, animlist.size() == 0);
  638. ITEM_DISABLED(TOOL_RENAME_ANIM, animlist.size() == 0);
  639. ITEM_DISABLED(TOOL_EDIT_TRANSITIONS, animlist.size() == 0);
  640. ITEM_DISABLED(TOOL_COPY_ANIM, animlist.size() == 0);
  641. ITEM_DISABLED(TOOL_REMOVE_ANIM, animlist.size() == 0);
  642. stop->set_disabled(animlist.size() == 0);
  643. play->set_disabled(animlist.size() == 0);
  644. play_bw->set_disabled(animlist.size() == 0);
  645. play_bw_from->set_disabled(animlist.size() == 0);
  646. play_from->set_disabled(animlist.size() == 0);
  647. frame->set_editable(animlist.size() != 0);
  648. animation->set_disabled(animlist.size() == 0);
  649. autoplay->set_disabled(animlist.size() == 0);
  650. tool_anim->set_disabled(player == NULL);
  651. onion_toggle->set_disabled(animlist.size() == 0);
  652. onion_skinning->set_disabled(animlist.size() == 0);
  653. pin->set_disabled(player == NULL);
  654. if (!player) {
  655. AnimationPlayerEditor::singleton->get_track_editor()->update_keying();
  656. EditorNode::get_singleton()->update_keying();
  657. return;
  658. }
  659. int active_idx = -1;
  660. for (List<StringName>::Element *E = animlist.front(); E; E = E->next()) {
  661. if (player->get_autoplay() == E->get())
  662. animation->add_icon_item(autoplay_icon, E->get());
  663. else
  664. animation->add_item(E->get());
  665. if (player->get_assigned_animation() == E->get())
  666. active_idx = animation->get_item_count() - 1;
  667. }
  668. updating = false;
  669. if (active_idx != -1) {
  670. animation->select(active_idx);
  671. autoplay->set_pressed(animation->get_item_text(active_idx) == player->get_autoplay());
  672. _animation_selected(active_idx);
  673. } else if (animation->get_item_count() > 0) {
  674. animation->select(0);
  675. autoplay->set_pressed(animation->get_item_text(0) == player->get_autoplay());
  676. _animation_selected(0);
  677. } else {
  678. _animation_selected(0);
  679. }
  680. if (animation->get_item_count()) {
  681. String current = animation->get_item_text(animation->get_selected());
  682. Ref<Animation> anim = player->get_animation(current);
  683. track_editor->set_animation(anim);
  684. Node *root = player->get_node(player->get_root());
  685. if (root) {
  686. track_editor->set_root(root);
  687. }
  688. }
  689. _update_animation();
  690. }
  691. void AnimationPlayerEditor::edit(AnimationPlayer *p_player) {
  692. if (player && pin->is_pressed())
  693. return; // Ignore, pinned.
  694. player = p_player;
  695. if (player) {
  696. _update_player();
  697. if (onion.enabled) {
  698. if (animation->get_item_count() > 0)
  699. _start_onion_skinning();
  700. else
  701. _stop_onion_skinning();
  702. }
  703. track_editor->show_select_node_warning(false);
  704. } else {
  705. if (onion.enabled)
  706. _stop_onion_skinning();
  707. track_editor->show_select_node_warning(true);
  708. }
  709. }
  710. void AnimationPlayerEditor::forward_canvas_force_draw_over_viewport(Control *p_overlay) {
  711. if (!onion.can_overlay)
  712. return;
  713. // Can happen on viewport resize, at least.
  714. if (!_are_onion_layers_valid())
  715. return;
  716. RID ci = p_overlay->get_canvas_item();
  717. Rect2 src_rect = p_overlay->get_global_rect();
  718. // Re-flip since captures are already flipped.
  719. src_rect.position.y = onion.capture_size.y - (src_rect.position.y + src_rect.size.y);
  720. src_rect.size.y *= -1;
  721. Rect2 dst_rect = Rect2(Point2(), p_overlay->get_size());
  722. float alpha_step = 1.0 / (onion.steps + 1);
  723. int cidx = 0;
  724. if (onion.past) {
  725. float alpha = 0;
  726. do {
  727. alpha += alpha_step;
  728. if (onion.captures_valid[cidx]) {
  729. RS::get_singleton()->canvas_item_add_texture_rect_region(
  730. ci, dst_rect, RS::get_singleton()->viewport_get_texture(onion.captures[cidx]), src_rect, Color(1, 1, 1, alpha));
  731. }
  732. cidx++;
  733. } while (cidx < onion.steps);
  734. }
  735. if (onion.future) {
  736. float alpha = 1;
  737. int base_cidx = cidx;
  738. do {
  739. alpha -= alpha_step;
  740. if (onion.captures_valid[cidx]) {
  741. RS::get_singleton()->canvas_item_add_texture_rect_region(
  742. ci, dst_rect, RS::get_singleton()->viewport_get_texture(onion.captures[cidx]), src_rect, Color(1, 1, 1, alpha));
  743. }
  744. cidx++;
  745. } while (cidx < base_cidx + onion.steps); // In case there's the present capture at the end, skip it.
  746. }
  747. }
  748. void AnimationPlayerEditor::_animation_duplicate() {
  749. if (!animation->get_item_count())
  750. return;
  751. String current = animation->get_item_text(animation->get_selected());
  752. Ref<Animation> anim = player->get_animation(current);
  753. if (!anim.is_valid())
  754. return;
  755. Ref<Animation> new_anim = memnew(Animation);
  756. List<PropertyInfo> plist;
  757. anim->get_property_list(&plist);
  758. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  759. if (E->get().usage & PROPERTY_USAGE_STORAGE) {
  760. new_anim->set(E->get().name, anim->get(E->get().name));
  761. }
  762. }
  763. new_anim->set_path("");
  764. String new_name = current;
  765. while (player->has_animation(new_name)) {
  766. new_name = new_name + " (copy)";
  767. }
  768. new_anim->set_name(new_name);
  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())
  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. updating = true;
  836. frame->set_value(Math::stepify(p_pos, _get_editor_step()));
  837. updating = false;
  838. _seek_value_changed(p_pos, !p_drag);
  839. EditorNode::get_singleton()->get_inspector()->refresh();
  840. }
  841. void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
  842. String current;
  843. if (animation->get_selected() >= 0 && animation->get_selected() < animation->get_item_count()) {
  844. current = animation->get_item_text(animation->get_selected());
  845. }
  846. Ref<Animation> anim;
  847. if (current != String()) {
  848. anim = player->get_animation(current);
  849. }
  850. switch (p_option) {
  851. case TOOL_NEW_ANIM: {
  852. _animation_new();
  853. } break;
  854. case TOOL_LOAD_ANIM: {
  855. _animation_load();
  856. } break;
  857. case TOOL_SAVE_ANIM: {
  858. if (anim.is_valid()) {
  859. _animation_save(anim);
  860. }
  861. } break;
  862. case TOOL_SAVE_AS_ANIM: {
  863. if (anim.is_valid()) {
  864. _animation_save_as(anim);
  865. }
  866. } break;
  867. case TOOL_DUPLICATE_ANIM: {
  868. _animation_duplicate();
  869. } break;
  870. case TOOL_RENAME_ANIM: {
  871. _animation_rename();
  872. } break;
  873. case TOOL_EDIT_TRANSITIONS: {
  874. _animation_blend();
  875. } break;
  876. case TOOL_REMOVE_ANIM: {
  877. _animation_remove();
  878. } break;
  879. case TOOL_COPY_ANIM: {
  880. if (!animation->get_item_count()) {
  881. error_dialog->set_text(TTR("No animation to copy!"));
  882. error_dialog->popup_centered();
  883. return;
  884. }
  885. String current2 = animation->get_item_text(animation->get_selected());
  886. Ref<Animation> anim2 = player->get_animation(current2);
  887. EditorSettings::get_singleton()->set_resource_clipboard(anim2);
  888. } break;
  889. case TOOL_PASTE_ANIM: {
  890. Ref<Animation> anim2 = EditorSettings::get_singleton()->get_resource_clipboard();
  891. if (!anim2.is_valid()) {
  892. error_dialog->set_text(TTR("No animation resource on clipboard!"));
  893. error_dialog->popup_centered();
  894. return;
  895. }
  896. String name = anim2->get_name();
  897. if (name == "") {
  898. name = TTR("Pasted Animation");
  899. }
  900. int idx = 1;
  901. String base = name;
  902. while (player->has_animation(name)) {
  903. idx++;
  904. name = base + " " + itos(idx);
  905. }
  906. undo_redo->create_action(TTR("Paste Animation"));
  907. undo_redo->add_do_method(player, "add_animation", name, anim2);
  908. undo_redo->add_undo_method(player, "remove_animation", name);
  909. undo_redo->add_do_method(this, "_animation_player_changed", player);
  910. undo_redo->add_undo_method(this, "_animation_player_changed", player);
  911. undo_redo->commit_action();
  912. _select_anim_by_name(name);
  913. } break;
  914. case TOOL_EDIT_RESOURCE: {
  915. if (!animation->get_item_count()) {
  916. error_dialog->set_text(TTR("No animation to edit!"));
  917. error_dialog->popup_centered();
  918. return;
  919. }
  920. String current2 = animation->get_item_text(animation->get_selected());
  921. Ref<Animation> anim2 = player->get_animation(current2);
  922. editor->edit_resource(anim2);
  923. } break;
  924. }
  925. }
  926. void AnimationPlayerEditor::_onion_skinning_menu(int p_option) {
  927. PopupMenu *menu = onion_skinning->get_popup();
  928. int idx = menu->get_item_index(p_option);
  929. switch (p_option) {
  930. case ONION_SKINNING_ENABLE: {
  931. onion.enabled = !onion.enabled;
  932. if (onion.enabled)
  933. _start_onion_skinning();
  934. else
  935. _stop_onion_skinning();
  936. } break;
  937. case ONION_SKINNING_PAST: {
  938. // Ensure at least one of past/future is checked.
  939. onion.past = onion.future ? !onion.past : true;
  940. menu->set_item_checked(idx, onion.past);
  941. } break;
  942. case ONION_SKINNING_FUTURE: {
  943. // Ensure at least one of past/future is checked.
  944. onion.future = onion.past ? !onion.future : true;
  945. menu->set_item_checked(idx, onion.future);
  946. } break;
  947. case ONION_SKINNING_1_STEP: // Fall-through.
  948. case ONION_SKINNING_2_STEPS:
  949. case ONION_SKINNING_3_STEPS: {
  950. onion.steps = (p_option - ONION_SKINNING_1_STEP) + 1;
  951. int one_frame_idx = menu->get_item_index(ONION_SKINNING_1_STEP);
  952. for (int i = 0; i <= ONION_SKINNING_LAST_STEPS_OPTION - ONION_SKINNING_1_STEP; i++) {
  953. menu->set_item_checked(one_frame_idx + i, onion.steps == i + 1);
  954. }
  955. } break;
  956. case ONION_SKINNING_DIFFERENCES_ONLY: {
  957. onion.differences_only = !onion.differences_only;
  958. menu->set_item_checked(idx, onion.differences_only);
  959. } break;
  960. case ONION_SKINNING_FORCE_WHITE_MODULATE: {
  961. onion.force_white_modulate = !onion.force_white_modulate;
  962. menu->set_item_checked(idx, onion.force_white_modulate);
  963. } break;
  964. case ONION_SKINNING_INCLUDE_GIZMOS: {
  965. onion.include_gizmos = !onion.include_gizmos;
  966. menu->set_item_checked(idx, onion.include_gizmos);
  967. } break;
  968. }
  969. }
  970. void AnimationPlayerEditor::_unhandled_key_input(const Ref<InputEvent> &p_ev) {
  971. Ref<InputEventKey> k = p_ev;
  972. if (is_visible_in_tree() && k.is_valid() && k->is_pressed() && !k->is_echo() && !k->get_alt() && !k->get_control() && !k->get_metakey()) {
  973. switch (k->get_keycode()) {
  974. case KEY_A: {
  975. if (!k->get_shift())
  976. _play_bw_from_pressed();
  977. else
  978. _play_bw_pressed();
  979. } break;
  980. case KEY_S: {
  981. _stop_pressed();
  982. } break;
  983. case KEY_D: {
  984. if (!k->get_shift())
  985. _play_from_pressed();
  986. else
  987. _play_pressed();
  988. } break;
  989. }
  990. }
  991. }
  992. void AnimationPlayerEditor::_editor_visibility_changed() {
  993. if (is_visible() && animation->get_item_count() > 0) {
  994. _start_onion_skinning();
  995. }
  996. }
  997. bool AnimationPlayerEditor::_are_onion_layers_valid() {
  998. ERR_FAIL_COND_V(!onion.past && !onion.future, false);
  999. Point2 capture_size = get_tree()->get_root()->get_size();
  1000. return onion.captures.size() == onion.get_needed_capture_count() && onion.capture_size == capture_size;
  1001. }
  1002. void AnimationPlayerEditor::_allocate_onion_layers() {
  1003. _free_onion_layers();
  1004. int captures = onion.get_needed_capture_count();
  1005. Point2 capture_size = get_tree()->get_root()->get_size();
  1006. onion.captures.resize(captures);
  1007. onion.captures_valid.resize(captures);
  1008. for (int i = 0; i < captures; i++) {
  1009. bool is_present = onion.differences_only && i == captures - 1;
  1010. // Each capture is a viewport with a canvas item attached that renders a full-size rect with the contents of the main viewport.
  1011. onion.captures.write[i] = RS::get_singleton()->viewport_create();
  1012. RS::get_singleton()->viewport_set_size(onion.captures[i], capture_size.width, capture_size.height);
  1013. RS::get_singleton()->viewport_set_update_mode(onion.captures[i], RS::VIEWPORT_UPDATE_ALWAYS);
  1014. RS::get_singleton()->viewport_set_transparent_background(onion.captures[i], !is_present);
  1015. RS::get_singleton()->viewport_attach_canvas(onion.captures[i], onion.capture.canvas);
  1016. }
  1017. // Reset the capture canvas item to the current root viewport texture (defensive).
  1018. RS::get_singleton()->canvas_item_clear(onion.capture.canvas_item);
  1019. RS::get_singleton()->canvas_item_add_texture_rect(onion.capture.canvas_item, Rect2(Point2(), capture_size), get_tree()->get_root()->get_texture()->get_rid());
  1020. onion.capture_size = capture_size;
  1021. }
  1022. void AnimationPlayerEditor::_free_onion_layers() {
  1023. for (int i = 0; i < onion.captures.size(); i++) {
  1024. if (onion.captures[i].is_valid()) {
  1025. RS::get_singleton()->free(onion.captures[i]);
  1026. }
  1027. }
  1028. onion.captures.clear();
  1029. onion.captures_valid.clear();
  1030. }
  1031. void AnimationPlayerEditor::_prepare_onion_layers_1() {
  1032. // This would be called per viewport and we want to act once only.
  1033. int64_t frame = get_tree()->get_frame();
  1034. if (frame == onion.last_frame)
  1035. return;
  1036. if (!onion.enabled || !is_processing() || !is_visible() || !get_player()) {
  1037. _stop_onion_skinning();
  1038. return;
  1039. }
  1040. onion.last_frame = frame;
  1041. // Refresh viewports with no onion layers overlaid.
  1042. onion.can_overlay = false;
  1043. plugin->update_overlays();
  1044. if (player->is_playing())
  1045. return;
  1046. // And go to next step afterwards.
  1047. call_deferred("_prepare_onion_layers_2");
  1048. }
  1049. void AnimationPlayerEditor::_prepare_onion_layers_1_deferred() {
  1050. call_deferred("_prepare_onion_layers_1");
  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 (Node3DEditor::get_singleton()->is_visible()) {
  1062. // 3D
  1063. spatial_edit_state = Node3DEditor::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. Node3DEditor::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 = Rect2(Vector2(), get_tree()->get_root()->get_size());
  1096. RS::get_singleton()->viewport_attach_to_screen(root_vp, Rect2());
  1097. RS::get_singleton()->viewport_set_update_mode(root_vp, RS::VIEWPORT_UPDATE_ALWAYS);
  1098. RID present_rid;
  1099. if (onion.differences_only) {
  1100. // Capture present scene as it is.
  1101. RS::get_singleton()->canvas_item_set_material(onion.capture.canvas_item, RID());
  1102. present_rid = onion.captures[onion.captures.size() - 1];
  1103. RS::get_singleton()->viewport_set_active(present_rid, true);
  1104. RS::get_singleton()->viewport_set_parent_viewport(root_vp, present_rid);
  1105. RS::get_singleton()->draw(false);
  1106. RS::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. RS::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 ? RS::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 Node3Ds.
  1133. values_backup.update_skeletons(); // Needed for Skeletons (2D & 3D).
  1134. RS::get_singleton()->viewport_set_active(onion.captures[cidx], true);
  1135. RS::get_singleton()->viewport_set_parent_viewport(root_vp, onion.captures[cidx]);
  1136. RS::get_singleton()->draw(false);
  1137. RS::get_singleton()->viewport_set_active(onion.captures[cidx], false);
  1138. }
  1139. cidx++;
  1140. }
  1141. // Restore root viewport.
  1142. RS::get_singleton()->viewport_set_parent_viewport(root_vp, RID());
  1143. RS::get_singleton()->viewport_attach_to_screen(root_vp, root_vp_screen_rect);
  1144. RS::get_singleton()->viewport_set_update_mode(root_vp, RS::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 (Node3DEditor::get_singleton()->is_visible()) {
  1152. // 3D
  1153. Node3DEditor::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", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) {
  1165. get_tree()->connect("idle_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred));
  1166. }
  1167. }
  1168. void AnimationPlayerEditor::_stop_onion_skinning() {
  1169. if (get_tree()->is_connected("idle_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_deferred))) {
  1170. get_tree()->disconnect("idle_frame", callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1_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("_animation_new"), &AnimationPlayerEditor::_animation_new);
  1182. ClassDB::bind_method(D_METHOD("_animation_rename"), &AnimationPlayerEditor::_animation_rename);
  1183. ClassDB::bind_method(D_METHOD("_animation_load"), &AnimationPlayerEditor::_animation_load);
  1184. ClassDB::bind_method(D_METHOD("_animation_remove"), &AnimationPlayerEditor::_animation_remove);
  1185. ClassDB::bind_method(D_METHOD("_animation_blend"), &AnimationPlayerEditor::_animation_blend);
  1186. ClassDB::bind_method(D_METHOD("_animation_edit"), &AnimationPlayerEditor::_animation_edit);
  1187. ClassDB::bind_method(D_METHOD("_animation_resource_edit"), &AnimationPlayerEditor::_animation_resource_edit);
  1188. ClassDB::bind_method(D_METHOD("_animation_player_changed"), &AnimationPlayerEditor::_animation_player_changed);
  1189. ClassDB::bind_method(D_METHOD("_list_changed"), &AnimationPlayerEditor::_list_changed);
  1190. ClassDB::bind_method(D_METHOD("_animation_duplicate"), &AnimationPlayerEditor::_animation_duplicate);
  1191. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &AnimationPlayerEditor::_unhandled_key_input);
  1192. ClassDB::bind_method(D_METHOD("_prepare_onion_layers_1"), &AnimationPlayerEditor::_prepare_onion_layers_1);
  1193. ClassDB::bind_method(D_METHOD("_prepare_onion_layers_2"), &AnimationPlayerEditor::_prepare_onion_layers_2);
  1194. ClassDB::bind_method(D_METHOD("_start_onion_skinning"), &AnimationPlayerEditor::_start_onion_skinning);
  1195. ClassDB::bind_method(D_METHOD("_stop_onion_skinning"), &AnimationPlayerEditor::_stop_onion_skinning);
  1196. }
  1197. AnimationPlayerEditor *AnimationPlayerEditor::singleton = NULL;
  1198. AnimationPlayer *AnimationPlayerEditor::get_player() const {
  1199. return player;
  1200. }
  1201. AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor, AnimationPlayerEditorPlugin *p_plugin) {
  1202. editor = p_editor;
  1203. plugin = p_plugin;
  1204. singleton = this;
  1205. updating = false;
  1206. set_focus_mode(FOCUS_ALL);
  1207. player = NULL;
  1208. HBoxContainer *hb = memnew(HBoxContainer);
  1209. add_child(hb);
  1210. play_bw_from = memnew(ToolButton);
  1211. play_bw_from->set_tooltip(TTR("Play selected animation backwards from current pos. (A)"));
  1212. hb->add_child(play_bw_from);
  1213. play_bw = memnew(ToolButton);
  1214. play_bw->set_tooltip(TTR("Play selected animation backwards from end. (Shift+A)"));
  1215. hb->add_child(play_bw);
  1216. stop = memnew(ToolButton);
  1217. stop->set_toggle_mode(true);
  1218. hb->add_child(stop);
  1219. stop->set_tooltip(TTR("Stop animation playback. (S)"));
  1220. play = memnew(ToolButton);
  1221. play->set_tooltip(TTR("Play selected animation from start. (Shift+D)"));
  1222. hb->add_child(play);
  1223. play_from = memnew(ToolButton);
  1224. play_from->set_tooltip(TTR("Play selected animation from current pos. (D)"));
  1225. hb->add_child(play_from);
  1226. frame = memnew(SpinBox);
  1227. hb->add_child(frame);
  1228. frame->set_custom_minimum_size(Size2(60, 0));
  1229. frame->set_stretch_ratio(2);
  1230. frame->set_step(0.0001);
  1231. frame->set_tooltip(TTR("Animation position (in seconds)."));
  1232. hb->add_child(memnew(VSeparator));
  1233. scale = memnew(LineEdit);
  1234. hb->add_child(scale);
  1235. scale->set_h_size_flags(SIZE_EXPAND_FILL);
  1236. scale->set_stretch_ratio(1);
  1237. scale->set_tooltip(TTR("Scale animation playback globally for the node."));
  1238. scale->hide();
  1239. delete_dialog = memnew(ConfirmationDialog);
  1240. add_child(delete_dialog);
  1241. delete_dialog->connect("confirmed", callable_mp(this, &AnimationPlayerEditor::_animation_remove_confirmed));
  1242. tool_anim = memnew(MenuButton);
  1243. tool_anim->set_flat(false);
  1244. tool_anim->set_tooltip(TTR("Animation Tools"));
  1245. tool_anim->set_text(TTR("Animation"));
  1246. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/new_animation", TTR("New")), TOOL_NEW_ANIM);
  1247. tool_anim->get_popup()->add_separator();
  1248. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/open_animation", TTR("Load")), TOOL_LOAD_ANIM);
  1249. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/save_animation", TTR("Save")), TOOL_SAVE_ANIM);
  1250. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/save_as_animation", TTR("Save As...")), TOOL_SAVE_AS_ANIM);
  1251. tool_anim->get_popup()->add_separator();
  1252. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/copy_animation", TTR("Copy")), TOOL_COPY_ANIM);
  1253. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/paste_animation", TTR("Paste")), TOOL_PASTE_ANIM);
  1254. tool_anim->get_popup()->add_separator();
  1255. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/duplicate_animation", TTR("Duplicate")), TOOL_DUPLICATE_ANIM);
  1256. tool_anim->get_popup()->add_separator();
  1257. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/rename_animation", TTR("Rename...")), TOOL_RENAME_ANIM);
  1258. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/edit_transitions", TTR("Edit Transitions...")), TOOL_EDIT_TRANSITIONS);
  1259. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/open_animation_in_inspector", TTR("Open in Inspector")), TOOL_EDIT_RESOURCE);
  1260. tool_anim->get_popup()->add_separator();
  1261. tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/remove_animation", TTR("Remove")), TOOL_REMOVE_ANIM);
  1262. hb->add_child(tool_anim);
  1263. animation = memnew(OptionButton);
  1264. hb->add_child(animation);
  1265. animation->set_h_size_flags(SIZE_EXPAND_FILL);
  1266. animation->set_tooltip(TTR("Display list of animations in player."));
  1267. animation->set_clip_text(true);
  1268. autoplay = memnew(ToolButton);
  1269. hb->add_child(autoplay);
  1270. autoplay->set_tooltip(TTR("Autoplay on Load"));
  1271. hb->add_child(memnew(VSeparator));
  1272. track_editor = memnew(AnimationTrackEditor);
  1273. hb->add_child(track_editor->get_edit_menu());
  1274. hb->add_child(memnew(VSeparator));
  1275. onion_toggle = memnew(ToolButton);
  1276. onion_toggle->set_toggle_mode(true);
  1277. onion_toggle->set_tooltip(TTR("Enable Onion Skinning"));
  1278. onion_toggle->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_onion_skinning_menu), varray(ONION_SKINNING_ENABLE));
  1279. hb->add_child(onion_toggle);
  1280. onion_skinning = memnew(MenuButton);
  1281. onion_skinning->set_tooltip(TTR("Onion Skinning Options"));
  1282. onion_skinning->get_popup()->add_separator(TTR("Directions"));
  1283. onion_skinning->get_popup()->add_check_item(TTR("Past"), ONION_SKINNING_PAST);
  1284. onion_skinning->get_popup()->set_item_checked(onion_skinning->get_popup()->get_item_count() - 1, true);
  1285. onion_skinning->get_popup()->add_check_item(TTR("Future"), ONION_SKINNING_FUTURE);
  1286. onion_skinning->get_popup()->add_separator(TTR("Depth"));
  1287. onion_skinning->get_popup()->add_radio_check_item(TTR("1 step"), ONION_SKINNING_1_STEP);
  1288. onion_skinning->get_popup()->set_item_checked(onion_skinning->get_popup()->get_item_count() - 1, true);
  1289. onion_skinning->get_popup()->add_radio_check_item(TTR("2 steps"), ONION_SKINNING_2_STEPS);
  1290. onion_skinning->get_popup()->add_radio_check_item(TTR("3 steps"), ONION_SKINNING_3_STEPS);
  1291. onion_skinning->get_popup()->add_separator();
  1292. onion_skinning->get_popup()->add_check_item(TTR("Differences Only"), ONION_SKINNING_DIFFERENCES_ONLY);
  1293. onion_skinning->get_popup()->add_check_item(TTR("Force White Modulate"), ONION_SKINNING_FORCE_WHITE_MODULATE);
  1294. onion_skinning->get_popup()->add_check_item(TTR("Include Gizmos (3D)"), ONION_SKINNING_INCLUDE_GIZMOS);
  1295. hb->add_child(onion_skinning);
  1296. hb->add_child(memnew(VSeparator));
  1297. pin = memnew(ToolButton);
  1298. pin->set_toggle_mode(true);
  1299. pin->set_tooltip(TTR("Pin AnimationPlayer"));
  1300. hb->add_child(pin);
  1301. pin->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_pin_pressed));
  1302. file = memnew(EditorFileDialog);
  1303. add_child(file);
  1304. name_dialog = memnew(ConfirmationDialog);
  1305. name_dialog->set_title(TTR("Create New Animation"));
  1306. name_dialog->set_hide_on_ok(false);
  1307. add_child(name_dialog);
  1308. VBoxContainer *vb = memnew(VBoxContainer);
  1309. name_dialog->add_child(vb);
  1310. name_title = memnew(Label(TTR("Animation Name:")));
  1311. vb->add_child(name_title);
  1312. name = memnew(LineEdit);
  1313. vb->add_child(name);
  1314. name_dialog->register_text_enter(name);
  1315. error_dialog = memnew(ConfirmationDialog);
  1316. error_dialog->get_ok()->set_text(TTR("Close"));
  1317. error_dialog->set_title(TTR("Error!"));
  1318. add_child(error_dialog);
  1319. name_dialog->connect("confirmed", callable_mp(this, &AnimationPlayerEditor::_animation_name_edited));
  1320. blend_editor.dialog = memnew(AcceptDialog);
  1321. add_child(blend_editor.dialog);
  1322. blend_editor.dialog->get_ok()->set_text(TTR("Close"));
  1323. blend_editor.dialog->set_hide_on_ok(true);
  1324. VBoxContainer *blend_vb = memnew(VBoxContainer);
  1325. blend_editor.dialog->add_child(blend_vb);
  1326. blend_editor.tree = memnew(Tree);
  1327. blend_editor.tree->set_columns(2);
  1328. blend_vb->add_margin_child(TTR("Blend Times:"), blend_editor.tree, true);
  1329. blend_editor.next = memnew(OptionButton);
  1330. blend_vb->add_margin_child(TTR("Next (Auto Queue):"), blend_editor.next);
  1331. blend_editor.dialog->set_title(TTR("Cross-Animation Blend Times"));
  1332. updating_blends = false;
  1333. blend_editor.tree->connect("item_edited", callable_mp(this, &AnimationPlayerEditor::_blend_edited));
  1334. autoplay->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_autoplay_pressed));
  1335. autoplay->set_toggle_mode(true);
  1336. play->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_pressed));
  1337. play_from->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_from_pressed));
  1338. play_bw->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_bw_pressed));
  1339. play_bw_from->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_play_bw_from_pressed));
  1340. stop->connect("pressed", callable_mp(this, &AnimationPlayerEditor::_stop_pressed));
  1341. animation->connect("item_selected", callable_mp(this, &AnimationPlayerEditor::_animation_selected));
  1342. file->connect("file_selected", callable_mp(this, &AnimationPlayerEditor::_dialog_action));
  1343. frame->connect("value_changed", callable_mp(this, &AnimationPlayerEditor::_seek_value_changed), make_binds(true));
  1344. scale->connect("text_entered", callable_mp(this, &AnimationPlayerEditor::_scale_changed));
  1345. renaming = false;
  1346. last_active = false;
  1347. timeline_position = 0;
  1348. set_process_unhandled_key_input(true);
  1349. add_child(track_editor);
  1350. track_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1351. track_editor->connect("timeline_changed", callable_mp(this, &AnimationPlayerEditor::_animation_key_editor_seek));
  1352. track_editor->connect("animation_len_changed", callable_mp(this, &AnimationPlayerEditor::_animation_key_editor_anim_len_changed));
  1353. _update_player();
  1354. // Onion skinning.
  1355. track_editor->connect("visibility_changed", callable_mp(this, &AnimationPlayerEditor::_editor_visibility_changed));
  1356. onion.enabled = false;
  1357. onion.past = true;
  1358. onion.future = false;
  1359. onion.steps = 1;
  1360. onion.differences_only = false;
  1361. onion.force_white_modulate = false;
  1362. onion.include_gizmos = false;
  1363. onion.last_frame = 0;
  1364. onion.can_overlay = false;
  1365. onion.capture_size = Size2();
  1366. onion.capture.canvas = RS::get_singleton()->canvas_create();
  1367. onion.capture.canvas_item = RS::get_singleton()->canvas_item_create();
  1368. RS::get_singleton()->canvas_item_set_parent(onion.capture.canvas_item, onion.capture.canvas);
  1369. onion.capture.material = Ref<ShaderMaterial>(memnew(ShaderMaterial));
  1370. onion.capture.shader = Ref<Shader>(memnew(Shader));
  1371. onion.capture.shader->set_code(" \
  1372. shader_type canvas_item; \
  1373. \
  1374. uniform vec4 bkg_color; \
  1375. uniform vec4 dir_color; \
  1376. uniform bool differences_only; \
  1377. uniform sampler2D present; \
  1378. \
  1379. float zero_if_equal(vec4 a, vec4 b) { \
  1380. return smoothstep(0.0, 0.005, length(a.rgb - b.rgb) / sqrt(3.0)); \
  1381. } \
  1382. \
  1383. void fragment() { \
  1384. vec4 capture_samp = texture(TEXTURE, UV); \
  1385. vec4 present_samp = texture(present, UV); \
  1386. float bkg_mask = zero_if_equal(capture_samp, bkg_color); \
  1387. float diff_mask = 1.0 - zero_if_equal(present_samp, bkg_color); \
  1388. diff_mask = min(1.0, diff_mask + float(!differences_only)); \
  1389. COLOR = vec4(capture_samp.rgb * dir_color.rgb, bkg_mask * diff_mask); \
  1390. } \
  1391. ");
  1392. RS::get_singleton()->material_set_shader(onion.capture.material->get_rid(), onion.capture.shader->get_rid());
  1393. }
  1394. AnimationPlayerEditor::~AnimationPlayerEditor() {
  1395. _free_onion_layers();
  1396. RS::get_singleton()->free(onion.capture.canvas);
  1397. RS::get_singleton()->free(onion.capture.canvas_item);
  1398. }
  1399. void AnimationPlayerEditorPlugin::_notification(int p_what) {
  1400. switch (p_what) {
  1401. case NOTIFICATION_ENTER_TREE: {
  1402. set_force_draw_over_forwarding_enabled();
  1403. } break;
  1404. }
  1405. }
  1406. void AnimationPlayerEditorPlugin::edit(Object *p_object) {
  1407. anim_editor->set_undo_redo(&get_undo_redo());
  1408. if (!p_object)
  1409. return;
  1410. anim_editor->edit(Object::cast_to<AnimationPlayer>(p_object));
  1411. }
  1412. bool AnimationPlayerEditorPlugin::handles(Object *p_object) const {
  1413. return p_object->is_class("AnimationPlayer");
  1414. }
  1415. void AnimationPlayerEditorPlugin::make_visible(bool p_visible) {
  1416. if (p_visible) {
  1417. editor->make_bottom_panel_item_visible(anim_editor);
  1418. anim_editor->set_process(true);
  1419. anim_editor->ensure_visibility();
  1420. }
  1421. }
  1422. AnimationPlayerEditorPlugin::AnimationPlayerEditorPlugin(EditorNode *p_node) {
  1423. editor = p_node;
  1424. anim_editor = memnew(AnimationPlayerEditor(editor, this));
  1425. anim_editor->set_undo_redo(EditorNode::get_undo_redo());
  1426. editor->add_bottom_panel_item(TTR("Animation"), anim_editor);
  1427. }
  1428. AnimationPlayerEditorPlugin::~AnimationPlayerEditorPlugin() {
  1429. }