From 01ab36ebc1c43764b8bfb4c41f4c837a3b8757cb Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Wed, 24 Nov 2021 17:29:43 +0100 Subject: Asset Browser: Support dragging catalogs into top level This was an oversight when I added catalog drag & drop support. I forgot to add this for dragging catalogs into the top level by dragging into to the "All" item as well. This made the drag & drop support rather broken because it wouldn't work for a basic case. --- source/blender/editors/asset/ED_asset_catalog.hh | 20 +++++++++++++++++--- .../blender/editors/asset/intern/asset_catalog.cc | 21 ++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) (limited to 'source/blender/editors/asset') diff --git a/source/blender/editors/asset/ED_asset_catalog.hh b/source/blender/editors/asset/ED_asset_catalog.hh index 8da8fc0d6c9..cbfce16e9eb 100644 --- a/source/blender/editors/asset/ED_asset_catalog.hh +++ b/source/blender/editors/asset/ED_asset_catalog.hh @@ -20,6 +20,8 @@ #pragma once +#include + #include "BKE_asset_catalog.hh" #include "BLI_string_ref.hh" @@ -37,6 +39,18 @@ void ED_asset_catalog_remove(AssetLibrary *library, const blender::bke::CatalogI void ED_asset_catalog_rename(AssetLibrary *library, blender::bke::CatalogID catalog_id, blender::StringRefNull new_name); -void ED_asset_catalog_move(AssetLibrary *library, - blender::bke::CatalogID src_catalog_id, - blender::bke::CatalogID dst_parent_catalog_id); +/** + * Reinsert catalog identified by \a src_catalog_id as child to catalog identified by \a + * dst_parent_catalog_id. If \a dst_parent_catalog_id is not set, the catalog is moved to the root + * level of the tree. + * The name of the reinserted catalog is made unique within the parent. Note that moving a catalog + * to the same level it was before will also change its name, since the name uniqueness check isn't + * smart enough to ignore the item to be reinserted. So the caller is expected to handle this case + * to avoid unwanted renames. + * + * Nothing is done (debug builds run into an assert) if the given catalog IDs can't be identified. + */ +void ED_asset_catalog_move( + AssetLibrary *library, + blender::bke::CatalogID src_catalog_id, + std::optional dst_parent_catalog_id = std::nullopt); diff --git a/source/blender/editors/asset/intern/asset_catalog.cc b/source/blender/editors/asset/intern/asset_catalog.cc index 8e1e5be2e47..218ebe48934 100644 --- a/source/blender/editors/asset/intern/asset_catalog.cc +++ b/source/blender/editors/asset/intern/asset_catalog.cc @@ -123,7 +123,7 @@ void ED_asset_catalog_rename(::AssetLibrary *library, void ED_asset_catalog_move(::AssetLibrary *library, const CatalogID src_catalog_id, - const CatalogID dst_parent_catalog_id) + const std::optional dst_parent_catalog_id) { bke::AssetCatalogService *catalog_service = BKE_asset_library_get_catalog_service(library); if (!catalog_service) { @@ -132,9 +132,24 @@ void ED_asset_catalog_move(::AssetLibrary *library, } AssetCatalog *src_catalog = catalog_service->find_catalog(src_catalog_id); - AssetCatalog *dst_catalog = catalog_service->find_catalog(dst_parent_catalog_id); + if (!src_catalog) { + BLI_assert_unreachable(); + return; + } + AssetCatalog *dst_catalog = dst_parent_catalog_id ? + catalog_service->find_catalog(*dst_parent_catalog_id) : + nullptr; + if (!dst_catalog && dst_parent_catalog_id) { + BLI_assert_unreachable(); + return; + } - const AssetCatalogPath new_path = dst_catalog->path / StringRef(src_catalog->path.name()); + std::string unique_name = catalog_name_ensure_unique( + *catalog_service, src_catalog->path.name(), dst_catalog ? dst_catalog->path.c_str() : ""); + /* If a destination catalog was given, construct the path using that. Otherwise, the path is just + * the name of the catalog to be moved, which means it ends up at the root level. */ + const AssetCatalogPath new_path = dst_catalog ? (dst_catalog->path / unique_name) : + AssetCatalogPath{unique_name}; const AssetCatalogPath clean_new_path = new_path.cleanup(); if (new_path == src_catalog->path || clean_new_path == src_catalog->path) { -- cgit v1.2.3