Blender V5.3
node_fn_value_to_string.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
6#include "node_shader_util.hh"
7
8#include "NOD_rna_define.hh"
10
12#include "UI_resources.hh"
13
14#include <algorithm>
15#include <charconv>
16#include <iomanip>
17#include <sstream>
18#include <string>
19
21
23{
24 b.is_function_node();
25 const bNode *node = b.node_or_null();
26
27 if (node != nullptr) {
28 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
29 b.add_input(data_type, "Value"_ustr);
30
31 auto &decimals = b.add_input<decl::Int>("Decimals"_ustr).min(0);
32 decimals.available(data_type == SOCK_FLOAT);
33
34 b.add_input<decl::Int>("Base"_ustr)
35 .min(2)
36 .max(36)
37 .default_value(10)
38 .description("Numeric base for the output string (e.g. 2 for binary, 16 for hexadecimal)")
39 .available(data_type == SOCK_INT);
40
41 b.add_input<decl::Int>("Padding"_ustr)
42 .min(0)
43 .default_value(0)
44 .description("Minimum number of characters in the output, zero-padded if shorter")
45 .available(data_type == SOCK_INT);
46 }
47
48 b.add_output<decl::String>("String"_ustr);
49}
50
51static const mf::MultiFunction *get_multi_function(const bNode &bnode)
52{
53 static auto float_to_str_fn = mf::build::SI2_SO<float, int, std::string>(
54 "Value To String", [](float a, int b) {
55 std::stringstream stream;
56 stream << std::fixed << std::setprecision(std::max(0, b)) << a;
57 return stream.str();
58 });
59
60 static auto int_to_str_fn = mf::build::SI3_SO<int, int, int, std::string>(
61 "Value To String", [](int value, int base, int padding) -> std::string {
62 if (base < 2 || base > 36) {
63 return {};
64 }
65 padding = std::max(0, padding);
66 /* Maximum possible string length is reached with -2^31=-2147483648 and base 2. */
67 char buf[33];
68 auto [ptr, ec] = std::to_chars(buf, buf + sizeof(buf), value, base);
69 std::string result(buf, ptr);
70 if (padding > int(result.size())) {
71 const size_t needed = size_t(padding) - result.size();
72 const size_t insert_pos = (!result.empty() && result[0] == '-') ? 1 : 0;
73 result.insert(insert_pos, needed, '0');
74 }
75 return result;
76 });
77
78 switch (bnode.custom1) {
79 case SOCK_FLOAT:
80 return &float_to_str_fn;
81 case SOCK_INT:
82 return &int_to_str_fn;
83 }
84
86 return nullptr;
87}
88
90{
91 const mf::MultiFunction *fn = get_multi_function(builder.node());
92 builder.set_matching_fn(fn);
93}
94
95static void node_init(bNodeTree * /*tree*/, bNode *node)
96{
97 node->custom1 = SOCK_FLOAT;
98}
99
101{
102 const eNodeSocketDatatype socket_type = params.other_socket().type;
103 if (params.in_out() == SOCK_IN) {
104 if (socket_type == SOCK_INT) {
105 params.add_item(IFACE_("Value"), [](LinkSearchOpParams &params) {
106 bNode &node = params.add_node("FunctionNodeValueToString"_ustr);
107 node.custom1 = SOCK_INT;
108 params.update_and_connect_available_socket(node, "Value"_ustr);
109 });
110 params.add_item(IFACE_("Decimals"), [](LinkSearchOpParams &params) {
111 bNode &node = params.add_node("FunctionNodeValueToString"_ustr);
112 params.update_and_connect_available_socket(node, "Decimals"_ustr);
113 });
114 }
115 else {
116 if (params.node_tree().typeinfo->validate_link(socket_type, SOCK_FLOAT)) {
117 params.add_item(IFACE_("Value"), [](LinkSearchOpParams &params) {
118 bNode &node = params.add_node("FunctionNodeValueToString"_ustr);
119 node.custom1 = SOCK_FLOAT;
120 params.update_and_connect_available_socket(node, "Value"_ustr);
121 });
122 }
123 }
124 }
125 else {
126 if (socket_type == SOCK_STRING) {
127 params.add_item(IFACE_("String"), [](LinkSearchOpParams &params) {
128 bNode &node = params.add_node("FunctionNodeValueToString"_ustr);
129 params.update_and_connect_available_socket(node, "String"_ustr);
130 });
131 }
132 }
133}
134
135static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
136{
137 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
138}
139
140static void node_rna(StructRNA *srna)
141{
142 static const EnumPropertyItem data_types[] = {
143 {SOCK_FLOAT, "FLOAT", ICON_NODE_SOCKET_FLOAT, "Float", "Floating-point value"},
144 {SOCK_INT, "INT", ICON_NODE_SOCKET_INT, "Integer", "32-bit integer"},
145 {0, nullptr, 0, nullptr, nullptr},
146 };
147
149 "data_type",
150 "Data Type",
151 "",
152 data_types,
154 SOCK_FLOAT);
155}
156
157static void node_register()
158{
159 static bke::bNodeType ntype;
160
161 common_node_type_base(&ntype, "FunctionNodeValueToString"_ustr, FN_NODE_VALUE_TO_STRING);
162 ntype.ui_name = "Value to String";
163 ntype.ui_description = "Generate a string representation of the given input value";
164 ntype.enum_name_legacy = "VALUE_TO_STRING";
166 ntype.declare = node_declare;
167 ntype.initfunc = node_init;
172
173 node_rna(ntype.rna_ext.srna);
174}
176
177} // namespace blender::nodes::node_fn_value_to_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_VALUE_TO_STRING
#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 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_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
static void node_init(bNodeTree *, bNode *node)
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)
uint padding(uint offset, uint alignment)
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
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
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
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)