Blender V4.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
7#include "NOD_rna_define.hh"
9
10#include "UI_interface.hh"
11#include "UI_resources.hh"
12
13#include <iomanip>
14#include <sstream>
15#include <string>
16
18
20{
21 const bNode *node = b.node_or_null();
22
23 if (node != nullptr) {
24 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
25 b.add_input(data_type, "Value");
26
27 auto &decimals = b.add_input<decl::Int>("Decimals").min(0);
28 decimals.available(data_type == SOCK_FLOAT);
29 }
30
31 b.add_output<decl::String>("String");
32}
33
34static const mf::MultiFunction *get_multi_function(const bNode &bnode)
35{
36 static auto float_to_str_fn = mf::build::SI2_SO<float, int, std::string>(
37 "Value To String", [](float a, int b) {
38 std::stringstream stream;
39 stream << std::fixed << std::setprecision(std::max(0, b)) << a;
40 return stream.str();
41 });
42
43 static auto int_to_str_fn = mf::build::SI1_SO<int, std::string>(
44 "Value To String", [](int a) { return std::to_string(a); });
45
46 switch (bnode.custom1) {
47 case SOCK_FLOAT:
48 return &float_to_str_fn;
49 case SOCK_INT:
50 return &int_to_str_fn;
51 }
52
54 return nullptr;
55}
56
58{
59 const mf::MultiFunction *fn = get_multi_function(builder.node());
60 builder.set_matching_fn(fn);
61}
62
63static void node_init(bNodeTree * /*tree*/, bNode *node)
64{
65 node->custom1 = SOCK_FLOAT;
66}
67
69{
70 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(params.other_socket().type);
71 if (params.in_out() == SOCK_IN) {
72 if (socket_type == SOCK_INT) {
73 params.add_item(IFACE_("Value"), [](LinkSearchOpParams &params) {
74 bNode &node = params.add_node("FunctionNodeValueToString");
75 node.custom1 = SOCK_INT;
76 params.update_and_connect_available_socket(node, "Value");
77 });
78 params.add_item(IFACE_("Decimals"), [](LinkSearchOpParams &params) {
79 bNode &node = params.add_node("FunctionNodeValueToString");
80 params.update_and_connect_available_socket(node, "Decimals");
81 });
82 }
83 else {
84 if (params.node_tree().typeinfo->validate_link(socket_type, SOCK_FLOAT)) {
85 params.add_item(IFACE_("Value"), [](LinkSearchOpParams &params) {
86 bNode &node = params.add_node("FunctionNodeValueToString");
87 node.custom1 = SOCK_FLOAT;
88 params.update_and_connect_available_socket(node, "Value");
89 });
90 }
91 }
92 }
93 else {
94 if (socket_type == SOCK_STRING) {
95 params.add_item(IFACE_("String"), [](LinkSearchOpParams &params) {
96 bNode &node = params.add_node("FunctionNodeValueToString");
97 params.update_and_connect_available_socket(node, "String");
98 });
99 }
100 }
101}
102
103static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
104{
105 uiItemR(layout, ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
106}
107
108static void node_rna(StructRNA *srna)
109{
110 static const EnumPropertyItem data_types[] = {
111 {SOCK_FLOAT, "FLOAT", 0, "Float", "Floating-point value"},
112 {SOCK_INT, "INT", 0, "Integer", "32-bit integer"},
113 {0, nullptr, 0, nullptr, nullptr},
114 };
115
117 "data_type",
118 "Data Type",
119 "",
120 data_types,
122 SOCK_FLOAT);
123}
124
125static void node_register()
126{
127 static blender::bke::bNodeType ntype;
128
129 fn_node_type_base(&ntype, FN_NODE_VALUE_TO_STRING, "Value to String", NODE_CLASS_CONVERTER);
130 ntype.declare = node_declare;
131 ntype.initfunc = node_init;
136
137 node_rna(ntype.rna_ext.srna);
138}
140
141} // namespace blender::nodes::node_fn_value_to_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:410
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define IFACE_(msgid)
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_INT
@ SOCK_FLOAT
@ SOCK_STRING
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
void set_matching_fn(const mf::MultiFunction *fn)
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
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)
void fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
#define min(a, b)
Definition sort.c:32
StructRNA * srna
Definition RNA_types.hh:780
int16_t custom1
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:363
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126