Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Eisel <julian@blender.org>2020-12-14 15:31:55 +0300
committerJulian Eisel <julian@blender.org>2020-12-15 19:03:48 +0300
commitb5d778a7d4072bfb091198a2094890157a6d017b (patch)
tree0a3a1ef47dc6ec1b0407ceec070b9a9bbd84af01 /source/blender/windowmanager
parent9363132c8601ebca6d89168e09bb10f81d6cb03a (diff)
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
Diffstat (limited to 'source/blender/windowmanager')
-rw-r--r--source/blender/windowmanager/WM_api.h10
-rw-r--r--source/blender/windowmanager/WM_types.h20
-rw-r--r--source/blender/windowmanager/intern/wm_dragdrop.c102
3 files changed, 105 insertions, 27 deletions
diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h
index da2115e12fb..1f205a71338 100644
--- a/source/blender/windowmanager/WM_api.h
+++ b/source/blender/windowmanager/WM_api.h
@@ -671,6 +671,7 @@ struct wmDrag *WM_event_start_drag(
struct bContext *C, int icon, int type, void *poin, double value, unsigned int flags);
void WM_event_drag_image(struct wmDrag *, struct ImBuf *, float scale, int sx, int sy);
void WM_drag_free(struct wmDrag *drag);
+void WM_drag_data_free(int dragtype, void *poin);
void WM_drag_free_list(struct ListBase *lb);
struct wmDropBox *WM_dropbox_add(
@@ -681,9 +682,12 @@ struct wmDropBox *WM_dropbox_add(
ListBase *WM_dropboxmap_find(const char *idname, int spaceid, int regionid);
/* ID drag and drop */
-void WM_drag_add_ID(struct wmDrag *drag, struct ID *id, struct ID *from_parent);
-struct ID *WM_drag_ID(const struct wmDrag *drag, short idcode);
-struct ID *WM_drag_ID_from_event(const struct wmEvent *event, short idcode);
+void WM_drag_add_local_ID(struct wmDrag *drag, struct ID *id, struct ID *from_parent);
+struct ID *WM_drag_get_local_ID(const struct wmDrag *drag, short idcode);
+struct ID *WM_drag_get_local_ID_from_event(const struct wmEvent *event, short idcode);
+
+struct wmDragAsset *WM_drag_get_asset_data(const struct wmDrag *drag, int idcode);
+struct ID *WM_drag_get_local_ID_or_import_from_asset(const struct wmDrag *drag, int idcode);
/* Set OpenGL viewport and scissor */
void wmViewport(const struct rcti *winrct);
diff --git a/source/blender/windowmanager/WM_types.h b/source/blender/windowmanager/WM_types.h
index 77258ef5224..0ddd0f20c7a 100644
--- a/source/blender/windowmanager/WM_types.h
+++ b/source/blender/windowmanager/WM_types.h
@@ -836,12 +836,13 @@ typedef void (*wmPaintCursorDraw)(struct bContext *C, int, int, void *customdata
/* *************** Drag and drop *************** */
#define WM_DRAG_ID 0
-#define WM_DRAG_RNA 1
-#define WM_DRAG_PATH 2
-#define WM_DRAG_NAME 3
-#define WM_DRAG_VALUE 4
-#define WM_DRAG_COLOR 5
-#define WM_DRAG_DATASTACK 6
+#define WM_DRAG_ASSET 1
+#define WM_DRAG_RNA 2
+#define WM_DRAG_PATH 3
+#define WM_DRAG_NAME 4
+#define WM_DRAG_VALUE 5
+#define WM_DRAG_COLOR 6
+#define WM_DRAG_DATASTACK 7
typedef enum wmDragFlags {
WM_DRAG_NOP = 0,
@@ -856,6 +857,13 @@ typedef struct wmDragID {
struct ID *from_parent;
} wmDragID;
+typedef struct wmDragAsset {
+ char name[64]; /* MAX_NAME */
+ /* Always freed. */
+ const char *path;
+ int id_type;
+} wmDragAsset;
+
typedef struct wmDrag {
struct wmDrag *next, *prev;
diff --git a/source/blender/windowmanager/intern/wm_dragdrop.c b/source/blender/windowmanager/intern/wm_dragdrop.c
index 373360c7b92..fe2e2d92127 100644
--- a/source/blender/windowmanager/intern/wm_dragdrop.c
+++ b/source/blender/windowmanager/intern/wm_dragdrop.c
@@ -37,6 +37,7 @@
#include "BIF_glutil.h"
#include "BKE_context.h"
+#include "BKE_global.h"
#include "BKE_idtype.h"
#include "GPU_shader.h"
@@ -146,16 +147,23 @@ wmDrag *WM_event_start_drag(
drag->flags = flags;
drag->icon = icon;
drag->type = type;
- if (type == WM_DRAG_PATH) {
- BLI_strncpy(drag->path, poin, FILE_MAX);
- }
- else if (type == WM_DRAG_ID) {
- if (poin) {
- WM_drag_add_ID(drag, poin, NULL);
- }
- }
- else {
- drag->poin = poin;
+ switch (type) {
+ case WM_DRAG_PATH:
+ BLI_strncpy(drag->path, poin, FILE_MAX);
+ break;
+ case WM_DRAG_ID:
+ if (poin) {
+ WM_drag_add_local_ID(drag, poin, NULL);
+ }
+ break;
+ case WM_DRAG_ASSET:
+ /* Move ownership of poin to wmDrag. */
+ drag->poin = poin;
+ drag->flags |= WM_DRAG_FREE_DATA;
+ break;
+ default:
+ drag->poin = poin;
+ break;
}
drag->value = value;
@@ -170,12 +178,26 @@ void WM_event_drag_image(wmDrag *drag, ImBuf *imb, float scale, int sx, int sy)
drag->sy = sy;
}
-void WM_drag_free(wmDrag *drag)
+void WM_drag_data_free(int dragtype, void *poin)
{
- if ((drag->flags & WM_DRAG_FREE_DATA) && drag->poin) {
- MEM_freeN(drag->poin);
+ /* Don't require all the callers to have a NULL-check, just allow passing NULL. */
+ if (!poin) {
+ return;
}
+ /* Not too nice, could become a callback. */
+ if (dragtype == WM_DRAG_ASSET) {
+ wmDragAsset *asset_drag = poin;
+ MEM_freeN((void *)asset_drag->path);
+ }
+ MEM_freeN(poin);
+}
+
+void WM_drag_free(wmDrag *drag)
+{
+ if (drag->flags & WM_DRAG_FREE_DATA) {
+ WM_drag_data_free(drag->type, drag->poin);
+ }
BLI_freelistN(&drag->ids);
MEM_freeN(drag);
}
@@ -279,7 +301,7 @@ void wm_drags_check_ops(bContext *C, const wmEvent *event)
/* ************** IDs ***************** */
-void WM_drag_add_ID(wmDrag *drag, ID *id, ID *from_parent)
+void WM_drag_add_local_ID(wmDrag *drag, ID *id, ID *from_parent)
{
/* Don't drag the same ID twice. */
LISTBASE_FOREACH (wmDragID *, drag_id, &drag->ids) {
@@ -302,7 +324,7 @@ void WM_drag_add_ID(wmDrag *drag, ID *id, ID *from_parent)
BLI_addtail(&drag->ids, drag_id);
}
-ID *WM_drag_ID(const wmDrag *drag, short idcode)
+ID *WM_drag_get_local_ID(const wmDrag *drag, short idcode)
{
if (drag->type != WM_DRAG_ID) {
return NULL;
@@ -317,14 +339,54 @@ ID *WM_drag_ID(const wmDrag *drag, short idcode)
return (idcode == 0 || GS(id->name) == idcode) ? id : NULL;
}
-ID *WM_drag_ID_from_event(const wmEvent *event, short idcode)
+ID *WM_drag_get_local_ID_from_event(const wmEvent *event, short idcode)
{
if (event->custom != EVT_DATA_DRAGDROP) {
return NULL;
}
ListBase *lb = event->customdata;
- return WM_drag_ID(lb->first, idcode);
+ return WM_drag_get_local_ID(lb->first, idcode);
+}
+
+wmDragAsset *WM_drag_get_asset_data(const wmDrag *drag, int idcode)
+{
+ if (drag->type != WM_DRAG_ASSET) {
+ return NULL;
+ }
+
+ wmDragAsset *asset_drag = drag->poin;
+ return (idcode == 0 || asset_drag->id_type == idcode) ? asset_drag : NULL;
+}
+
+static ID *wm_drag_asset_id_import(wmDragAsset *asset_drag)
+{
+ /* Append only for now, wmDragAsset could have a `link` bool. */
+ return WM_file_append_datablock(
+ G_MAIN, NULL, NULL, NULL, asset_drag->path, asset_drag->id_type, asset_drag->name);
+}
+
+/**
+ * When dragging a local ID, return that. Otherwise, if dragging an asset-handle, link or append
+ * that depending on what was chosen by the drag-box (currently append only in fact).
+ */
+ID *WM_drag_get_local_ID_or_import_from_asset(const wmDrag *drag, int idcode)
+{
+ if (!ELEM(drag->type, WM_DRAG_ASSET, WM_DRAG_ID)) {
+ return NULL;
+ }
+
+ if (drag->type == WM_DRAG_ID) {
+ return WM_drag_get_local_ID(drag, idcode);
+ }
+
+ wmDragAsset *asset_drag = WM_drag_get_asset_data(drag, idcode);
+ if (!asset_drag) {
+ return NULL;
+ }
+
+ /* Link/append the asset. */
+ return wm_drag_asset_id_import(asset_drag);
}
/* ************** draw ***************** */
@@ -342,7 +404,7 @@ static const char *wm_drag_name(wmDrag *drag)
{
switch (drag->type) {
case WM_DRAG_ID: {
- ID *id = WM_drag_ID(drag, 0);
+ ID *id = WM_drag_get_local_ID(drag, 0);
bool single = (BLI_listbase_count_at_most(&drag->ids, 2) == 1);
if (single) {
@@ -353,6 +415,10 @@ static const char *wm_drag_name(wmDrag *drag)
}
break;
}
+ case WM_DRAG_ASSET: {
+ const wmDragAsset *asset_drag = WM_drag_get_asset_data(drag, 0);
+ return asset_drag->name;
+ }
case WM_DRAG_PATH:
case WM_DRAG_NAME:
return drag->path;