animation_library_editor.cpp 30 KB

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