Blender V5.0
node_fn_input_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
8
9#include "BLT_translation.hh"
10
11#include "UI_interface.hh"
13#include "UI_resources.hh"
14
15#include "BLO_read_write.hh"
16
17#include "BLF_api.hh"
18
20
22{
23 b.is_function_node();
24 b.add_output<decl::String>("String").custom_draw([](CustomSocketDrawParams &params) {
25 params.layout.alignment_set(ui::LayoutAlign::Expand);
26 PropertyRNA *prop = RNA_struct_find_property(&params.node_ptr, "string");
27 params.layout.prop(&params.node_ptr,
28 prop,
29 -1,
30 0,
32 "",
33 ICON_NONE,
34 IFACE_("String"));
35 });
36}
37
39{
40 const bNode &bnode = builder.node();
41 NodeInputString *node_storage = static_cast<NodeInputString *>(bnode.storage);
42 std::string string = std::string((node_storage->string) ? node_storage->string : "");
43 builder.construct_and_set_matching_fn<mf::CustomMF_Constant<std::string>>(std::move(string));
44}
45
46static void node_init(bNodeTree * /*tree*/, bNode *node)
47{
48 node->storage = MEM_callocN<NodeInputString>(__func__);
49}
50
51static void node_storage_free(bNode *node)
52{
53 NodeInputString *storage = (NodeInputString *)node->storage;
54 if (storage == nullptr) {
55 return;
56 }
57 if (storage->string != nullptr) {
58 MEM_freeN(storage->string);
59 }
60 MEM_freeN(storage);
61}
62
63static void node_storage_copy(bNodeTree * /*dst_ntree*/, bNode *dest_node, const bNode *src_node)
64{
65 NodeInputString *source_storage = (NodeInputString *)src_node->storage;
66 NodeInputString *destination_storage = (NodeInputString *)MEM_dupallocN(source_storage);
67
68 if (source_storage->string) {
69 destination_storage->string = (char *)MEM_dupallocN(source_storage->string);
70 }
71
72 dest_node->storage = destination_storage;
73}
74
75static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
76{
77 const NodeInputString *storage = static_cast<const NodeInputString *>(node.storage);
78 BLO_write_string(&writer, storage->string);
79}
80
81static void node_blend_read(bNodeTree & /*tree*/, bNode &node, BlendDataReader &reader)
82{
83 NodeInputString *storage = static_cast<NodeInputString *>(node.storage);
84 BLO_read_string(&reader, &storage->string);
85}
86
88{
89 const eNodeSocketDatatype type = eNodeSocketDatatype(params.other_socket().type);
90 if (type != SOCK_STRING) {
91 return;
92 }
93 if (params.other_socket().in_out == SOCK_OUT) {
94 return;
95 }
96
97 params.add_item(IFACE_("String"), [](LinkSearchOpParams &params) {
98 bNode &node = params.add_node("FunctionNodeInputString");
99 params.update_and_connect_available_socket(node, "String");
100
101 /* Adapt width of the new node to its content. */
102 const StringRef string = static_cast<NodeInputString *>(node.storage)->string;
103 const uiFontStyle &fstyle = UI_style_get()->widget;
104 BLF_size(fstyle.uifont_id, fstyle.points);
105 const float width = BLF_width(fstyle.uifont_id, string.data(), string.size()) + 40.0f;
106 node.width = std::clamp(width, 140.0f, 1000.0f);
107 });
108}
109
110static void node_register()
111{
112 static blender::bke::bNodeType ntype;
113
114 fn_node_type_base(&ntype, "FunctionNodeInputString", FN_NODE_INPUT_STRING);
115 ntype.ui_name = "String";
116 ntype.ui_description = "Provide a string value that can be connected to other nodes in the tree";
117 ntype.enum_name_legacy = "INPUT_STRING";
118 ntype.nclass = NODE_CLASS_INPUT;
119 ntype.declare = node_declare;
120 ntype.initfunc = node_init;
127}
129
130} // namespace blender::nodes::node_fn_input_string_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define FN_NODE_INPUT_STRING
void BLF_size(int fontid, float size)
Definition blf.cc:443
float BLF_width(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:802
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:5828
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
#define IFACE_(msgid)
@ SOCK_OUT
eNodeSocketDatatype
@ SOCK_STRING
#define NOD_REGISTER_NODE(REGISTER_FUNC)
const uiStyle * UI_style_get()
@ UI_ITEM_R_SPLIT_EMPTY_NAME
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:5414
static void node_init(bNodeTree *, bNode *node)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_storage_copy(bNodeTree *, bNode *dest_node, const bNode *src_node)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_declare(NodeDeclarationBuilder &b)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
void fn_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
float width
void * storage
Defines a node type.
Definition BKE_node.hh:238
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:390
std::string ui_description
Definition BKE_node.hh:244
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:391
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
uiFontStyle widget