Assets: Sort assets by catalog

Sort assets in asset shelf and asset browser by catalogs (and within
catalogs by name like before). This groups similar assets better,
reducing visual noise and making specific assets easier to find.

Pull Request: https://projects.blender.org/blender/blender/pulls/121316
This commit is contained in:
Julian Eisel
2024-05-02 10:26:02 -04:00
committed by Hans Goudey
parent 1b0012d51c
commit 471378c666
3 changed files with 53 additions and 1 deletions

View File

@@ -142,7 +142,7 @@ void AssetList::setup()
/* Relevant bits from file_refresh(). */
/* TODO pass options properly. */
filelist_setrecursion(files, FILE_SELECT_MAX_RECURSIONS);
filelist_setsorting(files, FILE_SORT_ALPHA, false);
filelist_setsorting(files, FILE_SORT_ASSET_CATALOG, false);
filelist_setlibrary(files, &library_ref_);
filelist_setfilter_options(
files,

View File

@@ -567,6 +567,53 @@ static int compare_extension(void *user_data, const void *a1, const void *a2)
return compare_apply_inverted(compare_tiebreaker(entry1, entry2), sort_data);
}
static int compare_asset_catalog(void *user_data, const void *a1, const void *a2)
{
const FileListInternEntry *entry1 = static_cast<const FileListInternEntry *>(a1);
const FileListInternEntry *entry2 = static_cast<const FileListInternEntry *>(a2);
const FileSortData *sort_data = static_cast<const FileSortData *>(user_data);
if (entry1->asset && !entry2->asset) {
return 1;
}
if (!entry1->asset && entry2->asset) {
return -1;
}
if (!entry1->asset && !entry2->asset) {
if (int ret = compare_direntry_generic(entry1, entry2); ret) {
return ret;
}
return compare_apply_inverted(compare_tiebreaker(entry1, entry2), sort_data);
}
const asset_system::AssetLibrary &asset_library1 = entry1->asset->owner_asset_library();
const asset_system::AssetLibrary &asset_library2 = entry2->asset->owner_asset_library();
const asset_system::AssetCatalog *catalog1 = asset_library1.catalog_service().find_catalog(
entry1->asset->get_metadata().catalog_id);
const asset_system::AssetCatalog *catalog2 = asset_library2.catalog_service().find_catalog(
entry2->asset->get_metadata().catalog_id);
/* Always keep assets without catalog last. */
if (catalog1 && !catalog2) {
return 1;
}
if (!catalog1 && catalog2) {
return -1;
}
if (catalog1 && catalog2) {
const int order = BLI_strcasecmp_natural(catalog1->path.name().c_str(),
catalog2->path.name().c_str());
if (order) {
return compare_apply_inverted(order, sort_data);
}
}
return compare_apply_inverted(compare_tiebreaker(entry1, entry2), sort_data);
}
void filelist_sort(FileList *filelist)
{
if (filelist->flags & FL_NEED_SORTING) {
@@ -585,6 +632,9 @@ void filelist_sort(FileList *filelist)
case FILE_SORT_EXTENSION:
sort_cb = compare_extension;
break;
case FILE_SORT_ASSET_CATALOG:
sort_cb = compare_asset_catalog;
break;
case FILE_SORT_DEFAULT:
default:
BLI_assert(0);

View File

@@ -985,6 +985,8 @@ enum eFileSortType {
FILE_SORT_EXTENSION = 2,
FILE_SORT_TIME = 3,
FILE_SORT_SIZE = 4,
/* Assets: Sort by catalog. Within each catalog, assets will be sorted by name. */
FILE_SORT_ASSET_CATALOG = 5,
};
/** #SpaceFile.tags */