From 92ff750bbf75b4c55e2c6368c567f45e0dacdef6 Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Fri, 9 Jun 2023 15:17:48 +0200 Subject: [PATCH] Refactor: Get rid of asset handle for drag & drop `AssetHandle` is meant as temporary design and should be replaced by `AssetRepresentation`. This moves us another step closer to that. Rather than taking data from the volatile asset handle and storing that in the drag data, store the (more persistent) asset representation there and access data from it where needed. --- .../asset_system/AS_asset_representation.h | 3 ++ .../asset_system/AS_asset_representation.hh | 2 - .../editors/asset/intern/asset_list.cc | 1 - source/blender/editors/include/UI_interface.h | 5 +- .../editors/interface/interface_drag.cc | 5 +- .../interface_template_asset_view.cc | 23 ++++----- .../blender/editors/space_file/file_draw.cc | 14 ++---- .../editors/space_view3d/CMakeLists.txt | 1 + .../editors/space_view3d/space_view3d.cc | 7 ++- source/blender/windowmanager/WM_api.h | 5 +- source/blender/windowmanager/WM_types.h | 10 +--- .../windowmanager/intern/wm_dragdrop.cc | 47 +++++++++---------- 12 files changed, 49 insertions(+), 74 deletions(-) diff --git a/source/blender/asset_system/AS_asset_representation.h b/source/blender/asset_system/AS_asset_representation.h index 99a16ff8e60..dff57df3555 100644 --- a/source/blender/asset_system/AS_asset_representation.h +++ b/source/blender/asset_system/AS_asset_representation.h @@ -34,6 +34,9 @@ bool AS_asset_representation_is_local_id(const AssetRepresentation *asset) ATTR_ bool AS_asset_representation_is_never_link(const AssetRepresentation *asset) ATTR_WARN_UNUSED_RESULT; +bool AS_asset_representation_may_override_import_method(const AssetRepresentation *asset); +bool AS_asset_representation_use_relative_path_get(const AssetRepresentation *asset); + /** * C version of #AssetRepresentation::make_weak_reference. Returned pointer needs freeing with * #MEM_delete() or #BKE_asset_weak_reference_free(). diff --git a/source/blender/asset_system/AS_asset_representation.hh b/source/blender/asset_system/AS_asset_representation.hh index 7b9cb4b3994..9f0a5174bb4 100644 --- a/source/blender/asset_system/AS_asset_representation.hh +++ b/source/blender/asset_system/AS_asset_representation.hh @@ -130,5 +130,3 @@ std::string AS_asset_representation_full_path_get(const ::AssetRepresentation *a std::string AS_asset_representation_full_library_path_get(const ::AssetRepresentation *asset); std::optional AS_asset_representation_import_method_get( const ::AssetRepresentation *asset_handle); -bool AS_asset_representation_may_override_import_method(const ::AssetRepresentation *asset_handle); -bool AS_asset_representation_use_relative_path_get(const ::AssetRepresentation *asset_handle); diff --git a/source/blender/editors/asset/intern/asset_list.cc b/source/blender/editors/asset/intern/asset_list.cc index f2f531a8a2a..a61bfe7695c 100644 --- a/source/blender/editors/asset/intern/asset_list.cc +++ b/source/blender/editors/asset/intern/asset_list.cc @@ -31,7 +31,6 @@ #include "../space_file/file_indexer.h" #include "../space_file/filelist.h" -#include "ED_asset_handle.h" #include "ED_asset_indexer.h" #include "ED_asset_list.h" #include "ED_asset_list.hh" diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h index da1f640643a..f4fec3abac9 100644 --- a/source/blender/editors/include/UI_interface.h +++ b/source/blender/editors/include/UI_interface.h @@ -22,7 +22,7 @@ extern "C" { struct ARegion; struct AssetFilterSettings; -struct AssetHandle; +struct AssetRepresentation; struct AutoComplete; struct EnumPropertyItem; struct FileSelectParams; @@ -1813,8 +1813,7 @@ void UI_but_drag_attach_image(uiBut *but, struct ImBuf *imb, float scale); * \param asset: May be passed from a temporary variable, drag data only stores a copy of this. */ void UI_but_drag_set_asset(uiBut *but, - const struct AssetHandle *asset, - const char *path, + const struct AssetRepresentation *asset, int import_type, /* eAssetImportType */ int icon, struct ImBuf *imb, diff --git a/source/blender/editors/interface/interface_drag.cc b/source/blender/editors/interface/interface_drag.cc index 9ed9bbfef00..e26d0659e17 100644 --- a/source/blender/editors/interface/interface_drag.cc +++ b/source/blender/editors/interface/interface_drag.cc @@ -30,14 +30,13 @@ void UI_but_drag_attach_image(uiBut *but, ImBuf *imb, const float scale) } void UI_but_drag_set_asset(uiBut *but, - const AssetHandle *asset_handle, - const char *path, + const AssetRepresentation *asset, int import_type, int icon, ImBuf *imb, float scale) { - wmDragAsset *asset_drag = WM_drag_create_asset_data(asset_handle, path, import_type); + wmDragAsset *asset_drag = WM_drag_create_asset_data(asset, import_type); /* FIXME: This is temporary evil solution to get scene/view-layer/etc in the copy callback of the * #wmDropBox. diff --git a/source/blender/editors/interface/interface_template_asset_view.cc b/source/blender/editors/interface/interface_template_asset_view.cc index d5676b38f90..a4102bc45d9 100644 --- a/source/blender/editors/interface/interface_template_asset_view.cc +++ b/source/blender/editors/interface/interface_template_asset_view.cc @@ -6,6 +6,7 @@ * \ingroup edinterface */ +#include "AS_asset_representation.h" #include "AS_asset_representation.hh" #include "DNA_space_types.h" @@ -46,28 +47,20 @@ struct AssetViewListData { static void asset_view_item_but_drag_set(uiBut *but, AssetHandle *asset_handle) { - ID *id = ED_asset_handle_get_local_id(asset_handle); + AssetRepresentation *asset = ED_asset_handle_get_representation(asset_handle); + + ID *id = AS_asset_representation_local_id_get(asset); if (id != nullptr) { UI_but_drag_set_id(but, id); return; } - char blend_path[FILE_MAX_LIBEXTRA]; - ED_asset_handle_get_full_library_path(asset_handle, blend_path); - const eAssetImportMethod import_method = - ED_asset_handle_get_import_method(asset_handle).value_or(ASSET_IMPORT_APPEND_REUSE); + AS_asset_representation_import_method_get(asset).value_or(ASSET_IMPORT_APPEND_REUSE); - if (blend_path[0]) { - ImBuf *imbuf = ED_assetlist_asset_image_get(asset_handle); - UI_but_drag_set_asset(but, - asset_handle, - BLI_strdup(blend_path), - import_method, - ED_asset_handle_get_preview_icon_id(asset_handle), - imbuf, - 1.0f); - } + ImBuf *imbuf = ED_assetlist_asset_image_get(asset_handle); + UI_but_drag_set_asset( + but, asset, import_method, ED_asset_handle_get_preview_icon_id(asset_handle), imbuf, 1.0f); } static void asset_view_draw_item(uiList *ui_list, diff --git a/source/blender/editors/space_file/file_draw.cc b/source/blender/editors/space_file/file_draw.cc index a174221283e..ebe2b2ac173 100644 --- a/source/blender/editors/space_file/file_draw.cc +++ b/source/blender/editors/space_file/file_draw.cc @@ -39,7 +39,6 @@ #include "RNA_access.h" #include "RNA_prototypes.h" -#include "ED_asset_handle.h" #include "ED_fileselect.h" #include "ED_screen.h" @@ -156,17 +155,10 @@ static void file_but_enable_drag(uiBut *but, } else if (sfile->browse_mode == FILE_BROWSE_MODE_ASSETS && (file->typeflag & FILE_TYPE_ASSET) != 0) { - char blend_path[FILE_MAX_LIBEXTRA]; - if (BKE_blendfile_library_path_explode(path, blend_path, nullptr, nullptr)) { - const int import_method = ED_fileselect_asset_import_method_get(sfile, file); - BLI_assert(import_method > -1); + const int import_method = ED_fileselect_asset_import_method_get(sfile, file); + BLI_assert(import_method > -1); - AssetHandle asset{}; - asset.file_data = file; - - UI_but_drag_set_asset( - but, &asset, BLI_strdup(blend_path), import_method, icon, preview_image, scale); - } + UI_but_drag_set_asset(but, file->asset, import_method, icon, preview_image, scale); } else if (preview_image) { UI_but_drag_set_image(but, path, icon, preview_image, scale); diff --git a/source/blender/editors/space_view3d/CMakeLists.txt b/source/blender/editors/space_view3d/CMakeLists.txt index f3cafb1254a..4ebdfcbd6d6 100644 --- a/source/blender/editors/space_view3d/CMakeLists.txt +++ b/source/blender/editors/space_view3d/CMakeLists.txt @@ -4,6 +4,7 @@ set(INC ../include + ../../asset_system ../../blenfont ../../blenkernel ../../blenlib diff --git a/source/blender/editors/space_view3d/space_view3d.cc b/source/blender/editors/space_view3d/space_view3d.cc index b86bce4ff68..c44e14e5dc0 100644 --- a/source/blender/editors/space_view3d/space_view3d.cc +++ b/source/blender/editors/space_view3d/space_view3d.cc @@ -12,6 +12,8 @@ #include #include +#include "AS_asset_representation.h" + #include "DNA_collection_types.h" #include "DNA_defaults.h" #include "DNA_gpencil_legacy_types.h" @@ -496,7 +498,7 @@ static ID_Type view3d_drop_id_in_main_region_poll_get_id_type(bContext *C, wmDragAsset *asset_drag = WM_drag_get_asset_data(drag, 0); if (asset_drag) { - return ID_Type(asset_drag->id_type); + return AS_asset_representation_id_type_get(asset_drag->asset); } return ID_Type(0); @@ -731,7 +733,8 @@ static bool view3d_geometry_nodes_drop_poll(bContext *C, wmDrag *drag, const wmE if (!asset_data) { return false; } - const IDProperty *tree_type = BKE_asset_metadata_idprop_find(asset_data->metadata, "type"); + const AssetMetaData *metadata = AS_asset_representation_metadata_get(asset_data->asset); + const IDProperty *tree_type = BKE_asset_metadata_idprop_find(metadata, "type"); if (!tree_type || IDP_Int(tree_type) != NTREE_GEOMETRY) { return false; } diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h index 38587abe548..70f79cf3e36 100644 --- a/source/blender/windowmanager/WM_api.h +++ b/source/blender/windowmanager/WM_api.h @@ -1444,8 +1444,7 @@ bool WM_drag_is_ID_type(const struct wmDrag *drag, int idcode); /** * \note Does not store \a asset in any way, so it's fine to pass a temporary. */ -wmDragAsset *WM_drag_create_asset_data(const struct AssetHandle *asset, - const char *path, +wmDragAsset *WM_drag_create_asset_data(const struct AssetRepresentation *asset, int /* #eAssetImportMethod */ import_type); struct wmDragAsset *WM_drag_get_asset_data(const struct wmDrag *drag, int idcode); struct AssetMetaData *WM_drag_get_asset_meta_data(const struct wmDrag *drag, int idcode); @@ -1475,7 +1474,7 @@ struct wmDragAssetCatalog *WM_drag_get_asset_catalog_data(const struct wmDrag *d /** * \note Does not store \a asset in any way, so it's fine to pass a temporary. */ -void WM_drag_add_asset_list_item(wmDrag *drag, const struct AssetHandle *asset); +void WM_drag_add_asset_list_item(wmDrag *drag, const struct AssetRepresentation *asset); const ListBase *WM_drag_asset_list_get(const wmDrag *drag); const char *WM_drag_get_item_name(struct wmDrag *drag); diff --git a/source/blender/windowmanager/WM_types.h b/source/blender/windowmanager/WM_types.h index 3f7b8938c38..e329f2b4a39 100644 --- a/source/blender/windowmanager/WM_types.h +++ b/source/blender/windowmanager/WM_types.h @@ -1095,16 +1095,8 @@ typedef struct wmDragID { } wmDragID; typedef struct wmDragAsset { - /* NOTE: Can't store the #AssetHandle here, since the #FileDirEntry it wraps may be freed while - * dragging. So store necessary data here directly. */ - - char name[64]; /* MAX_NAME */ - /* Always freed. */ - const char *path; - int id_type; - struct AssetMetaData *metadata; int import_method; /* eAssetImportType */ - bool use_relative_path; + const struct AssetRepresentation *asset; /* FIXME: This is temporary evil solution to get scene/view-layer/etc in the copy callback of the * #wmDropBox. diff --git a/source/blender/windowmanager/intern/wm_dragdrop.cc b/source/blender/windowmanager/intern/wm_dragdrop.cc index 44ee394ed49..df46cab44a5 100644 --- a/source/blender/windowmanager/intern/wm_dragdrop.cc +++ b/source/blender/windowmanager/intern/wm_dragdrop.cc @@ -10,6 +10,9 @@ #include +#include "AS_asset_representation.h" +#include "AS_asset_representation.hh" + #include "DNA_asset_types.h" #include "DNA_screen_types.h" #include "DNA_space_types.h" @@ -212,7 +215,7 @@ wmDrag *WM_drag_data_create( LISTBASE_FOREACH (const CollectionPointerLink *, link, &asset_file_links) { const FileDirEntry *asset_file = static_cast(link->ptr.data); const AssetHandle asset_handle = {asset_file}; - WM_drag_add_asset_list_item(drag, &asset_handle); + WM_drag_add_asset_list_item(drag, ED_asset_handle_get_representation(&asset_handle)); } BLI_freelistN(&asset_file_links); break; @@ -566,23 +569,18 @@ bool WM_drag_is_ID_type(const wmDrag *drag, int idcode) return WM_drag_get_local_ID(drag, idcode) || WM_drag_get_asset_data(drag, idcode); } -wmDragAsset *WM_drag_create_asset_data(const AssetHandle *asset, const char *path, int import_type) +wmDragAsset *WM_drag_create_asset_data(const AssetRepresentation *asset, int import_type) { wmDragAsset *asset_drag = MEM_new(__func__); - STRNCPY(asset_drag->name, ED_asset_handle_get_name(asset)); - asset_drag->metadata = ED_asset_handle_get_metadata(asset); - asset_drag->path = path; - asset_drag->id_type = ED_asset_handle_get_id_type(asset); + asset_drag->asset = asset; asset_drag->import_method = import_type; - asset_drag->use_relative_path = ED_asset_handle_get_use_relative_path(asset); return asset_drag; } static void wm_drag_free_asset_data(wmDragAsset **asset_data) { - MEM_freeN((char *)(*asset_data)->path); MEM_SAFE_FREE(*asset_data); } @@ -593,14 +591,15 @@ wmDragAsset *WM_drag_get_asset_data(const wmDrag *drag, int idcode) } wmDragAsset *asset_drag = static_cast(drag->poin); - return ELEM(idcode, 0, asset_drag->id_type) ? asset_drag : nullptr; + ID_Type asset_idcode = AS_asset_representation_id_type_get(asset_drag->asset); + return ELEM(idcode, 0, asset_idcode) ? asset_drag : nullptr; } AssetMetaData *WM_drag_get_asset_meta_data(const wmDrag *drag, int idcode) { wmDragAsset *drag_asset = WM_drag_get_asset_data(drag, idcode); if (drag_asset) { - return drag_asset->metadata; + return AS_asset_representation_metadata_get(drag_asset->asset); } ID *local_id = WM_drag_get_local_ID(drag, idcode); @@ -618,8 +617,10 @@ ID *WM_drag_asset_id_import(wmDragAsset *asset_drag, const int flag_extra) eFileSel_Params_Flag flag = static_cast(flag_extra) | FILE_ACTIVE_COLLECTION; - const char *name = asset_drag->name; - ID_Type idtype = static_cast(asset_drag->id_type); + const char *name = AS_asset_representation_name_get(asset_drag->asset); + const std::string blend_path = AS_asset_representation_full_library_path_get(asset_drag->asset); + const ID_Type idtype = AS_asset_representation_id_type_get(asset_drag->asset); + const bool use_relative_path = AS_asset_representation_use_relative_path_get(asset_drag->asset); /* FIXME: Link/Append should happens in the operator called at the end of drop process, not from * here. */ @@ -629,15 +630,13 @@ ID *WM_drag_asset_id_import(wmDragAsset *asset_drag, const int flag_extra) ViewLayer *view_layer = CTX_data_view_layer(asset_drag->evil_C); View3D *view3d = CTX_wm_view3d(asset_drag->evil_C); - const bool use_relative_path = asset_drag->use_relative_path; - switch (eAssetImportMethod(asset_drag->import_method)) { case ASSET_IMPORT_LINK: return WM_file_link_datablock(bmain, scene, view_layer, view3d, - asset_drag->path, + blend_path.c_str(), idtype, name, flag | (use_relative_path ? FILE_RELPATH : 0)); @@ -646,7 +645,7 @@ ID *WM_drag_asset_id_import(wmDragAsset *asset_drag, const int flag_extra) scene, view_layer, view3d, - asset_drag->path, + blend_path.c_str(), idtype, name, flag | BLO_LIBLINK_APPEND_RECURSIVE | @@ -657,7 +656,7 @@ ID *WM_drag_asset_id_import(wmDragAsset *asset_drag, const int flag_extra) scene, view_layer, view3d, - asset_drag->path, + blend_path.c_str(), idtype, name, flag | BLO_LIBLINK_APPEND_RECURSIVE | BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR | @@ -708,10 +707,11 @@ void WM_drag_free_imported_drag_ID(Main *bmain, wmDrag *drag, wmDropBox *drop) return; } + ID_Type asset_id_type = AS_asset_representation_id_type_get(asset_drag->asset); /* Try to find the imported ID. For this to work either a "session_uuid" or "name" property must * have been defined (see #WM_operator_properties_id_lookup()). */ ID *id = WM_operator_properties_id_lookup_from_name_or_session_uuid( - bmain, drop->ptr, static_cast(asset_drag->id_type)); + bmain, drop->ptr, asset_id_type); if (id != nullptr) { /* Do not delete the dragged ID if it has any user, otherwise if it is a 're-used' ID it will * cause #95636. Note that we need first to add the user that we want to remove in @@ -730,7 +730,7 @@ wmDragAssetCatalog *WM_drag_get_asset_catalog_data(const wmDrag *drag) return static_cast(drag->poin); } -void WM_drag_add_asset_list_item(wmDrag *drag, const AssetHandle *asset) +void WM_drag_add_asset_list_item(wmDrag *drag, const AssetRepresentation *asset) { BLI_assert(drag->type == WM_DRAG_ASSET_LIST); @@ -738,17 +738,14 @@ void WM_drag_add_asset_list_item(wmDrag *drag, const AssetHandle *asset) /* Add to list. */ wmDragAssetListItem *drag_asset = MEM_cnew(__func__); - ID *local_id = ED_asset_handle_get_local_id(asset); + ID *local_id = AS_asset_representation_local_id_get(asset); if (local_id) { drag_asset->is_external = false; drag_asset->asset_data.local_id = local_id; } else { - char asset_blend_path[FILE_MAX_LIBEXTRA]; - ED_asset_handle_get_full_library_path(asset, asset_blend_path); drag_asset->is_external = true; - drag_asset->asset_data.external_info = WM_drag_create_asset_data( - asset, BLI_strdup(asset_blend_path), ASSET_IMPORT_APPEND); + drag_asset->asset_data.external_info = WM_drag_create_asset_data(asset, ASSET_IMPORT_APPEND); } BLI_addtail(&drag->asset_items, drag_asset); } @@ -844,7 +841,7 @@ const char *WM_drag_get_item_name(wmDrag *drag) } case WM_DRAG_ASSET: { const wmDragAsset *asset_drag = WM_drag_get_asset_data(drag, 0); - return asset_drag->name; + return AS_asset_representation_name_get(asset_drag->asset); } case WM_DRAG_PATH: { const wmDragPath *path_drag_data = static_cast(drag->poin);