Blender V5.0
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 "BKE_node_runtime.hh"
9
10#include "node_function_util.hh"
11
13
15
17
20 "STARTS_WITH",
21 0,
22 N_("Starts With"),
23 N_("True when the first input starts with the second")},
25 "ENDS_WITH",
26 0,
27 N_("Ends With"),
28 N_("True when the first input ends with the second")},
30 "CONTAINS",
31 0,
32 N_("Contains"),
33 N_("True when the first input contains the second as a substring")},
34 {0, nullptr, 0, nullptr, nullptr},
35};
36
38{
39 b.add_input<decl::String>("String").optional_label().is_default_link_socket();
40 b.add_input<decl::Menu>("Operation")
43 b.add_input<decl::String>("Key").optional_label().description(
44 "The string to find in the input string");
45 b.add_output<decl::Bool>("Result");
46}
47
49{
50 static auto fn = mf::build::SI3_SO<std::string, int, std::string, bool>(
51 "Starts With", [](const std::string &a, const int mode, const std::string &b) {
52 const StringRef strref_a(a);
53 const StringRef strref_b(b);
54 switch (MatchStringOperation(mode)) {
56 return strref_a.startswith(strref_b);
57 }
59 return strref_a.endswith(strref_b);
60 }
62 return strref_a.find(strref_b) != StringRef::not_found;
63 }
64 }
65 return false;
66 });
67 builder.set_matching_fn(&fn);
68}
69
71{
72 if (params.in_out() == SOCK_IN) {
73 if (params.node_tree().typeinfo->validate_link(eNodeSocketDatatype(params.other_socket().type),
75 {
77 item->identifier != nullptr;
78 item++)
79 {
80 if (item->name != nullptr && item->identifier[0] != '\0') {
81 MatchStringOperation operation = MatchStringOperation(item->value);
82 params.add_item(IFACE_(item->name), [operation](LinkSearchOpParams &params) {
83 bNode &node = params.add_node("FunctionNodeMatchString");
84 params.update_and_connect_available_socket(node, "String");
85 bke::node_find_socket(node, SOCK_IN, "Operation")
86 ->default_value_typed<bNodeSocketValueMenu>()
87 ->value = int(operation);
88 });
89 }
90 }
91 }
92 }
93
94 else {
95 params.add_item(IFACE_("Result"), [](LinkSearchOpParams &params) {
96 bNode &node = params.add_node("FunctionNodeMatchString");
97 params.update_and_connect_available_socket(node, "Result");
98 });
99 }
100}
101
102static void node_label(const bNodeTree * /*tree*/,
103 const bNode *node,
104 char *label,
105 int label_maxncpy)
106{
107 const char *name;
109 if (!enum_label) {
110 name = N_("Unknown");
111 }
112 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
113}
114
115static void node_register()
116{
117 static blender::bke::bNodeType ntype;
118
119 fn_node_type_base(&ntype, "FunctionNodeMatchString");
120 ntype.ui_name = "Match String";
121 ntype.ui_description = "Check if a given string exists within another string";
123 ntype.declare = node_declare;
124 ntype.labelfunc = node_label;
127
129}
131
132} // namespace blender::nodes::node_fn_match_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
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)
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:2416
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[]
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)
int16_t custom1
Defines a node type.
Definition BKE_node.hh:238
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
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
#define N_(msgid)