Blender V5.3
NOD_socket_items_ops.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLT_translation.hh"
12
13#include "NOD_rna_define.hh"
14#include "NOD_socket_items.hh"
15
16#include "WM_api.hh"
17
18#include "BKE_context.hh"
19#include "BKE_library.hh"
23
24#include "RNA_access.hh"
25#include "RNA_define.hh"
26#include "RNA_enum_types.hh"
27#include "RNA_prototypes.hh"
28
29#include "ED_node.hh"
30
31#include "UI_interface.hh"
32#include "UI_resources.hh"
33
34#include "DNA_space_types.h"
35
37
39 wmOperator *op,
40 const StringRef node_idname)
41{
43 if (!snode) {
44 return {};
45 }
46 if (!snode->edittree) {
47 return {};
48 }
49 if (!ID_IS_EDITABLE(snode->edittree)) {
50 return {};
51 }
52
53 bNode *node = nullptr;
54 if (RNA_struct_property_is_set(op->ptr, "node_identifier")) {
55 const int id = RNA_int_get(op->ptr, "node_identifier");
56 node = snode->edittree->node_by_id(id);
57 }
58 else {
60 }
61 if (!node) {
62 return {};
63 }
64
65 if (bke::zone_type_by_node_type(node->type_legacy) != nullptr) {
66 const bke::bNodeTreeZones *zones = snode->edittree->zones();
67 if (!zones) {
68 return {};
69 }
70 if (const bke::bNodeTreeZone *zone = zones->get_zone_by_node(node->identifier)) {
71 if (zone->input_node() == node) {
72 /* Assume the data is generally stored on the output and not the input node. */
73 node = const_cast<bNode *>(zone->output_node());
74 }
75 }
76 }
77
78 if (node->idname != node_idname) {
79 return {};
80 }
81 return RNA_pointer_create_discrete(&snode->edittree->id, RNA_Node, node);
82}
83
84inline void update_after_node_change(bContext *C, const PointerRNA node_ptr)
85{
86 bNode *node = static_cast<bNode *>(node_ptr.data);
87 bNodeTree *ntree = reinterpret_cast<bNodeTree *>(node_ptr.owner_id);
88
92}
93
94template<typename Accessor> inline bool editable_node_active_poll(bContext *C)
95{
97 if (!snode) {
98 return false;
99 }
100 if (!snode->edittree) {
101 return false;
102 }
103 if (!ID_IS_EDITABLE(snode->edittree)) {
104 return false;
105 }
106 return true;
107}
108
110{
111 RNA_def_int(ot->srna,
112 "node_identifier",
113 0,
114 0,
115 INT32_MAX,
116 "Node Identifier",
117 "Optional identifier of the node to operate on",
118 0,
119 INT32_MAX);
120}
121
122template<typename Accessor>
124 const char *name,
125 const char *idname,
126 const char *description)
127{
128 ot->name = name;
129 ot->idname = idname;
130 ot->description = description;
132 ot->flag = OPTYPE_UNDO;
133
134 ot->exec = [](bContext *C, wmOperator *op) -> wmOperatorStatus {
135 PointerRNA node_ptr = get_active_node_to_operate_on(C, op, Accessor::node_idname);
136 if (!node_ptr) {
137 return OPERATOR_CANCELLED;
138 }
139 bNode &node = *static_cast<bNode *>(node_ptr.data);
140 SocketItemsRef ref = Accessor::get_items_from_node(node);
141 if (*ref.items_num > 0 && ref.active_index) {
143 ref.items, ref.items_num, ref.active_index, *ref.active_index, Accessor::destruct_item);
144 update_after_node_change(C, node_ptr);
145 }
146 return OPERATOR_FINISHED;
147 };
148
150}
151
152template<typename Accessor>
154 const char *name,
155 const char *idname,
156 const char *description)
157{
158 ot->name = name;
159 ot->idname = idname;
160 ot->description = description;
162 ot->flag = OPTYPE_UNDO;
163
164 ot->exec = [](bContext *C, wmOperator *op) -> wmOperatorStatus {
165 PointerRNA node_ptr = get_active_node_to_operate_on(C, op, Accessor::node_idname);
166 if (!node_ptr) {
167 return OPERATOR_CANCELLED;
168 }
169 bNode &node = *static_cast<bNode *>(node_ptr.data);
170 const int index_to_remove = RNA_int_get(op->ptr, "index");
171 SocketItemsRef ref = Accessor::get_items_from_node(node);
173 ref.items, ref.items_num, ref.active_index, index_to_remove, Accessor::destruct_item);
174
175 update_after_node_change(C, node_ptr);
176 return OPERATOR_FINISHED;
177 };
178
179 RNA_def_int(ot->srna, "index", 0, 0, INT32_MAX, "Index", "Index to remove", 0, INT32_MAX);
180}
181
182template<typename Accessor>
183inline void add_item(wmOperatorType *ot,
184 const char *name,
185 const char *idname,
186 const char *description)
187{
188 ot->name = name;
189 ot->idname = idname;
190 ot->description = description;
193
194 static const char *socket_type_id = "socket_type";
195 static const char *item_name_id = "item_name";
196
197 ot->invoke = [](bContext *C, wmOperator *op, const wmEvent *event) {
198 const bool show_dialog = RNA_boolean_get(op->ptr, "show_dialog");
199 const bool needs_dialog = Accessor::has_type || Accessor::has_name;
200 if (show_dialog && needs_dialog) {
201 return WM_operator_props_popup_confirm_ex(C, op, event, IFACE_("Add Item"));
202 }
203 return op->type->exec(C, op);
204 };
205
206 ot->ui = [](bContext * /*C*/, wmOperator *op) {
207 ui::Layout &layout = *op->layout;
208 if constexpr (Accessor::has_name) {
209 PropertyRNA *prop = RNA_struct_find_property(op->ptr, item_name_id);
210 ui::Layout &row = layout.row(true);
211 row.activate_init_set(true);
212 row.prop(op->ptr, prop, RNA_NO_INDEX, 0, UI_ITEM_NONE, "", ICON_NONE, IFACE_("Name"));
213 }
214 if constexpr (Accessor::has_type) {
215 layout.prop(op->ptr, socket_type_id, UI_ITEM_NONE, "", ICON_NONE);
216 }
217 };
218
219 ot->exec = [](bContext *C, wmOperator *op) -> wmOperatorStatus {
220 PointerRNA node_ptr = get_active_node_to_operate_on(C, op, Accessor::node_idname);
221 if (!node_ptr) {
222 return OPERATOR_CANCELLED;
223 }
224 bNodeTree *ntree = reinterpret_cast<bNodeTree *>(node_ptr.owner_id);
225 bNode &node = *static_cast<bNode *>(node_ptr.data);
226 SocketItemsRef ref = Accessor::get_items_from_node(node);
227 const typename Accessor::ItemT *active_item = nullptr;
228 int dst_index = *ref.items_num;
229 if (ref.active_index) {
230 const int old_active_index = *ref.active_index;
231 if (old_active_index >= 0 && old_active_index < *ref.items_num) {
232 active_item = &(*ref.items)[old_active_index];
233 dst_index = active_item ? old_active_index + 1 : *ref.items_num;
234 }
235 }
236
237 /* Determine name if necessary. */
238 const bool init_from_active = RNA_boolean_get(op->ptr, "init_from_active");
239 std::optional<std::string> name;
240 if constexpr (Accessor::has_name) {
241 if (!init_from_active) {
242 name = RNA_string_get(op->ptr, item_name_id);
243 }
244 else {
245 name = active_item ? active_item->name : "";
246 if constexpr (Accessor::has_custom_initial_name) {
247 name = Accessor::custom_initial_name(node, *name);
248 }
249 }
250 }
251
252 /* Determine socket type if necessary. */
253 std::optional<eNodeSocketDatatype> socket_type;
254 if constexpr (Accessor::has_type) {
255 if (!init_from_active) {
256 socket_type = eNodeSocketDatatype(RNA_enum_get(op->ptr, socket_type_id));
257 }
258 else if (active_item) {
259 socket_type = Accessor::get_socket_type(*active_item);
260 }
261 else {
262 socket_type = Accessor::supports_socket_type(SOCK_GEOMETRY, ntree->type) ? SOCK_GEOMETRY :
264 }
265 }
266
267 if constexpr (Accessor::has_type && Accessor::has_name) {
269 *ntree, node, *socket_type, name->c_str());
270 }
271 else if constexpr (!Accessor::has_type && Accessor::has_name) {
273 }
274 else if constexpr (!Accessor::has_type && !Accessor::has_name) {
276 }
277 else {
279 }
280
281 dna::array::move_index(*ref.items, *ref.items_num, *ref.items_num - 1, dst_index);
282 if (ref.active_index) {
283 *ref.active_index = dst_index;
284 }
285
286 update_after_node_change(C, node_ptr);
287 return OPERATOR_FINISHED;
288 };
289
291
292 PropertyRNA *prop;
293 prop = RNA_def_boolean(ot->srna,
294 "show_dialog",
295 false,
296 "Show Dialog",
297 "Show a dialog to edit the initial properties");
299
300 prop = RNA_def_boolean(
301 ot->srna,
302 "init_from_active",
303 true,
304 "Init from Active",
305 "Instead of using the provided name or type, copy the state of the active item");
307
308 if constexpr (Accessor::has_type) {
309 prop = RNA_def_enum(ot->srna,
310 socket_type_id,
313 "Socket Type",
314 "Type of the new socket item");
316 prop,
317 nullptr,
318 nullptr,
319 [](bContext *C, PointerRNA * /*ptr*/, PropertyRNA * /*prop*/, bool *r_free) {
320 if (!C) {
321 *r_free = false;
323 }
324 *r_free = true;
325 SpaceNode *snode = CTX_wm_space_node(C);
327 [&](const EnumPropertyItem &item) -> bool {
328 return Accessor::supports_socket_type(
329 eNodeSocketDatatype(item.value), snode->edittree->type);
330 });
331 },
332 nullptr,
333 nullptr);
334 }
335 if constexpr (Accessor::has_name) {
336 RNA_def_string(ot->srna, item_name_id, nullptr, 0, "Item Name", "Name of the new socket item");
337 }
338}
339
340enum class MoveDirection {
341 Up = 0,
342 Down = 1,
343};
344
345template<typename Accessor>
347 const char *name,
348 const char *idname,
349 const char *description)
350{
351 ot->name = name;
352 ot->idname = idname;
353 ot->description = description;
355 ot->flag = OPTYPE_UNDO;
356
357 ot->exec = [](bContext *C, wmOperator *op) -> wmOperatorStatus {
358 PointerRNA node_ptr = get_active_node_to_operate_on(C, op, Accessor::node_idname);
359 if (!node_ptr) {
360 return OPERATOR_CANCELLED;
361 }
362 bNode &node = *static_cast<bNode *>(node_ptr.data);
363 const MoveDirection direction = MoveDirection(RNA_enum_get(op->ptr, "direction"));
364
365 SocketItemsRef ref = Accessor::get_items_from_node(node);
366 const int old_active_index = *ref.active_index;
367 if (direction == MoveDirection::Up && old_active_index > 0) {
368 dna::array::move_index(*ref.items, *ref.items_num, old_active_index, old_active_index - 1);
369 *ref.active_index -= 1;
370 }
371 else if (direction == MoveDirection::Down && old_active_index < *ref.items_num - 1) {
372 dna::array::move_index(*ref.items, *ref.items_num, old_active_index, old_active_index + 1);
373 *ref.active_index += 1;
374 }
375
376 update_after_node_change(C, node_ptr);
377 return OPERATOR_FINISHED;
378 };
379
380 static const EnumPropertyItem direction_items[] = {
381 {int(MoveDirection::Up), "UP", 0, "Up", ""},
382 {int(MoveDirection::Down), "DOWN", 0, "Down", ""},
383 {0, nullptr, 0, nullptr, nullptr},
384 };
385
386 RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Move direction");
388}
389
395template<typename Accessor> inline void make_add_item_operator()
396{
399 "Add Item",
400 Accessor::operator_idnames::add_item.c_str(),
401 "Add item below active item");
402 });
403}
404
410template<typename Accessor> inline void make_remove_active_item_operator()
411{
414 ot, "Remove Item", Accessor::operator_idnames::remove_item.c_str(), "Remove active item");
415 });
416}
417
423template<typename Accessor> inline void make_remove_item_by_index_operator()
424{
427 ot, "Remove Item", Accessor::operator_idnames::remove_item.c_str(), "Remove active item");
428 });
429}
430
436template<typename Accessor> inline void make_move_item_operator()
437{
440 ot, "Move Item", Accessor::operator_idnames::move_item.c_str(), "Move active item");
441 });
442}
443
455
456} // namespace blender::nodes::socket_items::ops
#define INT32_MAX
#define IFACE_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define RNA_NO_INDEX
#define NC_NODE
Definition WM_types.hh:407
#define NA_EDITED
Definition WM_types.hh:634
#define BLI_assert_unreachable()
const bNodeTreeZone * get_zone_by_node(const int32_t node_id) const
const bNodeZoneType * zone_type_by_node_type(const int node_type)
bNode * node_get_active(bNodeTree &ntree)
Definition node.cc:5445
void remove_index(T **items, int *items_num, int *active_index, const int index, void(*destruct_item)(T *))
void move_index(T *items, const int items_num, const int from_index, const int to_index)
void remove_item_by_index(wmOperatorType *ot, const char *name, const char *idname, const char *description)
void add_node_identifier_property(wmOperatorType *ot)
void move_active_item(wmOperatorType *ot, const char *name, const char *idname, const char *description)
void remove_active_item(wmOperatorType *ot, const char *name, const char *idname, const char *description)
PointerRNA get_active_node_to_operate_on(bContext *C, wmOperator *op, const StringRef node_idname)
void update_after_node_change(bContext *C, const PointerRNA node_ptr)
void add_item(wmOperatorType *ot, const char *name, const char *idname, const char *description)
Accessor::ItemT * add_item_with_name(bNode &node, const char *name)
Accessor::ItemT * add_item(bNode &node)
Accessor::ItemT * add_item_with_socket_type_and_name(bNodeTree &ntree, bNode &node, const eNodeSocketDatatype socket_type, const char *name, std::optional< int > dimensions=std::nullopt)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
void WM_main_add_notifier(uint type, void *reference)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int RNA_int_get(PointerRNA *ptr, const char *name)
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
SpaceNode * CTX_wm_space_node(const bContext *C)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void RNA_def_property_enum_funcs_runtime(PropertyRNA *prop, EnumPropertyGetFunc getfunc, EnumPropertySetFunc setfunc, EnumPropertyItemFunc itemfunc, EnumPropertyGetTransformFunc get_transform_fn, EnumPropertySetTransformFunc set_transform_fn)
wmOperatorStatus WM_operator_props_popup_confirm_ex(bContext *C, wmOperator *op, const wmEvent *, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_ntree_update_tag_node_property(bNodeTree *ntree, bNode *node)
Main * CTX_data_main(const bContext *C)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
struct bNodeTree * edittree
eNodeTree_Type type
void activate_init_set(bool activate_init)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & row(bool align)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141