animation_library_editor.cpp 30 KB

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