2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): none yet.
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/editors/transform/transform_input.c
24 * \ingroup edtransform
31 #include "DNA_screen_types.h"
34 #include "BLI_utildefines.h"
38 #include "transform.h"
40 #include "MEM_guardedalloc.h"
42 /* ************************** INPUT FROM MOUSE *************************** */
44 static void InputVector(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
46 float vec[3], dvec[3];
48 /* calculate the main translation and the precise one separate */
49 convertViewVec(t, dvec, (mval[0] - mi->precision_mval[0]), (mval[1] - mi->precision_mval[1]));
50 mul_v3_fl(dvec, 0.1f);
51 convertViewVec(t, vec, (mi->precision_mval[0] - t->imval[0]), (mi->precision_mval[1] - t->imval[1]));
52 add_v3_v3v3(output, vec, dvec);
55 convertViewVec(t, output, (mval[0] - t->imval[0]), (mval[1] - t->imval[1]));
60 static void InputSpring(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
62 float ratio, precise_ratio, dx, dy;
64 /* calculate ratio for shiftkey pos, and for total, and blend these for precision */
65 dx = (float)(mi->center[0] - mi->precision_mval[0]);
66 dy = (float)(mi->center[1] - mi->precision_mval[1]);
67 ratio = sqrtf(dx * dx + dy * dy);
69 dx = (float)(mi->center[0] - mval[0]);
70 dy = (float)(mi->center[1] - mval[1]);
71 precise_ratio = sqrtf(dx * dx + dy * dy);
73 ratio = (ratio + (precise_ratio - ratio) / 10.0f) / mi->factor;
76 dx = (float)(mi->center[0] - mval[0]);
77 dy = (float)(mi->center[1] - mval[1]);
78 ratio = sqrtf(dx * dx + dy * dy) / mi->factor;
84 static void InputSpringFlip(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
86 InputSpring(t, mi, mval, output);
89 /* values can become really big when zoomed in so use longs [#26598] */
90 if ((long long int)(mi->center[0] - mval[0]) * (long long int)(mi->center[0] - mi->imval[0]) +
91 (long long int)(mi->center[1] - mval[1]) * (long long int)(mi->center[1] - mi->imval[1]) < 0)
97 static void InputTrackBall(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
101 output[0] = (mi->imval[1] - mi->precision_mval[1]) + (mi->precision_mval[1] - mval[1]) * 0.1f;
102 output[1] = (mi->precision_mval[0] - mi->imval[0]) + (mval[0] - mi->precision_mval[0]) * 0.1f;
105 output[0] = (float)(mi->imval[1] - mval[1]);
106 output[1] = (float)(mval[0] - mi->imval[0]);
109 output[0] *= mi->factor;
110 output[1] *= mi->factor;
113 static void InputHorizontalRatio(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
117 pad = t->ar->winx / 10;
120 /* deal with Shift key by adding motion / 10 to motion before shift press */
121 x = mi->precision_mval[0] + (float)(mval[0] - mi->precision_mval[0]) / 10.0f;
127 output[0] = (x - pad) / (t->ar->winx - 2 * pad);
130 static void InputHorizontalAbsolute(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
134 InputVector(t, mi, mval, vec);
135 project_v3_v3v3(vec, vec, t->viewinv[0]);
137 output[0] = dot_v3v3(t->viewinv[0], vec) * 2.0f;
140 static void InputVerticalRatio(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
144 pad = t->ar->winy / 10;
147 /* deal with Shift key by adding motion / 10 to motion before shift press */
148 y = mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f;
154 output[0] = (y - pad) / (t->ar->winy - 2 * pad);
157 static void InputVerticalAbsolute(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
161 InputVector(t, mi, mval, vec);
162 project_v3_v3v3(vec, vec, t->viewinv[1]);
164 output[0] = dot_v3v3(t->viewinv[1], vec) * 2.0f;
167 void setCustomPoints(TransInfo *UNUSED(t), MouseInput *mi, const int mval_start[2], const int mval_end[2])
171 mi->data = MEM_reallocN(mi->data, sizeof(int) * 4);
175 data[0] = mval_start[0];
176 data[1] = mval_start[1];
177 data[2] = mval_end[0];
178 data[3] = mval_end[1];
181 static void InputCustomRatioFlip(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
186 int *data = mi->data;
189 dx = data[2] - data[0];
190 dy = data[3] - data[1];
192 length = sqrt(dx * dx + dy * dy);
195 /* deal with Shift key by adding motion / 10 to motion before shift press */
197 mdx = (mi->precision_mval[0] + (float)(mval[0] - mi->precision_mval[0]) / 10.0f) - data[2];
198 mdy = (mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f) - data[3];
200 distance = (length != 0.0) ? (mdx * dx + mdy * dy) / length : 0.0;
204 mdx = mval[0] - data[2];
205 mdy = mval[1] - data[3];
207 distance = (length != 0.0) ? (mdx * dx + mdy * dy) / length : 0.0;
210 output[0] = (length != 0.0) ? (double)(distance / length) : 0.0;
214 static void InputCustomRatio(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
216 InputCustomRatioFlip(t, mi, mval, output);
217 output[0] = -output[0];
220 static void InputAngle(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
222 double dx2 = mval[0] - mi->center[0];
223 double dy2 = mval[1] - mi->center[1];
224 double B = sqrt(dx2 * dx2 + dy2 * dy2);
226 double dx1 = mi->imval[0] - mi->center[0];
227 double dy1 = mi->imval[1] - mi->center[1];
228 double A = sqrt(dx1 * dx1 + dy1 * dy1);
230 double dx3 = mval[0] - mi->imval[0];
231 double dy3 = mval[1] - mi->imval[1];
233 double *angle = mi->data;
235 /* use doubles here, to make sure a "1.0" (no rotation) doesn't become 9.999999e-01, which gives 0.02 for acos */
236 double deler = (((dx1 * dx1 + dy1 * dy1) +
237 (dx2 * dx2 + dy2 * dy2) -
238 (dx3 * dx3 + dy3 * dy3)) / (2.0 * ((A * B) ? (A * B) : 1.0)));
239 /* ((A * B) ? (A * B) : 1.0) this takes care of potential divide by zero errors */
243 dphi = saacos((float)deler);
244 if ((dx1 * dy2 - dx2 * dy1) > 0.0) dphi = -dphi;
246 /* If the angle is zero, because of lack of precision close to the 1.0 value in acos
247 * approximate the angle with the opposite side of the normalized triangle
248 * This is a good approximation here since the smallest acos value seems to be around
249 * 0.02 degree and lower values don't even have a 0.01% error compared to the approximation
263 dphi = sqrt(dx * dx + dy * dy);
264 if ((dx1 * dy2 - dx2 * dy1) > 0.0) dphi = -dphi;
271 /* if no delta angle, don't update initial position */
273 mi->imval[0] = mval[0];
274 mi->imval[1] = mval[1];
277 *angle += (double)dphi;
282 static void InputAngleSpring(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
286 InputAngle(t, mi, mval, output);
287 InputSpring(t, mi, mval, toutput);
289 output[1] = toutput[0];
292 void initMouseInput(TransInfo *UNUSED(t), MouseInput *mi, const float center[2], const int mval[2])
297 mi->center[0] = center[0];
298 mi->center[1] = center[1];
300 mi->imval[0] = mval[0];
301 mi->imval[1] = mval[1];
306 static void calcSpringFactor(MouseInput *mi)
308 mi->factor = sqrtf(((float)(mi->center[1] - mi->imval[1])) * ((float)(mi->center[1] - mi->imval[1])) +
309 ((float)(mi->center[0] - mi->imval[0])) * ((float)(mi->center[0] - mi->imval[0])));
311 if (mi->factor == 0.0f) {
312 mi->factor = 1.0f; /* prevent Inf */
316 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
318 /* incase we allocate a new value */
319 void *mi_data_prev = mi->data;
323 mi->apply = InputVector;
324 t->helpline = HLP_NONE;
327 calcSpringFactor(mi);
328 mi->apply = InputSpring;
329 t->helpline = HLP_SPRING;
331 case INPUT_SPRING_FLIP:
332 calcSpringFactor(mi);
333 mi->apply = InputSpringFlip;
334 t->helpline = HLP_SPRING;
337 mi->data = MEM_callocN(sizeof(double), "angle accumulator");
338 mi->apply = InputAngle;
339 t->helpline = HLP_ANGLE;
341 case INPUT_ANGLE_SPRING:
342 calcSpringFactor(mi);
343 mi->data = MEM_callocN(sizeof(double), "angle accumulator");
344 mi->apply = InputAngleSpring;
345 t->helpline = HLP_ANGLE;
347 case INPUT_TRACKBALL:
348 /* factor has to become setting or so */
350 mi->apply = InputTrackBall;
351 t->helpline = HLP_TRACKBALL;
353 case INPUT_HORIZONTAL_RATIO:
354 mi->factor = (float)(mi->center[0] - mi->imval[0]);
355 mi->apply = InputHorizontalRatio;
356 t->helpline = HLP_HARROW;
358 case INPUT_HORIZONTAL_ABSOLUTE:
359 mi->apply = InputHorizontalAbsolute;
360 t->helpline = HLP_HARROW;
362 case INPUT_VERTICAL_RATIO:
363 mi->apply = InputVerticalRatio;
364 t->helpline = HLP_VARROW;
366 case INPUT_VERTICAL_ABSOLUTE:
367 mi->apply = InputVerticalAbsolute;
368 t->helpline = HLP_VARROW;
370 case INPUT_CUSTOM_RATIO:
371 mi->apply = InputCustomRatio;
372 t->helpline = HLP_NONE;
374 case INPUT_CUSTOM_RATIO_FLIP:
375 mi->apply = InputCustomRatioFlip;
376 t->helpline = HLP_NONE;
384 /* if we've allocated new data, free the old data
385 * less hassle then checking before every alloc above */
386 if (mi_data_prev && (mi_data_prev != mi->data)) {
387 MEM_freeN(mi_data_prev);
390 /* bootstrap mouse input with initial values */
391 applyMouseInput(t, mi, mi->imval, t->values);
394 void setInputPostFct(MouseInput *mi, void (*post)(struct TransInfo *t, float values[3]))
399 void applyMouseInput(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
401 if (mi->apply != NULL) {
402 mi->apply(t, mi, mval, output);
410 eRedrawFlag handleMouseInput(TransInfo *t, MouseInput *mi, const wmEvent *event)
412 eRedrawFlag redraw = TREDRAW_NOTHING;
414 switch (event->type) {
417 if (event->val == KM_PRESS) {
418 t->modifiers |= MOD_PRECISION;
419 /* shift is modifier for higher precision transform
420 * store the mouse position where the normal movement ended */
421 copy_v2_v2_int(mi->precision_mval, event->mval);
423 redraw = TREDRAW_HARD;
425 else if (event->val == KM_RELEASE) {
426 t->modifiers &= ~MOD_PRECISION;
428 redraw = TREDRAW_HARD;