[Bf-blender-cvs] [86c363a0270] blender2.8: WM: pass on wmDrag to drop operators, so they can get the data directly.

Brecht Van Lommel noreply at git.blender.org
Fri Aug 10 17:57:38 CEST 2018


Commit: 86c363a02706b1a5d3cb18d17b4b37bd78461ded
Author: Brecht Van Lommel
Date:   Tue Aug 7 10:57:09 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB86c363a02706b1a5d3cb18d17b4b37bd78461ded

WM: pass on wmDrag to drop operators, so they can get the data directly.

Currently drop operators work mostly by specifying the name of the datablock.
However there can be datablocks with the same name in different libraries, so
this gives wrong results in some cases.

Currently only outliner drop operators have been updated to use this mechanism.

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

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/intern/wm_dragdrop.c
M	source/blender/windowmanager/intern/wm_event_system.c

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

diff --git a/source/blender/editors/space_console/space_console.c b/source/blender/editors/space_console/space_console.c
index ce87ad3b177..3429d726349 100644
--- a/source/blender/editors/space_console/space_console.c
+++ b/source/blender/editors/space_console/space_console.c
@@ -172,29 +172,22 @@ static void console_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
 
 static bool id_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
 {
-//	SpaceConsole *sc = CTX_wm_space_console(C);
-	if (drag->type == WM_DRAG_ID)
-		return 1;
-	return 0;
+	return WM_drag_ID(drag, 0) != NULL;
 }
 
 static void id_drop_copy(wmDrag *drag, wmDropBox *drop)
 {
-	char *text;
-	ID *id = drag->poin;
+	ID *id = WM_drag_ID(drag, 0);
 
 	/* copy drag path to properties */
-	text = RNA_path_full_ID_py(id);
+	char *text = RNA_path_full_ID_py(id);
 	RNA_string_set(drop->ptr, "text", text);
 	MEM_freeN(text);
 }
 
 static bool path_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
 {
-	// SpaceConsole *sc = CTX_wm_space_console(C);
-	if (drag->type == WM_DRAG_PATH)
-		return 1;
-	return 0;
+	return (drag->type == WM_DRAG_PATH);
 }
 
 static void path_drop_copy(wmDrag *drag, wmDropBox *drop)
diff --git a/source/blender/editors/space_node/space_node.c b/source/blender/editors/space_node/space_node.c
index 74c036883e6..2b2e659d151 100644
--- a/source/blender/editors/space_node/space_node.c
+++ b/source/blender/editors/space_node/space_node.c
@@ -677,26 +677,17 @@ static void node_main_region_draw(const bContext *C, ARegion *ar)
 
 static bool node_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
 {
-	if (drag->type == WM_DRAG_ID) {
-		ID *id = drag->poin;
-		if (GS(id->name) == ID_IM)
-			return 1;
+	if (drag->type == WM_DRAG_PATH) {
+		return (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE));   /* rule might not work? */
 	}
-	else if (drag->type == WM_DRAG_PATH) {
-		if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE))   /* rule might not work? */
-			return 1;
+	else {
+		return WM_drag_ID(drag, ID_IM) != NULL;
 	}
-	return 0;
 }
 
 static bool node_mask_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
 {
-	if (drag->type == WM_DRAG_ID) {
-		ID *id = drag->poin;
-		if (GS(id->name) == ID_MSK)
-			return 1;
-	}
-	return 0;
+	return WM_drag_ID(drag, ID_MSK) != NULL;
 }
 
 static void node_id_drop_copy(wmDrag *drag, wmDropBox *drop)
diff --git a/source/blender/editors/space_outliner/outliner_dragdrop.c b/source/blender/editors/space_outliner/outliner_dragdrop.c
index c53a431a017..f9a4fda0c45 100644
--- a/source/blender/editors/space_outliner/outliner_dragdrop.c
+++ b/source/blender/editors/space_outliner/outliner_dragdrop.c
@@ -39,6 +39,7 @@
 #include "DNA_space_types.h"
 
 #include "BLI_listbase.h"
+#include "BLI_string.h"
 
 #include "BLT_translation.h"
 
@@ -105,64 +106,77 @@ static TreeElement *outliner_dropzone_find(const SpaceOops *soops, const float f
 	return NULL;
 }
 
-/* ******************** Parent Drop Operator *********************** */
-
-static bool parent_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event, const char **UNUSED(tooltip))
+static TreeElement *outliner_drop_find(bContext *C, const wmEvent *event)
 {
 	ARegion *ar = CTX_wm_region(C);
 	SpaceOops *soops = CTX_wm_space_outliner(C);
 	float fmval[2];
 	UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
 
-	if (drag->type == WM_DRAG_ID) {
-		ID *id = drag->poin;
-		if (GS(id->name) == ID_OB) {
-			/* Ensure item under cursor is valid drop target */
-			TreeElement *te = outliner_dropzone_find(soops, fmval, true);
-			TreeStoreElem *tselem = te ? TREESTORE(te) : NULL;
+	return outliner_dropzone_find(soops, fmval, true);
+}
 
-			if (!te) {
-				/* pass */
-			}
-			else if (te->idcode == ID_OB && tselem->type == 0) {
-				Scene *scene;
-				ID *te_id = tselem->id;
-
-				/* check if dropping self or parent */
-				if (te_id == id || (Object *)te_id == ((Object *)id)->parent)
-					return 0;
-
-				/* check that parent/child are both in the same scene */
-				scene = (Scene *)outliner_search_back(soops, te, ID_SCE);
-
-				/* currently outliner organized in a way that if there's no parent scene
-				 * element for object it means that all displayed objects belong to
-				 * active scene and parenting them is allowed (sergey)
-				 */
-				if (!scene) {
-					return 1;
-				}
-				else {
-					for (ViewLayer *view_layer = scene->view_layers.first;
-					     view_layer;
-					     view_layer = view_layer->next)
-					{
-						if (BKE_view_layer_base_find(view_layer, (Object *)id)) {
-							return 1;
-						}
-					}
-				}
-			}
-		}
+static ID *outliner_ID_drop_find(bContext *C, const wmEvent *event, short idcode)
+{
+	TreeElement *te = outliner_drop_find(C, event);
+	TreeStoreElem *tselem = (te) ? TREESTORE(te) : NULL;
+
+	if (te && te->idcode == idcode && tselem->type == 0) {
+		return tselem->id;
+	}
+	else {
+		return NULL;
 	}
-	return 0;
 }
 
-static void parent_drop_copy(wmDrag *drag, wmDropBox *drop)
+/* ******************** Parent Drop Operator *********************** */
+
+static bool parent_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event, const char **UNUSED(tooltip))
 {
-	ID *id = drag->poin;
+	SpaceOops *soops = CTX_wm_space_outliner(C);
+	Object *ob = (Object *)WM_drag_ID(drag, ID_OB);
+	if (!ob) {
+		return false;
+	}
+
+	/* Ensure item under cursor is valid drop target */
+	TreeElement *te = outliner_drop_find(C, event);
+	TreeStoreElem *tselem = te ? TREESTORE(te) : NULL;
+
+	if (!te) {
+		/* pass */
+	}
+	else if (te->idcode == ID_OB && tselem->type == 0) {
+		Scene *scene;
+		ID *te_id = tselem->id;
+
+		/* check if dropping self or parent */
+		if (te_id == &ob->id || (Object *)te_id == ob->parent)
+			return false;
+
+		/* check that parent/child are both in the same scene */
+		scene = (Scene *)outliner_search_back(soops, te, ID_SCE);
+
+		/* currently outliner organized in a way that if there's no parent scene
+		 * element for object it means that all displayed objects belong to
+		 * active scene and parenting them is allowed (sergey)
+		 */
+		if (!scene) {
+			return true;
+		}
+		else {
+			for (ViewLayer *view_layer = scene->view_layers.first;
+				 view_layer;
+				 view_layer = view_layer->next)
+			{
+				if (BKE_view_layer_base_find(view_layer, ob)) {
+					return true;
+				}
+			}
+		}
+	}
 
-	RNA_string_set(drop->ptr, "child", id->name + 2);
+	return false;
 }
 
 static int parent_drop_exec(bContext *C, wmOperator *op)
@@ -195,131 +209,121 @@ static int parent_drop_exec(bContext *C, wmOperator *op)
 
 static int parent_drop_invoke(bContext *C, wmOperator *op, const wmEvent *event)
 {
-	Object *par = NULL;
-	Object *ob = NULL;
-	SpaceOops *soops = CTX_wm_space_outliner(C);
-	ARegion *ar = CTX_wm_region(C);
 	Main *bmain = CTX_data_main(C);
-	Scene *scene = NULL;
-	TreeElement *te = NULL;
+	SpaceOops *soops = CTX_wm_space_outliner(C);
+	TreeElement *te = outliner_drop_find(C, event);
+	TreeStoreElem *tselem = te ? TREESTORE(te) : NULL;
+
+	if (!(te && te->idcode == ID_OB && tselem->type == 0)) {
+		return OPERATOR_CANCELLED;
+	}
+
+	Object *par = (Object *)tselem->id;
+	Object *ob = (Object *)WM_drag_ID_from_event(event, ID_OB);
+
+	if (ELEM(NULL, ob, par)) {
+		return OPERATOR_CANCELLED;
+	}
+	if (ob == par) {
+		return OPERATOR_CANCELLED;
+	}
+	if (ID_IS_LINKED(ob)) {
+		BKE_report(op->reports, RPT_INFO, "Can't edit library linked object");
+		return OPERATOR_CANCELLED;
+	}
+
 	char childname[MAX_ID_NAME];
 	char parname[MAX_ID_NAME];
-	int partype = 0;
-	float fmval[2];
+	STRNCPY(childname, ob->id.name);
+	STRNCPY(parname, par->id.name);
+	RNA_string_set(op->ptr, "child", childname);
+	RNA_string_set(op->ptr, "parent", parname);
 
-	UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &fmval[0], &fmval[1]);
+	Scene *scene = (Scene *)outliner_search_back(soops, te, ID_SCE);
 
-	/* Find object hovered over */
-	te = outliner_dropzone_find(soops, fmval, true);
+	if (scene == NULL) {
+		/* currently outlier organized in a way, that if there's no parent scene
+		 * element for object it means that all displayed objects belong to
+		 * active scene and parenting them is allowed (sergey)
+		 */
 
-	if (te) {
-		RNA_string_set(op->ptr, "parent", te->name);
-		/* Identify parent and child */
-		RNA_string_get(op->ptr, "child", childname);
-		ob = (Object *)BKE_libblock_find_name(bmain, ID_OB, childname);
-		RNA_string_get(op->ptr, "parent", parname);
-		par = (Object *)BKE_libblock_find_name(bmain, ID_OB, parname);
-
-		if (ELEM(NULL, ob, par)) {
-			if (par == NULL) printf("par==NULL\n");
-			return OPERATOR_CANCELLED;
-		}
-		if (ob == par) {
-			return OPERATOR_CANCELLED;
-		}
-		if (ID_IS_LINKED(ob)) {
-			BKE_report(op->reports, RPT_INFO, "Can't edit library linked object");
-			return OPERATOR_CANCELLED;
+		scene = CTX_data_scene(C);
+	}
+
+	if ((par->type != OB_ARMATURE) && (par->type != OB_CURVE) && (par->type != OB_LATTICE)) {
+		int partype = 0;
+		if (ED_object_parent_set(op->reports, C, scene, ob, par, partype, false, false, NULL)) {
+			DEG_relations_tag_update(bmain);
+			WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
+			WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
 		}
+	}
+	else {
+		/* Menu creation */
+		wmOperatorType *ot = WM_operatortype_find("OUTLINER_OT_parent_drop", false);
+		uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("Set Parent To"), ICON_NONE);
+		uiLayout *layout = UI_popup_menu_layout(pup);
+		PointerRNA ptr;
+
+		/* Cannot use uiItemEnumO()... have multiple properties to set. */
+		uiItemFullO_ptr(layout, ot, IFACE_("Object"), 0, NULL, WM_OP_EXEC_DEFAULT, 0, &ptr);
+		RNA_string_set(&ptr, "parent", parname);
+		RNA_string_set(&ptr, "child", childname);
+		RNA_enum_set(&ptr, "type", PAR_OBJECT);
+
+		/* par becomes parent, make the associated menus */
+		if (par->type == OB_ARMATURE) {
+			uiItemFullO_ptr(layout, ot, IFACE_("Armature Deform"), 0, NULL, WM_OP_EXEC_DEFAULT, 0, &ptr);
+			RNA_string_set(&ptr, "parent", parn

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list