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];
49 /* calculate the main translation and the precise one separate */
50 convertViewVec(t, dvec, (mval[0] - mi->precision_mval[0]), (mval[1] - mi->precision_mval[1]));
51 mul_v3_fl(dvec, 0.1f);
52 convertViewVec(t, vec, (mi->precision_mval[0] - t->imval[0]), (mi->precision_mval[1] - t->imval[1]));
53 add_v3_v3v3(output, vec, dvec);
57 convertViewVec(t, output, (mval[0] - t->imval[0]), (mval[1] - t->imval[1]));
62 static void InputSpring(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
64 float ratio, precise_ratio, dx, dy;
67 /* calculate ratio for shiftkey pos, and for total, and blend these for precision */
68 dx = (float)(mi->center[0] - mi->precision_mval[0]);
69 dy = (float)(mi->center[1] - mi->precision_mval[1]);
70 ratio = (float)sqrt( dx*dx + dy*dy);
72 dx= (float)(mi->center[0] - mval[0]);
73 dy= (float)(mi->center[1] - mval[1]);
74 precise_ratio = (float)sqrt( dx*dx + dy*dy);
76 ratio = (ratio + (precise_ratio - ratio) / 10.0f) / mi->factor;
80 dx = (float)(mi->center[0] - mval[0]);
81 dy = (float)(mi->center[1] - mval[1]);
82 ratio = (float)sqrt( dx*dx + dy*dy) / mi->factor;
88 static void InputSpringFlip(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
90 InputSpring(t, mi, mval, output);
93 /* values can become really big when zoomed in so use longs [#26598] */
94 if ((long long int)(mi->center[0] - mval[0]) * (long long int)(mi->center[0] - mi->imval[0]) +
95 (long long int)(mi->center[1] - mval[1]) * (long long int)(mi->center[1] - mi->imval[1]) < 0)
101 static void InputTrackBall(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
106 output[0] = ( mi->imval[1] - mi->precision_mval[1] ) + ( mi->precision_mval[1] - mval[1] ) * 0.1f;
107 output[1] = ( mi->precision_mval[0] - mi->imval[0] ) + ( mval[0] - mi->precision_mval[0] ) * 0.1f;
111 output[0] = (float)( mi->imval[1] - mval[1] );
112 output[1] = (float)( mval[0] - mi->imval[0] );
115 output[0] *= mi->factor;
116 output[1] *= mi->factor;
119 static void InputHorizontalRatio(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
123 pad = t->ar->winx / 10;
127 /* deal with Shift key by adding motion / 10 to motion before shift press */
128 x = mi->precision_mval[0] + (float)(mval[0] - mi->precision_mval[0]) / 10.0f;
134 output[0] = (x - pad) / (t->ar->winx - 2 * pad);
137 static void InputHorizontalAbsolute(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
141 InputVector(t, mi, mval, vec);
142 project_v3_v3v3(vec, vec, t->viewinv[0]);
144 output[0] = dot_v3v3(t->viewinv[0], vec) * 2.0f;
147 static void InputVerticalRatio(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
151 pad = t->ar->winy / 10;
154 /* deal with Shift key by adding motion / 10 to motion before shift press */
155 y = mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f;
161 output[0] = (y - pad) / (t->ar->winy - 2 * pad);
164 static void InputVerticalAbsolute(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
168 InputVector(t, mi, mval, vec);
169 project_v3_v3v3(vec, vec, t->viewinv[1]);
171 output[0] = dot_v3v3(t->viewinv[1], vec) * 2.0f;
174 void setCustomPoints(TransInfo *UNUSED(t), MouseInput *mi, int start[2], int end[2])
178 if (mi->data == NULL) {
179 mi->data = MEM_callocN(sizeof(int) * 4, "custom points");
190 static void InputCustomRatio(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
195 int *data = mi->data;
198 dx = data[2] - data[0];
199 dy = data[3] - data[1];
201 length = sqrt(dx*dx + dy*dy);
204 /* deal with Shift key by adding motion / 10 to motion before shift press */
206 mdx = (mi->precision_mval[0] + (float)(mval[0] - mi->precision_mval[0]) / 10.0f) - data[2];
207 mdy = (mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f) - data[3];
209 distance = (length != 0.0f)? (mdx*dx + mdy*dy) / length: 0.0f;
213 mdx = mval[0] - data[2];
214 mdy = mval[1] - data[3];
216 distance = (length != 0.0f)? (mdx*dx + mdy*dy) / length: 0.0f;
219 output[0] = (float)((length != 0.0f)? distance / length: 0.0f);
223 static void InputAngle(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
225 double dx2 = mval[0] - mi->center[0];
226 double dy2 = mval[1] - mi->center[1];
227 double B = sqrt(dx2*dx2+dy2*dy2);
229 double dx1 = mi->imval[0] - mi->center[0];
230 double dy1 = mi->imval[1] - mi->center[1];
231 double A = sqrt(dx1*dx1+dy1*dy1);
233 double dx3 = mval[0] - mi->imval[0];
234 double dy3 = mval[1] - mi->imval[1];
236 double *angle = mi->data;
238 /* use doubles here, to make sure a "1.0" (no rotation) doesnt become 9.999999e-01, which gives 0.02 for acos */
239 double deler = ((dx1*dx1+dy1*dy1)+(dx2*dx2+dy2*dy2)-(dx3*dx3+dy3*dy3))
240 / (2.0 * ((A*B)?(A*B):1.0));
241 /* ((A*B)?(A*B):1.0) this takes care of potential divide by zero errors */
245 dphi = saacos((float)deler);
246 if( (dx1*dy2-dx2*dy1)>0.0 ) dphi= -dphi;
248 /* If the angle is zero, because of lack of precision close to the 1.0 value in acos
249 * approximate the angle with the opposite side of the normalized triangle
250 * This is a good approximation here since the smallest acos value seems to be around
251 * 0.02 degree and lower values don't even have a 0.01% error compared to the approximation
266 dphi = sqrt(dx*dx + dy*dy);
267 if( (dx1*dy2-dx2*dy1)>0.0 ) dphi= -dphi;
270 if(mi->precision) dphi = dphi/30.0f;
272 /* if no delta angle, don't update initial position */
275 mi->imval[0] = mval[0];
276 mi->imval[1] = mval[1];
279 *angle += (double)dphi;
284 void initMouseInput(TransInfo *UNUSED(t), MouseInput *mi, int center[2], int mval[2])
289 mi->center[0] = center[0];
290 mi->center[1] = center[1];
292 mi->imval[0] = mval[0];
293 mi->imval[1] = mval[1];
298 static void calcSpringFactor(MouseInput *mi)
300 mi->factor = (float)sqrt(
302 ((float)(mi->center[1] - mi->imval[1]))*((float)(mi->center[1] - mi->imval[1]))
304 ((float)(mi->center[0] - mi->imval[0]))*((float)(mi->center[0] - mi->imval[0]))
307 if (mi->factor==0.0f)
308 mi->factor= 1.0f; /* prevent Inf */
311 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
313 /* may have been allocated previously */
314 /* TODO, holding R-key can cause mem leak, but this causes [#28903]
315 * disable for now. */
326 mi->apply = InputVector;
327 t->helpline = HLP_NONE;
330 calcSpringFactor(mi);
331 mi->apply = InputSpring;
332 t->helpline = HLP_SPRING;
334 case INPUT_SPRING_FLIP:
335 calcSpringFactor(mi);
336 mi->apply = InputSpringFlip;
337 t->helpline = HLP_SPRING;
340 mi->data = MEM_callocN(sizeof(double), "angle accumulator");
341 mi->apply = InputAngle;
342 t->helpline = HLP_ANGLE;
344 case INPUT_TRACKBALL:
345 /* factor has to become setting or so */
347 mi->apply = InputTrackBall;
348 t->helpline = HLP_TRACKBALL;
350 case INPUT_HORIZONTAL_RATIO:
351 mi->factor = (float)(mi->center[0] - mi->imval[0]);
352 mi->apply = InputHorizontalRatio;
353 t->helpline = HLP_HARROW;
355 case INPUT_HORIZONTAL_ABSOLUTE:
356 mi->apply = InputHorizontalAbsolute;
357 t->helpline = HLP_HARROW;
359 case INPUT_VERTICAL_RATIO:
360 mi->apply = InputVerticalRatio;
361 t->helpline = HLP_VARROW;
363 case INPUT_VERTICAL_ABSOLUTE:
364 mi->apply = InputVerticalAbsolute;
365 t->helpline = HLP_VARROW;
367 case INPUT_CUSTOM_RATIO:
368 mi->apply = InputCustomRatio;
369 t->helpline = HLP_NONE;
377 /* bootstrap mouse input with initial values */
378 applyMouseInput(t, mi, mi->imval, t->values);
381 void setInputPostFct(MouseInput *mi, void (*post)(struct TransInfo *, float [3]))
386 void applyMouseInput(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
388 if (mi->apply != NULL)
390 mi->apply(t, mi, mval, output);
399 int handleMouseInput(TransInfo *t, MouseInput *mi, wmEvent *event)
401 int redraw = TREDRAW_NOTHING;
407 if (event->val==KM_PRESS)
409 t->modifiers |= MOD_PRECISION;
410 /* shift is modifier for higher precision transform
411 * store the mouse position where the normal movement ended */
412 copy_v2_v2_int(mi->precision_mval, event->mval);
417 t->modifiers &= ~MOD_PRECISION;
420 redraw = TREDRAW_HARD;