Blender V5.3
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#include "node_shader_util.hh"
7
9
10#include "BLT_translation.hh"
11
12#include "UI_interface.hh"
14#include "UI_resources.hh"
15
16#include "BLO_read_write.hh"
17
18#include "BLF_api.hh"
19
21
23{
24 b.add_output<decl::String>("String"_ustr).custom_draw([](CustomSocketDrawParams &params) {
27 &params.node_ptr,
28 "string",
29 RNA_pointer_get(&params.node_ptr, "textbox_state").data_as<TextboxState>(),
30 "",
31 IFACE_("String"));
32 });
33}
34
36{
37 const bNode &bnode = builder.node();
38 NodeInputString *node_storage = static_cast<NodeInputString *>(bnode.storage);
39 std::string string = std::string((node_storage->string) ? node_storage->string : "");
40 builder.construct_and_set_matching_fn<mf::CustomMF_Constant<std::string>>(std::move(string));
41}
42
43static void node_init(bNodeTree * /*tree*/, bNode *node)
44{
45 NodeInputString *storage = MEM_new<NodeInputString>(__func__);
46 storage->textbox_state.visible_lines = 1;
47 node->storage = storage;
48}
49
51{
52 NodeInputString *storage = static_cast<NodeInputString *>(node->storage);
53 if (storage == nullptr) {
54 return;
55 }
56 if (storage->string != nullptr) {
57 MEM_delete(storage->string);
58 }
59 MEM_delete(storage);
60}
61
62static void node_storage_copy(bNodeTree * /*dst_ntree*/, bNode *dest_node, const bNode *src_node)
63{
64 NodeInputString *source_storage = static_cast<NodeInputString *>(src_node->storage);
65 NodeInputString *destination_storage = static_cast<NodeInputString *>(
66 MEM_dupalloc(source_storage));
67
68 if (source_storage->string) {
69 destination_storage->string = MEM_dupalloc(source_storage->string);
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 writer.write_string(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 = 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"_ustr);
98 params.update_and_connect_available_socket(node, "String"_ustr);
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 bke::bNodeType ntype;
112
113 common_node_type_base(&ntype, "FunctionNodeInputString"_ustr, FN_NODE_INPUT_STRING);
114 ntype.ui_name = "String";
115 ntype.ui_description = "Provide a string value that can be connected to other nodes in the tree";
116 ntype.enum_name_legacy = "INPUT_STRING";
117 ntype.nclass = NODE_CLASS_INPUT;
118 ntype.declare = node_declare;
119 ntype.initfunc = node_init;
126}
128
129} // namespace blender::nodes::node_fn_input_string_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define FN_NODE_INPUT_STRING
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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:6166
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)
const uiStyle * style_get()
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:835
void BLF_size(int fontid, float size)
Definition blf.cc:445
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void write_string(const char *data)
T * data_as() const
Definition RNA_types.hh:139
eNodeSocketDatatype type
eNodeSocketInOut in_out
Defines a node type.
Definition BKE_node.hh:265
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
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 alignment_set(LayoutAlign alignment)
void textbox_with_state(PointerRNA *ptr, StringRefNull propname, TextboxState *textbox_state, std::optional< StringRefNull > name_opt=std::nullopt, std::optional< StringRefNull > placeholder=std::nullopt)
uiFontStyle widget