[Bf-blender-cvs] [cb0b017d8f5] master: Cleanup: Store asset-handle in drag data

Julian Eisel noreply at git.blender.org
Tue Jul 20 21:50:35 CEST 2021


Commit: cb0b017d8f5178b58a59c66e9588199f2864608b
Author: Julian Eisel
Date:   Tue Jul 20 21:26:55 2021 +0200
Branches: master
https://developer.blender.org/rBcb0b017d8f5178b58a59c66e9588199f2864608b

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.

===================================================================

M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface.c
M	source/blender/editors/interface/interface_template_asset_view.cc
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_view3d/space_view3d.c
M	source/blender/windowmanager/WM_types.h
M	source/blender/windowmanager/intern/wm_dragdrop.c

===================================================================

diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index a25aac5803c..a6e465d04e8 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -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,
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c
index ddde4f5a9dc..72e379e9b0a 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -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;
diff --git a/source/blender/editors/interface/interface_template_asset_view.cc b/source/blender/editors/interface/interface_template_asset_view.cc
index 6fa5300401a..de3b49eec07 100644
--- a/source/blender/editors/interface/interface_template_asset_view.cc
+++ b/source/blender/editors/interface/interface_template_asset_view.cc
@@ -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,
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index a314a85491d..37a56816677 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -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,
diff --git a/source/blender/editors/space_view3d/space_view3d.c b/source/blender/editors/space_view3d/space_view3d.c
index 54f10e259f9..5baa12f7367 100644
--- a/source/blender/editors/space_view3d/space_view3d.c
+++ b/source/blender/editors/space_view3d/space_view3d.c
@@ -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;
diff --git a/source/blender/windowmanager/WM_types.h b/source/blender/windowmanager/WM_types.h
index 2b48a5f6648..cc8fb307c92 100644
--- a/source/blender/windowmanager/WM_types.h
+++ b/source/blender/windowmanager/WM_types.h
@@ -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;
 
diff --git a/source/blender/windowmanager/intern/wm_dragdrop.c b/source/blender/windowmanager/intern/wm_dragdrop.c
index da40040ce56..319e83f667f 100644
--- a/source/blender/windowmanager/intern/wm_dragdrop.c
+++ b/source/blender/windowmanager/intern/wm_dragdrop.c
@@ -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:



More information about the Bf-blender-cvs mailing list