Blender V4.5
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"
12#include "UI_resources.hh"
13
14#include "BLO_read_write.hh"
15
16#include "BLF_api.hh"
17
19
21{
22 b.is_function_node();
23 b.add_output<decl::String>("String").custom_draw([](CustomSocketDrawParams &params) {
25 PropertyRNA *prop = RNA_struct_find_property(&params.node_ptr, "string");
26 params.layout.prop(&params.node_ptr,
27 prop,
28 -1,
29 0,
31 "",
32 ICON_NONE,
33 IFACE_("String"));
34 });
35}
36
38{
39 const bNode &bnode = builder.node();
40 NodeInputString *node_storage = static_cast<NodeInputString *>(bnode.storage);
41 std::string string = std::string((node_storage->string) ? node_storage->string : "");
42 builder.construct_and_set_matching_fn<mf::CustomMF_Constant<std::string>>(std::move(string));
43}
44
45static void node_init(bNodeTree * /*tree*/, bNode *node)
46{
47 node->storage = MEM_callocN<NodeInputString>(__func__);
48}
49
50static void node_storage_free(bNode *node)
51{
52 NodeInputString *storage = (NodeInputString *)node->storage;
53 if (storage == nullptr) {
54 return;
55 }
56 if (storage->string != nullptr) {
57 MEM_freeN(storage->string);
58 }
59 MEM_freeN(storage);
60}
61
62static void node_storage_copy(bNodeTree * /*dst_ntree*/, bNode *dest_node, const bNode *src_node)
63{
64 NodeInputString *source_storage = (NodeInputString *)src_node->storage;
65 NodeInputString *destination_storage = (NodeInputString *)MEM_dupallocN(source_storage);
66
67 if (source_storage->string) {
68 destination_storage->string = (char *)MEM_dupallocN(source_storage->string);
69 }
70
71 dest_node->storage = destination_storage;
72}
73
74static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
75{
76 const NodeInputString *storage = static_cast<const NodeInputString *>(node.storage);
77 BLO_write_string(&writer, storage->string);
78}
79
80static void node_blend_read(bNodeTree & /*tree*/, bNode &node, BlendDataReader &reader)
81{
82 NodeInputString *storage = static_cast<NodeInputString *>(node.storage);
83 BLO_read_string(&reader, &storage->string);
84}
85
87{
88 const eNodeSocketDatatype type = eNodeSocketDatatype(params.other_socket().type);
89 if (type != SOCK_STRING) {
90 return;
91 }
92 if (params.other_socket().in_out == SOCK_OUT) {
93 return;
94 }
95
96 params.add_item(IFACE_("String"), [](LinkSearchOpParams &params) {
97 bNode &node = params.add_node("FunctionNodeInputString");
98 params.update_and_connect_available_socket(node, "String");
99
100 /* Adapt width of the new node to its content. */
101 const StringRef string = static_cast<NodeInputString *>(node.storage)->string;
102 const uiFontStyle &fstyle = UI_style_get()->widget;
103 BLF_size(fstyle.uifont_id, fstyle.points);
104 const float width = BLF_width(fstyle.uifont_id, string.data(), string.size()) + 40.0f;
105 node.width = std::clamp(width, 140.0f, 1000.0f);
106 });
107}
108
109static void node_register()
110{
111 static blender::bke::bNodeType ntype;
112
113 fn_node_type_base(&ntype, "FunctionNodeInputString", FN_NODE_INPUT_STRING);
114 ntype.ui_name = "String";
115 ntype.enum_name_legacy = "INPUT_STRING";
116 ntype.nclass = NODE_CLASS_INPUT;
117 ntype.declare = node_declare;
118 ntype.initfunc = node_init;
125}
127
128} // namespace blender::nodes::node_fn_input_string_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:433
#define FN_NODE_INPUT_STRING
void BLF_size(int fontid, float size)
Definition blf.cc:440
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:805
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:5351
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
@ UI_LAYOUT_ALIGN_EXPAND
void uiLayoutSetAlignment(uiLayout *layout, char alignment)
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:2748
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:5603
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:226
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:383
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:384
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:344
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:371
NodeDeclareFunction declare
Definition BKE_node.hh:355
uiFontStyle widget