[Bf-blender-cvs] [f3002a863ac] asset-browser: Asset Browser: Support dragging assets and appending on drop

Julian Eisel noreply at git.blender.org
Thu Dec 3 01:58:54 CET 2020


Commit: f3002a863ac7ae0efc96924974bf6739d5db838e
Author: Julian Eisel
Date:   Wed Dec 2 20:20:06 2020 +0100
Branches: asset-browser
https://developer.blender.org/rBf3002a863ac7ae0efc96924974bf6739d5db838e

Asset Browser: Support dragging assets and appending on drop

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 ID is replaced with one that
returns the local ID, or, in case of an external asset, appends the ID first.

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.

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

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

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 8fdd78fa6ad..77db90ef11d 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -726,6 +726,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 349d1f07832..fa91fd6946c 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 3c028977a36..19cce867074 100644
--- a/source/blender/editors/interface/interface_handlers.c
+++ b/source/blender/editors/interface/interface_handlers.c
@@ -1983,6 +1983,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,
@@ -2255,10 +2257,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 76c25799e2c..21d23dd1ddd 100644
--- a/source/blender/editors/space_node/space_node.c
+++ b/source/blender/editors/space_node/space_node.c
@@ -637,7 +637,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),
@@ -645,19 +645,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 865e06bf0b6..61c204a963b 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;
@@ -1459,14 +1459,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