Blender V5.3
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.hh"
7
8#include "BKE_node_runtime.hh"
9
10#include "node_function_util.hh"
11#include "node_shader_util.hh"
12
14
16
18
21 "STARTS_WITH",
22 0,
23 N_("Starts With"),
24 N_("True when the first input starts with the second")},
26 "ENDS_WITH",
27 0,
28 N_("Ends With"),
29 N_("True when the first input ends with the second")},
31 "CONTAINS",
32 0,
33 N_("Contains"),
34 N_("True when the first input contains the second as a substring")},
35 {0, nullptr, 0, nullptr, nullptr},
36};
37
39{
40 b.is_function_node();
41 b.add_input<decl::String>("String"_ustr).optional_label().is_default_link_socket();
42 b.add_input<decl::Menu>("Operation"_ustr)
45 b.add_input<decl::String>("Key"_ustr)
46 .optional_label()
47 .description("The string to find in the input string");
48 b.add_output<decl::Bool>("Result"_ustr);
49}
50
52{
53 static auto fn = mf::build::SI3_SO<std::string, MenuValue, std::string, bool>(
54 "Starts With", [](const std::string &a, const MenuValue mode, const std::string &b) {
55 const StringRef strref_a(a);
56 const StringRef strref_b(b);
57 switch (MatchStringOperation(mode.value)) {
59 return strref_a.startswith(strref_b);
60 }
62 return strref_a.endswith(strref_b);
63 }
65 return strref_a.find(strref_b) != StringRef::not_found;
66 }
67 }
68 return false;
69 });
70 builder.set_matching_fn(&fn);
71}
72
74{
75 if (params.in_out() == SOCK_IN) {
78 item->identifier != nullptr;
79 item++)
80 {
81 if (item->name != nullptr && item->identifier[0] != '\0') {
82 MatchStringOperation operation = MatchStringOperation(item->value);
83 params.add_item(IFACE_(item->name), [operation](LinkSearchOpParams &params) {
84 bNode &node = params.add_node("FunctionNodeMatchString"_ustr);
85 params.update_and_connect_available_socket(node, "String"_ustr);
86 bke::node_find_socket(node, SOCK_IN, "Operation"_ustr)
87 ->default_value_typed<bNodeSocketValueMenu>()
88 ->value = int(operation);
89 });
90 }
91 }
92 }
93 }
94
95 else {
96 params.add_item(IFACE_("Result"), [](LinkSearchOpParams &params) {
97 bNode &node = params.add_node("FunctionNodeMatchString"_ustr);
98 params.update_and_connect_available_socket(node, "Result"_ustr);
99 });
100 }
101}
102
103static void node_label(const bNodeTree * /*tree*/,
104 const bNode *node,
105 char *label,
106 int label_maxncpy)
107{
108 const char *name;
109 bool enum_label = RNA_enum_name(rna_enum_node_match_string_items, node->custom1, &name);
110 if (!enum_label) {
111 name = N_("Unknown");
112 }
113 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
114}
115
116static void node_register()
117{
118 static bke::bNodeType ntype;
119
120 common_node_type_base(&ntype, "FunctionNodeMatchString"_ustr);
121 ntype.ui_name = "Match String";
122 ntype.ui_description = "Check if a given string exists within another string";
124 ntype.declare = node_declare;
125 ntype.labelfunc = node_label;
128
130}
132
133} // namespace blender::nodes::node_fn_match_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr bool startswith(StringRef prefix) const
static constexpr int64_t not_found
constexpr int64_t find(char c, int64_t pos=0) const
constexpr bool endswith(StringRef suffix) const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void set_matching_fn(const mf::MultiFunction *fn)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_declare(NodeDeclarationBuilder &b)
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[]
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
signed char int8_t
Definition BKE_text.h:76
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
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
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)