Blender V5.3
transform_input.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <cstdlib>
11
12#include "DNA_screen_types.h"
13#include "DNA_sequence_types.h"
14#include "DNA_space_types.h"
15#include "DNA_userdef_types.h"
16
17#include "BKE_context.hh"
18
19#include "BLI_math_vector_c.hh"
20#include "BLI_utildefines.hh"
21
22#include "WM_api.hh"
23#include "WM_types.hh"
24
25#include "transform.hh"
26#include "transform_mode.hh"
27
28#include "ED_sequencer.hh"
29
30#include "MEM_guardedalloc.h"
31
32namespace blender::ed::transform {
33
34/* -------------------------------------------------------------------- */
37
39static void InputVector(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
40{
41 convertViewVec(t, output, mval[0] - mi->imval[0], mval[1] - mi->imval[1]);
42}
43
45static void InputSpring(TransInfo * /*t*/, MouseInput *mi, const double mval[2], float output[3])
46{
47 double dx, dy;
48 float ratio;
49
50 dx = double(mi->center[0]) - mval[0];
51 dy = double(mi->center[1]) - mval[1];
52 ratio = hypot(dx, dy) / double(mi->factor);
53
54 output[0] = ratio;
55}
56
58static void InputSpringFlip(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
59{
60 InputSpring(t, mi, mval, output);
61
62 /* Flip scale. */
63 /* Values can become really big when zoomed in so use longs #26598. */
64 if ((int64_t(int(mi->center[0]) - mval[0]) * int64_t(int(mi->center[0]) - mi->imval[0]) +
65 int64_t(int(mi->center[1]) - mval[1]) * int64_t(int(mi->center[1]) - mi->imval[1])) < 0)
66 {
67 output[0] *= -1.0f;
68 }
69}
70
72static void InputSpringDelta(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
73{
74 InputSpring(t, mi, mval, output);
75 output[0] -= 1.0f;
76}
77
79static void InputTrackBall(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
80{
81 output[0] = float(mi->imval[1] - mval[1]);
82 output[1] = float(mval[0] - mi->imval[0]);
83
84 output[0] *= mi->factor;
85 output[1] *= mi->factor;
86
87 if (t->flag & T_VIEW_NEGATIVE) {
89 }
90}
91
94 MouseInput *mi,
95 const double mval[2],
96 float output[3])
97{
98 const int winx = t->region ? t->region->winx : 1;
99
100 output[0] = ((mval[0] - mi->imval[0]) / winx) * 2.0f;
101}
102
105 MouseInput *mi,
106 const double mval[2],
107 float output[3])
108{
109 float vec[3];
110
111 InputVector(t, mi, mval, vec);
112 project_v3_v3v3(vec, vec, t->viewinv[0]);
113
114 output[0] = dot_v3v3(t->viewinv[0], vec) * 2.0f;
115}
116
117static void InputVerticalRatio(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
118{
119 const int winy = t->region ? t->region->winy : 1;
120
121 /* Dragging up increases (matching viewport zoom). */
122 output[0] = ((mval[1] - mi->imval[1]) / winy) * 2.0f;
123}
124
127 MouseInput *mi,
128 const double mval[2],
129 float output[3])
130{
131 float vec[3];
132
133 InputVector(t, mi, mval, vec);
134 project_v3_v3v3(vec, vec, t->viewinv[1]);
135
136 /* Dragging up increases (matching viewport zoom). */
137 output[0] = dot_v3v3(t->viewinv[1], vec) * 2.0f;
138}
139
141static void InputCustomRatioFlip(TransInfo * /*t*/,
142 MouseInput *mi,
143 const double mval[2],
144 float output[3])
145{
146 double length;
147 double distance;
148 double dx, dy;
149 const int *data = static_cast<const int *>(mi->data);
150
151 if (data) {
152 int mdx, mdy;
153 dx = data[2] - data[0];
154 dy = data[3] - data[1];
155
156 length = hypot(dx, dy);
157
158 mdx = mval[0] - data[2];
159 mdy = mval[1] - data[3];
160
161 distance = (length != 0.0) ? (mdx * dx + mdy * dy) / length : 0.0;
162
163 output[0] = (length != 0.0) ? (distance / length) : 0.0;
164 }
165}
166
168static void InputCustomRatio(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
169{
170 InputCustomRatioFlip(t, mi, mval, output);
171 output[0] = -output[0];
172}
173
175 double angle;
176 double mval_prev[2];
177};
178
180static void InputAngle(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
181{
182 InputAngle_Data *data = static_cast<InputAngle_Data *>(mi->data);
183 float dir_prev[2], dir_curr[2], mi_center[2];
184 copy_v2_v2(mi_center, mi->center);
185
186 sub_v2_v2v2(dir_prev, float2{float(data->mval_prev[0]), float(data->mval_prev[1])}, mi_center);
187 sub_v2_v2v2(dir_curr, float2{float(mval[0]), float(mval[1])}, mi_center);
188
189 if (normalize_v2(dir_prev) && normalize_v2(dir_curr)) {
190 float dphi = angle_normalized_v2v2(dir_prev, dir_curr);
191
192 if (cross_v2v2(dir_prev, dir_curr) > 0.0f) {
193 dphi = -dphi;
194 }
195
196 if (t->flag & T_VIEW_NEGATIVE) {
197 dphi = -dphi;
198 }
199
200 data->angle += double(dphi) * (mi->precision ? double(mi->precision_factor) : 1.0);
201
202 data->mval_prev[0] = mval[0];
203 data->mval_prev[1] = mval[1];
204 }
205
206 output[0] = data->angle;
207}
208
209static void InputAngleSpring(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
210{
211 float toutput[3];
212
213 InputAngle(t, mi, mval, output);
214 InputSpring(t, mi, mval, toutput);
215
216 output[1] = toutput[0];
217}
218
220
221/* -------------------------------------------------------------------- */
227
229 MouseInput *mi,
230 const int mval_start[2],
231 const int mval_end[2])
232{
233 int *data;
234
235 mi->data = MEM_realloc_uninitialized(mi->data, sizeof(int[4]));
236
237 data = static_cast<int *>(mi->data);
238
239 data[0] = mval_start[0];
240 data[1] = mval_start[1];
241 data[2] = mval_end[0];
242 data[3] = mval_end[1];
243}
244
246{
248 const int win_axis =
249 t->region ? ((abs(int(t->region->winx * dir[0])) + abs(int(t->region->winy * dir[1]))) / 2) :
250 1;
251 const int2 mval_start = int2(mi->imval + dir * win_axis);
252 const int2 mval_end = int2(mi->imval);
253 setCustomPoints(t, mi, mval_start, mval_end);
254}
255
257
258/* -------------------------------------------------------------------- */
261
263{
264 MouseInput *mi = &t->mouse;
265
266 mi->imval = mval;
267
268 if (mi->data && ELEM(mi->apply, InputAngle, InputAngleSpring)) {
269 InputAngle_Data *data = static_cast<InputAngle_Data *>(mi->data);
270 data->mval_prev[0] = mi->imval[0];
271 data->mval_prev[1] = mi->imval[1];
272 data->angle = 0.0f;
273 }
274}
275
277 TransInfo *t, MouseInput *mi, const float2 &center, const float2 &mval, const bool precision)
278{
279 mi->factor = 0;
280 mi->precision = precision;
281
282 mi->center = center;
283
284 mi->post = nullptr;
285
286 transform_input_reset(t, mval);
287}
288
290{
291 float mdir[2] = {float(mi->center[1] - mi->imval[1]), float(mi->center[0] - mi->imval[0])};
292
293 mi->factor = len_v2(mdir);
294
295 if (mi->factor == 0.0f) {
296 mi->factor = 1.0f; /* Prevent inf. */
297 }
298}
299
301{
302 if ((strip->flag & SEQ_LEFTSEL) != 0) {
304 }
305 if ((strip->flag & SEQ_RIGHTSEL) != 0) {
307 }
309}
310
312{
313 const Scene *scene = t->scene;
315
316 if (strips.size() == 1) {
317 return transform_seq_slide_strip_cursor_get(strips[0]);
318 }
319 if (strips.size() == 2) {
320 Strip *strip1 = strips[0];
321 Strip *strip2 = strips[1];
322
323 if (strip1->left_handle() > strip2->left_handle()) {
324 SWAP(Strip *, strip1, strip2);
325 }
326
327 if (strip1->channel != strip2->channel) {
329 }
330
331 if (strip1->right_handle(scene) != strip2->left_handle()) {
333 }
334
335 const int cursor1 = transform_seq_slide_strip_cursor_get(strip1);
336 const int cursor2 = transform_seq_slide_strip_cursor_get(strip2);
337
338 if (cursor1 == WM_CURSOR_RIGHT_HANDLE && cursor2 == WM_CURSOR_LEFT_HANDLE) {
340 }
341 }
342
344}
345
347{
348 /* In case we allocate a new value. */
349 void *mi_data_prev = mi->data;
350
351 mi->use_virtual_mval = true;
352 mi->precision_factor = 1.0f / 10.0f;
353
354 switch (mode) {
355 case INPUT_VECTOR:
356 mi->apply = InputVector;
357 t->helpline = HLP_NONE;
358 break;
359 case INPUT_SPRING:
361 mi->apply = InputSpring;
362 t->helpline = HLP_SPRING;
363 break;
367 t->helpline = HLP_SPRING;
368 break;
372 t->helpline = HLP_SPRING;
373 break;
374 case INPUT_ANGLE:
375 case INPUT_ANGLE_SPRING: {
377 mi->use_virtual_mval = false;
378 mi->precision_factor = 1.0f / 30.0f;
379 data = MEM_new_zeroed<InputAngle_Data>("angle accumulator");
380 data->mval_prev[0] = mi->imval[0];
381 data->mval_prev[1] = mi->imval[1];
382 mi->data = data;
383 if (mode == INPUT_ANGLE) {
384 mi->apply = InputAngle;
385 }
386 else {
389 }
390 t->helpline = HLP_ANGLE;
391 break;
392 }
393 case INPUT_TRACKBALL:
394 mi->precision_factor = 1.0f / 30.0f;
395 /* Factor has to become setting or so. */
396 mi->factor = 0.01f;
397 mi->apply = InputTrackBall;
399 break;
402 t->helpline = HLP_HARROW;
403 break;
406 t->helpline = HLP_HARROW;
407 break;
410 t->helpline = HLP_VARROW;
411 break;
414 t->helpline = HLP_VARROW;
415 break;
418 t->helpline = HLP_CARROW;
419 break;
422 t->helpline = HLP_CARROW;
423 break;
424 case INPUT_ERROR:
425 mi->apply = nullptr;
426 t->helpline = HLP_ERROR;
427 break;
428 case INPUT_ERROR_DASH:
429 mi->apply = nullptr;
431 break;
432 case INPUT_NONE:
433 default:
434 mi->apply = nullptr;
435 break;
436 }
437
438 /* Setup for the mouse cursor: either set a custom one,
439 * or hide it if it will be drawn with the helpline. */
440 wmWindow *win = CTX_wm_window(t->context);
441 switch (t->helpline) {
442 case HLP_NONE:
443 /* INPUT_VECTOR, INPUT_CUSTOM_RATIO, INPUT_CUSTOM_RATIO_FLIP. */
444 if (t->flag & T_MODAL) {
447 }
448 /* Only use special cursor, when tweaking strips with mouse. */
449 if (t->mode == TFM_SEQ_SLIDE) {
452 }
453 else {
455 if (sseq != nullptr) {
457 }
458 }
459 }
460
461 break;
462 case HLP_SPRING:
463 case HLP_ANGLE:
464 case HLP_TRACKBALL:
465 case HLP_HARROW:
466 case HLP_VARROW:
467 case HLP_CARROW:
468 if (t->flag & T_MODAL) {
471 }
472 break;
473 case HLP_ERROR:
474 case HLP_ERROR_DASH:
477 break;
478 default:
479 break;
480 }
481
482 /* If we've allocated new data, free the old data
483 * less hassle than checking before every alloc above. */
484 if (mi_data_prev && (mi_data_prev != mi->data)) {
485 MEM_delete_void(mi_data_prev);
486 }
487}
488
489void setInputPostFct(MouseInput *mi, void (*post)(TransInfo *t, float values[3]))
490{
491 mi->post = post;
492}
493
494void applyMouseInput(TransInfo *t, MouseInput *mi, const float2 &mval, float output[3])
495{
496 double mval_db[2];
497
498 if (mi->use_virtual_mval) {
499 /* Update accumulator. */
500 double mval_delta[2];
501
502 mval_delta[0] = (mval[0] - mi->imval[0]) - mi->virtual_mval.prev[0];
503 mval_delta[1] = (mval[1] - mi->imval[1]) - mi->virtual_mval.prev[1];
504
505 mi->virtual_mval.prev[0] += mval_delta[0];
506 mi->virtual_mval.prev[1] += mval_delta[1];
507
508 if (mi->precision) {
509 mval_delta[0] *= double(mi->precision_factor);
510 mval_delta[1] *= double(mi->precision_factor);
511 }
512
513 mi->virtual_mval.accum[0] += mval_delta[0];
514 mi->virtual_mval.accum[1] += mval_delta[1];
515
516 mval_db[0] = mi->imval[0] + mi->virtual_mval.accum[0];
517 mval_db[1] = mi->imval[1] + mi->virtual_mval.accum[1];
518 }
519 else {
520 mval_db[0] = mval[0];
521 mval_db[1] = mval[1];
522 }
523
524 if (mi->apply != nullptr) {
525 mi->apply(t, mi, mval_db, output);
526 }
527
528 if (mi->post) {
529 mi->post(t, output);
530 }
531}
532
533void transform_input_update(TransInfo *t, const float fac)
534{
535 MouseInput *mi = &t->mouse;
536 float2 offset = fac * (mi->imval - mi->center);
537 mi->imval = t->center2d + offset;
538 mi->factor *= fac;
539
540 float center_old[2];
541 copy_v2_v2(center_old, mi->center);
542 copy_v2_v2(mi->center, t->center2d);
543
544 if (mi->use_virtual_mval) {
545 /* Update accumulator. */
546 double2 mval_delta;
547 sub_v2_v2v2_db(mval_delta, mi->virtual_mval.accum, mi->virtual_mval.prev);
548 mval_delta[0] *= fac;
549 mval_delta[1] *= fac;
550 copy_v2_v2_db(mi->virtual_mval.accum, mi->virtual_mval.prev);
551 mi->virtual_mval.accum += mval_delta;
552 }
553
555 float offset_center[2];
556 sub_v2_v2v2(offset_center, mi->center, center_old);
557 InputAngle_Data *data = static_cast<InputAngle_Data *>(mi->data);
558 data->mval_prev[0] += offset_center[0];
559 data->mval_prev[1] += offset_center[1];
560 }
561
562 if (t->mode == TFM_EDGE_SLIDE) {
564 }
565 else if (t->mode == TFM_VERT_SLIDE) {
567 }
568}
569
571{
572 MouseInput *mi = &t->mouse;
574 InputAngle_Data *data = static_cast<InputAngle_Data *>(mi->data);
575 data->angle = 0.0;
576 data->mval_prev[0] = mi->imval[0];
577 data->mval_prev[1] = mi->imval[1];
578 }
579 else {
580 memset(&mi->virtual_mval, 0, sizeof(mi->virtual_mval));
581 }
582}
583
585
586} // namespace blender::ed::transform
#define BLI_ASSERT_UNIT_V2(v)
Read Blender memory allocation and freeing.
#define SWAP(type, a, b)
#define ELEM(...)
#define MEM_realloc_uninitialized(vmemh, len)
#define output
int64_t size() const
#define abs
float distance(float, float) RET
float length(VecOp< float, D >) RET
float hypot(float x, float y)
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
static void InputAngleSpring(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
static void InputAngle(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
void transform_mode_vert_slide_reproject_input(TransInfo *t)
void transform_input_virtual_mval_reset(TransInfo *t)
static void InputVerticalRatio(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
void setCustomPointsFromDirection(TransInfo *t, MouseInput *mi, const float2 &dir)
static void InputSpringFlip(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
static void InputSpringDelta(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
static void InputCustomRatio(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
void transform_input_reset(TransInfo *t, const float2 &mval)
void initMouseInput(TransInfo *t, MouseInput *mi, const float2 &center, const float2 &mval, bool precision)
static int transform_seq_slide_strip_cursor_get(const Strip *strip)
void convertViewVec(TransInfo *t, float r_vec[3], double dx, double dy)
void setCustomPoints(TransInfo *t, MouseInput *mi, const int mval_start[2], const int mval_end[2])
bool transform_mode_edge_seq_slide_use_restore_handle_selection(const TransInfo *t)
static void InputHorizontalAbsolute(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
void setInputPostFct(MouseInput *mi, void(*post)(TransInfo *t, float values[3]))
static void InputVector(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
static void InputVerticalAbsolute(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
static void InputCustomRatioFlip(TransInfo *, MouseInput *mi, const double mval[2], float output[3])
void applyMouseInput(TransInfo *t, MouseInput *mi, const float2 &mval, float output[3])
static void InputHorizontalRatio(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
static void InputSpring(TransInfo *, MouseInput *mi, const double mval[2], float output[3])
void transform_input_update(TransInfo *t, const float fac)
static int transform_seq_slide_cursor_get(TransInfo *t)
static void calcSpringFactor(MouseInput *mi)
void transform_mode_edge_slide_reproject_input(TransInfo *t)
static void InputTrackBall(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
VectorSet< Strip * > selected_strips_from_context(bContext *C)
__int64 int64_t
Definition BKE_text.h:90
MINLINE void sub_v2_v2v2_db(double r[2], const double a[2], const double b[2])
MINLINE float cross_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v2_v2(float r[2], const float a[2])
void WM_cursor_modal_set(wmWindow *win, int val)
VecBase< double, 2 > double2
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])
void MEM_delete_void(void *vmemh)
SpaceSeq * CTX_wm_space_seq(const bContext *C)
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
float angle_normalized_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void negate_v2(float r[2])
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BMesh const char void * data
MINLINE void copy_v2_v2_db(double r[2], const double a[2])
@ WM_CURSOR_LEFT_HANDLE
Definition wm_cursors.hh:66
@ WM_CURSOR_NSEW_SCROLL
Definition wm_cursors.hh:54
@ WM_CURSOR_STOP
Definition wm_cursors.hh:20
@ WM_CURSOR_RIGHT_HANDLE
Definition wm_cursors.hh:67
@ WM_CURSOR_NONE
Definition wm_cursors.hh:61
@ WM_CURSOR_BOTH_HANDLES
Definition wm_cursors.hh:68
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
@ SPACE_SEQ_DESELECT_STRIP_HANDLE
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eSpaceSeq_Flag flag
void(*) post(TransInfo *t, float values[3])
Definition transform.hh:625
void(*) apply(TransInfo *t, MouseInput *mi, const double mval[2], float output[3])
Definition transform.hh:624
struct blender::ed::transform::MouseInput::@010363217010073026234025274315031014173306050225 virtual_mval
transform modes used by different operators.