Commit: 897f35cd18debd2db86bf058c0c97cfc2765725a
Author: Julian Eisel
Date:   Fri Aug 28 15:49:31 2020 +0200
Branches: asset-browser
https://developer.blender.org/rB897f35cd18debd2db86bf058c0c97cfc2765725a

Support dragging assets from Asset Browser into viewport

This should work for objects, groups, materials, images. It can be added
for more data-block types.

The Asset Browser UI code basically forwards some asset information
(like the path) to the general UI code. This can then append the asset's
data-block once dropping it into the viewport.

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

M       source/blender/editors/include/UI_interface.h
M       source/blender/editors/interface/interface.c
M       source/blender/editors/interface/interface_handlers.c
M       source/blender/editors/space_file/file_draw.c
M       source/blender/editors/space_file/filelist.c
M       source/blender/editors/space_view3d/space_view3d.c
M       source/blender/makesdna/DNA_space_types.h
M       source/blender/windowmanager/WM_api.h
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 1fa9ed0b2c0..df1137bc6fe 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -715,6 +715,13 @@ void UI_block_translate(uiBlock *block, int x, int y);
 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 char *path,
+                           int id_type,
+                           int icon,
+                           struct ImBuf *imb,
+                           float scale);
 void UI_but_drag_set_rna(uiBut *but, struct PointerRNA *ptr);
 void UI_but_drag_set_path(uiBut *but, const char *path, const bool use_free);
 void UI_but_drag_set_name(uiBut *but, const char *name);
diff --git a/source/blender/editors/interface/interface.c 
b/source/blender/editors/interface/interface.c
index f076ed083bb..0a9dadc87b4 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -3303,7 +3303,7 @@ static void ui_but_free(const bContext *C, uiBut *but)
   }
 
   if (but->dragpoin && (but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
-    MEM_freeN(but->dragpoin);
+    WM_drag_data_free(but->dragtype, but->dragpoin);
   }
   ui_but_extra_operator_icons_free(but);
 
@@ -6048,17 +6048,42 @@ void UI_but_drag_set_id(uiBut *but, ID *id)
 {
   but->dragtype = WM_DRAG_ID;
   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
-    MEM_SAFE_FREE(but->dragpoin);
+    WM_drag_data_free(but->dragtype, but->dragpoin);
     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
   }
   but->dragpoin = (void *)id;
 }
 
+void UI_but_drag_set_asset(uiBut *but,
+                           const char *name,
+                           const char *path,
+                           int id_type,
+                           int icon,
+                           struct ImBuf *imb,
+                           float scale)
+{
+  wmDragAsset *asset_drag = MEM_mallocN(sizeof(*asset_drag), "wmDragAsset");
+
+  BLI_strncpy(asset_drag->name, name, sizeof(asset_drag->name));
+  asset_drag->path = path;
+  asset_drag->id_type = id_type;
+
+  but->dragtype = WM_DRAG_ASSET;
+  ui_def_but_icon(but, icon, 0); /* no flag UI_HAS_ICON, so icon doesn't draw 
in button */
+  if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
+    WM_drag_data_free(but->dragtype, but->dragpoin);
+  }
+  but->dragpoin = asset_drag;
+  but->dragflag |= UI_BUT_DRAGPOIN_FREE;
+  but->imb = imb;
+  but->imb_scale = scale;
+}
+
 void UI_but_drag_set_rna(uiBut *but, PointerRNA *ptr)
 {
   but->dragtype = WM_DRAG_RNA;
   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
-    MEM_SAFE_FREE(but->dragpoin);
+    WM_drag_data_free(but->dragtype, but->dragpoin);
     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
   }
   but->dragpoin = (void *)ptr;
@@ -6068,7 +6093,7 @@ void UI_but_drag_set_path(uiBut *but, const char *path, 
const bool use_free)
 {
   but->dragtype = WM_DRAG_PATH;
   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
-    MEM_SAFE_FREE(but->dragpoin);
+    WM_drag_data_free(but->dragtype, but->dragpoin);
     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
   }
   but->dragpoin = (void *)path;
@@ -6081,7 +6106,7 @@ void UI_but_drag_set_name(uiBut *but, const char *name)
 {
   but->dragtype = WM_DRAG_NAME;
   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
-    MEM_SAFE_FREE(but->dragpoin);
+    WM_drag_data_free(but->dragtype, but->dragpoin);
     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
   }
   but->dragpoin = (void *)name;
@@ -6099,7 +6124,7 @@ void UI_but_drag_set_image(
   but->dragtype = WM_DRAG_PATH;
   ui_def_but_icon(but, icon, 0); /* no flag UI_HAS_ICON, so icon doesn't draw 
in button */
   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
-    MEM_SAFE_FREE(but->dragpoin);
+    WM_drag_data_free(but->dragtype, but->dragpoin);
     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
   }
   but->dragpoin = (void *)path;
diff --git a/source/blender/editors/interface/interface_handlers.c 
b/source/blender/editors/interface/interface_handlers.c
index bf88b3c0318..0f8db2a60b6 100644
--- a/source/blender/editors/interface/interface_handlers.c
+++ b/source/blender/editors/interface/interface_handlers.c
@@ -1980,6 +1980,8 @@ static bool ui_but_drag_init(bContext *C,
     else {
       wmDrag *drag = WM_event_start_drag(
           C, but->icon, but->dragtype, but->dragpoin, ui_but_value_get(but), 
WM_DRAG_NOP);
+      /* wmDrag has ownership over dragpoin now, stop messing with it. */
+      but->dragpoin = NULL;
 
       if (but->imb) {
         WM_event_drag_image(drag,
@@ -2252,6 +2254,7 @@ static void ui_but_drop(bContext *C, const wmEvent 
*event, uiBut *but, uiHandleB
   ListBase *drags = event->customdata; /* drop event type has listbase 
customdata by default */
 
   LISTBASE_FOREACH (wmDrag *, wmd, drags) {
+    /* TODO asset dropping. */
     if (wmd->type == WM_DRAG_ID) {
       /* align these types with UI_but_active_drop_name */
       if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
diff --git a/source/blender/editors/space_file/file_draw.c 
b/source/blender/editors/space_file/file_draw.c
index 7039eba7db1..fc10ec020f0 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -134,6 +134,8 @@ static void draw_tile(int sx, int sy, int width, int 
height, int colorid, int sh
 }
 
 static void file_draw_icon(uiBlock *block,
+                           const FileDirEntry *file,
+                           const char *rootpath,
                            const char *path,
                            int sx,
                            int sy,
@@ -157,8 +159,16 @@ static void file_draw_icon(uiBlock *block,
   UI_but_func_tooltip_set(but, file_draw_tooltip_func, BLI_strdup(path));
 
   if (drag) {
-    /* path is no more static, cannot give it directly to but... */
-    UI_but_drag_set_path(but, BLI_strdup(path), true);
+    if (file->typeflag & FILE_TYPE_ASSET) {
+      char *rootpath_cpy = BLI_strdup(rootpath);
+      BLI_path_slash_rstrip(rootpath_cpy);
+      UI_but_drag_set_asset(
+          but, file->name, rootpath_cpy, file->blentype, icon, file->image, 
UI_DPI_FAC);
+    }
+    else {
+      /* path is no more static, cannot give it directly to but... */
+      UI_but_drag_set_path(but, BLI_strdup(path), true);
+    }
   }
 }
 
@@ -210,6 +220,8 @@ void file_calc_previews(const bContext *C, ARegion *region)
 }
 
 static void file_draw_preview(uiBlock *block,
+                              const FileDirEntry *file,
+                              const char *rootpath,
                               const char *path,
                               int sx,
                               int sy,
@@ -218,7 +230,6 @@ static void file_draw_preview(uiBlock *block,
                               const int icon,
                               FileLayout *layout,
                               const bool is_icon,
-                              const int typeflags,
                               const bool drag,
                               const bool dimmed,
                               const bool is_link)
@@ -232,7 +243,7 @@ static void file_draw_preview(uiBlock *block,
   float scale;
   int ex, ey;
   bool show_outline = !is_icon &&
-                      (typeflags & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE | 
FILE_TYPE_BLENDER));
+                      (file->typeflag & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE | 
FILE_TYPE_BLENDER));
 
   BLI_assert(imb != NULL);
 
@@ -273,14 +284,14 @@ static void file_draw_preview(uiBlock *block,
 
   float col[4] = {1.0f, 1.0f, 1.0f, 1.0f};
   if (is_icon) {
-    if (typeflags & FILE_TYPE_DIR) {
+    if (file->typeflag & FILE_TYPE_DIR) {
       UI_GetThemeColor4fv(TH_ICON_FOLDER, col);
     }
     else {
       UI_GetThemeColor4fv(TH_TEXT, col);
     }
   }
-  else if (typeflags & FILE_TYPE_FTFONT) {
+  else if (file->typeflag & FILE_TYPE_FTFONT) {
     UI_GetThemeColor4fv(TH_TEXT, col);
   }
 
@@ -288,7 +299,7 @@ static void file_draw_preview(uiBlock *block,
     col[3] *= 0.3f;
   }
 
-  if (!is_icon && typeflags & FILE_TYPE_BLENDERLIB) {
+  if (!is_icon && file->typeflag & FILE_TYPE_BLENDERLIB) {
     /* Datablock preview images use premultiplied alpha. */
     GPU_blend(GPU_BLEND_ALPHA_PREMULT);
   }
@@ -324,7 +335,7 @@ static void file_draw_preview(uiBlock *block,
       icon_color[2] = 255;
     }
     icon_x = xco + (ex / 2.0f) - (icon_size / 2.0f);
-    icon_y = yco + (ey / 2.0f) - (icon_size * ((typeflags & FILE_TYPE_DIR) ? 
0.78f : 0.75f));
+    icon_y = yco + (ey / 2.0f) - (icon_size * ((file->typeflag & 
FILE_TYPE_DIR) ? 0.78f : 0.75f));
     UI_icon_draw_ex(
         icon_x, icon_y, icon, icon_aspect / U.dpi_fac, icon_opacity, 0.0f, 
icon_color, false);
   }
@@ -346,13 +357,13 @@ static void file_draw_preview(uiBlock *block,
       /* Link to folder or non-previewed file. */
       uchar icon_color[4];
       UI_GetThemeColor4ubv(TH_BACK, icon_color);
-      icon_x = xco + ((typeflags & FILE_TYPE_DIR) ? 0.14f : 0.23f) * scaledx;
-      icon_y = yco + ((typeflags & FILE_TYPE_DIR) ? 0.24f : 0.14f) * scaledy;
+      icon_x = xco + ((file->typeflag & FILE_TYPE_DIR) ? 0.14f : 0.23f) * 
scaledx;
+      icon_y = yco + ((file->typeflag & FILE_TYPE_DIR) ? 0.24f : 0.14f) * 
scaledy;
       UI_icon_draw_ex(
           icon_x, icon_y, arrow, icon_aspect / U.dpi_fac * 1.8, 0.3f, 0.0f, 
icon_color, false);
     }
   }
-  else if (icon && !is_icon && !(typeflags & FILE_TYPE_FTFONT)) {
+  else if (icon && !is_icon && !(file->typeflag & FILE_TYPE_FTFONT)) {
     /* Smaller, fainter icon at bottom-left for preview image thumbnail, but 
not for fonts. */
     float icon_x, icon_y;
     const uchar dark[4] = {0, 0, 0, 255};
@@ -386,7 +397,14 @@ static void file_draw_preview(uiBlock *block,
   /* dragregion */
   if (drag) {
     /* path is no more static, cannot give it directly to but... */
-    UI_but_drag_set_image(but, BLI_strdup(path), icon, imb, scale, true);
+    if (file->typeflag & FILE_TYPE_ASSET) {
+      char *rootpath_cpy = BLI_strdup(rootpath);
+      BLI_path_slash_rstrip(rootpath_cpy);
+      UI_but_drag_s

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to