[Bf-blender-cvs] [1006e84faac] temp-enum-socket: progress

Jacques Lucke noreply at git.blender.org
Mon Nov 8 16:07:03 CET 2021


Commit: 1006e84faac78d97a8b6fb09dd10732837bce107
Author: Jacques Lucke
Date:   Sun Nov 7 11:53:12 2021 +0100
Branches: temp-enum-socket
https://developer.blender.org/rB1006e84faac78d97a8b6fb09dd10732837bce107

progress

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

M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/NOD_socket_declarations.hh
M	source/blender/nodes/function/nodes/node_fn_enum.cc
M	source/blender/nodes/intern/node_socket_declarations.cc

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

diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h
index 5ec04065a6f..ad527fe5a82 100644
--- a/source/blender/makesdna/DNA_node_types.h
+++ b/source/blender/makesdna/DNA_node_types.h
@@ -671,6 +671,11 @@ typedef struct bNodeSocketValueMaterial {
 
 typedef struct bNodeSocketValueEnum {
   int value;
+  char _pad[4];
+  /**
+   * Possible items for the enum. This is only runtime data and is not owned by the socket.
+   */
+  const struct EnumPropertyItem *items;
 } bNodeSocketValueEnum;
 
 /* Data structs, for node->storage. */
diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c
index bdd965ddf9c..4494a553573 100644
--- a/source/blender/makesrna/intern/rna_nodetree.c
+++ b/source/blender/makesrna/intern/rna_nodetree.c
@@ -4588,6 +4588,20 @@ static NodeFunctionEnumItem *rna_NodeFunctionEnumItems_items_new(ID *id, bNode *
   return item;
 }
 
+static const EnumPropertyItem *rna_NodeSocketEnum_items(bContext *UNUSED(C),
+                                                        PointerRNA *ptr,
+                                                        PropertyRNA *UNUSED(prop),
+                                                        bool *r_free)
+{
+  *r_free = false;
+  bNodeSocket *socket = ptr->data;
+  bNodeSocketValueEnum *storage = (bNodeSocketValueEnum *)socket->default_value;
+  if (storage->items == NULL) {
+    return DummyRNA_NULL_items;
+  }
+  return storage->items;
+}
+
 #else
 
 static const EnumPropertyItem prop_image_layer_items[] = {
@@ -12045,7 +12059,8 @@ static void rna_def_node_socket_enum(BlenderRNA *brna,
 
   prop = RNA_def_property(srna, "default_value", PROP_ENUM, PROP_NONE);
   RNA_def_property_enum_sdna(prop, NULL, "value");
-  RNA_def_property_enum_items(prop, node_socket_data_type_items); /* TODO */
+  RNA_def_property_enum_funcs(prop, NULL, NULL, "rna_NodeSocketEnum_items");
+  RNA_def_property_enum_items(prop, DummyRNA_NULL_items);
   RNA_def_property_ui_text(prop, "Default Value", "Input value used for unconnected socket");
   RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_NodeSocketStandard_value_update");
   RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
diff --git a/source/blender/nodes/NOD_socket_declarations.hh b/source/blender/nodes/NOD_socket_declarations.hh
index ef440806b34..d95ece947aa 100644
--- a/source/blender/nodes/NOD_socket_declarations.hh
+++ b/source/blender/nodes/NOD_socket_declarations.hh
@@ -22,6 +22,7 @@
 
 #include "BLI_color.hh"
 #include "BLI_float3.hh"
+#include "BLI_utility_mixins.hh"
 
 namespace blender::nodes::decl {
 
@@ -167,14 +168,31 @@ class StringBuilder : public SocketDeclarationBuilder<String> {
 
 class EnumBuilder;
 
+class EnumItems : NonCopyable, NonMovable {
+ private:
+  const EnumPropertyItem *items_;
+  std::function<void()> free_fn_;
+
+ public:
+  EnumItems();
+  EnumItems(const EnumPropertyItem *items, std::function<void()> free_fn);
+  ~EnumItems();
+
+  const EnumPropertyItem *items() const;
+};
+
 class Enum : public SocketDeclaration {
  private:
   int default_value_ = 0;
+  std::shared_ptr<EnumItems> items_;
+
   friend EnumBuilder;
 
  public:
   using Builder = EnumBuilder;
 
+  const std::shared_ptr<EnumItems> &items() const;
+
   bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
   bool matches(const bNodeSocket &socket) const override;
 };
@@ -182,6 +200,9 @@ class Enum : public SocketDeclaration {
 class EnumBuilder : public SocketDeclarationBuilder<Enum> {
  public:
   EnumBuilder &default_value(const int value);
+
+  EnumBuilder &static_items(const EnumPropertyItem *items);
+  EnumBuilder &dynamic_items(std::shared_ptr<EnumItems> items);
 };
 
 class IDSocketDeclaration : public SocketDeclaration {
diff --git a/source/blender/nodes/function/nodes/node_fn_enum.cc b/source/blender/nodes/function/nodes/node_fn_enum.cc
index 49e9f4f8bd7..587c0d5338d 100644
--- a/source/blender/nodes/function/nodes/node_fn_enum.cc
+++ b/source/blender/nodes/function/nodes/node_fn_enum.cc
@@ -31,6 +31,22 @@ static void fn_node_enum_declare(NodeDeclarationBuilder &b)
   if (node == nullptr) {
     return;
   }
+
+  static EnumPropertyItem my_items[] = {
+      {0, "GRAVITY", 0, "Gravity", "Applies gravity to the simulation"},
+      {1, "INFLATE", 0, "Inflate", "Inflates the cloth"},
+      {2, "EXPAND", 0, "Expand", "Expands the cloth's dimensions"},
+      {3, "PINCH", 0, "Pinch", "Pulls the cloth to the cursor's start position"},
+      {4,
+       "SCALE",
+       0,
+       "Scale",
+       "Scales the mesh as a soft body using the origin of the object as scale"},
+      {0, NULL, 0, NULL, NULL},
+  };
+
+  b.add_input<decl::Enum>("Enum").static_items(my_items).hide_label();
+
   const NodeFunctionEnum *storage = (const NodeFunctionEnum *)node->storage;
   LISTBASE_FOREACH (const NodeFunctionEnumItem *, item, &storage->items) {
     b.add_output<decl::Bool>(N_("Bool"), "item_" + std::to_string(item->value));
diff --git a/source/blender/nodes/intern/node_socket_declarations.cc b/source/blender/nodes/intern/node_socket_declarations.cc
index d83f0be7ac4..548267b5c66 100644
--- a/source/blender/nodes/intern/node_socket_declarations.cc
+++ b/source/blender/nodes/intern/node_socket_declarations.cc
@@ -19,6 +19,8 @@
 
 #include "BKE_node.h"
 
+#include "RNA_enum_types.h"
+
 #include "BLI_math_vector.h"
 
 namespace blender::nodes::decl {
@@ -277,12 +279,39 @@ bool String::matches(const bNodeSocket &socket) const
 /** \name #Enum
  * \{ */
 
+EnumItems::EnumItems() : items_(DummyRNA_NULL_items)
+{
+}
+
+EnumItems::EnumItems(const EnumPropertyItem *items, std::function<void()> free_fn)
+    : items_(items), free_fn_(std::move(free_fn))
+{
+}
+
+EnumItems::~EnumItems()
+{
+  if (free_fn_) {
+    free_fn_();
+  }
+}
+
+const EnumPropertyItem *EnumItems::items() const
+{
+  return items_;
+}
+
+const std::shared_ptr<EnumItems> &Enum::items() const
+{
+  return items_;
+}
+
 bNodeSocket &Enum::build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const
 {
   bNodeSocket &socket = *nodeAddStaticSocket(
       &ntree, &node, in_out, SOCK_ENUM, PROP_NONE, identifier_.c_str(), name_.c_str());
   ((bNodeSocketValueEnum *)socket.default_value)->value = default_value_;
   this->set_common_flags(socket);
+  ((bNodeSocketValueEnum *)socket.default_value)->items = items_->items();
   return socket;
 }
 
@@ -297,6 +326,18 @@ bool Enum::matches(const bNodeSocket &socket) const
   return true;
 }
 
+EnumBuilder &EnumBuilder::static_items(const EnumPropertyItem *items)
+{
+  decl_->items_ = std::make_shared<EnumItems>(items, nullptr);
+  return *this;
+}
+
+EnumBuilder &EnumBuilder::dynamic_items(std::shared_ptr<EnumItems> items)
+{
+  decl_->items_ = std::move(items);
+  return *this;
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */



More information about the Bf-blender-cvs mailing list