Blender V5.3
compositor_gizmos.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BLI_math_matrix_c.hh"
12#include "BLI_math_vector.hh"
13#include "BLI_math_vector_c.hh"
14#include "BLI_rect.hh"
15#include "BLI_string.hh"
16
17#include "BKE_context.hh"
18#include "BKE_image.hh"
19#include "BKE_node.hh"
20#include "BKE_node_runtime.hh"
21
22#include "DNA_listBase.h"
23#include "DNA_node_types.h"
24#include "DNA_screen_types.h"
25#include "DNA_space_types.h"
27
28#include "ED_gizmo_library.hh"
29#include "ED_image.hh"
30
31#include "IMB_imbuf_types.hh"
32
33#include "RNA_access.hh"
34#include "RNA_prototypes.hh"
35
36#include "WM_api.hh"
37#include "WM_types.hh"
38
39#include "NOD_compositor_gizmos.hh" /* Own include. */
40#include "NOD_eval_log.hh"
41
42namespace blender::nodes::gizmos {
43
44/* -------------------------------------------------------------------- */
47
48static float2 GIZMO_NODE_DEFAULT_DIMS{64.0f, 64.0f};
49
50static float2 node_gizmo_safe_calc_dims(const ImBuf *ibuf, const float2 &fallback_dims)
51{
52 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
53 return float2{float(ibuf->x), float(ibuf->y)};
54 }
55
56 /* We typically want to divide by dims, so avoid returning zero here. */
57 BLI_assert(!math::is_any_zero(fallback_dims));
58 return fallback_dims;
59}
60
62 const float zoom,
63 const float2 space_offset,
64 const float2 &image_dims,
65 const float2 &image_offset,
66 float matrix_space[4][4])
67{
68 unit_m4(matrix_space);
69 mul_v3_fl(matrix_space[0], zoom * image_dims.x);
70 mul_v3_fl(matrix_space[1], zoom * image_dims.y);
71 matrix_space[3][0] = ((region->winx / 2) + space_offset.x) -
72 ((image_dims.x / 2.0f - image_offset.x + 0.5f) * zoom);
73 matrix_space[3][1] = ((region->winy / 2) + space_offset.y) -
74 ((image_dims.y / 2.0f - image_offset.y + 0.5f) * zoom);
75}
76
77static void node_gizmo_calc_matrix_space(const ARegion *region,
78 const float zoom,
79 const float2 offset,
80 float matrix_space[4][4])
81{
82 unit_m4(matrix_space);
83 mul_v3_fl(matrix_space[0], zoom);
84 mul_v3_fl(matrix_space[1], zoom);
85 matrix_space[3][0] = (region->winx / 2) - offset.x - 0.5f * zoom;
86 matrix_space[3][1] = (region->winy / 2) - offset.y - 0.5f * zoom;
87}
88
89static bool node_gizmo_is_set_visible(const SpaceNode &snode)
90{
91 if ((snode.flag & SNODE_BACKDRAW) == 0) {
92 return false;
93 }
94
95 if (!snode.edittree || snode.edittree->type != NTREE_COMPOSIT) {
96 return false;
97 }
98
100 return true;
101 }
102
103 return false;
104}
105
107{
108 if (!ELEM(sima.mode, SI_MODE_VIEW, SI_MODE_MASK)) {
109 return false;
110 }
111
113 return false;
114 }
115
116 Image *image = ED_space_image(&sima);
117 if (!(image && image->source == IMA_SRC_VIEWER && image->type == IMA_TYPE_COMPOSITE)) {
118 return false;
119 }
120
121 return true;
122}
123
124static bool gizmo_is_interacting(const wmGizmo &gz)
125{
126 return gz.interaction_data != nullptr;
127}
128
132 const bNode &node,
133 const wmGizmo &gz)
134{
135 if (gizmo_is_interacting(gz)) {
136 return false;
137 }
138
140 snode);
141 eval_log::NodeTreeLog *tree_log = tree_logs.get_main_tree_log(node);
142 if (!tree_log) {
143 return false;
144 }
145
147 return tree_log->evaluated_gizmo_nodes.contains(node.identifier);
148}
149
151{
152 wmWindowManager *window_manager = CTX_wm_manager(C);
153
154 for (wmWindow &window : window_manager->windows) {
155 bScreen *screen = WM_window_get_active_screen(&window);
156 for (ScrArea &area : screen->areabase) {
157 SpaceLink *space_link = area.spacedata.first();
158 if (!space_link || space_link->spacetype != SPACE_NODE) {
159 continue;
160 }
161 SpaceNode *snode = reinterpret_cast<SpaceNode *>(space_link);
162 if (snode->edittree && snode->edittree->type == NTREE_COMPOSIT) {
163 bNodeTreePath *path = snode->treepath.last();
164 if (snode->nodetree->active_viewer_key == path->parent_key) {
165 return snode;
166 }
167 }
168 }
169 }
170
171 return nullptr;
172}
173
175 bNode &node,
176 const UString socket_identifier,
177 const FunctionRef<void(PointerRNA &, PropertyRNA *, int)> callback,
178 const int index = 0)
179{
180 bNodeSocket *socket = bke::node_find_socket(node, SOCK_IN, socket_identifier);
181 BLI_assert(socket != nullptr);
182 PointerRNA ptr = RNA_pointer_create_discrete(&node.owner_tree().id, RNA_NodeSocket, socket);
183 PropertyRNA *prop = RNA_struct_find_property(&ptr, "default_value");
184 callback(ptr, prop, index);
185}
186
188{
189 for (wmGizmo &gz : gzgroup->gizmos) {
191 }
192}
193
195
210
211static bool show_box_mask_gizmo(const SpaceNode &snode)
212{
213 bNodeTree *node_tree = snode.edittree;
214 BLI_assert(node_tree);
215
216 bNode *node = bke::node_get_active(*node_tree);
217 if (node == nullptr) {
218 return false;
219 }
220
221 if (node && node->is_type("CompositorNodeBoxMask"_ustr)) {
222 node_tree->ensure_topology_cache();
223 for (bNodeSocket &input : node->inputs) {
224 if (STR_ELEM(input.name, "Position", "Size", "Rotation") && input.is_directly_linked()) {
225 return false;
226 }
227 }
228 return true;
229 }
230
231 return false;
232}
233
235{
236 SpaceNode *snode = CTX_wm_space_node(C);
237 if (snode == nullptr) {
238 return false;
239 }
240 if (!node_gizmo_is_set_visible(*snode)) {
241 return false;
242 }
243
244 return show_box_mask_gizmo(*snode);
245}
246
248{
249 const SpaceImage *sima = CTX_wm_space_image(C);
250 if (sima == nullptr) {
251 return false;
252 }
253
254 if (!image_gizmo_is_set_visible(*sima)) {
255 return false;
256 }
257
258 const SpaceNode *snode = find_active_node_editor(C);
259 if (snode == nullptr || snode->edittree == nullptr) {
260 return false;
261 }
262
263 return show_box_mask_gizmo(*snode);
264}
265
266void box_mask_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
267{
268 NodeBBoxWidgetGroup *mask_group = MEM_new<NodeBBoxWidgetGroup>(__func__);
269 mask_group->border = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, nullptr);
270
271 RNA_enum_set(mask_group->border->ptr,
272 "transform",
275
276 RNA_enum_set(mask_group->border->ptr,
277 "draw_options",
280
281 gzgroup->customdata = mask_group;
282 gzgroup->customdata_free = [](void *customdata) {
283 MEM_delete(static_cast<NodeBBoxWidgetGroup *>(customdata));
284 };
285
287}
288
290{
291 ARegion *region = CTX_wm_region(C);
292 wmGizmo *gz = gzgroup->gizmos.first();
293
294 SpaceNode *snode = CTX_wm_space_node(C);
295
297 region, snode->zoom, float2{-snode->xof, -snode->yof}, gz->matrix_space);
298}
299
301{
302 ARegion *region = CTX_wm_region(C);
303 wmGizmo *gz = gzgroup->gizmos.first();
304
306 const float2 offset = float2{sima->xof, sima->yof} * sima->zoom;
307
308 node_gizmo_calc_matrix_space(region, sima->zoom, offset, gz->matrix_space);
309}
310
312 wmGizmoProperty *gz_prop,
313 void *value_p)
314{
315 float (*matrix)[4] = static_cast<float (*)[4]>(value_p);
316 BLI_assert(gz_prop->type->array_length == 16);
317 NodeBBoxWidgetGroup *mask_group = static_cast<NodeBBoxWidgetGroup *>(
319 const float2 dims = mask_group->state.dims;
320 const float2 offset = mask_group->state.offset;
321 const bNode *node = static_cast<const bNode *>(gz_prop->custom_func.user_data);
322 const float aspect = dims.x / dims.y;
323
324 float loc[3], rot[3][3], size[3];
325 mat4_to_loc_rot_size(loc, rot, size, matrix);
326
327 const bNodeSocket *rotation_input = bke::node_find_socket(*node, SOCK_IN, "Rotation"_ustr);
328 const float rotation = rotation_input->default_value_typed<bNodeSocketValueFloat>()->value;
329 axis_angle_to_mat3_single(rot, 'Z', rotation);
330
331 const bNodeSocket *position_input = bke::node_find_socket(*node, SOCK_IN, "Position"_ustr);
332 const float2 position = position_input->default_value_typed<bNodeSocketValueVector>()->value;
333 loc[0] = (position.x - 0.5) * dims.x + offset.x;
334 loc[1] = (position.y - 0.5) * dims.y + offset.y;
335 loc[2] = 0;
336
337 const bNodeSocket *size_input = bke::node_find_socket(*node, SOCK_IN, "Size"_ustr);
338 const float2 size_value = size_input->default_value_typed<bNodeSocketValueVector>()->value;
339 size[0] = size_value.x;
340 size[1] = size_value.y * aspect;
341 size[2] = 1;
342
343 loc_rot_size_to_mat4(matrix, loc, rot, size);
344}
345
347{
349 bbox_group->update_data.context, &bbox_group->update_data.ptr, bbox_group->update_data.prop);
350}
351
353 wmGizmoProperty *gz_prop,
354 const void *value_p)
355{
356 const float (*matrix)[4] = static_cast<const float (*)[4]>(value_p);
357 BLI_assert(gz_prop->type->array_length == 16);
358 NodeBBoxWidgetGroup *mask_group = static_cast<NodeBBoxWidgetGroup *>(
360 const float2 dims = mask_group->state.dims;
361 const float2 offset = mask_group->state.offset;
362 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
363
364 bNodeSocket *position_input = bke::node_find_socket(*node, SOCK_IN, "Position"_ustr);
365 const float2 position = position_input->default_value_typed<bNodeSocketValueVector>()->value;
366
367 bNodeSocket *size_input = bke::node_find_socket(*node, SOCK_IN, "Size"_ustr);
368 const float2 size_value = size_input->default_value_typed<bNodeSocketValueVector>()->value;
369
370 const float aspect = dims.x / dims.y;
371 rctf rct;
372 rct.xmin = position.x - size_value.x / 2;
373 rct.xmax = position.x + size_value.x / 2;
374 rct.ymin = position.y - size_value.y / 2;
375 rct.ymax = position.y + size_value.y / 2;
376
377 float loc[3];
378 float rot[3][3];
379 float size[3];
380 mat4_to_loc_rot_size(loc, rot, size, matrix);
381
382 float eul[3];
383
384 /* Rotation can't be extracted from matrix when the gizmo width or height is zero. */
385 if (size[0] != 0 and size[1] != 0) {
386 mat4_to_eul(eul, matrix);
387 bNodeSocket *rotation_input = bke::node_find_socket(*node, SOCK_IN, "Rotation"_ustr);
388 rotation_input->default_value_typed<bNodeSocketValueFloat>()->value = eul[2];
389 }
390
391 BLI_rctf_resize(&rct, fabsf(size[0]), fabsf(size[1]) / aspect);
393 &rct, ((loc[0] - offset.x) / dims.x) + 0.5, ((loc[1] - offset.y) / dims.y) + 0.5);
394
395 size_input->default_value_typed<bNodeSocketValueVector>()->value[0] = size[0];
396 size_input->default_value_typed<bNodeSocketValueVector>()->value[1] = size[1] / aspect;
397 position_input->default_value_typed<bNodeSocketValueVector>()->value[0] = rct.xmin + size[0] / 2;
398 position_input->default_value_typed<bNodeSocketValueVector>()->value[1] = rct.ymin +
399 size[1] / aspect / 2;
400
401 gizmo_node_bbox_update(mask_group);
402}
403
405 wmGizmoProperty *gz_prop,
406 const FunctionRef<void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
407{
408 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
409
410 apply_socket_callback(*node, "Position"_ustr, callback, -1);
411 apply_socket_callback(*node, "Size"_ustr, callback, -1);
412 apply_socket_callback(*node, "Rotation"_ustr, callback);
413}
414
416{
417 Main *bmain = CTX_data_main(C);
418 NodeBBoxWidgetGroup *mask_group = static_cast<NodeBBoxWidgetGroup *>(gzgroup->customdata);
419 wmGizmo *gz = mask_group->border;
420
421 void *lock;
422 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Render Result");
423 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
424
425 if (ibuf == nullptr) [[unlikely]] {
427 BKE_image_release_ibuf(ima, ibuf, lock);
428 return;
429 }
430
432 mask_group->state.offset = flag_is_set(ibuf->flags, ImBufFlags::HasDisplayWindow) ?
433 float2(ibuf->display_offset) :
434 float2(0.0f);
435
436 RNA_float_set_array(gz->ptr, "dimensions", mask_group->state.dims);
438
439 const SpaceNode *snode = find_active_node_editor(C);
440 BLI_assert(snode != nullptr);
441
443
444 mask_group->update_data.context = const_cast<bContext *>(C);
445 bNodeSocket *source_input = bke::node_find_socket(*node, SOCK_IN, "Mask"_ustr);
447 reinterpret_cast<ID *>(snode->edittree), RNA_NodeSocket, source_input);
448 mask_group->update_data.prop = RNA_struct_find_property(&mask_group->update_data.ptr, "enabled");
449 BLI_assert(mask_group->update_data.prop != nullptr);
450
454 params.range_get_fn = nullptr;
455 params.user_data = node;
457 WM_gizmo_target_property_def_func(gz, "matrix", &params);
458
459 BKE_image_release_ibuf(ima, ibuf, lock);
460}
461
462static void node_input_to_rect(const bNode *node,
463 const float2 &dims,
464 const float2 offset,
465 rctf *r_rect)
466{
467
468 const bNodeSocket *x_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
469 PointerRNA x_input_rna_pointer = RNA_pointer_create_discrete(
470 nullptr, RNA_NodeSocket, const_cast<bNodeSocket *>(x_input));
471 const float xmin = float(RNA_int_get(&x_input_rna_pointer, "default_value"));
472
473 const bNodeSocket *y_input = bke::node_find_socket(*node, SOCK_IN, "Y"_ustr);
474 PointerRNA y_input_rna_pointer = RNA_pointer_create_discrete(
475 nullptr, RNA_NodeSocket, const_cast<bNodeSocket *>(y_input));
476 const float ymin = float(RNA_int_get(&y_input_rna_pointer, "default_value"));
477
478 const bNodeSocket *width_input = bke::node_find_socket(*node, SOCK_IN, "Width"_ustr);
479 PointerRNA width_input_rna_pointer = RNA_pointer_create_discrete(
480 nullptr, RNA_NodeSocket, const_cast<bNodeSocket *>(width_input));
481 const float width = float(RNA_int_get(&width_input_rna_pointer, "default_value"));
482
483 const bNodeSocket *height_input = bke::node_find_socket(*node, SOCK_IN, "Height"_ustr);
484 PointerRNA height_input_rna_pointer = RNA_pointer_create_discrete(
485 nullptr, RNA_NodeSocket, const_cast<bNodeSocket *>(height_input));
486 const float height = float(RNA_int_get(&height_input_rna_pointer, "default_value"));
487
488 r_rect->xmin = (xmin + offset.x) / dims.x;
489 r_rect->xmax = (xmin + width + offset.x) / dims.x;
490 r_rect->ymin = (ymin + offset.y) / dims.y;
491 r_rect->ymax = (ymin + height + offset.y) / dims.y;
492}
493
495 const rctf *rect,
496 const float2 &dims,
497 const float2 &offset)
498{
499 bNodeSocket *x_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
500 PointerRNA x_input_rna_pointer = RNA_pointer_create_discrete(
501 nullptr, RNA_NodeSocket, const_cast<bNodeSocket *>(x_input));
502
503 bNodeSocket *y_input = bke::node_find_socket(*node, SOCK_IN, "Y"_ustr);
504 PointerRNA y_input_rna_pointer = RNA_pointer_create_discrete(
505 nullptr, RNA_NodeSocket, const_cast<bNodeSocket *>(y_input));
506
507 bNodeSocket *width_input = bke::node_find_socket(*node, SOCK_IN, "Width"_ustr);
508 PointerRNA width_input_rna_pointer = RNA_pointer_create_discrete(
509 nullptr, RNA_NodeSocket, const_cast<bNodeSocket *>(width_input));
510
511 bNodeSocket *height_input = bke::node_find_socket(*node, SOCK_IN, "Height"_ustr);
512 PointerRNA height_input_rna_pointer = RNA_pointer_create_discrete(
513 nullptr, RNA_NodeSocket, const_cast<bNodeSocket *>(height_input));
514
515 const float xmin = rect->xmin * dims.x - offset.x;
516 const float width = rect->xmax * dims.x - offset.x - xmin;
517 const float ymin = rect->ymin * dims.y - offset.y;
518 const float height = rect->ymax * dims.y - offset.y - ymin;
519
520 RNA_int_set(&x_input_rna_pointer, "default_value", math::round(xmin));
521 RNA_int_set(&y_input_rna_pointer, "default_value", math::round(ymin));
522 RNA_int_set(&width_input_rna_pointer, "default_value", math::round(width));
523 RNA_int_set(&height_input_rna_pointer, "default_value", math::round(height));
524}
525
527 wmGizmoProperty *gz_prop,
528 void *value_p)
529{
530 float (*matrix)[4] = static_cast<float (*)[4]>(value_p);
531 BLI_assert(gz_prop->type->array_length == 16);
532 NodeBBoxWidgetGroup *crop_group = static_cast<NodeBBoxWidgetGroup *>(
534 const float2 dims = crop_group->state.dims;
535 const float2 offset = crop_group->state.offset;
536 const bNode *node = static_cast<const bNode *>(gz_prop->custom_func.user_data);
537
538 rctf rct;
539 node_input_to_rect(node, dims, offset, &rct);
540
541 matrix[0][0] = fabsf(BLI_rctf_size_x(&rct));
542 matrix[1][1] = fabsf(BLI_rctf_size_y(&rct));
543 matrix[3][0] = (BLI_rctf_cent_x(&rct) - 0.5f) * dims[0];
544 matrix[3][1] = (BLI_rctf_cent_y(&rct) - 0.5f) * dims[1];
545}
546
548 wmGizmoProperty *gz_prop,
549 const void *value_p)
550{
551 const float (*matrix)[4] = static_cast<const float (*)[4]>(value_p);
552 BLI_assert(gz_prop->type->array_length == 16);
553 NodeBBoxWidgetGroup *crop_group = static_cast<NodeBBoxWidgetGroup *>(
555 const float2 dims = crop_group->state.dims;
556 const float2 offset = crop_group->state.offset;
557 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
558
559 rctf rct;
560 node_input_to_rect(node, dims, offset, &rct);
561 BLI_rctf_resize(&rct, fabsf(matrix[0][0]), fabsf(matrix[1][1]));
562 BLI_rctf_recenter(&rct, ((matrix[3][0]) / dims[0]) + 0.5f, ((matrix[3][1]) / dims[1]) + 0.5f);
563 rctf rct_isect{};
564 rct_isect.xmin = offset.x / dims.x;
565 rct_isect.xmax = offset.x / dims.x + 1;
566 rct_isect.ymin = offset.y;
567 rct_isect.ymax = offset.y / dims.y + 1;
568 BLI_rctf_isect(&rct_isect, &rct, &rct);
569 node_input_from_rect(node, &rct, dims, offset);
570 gizmo_node_bbox_update(crop_group);
571}
572
573static bool show_crop_gizmo(const SpaceNode &snode)
574{
575 bNodeTree *node_tree = snode.edittree;
576 BLI_assert(node_tree);
577
578 bNode *node = bke::node_get_active(*node_tree);
579
580 if (!node || !node->is_type("CompositorNodeCrop"_ustr)) {
581 return false;
582 }
583
584 node_tree->ensure_topology_cache();
585 for (bNodeSocket &input : node->inputs) {
586 if (!STREQ(input.name, "Image") && input.is_directly_linked()) {
587 /* Note: the Image input could be connected to a single value input, in which case the
588 * gizmo has no effect. */
589 return false;
590 }
591 if (STREQ(input.name, "Alpha Crop") && !input.is_directly_linked()) {
592 PointerRNA input_rna_pointer = RNA_pointer_create_discrete(nullptr, RNA_NodeSocket, &input);
593 if (RNA_boolean_get(&input_rna_pointer, "default_value")) {
594 /* If Alpha Crop is not set, the image size changes depending on the input parameters,
595 * so we can't usefully edit the crop in this case. */
596 return true;
597 }
598 }
599 }
600
601 return false;
602}
603
605{
606 SpaceNode *snode = CTX_wm_space_node(C);
607 if (snode == nullptr) {
608 return false;
609 }
610 if (!node_gizmo_is_set_visible(*snode)) {
611 return false;
612 }
613
614 return show_crop_gizmo(*snode);
615}
616
618{
619 const SpaceImage *sima = CTX_wm_space_image(C);
620 if (sima == nullptr) {
621 return false;
622 }
623
624 if (!image_gizmo_is_set_visible(*sima)) {
625 return false;
626 }
627
628 const SpaceNode *snode = find_active_node_editor(C);
629 if (snode == nullptr || snode->edittree == nullptr) {
630 return false;
631 }
632
633 return show_crop_gizmo(*snode);
634}
635
637{
638 ARegion *region = CTX_wm_region(C);
639 wmGizmo *gz = gzgroup->gizmos.first();
640
641 SpaceNode *snode = CTX_wm_space_node(C);
642
643 node_gizmo_calc_matrix_space(region, snode->zoom, {-snode->xof, -snode->yof}, gz->matrix_space);
644}
645
647 wmGizmoProperty *gz_prop,
648 const FunctionRef<void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
649{
650 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
651
652 apply_socket_callback(*node, "X"_ustr, callback);
653 apply_socket_callback(*node, "Y"_ustr, callback);
654 apply_socket_callback(*node, "Width"_ustr, callback);
655 apply_socket_callback(*node, "Height"_ustr, callback);
656}
657
658void crop_refresh(const bContext *C, wmGizmoGroup *gzgroup)
659{
660 Main *bmain = CTX_data_main(C);
661 const SpaceNode *snode = find_active_node_editor(C);
662 BLI_assert(snode != nullptr);
663
664 NodeBBoxWidgetGroup *crop_group = static_cast<NodeBBoxWidgetGroup *>(gzgroup->customdata);
665 wmGizmo *gz = crop_group->border;
666
667 void *lock;
668 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Viewer Node");
669 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
670
671 if (ibuf == nullptr) [[unlikely]] {
673 BKE_image_release_ibuf(ima, ibuf, lock);
674 return;
675 }
676
678 crop_group->state.offset = flag_is_set(ibuf->flags, ImBufFlags::HasDisplayWindow) ?
679 float2(ibuf->display_offset) :
680 float2(0.0f);
681
682 RNA_float_set_array(gz->ptr, "dimensions", crop_group->state.dims);
684
686
687 crop_group->update_data.context = const_cast<bContext *>(C);
688 bNodeSocket *source_input = bke::node_find_socket(*node, SOCK_IN, "Alpha Crop"_ustr);
690 reinterpret_cast<ID *>(snode->edittree), RNA_NodeSocket, source_input);
691 crop_group->update_data.prop = RNA_struct_find_property(&crop_group->update_data.ptr, "enabled");
692 BLI_assert(crop_group->update_data.prop != nullptr);
693
697 params.range_get_fn = nullptr;
698 params.user_data = node;
700 WM_gizmo_target_property_def_func(gz, "matrix", &params);
701
702 BKE_image_release_ibuf(ima, ibuf, lock);
703}
704
705void crop_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
706{
707 NodeBBoxWidgetGroup *crop_group = MEM_new<NodeBBoxWidgetGroup>(__func__);
708 crop_group->border = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, nullptr);
709
710 RNA_enum_set(crop_group->border->ptr,
711 "transform",
713 RNA_enum_set(crop_group->border->ptr,
714 "draw_options",
717
718 gzgroup->customdata = crop_group;
719 gzgroup->customdata_free = [](void *customdata) {
720 MEM_delete(static_cast<NodeBBoxWidgetGroup *>(customdata));
721 };
722
724}
725
728
729 struct {
730 float2 dims;
733};
734
735static bool show_glare_gizmo(const SpaceNode &snode)
736{
737 bNodeTree *node_tree = snode.edittree;
738 BLI_assert(node_tree != nullptr);
739
740 bNode *node = bke::node_get_active(*node_tree);
741
742 if (!node || !node->is_type("CompositorNodeGlare"_ustr)) {
743 return false;
744 }
745
746 bNodeSocket &type_socket = *bke::node_find_socket(*node, SOCK_IN, "Type"_ustr);
747 node_tree->ensure_topology_cache();
748 if (type_socket.is_directly_linked()) {
749 return false;
750 }
751
752 if (type_socket.default_value_typed<bNodeSocketValueMenu>()->value != CMP_NODE_GLARE_SUN_BEAMS) {
753 return false;
754 }
755
756 for (bNodeSocket &input : node->inputs) {
757 if (STR_ELEM(input.name, "Sun Position") && input.is_directly_linked()) {
758 return false;
759 }
760 }
761 return true;
762}
763
765{
766 SpaceNode *snode = CTX_wm_space_node(C);
767 if (snode == nullptr) {
768 return false;
769 }
770 if (!node_gizmo_is_set_visible(*snode)) {
771 return false;
772 }
773
774 return show_glare_gizmo(*snode);
775}
776
778{
779
780 NodeGlareWidgetGroup *glare_group = static_cast<NodeGlareWidgetGroup *>(gzgroup->customdata);
781 ARegion *region = CTX_wm_region(C);
782 wmGizmo *gz = gzgroup->gizmos.first();
783
785 const float2 offset = float2{-sima->xof, -sima->yof} * sima->zoom;
786
788 sima->zoom,
789 offset,
790 glare_group->state.dims,
791 glare_group->state.offset,
792 gz->matrix_space);
793}
794
796{
797 const SpaceImage *sima = CTX_wm_space_image(C);
798 if (sima == nullptr) {
799 return false;
800 }
801
802 if (!image_gizmo_is_set_visible(*sima)) {
803 return false;
804 }
805
806 const SpaceNode *snode = find_active_node_editor(C);
807 if (snode == nullptr || snode->edittree == nullptr) {
808 return false;
809 }
810
811 return show_glare_gizmo(*snode);
812}
813
815{
816
817 NodeGlareWidgetGroup *glare_group = static_cast<NodeGlareWidgetGroup *>(gzgroup->customdata);
818 ARegion *region = CTX_wm_region(C);
819 wmGizmo *gz = gzgroup->gizmos.first();
820
821 SpaceNode *snode = CTX_wm_space_node(C);
822
824 snode->zoom,
825 {snode->xof, snode->yof},
826 glare_group->state.dims,
827 glare_group->state.offset,
828 gz->matrix_space);
829}
830
831void glare_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
832{
834
835 glare_group->gizmo = WM_gizmo_new("GIZMO_GT_move_3d", gzgroup, nullptr);
836 wmGizmo *gz = glare_group->gizmo;
837
839
840 gz->scale_basis = 0.05f / 75.0f;
841
842 gzgroup->customdata = glare_group;
843
845}
846
847void glare_refresh(const bContext *C, wmGizmoGroup *gzgroup)
848{
849 Main *bmain = CTX_data_main(C);
850 NodeGlareWidgetGroup *glare_group = static_cast<NodeGlareWidgetGroup *>(gzgroup->customdata);
851 wmGizmo *gz = glare_group->gizmo;
852
853 void *lock;
854 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Viewer Node");
855 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
856
857 if (ibuf == nullptr) [[unlikely]] {
859 BKE_image_release_ibuf(ima, ibuf, lock);
860 return;
861 }
862
864 glare_group->state.offset = flag_is_set(ibuf->flags, ImBufFlags::HasDisplayWindow) ?
865 float2(ibuf->display_offset) :
866 float2(0.0f);
867
869 BLI_assert(snode != nullptr);
870
872
873 /* Need to set property here for undo. TODO: would prefer to do this in _init. */
874 bNodeSocket *source_input = bke::node_find_socket(*node, SOCK_IN, "Sun Position"_ustr);
876 reinterpret_cast<ID *>(snode->edittree), RNA_NodeSocket, source_input);
877 WM_gizmo_target_property_def_rna(gz, "offset", &socket_pointer, "default_value", -1);
878
880
881 BKE_image_release_ibuf(ima, ibuf, lock);
882}
883
886
887 struct {
888 float2 dims;
891};
892
893static bool show_corner_pin(const SpaceNode &snode)
894{
896
897 if (node && node->is_type("CompositorNodeCornerPin"_ustr)) {
898 return true;
899 }
900
901 return false;
902}
903
905{
906 SpaceNode *snode = CTX_wm_space_node(C);
907 if (snode == nullptr) {
908 return false;
909 }
910 if (!node_gizmo_is_set_visible(*snode)) {
911 return false;
912 }
913
914 return show_corner_pin(*snode);
915}
916
918{
919 const SpaceImage *sima = CTX_wm_space_image(C);
920 if (sima == nullptr) {
921 return false;
922 }
923
924 if (!image_gizmo_is_set_visible(*sima)) {
925 return false;
926 }
927
928 const SpaceNode *snode = find_active_node_editor(C);
929 if (snode == nullptr || snode->edittree == nullptr) {
930 return false;
931 }
932
933 return show_corner_pin(*snode);
934}
935
937{
938 NodeCornerPinWidgetGroup *cpin_group = static_cast<NodeCornerPinWidgetGroup *>(
939 gzgroup->customdata);
940 ARegion *region = CTX_wm_region(C);
941
942 SpaceNode *snode = CTX_wm_space_node(C);
943
944 float matrix_space[4][4];
946 snode->zoom,
947 {snode->xof, snode->yof},
948 cpin_group->state.dims,
949 cpin_group->state.offset,
950 matrix_space);
951
952 for (int i = 0; i < 4; i++) {
953 wmGizmo *gz = cpin_group->gizmos[i];
954 copy_m4_m4(gz->matrix_space, matrix_space);
955 }
956}
957
959{
960
961 ARegion *region = CTX_wm_region(C);
963
964 NodeCornerPinWidgetGroup *cpin_group = static_cast<NodeCornerPinWidgetGroup *>(
965 gzgroup->customdata);
966
967 const float2 offset = float2{-sima->xof, -sima->yof} * sima->zoom;
968
969 for (wmGizmo &gz : gzgroup->gizmos) {
971 sima->zoom,
972 offset,
973 cpin_group->state.dims,
974 cpin_group->state.offset,
975 gz.matrix_space);
976 }
977}
978
979void corner_pin_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
980{
982 const wmGizmoType *gzt_move_3d = WM_gizmotype_find("GIZMO_GT_move_3d", false);
983
984 for (int i = 0; i < 4; i++) {
985 cpin_group->gizmos[i] = WM_gizmo_new_ptr(gzt_move_3d, gzgroup, nullptr);
986 wmGizmo *gz = cpin_group->gizmos[i];
987
989
990 gz->scale_basis = 0.05f / 75.0;
991 }
992
993 gzgroup->customdata = cpin_group;
994
996}
997
999{
1000 Main *bmain = CTX_data_main(C);
1001 NodeCornerPinWidgetGroup *cpin_group = static_cast<NodeCornerPinWidgetGroup *>(
1002 gzgroup->customdata);
1003
1004 void *lock;
1005 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Viewer Node");
1006 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
1007
1008 if (ibuf == nullptr) [[unlikely]] {
1009 for (int i = 0; i < 4; i++) {
1010 wmGizmo *gz = cpin_group->gizmos[i];
1012 }
1013 BKE_image_release_ibuf(ima, ibuf, lock);
1014 return;
1015 }
1016
1018 cpin_group->state.offset = flag_is_set(ibuf->flags, ImBufFlags::HasDisplayWindow) ?
1019 float2(ibuf->display_offset) :
1020 float2(0.0f);
1021
1023 BLI_assert(snode != nullptr);
1024
1026
1027 /* need to set property here for undo. TODO: would prefer to do this in _init. */
1028 int i = 0;
1029 for (bNodeSocket *sock = node->inputs.first(); sock && i < 4; sock = sock->next) {
1030 if (sock->type == SOCK_VECTOR) {
1031 wmGizmo *gz = cpin_group->gizmos[i++];
1032
1034 id_cast<ID *>(snode->edittree), RNA_NodeSocket, sock);
1035 WM_gizmo_target_property_def_rna(gz, "offset", &sockptr, "default_value", -1);
1036
1038 }
1039 }
1040
1041 BKE_image_release_ibuf(ima, ibuf, lock);
1042}
1043
1044static bool show_ellipse_mask_gizmo(const SpaceNode &snode)
1045{
1047
1048 if (node && node->is_type("CompositorNodeEllipseMask"_ustr)) {
1049 snode.edittree->ensure_topology_cache();
1050 for (bNodeSocket &input : node->inputs) {
1051 if (STR_ELEM(input.name, "Position", "Size", "Rotation") && input.is_directly_linked()) {
1052 return false;
1053 }
1054 }
1055 return true;
1056 }
1057
1058 return false;
1059}
1060
1062{
1063 SpaceNode *snode = CTX_wm_space_node(C);
1064 if (snode == nullptr) {
1065 return false;
1066 }
1067 if (!node_gizmo_is_set_visible(*snode)) {
1068 return false;
1069 }
1070
1071 return show_ellipse_mask_gizmo(*snode);
1072}
1073
1075{
1076 const SpaceImage *sima = CTX_wm_space_image(C);
1077 if (sima == nullptr) {
1078 return false;
1079 }
1080
1081 if (!image_gizmo_is_set_visible(*sima)) {
1082 return false;
1083 }
1084
1085 const SpaceNode *snode = find_active_node_editor(C);
1086 if (snode == nullptr || snode->edittree == nullptr) {
1087 return false;
1088 }
1089
1090 return show_ellipse_mask_gizmo(*snode);
1091}
1092
1093void ellipse_mask_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
1094{
1095 NodeBBoxWidgetGroup *mask_group = MEM_new<NodeBBoxWidgetGroup>(__func__);
1096 mask_group->border = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, nullptr);
1097
1098 RNA_enum_set(mask_group->border->ptr,
1099 "transform",
1102 RNA_enum_set(mask_group->border->ptr, "draw_style", ED_GIZMO_CAGE2D_STYLE_CIRCLE);
1103 RNA_enum_set(mask_group->border->ptr,
1104 "draw_options",
1107
1108 gzgroup->customdata = mask_group;
1109 gzgroup->customdata_free = [](void *customdata) {
1110 MEM_delete(static_cast<NodeBBoxWidgetGroup *>(customdata));
1111 };
1112
1113 gizmo_group_enable_undo(gzgroup);
1114}
1115
1117 wmGizmoProperty *gz_prop,
1118 const FunctionRef<void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
1119{
1120 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
1121
1122 apply_socket_callback(*node, "Position"_ustr, callback, -1);
1123 apply_socket_callback(*node, "Rotation"_ustr, callback);
1124}
1125
1127 wmGizmoProperty *gz_prop,
1128 void *value_p)
1129{
1130 float (*matrix)[4] = reinterpret_cast<float (*)[4]>(value_p);
1131 BLI_assert(gz_prop->type->array_length == 16);
1132 NodeBBoxWidgetGroup *split_group = static_cast<NodeBBoxWidgetGroup *>(
1134 const float2 dims = split_group->state.dims;
1135 const float2 offset = split_group->state.offset;
1136 const bNode *node = static_cast<const bNode *>(gz_prop->custom_func.user_data);
1137
1138 float loc[3], rot[3][3], size[3];
1139 mat4_to_loc_rot_size(loc, rot, size, matrix);
1140
1141 const bNodeSocket *pos_input = bke::node_find_socket(*node, SOCK_IN, "Position"_ustr);
1142 const float2 pos = pos_input->default_value_typed<bNodeSocketValueVector>()->value;
1143
1144 const bNodeSocket *rotation_input = bke::node_find_socket(*node, SOCK_IN, "Rotation"_ustr);
1145 const float rotation = rotation_input->default_value_typed<bNodeSocketValueFloat>()->value;
1146
1147 const float gizmo_width = 0.1f;
1148 axis_angle_to_mat3_single(rot, 'Z', rotation);
1150 matrix,
1151 float3{(pos.x - 0.5f) * dims.x + offset.x, (pos.y - 0.5f) * dims.y + offset.y, 0.0f},
1152 rot,
1153 float3{gizmo_width, std::numeric_limits<float>::epsilon(), 1.0f});
1154}
1155
1157 wmGizmoProperty *gz_prop,
1158 const void *value_p)
1159{
1160 const float (*matrix)[4] = reinterpret_cast<const float (*)[4]>(value_p);
1161 BLI_assert(gz_prop->type->array_length == 16);
1162 NodeBBoxWidgetGroup *split_group = reinterpret_cast<NodeBBoxWidgetGroup *>(
1164 const float2 dims = split_group->state.dims;
1165 const float2 offset = split_group->state.offset;
1166 bNode *node = reinterpret_cast<bNode *>(gz_prop->custom_func.user_data);
1167
1168 bNodeSocket *position_input = bke::node_find_socket(*node, SOCK_IN, "Position"_ustr);
1169 bNodeSocket *rotation_input = bke::node_find_socket(*node, SOCK_IN, "Rotation"_ustr);
1170
1171 float pos_x = (matrix[3][0] - offset.x) + dims.x * 0.5;
1172 float pos_y = (matrix[3][1] - offset.y) + dims.y * 0.5;
1173
1174 /* Prevent dragging the gizmo outside the image. */
1175 pos_x = math::clamp(pos_x, 0.0f, dims.x);
1176 pos_y = math::clamp(pos_y, 0.0f, dims.y);
1177
1178 position_input->default_value_typed<bNodeSocketValueVector>()->value[0] = pos_x / dims.x;
1179 position_input->default_value_typed<bNodeSocketValueVector>()->value[1] = pos_y / dims.y;
1180
1181 float3 eul;
1182 mat4_to_eul(eul, matrix);
1183
1184 rotation_input->default_value_typed<bNodeSocketValueFloat>()->value = eul[2];
1185
1186 gizmo_node_bbox_update(split_group);
1187}
1188
1189static bool show_split(const SpaceNode &snode)
1190{
1192
1193 if (node && node->is_type("CompositorNodeSplit"_ustr)) {
1194 snode.edittree->ensure_topology_cache();
1195 for (bNodeSocket &input : node->inputs) {
1196 if (STR_ELEM(input.name, "Position", "Rotation") && input.is_directly_linked()) {
1197 return false;
1198 }
1199 }
1200 return true;
1201 }
1202
1203 return false;
1204}
1205
1206void split_refresh(const bContext *C, wmGizmoGroup *gzgroup)
1207{
1208 Main *bmain = CTX_data_main(C);
1209 NodeBBoxWidgetGroup *split_group = reinterpret_cast<NodeBBoxWidgetGroup *>(gzgroup->customdata);
1210 wmGizmo *gz = split_group->border;
1211
1212 void *lock;
1213 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Render Result");
1214 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
1215
1216 if (ibuf == nullptr) [[unlikely]] {
1218 BKE_image_release_ibuf(ima, ibuf, lock);
1219 return;
1220 }
1221
1222 /* Larger fallback size otherwise the gizmo would be partially hidden. */
1223 split_group->state.dims = node_gizmo_safe_calc_dims(ibuf, float2{1000.0f, 1000.0f});
1224 split_group->state.offset = flag_is_set(ibuf->flags, ImBufFlags::HasDisplayWindow) ?
1225 float2(ibuf->display_offset) :
1226 float2(0.0f);
1227
1228 RNA_float_set_array(gz->ptr, "dimensions", split_group->state.dims);
1230
1232 BLI_assert(snode != nullptr);
1233
1235
1236 split_group->update_data.context = const_cast<bContext *>(C);
1237 bNodeSocket *source_input = bke::node_find_socket(*node, SOCK_IN, "Position"_ustr);
1238 split_group->update_data.ptr = RNA_pointer_create_discrete(
1239 reinterpret_cast<ID *>(snode->edittree), RNA_NodeSocket, source_input);
1240 split_group->update_data.prop = RNA_struct_find_property(&split_group->update_data.ptr,
1241 "enabled");
1242
1243 wmGizmoPropertyFnParams params{};
1246 params.range_get_fn = nullptr;
1247 params.user_data = node;
1249 WM_gizmo_target_property_def_func(gz, "matrix", &params);
1250
1251 BKE_image_release_ibuf(ima, ibuf, lock);
1252}
1253
1255{
1256 SpaceNode *snode = CTX_wm_space_node(C);
1257 if (snode == nullptr) {
1258 return false;
1259 }
1260 if (!node_gizmo_is_set_visible(*snode)) {
1261 return false;
1262 }
1263
1264 return show_split(*snode);
1265}
1266
1268{
1269 const SpaceImage *sima = CTX_wm_space_image(C);
1270 if (sima == nullptr) {
1271 return false;
1272 }
1273
1274 if (!image_gizmo_is_set_visible(*sima)) {
1275 return false;
1276 }
1277
1278 const SpaceNode *snode = find_active_node_editor(C);
1279 if (snode == nullptr || snode->edittree == nullptr) {
1280 return false;
1281 }
1282
1283 return show_split(*snode);
1284}
1285
1286void split_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
1287{
1288 NodeBBoxWidgetGroup *split_group = MEM_new<NodeBBoxWidgetGroup>(__func__);
1289 split_group->border = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, nullptr);
1290
1291 RNA_enum_set(split_group->border->ptr,
1292 "transform",
1294 RNA_enum_set(split_group->border->ptr, "draw_options", ED_GIZMO_CAGE_DRAW_FLAG_NOP);
1295
1296 gzgroup->customdata = split_group;
1297 gzgroup->customdata_free = [](void *customdata) {
1298 MEM_delete(static_cast<NodeBBoxWidgetGroup *>(customdata));
1299 };
1300
1301 gizmo_group_enable_undo(gzgroup);
1302}
1303
1311
1313 wmGizmoProperty *gz_prop,
1314 void *value_p)
1315{
1316 float (*matrix)[4] = static_cast<float (*)[4]>(value_p);
1317 BLI_assert(gz_prop->type->array_length == 16);
1318 const SpaceNode *snode = static_cast<const SpaceNode *>(gz_prop->custom_func.user_data);
1319 NodeTransformWidgetGroup *transform_group = static_cast<NodeTransformWidgetGroup *>(
1321 const float2 offset = transform_group->state.offset;
1322 matrix[0][0] = snode->zoom;
1323 matrix[1][1] = snode->zoom;
1324 matrix[3][0] = snode->xof + (offset.x - 0.5f) * snode->zoom;
1325 matrix[3][1] = snode->yof + (offset.y - 0.5f) * snode->zoom;
1326}
1327
1329 wmGizmoProperty *gz_prop,
1330 const void *value_p)
1331{
1332 const float (*matrix)[4] = static_cast<const float (*)[4]>(value_p);
1333 BLI_assert(gz_prop->type->array_length == 16);
1334 NodeTransformWidgetGroup *transform_group = static_cast<NodeTransformWidgetGroup *>(
1336 const float2 offset = transform_group->state.offset;
1337 SpaceNode *snode = static_cast<SpaceNode *>(gz_prop->custom_func.user_data);
1338 snode->zoom = matrix[0][0];
1339 snode->xof = matrix[3][0] - (offset.x - 0.5f) * snode->zoom;
1340 snode->yof = matrix[3][1] - (offset.y - 0.5f) * snode->zoom;
1341}
1342
1343bool viewer_poll(const bContext *C, wmGizmoGroupType * /*gzgt*/)
1344{
1345 SpaceNode *snode = CTX_wm_space_node(C);
1346 if (snode == nullptr) {
1347 return false;
1348 }
1349 if (!node_gizmo_is_set_visible(*snode)) {
1350 return false;
1351 }
1352
1354
1355 if (node && node->is_type("CompositorNodeViewer"_ustr)) {
1356 return true;
1357 }
1358
1359 return false;
1360}
1361
1362void viewer_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
1363{
1364 /* The backdrop transform only changes the editor view, so it does not create undo steps. */
1365 NodeTransformWidgetGroup *transform_group = MEM_new<NodeTransformWidgetGroup>(__func__);
1366
1367 transform_group->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, nullptr);
1368
1369 RNA_enum_set(transform_group->gizmo->ptr,
1370 "transform",
1372 RNA_enum_set(transform_group->gizmo->ptr,
1373 "draw_options",
1376
1377 gzgroup->customdata = transform_group;
1378 gzgroup->customdata_free = [](void *customdata) {
1379 MEM_delete(static_cast<NodeTransformWidgetGroup *>(customdata));
1380 };
1381}
1382
1384{
1385 Main *bmain = CTX_data_main(C);
1386 NodeTransformWidgetGroup *transform_group = static_cast<NodeTransformWidgetGroup *>(
1387 gzgroup->customdata);
1388 wmGizmo *cage = transform_group->gizmo;
1389 const ARegion *region = CTX_wm_region(C);
1390 /* center is always at the origin */
1391 const float origin[3] = {float(region->winx / 2), float(region->winy / 2), 0.0f};
1392
1393 void *lock;
1394 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Viewer Node");
1395 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
1396
1397 if (ibuf == nullptr) [[unlikely]] {
1399 BKE_image_release_ibuf(ima, ibuf, lock);
1400 return;
1401 }
1402
1403 transform_group->state.offset = flag_is_set(ibuf->flags, ImBufFlags::HasDisplayWindow) ?
1404 float2(ibuf->display_offset) :
1405 float2(0.0f);
1407
1408 RNA_float_set_array(cage->ptr, "dimensions", dims);
1409 WM_gizmo_set_matrix_location(cage, origin);
1410 WM_gizmo_set_flag(cage, WM_GIZMO_HIDDEN, false);
1411
1412 /* Need to set property here for undo. TODO: would prefer to do this in _init. */
1413 SpaceNode *snode = CTX_wm_space_node(C);
1414#if 0
1415 PointerRNA nodeptr = RNA_pointer_create_discrete(snode->id, RNA_SpaceNodeEditor, snode);
1416 WM_gizmo_target_property_def_rna(cage, "offset", &nodeptr, "backdrop_offset", -1);
1417 WM_gizmo_target_property_def_rna(cage, "scale", &nodeptr, "backdrop_zoom", -1);
1418#endif
1419
1420 wmGizmoPropertyFnParams params{};
1423 params.range_get_fn = nullptr;
1424 params.user_data = snode;
1425 WM_gizmo_target_property_def_func(cage, "matrix", &params);
1426
1427 BKE_image_release_ibuf(ima, ibuf, lock);
1428}
1429
1431 wmGizmoProperty *gz_prop,
1432 void *value_p)
1433{
1434 float *gz_offset = static_cast<float(*)>(value_p);
1435 BLI_assert(gz_prop->type->array_length == 3);
1436 NodeBBoxWidgetGroup *translate_group = static_cast<NodeBBoxWidgetGroup *>(
1438 const float2 offset = translate_group->state.offset;
1439 const bNode *node = static_cast<const bNode *>(gz_prop->custom_func.user_data);
1440
1441 const bNodeSocket *x_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
1442 const float x = x_input->default_value_typed<bNodeSocketValueFloat>()->value;
1443
1444 const bNodeSocket *y_input = bke::node_find_socket(*node, SOCK_IN, "Y"_ustr);
1445 const float y = y_input->default_value_typed<bNodeSocketValueFloat>()->value;
1446
1447 gz_offset[0] = x + offset.x;
1448 gz_offset[1] = y + offset.y;
1449 gz_offset[2] = 0.0f;
1450}
1451
1453 wmGizmoProperty *gz_prop,
1454 const void *value_p)
1455{
1456 const float *gz_offset = static_cast<const float(*)>(value_p);
1457 BLI_assert(gz_prop->type->array_length == 3);
1458 NodeBBoxWidgetGroup *translate_group = static_cast<NodeBBoxWidgetGroup *>(
1460 const float2 offset = translate_group->state.offset;
1461 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
1462
1463 bNodeSocket *x_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
1464 bNodeSocket *y_input = bke::node_find_socket(*node, SOCK_IN, "Y"_ustr);
1465
1466 x_input->default_value_typed<bNodeSocketValueFloat>()->value = gz_offset[0] - offset.x;
1467 y_input->default_value_typed<bNodeSocketValueFloat>()->value = gz_offset[1] - offset.y;
1468
1469 gizmo_node_bbox_update(translate_group);
1470}
1471
1472void translate_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
1473{
1474 NodeBBoxWidgetGroup *translate_group = MEM_new<NodeBBoxWidgetGroup>(__func__);
1475
1476 translate_group->state.dims = GIZMO_NODE_DEFAULT_DIMS;
1477 translate_group->state.offset = float2(0.0f);
1478
1479 translate_group->border = WM_gizmo_new("GIZMO_GT_move_3d", gzgroup, nullptr);
1480
1481 RNA_enum_set(translate_group->border->ptr, "draw_style", ED_GIZMO_MOVE_STYLE_CROSS_2D);
1482
1483 gzgroup->customdata = translate_group;
1484 gzgroup->customdata_free = [](void *customdata) {
1485 MEM_delete(static_cast<NodeBBoxWidgetGroup *>(customdata));
1486 };
1487
1488 gizmo_group_enable_undo(gzgroup);
1489}
1490
1492 const bNode &node)
1493{
1494 const bNodeSocket *image_input = bke::node_find_socket(node, SOCK_IN, "Image"_ustr);
1495 if (!image_input) {
1496 return nullptr;
1497 }
1498
1500 snode);
1501 eval_log::NodeTreeLog *tree_log = tree_logs.get_main_tree_log(*image_input);
1502 if (!tree_log) {
1503 return nullptr;
1504 }
1505
1506 tree_log->ensure_socket_values();
1507 return dynamic_cast<const eval_log::ImageInfoLog *>(
1508 tree_log->find_socket_value_log(*image_input));
1509}
1510
1512{
1513 const eval_log::ImageInfoLog *image_log = gizmo_input_image_log(snode, node);
1514 if (!image_log) {
1515 return float2(0.0f);
1516 }
1517
1518 /* Logging considers data and display size. */
1519 return image_log->transformation.location();
1520}
1521
1523 wmGizmoProperty *gz_prop,
1524 const FunctionRef<void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
1525{
1526 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
1527
1528 apply_socket_callback(*node, "X"_ustr, callback);
1529 apply_socket_callback(*node, "Y"_ustr, callback);
1530}
1531
1533{
1534 Main *bmain = CTX_data_main(C);
1535 NodeBBoxWidgetGroup *translate_group = static_cast<NodeBBoxWidgetGroup *>(gzgroup->customdata);
1536 wmGizmo *gz = translate_group->border;
1537
1538 void *lock;
1539 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Viewer Node");
1540 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
1541
1542 if (ibuf == nullptr) [[unlikely]] {
1544 BKE_image_release_ibuf(ima, ibuf, lock);
1545 return;
1546 }
1547
1548 translate_group->state.dims = node_gizmo_safe_calc_dims(ibuf, GIZMO_NODE_DEFAULT_DIMS);
1549
1551 BLI_assert(snode != nullptr);
1552
1554
1555 if (should_sync_gizmo_state_from_log(*snode, *node, *gz)) {
1556 translate_group->state.offset = translate_gizmo_input_offset(*snode, *node);
1557 }
1558 translate_group->update_data.context = const_cast<bContext *>(C);
1559 bNodeSocket *source_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
1560 translate_group->update_data.ptr = RNA_pointer_create_discrete(
1561 reinterpret_cast<ID *>(snode->edittree), RNA_NodeSocket, source_input);
1562 translate_group->update_data.prop = RNA_struct_find_property(&translate_group->update_data.ptr,
1563 "enabled");
1564 BLI_assert(translate_group->update_data.prop != nullptr);
1565
1566 wmGizmoPropertyFnParams params{};
1569 params.range_get_fn = nullptr;
1570 params.user_data = node;
1572 WM_gizmo_target_property_def_func(gz, "offset", &params);
1573
1575
1576 BKE_image_release_ibuf(ima, ibuf, lock);
1577}
1578
1579static bool show_translate_gizmo(const SpaceNode &snode)
1580{
1581 bNodeTree *node_tree = snode.edittree;
1582 BLI_assert(node_tree != nullptr);
1583
1584 bNode *node = bke::node_get_active(*node_tree);
1585
1586 if (!node || !node->is_type("CompositorNodeTranslate"_ustr)) {
1587 return false;
1588 }
1589
1590 node_tree->ensure_topology_cache();
1591 for (bNodeSocket &input : node->inputs) {
1592 if (STR_ELEM(input.name, "X", "Y") && input.is_directly_linked()) {
1593 return false;
1594 }
1595 }
1596 return true;
1597}
1598
1600{
1601 SpaceNode *snode = CTX_wm_space_node(C);
1602 if (snode == nullptr) {
1603 return false;
1604 }
1605 if (!node_gizmo_is_set_visible(*snode)) {
1606 return false;
1607 }
1608
1609 return show_translate_gizmo(*snode);
1610}
1611
1613{
1614 const SpaceImage *sima = CTX_wm_space_image(C);
1615 if (sima == nullptr) {
1616 return false;
1617 }
1618
1619 if (!image_gizmo_is_set_visible(*sima)) {
1620 return false;
1621 }
1622
1623 const SpaceNode *snode = find_active_node_editor(C);
1624 if (snode == nullptr || snode->edittree == nullptr) {
1625 return false;
1626 }
1627
1628 return show_translate_gizmo(*snode);
1629}
1630
1631static bool show_transform_gizmo(const SpaceNode &snode)
1632{
1633 bNodeTree *node_tree = snode.edittree;
1634 BLI_assert(node_tree != nullptr);
1635
1636 bNode *node = bke::node_get_active(*node_tree);
1637
1638 if (!node || !node->is_type("CompositorNodeTransform"_ustr)) {
1639 return false;
1640 }
1641
1642 node_tree->ensure_topology_cache();
1643 for (bNodeSocket &input : node->inputs) {
1644 if (STR_ELEM(input.identifier, "X", "Y", "Angle", "Scale") && input.is_directly_linked()) {
1645 return false;
1646 }
1647 }
1648 return true;
1649}
1650
1652{
1653 const SpaceImage *sima = CTX_wm_space_image(C);
1654 if (sima == nullptr) {
1655 return false;
1656 }
1657
1658 if (!image_gizmo_is_set_visible(*sima)) {
1659 return false;
1660 }
1661
1662 const SpaceNode *snode = find_active_node_editor(C);
1663 if (snode == nullptr || snode->edittree == nullptr) {
1664 return false;
1665 }
1666
1667 return show_transform_gizmo(*snode);
1668}
1669
1671{
1672 SpaceNode *snode = CTX_wm_space_node(C);
1673 if (snode == nullptr) {
1674 return false;
1675 }
1676 if (!node_gizmo_is_set_visible(*snode)) {
1677 return false;
1678 }
1679
1680 return show_transform_gizmo(*snode);
1681}
1682
1683void transform_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
1684{
1685 NodeBBoxWidgetGroup *transform_group = MEM_new<NodeBBoxWidgetGroup>(__func__);
1686
1687 transform_group->state.dims = GIZMO_NODE_DEFAULT_DIMS;
1688 transform_group->state.offset = float2(0.0f);
1689
1690 transform_group->border = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, nullptr);
1691
1692 RNA_enum_set(transform_group->border->ptr,
1693 "transform",
1696
1697 RNA_enum_set(transform_group->border->ptr,
1698 "draw_options",
1701
1702 gzgroup->customdata = transform_group;
1703 gzgroup->customdata_free = [](void *customdata) {
1704 MEM_delete(static_cast<NodeBBoxWidgetGroup *>(customdata));
1705 };
1706
1707 gizmo_group_enable_undo(gzgroup);
1708}
1709
1711 const float2 &scale,
1712 const float rotation)
1713{
1714 const float2 scaled_offset = offset * scale;
1715 float2 transformed_offset;
1716 /* The nodes transform the input image around the origin of the compositing space, so the
1717 * location of the input image is transformed as well. */
1718 rotate_v2_v2fl(transformed_offset, scaled_offset, rotation);
1719 return transformed_offset;
1720}
1721
1723 wmGizmoProperty *gz_prop,
1724 void *value_p)
1725{
1726 float (*matrix)[4] = static_cast<float (*)[4]>(value_p);
1727 BLI_assert(gz_prop->type->array_length == 16);
1728 NodeBBoxWidgetGroup *mask_group = static_cast<NodeBBoxWidgetGroup *>(
1730 // const float2 dims = mask_group->state.dims;
1731 const float2 offset = mask_group->state.offset;
1732 const bNode *node = static_cast<const bNode *>(gz_prop->custom_func.user_data);
1733
1734 float loc[3], rot[3][3], size[3];
1735 mat4_to_loc_rot_size(loc, rot, size, matrix);
1736
1737 const bNodeSocket *rotation_input = bke::node_find_socket(*node, SOCK_IN, "Angle"_ustr);
1738 const float rotation = rotation_input->default_value_typed<bNodeSocketValueFloat>()->value;
1739 axis_angle_to_mat3_single(rot, 'Z', rotation);
1740
1741 const bNodeSocket *scale_input = bke::node_find_socket(*node, SOCK_IN, "Scale"_ustr);
1742 const float scale = scale_input->default_value_typed<bNodeSocketValueFloat>()->value;
1743 size[0] = scale;
1744 size[1] = scale;
1745 size[2] = 1.0f;
1746
1747 const bNodeSocket *x_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
1748 const float x = x_input->default_value_typed<bNodeSocketValueFloat>()->value;
1749
1750 const bNodeSocket *y_input = bke::node_find_socket(*node, SOCK_IN, "Y"_ustr);
1751 const float y = y_input->default_value_typed<bNodeSocketValueFloat>()->value;
1752
1753 const float2 transformed_offset = node_gizmo_transformed_offset(offset, float2(scale), rotation);
1754 loc[0] = x + transformed_offset.x;
1755 loc[1] = y + transformed_offset.y;
1756 loc[2] = 0.0f;
1757
1758 loc_rot_size_to_mat4(matrix, loc, rot, size);
1759}
1760
1762 wmGizmoProperty *gz_prop,
1763 const void *value_p)
1764{
1765 const float (*matrix)[4] = static_cast<const float (*)[4]>(value_p);
1766 BLI_assert(gz_prop->type->array_length == 16);
1767 NodeBBoxWidgetGroup *transform_group = static_cast<NodeBBoxWidgetGroup *>(
1769 const float2 offset = transform_group->state.offset;
1770 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
1771
1772 bNodeSocket *x_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
1773 bNodeSocket *y_input = bke::node_find_socket(*node, SOCK_IN, "Y"_ustr);
1774 bNodeSocket *scale_input = bke::node_find_socket(*node, SOCK_IN, "Scale"_ustr);
1775 bNodeSocket *rotation_input = bke::node_find_socket(*node, SOCK_IN, "Angle"_ustr);
1776
1777 float loc[3];
1778 float rot[3][3];
1779 float size[3];
1780 mat4_to_loc_rot_size(loc, rot, size, matrix);
1781
1782 float eul[3];
1783
1784 /* Rotation can't be extracted from matrix when the gizmo width or height is zero. */
1785 bNodeSocketValueFloat *rotation = rotation_input->default_value_typed<bNodeSocketValueFloat>();
1786 if (size[0] != 0 and size[1] != 0) {
1787 /* Take angle closest to the current angle. */
1788 const float oldrot[3] = {0.0f, 0.0f, rotation->value};
1789 mat4_to_compatible_eulO(eul, oldrot, EULER_ORDER_XYZ, matrix);
1790 rotation->value = eul[2];
1791 }
1792
1793 const float scale = size[0];
1794 scale_input->default_value_typed<bNodeSocketValueFloat>()->value = scale;
1795
1796 const float2 transformed_offset = node_gizmo_transformed_offset(
1797 offset, float2(scale), rotation->value);
1798 x_input->default_value_typed<bNodeSocketValueFloat>()->value = loc[0] - transformed_offset.x;
1799 y_input->default_value_typed<bNodeSocketValueFloat>()->value = loc[1] - transformed_offset.y;
1800
1801 gizmo_node_bbox_update(transform_group);
1802}
1803
1804/* For transform gizmos, the image at a node's input is queried from the evaluation log, because
1805 * neither the node's input sockets nor the final #ImBuf describe it individually. When two
1806 * translate nodes are chained for example, #ImBuf::display_offset holds the combined result of
1807 * both nodes, whereas each gizmo needs the result of its node alone. */
1809 const bNode &node,
1810 const float2 &fallback)
1811{
1812 const eval_log::ImageInfoLog *image_log = gizmo_input_image_log(snode, node);
1813 if (!image_log) {
1814 return fallback;
1815 }
1816
1817 /* The logged size does not have the image transformation applied. */
1818 const float3x3 &transformation = image_log->transformation;
1819 const float2 scale = float2(math::length(transformation.x_axis()),
1820 math::length(transformation.y_axis()));
1821
1822 /* Logging considers data and display size. */
1823 return float2(image_log->data_size) * scale;
1824}
1825
1827 wmGizmoProperty *gz_prop,
1828 const FunctionRef<void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
1829{
1830 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
1831
1832 apply_socket_callback(*node, "X"_ustr, callback);
1833 apply_socket_callback(*node, "Y"_ustr, callback);
1834 apply_socket_callback(*node, "Angle"_ustr, callback);
1835 apply_socket_callback(*node, "Scale"_ustr, callback);
1836}
1837
1839{
1840 Main *bmain = CTX_data_main(C);
1841 NodeBBoxWidgetGroup *transform_group = static_cast<NodeBBoxWidgetGroup *>(gzgroup->customdata);
1842 wmGizmo *gz = transform_group->border;
1843
1844 void *lock;
1845 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Render Result");
1846 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
1847
1848 if (ibuf == nullptr) [[unlikely]] {
1850 BKE_image_release_ibuf(ima, ibuf, lock);
1851 return;
1852 }
1853
1855
1856 const SpaceNode *snode = find_active_node_editor(C);
1857 BLI_assert(snode != nullptr);
1858
1860
1861 if (should_sync_gizmo_state_from_log(*snode, *node, *gz)) {
1862 transform_group->state.offset = translate_gizmo_input_offset(*snode, *node);
1863 /* Fall back to the size of the viewer image if the input size is not known, which is closer to
1864 * the actual size than an arbitrary default. */
1866 transform_group->state.dims = gizmo_input_dims_from_log(*snode, *node, fallback);
1867 }
1868 RNA_float_set_array(gz->ptr, "dimensions", transform_group->state.dims);
1869
1870 transform_group->update_data.context = const_cast<bContext *>(C);
1871 bNodeSocket *source_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
1872 transform_group->update_data.ptr = RNA_pointer_create_discrete(
1873 reinterpret_cast<ID *>(snode->edittree), RNA_NodeSocket, source_input);
1874 transform_group->update_data.prop = RNA_struct_find_property(&transform_group->update_data.ptr,
1875 "enabled");
1876 BLI_assert(transform_group->update_data.prop != nullptr);
1877
1878 wmGizmoPropertyFnParams params{};
1881 params.range_get_fn = nullptr;
1882 params.user_data = node;
1884 WM_gizmo_target_property_def_func(gz, "matrix", &params);
1885
1886 BKE_image_release_ibuf(ima, ibuf, lock);
1887}
1888
1890{
1891 const bNodeSocket *type_input = bke::node_find_socket(node, SOCK_IN, "Type"_ustr);
1892 return type_input->default_value_typed<bNodeSocketValueMenu>()->value == CMP_NODE_SCALE_ABSOLUTE;
1893}
1894
1896{
1897 const bNodeSocket *x_input = bke::node_find_socket(node, SOCK_IN, "X"_ustr);
1898 const bNodeSocket *y_input = bke::node_find_socket(node, SOCK_IN, "Y"_ustr);
1899 const float2 value = float2(x_input->default_value_typed<bNodeSocketValueFloat>()->value,
1900 y_input->default_value_typed<bNodeSocketValueFloat>()->value);
1901
1902 return node_scale_is_absolute(node) ? value / dims : value;
1903}
1904
1905static void node_scale_gizmo_scale_set(bNode &node, const float2 &dims, const float2 &scale)
1906{
1907 bNodeSocket *x_input = bke::node_find_socket(node, SOCK_IN, "X"_ustr);
1908 bNodeSocket *y_input = bke::node_find_socket(node, SOCK_IN, "Y"_ustr);
1909
1910 const float2 value = node_scale_is_absolute(node) ? scale * dims : scale;
1911
1912 x_input->default_value_typed<bNodeSocketValueFloat>()->value = value.x;
1913 y_input->default_value_typed<bNodeSocketValueFloat>()->value = value.y;
1914}
1915
1917 wmGizmoProperty *gz_prop,
1918 void *value_p)
1919{
1920 float (*matrix)[4] = static_cast<float (*)[4]>(value_p);
1921 BLI_assert(gz_prop->type->array_length == 16);
1922 const NodeBBoxWidgetGroup *scale_group = static_cast<NodeBBoxWidgetGroup *>(
1924 const float2 dims = scale_group->state.dims;
1925 const float2 offset = scale_group->state.offset;
1926 const bNode *node = static_cast<const bNode *>(gz_prop->custom_func.user_data);
1927
1929
1930 float rot[3][3];
1931 unit_m3(rot);
1932
1933 const float2 location = node_gizmo_transformed_offset(offset, scale, 0.0f);
1935 matrix, float3{location.x, location.y, 0.0f}, rot, float3{scale.x, scale.y, 1.0f});
1936}
1937
1939 wmGizmoProperty *gz_prop,
1940 const void *value_p)
1941{
1942 const float (*matrix)[4] = static_cast<const float (*)[4]>(value_p);
1943 BLI_assert(gz_prop->type->array_length == 16);
1944 NodeBBoxWidgetGroup *scale_group = static_cast<NodeBBoxWidgetGroup *>(
1946 const float2 dims = scale_group->state.dims;
1947 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
1948
1949 float loc[3], rot[3][3], size[3];
1950 mat4_to_loc_rot_size(loc, rot, size, matrix);
1951
1952 node_scale_gizmo_scale_set(*node, dims, float2(size[0], size[1]));
1953
1954 gizmo_node_bbox_update(scale_group);
1955}
1956
1958 wmGizmoProperty *gz_prop,
1959 const FunctionRef<void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
1960{
1961 bNode *node = static_cast<bNode *>(gz_prop->custom_func.user_data);
1962
1963 apply_socket_callback(*node, "X"_ustr, callback);
1964 apply_socket_callback(*node, "Y"_ustr, callback);
1965}
1966
1967void scale_refresh(const bContext *C, wmGizmoGroup *gzgroup)
1968{
1969 Main *bmain = CTX_data_main(C);
1970 NodeBBoxWidgetGroup *scale_group = static_cast<NodeBBoxWidgetGroup *>(gzgroup->customdata);
1971 wmGizmo *gz = scale_group->border;
1972
1973 void *lock;
1974 Image *ima = BKE_image_ensure_viewer(bmain, IMA_TYPE_COMPOSITE, "Render Result");
1975 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, nullptr, &lock);
1976
1977 if (ibuf == nullptr) [[unlikely]] {
1979 BKE_image_release_ibuf(ima, ibuf, lock);
1980 return;
1981 }
1982
1984
1985 const SpaceNode *snode = find_active_node_editor(C);
1986 BLI_assert(snode != nullptr);
1987
1989
1990 if (should_sync_gizmo_state_from_log(*snode, *node, *gz)) {
1991 scale_group->state.offset = translate_gizmo_input_offset(*snode, *node);
1993 scale_group->state.dims = gizmo_input_dims_from_log(*snode, *node, fallback);
1994 }
1995 RNA_float_set_array(gz->ptr, "dimensions", scale_group->state.dims);
1996
1997 scale_group->update_data.context = const_cast<bContext *>(C);
1998 bNodeSocket *source_input = bke::node_find_socket(*node, SOCK_IN, "X"_ustr);
1999 scale_group->update_data.ptr = RNA_pointer_create_discrete(
2000 reinterpret_cast<ID *>(snode->edittree), RNA_NodeSocket, source_input);
2001 scale_group->update_data.prop = RNA_struct_find_property(&scale_group->update_data.ptr,
2002 "enabled");
2003 BLI_assert(scale_group->update_data.prop != nullptr);
2004
2005 wmGizmoPropertyFnParams params{};
2008 params.range_get_fn = nullptr;
2009 params.user_data = node;
2011 WM_gizmo_target_property_def_func(gz, "matrix", &params);
2012
2013 BKE_image_release_ibuf(ima, ibuf, lock);
2014}
2015void scale_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
2016{
2017 NodeBBoxWidgetGroup *scale_group = MEM_new<NodeBBoxWidgetGroup>(__func__);
2018
2019 scale_group->state.dims = GIZMO_NODE_DEFAULT_DIMS;
2020 scale_group->state.offset = float2(0.0f);
2021
2022 scale_group->border = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, nullptr);
2023
2024 RNA_enum_set(scale_group->border->ptr, "transform", ED_GIZMO_CAGE_XFORM_FLAG_SCALE);
2025 RNA_enum_set(scale_group->border->ptr, "draw_options", ED_GIZMO_CAGE_DRAW_FLAG_CORNER_HANDLES);
2026
2027 gzgroup->customdata = scale_group;
2028 gzgroup->customdata_free = [](void *customdata) {
2029 MEM_delete(static_cast<NodeBBoxWidgetGroup *>(customdata));
2030 };
2031
2032 gizmo_group_enable_undo(gzgroup);
2033}
2034
2035static bool show_scale_gizmo(const SpaceNode &snode)
2036{
2037 bNodeTree *node_tree = snode.edittree;
2038 BLI_assert(node_tree != nullptr);
2039
2040 bNode *node = bke::node_get_active(*node_tree);
2041
2042 if (!node || !node->is_type("CompositorNodeScale"_ustr)) {
2043 return false;
2044 }
2045
2046 node_tree->ensure_topology_cache();
2047 for (bNodeSocket &input : node->inputs) {
2048 if (STR_ELEM(input.identifier, "X", "Y", "Type") && input.is_directly_linked()) {
2049 return false;
2050 }
2051 }
2052 return true;
2053}
2054
2056{
2057 SpaceNode *snode = CTX_wm_space_node(C);
2058 if (snode == nullptr) {
2059 return false;
2060 }
2061 if (!node_gizmo_is_set_visible(*snode)) {
2062 return false;
2063 }
2064
2065 return show_scale_gizmo(*snode);
2066}
2067
2069{
2070 const SpaceImage *sima = CTX_wm_space_image(C);
2071 if (sima == nullptr) {
2072 return false;
2073 }
2074
2075 if (!image_gizmo_is_set_visible(*sima)) {
2076 return false;
2077 }
2078
2079 const SpaceNode *snode = find_active_node_editor(C);
2080 if (snode == nullptr || snode->edittree == nullptr) {
2081 return false;
2082 }
2083
2084 return show_scale_gizmo(*snode);
2085}
2086
2087/* -------------------------------------------------------------------- */
2090
2092{
2093 return node.is_type("CompositorNodeBoxMask"_ustr) || node.is_type("CompositorNodeCrop"_ustr) ||
2094 node.is_type("CompositorNodeEllipseMask"_ustr) ||
2095 node.is_type("CompositorNodeGlare"_ustr) ||
2096 node.is_type("CompositorNodeCornerPin"_ustr) ||
2097 node.is_type("CompositorNodeSplit"_ustr) ||
2098 node.is_type("CompositorNodeTranslate"_ustr) ||
2099 node.is_type("CompositorNodeTransform"_ustr) ||
2100 node.is_type("CompositorNodeScale"_ustr) || node.is_type("CompositorNodeViewer"_ustr);
2101}
2102
2104
2105} // namespace blender::nodes::gizmos
#define STR_ELEM(...)
These structs are the foundation for all linked lists in the library system.
#define C
Definition RandGen.cpp:29
volatile int lock
#define ELEM(...)
#define STREQ(a, b)
bool contains(const Key &key) const
Definition BLI_set.hh:310
NodeTreeLog * get_main_tree_log(const bke::bNodeTreeZone *zone) const
Definition eval_log.cc:1116
ValueLog * find_socket_value_log(const bNodeSocket &query_socket)
Definition eval_log.cc:734
static ContextualNodeTreeLogs get_contextual_tree_logs(const SpaceNode &snode)
Definition eval_log.cc:1048
#define rot(x, k)
#define position
float3x3 scale(float3x3 mat, float2 scale)
const int state
bNode * node_get_active(bNodeTree &ntree)
Definition node.cc:5445
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
T clamp(const T &a, const T &min, const T &max)
bool is_any_zero(const T &a)
T length(const VecBase< T, Size > &a)
T round(const T &a)
static void gizmo_node_bbox_update(NodeBBoxWidgetGroup *bbox_group)
static bool show_translate_gizmo(const SpaceNode &snode)
static void gizmo_node_crop_foreach_rna_prop(wmGizmoProperty *gz_prop, const FunctionRef< void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
static bool show_split(const SpaceNode &snode)
bool translate_poll_space_node(const bContext *C, wmGizmoGroupType *)
bool split_poll_space_image(const bContext *C, wmGizmoGroupType *)
bool box_mask_poll_space_node(const bContext *C, wmGizmoGroupType *)
void split_setup(const bContext *, wmGizmoGroup *gzgroup)
static const eval_log::ImageInfoLog * gizmo_input_image_log(const SpaceNode &snode, const bNode &node)
static void gizmo_node_split_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
void split_refresh(const bContext *C, wmGizmoGroup *gzgroup)
void box_mask_refresh(const bContext *C, wmGizmoGroup *gzgroup)
void glare_draw_prepare_space_node(const bContext *C, wmGizmoGroup *gzgroup)
bool crop_poll_space_image(const bContext *C, wmGizmoGroupType *)
void viewer_setup(const bContext *, wmGizmoGroup *gzgroup)
static bool should_sync_gizmo_state_from_log(const SpaceNode &snode, const bNode &node, const wmGizmo &gz)
static void gizmo_node_split_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
void translate_setup(const bContext *, wmGizmoGroup *gzgroup)
static float2 node_scale_gizmo_scale_get(const bNode &node, const float2 &dims)
void glare_draw_prepare_space_image(const bContext *C, wmGizmoGroup *gzgroup)
void corner_pin_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static void apply_socket_callback(bNode &node, const UString socket_identifier, const FunctionRef< void(PointerRNA &, PropertyRNA *, int)> callback, const int index=0)
bool corner_pin_poll_space_node(const bContext *C, wmGizmoGroupType *)
void glare_setup(const bContext *, wmGizmoGroup *gzgroup)
bool box_mask_poll_space_image(const bContext *C, wmGizmoGroupType *)
static bool gizmo_is_interacting(const wmGizmo &gz)
void box_mask_setup(const bContext *, wmGizmoGroup *gzgroup)
static void gizmo_node_translate_prop_offset_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
static bool show_scale_gizmo(const SpaceNode &snode)
static bool show_crop_gizmo(const SpaceNode &snode)
void crop_draw_prepare_space_node(const bContext *C, wmGizmoGroup *gzgroup)
static void gizmo_node_box_mask_foreach_rna_prop(wmGizmoProperty *gz_prop, const FunctionRef< void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
bool glare_poll_space_image(const bContext *C, wmGizmoGroupType *)
static bool show_transform_gizmo(const SpaceNode &snode)
static bool image_gizmo_is_set_visible(const SpaceImage &sima)
static void gizmo_node_box_mask_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
static void gizmo_node_scale_foreach_rna_prop(wmGizmoProperty *gz_prop, const FunctionRef< void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
void transform_setup(const bContext *, wmGizmoGroup *gzgroup)
static bool node_gizmo_is_set_visible(const SpaceNode &snode)
bool viewer_poll(const bContext *C, wmGizmoGroupType *)
void transform_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static float2 gizmo_input_dims_from_log(const SpaceNode &snode, const bNode &node, const float2 &fallback)
bool translate_poll_space_image(const bContext *C, wmGizmoGroupType *)
void bbox_draw_prepare_space_image(const bContext *C, wmGizmoGroup *gzgroup)
void viewer_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static void gizmo_node_crop_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
static SpaceNode * find_active_node_editor(const bContext *C)
static bool show_corner_pin(const SpaceNode &snode)
static void gizmo_node_scale_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
static void gizmo_node_transform_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
bool crop_poll_space_node(const bContext *C, wmGizmoGroupType *)
static void gizmo_node_backdrop_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
bool ellipse_mask_poll_space_node(const bContext *C, wmGizmoGroupType *)
static void gizmo_node_crop_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
void corner_pin_draw_prepare_space_node(const bContext *C, wmGizmoGroup *gzgroup)
bool transform_poll_space_image(const bContext *C, wmGizmoGroupType *)
void corner_pin_setup(const bContext *, wmGizmoGroup *gzgroup)
static bool show_box_mask_gizmo(const SpaceNode &snode)
static void gizmo_node_box_mask_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
void ellipse_mask_setup(const bContext *, wmGizmoGroup *gzgroup)
void translate_refresh(const bContext *C, wmGizmoGroup *gzgroup)
bool ellipse_mask_poll_space_image(const bContext *C, wmGizmoGroupType *)
bool transform_poll_space_node(const bContext *C, wmGizmoGroupType *)
static float2 GIZMO_NODE_DEFAULT_DIMS
static void node_gizmo_calc_matrix_space(const ARegion *region, const float zoom, const float2 offset, float matrix_space[4][4])
void corner_pin_draw_prepare_space_image(const bContext *C, wmGizmoGroup *gzgroup)
void crop_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static float2 translate_gizmo_input_offset(const SpaceNode &snode, const bNode &node)
bool split_poll_space_node(const bContext *C, wmGizmoGroupType *)
static float2 node_gizmo_transformed_offset(const float2 &offset, const float2 &scale, const float rotation)
static void gizmo_node_translate_foreach_rna_prop(wmGizmoProperty *gz_prop, const FunctionRef< void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
bool scale_poll_space_node(const bContext *C, wmGizmoGroupType *)
void scale_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static bool show_ellipse_mask_gizmo(const SpaceNode &snode)
static void gizmo_node_transform_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
void bbox_draw_prepare_space_node(const bContext *C, wmGizmoGroup *gzgroup)
static bool show_glare_gizmo(const SpaceNode &snode)
void glare_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static void gizmo_node_scale_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
static void gizmo_group_enable_undo(wmGizmoGroup *gzgroup)
bool scale_poll_space_image(const bContext *C, wmGizmoGroupType *)
static void gizmo_node_transform_foreach_rna_prop(wmGizmoProperty *gz_prop, const FunctionRef< void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
static void gizmo_node_backdrop_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
void crop_setup(const bContext *, wmGizmoGroup *gzgroup)
bool node_has_gizmo(const bNode &node)
static void node_gizmo_calc_matrix_space_with_image_dims(const ARegion *region, const float zoom, const float2 space_offset, const float2 &image_dims, const float2 &image_offset, float matrix_space[4][4])
bool glare_poll_space_node(const bContext *C, wmGizmoGroupType *)
static float2 node_gizmo_safe_calc_dims(const ImBuf *ibuf, const float2 &fallback_dims)
static void gizmo_node_split_foreach_rna_prop(wmGizmoProperty *gz_prop, const FunctionRef< void(PointerRNA &ptr, PropertyRNA *prop, int index)> callback)
static void gizmo_node_translate_prop_offset_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
static void node_input_from_rect(bNode *node, const rctf *rect, const float2 &dims, const float2 &offset)
static bool node_scale_is_absolute(const bNode &node)
static void node_input_to_rect(const bNode *node, const float2 &dims, const float2 offset, rctf *r_rect)
static void node_scale_gizmo_scale_set(bNode &node, const float2 &dims, const float2 &scale)
bool corner_pin_poll_space_image(const bContext *C, wmGizmoGroupType *)
void scale_setup(const bContext *, wmGizmoGroup *gzgroup)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
void mat4_to_loc_rot_size(float loc[3], float rot[3][3], float size[3], const float wmat[4][4])
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void BLI_rctf_recenter(struct rctf *rect, float x, float y)
Definition rct.cc:616
void WM_gizmo_target_property_def_func(wmGizmo *gz, const char *idname, const wmGizmoPropertyFnParams *params)
const wmGizmoType * WM_gizmotype_find(const StringRef idname, bool quiet)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
void BLI_rctf_resize(struct rctf *rect, float x, float y)
Definition rct.cc:671
@ SNODE_GIZMO_HIDE_ACTIVE_NODE
void mat4_to_eul(float eul[3], const float mat[4][4])
int RNA_int_get(PointerRNA *ptr, const char *name)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
@ ED_GIZMO_CAGE_XFORM_FLAG_SCALE
@ ED_GIZMO_CAGE_XFORM_FLAG_TRANSLATE
@ ED_GIZMO_CAGE_XFORM_FLAG_ROTATE
@ ED_GIZMO_CAGE_XFORM_FLAG_SCALE_UNIFORM
MINLINE void mul_v3_fl(float r[3], float f)
@ ED_GIZMO_CAGE2D_STYLE_CIRCLE
void WM_gizmo_target_property_def_rna(wmGizmo *gz, const char *idname, PointerRNA *ptr, const char *propname, int index)
@ CMP_NODE_GLARE_SUN_BEAMS
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:303
VecBase< float, 2 > float2
@ SI_GIZMO_HIDE_ACTIVE_NODE
void rotate_v2_v2fl(float r[2], const float p[2], float angle)
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
wmWindowManager * CTX_wm_manager(const bContext *C)
@ ED_GIZMO_MOVE_STYLE_CROSS_2D
void mat4_to_compatible_eulO(float eul[3], const float oldrot[3], short order, const float mat[4][4])
void unit_m4(float m[4][4])
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
SpaceNode * CTX_wm_space_node(const bContext *C)
wmGizmo * WM_gizmo_new(const StringRef idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:97
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
MatBase< float, 3, 3 > float3x3
void loc_rot_size_to_mat4(float R[4][4], const float loc[3], const float rot[3][3], const float size[3])
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
@ WM_GIZMO_NEEDS_UNDO
@ WM_GIZMO_DRAW_MODAL
SpaceImage * CTX_wm_space_image(const bContext *C)
Main * CTX_data_main(const bContext *C)
void unit_m3(float m[3][3])
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
bool BLI_rctf_isect(const struct rctf *src1, const struct rctf *src2, struct rctf *dest)
Image * BKE_image_ensure_viewer(Main *bmain, int type, const char *name)
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition BLI_rect.hh:198
@ CMP_NODE_SCALE_ABSOLUTE
@ ED_GIZMO_CAGE_DRAW_FLAG_XFORM_CENTER_HANDLE
@ ED_GIZMO_CAGE_DRAW_FLAG_CORNER_HANDLES
@ ED_GIZMO_CAGE_DRAW_FLAG_NOP
void copy_m4_m4(float m1[4][4], const float m2[4][4])
VecBase< float, 3 > float3
void WM_gizmo_set_matrix_location(wmGizmo *gz, const float origin[3])
Definition wm_gizmo.cc:283
wmGizmo * WM_gizmo_new_ptr(const wmGizmoType *gzt, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:84
bScreen * WM_window_get_active_screen(const wmWindow *win)
ARegion * CTX_wm_region(const bContext *C)
BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
Definition BLI_rect.hh:194
Image * ED_space_image(const SpaceImage *sima)
Definition image_edit.cc:43
void axis_angle_to_mat3_single(float R[3][3], char axis, float angle)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< SpaceLink > spacedata
eSpaceImage_Gizmo_Flag gizmo_flag
eSpaceImage_Mode mode
struct bNodeTree * edittree
eSpaceNode_Gizmo_Flag gizmo_flag
struct bNodeTree * nodetree
ListBaseT< bNodeTreePath > treepath
eSpaceNode_Flag flag
bNodeInstanceKey parent_key
eNodeTree_Type type
bNodeInstanceKey active_viewer_key
ListBaseT< struct ScrArea > areabase
struct blender::nodes::gizmos::NodeBBoxWidgetGroup::@301275026076240310066360212070034344023116207314 state
struct blender::nodes::gizmos::NodeBBoxWidgetGroup::@127224212361201164331120312126070334307171063225 update_data
struct blender::nodes::gizmos::NodeCornerPinWidgetGroup::@002257352000363205342360322301326023266120133321 state
struct blender::nodes::gizmos::NodeGlareWidgetGroup::@024175237200245157005202325303262336257207162254 state
struct blender::nodes::gizmos::NodeTransformWidgetGroup::@366363201126377352042167074221261277004160104176 state
ListBaseT< wmGizmo > gizmos
void(*) customdata_free(void *)
wmGizmoPropertyFnSet value_set_fn
wmGizmoPropertyFnRangeGet range_get_fn
wmGizmoPropertyFnGet value_get_fn
wmGizmoPropertyFnForeachRNAProp foreach_rna_prop_fn
const wmGizmoPropertyType * type
struct blender::wmGizmoProperty::@216011055122204070127377170176223170047224321165 custom_func
float matrix_space[4][4]
wmGizmoGroup * parent_gzgroup