Blender V5.3
transform_convert_node.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_space_types.h"
10#include "DNA_userdef_types.h"
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_listbase.hh"
15#include "BLI_math_matrix_c.hh"
16#include "BLI_math_vector.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_rect.hh"
19
20#include "BKE_context.hh"
22#include "BKE_node.hh"
23#include "BKE_node_runtime.hh"
24
25#include "ED_node.hh"
26
27#include "UI_view2d.hh"
28
29#include "transform.hh"
30#include "transform_convert.hh"
31#include "transform_snap.hh"
32
33#include "WM_api.hh"
34
35namespace blender::ed::transform {
36
39
40 /* Compare if the view has changed so we can update with `transformViewUpdate`. */
42
43 bool is_new_node = false;
44
46};
47
48/* -------------------------------------------------------------------- */
51
53 TransData2D &td2d,
54 bNode &node,
55 const float dpi_fac)
56{
57 /* Account for parents (nested nodes). */
58 float2 loc = float2(node.location) * dpi_fac;
59
60 /* Use top-left corner as the transform origin for nodes. */
61 /* Weirdo - but the node system is a mix of free 2d elements and DPI sensitive UI. */
62 td2d.loc[0] = loc.x;
63 td2d.loc[1] = loc.y;
64 td2d.loc[2] = 0.0f;
65 td2d.loc2d = td2d.loc; /* Current location. */
66
67 td.loc = td2d.loc;
68 copy_v3_v3(td.iloc, td.loc);
69 /* Use node center instead of origin (top-left corner). */
70 td.center[0] = td2d.loc[0];
71 td.center[1] = td2d.loc[1];
72 td.center[2] = 0.0f;
73
74 memset(td.axismtx, 0, sizeof(td.axismtx));
75 td.axismtx[2][2] = 1.0f;
76
77 td.val = nullptr;
78
79 td.flag = TD_SELECTED;
80 td.dist = 0.0f;
81
82 unit_m3(td.mtx);
83 unit_m3(td.smtx);
84
85 td.extra = &node;
86}
87
88static bool is_node_parent_select(const bNode *node)
89{
90 while ((node = node->parent)) {
91 if (node->is_selected()) {
92 return true;
93 }
94 }
95 return false;
96}
97
105{
106 /* Check for frame nodes that adjust their size based on the contained child nodes. */
107 if (node->is_frame()) {
108 const NodeFrame *data = static_cast<const NodeFrame *>(node->storage);
109 const bool shrinking = data->flag & NODE_FRAME_SHRINK;
110 const bool is_parent = !node->direct_children_in_frame().is_empty();
111
112 if (is_parent && shrinking) {
113 return true;
114 }
115 }
116
117 /* Now check for child nodes of manually resized frames. */
118 while ((node = node->parent)) {
119 const NodeFrame *parent_data = static_cast<const NodeFrame *>(node->storage);
120 const bool parent_shrinking = parent_data->flag & NODE_FRAME_SHRINK;
121 const bool parent_transformed = transformed_nodes.contains(node);
122
123 if (parent_transformed && !parent_shrinking) {
124 return true;
125 }
126 }
127
128 return false;
129}
130
132{
133 VectorSet<bNode *> nodes = node_tree.all_nodes();
134
135 /* Keep only nodes that are selected or inside a frame that is selected. */
136 nodes.remove_if([&](bNode *node) {
137 const bool node_selected = node->is_selected();
138 const bool parent_selected = is_node_parent_select(node);
139 return (!node_selected && !parent_selected);
140 });
141
142 /* Remove nodes that are transformed together with their parent or child nodes. */
143 nodes.remove_if([&](bNode *node) { return transform_tied_to_other_node(node, nodes); });
144
145 return nodes;
146}
147
148static void createTransNodeData(bContext * /*C*/, TransInfo *t)
149{
150 SpaceNode *snode = t->area->spacedata.first_as<SpaceNode>();
151 bNodeTree *node_tree = snode->edittree;
152 if (!node_tree) {
153 return;
154 }
155
156 /* Custom data to enable edge panning during the node transform. */
157 TransCustomDataNode *customdata = MEM_new<TransCustomDataNode>(__func__);
158 view2d_edge_pan_init(t->context,
159 &customdata->edgepan_data,
166 customdata->viewrect_prev = customdata->edgepan_data.initial_rect;
167 customdata->is_new_node = t->remove_on_cancel;
168
169 if (t->region) {
171 *snode, *t->region, t->modifiers & MOD_NODE_ATTACH, customdata->is_new_node);
173 }
174
175 t->custom.type.data = customdata;
176 t->custom.type.free_cb = [](TransInfo *, TransDataContainer *, TransCustomData *custom_data) {
177 TransCustomDataNode *data = static_cast<TransCustomDataNode *>(custom_data->data);
178 MEM_delete(data);
179 custom_data->data = nullptr;
180 };
181
183
184 /* Nodes don't support proportional editing and probably never will. */
185 t->flag = t->flag & ~T_PROP_EDIT_ALL;
186
188 if (nodes.is_empty()) {
189 return;
190 }
191
192 tc->data_len = nodes.size();
193 tc->data = MEM_new_array_zeroed<TransData>(tc->data_len, __func__);
195
196 for (const int i : nodes.index_range()) {
198 }
199}
200
202
203/* -------------------------------------------------------------------- */
206
208{
209 if (!(transform_snap_is_active(t) &&
211 {
212 return;
213 }
214
215 float2 grid_size = t->snap_spatial;
216 if (t->modifiers & MOD_PRECISION) {
217 grid_size *= t->snap_spatial_precision;
218 }
219
220 /* Early exit on unusable grid size. */
221 if (math::is_zero(grid_size)) {
222 return;
223 }
224
226 for (const int i : IndexRange(tc->data_len)) {
227 TransData &td = tc->data[i];
228 if (td.flag & TD_SKIP) {
229 continue;
230 }
231
232 if ((t->flag & T_PROP_EDIT) && (td.factor == 0.0f)) {
233 continue;
234 }
235
236 /* Nodes are snapped to the grid by first aligning their initial position to the grid and
237 * then offsetting them in grid increments.
238 *
239 * This ensures that multiple unsnapped nodes snap to the grid in sync while moving.
240 */
241
242 const float2 inital_location = td.iloc;
243 const float2 target_location = td.loc;
244 const float2 offset = target_location - inital_location;
245
246 const float2 snapped_inital_location = math::round(inital_location / grid_size) * grid_size;
247 const float2 snapped_offset = math::round(offset / grid_size) * grid_size;
248 const float2 snapped_target_location = snapped_inital_location + snapped_offset;
249
250 copy_v2_v2(td.loc, snapped_target_location);
251 }
252 }
253}
254
255static void move_child_nodes(bNode &node, const float2 &delta)
256{
257 for (bNode *child : node.direct_children_in_frame()) {
258 child->location[0] += delta.x;
259 child->location[1] += delta.y;
260 if (child->is_frame()) {
261 move_child_nodes(*child, delta);
262 }
263 }
264}
265
266static bool has_selected_parent(const bNode &node)
267{
268 for (bNode *parent = node.parent; parent; parent = parent->parent) {
269 if (parent->is_selected()) {
270 return true;
271 }
272 }
273 return false;
274}
275
277{
278 const float dpi_fac = UI_SCALE_FAC;
279 SpaceNode *snode = t->area->spacedata.first_as<SpaceNode>();
280
281 TransCustomDataNode *customdata = static_cast<TransCustomDataNode *>(t->custom.type.data);
282
283 if (t->options & CTX_VIEW2D_EDGE_PAN) {
284 if (t->state == TRANS_CANCEL) {
285 view2d_edge_pan_cancel(t->context, &customdata->edgepan_data);
286 }
287 else if (t->region) {
288 /* Edge panning functions expect window coordinates, mval is relative to region. */
289 const int xy[2] = {
290 t->region->winrct.xmin + int(t->mval[0]),
291 t->region->winrct.ymin + int(t->mval[1]),
292 };
293 ui::view2d_edge_pan_apply(t->context, &customdata->edgepan_data, xy);
294 }
295 }
296
297 float offset[2] = {0.0f, 0.0f};
298 if (t->state != TRANS_CANCEL) {
299 if (t->region &&
300 !BLI_rctf_compare(&customdata->viewrect_prev, &t->region->v2d.cur, FLT_EPSILON))
301 {
302 /* Additional offset due to change in view2D rect. */
303 BLI_rctf_transform_pt_v(&t->region->v2d.cur, &customdata->viewrect_prev, offset, offset);
305 customdata->viewrect_prev = t->region->v2d.cur;
306 }
307 }
308
309 if (t->modifiers & MOD_NODE_FRAME) {
311 Vector<bNode *> nodes_to_detach;
312 for (bNode *node : snode->edittree->all_nodes()) {
313 if (!node->is_selected()) {
314 continue;
315 }
317 continue;
318 }
319 if (!node->parent) {
320 continue;
321 }
322 customdata->old_parent_by_detached_node.add(node, node->parent);
323 nodes_to_detach.append(node);
324 }
325 if (nodes_to_detach.is_empty()) {
327 t->context, "NODE_OT_attach", wm::OpCallContext::InvokeDefault, nullptr, nullptr);
328 }
329 else {
330 for (bNode *node : nodes_to_detach) {
332 }
333 }
334 }
335
338
339 /* Flush to 2d vector from internally used 3d vector. */
340 for (int i = 0; i < tc->data_len; i++) {
341 TransData *td = &tc->data[i];
342 TransData2D *td2d = &tc->data_2d[i];
343 bNode *node = static_cast<bNode *>(td->extra);
344
345 float2 loc = float2(td2d->loc) + offset;
346
347 /* Weirdo - but the node system is a mix of free 2d elements and DPI sensitive UI. */
348 loc /= dpi_fac;
349
350 if (node->is_frame()) {
351 const float2 delta = loc - float2(node->location);
352 move_child_nodes(*node, delta);
353 }
354
355 node->location[0] = loc.x;
356 node->location[1] = loc.y;
357 }
358
359 /* Handle intersection with noodles. */
360 if (tc->data_len == 1 && t->region) {
362 *snode, *t->region, t->modifiers & MOD_NODE_ATTACH, customdata->is_new_node);
363 }
364 if (t->region) {
366 }
367 }
368}
369
371
372/* -------------------------------------------------------------------- */
375
377{
378 Main *bmain = CTX_data_main(C);
379 SpaceNode *snode = t->area->spacedata.first_as<SpaceNode>();
380 bNodeTree *ntree = snode->edittree;
381 const TransCustomDataNode &customdata = *static_cast<TransCustomDataNode *>(t->custom.type.data);
382
383 const bool canceled = (t->state == TRANS_CANCEL);
384
385 if (canceled) {
386 for (auto &&[node, parent] : customdata.old_parent_by_detached_node.items()) {
387 bke::node_attach_node(*ntree, *node, *parent);
388 }
389 }
390 if (canceled && t->remove_on_cancel) {
391 /* Remove selected nodes on cancel. */
392 if (ntree) {
393 for (bNode &node : ntree->nodes.items_mutable()) {
394 if (node.is_selected()) {
395 bke::node_remove_node(bmain, *ntree, node, true);
396 }
397 }
398 BKE_main_ensure_invariants(*bmain, ntree->id);
399 }
400 }
401
402 if (!canceled) {
404 if (t->modifiers & MOD_NODE_ATTACH) {
405 space_node::node_insert_on_link_flags(*bmain, *snode, customdata.is_new_node);
406 }
407 }
408
411
412 wmOperatorType *ot = WM_operatortype_find("NODE_OT_insert_offset", true);
413 BLI_assert(ot);
417}
418
420
422 /*flags*/ (T_POINTS | T_2D_EDIT),
423 /*create_trans_data*/ createTransNodeData,
424 /*recalc_data*/ flushTransNodes,
425 /*special_aftertrans_update*/ special_aftertrans_update__node,
426};
427
428} // namespace blender::ed::transform
#define UI_SCALE_FAC
#define NODE_EDGE_PAN_OUTSIDE_PAD
Definition ED_node_c.hh:34
#define NODE_EDGE_PAN_INSIDE_PAD
Definition ED_node_c.hh:33
#define NODE_EDGE_PAN_MAX_SPEED
Definition ED_node_c.hh:36
#define NODE_EDGE_PAN_DELAY
Definition ED_node_c.hh:37
#define NODE_EDGE_PAN_ZOOM_INFLUENCE
Definition ED_node_c.hh:38
#define NODE_EDGE_PAN_SPEED_RAMP
Definition ED_node_c.hh:35
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
constexpr IndexRange()=default
bool contains(const Key &key) const
void append(const T &value)
bool is_empty() const
#define FLT_EPSILON
void node_attach_node(bNodeTree &ntree, bNode &node, bNode &parent)
Definition node.cc:4883
void node_remove_node(Main *bmain, bNodeTree &ntree, bNode &node, bool do_id_user, bool remove_animation=true)
Definition node.cc:5155
void node_detach_node(bNodeTree &ntree, bNode &node)
Definition node.cc:4891
void node_insert_on_link_flags_set(SpaceNode &snode, const ARegion &region, bool attach_enabled, bool is_new_node)
void node_insert_on_frame_flag_set(SpaceNode &snode, ARegion &region, const int2 &cursor)
void node_insert_on_link_flags_clear(bNodeTree &node_tree)
void node_insert_on_frame_flag_clear(SpaceNode &snode)
void node_insert_on_link_flags(Main &bmain, SpaceNode &snode, bool is_new_node)
static void createTransNodeData(bContext *, TransInfo *t)
TransConvertTypeInfo TransConvertType_Node
static bool has_selected_parent(const bNode &node)
static void special_aftertrans_update__node(bContext *C, TransInfo *t)
static void node_snap_grid_apply(TransInfo *t)
static void flushTransNodes(TransInfo *t)
static void create_transform_data_for_node(TransData &td, TransData2D &td2d, bNode &node, const float dpi_fac)
void transformViewUpdate(TransInfo *t)
bool transform_snap_is_active(const TransInfo *t)
static bool transform_tied_to_other_node(bNode *node, VectorSet< bNode * > transformed_nodes)
static VectorSet< bNode * > get_transformed_nodes(bNodeTree &node_tree)
static void move_child_nodes(bNode &node, const float2 &delta)
static bool is_node_parent_select(const bNode *node)
bool is_zero(const T &a)
T round(const T &a)
void view2d_edge_pan_apply(bContext *C, View2DEdgePanData *vpd, const int xy[2]) ATTR_NONNULL(1
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void WM_operator_properties_free(PointerRNA *ptr)
MINLINE void copy_v2_v2(float r[2], const float a[2])
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
bool BLI_rctf_compare(const struct rctf *rect_a, const struct rctf *rect_b, float limit)
BMesh const char void * data
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
void ED_node_post_apply_transform(bContext *C, bNodeTree *ntree)
Definition node_edit.cc:366
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void BLI_rctf_transform_pt_v(const rctf *dst, const rctf *src, float xy_dst[2], const float xy_src[2])
Definition rct.cc:540
Main * CTX_data_main(const bContext *C)
void unit_m3(float m[3][3])
wmOperatorStatus WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
ListBaseT< SpaceLink > spacedata
struct bNodeTree * edittree
void(*) free_cb(TransInfo *, TransDataContainer *tc, TransCustomData *custom_data)
Definition transform.hh:655
TransCustomDataContainer custom
#define TRANS_DATA_CONTAINER_FIRST_SINGLE(t)
Definition transform.hh:45
#define T_PROP_EDIT_ALL
Definition transform.hh:34
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:48
conversion and adaptation of different datablocks to a common struct.