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:
authorJacques Lucke <jacques@blender.org>2021-08-30 18:13:46 +0300
committerJacques Lucke <jacques@blender.org>2021-08-30 18:23:05 +0300
commit1e69a25043120cc8dddc3f58622eb50e1443def1 (patch)
tree16bcb3eb98215dbbd5c54880cb3d14544e316d3f /source/blender/nodes/NOD_socket_declarations.hh
parente8684eff301d09e25b4e7ef6221a5289661da2e3 (diff)
Nodes: add more flexible method to declare sockets of a node
Previously, built-in nodes had to implement "socket templates" (`bNodeSocketTemplate`) to tell Blender which sockets they have. It was nice that this was declarative, but this approach was way too rigid and was cumbersome to use in many cases. This commit starts to move us away from this rigid structure by letting nodes implement a function that declares the sockets the node has. Right now this is used as a direct replacement of the "socket template" approach to keep the refactor smaller. It's just a bit easier to read and write. In the future we want to support more complex features like dynamic numbers of sockets and type inferencing. Those features will be easier to build on this new approach. This new approach can live side by side with `bNodeSocketTemplate` for a while. That makes it easier to update nodes one by one. Note: In `bNodeSocketTemplate` socket identifiers were made unique automatically. In this new approach, one has to specify unique identifiers manually (unless the name is unique already). Differential Revision: https://developer.blender.org/D12335
Diffstat (limited to 'source/blender/nodes/NOD_socket_declarations.hh')
-rw-r--r--source/blender/nodes/NOD_socket_declarations.hh239
1 files changed, 239 insertions, 0 deletions
diff --git a/source/blender/nodes/NOD_socket_declarations.hh b/source/blender/nodes/NOD_socket_declarations.hh
new file mode 100644
index 00000000000..639d2c12d73
--- /dev/null
+++ b/source/blender/nodes/NOD_socket_declarations.hh
@@ -0,0 +1,239 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include "NOD_node_declaration.hh"
+
+#include "RNA_types.h"
+
+#include "BLI_color.hh"
+#include "BLI_float3.hh"
+
+namespace blender::nodes::decl {
+
+class Float : public SocketDeclaration {
+ private:
+ float default_value_ = 0.0f;
+ float soft_min_value_ = -FLT_MAX;
+ float soft_max_value_ = FLT_MAX;
+ PropertySubType subtype_ = PROP_NONE;
+
+ public:
+ Float &min(const float value)
+ {
+ soft_min_value_ = value;
+ return *this;
+ }
+
+ Float &max(const float value)
+ {
+ soft_max_value_ = value;
+ return *this;
+ }
+
+ Float &default_value(const float value)
+ {
+ default_value_ = value;
+ return *this;
+ }
+
+ Float &subtype(PropertySubType subtype)
+ {
+ subtype_ = subtype;
+ return *this;
+ }
+
+ bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+ bool matches(const bNodeSocket &socket) const override;
+ bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const override;
+};
+
+class Int : public SocketDeclaration {
+ private:
+ int default_value_ = 0;
+ int soft_min_value_ = INT32_MIN;
+ int soft_max_value_ = INT32_MAX;
+ PropertySubType subtype_ = PROP_NONE;
+
+ public:
+ Int &min(const int value)
+ {
+ soft_min_value_ = value;
+ return *this;
+ }
+
+ Int &max(const int value)
+ {
+ soft_max_value_ = value;
+ return *this;
+ }
+
+ Int &default_value(const int value)
+ {
+ default_value_ = value;
+ return *this;
+ }
+
+ Int &subtype(PropertySubType subtype)
+ {
+ subtype_ = subtype;
+ return *this;
+ }
+
+ bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+ bool matches(const bNodeSocket &socket) const override;
+ bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const override;
+};
+
+class Vector : public SocketDeclaration {
+ private:
+ float3 default_value_ = {0, 0, 0};
+ PropertySubType subtype_ = PROP_NONE;
+
+ public:
+ Vector &default_value(const float3 value)
+ {
+ default_value_ = value;
+ return *this;
+ }
+
+ Vector &subtype(PropertySubType subtype)
+ {
+ subtype_ = subtype;
+ return *this;
+ }
+
+ bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+ bool matches(const bNodeSocket &socket) const override;
+ bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const override;
+};
+
+class Bool : public SocketDeclaration {
+ private:
+ bool default_value_ = false;
+
+ public:
+ Bool &default_value(const bool value)
+ {
+ default_value_ = value;
+ return *this;
+ }
+
+ bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+ bool matches(const bNodeSocket &socket) const override;
+};
+
+class Color : public SocketDeclaration {
+ private:
+ ColorGeometry4f default_value_;
+
+ public:
+ Color &default_value(const ColorGeometry4f value)
+ {
+ default_value_ = value;
+ return *this;
+ }
+
+ bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+ bool matches(const bNodeSocket &socket) const override;
+};
+
+class String : public SocketDeclaration {
+ public:
+ bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+ bool matches(const bNodeSocket &socket) const override;
+};
+
+namespace detail {
+struct CommonIDSocketData {
+ const char *idname;
+ bool hide_label = false;
+};
+
+bNodeSocket &build_id_socket(bNodeTree &ntree,
+ bNode &node,
+ eNodeSocketInOut in_out,
+ const CommonIDSocketData &data,
+ StringRefNull name,
+ StringRefNull identifier);
+bool matches_id_socket(const bNodeSocket &socket,
+ const CommonIDSocketData &data,
+ StringRefNull name,
+ StringRefNull identifier);
+
+template<typename Subtype> class IDSocketDeclaration : public SocketDeclaration {
+ private:
+ CommonIDSocketData data_;
+
+ public:
+ IDSocketDeclaration(const char *idname) : data_({idname})
+ {
+ }
+
+ Subtype &hide_label(bool value)
+ {
+ data_.hide_label = value;
+ return *(Subtype *)this;
+ }
+
+ bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override
+ {
+ return build_id_socket(ntree, node, in_out, data_, name_, identifier_);
+ }
+
+ bool matches(const bNodeSocket &socket) const override
+ {
+ return matches_id_socket(socket, data_, name_, identifier_);
+ }
+};
+} // namespace detail
+
+class Object : public detail::IDSocketDeclaration<Object> {
+ public:
+ Object() : detail::IDSocketDeclaration<Object>("NodeSocketObject")
+ {
+ }
+};
+
+class Material : public detail::IDSocketDeclaration<Material> {
+ public:
+ Material() : detail::IDSocketDeclaration<Material>("NodeSocketMaterial")
+ {
+ }
+};
+
+class Collection : public detail::IDSocketDeclaration<Collection> {
+ public:
+ Collection() : detail::IDSocketDeclaration<Collection>("NodeSocketCollection")
+ {
+ }
+};
+
+class Texture : public detail::IDSocketDeclaration<Texture> {
+ public:
+ Texture() : detail::IDSocketDeclaration<Texture>("NodeSocketTexture")
+ {
+ }
+};
+
+class Geometry : public SocketDeclaration {
+ public:
+ bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+ bool matches(const bNodeSocket &socket) const override;
+};
+
+} // namespace blender::nodes::decl