Skip to content

Commit

Permalink
Expose 'Reimport' on right-click context menu in the FileSystem panel
Browse files Browse the repository at this point in the history
  • Loading branch information
nongvantinh committed May 1, 2023
1 parent 4d5f10f commit 44921db
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
41 changes: 34 additions & 7 deletions editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2034,13 +2034,7 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selected
} break;

case FILE_REIMPORT: {
// Reimport all selected files.
Vector<String> reimport;
for (int i = 0; i < p_selected.size(); i++) {
reimport.push_back(p_selected[i]);
}

ERR_FAIL_COND_MSG(reimport.size() == 0, "You need to select files to reimport them.");
ImportDock::get_singleton()->reimport_resources(p_selected);
} break;

case FILE_NEW_FOLDER: {
Expand Down Expand Up @@ -2650,6 +2644,39 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, Vector<Str
if (!all_not_favorites) {
p_popup->add_icon_item(get_theme_icon(SNAME("NonFavorite"), SNAME("EditorIcons")), TTR("Remove from Favorites"), FILE_REMOVE_FAVORITE);
}

{
List<String> resource_extensions;
ResourceFormatImporter::get_singleton()->get_recognized_extensions_for_type("Resource", &resource_extensions);
HashMap<String, int> extension_list;
for (const String &E : resource_extensions) {
extension_list[E] = 0;
}

bool resource_valid = true;
for (int i = 0; i != p_paths.size(); ++i) {
String extension = p_paths[i].get_extension();

if (extension_list.has(extension)) {
++extension_list[extension];
} else {
resource_valid = false;
break;
}
}

// Prevent corruption of reimported files when multiple file types are present.
int num_resource_types = 0;
for (const KeyValue<String, int> &elem : extension_list) {
if (0 != elem.value) {
++num_resource_types;
}
}

if (resource_valid && 1 == num_resource_types) {
p_popup->add_icon_item(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")), TTR("Reimport"), FILE_REIMPORT);
}
}
}

if (p_paths.size() == 1) {
Expand Down
15 changes: 15 additions & 0 deletions editor/import_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,21 @@ void ImportDock::set_edit_multiple_paths(const Vector<String> &p_paths) {
}
}

void ImportDock::reimport_resources(const Vector<String> &p_paths) {
switch (p_paths.size()) {
case 0:
ERR_FAIL_MSG("You need to select files to reimport them.");
case 1:
set_edit_path(p_paths[0]);
break;
default:
set_edit_multiple_paths(p_paths);
break;
}

_reimport_attempt();
}

void ImportDock::_update_preset_menu() {
preset->get_popup()->clear();

Expand Down
1 change: 1 addition & 0 deletions editor/import_dock.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ImportDock : public VBoxContainer {
public:
void set_edit_path(const String &p_path);
void set_edit_multiple_paths(const Vector<String> &p_paths);
void reimport_resources(const Vector<String> &p_paths);
void initialize_import_options() const;
void clear();

Expand Down

0 comments on commit 44921db

Please sign in to comment.