Blender V4.5
socket_search_link.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
5#include <fmt/format.h>
6
7#include "BLI_set.hh"
8
9#include "BKE_context.hh"
10#include "BKE_node.hh"
11
12#include "UI_interface.hh"
13
14#include "BLT_translation.hh"
15
17#include "NOD_socket.hh"
19
20namespace blender::nodes {
21
22void GatherLinkSearchOpParams::add_item(std::string socket_name,
24 const int weight)
25{
26 std::string name = fmt::format("{}{} " UI_MENU_ARROW_SEP " {}",
27 IFACE_(node_type_.ui_name),
28 node_type_.deprecation_notice ? IFACE_(" (Deprecated)") : "",
29 socket_name);
30 this->add_item_full_name(std::move(name), std::move(fn), weight);
31}
32
35 int weight)
36{
37 items_.append({std::move(name), std::move(fn), weight});
38}
39
41{
42 return other_socket_;
43}
44
46{
47 return snode_;
48}
49
51{
52 return node_tree_;
53}
54
56{
57 return node_type_;
58}
59
61{
62 return other_socket_.in_out == SOCK_IN ? SOCK_OUT : SOCK_IN;
63}
64
66{
67 const eNodeSocketInOut in_out = socket.in_out == SOCK_IN ? SOCK_OUT : SOCK_IN;
68 bNodeSocket *new_node_socket = bke::node_find_enabled_socket(new_node, in_out, socket_name);
69 if (new_node_socket == nullptr) {
70 /* If the socket isn't found, some node's search gather functions probably aren't configured
71 * properly. It's likely enough that it's worth avoiding a crash in a release build though. */
73 return;
74 }
75 bke::node_add_link(node_tree, new_node, *new_node_socket, node, socket);
76 if (in_out == SOCK_OUT) {
77 /* If the old socket already contained a value, then transfer it to a new one, from
78 * which this value will get there. */
80 }
81}
82
84{
85 std::string idname_str = idname;
86 bNode *node = bke::node_add_node(&C, node_tree, idname_str.c_str());
87 BLI_assert(node != nullptr);
88 added_nodes_.append(node);
89 return *node;
90}
91
93{
94 return this->add_node(node_type.idname);
95}
96
98 StringRef socket_name)
99{
101 if (new_node.typeinfo->updatefunc) {
102 new_node.typeinfo->updatefunc(&node_tree, &new_node);
103 }
104 this->connect_available_socket(new_node, socket_name);
105}
106
108 Span<SocketDeclaration *> declarations)
109{
110 const bke::bNodeType &node_type = params.node_type();
111
112 const SocketDeclaration *main_socket = nullptr;
113 Vector<const SocketDeclaration *> connectable_sockets;
114
115 Set<StringRef> socket_names;
116 for (const int i : declarations.index_range()) {
117 const SocketDeclaration &socket = *declarations[i];
118 if (!socket_names.add(socket.name)) {
119 /* Don't add sockets with the same name to the search. Needed to support being called from
120 * #search_link_ops_for_basic_node, which should have "okay" behavior for nodes with
121 * duplicate socket names. */
122 continue;
123 }
124 if (!socket.can_connect(params.other_socket())) {
125 continue;
126 }
127 if (socket.is_default_link_socket || main_socket == nullptr) {
128 /* Either the first connectable or explicitly tagged socket is the main socket. */
129 main_socket = &socket;
130 }
131 connectable_sockets.append(&socket);
132 }
133 for (const int i : connectable_sockets.index_range()) {
134 const SocketDeclaration &socket = *connectable_sockets[i];
135 /* Give non-main sockets a lower weight so that they don't show up at the top of the search
136 * when they are not explicitly searched for. The -1 is used to make sure that the first socket
137 * has a smaller weight than zero so that it does not have the same weight as the main socket.
138 * Negative weights are used to avoid making the highest weight dependent on the number of
139 * sockets. */
140 const int weight = (&socket == main_socket) ? 0 : -1 - i;
141 params.add_item(
142 IFACE_(socket.name),
143 [&node_type, &socket](LinkSearchOpParams &params) {
144 bNode &node = params.add_node(node_type);
145 socket.make_available(node);
146 params.update_and_connect_available_socket(node, socket.name);
147 },
148 weight);
149 }
150}
151
153{
154 const bke::bNodeType &node_type = params.node_type();
155 if (!node_type.static_declaration) {
156 return;
157 }
158 const NodeDeclaration &declaration = *node_type.static_declaration;
160}
161
162} // namespace blender::nodes
Main * CTX_data_main(const bContext *C)
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define IFACE_(msgid)
eNodeSocketInOut
@ SOCK_OUT
@ SOCK_IN
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
void append(const T &value)
IndexRange index_range() const
const bke::bNodeType & node_type() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void add_item_full_name(std::string name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void connect_available_socket(bNode &new_node, StringRef socket_name)
void update_and_connect_available_socket(bNode &new_node, StringRef socket_name)
Span< SocketDeclaration * > sockets(eNodeSocketInOut in_out) const
virtual bool can_connect(const bNodeSocket &socket) const =0
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
bNode * node_add_node(const bContext *C, bNodeTree &ntree, StringRef idname)
Definition node.cc:3788
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4087
bNodeSocket * node_find_enabled_socket(bNode &node, eNodeSocketInOut in_out, StringRef name)
Definition node.cc:2885
void node_socket_move_default_value(Main &bmain, bNodeTree &tree, bNodeSocket &src, bNodeSocket &dst)
Definition node.cc:4010
void search_link_ops_for_basic_node(GatherLinkSearchOpParams &params)
void search_link_ops_for_declarations(GatherLinkSearchOpParams &params, Span< SocketDeclaration * > declarations)
void update_node_declaration_and_sockets(bNodeTree &ntree, bNode &node)
#define UI_MENU_ARROW_SEP
bNodeTypeHandle * typeinfo
Defines a node type.
Definition BKE_node.hh:226
blender::nodes::NodeDeclaration * static_declaration
Definition BKE_node.hh:364
std::function< void(LinkSearchOpParams &link_params)> LinkSocketFn
i
Definition text_draw.cc:230