animation_library_editor.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*************************************************************************/
  2. /* animation_library_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_library_editor.h"
  31. #include "editor/editor_file_dialog.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. void AnimationLibraryEditor::set_animation_player(Object *p_player) {
  35. player = p_player;
  36. }
  37. void AnimationLibraryEditor::_add_library() {
  38. add_library_dialog->set_title(TTR("Library Name:"));
  39. add_library_name->set_text("");
  40. add_library_dialog->popup_centered();
  41. add_library_name->grab_focus();
  42. adding_animation = false;
  43. adding_animation_to_library = StringName();
  44. _add_library_validate("");
  45. }
  46. void AnimationLibraryEditor::_add_library_validate(const String &p_name) {
  47. String error;
  48. if (adding_animation) {
  49. Ref<AnimationLibrary> al = player->call("get_animation_library", adding_animation_to_library);
  50. ERR_FAIL_COND(al.is_null());
  51. if (p_name == "") {
  52. error = TTR("Animation name can't be empty.");
  53. } else if (!AnimationLibrary::is_valid_animation_name(p_name)) {
  54. error = TTR("Animation name contains invalid characters: '/', ':', ',' or '['.");
  55. } else if (al->has_animation(p_name)) {
  56. error = TTR("Animation with the same name already exists.");
  57. }
  58. } else {
  59. if (p_name == "" && bool(player->call("has_animation_library", ""))) {
  60. error = TTR("Enter a library name.");
  61. } else if (!AnimationLibrary::is_valid_library_name(p_name)) {
  62. error = TTR("Library name contains invalid characters: '/', ':', ',' or '['.");
  63. } else if (bool(player->call("has_animation_library", p_name))) {
  64. error = TTR("Library with the same name already exists.");
  65. }
  66. }
  67. if (error != "") {
  68. add_library_validate->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  69. add_library_validate->set_text(error);
  70. add_library_dialog->get_ok_button()->set_disabled(true);
  71. } else {
  72. if (adding_animation) {
  73. add_library_validate->set_text(TTR("Animation name is valid."));
  74. } else {
  75. if (p_name == "") {
  76. add_library_validate->set_text(TTR("Global library will be created."));
  77. } else {
  78. add_library_validate->set_text(TTR("Library name is valid."));
  79. }
  80. }
  81. add_library_validate->add_theme_color_override("font_color", get_theme_color(SNAME("success_color"), SNAME("Editor")));
  82. add_library_dialog->get_ok_button()->set_disabled(false);
  83. }
  84. }
  85. void AnimationLibraryEditor::_add_library_confirm() {
  86. if (adding_animation) {
  87. String anim_name = add_library_name->get_text();
  88. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  89. Ref<AnimationLibrary> al = player->call("get_animation_library", adding_animation_to_library);
  90. ERR_FAIL_COND(!al.is_valid());
  91. Ref<Animation> anim;
  92. anim.instantiate();
  93. undo_redo->create_action(vformat(TTR("Add Animation to Library: %s"), anim_name));
  94. undo_redo->add_do_method(al.ptr(), "add_animation", anim_name, anim);
  95. undo_redo->add_undo_method(al.ptr(), "remove_animation", anim_name);
  96. undo_redo->add_do_method(this, "_update_editor", player);
  97. undo_redo->add_undo_method(this, "_update_editor", player);
  98. undo_redo->commit_action();
  99. } else {
  100. String lib_name = add_library_name->get_text();
  101. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  102. Ref<AnimationLibrary> al;
  103. al.instantiate();
  104. undo_redo->create_action(vformat(TTR("Add Animation Library: %s"), lib_name));
  105. undo_redo->add_do_method(player, "add_animation_library", lib_name, al);
  106. undo_redo->add_undo_method(player, "remove_animation_library", lib_name);
  107. undo_redo->add_do_method(this, "_update_editor", player);
  108. undo_redo->add_undo_method(this, "_update_editor", player);
  109. undo_redo->commit_action();
  110. }
  111. }
  112. void AnimationLibraryEditor::_load_library() {
  113. List<String> extensions;
  114. ResourceLoader::get_recognized_extensions_for_type("AnimationLibrary", &extensions);
  115. file_dialog->set_title(TTR("Load Animation"));
  116. file_dialog->clear_filters();
  117. for (const String &K : extensions) {
  118. file_dialog->add_filter("*." + K);
  119. }
  120. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  121. file_dialog->set_current_file("");
  122. file_dialog->popup_centered_ratio();
  123. file_dialog_action = FILE_DIALOG_ACTION_OPEN_LIBRARY;
  124. }
  125. void AnimationLibraryEditor::_file_popup_selected(int p_id) {
  126. Ref<AnimationLibrary> al = player->call("get_animation_library", file_dialog_library);
  127. Ref<Animation> anim;
  128. if (file_dialog_animation != StringName()) {
  129. anim = al->get_animation(file_dialog_animation);
  130. ERR_FAIL_COND(anim.is_null());
  131. }
  132. switch (p_id) {
  133. case FILE_MENU_SAVE_LIBRARY: {
  134. if (al->get_path().is_resource_file()) {
  135. EditorNode::get_singleton()->save_resource(al);
  136. break;
  137. }
  138. [[fallthrough]];
  139. }
  140. case FILE_MENU_SAVE_AS_LIBRARY: {
  141. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  142. file_dialog->set_title(TTR("Save Library"));
  143. if (al->get_path().is_resource_file()) {
  144. file_dialog->set_current_path(al->get_path());
  145. } else {
  146. file_dialog->set_current_file(String(file_dialog_library) + ".res");
  147. }
  148. file_dialog->clear_filters();
  149. List<String> exts;
  150. ResourceLoader::get_recognized_extensions_for_type("AnimationLibrary", &exts);
  151. for (const String &K : exts) {
  152. file_dialog->add_filter("*." + K);
  153. }
  154. file_dialog->popup_centered_ratio();
  155. file_dialog_action = FILE_DIALOG_ACTION_SAVE_LIBRARY;
  156. } break;
  157. case FILE_MENU_MAKE_LIBRARY_UNIQUE: {
  158. StringName lib_name = file_dialog_library;
  159. Ref<AnimationLibrary> ald = al->duplicate();
  160. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  161. undo_redo->create_action(vformat(TTR("Make Animation Library Unique: %s"), lib_name));
  162. undo_redo->add_do_method(player, "remove_animation_library", lib_name);
  163. undo_redo->add_do_method(player, "add_animation_library", lib_name, ald);
  164. undo_redo->add_undo_method(player, "remove_animation_library", lib_name);
  165. undo_redo->add_undo_method(player, "add_animation_library", lib_name, al);
  166. undo_redo->add_do_method(this, "_update_editor", player);
  167. undo_redo->add_undo_method(this, "_update_editor", player);
  168. undo_redo->commit_action();
  169. } break;
  170. case FILE_MENU_EDIT_LIBRARY: {
  171. EditorNode::get_singleton()->push_item(al.ptr());
  172. } break;
  173. case FILE_MENU_SAVE_ANIMATION: {
  174. if (anim->get_path().is_resource_file()) {
  175. EditorNode::get_singleton()->save_resource(anim);
  176. break;
  177. }
  178. [[fallthrough]];
  179. }
  180. case FILE_MENU_SAVE_AS_ANIMATION: {
  181. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  182. file_dialog->set_title(TTR("Save Animation"));
  183. if (anim->get_path().is_resource_file()) {
  184. file_dialog->set_current_path(anim->get_path());
  185. } else {
  186. file_dialog->set_current_file(String(file_dialog_animation) + ".res");
  187. }
  188. file_dialog->clear_filters();
  189. List<String> exts;
  190. ResourceLoader::get_recognized_extensions_for_type("Animation", &exts);
  191. for (const String &K : exts) {
  192. file_dialog->add_filter("*." + K);
  193. }
  194. file_dialog->popup_centered_ratio();
  195. file_dialog_action = FILE_DIALOG_ACTION_SAVE_ANIMATION;
  196. } break;
  197. case FILE_MENU_MAKE_ANIMATION_UNIQUE: {
  198. StringName anim_name = file_dialog_animation;
  199. Ref<Animation> animd = anim->duplicate();
  200. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  201. undo_redo->create_action(vformat(TTR("Make Animation Unique: %s"), anim_name));
  202. undo_redo->add_do_method(al.ptr(), "remove_animation", anim_name);
  203. undo_redo->add_do_method(al.ptr(), "add_animation", anim_name, animd);
  204. undo_redo->add_undo_method(al.ptr(), "remove_animation", anim_name);
  205. undo_redo->add_undo_method(al.ptr(), "add_animation", anim_name, anim);
  206. undo_redo->add_do_method(this, "_update_editor", player);
  207. undo_redo->add_undo_method(this, "_update_editor", player);
  208. undo_redo->commit_action();
  209. } break;
  210. case FILE_MENU_EDIT_ANIMATION: {
  211. EditorNode::get_singleton()->push_item(anim.ptr());
  212. } break;
  213. }
  214. }
  215. void AnimationLibraryEditor::_load_file(String p_path) {
  216. switch (file_dialog_action) {
  217. case FILE_DIALOG_ACTION_OPEN_LIBRARY: {
  218. Ref<AnimationLibrary> al = ResourceLoader::load(p_path);
  219. if (al.is_null()) {
  220. error_dialog->set_text(TTR("Invalid AnimationLibrary file."));
  221. error_dialog->popup_centered();
  222. return;
  223. }
  224. TypedArray<StringName> libs = player->call("get_animation_library_list");
  225. for (int i = 0; i < libs.size(); i++) {
  226. const StringName K = libs[i];
  227. Ref<AnimationLibrary> al2 = player->call("get_animation_library", K);
  228. if (al2 == al) {
  229. error_dialog->set_text(TTR("This library is already added to the player."));
  230. error_dialog->popup_centered();
  231. return;
  232. }
  233. }
  234. String name = AnimationLibrary::validate_library_name(p_path.get_file().get_basename());
  235. int attempt = 1;
  236. while (bool(player->call("has_animation_library", name))) {
  237. attempt++;
  238. name = p_path.get_file().get_basename() + " " + itos(attempt);
  239. }
  240. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  241. undo_redo->create_action(vformat(TTR("Add Animation Library: %s"), name));
  242. undo_redo->add_do_method(player, "add_animation_library", name, al);
  243. undo_redo->add_undo_method(player, "remove_animation_library", name);
  244. undo_redo->add_do_method(this, "_update_editor", player);
  245. undo_redo->add_undo_method(this, "_update_editor", player);
  246. undo_redo->commit_action();
  247. } break;
  248. case FILE_DIALOG_ACTION_OPEN_ANIMATION: {
  249. Ref<Animation> anim = ResourceLoader::load(p_path);
  250. if (anim.is_null()) {
  251. error_dialog->set_text(TTR("Invalid Animation file."));
  252. error_dialog->popup_centered();
  253. return;
  254. }
  255. Ref<AnimationLibrary> al = player->call("get_animation_library", adding_animation_to_library);
  256. List<StringName> anims;
  257. al->get_animation_list(&anims);
  258. for (const StringName &K : anims) {
  259. Ref<Animation> a2 = al->get_animation(K);
  260. if (a2 == anim) {
  261. error_dialog->set_text(TTR("This animation is already added to the library."));
  262. error_dialog->popup_centered();
  263. return;
  264. }
  265. }
  266. String name = p_path.get_file().get_basename();
  267. int attempt = 1;
  268. while (al->has_animation(name)) {
  269. attempt++;
  270. name = p_path.get_file().get_basename() + " " + itos(attempt);
  271. }
  272. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  273. undo_redo->create_action(vformat(TTR("Load Animation into Library: %s"), name));
  274. undo_redo->add_do_method(al.ptr(), "add_animation", name, anim);
  275. undo_redo->add_undo_method(al.ptr(), "remove_animation", name);
  276. undo_redo->add_do_method(this, "_update_editor", player);
  277. undo_redo->add_undo_method(this, "_update_editor", player);
  278. undo_redo->commit_action();
  279. } break;
  280. case FILE_DIALOG_ACTION_SAVE_LIBRARY: {
  281. Ref<AnimationLibrary> al = player->call("get_animation_library", file_dialog_library);
  282. String prev_path = al->get_path();
  283. EditorNode::get_singleton()->save_resource_in_path(al, p_path);
  284. if (al->get_path() != prev_path) { // Save successful.
  285. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  286. undo_redo->create_action(vformat(TTR("Save Animation library to File: %s"), file_dialog_library));
  287. undo_redo->add_do_method(al.ptr(), "set_path", al->get_path());
  288. undo_redo->add_undo_method(al.ptr(), "set_path", prev_path);
  289. undo_redo->add_do_method(this, "_update_editor", player);
  290. undo_redo->add_undo_method(this, "_update_editor", player);
  291. undo_redo->commit_action();
  292. }
  293. } break;
  294. case FILE_DIALOG_ACTION_SAVE_ANIMATION: {
  295. Ref<AnimationLibrary> al = player->call("get_animation_library", file_dialog_library);
  296. Ref<Animation> anim;
  297. if (file_dialog_animation != StringName()) {
  298. anim = al->get_animation(file_dialog_animation);
  299. ERR_FAIL_COND(anim.is_null());
  300. }
  301. String prev_path = anim->get_path();
  302. EditorNode::get_singleton()->save_resource_in_path(anim, p_path);
  303. if (anim->get_path() != prev_path) { // Save successful.
  304. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  305. undo_redo->create_action(vformat(TTR("Save Animation to File: %s"), file_dialog_animation));
  306. undo_redo->add_do_method(anim.ptr(), "set_path", anim->get_path());
  307. undo_redo->add_undo_method(anim.ptr(), "set_path", prev_path);
  308. undo_redo->add_do_method(this, "_update_editor", player);
  309. undo_redo->add_undo_method(this, "_update_editor", player);
  310. undo_redo->commit_action();
  311. }
  312. } break;
  313. }
  314. }
  315. void AnimationLibraryEditor::_item_renamed() {
  316. TreeItem *ti = tree->get_edited();
  317. String text = ti->get_text(0);
  318. String old_text = ti->get_metadata(0);
  319. bool restore_text = false;
  320. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  321. if (String(text).contains("/") || String(text).contains(":") || String(text).contains(",") || String(text).contains("[")) {
  322. restore_text = true;
  323. } else {
  324. if (ti->get_parent() == tree->get_root()) {
  325. // Renamed library
  326. if (player->call("has_animation_library", text)) {
  327. restore_text = true;
  328. } else {
  329. undo_redo->create_action(vformat(TTR("Rename Animation Library: %s"), text));
  330. undo_redo->add_do_method(player, "rename_animation_library", old_text, text);
  331. undo_redo->add_undo_method(player, "rename_animation_library", text, old_text);
  332. undo_redo->add_do_method(this, "_update_editor", player);
  333. undo_redo->add_undo_method(this, "_update_editor", player);
  334. updating = true;
  335. undo_redo->commit_action();
  336. updating = false;
  337. ti->set_metadata(0, text);
  338. if (text == "") {
  339. ti->set_suffix(0, TTR("[Global]"));
  340. } else {
  341. ti->set_suffix(0, "");
  342. }
  343. }
  344. } else {
  345. // Renamed anim
  346. StringName library = ti->get_parent()->get_metadata(0);
  347. Ref<AnimationLibrary> al = player->call("get_animation_library", library);
  348. if (al.is_valid()) {
  349. if (al->has_animation(text)) {
  350. restore_text = true;
  351. } else {
  352. undo_redo->create_action(vformat(TTR("Rename Animation: %s"), text));
  353. undo_redo->add_do_method(al.ptr(), "rename_animation", old_text, text);
  354. undo_redo->add_undo_method(al.ptr(), "rename_animation", text, old_text);
  355. undo_redo->add_do_method(this, "_update_editor", player);
  356. undo_redo->add_undo_method(this, "_update_editor", player);
  357. updating = true;
  358. undo_redo->commit_action();
  359. updating = false;
  360. ti->set_metadata(0, text);
  361. }
  362. } else {
  363. restore_text = true;
  364. }
  365. }
  366. }
  367. if (restore_text) {
  368. ti->set_text(0, old_text);
  369. }
  370. }
  371. void AnimationLibraryEditor::_button_pressed(TreeItem *p_item, int p_column, int p_button) {
  372. if (p_item->get_parent() == tree->get_root()) {
  373. // Library
  374. StringName lib_name = p_item->get_metadata(0);
  375. Ref<AnimationLibrary> al = player->call("get_animation_library", lib_name);
  376. switch (p_button) {
  377. case LIB_BUTTON_ADD: {
  378. add_library_dialog->set_title(TTR("Animation Name:"));
  379. add_library_name->set_text("");
  380. add_library_dialog->popup_centered();
  381. add_library_name->grab_focus();
  382. adding_animation = true;
  383. adding_animation_to_library = p_item->get_metadata(0);
  384. _add_library_validate("");
  385. } break;
  386. case LIB_BUTTON_LOAD: {
  387. adding_animation_to_library = p_item->get_metadata(0);
  388. List<String> extensions;
  389. ResourceLoader::get_recognized_extensions_for_type("Animation", &extensions);
  390. file_dialog->clear_filters();
  391. for (const String &K : extensions) {
  392. file_dialog->add_filter("*." + K);
  393. }
  394. file_dialog->set_title(TTR("Load Animation"));
  395. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  396. file_dialog->set_current_file("");
  397. file_dialog->popup_centered_ratio();
  398. file_dialog_action = FILE_DIALOG_ACTION_OPEN_ANIMATION;
  399. } break;
  400. case LIB_BUTTON_PASTE: {
  401. Ref<Animation> anim = EditorSettings::get_singleton()->get_resource_clipboard();
  402. if (!anim.is_valid()) {
  403. error_dialog->set_text(TTR("No animation resource in clipboard!"));
  404. error_dialog->popup_centered();
  405. return;
  406. }
  407. anim = anim->duplicate(); // Users simply dont care about referencing, so making a copy works better here.
  408. String base_name;
  409. if (anim->get_name() != "") {
  410. base_name = anim->get_name();
  411. } else {
  412. base_name = TTR("Pasted Animation");
  413. }
  414. String name = base_name;
  415. int attempt = 1;
  416. while (al->has_animation(name)) {
  417. attempt++;
  418. name = base_name + " (" + itos(attempt) + ")";
  419. }
  420. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  421. undo_redo->create_action(vformat(TTR("Add Animation to Library: %s"), name));
  422. undo_redo->add_do_method(al.ptr(), "add_animation", name, anim);
  423. undo_redo->add_undo_method(al.ptr(), "remove_animation", name);
  424. undo_redo->add_do_method(this, "_update_editor", player);
  425. undo_redo->add_undo_method(this, "_update_editor", player);
  426. undo_redo->commit_action();
  427. } break;
  428. case LIB_BUTTON_FILE: {
  429. file_popup->clear();
  430. file_popup->add_item(TTR("Save"), FILE_MENU_SAVE_LIBRARY);
  431. file_popup->add_item(TTR("Save As"), FILE_MENU_SAVE_AS_LIBRARY);
  432. file_popup->add_separator();
  433. file_popup->add_item(TTR("Make Unique"), FILE_MENU_MAKE_LIBRARY_UNIQUE);
  434. file_popup->add_separator();
  435. file_popup->add_item(TTR("Open in Inspector"), FILE_MENU_EDIT_LIBRARY);
  436. Rect2 pos = tree->get_item_rect(p_item, 1, 0);
  437. Vector2 popup_pos = tree->get_screen_position() + pos.position + Vector2(0, pos.size.height);
  438. file_popup->popup(Rect2(popup_pos, Size2()));
  439. file_dialog_animation = StringName();
  440. file_dialog_library = lib_name;
  441. } break;
  442. case LIB_BUTTON_DELETE: {
  443. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  444. undo_redo->create_action(vformat(TTR("Remove Animation Library: %s"), lib_name));
  445. undo_redo->add_do_method(player, "remove_animation_library", lib_name);
  446. undo_redo->add_undo_method(player, "add_animation_library", lib_name, al);
  447. undo_redo->add_do_method(this, "_update_editor", player);
  448. undo_redo->add_undo_method(this, "_update_editor", player);
  449. undo_redo->commit_action();
  450. } break;
  451. }
  452. } else {
  453. // Animation
  454. StringName lib_name = p_item->get_parent()->get_metadata(0);
  455. StringName anim_name = p_item->get_metadata(0);
  456. Ref<AnimationLibrary> al = player->call("get_animation_library", lib_name);
  457. Ref<Animation> anim = al->get_animation(anim_name);
  458. ERR_FAIL_COND(!anim.is_valid());
  459. switch (p_button) {
  460. case ANIM_BUTTON_COPY: {
  461. if (anim->get_name() == "") {
  462. anim->set_name(anim_name); // Keep the name around
  463. }
  464. EditorSettings::get_singleton()->set_resource_clipboard(anim);
  465. } break;
  466. case ANIM_BUTTON_FILE: {
  467. file_popup->clear();
  468. file_popup->add_item(TTR("Save"), FILE_MENU_SAVE_ANIMATION);
  469. file_popup->add_item(TTR("Save As"), FILE_MENU_SAVE_AS_ANIMATION);
  470. file_popup->add_separator();
  471. file_popup->add_item(TTR("Make Unique"), FILE_MENU_MAKE_ANIMATION_UNIQUE);
  472. file_popup->add_separator();
  473. file_popup->add_item(TTR("Open in Inspector"), FILE_MENU_EDIT_ANIMATION);
  474. Rect2 pos = tree->get_item_rect(p_item, 1, 0);
  475. Vector2 popup_pos = tree->get_screen_position() + pos.position + Vector2(0, pos.size.height);
  476. file_popup->popup(Rect2(popup_pos, Size2()));
  477. file_dialog_animation = anim_name;
  478. file_dialog_library = lib_name;
  479. } break;
  480. case ANIM_BUTTON_DELETE: {
  481. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  482. undo_redo->create_action(vformat(TTR("Remove Animation from Library: %s"), anim_name));
  483. undo_redo->add_do_method(al.ptr(), "remove_animation", anim_name);
  484. undo_redo->add_undo_method(al.ptr(), "add_animation", anim_name, anim);
  485. undo_redo->add_do_method(this, "_update_editor", player);
  486. undo_redo->add_undo_method(this, "_update_editor", player);
  487. undo_redo->commit_action();
  488. } break;
  489. }
  490. }
  491. }
  492. void AnimationLibraryEditor::update_tree() {
  493. if (updating) {
  494. return;
  495. }
  496. tree->clear();
  497. ERR_FAIL_COND(!player);
  498. Color ss_color = get_theme_color(SNAME("prop_subsection"), SNAME("Editor"));
  499. TreeItem *root = tree->create_item();
  500. TypedArray<StringName> libs = player->call("get_animation_library_list");
  501. for (int i = 0; i < libs.size(); i++) {
  502. const StringName K = libs[i];
  503. TreeItem *libitem = tree->create_item(root);
  504. libitem->set_text(0, K);
  505. if (K == StringName()) {
  506. libitem->set_suffix(0, TTR("[Global]"));
  507. } else {
  508. libitem->set_suffix(0, "");
  509. }
  510. libitem->set_editable(0, true);
  511. libitem->set_metadata(0, K);
  512. libitem->set_icon(0, get_theme_icon("AnimationLibrary", "EditorIcons"));
  513. libitem->add_button(0, get_theme_icon("Add", "EditorIcons"), LIB_BUTTON_ADD, false, TTR("Add Animation to Library"));
  514. libitem->add_button(0, get_theme_icon("Load", "EditorIcons"), LIB_BUTTON_LOAD, false, TTR("Load animation from file and add to library"));
  515. libitem->add_button(0, get_theme_icon("ActionPaste", "EditorIcons"), LIB_BUTTON_PASTE, false, TTR("Paste Animation to Library from clipboard"));
  516. Ref<AnimationLibrary> al = player->call("get_animation_library", K);
  517. if (al->get_path().is_resource_file()) {
  518. libitem->set_text(1, al->get_path().get_file());
  519. libitem->set_tooltip(1, al->get_path());
  520. } else {
  521. libitem->set_text(1, TTR("[built-in]"));
  522. }
  523. libitem->add_button(1, get_theme_icon("Save", "EditorIcons"), LIB_BUTTON_FILE, false, TTR("Save animation library to resource on disk"));
  524. libitem->add_button(1, get_theme_icon("Remove", "EditorIcons"), LIB_BUTTON_DELETE, false, TTR("Remove animation library"));
  525. libitem->set_custom_bg_color(0, ss_color);
  526. List<StringName> animations;
  527. al->get_animation_list(&animations);
  528. for (const StringName &L : animations) {
  529. TreeItem *anitem = tree->create_item(libitem);
  530. anitem->set_text(0, L);
  531. anitem->set_editable(0, true);
  532. anitem->set_metadata(0, L);
  533. anitem->set_icon(0, get_theme_icon("Animation", "EditorIcons"));
  534. anitem->add_button(0, get_theme_icon("ActionCopy", "EditorIcons"), ANIM_BUTTON_COPY, false, TTR("Copy animation to clipboard"));
  535. Ref<Animation> anim = al->get_animation(L);
  536. if (anim->get_path().is_resource_file()) {
  537. anitem->set_text(1, anim->get_path().get_file());
  538. anitem->set_tooltip(1, anim->get_path());
  539. } else {
  540. anitem->set_text(1, TTR("[built-in]"));
  541. }
  542. anitem->add_button(1, get_theme_icon("Save", "EditorIcons"), ANIM_BUTTON_FILE, false, TTR("Save animation to resource on disk"));
  543. anitem->add_button(1, get_theme_icon("Remove", "EditorIcons"), ANIM_BUTTON_DELETE, false, TTR("Remove animation from Library"));
  544. }
  545. }
  546. }
  547. void AnimationLibraryEditor::show_dialog() {
  548. update_tree();
  549. popup_centered_ratio(0.5);
  550. }
  551. void AnimationLibraryEditor::_update_editor(Object *p_player) {
  552. emit_signal("update_editor", p_player);
  553. }
  554. void AnimationLibraryEditor::_bind_methods() {
  555. ClassDB::bind_method(D_METHOD("_update_editor", "player"), &AnimationLibraryEditor::_update_editor);
  556. ADD_SIGNAL(MethodInfo("update_editor"));
  557. }
  558. AnimationLibraryEditor::AnimationLibraryEditor() {
  559. set_title(TTR("Edit Animation Libraries"));
  560. file_dialog = memnew(EditorFileDialog);
  561. add_child(file_dialog);
  562. file_dialog->connect("file_selected", callable_mp(this, &AnimationLibraryEditor::_load_file));
  563. add_library_dialog = memnew(ConfirmationDialog);
  564. VBoxContainer *dialog_vb = memnew(VBoxContainer);
  565. add_library_name = memnew(LineEdit);
  566. dialog_vb->add_child(add_library_name);
  567. add_library_name->connect("text_changed", callable_mp(this, &AnimationLibraryEditor::_add_library_validate));
  568. add_child(add_library_dialog);
  569. add_library_validate = memnew(Label);
  570. dialog_vb->add_child(add_library_validate);
  571. add_library_dialog->add_child(dialog_vb);
  572. add_library_dialog->connect("confirmed", callable_mp(this, &AnimationLibraryEditor::_add_library_confirm));
  573. add_library_dialog->register_text_enter(add_library_name);
  574. VBoxContainer *vb = memnew(VBoxContainer);
  575. HBoxContainer *hb = memnew(HBoxContainer);
  576. hb->add_spacer(true);
  577. Button *b = memnew(Button(TTR("Add Library")));
  578. b->connect("pressed", callable_mp(this, &AnimationLibraryEditor::_add_library));
  579. hb->add_child(b);
  580. b = memnew(Button(TTR("Load Library")));
  581. b->connect("pressed", callable_mp(this, &AnimationLibraryEditor::_load_library));
  582. hb->add_child(b);
  583. vb->add_child(hb);
  584. tree = memnew(Tree);
  585. vb->add_child(tree);
  586. tree->set_columns(2);
  587. tree->set_column_titles_visible(true);
  588. tree->set_column_title(0, TTR("Resource"));
  589. tree->set_column_title(1, TTR("Storage"));
  590. tree->set_column_expand(0, true);
  591. tree->set_column_custom_minimum_width(1, EDSCALE * 250);
  592. tree->set_column_expand(1, false);
  593. tree->set_hide_root(true);
  594. tree->set_hide_folding(true);
  595. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  596. tree->connect("item_edited", callable_mp(this, &AnimationLibraryEditor::_item_renamed));
  597. tree->connect("button_pressed", callable_mp(this, &AnimationLibraryEditor::_button_pressed));
  598. file_popup = memnew(PopupMenu);
  599. add_child(file_popup);
  600. file_popup->connect("id_pressed", callable_mp(this, &AnimationLibraryEditor::_file_popup_selected));
  601. add_child(vb);
  602. error_dialog = memnew(AcceptDialog);
  603. error_dialog->set_title(TTR("Error:"));
  604. add_child(error_dialog);
  605. }