[Bf-codereview] Patch: generic node socket display in the Node Editor (issue 12440043)

sjdv1982 at gmail.com sjdv1982 at gmail.com
Sun Aug 4 19:21:33 CEST 2013


Reviewers: lukas.toenne1, bf-codereview_blender.org,

Description:
Summary: This patch enables generic node socket drawing, independent of
whether the socket is input or output. This enables label display and
property editing for all sockets.
Generic node sockets can be enabled for custom Node types by enabling
"bl_generic_sockets".
Custom NodeSocket types within these Nodes can have their labels hidden
if "bl_hide_label" is enabled, and can be drawn on the same row as the
next socket if "bl_shared_row" is enabled. This allows input socket and
output socket to be drawn at the same position.

Current situation:
Input and output sockets, while having the same bNodeSocket type, are
treated differently in drawing, calling either
nodesocket->typeinfo->drawinputfunc or drawoutputfunc.
The default value of drawoutputfunc draws the label, but does not call
draw() on the socket in the property panel. For drawinputfunc, the
behavior is reversed.

Patch:
This patch implements a generic nodesocket draw function, for both
inputs and outputs. When "bl_generic_sockets" is enabled in a custom
Node definition, both its drawinputfunc and drawoutputfunc are set to
the generic draw function.

The generic draw function always calls the panel draw() function, and it
always displays the label. However, label drawing can be disabled for a
particular custom NodeSocket typr by specifying "bl_hide_label" as True
in the NodeSocket definition.
In addition, a NodeSocket can be drawn on the same line as the socket
that comes after, by enabling "bl_shared_row" in the NodeSocket
definition. (The drawing implementation of same-row sockets is in the
patch https://codereview.appspot.com/12437043/)

Internally, SOCKTYPE_HIDE_LABEL and SOCKTYPE_SHARED_ROW are defined as
mask bits in NodeSocket->typeinfo->flag, and
NODETYPE_GENERIC as a mask bit in Node->typeinfo->flag.

In existing code, Node->typeinfo->flag (16 bits) was mixed up with the
NODE_PREVIEW mask bit for Node->flag (32 bits). This mixing-up was
removed by defining a new NODETYPE_PREVIEW mask bit. The actual value of
the mask bit was kept the same.


Please review this at https://codereview.appspot.com/12440043/

Affected files:
   source/blender/blenkernel/BKE_node.h
   source/blender/blenkernel/intern/node.c
   source/blender/editors/space_node/drawnode.c
   source/blender/editors/space_node/node_draw.c
   source/blender/editors/space_node/node_edit.c
   source/blender/makesdna/DNA_node_types.h
   source/blender/makesrna/intern/rna_nodetree.c


Index: source/blender/makesrna/intern/rna_nodetree.c
===================================================================
--- source/blender/makesrna/intern/rna_nodetree.c	(revision 58801)
+++ source/blender/makesrna/intern/rna_nodetree.c	(working copy)
@@ -6115,6 +6115,14 @@
  	RNA_def_property_flag(prop, PROP_REGISTER | PROP_NEVER_CLAMP);
  	RNA_def_property_ui_text(prop, "ID Name", "");

+	prop = RNA_def_property(srna, "bl_hide_label", PROP_BOOLEAN, PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "typeinfo->flag",  
SOCKTYPE_HIDE_LABEL);
+	RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
+
+	prop = RNA_def_property(srna, "bl_shared_row", PROP_BOOLEAN, PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "typeinfo->flag",  
SOCKTYPE_SHARED_ROW);
+	RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
+
  	/* draw socket */
  	func = RNA_def_function(srna, "draw", NULL);
  	RNA_def_function_ui_description(func, "Draw socket");
@@ -6946,6 +6954,10 @@
  	RNA_def_property_float_sdna(prop, NULL, "typeinfo->minheight");
  	RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);

+	prop = RNA_def_property(srna, "bl_generic_sockets", PROP_BOOLEAN,  
PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "typeinfo->flag",  
NODETYPE_SOCKETS_GENERIC);
+	RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
+
  	/* poll */
  	func = RNA_def_function(srna, "poll", NULL);
  	RNA_def_function_ui_description(func, "If non-null output is returned,  
the node type can be added to the tree");
Index: source/blender/editors/space_node/node_edit.c
===================================================================
--- source/blender/editors/space_node/node_edit.c	(revision 58801)
+++ source/blender/editors/space_node/node_edit.c	(working copy)
@@ -1421,7 +1421,7 @@
  	for (node = snode->edittree->nodes.first; node; node = node->next) {
  		if (node->flag & SELECT) {
  			
-			if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag &  
NODE_PREVIEW) == 0)
+			if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag &  
NODETYPE_PREVIEW) == 0)
  				continue;
  			if (toggle_flag == NODE_OPTIONS && !(node->typeinfo->uifunc ||  
node->typeinfo->uifuncbut))
  				continue;
@@ -1435,7 +1435,7 @@
  	for (node = snode->edittree->nodes.first; node; node = node->next) {
  		if (node->flag & SELECT) {
  			
-			if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag &  
NODE_PREVIEW) == 0)
+			if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag &  
NODETYPE_PREVIEW) == 0)
  				continue;
  			if (toggle_flag == NODE_OPTIONS && !(node->typeinfo->uifunc ||  
node->typeinfo->uifuncbut))
  				continue;
Index: source/blender/editors/space_node/node_draw.c
===================================================================
--- source/blender/editors/space_node/node_draw.c	(revision 58801)
+++ source/blender/editors/space_node/node_draw.c	(working copy)
@@ -805,7 +805,7 @@
  	iconofs = rct->xmax - 0.35f * U.widget_unit;
  	
  	/* preview */
-	if (node->typeinfo->flag & NODE_PREVIEW) {
+	if (node->typeinfo->flag & NODETYPE_PREVIEW) {
  		uiBut *but;
  		iconofs -= iconbutw;
  		uiBlockSetEmboss(node->block, UI_EMBOSSN);
Index: source/blender/editors/space_node/drawnode.c
===================================================================
--- source/blender/editors/space_node/drawnode.c	(revision 58801)
+++ source/blender/editors/space_node/drawnode.c	(working copy)
@@ -98,6 +98,13 @@
  	node_socket_button_label(C, layout, ptr, node_ptr, IFACE_(sock->name));
  }

+static void node_draw_io_generic(bContext *C, uiLayout *layout, PointerRNA  
*ptr, PointerRNA *node_ptr)
+{
+	bNodeSocket *sock = (bNodeSocket *)ptr->data;
+	sock->typeinfo->draw(C, layout, ptr, node_ptr, IFACE_(sock->name));
+	if (!(sock->typeinfo->flag & SOCKTYPE_HIDE_LABEL))
+			node_socket_button_label(C, layout, ptr, node_ptr, IFACE_(sock->name));
+}

  /* ****************** BASE DRAW FUNCTIONS FOR NEW OPERATOR NODES  
***************** */

@@ -2727,8 +2734,14 @@
  	/* default ui functions */
  	ntype->drawfunc = node_draw_default;
  	ntype->drawupdatefunc = node_update_default;
-	ntype->drawinputfunc = node_draw_input_default;
-	ntype->drawoutputfunc = node_draw_output_default;
+	if (ntype->flag & NODETYPE_SOCKETS_GENERIC) {
+		ntype->drawinputfunc = node_draw_io_generic;
+		ntype->drawoutputfunc = node_draw_io_generic;
+	}
+	else {
+		ntype->drawinputfunc = node_draw_input_default;
+		ntype->drawoutputfunc = node_draw_output_default;
+	}
  	ntype->resize_area_func = node_resize_area_default;
  	ntype->select_area_func = node_select_area_default;
  	ntype->tweak_area_func = node_tweak_area_default;
Index: source/blender/blenkernel/intern/node.c
===================================================================
--- source/blender/blenkernel/intern/node.c	(revision 58801)
+++ source/blender/blenkernel/intern/node.c	(working copy)
@@ -1267,7 +1267,7 @@
  int BKE_node_preview_used(bNode *node)
  {
  	/* XXX check for closed nodes? */
-	return (node->typeinfo->flag & NODE_PREVIEW) != 0;
+	return (node->typeinfo->flag & NODETYPE_PREVIEW) != 0;
  }

  bNodePreview *BKE_node_preview_verify(bNodeInstanceHash *previews,  
bNodeInstanceKey key, int xsize, int ysize, int create)
@@ -1511,7 +1511,7 @@
  		return;
  	
  	for (node = ntree->nodes.first; node; node = node->next) {
-		if (node->typeinfo->flag & NODE_PREVIEW)
+		if (node->typeinfo->flag & NODETYPE_PREVIEW)
  			nodeClearPreview(node);
  		if (node->type == NODE_GROUP)
  			ntreeClearPreview((bNodeTree *)node->id);
Index: source/blender/blenkernel/BKE_node.h
===================================================================
--- source/blender/blenkernel/BKE_node.h	(revision 58801)
+++ source/blender/blenkernel/BKE_node.h	(working copy)
@@ -112,6 +112,7 @@
   */
  typedef struct bNodeSocketType {
  	char idname[64];				/* identifier name */
+	int flag; int pad;
  	
  	void (*draw)(struct bContext *C, struct uiLayout *layout, struct  
PointerRNA *ptr, struct PointerRNA *node_ptr, const char *text);
  	void (*draw_color)(struct bContext *C, struct PointerRNA *ptr, struct  
PointerRNA *node_ptr, float *r_color);
Index: source/blender/makesdna/DNA_node_types.h
===================================================================
--- source/blender/makesdna/DNA_node_types.h	(revision 58801)
+++ source/blender/makesdna/DNA_node_types.h	(working copy)
@@ -167,6 +167,12 @@
  	/* DEPRECATED, only kept here to avoid reusing the flag */
  #define SOCK_AUTO_HIDDEN__DEPRECATED	256

+/* sock->typeinfo->flag
+ *  only used by generic input/output sockets
+*/
+#define SOCKTYPE_HIDE_LABEL 		1
+#define SOCKTYPE_SHARED_ROW		 	2
+
  /* limit data in bNode to what we want to see saved? */
  typedef struct bNode {
  	struct bNode *next, *prev, *new_node;
@@ -262,6 +268,18 @@
  	 */
  #define NODE_DO_OUTPUT_RECALC	(1<<17)

+
+/* node->typeinfo->flag
+ *  these are only 16-bits (32 bit for node->flag!)
+ */
+#define NODETYPE_PREVIEW 				 4    /* equal to NODE_PREVIEW */
+/* node->typeinfo->flag */
+/* node uses generic input/output sockets,
+ *  that can draw both labels and panel properties
+ * this is only used in node->typeinfo->flag
+ */
+#define NODETYPE_SOCKETS_GENERIC		 256
+
  /* node->update */
  /* XXX NODE_UPDATE is a generic update flag. More fine-grained updates
   * might be used in the future, but currently all work the same way.




More information about the Bf-codereview mailing list