editor_reimport_dialog.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*************************************************************************/
  2. /* editor_reimport_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "editor_reimport_dialog.h"
  31. #include "editor_file_system.h"
  32. #include "editor_node.h"
  33. void EditorReImportDialog::popup_reimport() {
  34. if (EditorFileSystem::get_singleton()->is_scanning()) {
  35. error->set_text(TTR("Please wait for scan to complete."));
  36. error->popup_centered_minsize();
  37. return;
  38. }
  39. tree->clear();
  40. items.clear();
  41. List<String> ril;
  42. EditorFileSystem::get_singleton()->get_changed_sources(&ril);
  43. scene_must_save = false;
  44. TreeItem *root = tree->create_item();
  45. for (List<String>::Element *E = ril.front(); E; E = E->next()) {
  46. TreeItem *item = tree->create_item(root);
  47. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  48. item->set_metadata(0, E->get());
  49. item->set_text(0, E->get().replace_first("res://", ""));
  50. item->set_tooltip(0, E->get());
  51. item->set_checked(0, true);
  52. item->set_editable(0, true);
  53. items.push_back(item);
  54. String name = E->get();
  55. if (EditorFileSystem::get_singleton()->get_file_type(name) == "PackedScene" && EditorNode::get_singleton()->is_scene_in_use(name)) {
  56. scene_must_save = true;
  57. }
  58. }
  59. if (scene_must_save) {
  60. if (EditorNode::get_singleton()->get_edited_scene() && EditorNode::get_singleton()->get_edited_scene()->get_filename() == "") {
  61. error->set_text(TTR("Current scene must be saved to re-import."));
  62. error->popup_centered_minsize();
  63. get_ok()->set_text(TTR("Re-Import"));
  64. get_ok()->set_disabled(true);
  65. return;
  66. }
  67. get_ok()->set_disabled(false);
  68. get_ok()->set_text(TTR("Save & Re-Import"));
  69. } else {
  70. get_ok()->set_text(TTR("Re-Import"));
  71. get_ok()->set_disabled(false);
  72. }
  73. popup_centered(Size2(600, 400));
  74. }
  75. void EditorReImportDialog::ok_pressed() {
  76. if (EditorFileSystem::get_singleton()->is_scanning()) {
  77. error->set_text(TTR("Please wait for scan to complete."));
  78. error->popup_centered_minsize();
  79. return;
  80. }
  81. EditorProgress ep("reimport", TTR("Re-Importing"), items.size());
  82. String reload_fname;
  83. if (scene_must_save && EditorNode::get_singleton()->get_edited_scene()) {
  84. reload_fname = EditorNode::get_singleton()->get_edited_scene()->get_filename();
  85. EditorNode::get_singleton()->save_scene(reload_fname);
  86. EditorNode::get_singleton()->clear_scene();
  87. }
  88. for (int i = 0; i < items.size(); i++) {
  89. String it = items[i]->get_metadata(0);
  90. ep.step(items[i]->get_text(0), i);
  91. print_line("reload import from: " + it);
  92. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(it);
  93. ERR_CONTINUE(rimd.is_null());
  94. String editor = rimd->get_editor();
  95. Ref<EditorImportPlugin> eip = EditorImportExport::get_singleton()->get_import_plugin_by_name(editor);
  96. ERR_CONTINUE(eip.is_null());
  97. Error err = eip->import(it, rimd);
  98. if (err != OK) {
  99. EditorNode::add_io_error("Error Importing:\n " + it);
  100. }
  101. }
  102. if (reload_fname != "") {
  103. EditorNode::get_singleton()->load_scene(reload_fname);
  104. }
  105. EditorFileSystem::get_singleton()->scan_sources();
  106. }
  107. EditorReImportDialog::EditorReImportDialog() {
  108. tree = memnew(Tree);
  109. add_child(tree);
  110. tree->set_hide_root(true);
  111. set_child_rect(tree);
  112. set_title(TTR("Re-Import Changed Resources"));
  113. error = memnew(AcceptDialog);
  114. add_child(error);
  115. scene_must_save = false;
  116. }