Blender V5.3
wm_gesture_ops.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
14#include "MEM_guardedalloc.h"
15
16#include <fmt/format.h>
17
18#include <algorithm>
19
20#include "DNA_space_types.h"
22
24#include "BLI_math_vector.hh"
25#include "BLI_math_vector_c.hh"
27#include "BLI_rect.hh"
28
29#include "BKE_context.hh"
30
31#include "WM_api.hh"
32#include "WM_types.hh"
33
34#include "wm.hh"
35#include "wm_event_types.hh"
36
37#include "ED_screen.hh"
38#include "ED_select_utils.hh"
39
40#include "UI_view2d.hh"
41
42#include "RNA_access.hh"
43
44#define GESTURE_EDGE_PAN_INSIDE_PAD 2
45#define GESTURE_EDGE_PAN_OUTSIDE_PAD 0
46#define GESTURE_EDGE_PAN_SPEED_RAMP 1.0f
47#define GESTURE_EDGE_PAN_MAX_SPEED 26.0f
48#define GESTURE_EDGE_PAN_DELAY 0.5f
49#define GESTURE_EDGE_PAN_ZOOM_INFLUENCE 0.0f
50
51namespace blender {
52
53/* -------------------------------------------------------------------- */
63
65{
66 wmWindow *win = CTX_wm_window(C);
67 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
68
69 WM_gesture_end(win, gesture); /* Frees gesture itself, and unregisters from window. */
70 op->customdata = nullptr;
71
73
74 if (RNA_struct_find_property(op->ptr, "cursor")) {
76 }
77}
78
79static void gesture_modal_state_to_operator(wmOperator *op, int modal_state)
80{
81 PropertyRNA *prop;
82
83 switch (modal_state) {
86 if ((prop = RNA_struct_find_property(op->ptr, "deselect"))) {
87 RNA_property_boolean_set(op->ptr, prop, (modal_state == GESTURE_MODAL_DESELECT));
88 }
89 if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
91 op->ptr, prop, (modal_state == GESTURE_MODAL_DESELECT) ? SEL_OP_SUB : SEL_OP_ADD);
92 }
93 break;
96 if ((prop = RNA_struct_find_property(op->ptr, "zoom_out"))) {
97 RNA_property_boolean_set(op->ptr, prop, (modal_state == GESTURE_MODAL_OUT));
98 }
99 break;
100 }
101}
102
104{
105 if (gesture->edge_pan_data == nullptr) {
106 return;
107 }
108 const View2D *v2d = gesture->edge_pan_data->v2d;
109 gesture->init_mval_view = float2(ui::view2d_region_to_view_x(v2d, mval.x),
110 ui::view2d_region_to_view_y(v2d, mval.y));
111}
112
114{
115 PropertyRNA *prop;
116
117 if ((prop = RNA_struct_find_property(op->ptr, "deselect"))) {
118 if (RNA_property_is_set(op->ptr, prop)) {
119 return RNA_property_boolean_get(op->ptr, prop) ? GESTURE_MODAL_DESELECT :
121 }
122 }
123 if ((prop = RNA_struct_find_property(op->ptr, "mode"))) {
124 if (RNA_property_is_set(op->ptr, prop)) {
125 return RNA_property_enum_get(op->ptr, prop) == SEL_OP_SUB ? GESTURE_MODAL_DESELECT :
127 }
128 }
129 if ((prop = RNA_struct_find_property(op->ptr, "zoom_out"))) {
130 if (RNA_property_is_set(op->ptr, prop)) {
132 }
133 }
134 return GESTURE_MODAL_NOP;
135}
136
138
139/* -------------------------------------------------------------------- */
149
151{
152 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
153 const rcti *rect = static_cast<const rcti *>(gesture->customdata);
154
155 if (rect->xmin == rect->xmax || rect->ymin == rect->ymax) {
156 return false;
157 }
158
159 /* Operator arguments and storage. */
160 RNA_int_set(op->ptr, "xmin", min_ii(rect->xmin, rect->xmax));
161 RNA_int_set(op->ptr, "ymin", min_ii(rect->ymin, rect->ymax));
162 RNA_int_set(op->ptr, "xmax", max_ii(rect->xmin, rect->xmax));
163 RNA_int_set(op->ptr, "ymax", max_ii(rect->ymin, rect->ymax));
164
165 return true;
166}
167
169{
170 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
171
172 if (!gesture_box_apply_rect(op)) {
173 return false;
174 }
175
176 if (gesture->wait_for_input) {
178 }
179
180 const wmOperatorStatus retval = op->type->exec(C, op);
181 OPERATOR_RETVAL_CHECK(retval);
182
183 return (retval & OPERATOR_FINISHED) ? true : false;
184}
185
187{
188 wmWindow *win = CTX_wm_window(C);
189 const ARegion *region = CTX_wm_region(C);
190 const bool wait_for_input = !WM_event_is_mouse_drag_or_press(event) &&
191 RNA_boolean_get(op->ptr, "wait_for_input");
192
193 if (wait_for_input) {
194 op->customdata = WM_gesture_new(win, region, event, WM_GESTURE_CROSS_RECT);
195 }
196 else {
197 op->customdata = WM_gesture_new(win, region, event, WM_GESTURE_RECT);
198 }
199
200 {
201 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
202 gesture->wait_for_input = wait_for_input;
204 gesture->edge_pan_data = MEM_new<ui::View2DEdgePanData>(__func__);
206 gesture->edge_pan_data,
213
214 const rcti *rect = static_cast<const rcti *>(gesture->customdata);
216 }
217 }
218
219 /* Add modal handler. */
221
223
225}
226
228{
229 wmWindow *win = CTX_wm_window(C);
230 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
231 rcti *rect = static_cast<rcti *>(gesture->customdata);
232
233 if (event->type == EVT_MODAL_MAP) {
234 switch (event->val) {
235 case GESTURE_MODAL_MOVE: {
236 gesture->move = !gesture->move;
237 break;
238 }
239 case GESTURE_MODAL_BEGIN: {
240 if (gesture->type == WM_GESTURE_CROSS_RECT && gesture->is_active == false) {
241 gesture->is_active = true;
243 }
244 break;
245 }
248 case GESTURE_MODAL_IN:
249 case GESTURE_MODAL_OUT: {
250 if (gesture->wait_for_input) {
251 gesture->modal_state = event->val;
252 }
253 if (gesture_box_apply(C, op)) {
254 gesture_modal_end(C, op);
255 return OPERATOR_FINISHED;
256 }
257 gesture_modal_end(C, op);
258 return OPERATOR_CANCELLED;
259 }
261 gesture_modal_end(C, op);
262 return OPERATOR_CANCELLED;
263 }
264 }
265 }
266 else {
267 switch (event->type) {
268 case MOUSEMOVE: {
269 if (gesture->edge_pan_data) {
270 const View2D *v2d = gesture->edge_pan_data->v2d;
271 ui::view2d_edge_pan_apply_event(C, gesture->edge_pan_data, event);
272 if (!gesture->move) {
273 rect->xmin = ui::view2d_view_to_region_x(v2d, gesture->init_mval_view.x);
274 rect->ymin = ui::view2d_view_to_region_y(v2d, gesture->init_mval_view.y);
275 }
276 }
277
278 if (gesture->type == WM_GESTURE_CROSS_RECT && gesture->is_active == false) {
279 rect->xmin = rect->xmax = event->xy[0] - gesture->winrct.xmin;
280 rect->ymin = rect->ymax = event->xy[1] - gesture->winrct.ymin;
282 }
283 else if (gesture->move) {
285 (event->xy[0] - gesture->winrct.xmin) - rect->xmax,
286 (event->xy[1] - gesture->winrct.ymin) - rect->ymax);
288 }
289 else {
290 rect->xmax = event->xy[0] - gesture->winrct.xmin;
291 rect->ymax = event->xy[1] - gesture->winrct.ymin;
292 }
294
296
297 break;
298 }
299#ifdef WITH_INPUT_NDOF
300 case NDOF_MOTION: {
302 }
303#endif
304
305 default:
306#if 0 /* This allows view navigation, keep disabled as it's too unpredictable. */
308#endif
309 break;
310 }
311 }
312
313 gesture->is_active_prev = gesture->is_active;
315}
316
321
323
324/* -------------------------------------------------------------------- */
332
333static void gesture_circle_apply(bContext *C, wmOperator *op);
334
336{
337 wmWindow *win = CTX_wm_window(C);
338 const bool wait_for_input = !WM_event_is_mouse_drag_or_press(event) &&
339 RNA_boolean_get(op->ptr, "wait_for_input");
340
342 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
343 rcti *rect = static_cast<rcti *>(gesture->customdata);
344
345 /* Default or previously stored value. */
346 rect->xmax = RNA_int_get(op->ptr, "radius");
347
348 gesture->wait_for_input = wait_for_input;
349
350 /* Starting with the mode starts immediately,
351 * like having 'wait_for_input' disabled (some tools use this). */
352 if (gesture->wait_for_input == false) {
353 gesture->is_active = true;
355 gesture->is_active_prev = true;
356 }
357
358 /* Add modal handler. */
360
362
364}
365
367{
368 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
369 const rcti *rect = static_cast<const rcti *>(gesture->customdata);
370
371 if (gesture->wait_for_input && (gesture->modal_state == GESTURE_MODAL_NOP)) {
372 return;
373 }
374
375 /* Operator arguments and storage. */
376 RNA_int_set(op->ptr, "x", rect->xmin);
377 RNA_int_set(op->ptr, "y", rect->ymin);
378 RNA_int_set(op->ptr, "radius", rect->xmax);
379
380 /* When 'wait_for_input' is false,
381 * use properties to get the selection state (typically tool settings).
382 * This is done so executing as a mode can select & de-select, see: #58594. */
383 if (gesture->wait_for_input) {
385 }
386
387 if (op->type->exec) {
388 const wmOperatorStatus retval = op->type->exec(C, op);
389 OPERATOR_RETVAL_CHECK(retval);
390 }
391}
392
394{
395 wmWindow *win = CTX_wm_window(C);
396 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
397 rcti *rect = static_cast<rcti *>(gesture->customdata);
398
399 if (event->type == MOUSEMOVE) {
400
401 rect->xmin = event->xy[0] - gesture->winrct.xmin;
402 rect->ymin = event->xy[1] - gesture->winrct.ymin;
403
405
406 if (gesture->is_active) {
408 }
409 }
410 else if (event->type == EVT_MODAL_MAP) {
411 bool is_circle_size = false;
412 bool is_finished = false;
413 float fac;
414
415 switch (event->val) {
417 fac = 0.3f * (event->xy[1] - event->prev_xy[1]);
418 if (fac > 0) {
419 rect->xmax += ceil(fac);
420 }
421 else {
422 rect->xmax += floor(fac);
423 }
424 rect->xmax = std::max(rect->xmax, 1);
425 is_circle_size = true;
426 break;
428 rect->xmax += 2 + rect->xmax / 10;
429 is_circle_size = true;
430 break;
432 rect->xmax -= 2 + rect->xmax / 10;
433 rect->xmax = std::max(rect->xmax, 1);
434 is_circle_size = true;
435 break;
438 case GESTURE_MODAL_NOP: {
439 if (gesture->wait_for_input) {
440 gesture->modal_state = event->val;
441 }
442 if (event->val == GESTURE_MODAL_NOP) {
443 /* Single action, click-drag & release to exit. */
444 if (gesture->wait_for_input == false) {
445 is_finished = true;
446 }
447 }
448 else {
449 /* Apply first click. */
450 gesture->is_active = true;
453 }
454 break;
455 }
458 is_finished = true;
459 }
460
461 if (is_finished) {
462 gesture_modal_end(C, op);
463 return OPERATOR_FINISHED; /* Use finish or we don't get an undo. */
464 }
465
466 if (is_circle_size) {
468
469 /* So next use remembers last seen size, even if we didn't apply it. */
470 RNA_int_set(op->ptr, "radius", rect->xmax);
471 }
472 }
473#ifdef WITH_INPUT_NDOF
474 else if (event->type == NDOF_MOTION) {
476 }
477#endif
478
479#if 0
480 /* Allow view navigation??? */
481 /* NOTE: this gives issues:
482 * 1) other modal ops run on top (box select),
483 * 2) middle-mouse is used now 3) tablet/trackpad? */
484 else {
486 }
487#endif
488
489 gesture->is_active_prev = gesture->is_active;
491}
492
497
498#if 0
499/* Template to copy from. */
500void WM_OT_circle_gesture(wmOperatorType *ot)
501{
502 ot->name = "Circle Gesture";
503 ot->idname = "WM_OT_circle_gesture";
504 ot->description = "Enter rotate mode with a circular gesture";
505
506 ot->invoke = WM_gesture_circle_invoke;
507 ot->modal = WM_gesture_circle_modal;
508 ot->poll = WM_operator_winactive;
509
510 /* Properties. */
512}
513#endif
514
516
517/* -------------------------------------------------------------------- */
525
527{
528 wmWindow *win = CTX_wm_window(C);
529 PropertyRNA *prop;
530
532 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
533 gesture->use_smooth = RNA_boolean_get(op->ptr, "use_smooth_stroke");
534
535 /* Add modal handler. */
537
539
540 if ((prop = RNA_struct_find_property(op->ptr, "cursor"))) {
542 }
543
545}
546
548{
549 wmWindow *win = CTX_wm_window(C);
550 PropertyRNA *prop;
551
553 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
554 if ((prop = RNA_struct_find_property(op->ptr, "use_smooth_stroke"))) {
555 gesture->use_smooth = RNA_property_boolean_get(op->ptr, prop);
556 }
557
558 /* Add modal handler. */
560
562
563 if ((prop = RNA_struct_find_property(op->ptr, "cursor"))) {
565 }
566
568}
569
571{
573 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
574 PointerRNA itemptr;
575 float loc[2];
576 int i;
577 const float *lasso = static_cast<const float *>(gesture->customdata);
578
579 /* Operator storage as path. */
580
581 RNA_collection_clear(op->ptr, "path");
582 for (i = 0; i < gesture->points; i++, lasso += 2) {
583 loc[0] = lasso[0];
584 loc[1] = lasso[1];
585 RNA_collection_add(op->ptr, "path", &itemptr);
586 RNA_float_set_array(&itemptr, "loc", loc);
587 }
588
589 gesture_modal_end(C, op);
590
591 if (op->type->exec) {
592 retval = op->type->exec(C, op);
593 OPERATOR_RETVAL_CHECK(retval);
594 }
595
596 return retval;
597}
598
600{
601 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
602 const float factor = gesture->use_smooth ? RNA_float_get(op->ptr, "smooth_stroke_factor") : 0.0f;
603 const int radius = gesture->use_smooth ? RNA_int_get(op->ptr, "smooth_stroke_radius") : 0;
604
605 if (event->type == EVT_MODAL_MAP) {
606 switch (event->val) {
607 case GESTURE_MODAL_MOVE: {
608 gesture->move = !gesture->move;
609 break;
610 }
611 }
612 }
613 else {
614 switch (event->type) {
615 case MOUSEMOVE:
616 case INBETWEEN_MOUSEMOVE: {
618 gesture->mval = int2((event->xy[0] - gesture->winrct.xmin),
619 (event->xy[1] - gesture->winrct.ymin));
620
621 if (gesture->points == gesture->points_alloc) {
622 gesture->points_alloc *= 2;
624 gesture->customdata, sizeof(float[2]) * gesture->points_alloc);
625 }
626
627 {
628 float (*lasso)[2] = static_cast<float (*)[2]>(gesture->customdata);
629 const float2 current_mouse_position = float2(gesture->mval);
630 const float2 last_position(lasso[gesture->points - 1][0], lasso[gesture->points - 1][1]);
631
632 const float2 delta = current_mouse_position - last_position;
633 const float dist_squared = math::length_squared(delta);
634
635 /* Move the lasso. */
636 if (gesture->move) {
637 for (int i = 0; i < gesture->points; i++) {
638 lasso[i][0] += delta.x;
639 lasso[i][1] += delta.y;
640 }
641 }
642 else if (gesture->use_smooth) {
643 if (dist_squared > square_f(radius)) {
644 float2 result = math::interpolate(current_mouse_position, last_position, factor);
645
646 lasso[gesture->points][0] = result.x;
647 lasso[gesture->points][1] = result.y;
648 gesture->points++;
649 }
650 }
651 else if (dist_squared > pow2f(2.0f * UI_SCALE_FAC)) {
652 /* Make a simple distance check to get a smoother lasso even if smoothing isn't enabled
653 * add only when at least 2 pixels between this and previous location. */
654 lasso[gesture->points][0] = gesture->mval.x;
655 lasso[gesture->points][1] = gesture->mval.y;
656 gesture->points++;
657 }
658 }
659 break;
660 }
661 case LEFTMOUSE:
662 case MIDDLEMOUSE:
663 case RIGHTMOUSE: {
664 if (event->val == KM_RELEASE) { /* Key release. */
665 return gesture_lasso_apply(C, op);
666 }
667 break;
668 }
669 case EVT_ESCKEY: {
670 gesture_modal_end(C, op);
671 return OPERATOR_CANCELLED;
672 }
673 default: {
674 break;
675 }
676 }
677 }
678
679 gesture->is_active_prev = gesture->is_active;
681}
682
684{
685 return WM_gesture_lasso_modal(C, op, event);
686}
687
692
697
699{
700 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "path");
701 BLI_assert(prop != nullptr);
702 if (!prop) {
703 return {};
704 }
705 const int len = RNA_property_collection_length(op->ptr, prop);
706 if (len == 0) {
707 return {};
708 }
709
710 int i = 0;
711 Array<int2> mcoords(len);
712
713 RNA_PROP_BEGIN (op->ptr, itemptr, prop) {
714 float loc[2];
715 RNA_float_get_array(&itemptr, "loc", loc);
716 mcoords[i] = int2(loc[0], loc[1]);
717 i++;
718 }
720
721 return mcoords;
722}
723
724#if 0
725/* Template to copy from. */
726
727static wmOperatorStatus gesture_lasso_exec(bContext *C, wmOperator *op)
728{
729 RNA_BEGIN (op->ptr, itemptr, "path") {
730 float loc[2];
731
732 RNA_float_get_array(&itemptr, "loc", loc);
733 printf("Location: %f %f\n", loc[0], loc[1]);
734 }
735 RNA_END;
736
737 return OPERATOR_FINISHED;
738}
739
740void WM_OT_lasso_gesture(wmOperatorType *ot)
741{
742 PropertyRNA *prop;
743
744 ot->name = "Lasso Gesture";
745 ot->idname = "WM_OT_lasso_gesture";
746 ot->description = "Draw a shape defined by the cursor";
747
748 ot->invoke = WM_gesture_lasso_invoke;
749 ot->modal = WM_gesture_lasso_modal;
750 ot->exec = gesture_lasso_exec;
751
752 ot->poll = WM_operator_winactive;
753
754 ot->flag = OPTYPE_DEPENDS_ON_CURSOR;
755
756 prop = RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
757 RNA_def_property_struct_runtime(ot->srna, prop, RNA_OperatorMousePath);
758}
759#endif
760
762
763/* -------------------------------------------------------------------- */
771{
772 wmWindow *win = CTX_wm_window(C);
773 PropertyRNA *prop;
774
776
777 /* add modal handler */
779
781
782 if ((prop = RNA_struct_find_property(op->ptr, "cursor"))) {
784 }
785
787}
788
789/* Calculates the number of valid points in a polyline gesture where
790 * a duplicated end point is invalid for submission */
791static int gesture_polyline_valid_points(const wmGesture &wmGesture, const bool is_click_submitted)
792{
794
795 const int num_points = wmGesture.points;
796 if (is_click_submitted) {
797 return num_points;
798 }
799
800 short (*points)[2] = static_cast<short int (*)[2]>(wmGesture.customdata);
801
802 const short prev_x = points[num_points - 1][0];
803 const short prev_y = points[num_points - 1][1];
804
805 return (wmGesture.mval.x == prev_x && wmGesture.mval.y == prev_y) ? num_points : num_points + 1;
806}
807
816static bool gesture_polyline_can_apply(const wmGesture &wmGesture, const bool is_click_submitted)
817{
818 if (wmGesture.points < 2) {
819 return false;
820 }
821
822 const int valid_points = gesture_polyline_valid_points(wmGesture, is_click_submitted);
823 if (valid_points <= 2) {
824 return false;
825 }
826
827 return true;
828}
829
831 wmOperator *op,
832 const bool is_click_submitted)
833{
834 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
835 BLI_assert(gesture_polyline_can_apply(*gesture, is_click_submitted));
836
837 const int valid_points = gesture_polyline_valid_points(*gesture, is_click_submitted);
838 const short *border = static_cast<const short int *>(gesture->customdata);
839
840 PointerRNA itemptr;
841 float loc[2];
842 RNA_collection_clear(op->ptr, "path");
843 for (int i = 0; i < gesture->points; i++, border += 2) {
844 loc[0] = border[0];
845 loc[1] = border[1];
846 RNA_collection_add(op->ptr, "path", &itemptr);
847 RNA_float_set_array(&itemptr, "loc", loc);
848 }
849 if (valid_points > gesture->points) {
850 loc[0] = gesture->mval.x;
851 loc[1] = gesture->mval.y;
852 RNA_collection_add(op->ptr, "path", &itemptr);
853 RNA_float_set_array(&itemptr, "loc", loc);
854 }
855
856 gesture_modal_end(C, op);
857
859 if (op->type->exec) {
860 retval = op->type->exec(C, op);
861 OPERATOR_RETVAL_CHECK(retval);
862 }
863
864 return retval;
865}
866
868{
869 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
870
871 if (event->type == EVT_MODAL_MAP) {
872 switch (event->val) {
874 gesture->move = !gesture->move;
875 break;
878 short (*border)[2] = static_cast<short int (*)[2]>(gesture->customdata);
879 const short prev_x = border[gesture->points - 1][0];
880 const short prev_y = border[gesture->points - 1][1];
881
882 if (gesture->mval.x == prev_x && gesture->mval.y == prev_y) {
883 break;
884 }
885
886 const float2 cur(gesture->mval);
887 const float2 orig(border[0][0], border[0][1]);
888
889 const float dist = len_v2v2(cur, orig);
890
893 {
894 return gesture_polyline_apply(C, op, true);
895 }
896
897 gesture->points++;
898 border[gesture->points - 1][0] = gesture->mval.x;
899 border[gesture->points - 1][1] = gesture->mval.y;
900 break;
901 }
903 if (gesture_polyline_can_apply(*gesture, false)) {
904 return gesture_polyline_apply(C, op, false);
905 }
906 break;
908 gesture_modal_end(C, op);
909 return OPERATOR_CANCELLED;
910 }
911 }
912 else {
913 switch (event->type) {
914 case MOUSEMOVE:
915 case INBETWEEN_MOUSEMOVE: {
917 gesture->mval = int2((event->xy[0] - gesture->winrct.xmin),
918 (event->xy[1] - gesture->winrct.ymin));
919 if (gesture->points == gesture->points_alloc) {
920 gesture->points_alloc *= 2;
922 gesture->customdata, sizeof(short[2]) * gesture->points_alloc);
923 }
924 short (*border)[2] = static_cast<short int (*)[2]>(gesture->customdata);
925
926 /* move the lasso */
927 if (gesture->move) {
928 const int dx = gesture->mval.x - border[gesture->points - 1][0];
929 const int dy = gesture->mval.y - border[gesture->points - 1][1];
930
931 for (int i = 0; i < gesture->points; i++) {
932 border[i][0] += dx;
933 border[i][1] += dy;
934 }
935 }
936 break;
937 }
938 default: {
939 break;
940 }
941 }
942 }
943
944 gesture->is_active_prev = gesture->is_active;
946}
947
952
953/* template to copy from */
954#if 0
955static wmOperatorStatus gesture_polyline_exec(bContext *C, wmOperator *op)
956{
957 RNA_BEGIN (op->ptr, itemptr, "path") {
958 float loc[2];
959
960 RNA_float_get_array(&itemptr, "loc", loc);
961 printf("Location: %f %f\n", loc[0], loc[1]);
962 }
963 RNA_END;
964
965 return OPERATOR_FINISHED;
966}
967
968void WM_OT_polyline_gesture(wmOperatorType *ot)
969{
970 PropertyRNA *prop;
971
972 ot->name = "Polyline Gesture";
973 ot->idname = "WM_OT_polyline_gesture";
974 ot->description = "Outline a selection area with each mouse click";
975
976 ot->invoke = WM_gesture_polyline_invoke;
977 ot->modal = WM_gesture_polyline_modal;
978 ot->exec = gesture_polyline_exec;
979
980 ot->poll = WM_operator_winactive;
981
983}
984#endif
985
987
988/* -------------------------------------------------------------------- */
1000
1005
1006static SnapAngle get_snap_angle(const ScrArea &area, const ToolSettings &tool_settings)
1007{
1008 SnapAngle snap_angle;
1009 if (area.spacetype == SPACE_VIEW3D) {
1010 snap_angle.increment = tool_settings.snap_angle_increment_3d;
1011 snap_angle.precise_increment = tool_settings.snap_angle_increment_3d_precision;
1012 }
1013 else {
1014 snap_angle.increment = tool_settings.snap_angle_increment_2d;
1015 snap_angle.precise_increment = tool_settings.snap_angle_increment_2d_precision;
1016 }
1017
1018 return snap_angle;
1019}
1020
1022{
1023 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
1024 const rcti *rect = static_cast<const rcti *>(gesture->customdata);
1025
1026 if (rect->xmin == rect->xmax && rect->ymin == rect->ymax) {
1027 return false;
1028 }
1029
1030 /* Operator arguments and storage. */
1031 RNA_int_set(op->ptr, "xstart", rect->xmin);
1032 RNA_int_set(op->ptr, "ystart", rect->ymin);
1033 RNA_int_set(op->ptr, "xend", rect->xmax);
1034 RNA_int_set(op->ptr, "yend", rect->ymax);
1035 RNA_boolean_set(op->ptr, "flip", gesture->use_flip);
1036
1037 if (op->type->exec) {
1038 const wmOperatorStatus retval = op->type->exec(C, op);
1039 OPERATOR_RETVAL_CHECK(retval);
1040 }
1041
1042 return true;
1043}
1044
1046{
1047 wmWindow *win = CTX_wm_window(C);
1048 PropertyRNA *prop;
1049
1051
1053 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
1054 gesture->is_active = true;
1055 }
1056
1057 /* Add modal handler. */
1059
1061
1062 if ((prop = RNA_struct_find_property(op->ptr, "cursor"))) {
1064 }
1065
1067}
1069 wmOperator *op,
1070 const wmEvent *event)
1071{
1073 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
1074 gesture->draw_active_side = true;
1075 gesture->use_flip = false;
1077}
1078
1079static void wm_gesture_straightline_do_angle_snap(rcti *rect, float snap_angle)
1080{
1081 const float line_start[2] = {float(rect->xmin), float(rect->ymin)};
1082 const float line_end[2] = {float(rect->xmax), float(rect->ymax)};
1083 const float x_axis[2] = {1.0f, 0.0f};
1084
1085 float line_direction[2];
1086 sub_v2_v2v2(line_direction, line_end, line_start);
1087 const float line_length = normalize_v2(line_direction);
1088
1089 const float current_angle = angle_signed_v2v2(x_axis, line_direction);
1090 const float adjusted_angle = current_angle + (snap_angle / 2.0f);
1091 const float angle_snapped = -floorf(adjusted_angle / snap_angle) * snap_angle;
1092
1093 float line_snapped_end[2];
1094 rotate_v2_v2fl(line_snapped_end, x_axis, angle_snapped);
1095 mul_v2_fl(line_snapped_end, line_length);
1096 add_v2_v2(line_snapped_end, line_start);
1097
1098 rect->xmax = int(line_snapped_end[0]);
1099 rect->ymax = int(line_snapped_end[1]);
1100
1101 /* Check whether `angle_snapped` is a multiple of 45 degrees, if so ensure X and Y directions
1102 * are the same length (there could be an off-by-one due to rounding error). */
1103 const float fract_45 = fractf(angle_snapped / DEG2RADF(45.0f));
1104 const float fract_90 = fractf(angle_snapped / DEG2RADF(90.0f));
1105 /* Check if it's a multiple of 45 but not 90 degrees. */
1106 if ((compare_ff(fract_45, 0.0f, 1e-6) || compare_ff(fabsf(fract_45), 1.0f, 1e-6)) &&
1107 !(compare_ff(fract_90, 0.0f, 1e-6) || compare_ff(fabsf(fract_90), 1.0f, 1e-6)))
1108 {
1109 int xlen = abs(rect->xmax - rect->xmin);
1110 int ylen = rect->ymax - rect->ymin;
1111 if (abs(ylen) != xlen) {
1112 ylen = xlen * (ylen >= 0 ? 1 : -1);
1113 rect->ymax = rect->ymin + ylen;
1114 }
1115 }
1116}
1117
1119{
1120 const Scene *scene = CTX_data_scene(C);
1121 const ScrArea *area = CTX_wm_area(C);
1122 const SnapAngle snap_angle = get_snap_angle(*area, *scene->toolsettings);
1123
1124 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
1125 wmWindow *win = CTX_wm_window(C);
1126 rcti *rect = static_cast<rcti *>(gesture->customdata);
1127
1128 if (event->type == EVT_MODAL_MAP) {
1129 switch (event->val) {
1130 case GESTURE_MODAL_MOVE: {
1131 gesture->move = !gesture->move;
1132 break;
1133 }
1134 case GESTURE_MODAL_BEGIN: {
1135 if (gesture->is_active == false) {
1136 gesture->is_active = true;
1138 }
1139 break;
1140 }
1141 case GESTURE_MODAL_SNAP: {
1142 /* Toggle snapping on/off. */
1143 gesture->use_snap = !gesture->use_snap;
1144 break;
1145 }
1146 case GESTURE_MODAL_FLIP: {
1147 /* Toggle flipping on/off. */
1148 gesture->use_flip = !gesture->use_flip;
1151 break;
1152 }
1153 case GESTURE_MODAL_SELECT: {
1154 if (gesture_straightline_apply(C, op)) {
1155 gesture_modal_end(C, op);
1156 return OPERATOR_FINISHED;
1157 }
1158 gesture_modal_end(C, op);
1159 return OPERATOR_CANCELLED;
1160 }
1161 case GESTURE_MODAL_CANCEL: {
1162 gesture_modal_end(C, op);
1163 return OPERATOR_CANCELLED;
1164 }
1165 }
1166 }
1167 else {
1168 switch (event->type) {
1169 case MOUSEMOVE: {
1170 if (gesture->is_active == false) {
1171 rect->xmin = rect->xmax = event->xy[0] - gesture->winrct.xmin;
1172 rect->ymin = rect->ymax = event->xy[1] - gesture->winrct.ymin;
1173 }
1174 else if (gesture->move) {
1175 BLI_rcti_translate(rect,
1176 (event->xy[0] - gesture->winrct.xmin) - rect->xmax,
1177 (event->xy[1] - gesture->winrct.ymin) - rect->ymax);
1179 }
1180 else {
1181 rect->xmax = event->xy[0] - gesture->winrct.xmin;
1182 rect->ymax = event->xy[1] - gesture->winrct.ymin;
1184 }
1185
1186 if (gesture->use_snap) {
1189 }
1190
1192
1193 break;
1194 }
1195 default: {
1196 break;
1197 }
1198 }
1199 }
1200
1201 gesture->is_active_prev = gesture->is_active;
1203}
1204
1206 wmOperator *op,
1207 const wmEvent *event)
1208{
1209 const Scene *scene = CTX_data_scene(C);
1210 const ScrArea *area = CTX_wm_area(C);
1211 const SnapAngle snap_angle = get_snap_angle(*area, *scene->toolsettings);
1212
1213 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
1214 wmWindow *win = CTX_wm_window(C);
1215 rcti *rect = static_cast<rcti *>(gesture->customdata);
1216
1217 if (event->type == EVT_MODAL_MAP) {
1218 switch (event->val) {
1219 case GESTURE_MODAL_MOVE: {
1220 gesture->move = !gesture->move;
1221 break;
1222 }
1223 case GESTURE_MODAL_BEGIN: {
1224 if (gesture->is_active == false) {
1225 gesture->is_active = true;
1227 }
1228 break;
1229 }
1230 case GESTURE_MODAL_SNAP: {
1231 /* Toggle snapping on/off. */
1232 gesture->use_snap = !gesture->use_snap;
1233 break;
1234 }
1235 case GESTURE_MODAL_FLIP: {
1236 /* Toggle flip on/off. */
1237 gesture->use_flip = !gesture->use_flip;
1239 break;
1240 }
1243 case GESTURE_MODAL_IN:
1244 case GESTURE_MODAL_OUT: {
1245 if (gesture->wait_for_input) {
1246 gesture->modal_state = event->val;
1247 }
1248 if (gesture_straightline_apply(C, op)) {
1249 gesture_modal_end(C, op);
1250 return OPERATOR_FINISHED;
1251 }
1252 gesture_modal_end(C, op);
1253 return OPERATOR_CANCELLED;
1254 }
1255 case GESTURE_MODAL_CANCEL: {
1256 gesture_modal_end(C, op);
1257 return OPERATOR_CANCELLED;
1258 }
1259 }
1260 }
1261 else {
1262 switch (event->type) {
1263 case MOUSEMOVE: {
1264 if (gesture->is_active == false) {
1265 rect->xmin = rect->xmax = event->xy[0] - gesture->winrct.xmin;
1266 rect->ymin = rect->ymax = event->xy[1] - gesture->winrct.ymin;
1267 }
1268 else if (gesture->move) {
1269 BLI_rcti_translate(rect,
1270 (event->xy[0] - gesture->winrct.xmin) - rect->xmax,
1271 (event->xy[1] - gesture->winrct.ymin) - rect->ymax);
1272 }
1273 else {
1274 rect->xmax = event->xy[0] - gesture->winrct.xmin;
1275 rect->ymax = event->xy[1] - gesture->winrct.ymin;
1276 }
1277
1278 if (gesture->use_snap) {
1280 }
1281
1283
1284 break;
1285 }
1286 default: {
1287 break;
1288 }
1289 }
1290 }
1291
1292 gesture->is_active_prev = gesture->is_active;
1294}
1295
1300
1301#if 0
1302/* Template to copy from. */
1303void WM_OT_straightline_gesture(wmOperatorType *ot)
1304{
1305 PropertyRNA *prop;
1306
1307 ot->name = "Straight Line Gesture";
1308 ot->idname = "WM_OT_straightline_gesture";
1309 ot->description = "Draw a straight line defined by the cursor";
1310
1311 ot->invoke = WM_gesture_straightline_invoke;
1312 ot->modal = WM_gesture_straightline_modal;
1313 ot->exec = gesture_straightline_exec;
1314
1315 ot->poll = WM_operator_winactive;
1316
1317 WM_operator_properties_gesture_straightline(ot, 0);
1318}
1319#endif
1320
1322
1323} // namespace blender
#define DEG2RADF(_deg)
#define UI_SCALE_FAC
#define OPERATOR_RETVAL_CHECK(ret)
Read Blender memory allocation and freeing.
#define RNA_PROP_END
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
#define RNA_PROP_BEGIN(sptr, itemptr, prop)
#define C
Definition RandGen.cpp:29
#define WM_GESTURE_RECT
Definition WM_types.hh:653
#define WM_GESTURE_STRAIGHTLINE
Definition WM_types.hh:657
#define WM_GESTURE_LINES
Definition WM_types.hh:652
#define WM_GESTURE_LASSO
Definition WM_types.hh:655
#define WM_GESTURE_CIRCLE
Definition WM_types.hh:656
#define WM_GESTURE_CROSS_RECT
Definition WM_types.hh:654
#define WM_GESTURE_POLYLINE
Definition WM_types.hh:658
#define UNUSED_FUNCTION(x)
#define MEM_realloc_uninitialized(vmemh, len)
#define printf
#define abs
#define floor
#define ceil
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
T length_squared(const VecBase< T, Size > &a)
T interpolate(const T &a, const T &b, const FactorT &t)
float view2d_view_to_region_y(const View2D *v2d, float y)
Definition view2d.cc:1678
float view2d_region_to_view_x(const View2D *v2d, float x)
Definition view2d.cc:1640
float view2d_region_to_view_y(const View2D *v2d, float y)
Definition view2d.cc:1645
void view2d_edge_pan_init(bContext *C, View2DEdgePanData *vpd, float inside_pad, float outside_pad, float speed_ramp, float max_speed, float delay, float zoom_influence)
void void view2d_edge_pan_apply_event(bContext *C, View2DEdgePanData *vpd, const wmEvent *event)
bool view2d_edge_pan_poll(bContext *C)
float view2d_view_to_region_x(const View2D *v2d, float x)
Definition view2d.cc:1673
constexpr float POLYLINE_CLICK_RADIUS
Definition WM_types.hh:648
BMesh Iterators.
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)
ScrArea * CTX_wm_area(const bContext *C)
void WM_operator_properties_gesture_circle(wmOperatorType *ot)
static void gesture_modal_state_to_operator(wmOperator *op, int modal_state)
wmOperatorStatus WM_gesture_polyline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
wmOperatorStatus WM_gesture_box_invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus WM_gesture_straightline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
MINLINE void mul_v2_fl(float r[2], float f)
static int UNUSED_FUNCTION gesture_modal_state_from_operator(wmOperator *op)
static void gesture_circle_apply(bContext *C, wmOperator *op)
void WM_gesture_lasso_cancel(bContext *C, wmOperator *op)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
wmGesture * WM_gesture_new(wmWindow *window, const ARegion *region, const wmEvent *event, int type)
Definition wm_gesture.cc:40
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
wmOperatorStatus WM_gesture_straightline_modal(bContext *C, wmOperator *op, const wmEvent *event)
void WM_cursor_modal_set(wmWindow *win, int val)
static void gesture_modal_end(bContext *C, wmOperator *op)
MINLINE float fractf(float a)
float angle_signed_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
int RNA_int_get(PointerRNA *ptr, const char *name)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
wmWindow * CTX_wm_window(const bContext *C)
MINLINE float normalize_v2(float n[2])
MINLINE int max_ii(int a, int b)
@ GESTURE_MODAL_CIRCLE_ADD
@ GESTURE_MODAL_DESELECT
@ GESTURE_MODAL_CIRCLE_SUB
@ GESTURE_MODAL_CIRCLE_SIZE
@ GESTURE_MODAL_CONFIRM
int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
wmOperatorStatus WM_gesture_straightline_active_side_invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus WM_gesture_lasso_modal(bContext *C, wmOperator *op, const wmEvent *event)
static bool gesture_box_apply(bContext *C, wmOperator *op)
void WM_gesture_circle_cancel(bContext *C, wmOperator *op)
static void gesture_init_mval_set(wmGesture *gesture, const int2 mval)
MINLINE float pow2f(float x)
@ INBETWEEN_MOUSEMOVE
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
void WM_gesture_lines_cancel(bContext *C, wmOperator *op)
wmOperatorStatus WM_gesture_lines_modal(bContext *C, wmOperator *op, const wmEvent *event)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
static bool gesture_polyline_can_apply(const wmGesture &wmGesture, const bool is_click_submitted)
wmOperatorStatus WM_gesture_lines_invoke(bContext *C, wmOperator *op, const wmEvent *event)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
static void wm_gesture_straightline_do_angle_snap(rcti *rect, float snap_angle)
void RNA_collection_clear(PointerRNA *ptr, const char *name)
MINLINE void add_v2_v2(float r[2], const float a[2])
@ PROP_COLLECTION
Definition RNA_types.hh:216
void rotate_v2_v2fl(float r[2], const float p[2], float angle)
static wmOperatorStatus gesture_lasso_apply(bContext *C, wmOperator *op)
static wmOperatorStatus gesture_polyline_apply(bContext *C, wmOperator *op, const bool is_click_submitted)
wmOperatorStatus WM_gesture_circle_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void BLI_rcti_translate(struct rcti *rect, int x, int y)
Definition rct.cc:580
void WM_cursor_modal_restore(wmWindow *win)
static bool gesture_straightline_apply(bContext *C, wmOperator *op)
void wm_gesture_tag_redraw(wmWindow *win)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_gesture_end(wmWindow *win, wmGesture *gesture)
wmOperatorStatus WM_gesture_polyline_modal(bContext *C, wmOperator *op, const wmEvent *event)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void RNA_def_property_struct_runtime(StructOrFunctionRNA *cont, PropertyRNA *prop, StructRNA *type)
void WM_gesture_polyline_cancel(bContext *C, wmOperator *op)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
wmOperatorStatus WM_gesture_box_modal(bContext *C, wmOperator *op, const wmEvent *event)
bool WM_operator_winactive(bContext *C)
wmOperatorStatus WM_gesture_lasso_invoke(bContext *C, wmOperator *op, const wmEvent *event)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
Array< int2 > WM_gesture_lasso_path_to_array(bContext *, wmOperator *op)
void WM_gesture_box_cancel(bContext *C, wmOperator *op)
static bool gesture_box_apply_rect(wmOperator *op)
static int gesture_polyline_valid_points(const wmGesture &wmGesture, const bool is_click_submitted)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
MINLINE float square_f(float a)
@ KM_RELEASE
Definition WM_types.hh:325
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
Scene * CTX_data_scene(const bContext *C)
wmOperatorStatus WM_gesture_straightline_oneshot_modal(bContext *C, wmOperator *op, const wmEvent *event)
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
static SnapAngle get_snap_angle(const ScrArea &area, const ToolSettings &tool_settings)
void WM_operator_properties_gesture_polyline(wmOperatorType *ot)
bool WM_event_is_mouse_drag_or_press(const wmEvent *event)
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
wmOperatorStatus WM_gesture_circle_modal(bContext *C, wmOperator *op, const wmEvent *event)
void RNA_collection_add(PointerRNA *ptr, const char *name, PointerRNA *r_value)
MINLINE int min_ii(int a, int b)
ARegion * CTX_wm_region(const bContext *C)
float RNA_float_get(PointerRNA *ptr, const char *name)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
MINLINE int compare_ff(float a, float b, float max_diff)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784
void WM_gesture_straightline_cancel(bContext *C, wmOperator *op)
#define floorf
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct ToolSettings * toolsettings
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
struct wmOperatorType * type
#define GESTURE_EDGE_PAN_DELAY
#define GESTURE_EDGE_PAN_OUTSIDE_PAD
#define GESTURE_EDGE_PAN_MAX_SPEED
#define GESTURE_EDGE_PAN_ZOOM_INFLUENCE
#define GESTURE_EDGE_PAN_INSIDE_PAD
#define GESTURE_EDGE_PAN_SPEED_RAMP