animation_library_editor.cpp 26 KB

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