[Bf-blender-cvs] [b5d778a7d40] master: Asset System: Support dragging assets and appending on drop

Julian Eisel noreply at git.blender.org
Tue Dec 15 17:04:34 CET 2020


Commit: b5d778a7d4072bfb091198a2094890157a6d017b
Author: Julian Eisel
Date:   Mon Dec 14 13:31:55 2020 +0100
Branches: master
https://developer.blender.org/rBb5d778a7d4072bfb091198a2094890157a6d017b

Asset System: Support dragging assets and appending on drop

For the Asset Browser, it needs to be possible to drag assets into various
editors, which may not come from the current .blend file. In other words, the
dragging needs to work with just the asset metadata, without direct access to
the data-block itself.

Idea is simple: When dragging an asset, store the source file-path and
data-block name and when dropping, append the data-block. It uses existing drop
operators, but the function to get the dropped data-block is replaced with one
that returns the local data-block, or, in case of an external asset, appends
the data-block first.

The drop operators need to be adjusted to use this new function that respects
assets. With this patch it only works for dragging assets into the 3D view.

Note that I expect this to be a short-lived change. A refactor like D4071 is
needed to make the drag & drop system more future proof for assets and other
use cases.

Part of the first Asset Browser milestone. Check the #asset_browser_milestone_1
project milestone on developer.blender.org.

Differential Revision: https://developer.blender.org/D9721

Reviewed by: Bastien Montagne, Brecht Van Lommel

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

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_console/space_console.c
M	source/blender/editors/space_node/space_node.c
M	source/blender/editors/space_outliner/outliner_dragdrop.c
M	source/blender/editors/space_text/space_text.c
M	source/blender/editors/space_view3d/space_view3d.c
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 352c58032f5..83e94664c0b 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -727,6 +727,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 254d0909367..c5c2f0e55c4 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -3352,7 +3352,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);
 
@@ -6098,17 +6098,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;
@@ -6118,7 +6143,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;
@@ -6131,7 +6156,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;
@@ -6149,7 +6174,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 bcb79743b12..f39ad4c4ed5 100644
--- a/source/blender/editors/interface/interface_handlers.c
+++ b/source/blender/editors/interface/interface_handlers.c
@@ -1987,6 +1987,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,
@@ -2259,10 +2261,11 @@ 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)) {
-        ID *id = WM_drag_ID(wmd, 0);
+        ID *id = WM_drag_get_local_ID(wmd, 0);
 
         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
 
diff --git a/source/blender/editors/space_console/space_console.c b/source/blender/editors/space_console/space_console.c
index a54faa41122..9b8e9e0e871 100644
--- a/source/blender/editors/space_console/space_console.c
+++ b/source/blender/editors/space_console/space_console.c
@@ -164,12 +164,12 @@ static bool id_drop_poll(bContext *UNUSED(C),
                          const wmEvent *UNUSED(event),
                          const char **UNUSED(tooltip))
 {
-  return WM_drag_ID(drag, 0) != NULL;
+  return WM_drag_get_local_ID(drag, 0) != NULL;
 }
 
 static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
 {
-  ID *id = WM_drag_ID(drag, 0);
+  ID *id = WM_drag_get_local_ID(drag, 0);
 
   /* copy drag path to properties */
   char *text = RNA_path_full_ID_py(G_MAIN, id);
diff --git a/source/blender/editors/space_node/space_node.c b/source/blender/editors/space_node/space_node.c
index afc1a963f4f..ad7632377a3 100644
--- a/source/blender/editors/space_node/space_node.c
+++ b/source/blender/editors/space_node/space_node.c
@@ -645,7 +645,7 @@ static bool node_ima_drop_poll(bContext *UNUSED(C),
     /* rule might not work? */
     return (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE));
   }
-  return WM_drag_ID(drag, ID_IM) != NULL;
+  return WM_drag_get_local_ID(drag, ID_IM) != NULL;
 }
 
 static bool node_mask_drop_poll(bContext *UNUSED(C),
@@ -653,19 +653,19 @@ static bool node_mask_drop_poll(bContext *UNUSED(C),
                                 const wmEvent *UNUSED(event),
                                 const char **UNUSED(r_tooltip))
 {
-  return WM_drag_ID(drag, ID_MSK) != NULL;
+  return WM_drag_get_local_ID(drag, ID_MSK) != NULL;
 }
 
 static void node_id_drop_copy(wmDrag *drag, wmDropBox *drop)
 {
-  ID *id = WM_drag_ID(drag, 0);
+  ID *id = WM_drag_get_local_ID(drag, 0);
 
   RNA_string_set(drop->ptr, "name", id->name + 2);
 }
 
 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
 {
-  ID *id = WM_drag_ID(drag, 0);
+  ID *id = WM_drag_get_local_ID(drag, 0);
 
   if (id) {
     RNA_string_set(drop->ptr, "name", id->name + 2);
diff --git a/source/blender/editors/space_outliner/outliner_dragdrop.c b/source/blender/editors/space_outliner/outliner_dragdrop.c
index dcb8ef9c954..152bbc96281 100644
--- a/source/blender/editors/space_outliner/outliner_dragdrop.c
+++ b/source/blender/editors/space_outliner/outliner_dragdrop.c
@@ -333,7 +333,7 @@ static bool parent_drop_poll(bContext *C,
     ED_region_tag_redraw_no_rebuild(CTX_wm_region(C));
   }
 
-  Object *potential_child = (Object *)WM_drag_ID(drag, ID_OB);
+  Object *potential_child = (Object *)WM_drag_get_local_ID(drag, ID_OB);
   if (!potential_child) {
     return false;
   }
@@ -421,7 +421,7 @@ static int parent_drop_invoke(bContext *C, wmOperator *op, const wmEvent *event)
   }
 
   Object *par = (Object *)tselem->id;
-  Object *ob = (Object *)WM_drag_ID_from_event(event, ID_OB);
+  Object *ob = (Object *)WM_drag_get_local_ID_from_event(event, ID_OB);
 
   if (ELEM(NULL, ob, par)) {
     return OPERATOR_CANCELLED;
@@ -473,7 +473,7 @@ static bool parent_clear_poll(bContext *C,
     }
   }
 
-  Object *ob = (Object *)WM_drag_ID(drag, ID_OB);
+  Object *ob = (Object *)WM_drag_get_local_ID(drag, ID_OB);
   if (!ob) {
     return false;
   }
@@ -552,7 +552,7 @@ static bool scene_drop_poll(bContext *C,
                             const char **UNUSED(r_tooltip))
 {
   /* Ensure item under cursor is valid drop target */
-  Object *ob = (Object *)WM_drag_ID(drag, ID_OB);
+  Object *ob = (Object *)WM_drag_get_local_ID(drag, ID_OB);
   return (ob && (outliner_ID_drop_find(C, event, ID_SCE) != NULL));
 }
 
@@ -560,7 +560,7 @@ static int scene_drop_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent
 {
   Main *bmain = CTX_data_main(C);
   Scene *scene = (Scene *)outliner_ID_drop_find(C, event, ID_SCE);
-  Object *ob = (Object *)WM_drag_ID_from_event(event, ID_OB);
+  Object *ob = (Object *)WM_drag_get_local_ID_from_event(event, ID_OB);
 
   if (ELEM(NULL, ob, scene) || ID_IS_LINKED(scene)) {
     return OPERATOR_CANCELLED;
@@ -620,7 +620,7 @@ static bool material_drop_poll(bContext *C,
                                const char **UNUSED(r_tooltip))
 {
   /* Ensure item under cursor is valid drop target */
-  Material *ma = (Material *)WM_drag_ID(drag, ID_MA);
+  Material *ma = (Material *)WM_drag_get_local_ID(drag, ID_MA);
   return (ma && (outliner_ID_drop_find(C, event, ID_OB) != NULL));
 }
 
@@ -628,7 +628,7 @@ static int material_drop_invoke(bContext *C, wmOperator *UNUSED(op), const wmEve
 {
   Main *bmain = CTX_data_main(C);
   Object *ob = (Object *)outliner_ID_drop_find(C, event, ID_OB);
-  Material *ma = (Material *)WM_drag_ID_from_event(event, ID_MA);
+  Material *ma = (Material *)WM_drag_get_local_ID_from_event(event, ID_MA);
 
   if (ELEM(NULL, ob, ma)) {
     return OPERATOR_CANCELLED;
@@ -1461,14 +1461,14 @@ static int outliner_item_drag_drop_invoke(bContext *C,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list