Blender V5.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.hh"
6#include "BLI_string_utf8.hh"
7
9
10#include "RNA_enum_types.hh"
11
13#include "UI_resources.hh"
14
18
19#include "NOD_rna_define.hh"
20
21#include "node_function_util.hh"
22#include "node_shader_util.hh"
23
25
27{
28 b.is_function_node();
29 b.add_input<decl::Bool>("Boolean"_ustr, "Boolean"_ustr);
30
31 const bNode *node = b.node_or_null();
32 if (node != nullptr) {
33 const auto type = NodeBooleanMathOperation(node->custom1);
34 if (type != NODE_BOOLEAN_MATH_NOT) {
35 b.add_input<decl::Bool>("Boolean"_ustr, "Boolean_001"_ustr);
36 }
37 }
38 b.add_output<decl::Bool>("Boolean"_ustr);
39}
40
41static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
42{
43 layout.prop(ptr, "operation", UI_ITEM_NONE, "", ICON_NONE);
44}
45
46static void node_label(const bNodeTree * /*tree*/,
47 const bNode *node,
48 char *label,
49 int label_maxncpy)
50{
51 const char *name;
52 bool enum_label = RNA_enum_name(rna_enum_node_boolean_math_items, node->custom1, &name);
53 if (!enum_label) {
54 name = N_("Unknown");
55 }
56 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
57}
58
60{
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"_ustr);
73 node.custom1 = operation;
74 params.update_and_connect_available_socket(node, "Boolean"_ustr);
75 });
76 }
77 }
78}
79
80static const mf::MultiFunction *get_multi_function(const bNode &bnode)
81{
82 switch (bnode.custom1) {
84 return &fn::multi_function::registry::lookup("bool && bool"_ustr);
86 return &fn::multi_function::registry::lookup("bool || bool"_ustr);
88 return &fn::multi_function::registry::lookup("!bool"_ustr);
90 return &fn::multi_function::registry::lookup("!(bool && bool)"_ustr);
92 return &fn::multi_function::registry::lookup("!(bool || bool)"_ustr);
94 return &fn::multi_function::registry::lookup("bool == bool"_ustr);
96 return &fn::multi_function::registry::lookup("bool != bool"_ustr);
98 return &fn::multi_function::registry::lookup("!bool || bool"_ustr);
100 return &fn::multi_function::registry::lookup("bool && !bool"_ustr);
101 }
102
104 return nullptr;
105}
106
108{
109 const mf::MultiFunction *fn = get_multi_function(builder.node());
110 builder.set_matching_fn(fn);
111}
112
114{
115 using namespace value_elem;
117 switch (op) {
119 params.set_output_elem("Boolean"_ustr, params.get_input_elem<BoolElem>("Boolean"_ustr));
120 break;
121 }
122 default: {
123 break;
124 }
125 }
126}
127
129{
130 using namespace value_elem;
132 switch (op) {
134 params.set_input_elem("Boolean"_ustr, params.get_output_elem<BoolElem>("Boolean"_ustr));
135 break;
136 }
137 default: {
138 break;
139 }
140 }
141}
142
143static const char *gpu_shader_get_name(const NodeBooleanMathOperation operation)
144{
145 switch (operation) {
147 return "boolean_math_and";
149 return "boolean_math_or";
151 return "boolean_math_not";
153 return "boolean_math_nand";
155 return "boolean_math_nor";
157 return "boolean_math_xnor";
159 return "boolean_math_xor";
161 return "boolean_math_imply";
163 return "boolean_math_nimply";
164 }
165
167 return nullptr;
168}
169
171 bNode *node,
172 bNodeExecData * /*execdata*/,
175{
176 const char *name = gpu_shader_get_name(NodeBooleanMathOperation(node->custom1));
177
178 if (name == nullptr) {
179 return 0;
180 }
181
182 return GPU_stack_link(mat, node, name, in, out);
183}
184
186{
188 const UString first_input_id = "Boolean"_ustr;
189 const UString output_id = "Boolean"_ustr;
190 switch (op) {
192 params.set_input(first_input_id, !params.get_output<bool>(output_id));
193 break;
194 }
195 default: {
196 break;
197 }
198 }
199}
200
201static void node_rna(StructRNA *srna)
202{
204 "operation",
205 "Operation",
206 "",
209}
210
211static void node_register()
212{
213 static bke::bNodeType ntype;
214
215 common_node_type_base(&ntype, "FunctionNodeBooleanMath"_ustr, FN_NODE_BOOLEAN_MATH);
216 ntype.ui_name = "Boolean Math";
217 ntype.ui_description = "Perform a logical operation on the given boolean inputs";
218 ntype.enum_name_legacy = "BOOLEAN_MATH";
220 ntype.declare = node_declare;
221 ntype.labelfunc = node_label;
230
231 node_rna(ntype.rna_ext.srna);
232}
234
235} // namespace blender::nodes::node_fn_boolean_math_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_BOOLEAN_MATH
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void set_matching_fn(const mf::MultiFunction *fn)
void set_input(const UString identifier, T value)
T get_input_elem(const UString identifier) const
void set_output_elem(const UString identifier, T elem)
void set_input_elem(const UString identifier, T elem)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
const MultiFunction & lookup(UString id)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
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_eval_inverse_elem(value_elem::InverseElemEvalParams &params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static const char * gpu_shader_get_name(const NodeBooleanMathOperation operation)
static void node_eval_elem(value_elem::ElemEvalParams &params)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
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)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
@ NODE_BOOLEAN_MATH_XOR
@ NODE_BOOLEAN_MATH_NIMPLY
@ NODE_BOOLEAN_MATH_NAND
@ NODE_BOOLEAN_MATH_AND
@ NODE_BOOLEAN_MATH_IMPLY
@ NODE_BOOLEAN_MATH_OR
@ NODE_BOOLEAN_MATH_NOT
@ NODE_BOOLEAN_MATH_NOR
@ NODE_BOOLEAN_MATH_XNOR
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
const EnumPropertyItem rna_enum_node_boolean_math_items[]
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:434
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:441
std::string ui_description
Definition BKE_node.hh:271
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:428
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
#define N_(msgid)