Blender V4.5
node_fn_match_string.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_string_ref.hh"
6#include "BLI_string_utf8.h"
7
8#include "UI_interface.hh"
9
10#include "RNA_enum_types.hh"
11
12#include "node_function_util.hh"
13
14#include "NOD_rna_define.hh"
16
18
20
23 "STARTS_WITH",
24 0,
25 "Starts With",
26 "True when the first input starts with the second"},
28 "ENDS_WITH",
29 0,
30 "Ends With",
31 "True when the first input ends with the second"},
33 "CONTAINS",
34 0,
35 "Contains",
36 "True when the first input contains the second as a substring"},
37 {0, nullptr, 0, nullptr, nullptr},
38};
39
41{
42 b.add_input<decl::String>("String").hide_label();
43 b.add_input<decl::String>("Key").hide_label().description(
44 "The string to find in the input string");
45 b.add_output<decl::Bool>("Result");
46}
47
48static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
49{
50 layout->prop(ptr, "operation", UI_ITEM_NONE, "", ICON_NONE);
51}
52
53static void node_init(bNodeTree * /*tree*/, bNode *node)
54{
56}
57
58static const mf::MultiFunction *get_multi_function(const bNode &bnode)
59{
60 const MatchStringOperation operation = MatchStringOperation(bnode.custom1);
61
62 switch (operation) {
64 static auto fn = mf::build::SI2_SO<std::string, std::string, bool>(
65 "Starts With", [](const std::string &a, const std::string &b) {
66 const StringRef strref_a(a);
67 const StringRef strref_b(b);
68 return strref_a.startswith(strref_b);
69 });
70 return &fn;
71 }
73 static auto fn = mf::build::SI2_SO<std::string, std::string, bool>(
74 "Ends With", [](const std::string &a, const std::string &b) {
75 const StringRef strref_a(a);
76 const StringRef strref_b(b);
77 return strref_a.endswith(strref_b);
78 });
79 return &fn;
80 }
82 static auto fn = mf::build::SI2_SO<std::string, std::string, bool>(
83 "Contains", [](const std::string &a, const std::string &b) {
84 const StringRef strref_a(a);
85 const StringRef strref_b(b);
86 return strref_a.find(strref_b) != StringRef::not_found;
87 });
88 return &fn;
89 }
90 }
92 return nullptr;
93}
94
96{
97 const mf::MultiFunction *fn = get_multi_function(builder.node());
98 builder.set_matching_fn(fn);
99}
100
102{
103 if (params.in_out() == SOCK_IN) {
104 if (params.node_tree().typeinfo->validate_link(eNodeSocketDatatype(params.other_socket().type),
106 {
108 item->identifier != nullptr;
109 item++)
110 {
111 if (item->name != nullptr && item->identifier[0] != '\0') {
112 MatchStringOperation operation = MatchStringOperation(item->value);
113 params.add_item(IFACE_(item->name), [operation](LinkSearchOpParams &params) {
114 bNode &node = params.add_node("FunctionNodeMatchString");
115 node.custom1 = int8_t(operation);
116 params.update_and_connect_available_socket(node, "String");
117 });
118 }
119 }
120 }
121 }
122
123 else {
124 params.add_item(IFACE_("Result"), [](LinkSearchOpParams &params) {
125 bNode &node = params.add_node("FunctionNodeMatchString");
126 params.update_and_connect_available_socket(node, "Result");
127 });
128 }
129}
130
131static void node_label(const bNodeTree * /*tree*/,
132 const bNode *node,
133 char *label,
134 int label_maxncpy)
135{
136 const char *name;
137 bool enum_label = RNA_enum_name(rna_enum_node_match_string_items, node->custom1, &name);
138 if (!enum_label) {
139 name = "Unknown";
140 }
141 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
142}
143
144static void node_rna(StructRNA *srna)
145{
147 "operation",
148 "Operation",
149 "",
153}
154
155static void node_register()
156{
157 static blender::bke::bNodeType ntype;
158
159 fn_node_type_base(&ntype, "FunctionNodeMatchString");
160 ntype.ui_name = "Match String";
162 ntype.declare = node_declare;
163 ntype.labelfunc = node_label;
165 ntype.initfunc = node_init;
168
170 node_rna(ntype.rna_ext.srna);
171}
173
174} // namespace blender::nodes::node_fn_match_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:439
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
#define IFACE_(msgid)
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_STRING
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
static constexpr int64_t not_found
constexpr int64_t find(char c, int64_t pos=0) const
constexpr bool startswith(StringRef prefix) const
constexpr bool endswith(StringRef suffix) const
void set_matching_fn(const mf::MultiFunction *fn)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
const EnumPropertyItem rna_enum_node_match_string_items[]
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
StructRNA * srna
Definition RNA_types.hh:909
int16_t custom1
Defines a node type.
Definition BKE_node.hh:226
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:258
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:344
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:371
NodeDeclareFunction declare
Definition BKE_node.hh:355
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)
PointerRNA * ptr
Definition wm_files.cc:4226