Blender V4.3
node_fn_boolean_math.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_listbase.h"
6#include "BLI_string.h"
7#include "BLI_string_utf8.h"
8
9#include "RNA_enum_types.hh"
10
11#include "UI_interface.hh"
12#include "UI_resources.hh"
13
17
18#include "NOD_rna_define.hh"
19
20#include "node_function_util.hh"
21
23
25{
26 b.is_function_node();
27 b.add_input<decl::Bool>("Boolean", "Boolean");
28 b.add_input<decl::Bool>("Boolean", "Boolean_001");
29 b.add_output<decl::Bool>("Boolean");
30}
31
32static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
33{
34 uiItemR(layout, ptr, "operation", UI_ITEM_NONE, "", ICON_NONE);
35}
36
37static void node_update(bNodeTree *ntree, bNode *node)
38{
39 bNodeSocket *sockB = (bNodeSocket *)BLI_findlink(&node->inputs, 1);
40
41 bke::node_set_socket_availability(ntree, sockB, !ELEM(node->custom1, NODE_BOOLEAN_MATH_NOT));
42}
43
44static void node_label(const bNodeTree * /*tree*/,
45 const bNode *node,
46 char *label,
47 int label_maxncpy)
48{
49 const char *name;
50 bool enum_label = RNA_enum_name(rna_enum_node_boolean_math_items, node->custom1, &name);
51 if (!enum_label) {
52 name = "Unknown";
53 }
54 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
55}
56
58{
59 if (!params.node_tree().typeinfo->validate_link(
60 static_cast<eNodeSocketDatatype>(params.other_socket().type), SOCK_BOOLEAN))
61 {
62 return;
63 }
64
66 item->identifier != nullptr;
67 item++)
68 {
69 if (item->name != nullptr && item->identifier[0] != '\0') {
70 NodeBooleanMathOperation operation = static_cast<NodeBooleanMathOperation>(item->value);
71 params.add_item(IFACE_(item->name), [operation](LinkSearchOpParams &params) {
72 bNode &node = params.add_node("FunctionNodeBooleanMath");
73 node.custom1 = operation;
74 params.update_and_connect_available_socket(node, "Boolean");
75 });
76 }
77 }
78}
79
80static const mf::MultiFunction *get_multi_function(const bNode &bnode)
81{
82 static auto exec_preset = mf::build::exec_presets::AllSpanOrSingle();
83 static auto and_fn = mf::build::SI2_SO<bool, bool, bool>(
84 "And", [](bool a, bool b) { return a && b; }, exec_preset);
85 static auto or_fn = mf::build::SI2_SO<bool, bool, bool>(
86 "Or", [](bool a, bool b) { return a || b; }, exec_preset);
87 static auto not_fn = mf::build::SI1_SO<bool, bool>(
88 "Not", [](bool a) { return !a; }, exec_preset);
89 static auto nand_fn = mf::build::SI2_SO<bool, bool, bool>(
90 "Not And", [](bool a, bool b) { return !(a && b); }, exec_preset);
91 static auto nor_fn = mf::build::SI2_SO<bool, bool, bool>(
92 "Nor", [](bool a, bool b) { return !(a || b); }, exec_preset);
93 static auto xnor_fn = mf::build::SI2_SO<bool, bool, bool>(
94 "Equal", [](bool a, bool b) { return a == b; }, exec_preset);
95 static auto xor_fn = mf::build::SI2_SO<bool, bool, bool>(
96 "Not Equal", [](bool a, bool b) { return a != b; }, exec_preset);
97 static auto imply_fn = mf::build::SI2_SO<bool, bool, bool>(
98 "Imply", [](bool a, bool b) { return !a || b; }, exec_preset);
99 static auto nimply_fn = mf::build::SI2_SO<bool, bool, bool>(
100 "Subtract", [](bool a, bool b) { return a && !b; }, exec_preset);
101
102 switch (bnode.custom1) {
104 return &and_fn;
106 return &or_fn;
108 return &not_fn;
110 return &nand_fn;
112 return &nor_fn;
114 return &xnor_fn;
116 return &xor_fn;
118 return &imply_fn;
120 return &nimply_fn;
121 }
122
124 return nullptr;
125}
126
128{
129 const mf::MultiFunction *fn = get_multi_function(builder.node());
130 builder.set_matching_fn(fn);
131}
132
134{
135 using namespace value_elem;
137 switch (op) {
139 params.set_output_elem("Boolean", params.get_input_elem<BoolElem>("Boolean"));
140 break;
141 }
142 default: {
143 break;
144 }
145 }
146}
147
149{
150 using namespace value_elem;
152 switch (op) {
154 params.set_input_elem("Boolean", params.get_output_elem<BoolElem>("Boolean"));
155 break;
156 }
157 default: {
158 break;
159 }
160 }
161}
162
164{
166 const StringRef first_input_id = "Boolean";
167 const StringRef output_id = "Boolean";
168 switch (op) {
170 params.set_input(first_input_id, !params.get_output<bool>(output_id));
171 break;
172 }
173 default: {
174 break;
175 }
176 }
177}
178
179static void node_rna(StructRNA *srna)
180{
182 "operation",
183 "Operation",
184 "",
187}
188
189static void node_register()
190{
191 static blender::bke::bNodeType ntype;
192
193 fn_node_type_base(&ntype, FN_NODE_BOOLEAN_MATH, "Boolean Math", NODE_CLASS_CONVERTER);
194 ntype.declare = node_declare;
195 ntype.labelfunc = node_label;
196 ntype.updatefunc = node_update;
204
205 node_rna(ntype.rna_ext.srna);
206}
208
209} // namespace blender::nodes::node_fn_boolean_math_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:410
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
#define ELEM(...)
#define IFACE_(msgid)
eNodeSocketDatatype
@ SOCK_BOOLEAN
NodeBooleanMathOperation
@ NODE_BOOLEAN_MATH_IMPLY
@ NODE_BOOLEAN_MATH_AND
@ NODE_BOOLEAN_MATH_NAND
@ NODE_BOOLEAN_MATH_XOR
@ NODE_BOOLEAN_MATH_NOT
@ NODE_BOOLEAN_MATH_OR
@ NODE_BOOLEAN_MATH_NIMPLY
@ NODE_BOOLEAN_MATH_XNOR
@ NODE_BOOLEAN_MATH_NOR
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
void set_matching_fn(const mf::MultiFunction *fn)
local_group_size(16, 16) .push_constant(Type b
const char * label
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_set_socket_availability(bNodeTree *ntree, bNodeSocket *sock, bool is_available)
Definition node.cc:3911
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static void node_eval_inverse(inverse_eval::InverseEvalParams &params)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_eval_inverse_elem(value_elem::InverseElemEvalParams &params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_update(bNodeTree *ntree, bNode *node)
static void node_eval_elem(value_elem::ElemEvalParams &params)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
const EnumPropertyItem rna_enum_node_boolean_math_items[]
const char * identifier
Definition RNA_types.hh:506
StructRNA * srna
Definition RNA_types.hh:780
int16_t custom1
Defines a node type.
Definition BKE_node.hh:218
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:378
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:385
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:249
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:372
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:363
NodeDeclareFunction declare
Definition BKE_node.hh:347
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:257
PointerRNA * ptr
Definition wm_files.cc:4126