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