Cleanup: Store asset-handle in drag data

Would previously pass a few properties that are available via the
asset-handle now. This asset-handle is also required for some of the
asset API, e.g. the temporary ID loading. This will probably be needed
before too long.
This commit is contained in:
Julian Eisel 2021-07-20 21:26:55 +02:00
parent 0af08cea40
commit cb0b017d8f
Notes: blender-bot 2023-02-14 03:29:37 +01:00
Referenced by commit eb2a6f454b, Fix T90318: Dragging asset while Asset Browser is still loading crashes
7 changed files with 32 additions and 22 deletions

View File

@ -35,9 +35,11 @@ extern "C" {
/* Struct Declarations */
struct ARegion;
struct AssetHandle;
struct AssetFilterSettings;
struct AutoComplete;
struct EnumPropertyItem;
struct FileDirEntry;
struct FileSelectParams;
struct ID;
struct IDProperty;
@ -769,9 +771,8 @@ int UI_but_return_value_get(uiBut *but);
void UI_but_drag_set_id(uiBut *but, struct ID *id);
void UI_but_drag_set_asset(uiBut *but,
const char *name,
const struct AssetHandle *asset,
const char *path,
int id_type,
int import_type, /* eFileAssetImportType */
int icon,
struct ImBuf *imb,

View File

@ -6176,10 +6176,12 @@ void UI_but_drag_set_id(uiBut *but, ID *id)
but->dragpoin = (void *)id;
}
/**
* \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 char *name,
const AssetHandle *asset,
const char *path,
int id_type,
int import_type,
int icon,
struct ImBuf *imb,
@ -6187,9 +6189,10 @@ void UI_but_drag_set_asset(uiBut *but,
{
wmDragAsset *asset_drag = MEM_mallocN(sizeof(*asset_drag), "wmDragAsset");
BLI_strncpy(asset_drag->name, name, sizeof(asset_drag->name));
asset_drag->asset_handle = MEM_mallocN(sizeof(asset_drag->asset_handle),
"wmDragAsset asset handle");
*asset_drag->asset_handle = *asset;
asset_drag->path = path;
asset_drag->id_type = id_type;
asset_drag->import_type = import_type;
but->dragtype = WM_DRAG_ASSET;

View File

@ -59,14 +59,15 @@ static void asset_view_item_but_drag_set(uiBut *but,
}
char blend_path[FILE_MAX_LIBEXTRA];
/* Context can be NULL here, it's only needed for a File Browser specific hack that should go
* away before too long. */
ED_asset_handle_get_full_library_path(NULL, &list_data->asset_library, asset_handle, blend_path);
if (blend_path[0]) {
ImBuf *imbuf = ED_assetlist_asset_image_get(asset_handle);
UI_but_drag_set_asset(but,
asset_handle->file_data->name,
asset_handle,
BLI_strdup(blend_path),
asset_handle->file_data->blentype,
FILE_ASSET_IMPORT_APPEND,
ED_asset_handle_get_preview_icon_id(asset_handle),
imbuf,

View File

@ -185,9 +185,8 @@ static void file_draw_icon(const SpaceFile *sfile,
BLI_assert(asset_params != NULL);
UI_but_drag_set_asset(but,
file->name,
&(AssetHandle){.file_data = file},
BLI_strdup(blend_path),
file->blentype,
asset_params->import_type,
icon,
preview_image,
@ -500,9 +499,8 @@ static void file_draw_preview(const SpaceFile *sfile,
BLI_assert(asset_params != NULL);
UI_but_drag_set_asset(but,
file->name,
&(AssetHandle){.file_data = file},
BLI_strdup(blend_path),
file->blentype,
asset_params->import_type,
icon,
imb,

View File

@ -53,6 +53,7 @@
#include "BKE_screen.h"
#include "BKE_workspace.h"
#include "ED_asset.h"
#include "ED_render.h"
#include "ED_screen.h"
#include "ED_space_api.h"
@ -495,7 +496,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 asset_drag->id_type;
return ED_asset_handle_get_id_type(asset_drag->asset_handle);
}
return 0;

View File

@ -924,10 +924,10 @@ typedef struct wmDragID {
} wmDragID;
typedef struct wmDragAsset {
char name[64]; /* MAX_NAME */
/* Owning pointer. Contains the file with all the asset data (name, local ID, etc.) */
struct AssetHandle *asset_handle;
/* Always freed. */
const char *path;
int id_type;
int import_type; /* eFileAssetImportType */
} wmDragAsset;

View File

@ -42,6 +42,8 @@
#include "BKE_idtype.h"
#include "BKE_lib_id.h"
#include "ED_asset.h"
#include "GPU_shader.h"
#include "GPU_state.h"
#include "GPU_viewport.h"
@ -196,6 +198,7 @@ void WM_drag_data_free(int dragtype, void *poin)
/* Not too nice, could become a callback. */
if (dragtype == WM_DRAG_ASSET) {
wmDragAsset *asset_drag = poin;
MEM_SAFE_FREE(asset_drag->asset_handle);
MEM_freeN((void *)asset_drag->path);
}
MEM_freeN(poin);
@ -373,18 +376,20 @@ wmDragAsset *WM_drag_get_asset_data(const wmDrag *drag, int idcode)
}
wmDragAsset *asset_drag = drag->poin;
return (ELEM(idcode, 0, asset_drag->id_type)) ? asset_drag : NULL;
ID_Type idtype = ED_asset_handle_get_id_type(asset_drag->asset_handle);
return (ELEM(idcode, 0, (int)idtype)) ? asset_drag : NULL;
}
static ID *wm_drag_asset_id_import(wmDragAsset *asset_drag)
{
const char *name = ED_asset_handle_get_name(asset_drag->asset_handle);
ID_Type idtype = ED_asset_handle_get_id_type(asset_drag->asset_handle);
switch ((eFileAssetImportType)asset_drag->import_type) {
case FILE_ASSET_IMPORT_LINK:
return WM_file_link_datablock(
G_MAIN, NULL, NULL, NULL, asset_drag->path, asset_drag->id_type, asset_drag->name);
return WM_file_link_datablock(G_MAIN, NULL, NULL, NULL, asset_drag->path, idtype, name);
case FILE_ASSET_IMPORT_APPEND:
return WM_file_append_datablock(
G_MAIN, NULL, NULL, NULL, asset_drag->path, asset_drag->id_type, asset_drag->name);
return WM_file_append_datablock(G_MAIN, NULL, NULL, NULL, asset_drag->path, idtype, name);
}
BLI_assert_unreachable();
@ -444,7 +449,8 @@ void WM_drag_free_imported_drag_ID(struct Main *bmain, wmDrag *drag, wmDropBox *
return;
}
ID *id = BKE_libblock_find_name(bmain, asset_drag->id_type, name);
ID_Type idtype = ED_asset_handle_get_id_type(asset_drag->asset_handle);
ID *id = BKE_libblock_find_name(bmain, idtype, name);
if (id) {
BKE_id_delete(bmain, id);
}
@ -478,7 +484,7 @@ static const char *wm_drag_name(wmDrag *drag)
}
case WM_DRAG_ASSET: {
const wmDragAsset *asset_drag = WM_drag_get_asset_data(drag, 0);
return asset_drag->name;
return ED_asset_handle_get_name(asset_drag->asset_handle);
}
case WM_DRAG_PATH:
case WM_DRAG_NAME: