Blender V5.0
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_utf8.h"
7
8#include "RNA_enum_types.hh"
9
11#include "UI_resources.hh"
12
16
17#include "NOD_rna_define.hh"
18
19#include "node_function_util.hh"
20
22
24{
25 b.is_function_node();
26 b.add_input<decl::Bool>("Boolean", "Boolean");
27 b.add_input<decl::Bool>("Boolean", "Boolean_001");
28 b.add_output<decl::Bool>("Boolean");
29}
30
31static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
32{
33 layout->prop(ptr, "operation", UI_ITEM_NONE, "", ICON_NONE);
34}
35
36static void node_update(bNodeTree *ntree, bNode *node)
37{
38 bNodeSocket *sockB = (bNodeSocket *)BLI_findlink(&node->inputs, 1);
39
41}
42
43static void node_label(const bNodeTree * /*tree*/,
44 const bNode *node,
45 char *label,
46 int label_maxncpy)
47{
48 const char *name;
50 if (!enum_label) {
51 name = N_("Unknown");
52 }
53 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
54}
55
57{
58 if (!params.node_tree().typeinfo->validate_link(eNodeSocketDatatype(params.other_socket().type),
60 {
61 return;
62 }
63
65 item->identifier != nullptr;
66 item++)
67 {
68 if (item->name != nullptr && item->identifier[0] != '\0') {
69 NodeBooleanMathOperation operation = static_cast<NodeBooleanMathOperation>(item->value);
70 params.add_item(IFACE_(item->name), [operation](LinkSearchOpParams &params) {
71 bNode &node = params.add_node("FunctionNodeBooleanMath");
72 node.custom1 = operation;
73 params.update_and_connect_available_socket(node, "Boolean");
74 });
75 }
76 }
77}
78
79static const mf::MultiFunction *get_multi_function(const bNode &bnode)
80{
81 static auto exec_preset = mf::build::exec_presets::AllSpanOrSingle();
82 static auto and_fn = mf::build::SI2_SO<bool, bool, bool>(
83 "And", [](bool a, bool b) { return a && b; }, exec_preset);
84 static auto or_fn = mf::build::SI2_SO<bool, bool, bool>(
85 "Or", [](bool a, bool b) { return a || b; }, exec_preset);
86 static auto not_fn = mf::build::SI1_SO<bool, bool>(
87 "Not", [](bool a) { return !a; }, exec_preset);
88 static auto nand_fn = mf::build::SI2_SO<bool, bool, bool>(
89 "Not And", [](bool a, bool b) { return !(a && b); }, exec_preset);
90 static auto nor_fn = mf::build::SI2_SO<bool, bool, bool>(
91 "Nor", [](bool a, bool b) { return !(a || b); }, exec_preset);
92 static auto xnor_fn = mf::build::SI2_SO<bool, bool, bool>(
93 "Equal", [](bool a, bool b) { return a == b; }, exec_preset);
94 static auto xor_fn = mf::build::SI2_SO<bool, bool, bool>(
95 "Not Equal", [](bool a, bool b) { return a != b; }, exec_preset);
96 static auto imply_fn = mf::build::SI2_SO<bool, bool, bool>(
97 "Imply", [](bool a, bool b) { return !a || b; }, exec_preset);
98 static auto nimply_fn = mf::build::SI2_SO<bool, bool, bool>(
99 "Subtract", [](bool a, bool b) { return a && !b; }, exec_preset);
100
101 switch (bnode.custom1) {
103 return &and_fn;
105 return &or_fn;
107 return &not_fn;
109 return &nand_fn;
111 return &nor_fn;
113 return &xnor_fn;
115 return &xor_fn;
117 return &imply_fn;
119 return &nimply_fn;
120 }
121
123 return nullptr;
124}
125
127{
128 const mf::MultiFunction *fn = get_multi_function(builder.node());
129 builder.set_matching_fn(fn);
130}
131
133{
134 using namespace value_elem;
136 switch (op) {
138 params.set_output_elem("Boolean", params.get_input_elem<BoolElem>("Boolean"));
139 break;
140 }
141 default: {
142 break;
143 }
144 }
145}
146
148{
149 using namespace value_elem;
151 switch (op) {
153 params.set_input_elem("Boolean", params.get_output_elem<BoolElem>("Boolean"));
154 break;
155 }
156 default: {
157 break;
158 }
159 }
160}
161
163{
165 const StringRef first_input_id = "Boolean";
166 const StringRef output_id = "Boolean";
167 switch (op) {
169 params.set_input(first_input_id, !params.get_output<bool>(output_id));
170 break;
171 }
172 default: {
173 break;
174 }
175 }
176}
177
178static void node_rna(StructRNA *srna)
179{
181 "operation",
182 "Operation",
183 "",
186}
187
188static void node_register()
189{
190 static blender::bke::bNodeType ntype;
191
192 fn_node_type_base(&ntype, "FunctionNodeBooleanMath", FN_NODE_BOOLEAN_MATH);
193 ntype.ui_name = "Boolean Math";
194 ntype.ui_description = "Perform a logical operation on the given boolean inputs";
195 ntype.enum_name_legacy = "BOOLEAN_MATH";
197 ntype.declare = node_declare;
198 ntype.labelfunc = node_label;
199 ntype.updatefunc = node_update;
207
208 node_rna(ntype.rna_ext.srna);
209}
211
212} // namespace blender::nodes::node_fn_boolean_math_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define FN_NODE_BOOLEAN_MATH
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:534
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 set_matching_fn(const mf::MultiFunction *fn)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:4739
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, std::string idname, const std::optional< int16_t > legacy_type)
const char * name
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
const EnumPropertyItem rna_enum_node_boolean_math_items[]
StructRNA * srna
int16_t custom1
ListBase inputs
Defines a node type.
Definition BKE_node.hh:238
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:403
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:410
std::string ui_description
Definition BKE_node.hh:244
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:270
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:397
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:281
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4238