[Bf-blender-cvs] [fe4e85a9240] master: Nodes: Dynamic node class for Map Range node

Charlie Jolly noreply at git.blender.org
Mon Feb 7 16:39:51 CET 2022


Commit: fe4e85a9240ff95909be33c7e72e0ab969d5a4ca
Author: Charlie Jolly
Date:   Mon Feb 7 15:29:16 2022 +0000
Branches: master
https://developer.blender.org/rBfe4e85a9240ff95909be33c7e72e0ab969d5a4ca

Nodes: Dynamic node class for Map Range node

This patch makes it possible to set the UI color of a node's
header bar and override the default from the node's typeinfo.

Currently the color is taken from the `.nclass`
member of the node's bNodeType->TypeInfo struct.
This is created once when registering the node.
The TypeInfo is used for both UI and non-UI functionality.
Since the TypeInfo is shared, the header bar for the node
can't be changed without changing all nodes of that type.

The Map Range node is shown as a `Converter` or blue color by default.
This patch allows this to be changed dynamically to `Vector` or purple.

This is done by adding a `ui_class` callback to node typeinfo struct.

Reviewed By: HooglyBoogly

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

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

M	source/blender/blenkernel/BKE_node.h
M	source/blender/editors/space_node/node_draw.cc
M	source/blender/nodes/shader/nodes/node_shader_map_range.cc

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

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index d583b5f0648..7ffa180b523 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -274,6 +274,9 @@ typedef struct bNodeType {
                     char *label,
                     int maxlen);
 
+  /** Optional override for node class, used for drawing node header. */
+  int (*ui_class)(const struct bNode *node);
+
   /** Called when the node is updated in the editor. */
   void (*updatefunc)(struct bNodeTree *ntree, struct bNode *node);
   /** Check and update if internal ID data has changed. */
diff --git a/source/blender/editors/space_node/node_draw.cc b/source/blender/editors/space_node/node_draw.cc
index 834bb3e5802..81c63e9bddb 100644
--- a/source/blender/editors/space_node/node_draw.cc
+++ b/source/blender/editors/space_node/node_draw.cc
@@ -626,7 +626,9 @@ static void node_update_hidden(bNode &node, uiBlock &block)
 
 static int node_get_colorid(const bNode &node)
 {
-  switch (node.typeinfo->nclass) {
+  const int nclass = (node.typeinfo->ui_class == nullptr) ? node.typeinfo->nclass :
+                                                            node.typeinfo->ui_class(&node);
+  switch (nclass) {
     case NODE_CLASS_INPUT:
       return TH_NODE_INPUT;
     case NODE_CLASS_OUTPUT:
diff --git a/source/blender/nodes/shader/nodes/node_shader_map_range.cc b/source/blender/nodes/shader/nodes/node_shader_map_range.cc
index bc7ca661a77..254ab9f866d 100644
--- a/source/blender/nodes/shader/nodes/node_shader_map_range.cc
+++ b/source/blender/nodes/shader/nodes/node_shader_map_range.cc
@@ -66,6 +66,16 @@ static void node_shader_buts_map_range(uiLayout *layout, bContext *UNUSED(C), Po
   }
 }
 
+static int node_shader_map_range_ui_class(const bNode *node)
+{
+  const NodeMapRange &storage = node_storage(*node);
+  const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);
+  if (data_type == CD_PROP_FLOAT3) {
+    return NODE_CLASS_OP_VECTOR;
+  }
+  return NODE_CLASS_CONVERTER;
+}
+
 static void node_shader_update_map_range(bNodeTree *ntree, bNode *node)
 {
   const NodeMapRange &storage = node_storage(*node);
@@ -665,6 +675,7 @@ void register_node_type_sh_map_range()
   sh_fn_node_type_base(&ntype, SH_NODE_MAP_RANGE, "Map Range", NODE_CLASS_CONVERTER);
   ntype.declare = file_ns::sh_node_map_range_declare;
   ntype.draw_buttons = file_ns::node_shader_buts_map_range;
+  ntype.ui_class = file_ns::node_shader_map_range_ui_class;
   node_type_init(&ntype, file_ns::node_shader_init_map_range);
   node_type_storage(
       &ntype, "NodeMapRange", node_free_standard_storage, node_copy_standard_storage);



More information about the Bf-blender-cvs mailing list