4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Contributor(s): none yet.
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/editors/transform/transform_input.c
26 * \ingroup edtransform
33 #include "DNA_screen_types.h"
36 #include "BLI_utildefines.h"
40 #include "transform.h"
42 #include "MEM_guardedalloc.h"
44 /* ************************** INPUT FROM MOUSE *************************** */
46 static void InputVector(TransInfo *t, MouseInput *mi, short mval[2], float output[3])
48 float vec[3], dvec[3];
51 /* calculate the main translation and the precise one separate */
52 convertViewVec(t, dvec, (short)(mval[0] - mi->precision_mval[0]), (short)(mval[1] - mi->precision_mval[1]));
53 mul_v3_fl(dvec, 0.1f);
54 convertViewVec(t, vec, (short)(mi->precision_mval[0] - t->imval[0]), (short)(mi->precision_mval[1] - t->imval[1]));
55 add_v3_v3v3(output, vec, dvec);
59 convertViewVec(t, output, (short)(mval[0] - t->imval[0]), (short)(mval[1] - t->imval[1]));
64 static void InputSpring(TransInfo *UNUSED(t), MouseInput *mi, short mval[2], float output[3])
66 float ratio, precise_ratio, dx, dy;
69 /* calculate ratio for shiftkey pos, and for total, and blend these for precision */
70 dx = (float)(mi->center[0] - mi->precision_mval[0]);
71 dy = (float)(mi->center[1] - mi->precision_mval[1]);
72 ratio = (float)sqrt( dx*dx + dy*dy);
74 dx= (float)(mi->center[0] - mval[0]);
75 dy= (float)(mi->center[1] - mval[1]);
76 precise_ratio = (float)sqrt( dx*dx + dy*dy);
78 ratio = (ratio + (precise_ratio - ratio) / 10.0f) / mi->factor;
82 dx = (float)(mi->center[0] - mval[0]);
83 dy = (float)(mi->center[1] - mval[1]);
84 ratio = (float)sqrt( dx*dx + dy*dy) / mi->factor;
90 static void InputSpringFlip(TransInfo *t, MouseInput *mi, short mval[2], float output[3])
92 InputSpring(t, mi, mval, output);
95 if ((mi->center[0] - mval[0]) * (mi->center[0] - mi->imval[0]) +
96 (mi->center[1] - mval[1]) * (mi->center[1] - mi->imval[1]) < 0)
102 static void InputTrackBall(TransInfo *UNUSED(t), MouseInput *mi, short mval[2], float output[3])
107 output[0] = ( mi->imval[1] - mi->precision_mval[1] ) + ( mi->precision_mval[1] - mval[1] ) * 0.1f;
108 output[1] = ( mi->precision_mval[0] - mi->imval[0] ) + ( mval[0] - mi->precision_mval[0] ) * 0.1f;
112 output[0] = (float)( mi->imval[1] - mval[1] );
113 output[1] = (float)( mval[0] - mi->imval[0] );
116 output[0] *= mi->factor;
117 output[1] *= mi->factor;
120 static void InputHorizontalRatio(TransInfo *t, MouseInput *mi, short 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, short mval[2], float output[3]) {
140 InputVector(t, mi, mval, vec);
141 project_v3_v3v3(vec, vec, t->viewinv[0]);
143 output[0] = dot_v3v3(t->viewinv[0], vec) * 2.0f;
146 static void InputVerticalRatio(TransInfo *t, MouseInput *mi, short mval[2], float output[3]) {
149 pad = t->ar->winy / 10;
152 /* deal with Shift key by adding motion / 10 to motion before shift press */
153 y = mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f;
159 output[0] = (y - pad) / (t->ar->winy - 2 * pad);
162 static void InputVerticalAbsolute(TransInfo *t, MouseInput *mi, short mval[2], float output[3]) {
165 InputVector(t, mi, mval, vec);
166 project_v3_v3v3(vec, vec, t->viewinv[1]);
168 output[0] = dot_v3v3(t->viewinv[1], vec) * 2.0f;
171 void setCustomPoints(TransInfo *UNUSED(t), MouseInput *mi, short start[2], short end[2])
175 if (mi->data == NULL) {
176 mi->data = MEM_callocN(sizeof(short) * 4, "custom points");
187 static void InputCustomRatio(TransInfo *UNUSED(t), MouseInput *mi, short mval[2], float output[3])
191 short *data = mi->data;
195 dx = data[2] - data[0];
196 dy = data[3] - data[1];
198 length = (float)sqrtf(dx*dx + dy*dy);
201 /* deal with Shift key by adding motion / 10 to motion before shift press */
203 mdx = (mi->precision_mval[0] + (float)(mval[0] - mi->precision_mval[0]) / 10.0f) - data[2];
204 mdy = (mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f) - data[3];
206 distance = (length != 0.0f)? (mdx*dx + mdy*dy) / length: 0.0f;
210 mdx = mval[0] - data[2];
211 mdy = mval[1] - data[3];
213 distance = (length != 0.0f)? (mdx*dx + mdy*dy) / length: 0.0f;
216 output[0] = (length != 0.0f)? distance / length: 0.0f;
220 static void InputAngle(TransInfo *UNUSED(t), MouseInput *mi, short 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) doesnt become 9.999999e-01, which gives 0.02 for acos */
236 double deler = ((dx1*dx1+dy1*dy1)+(dx2*dx2+dy2*dy2)-(dx3*dx3+dy3*dy3))
237 / (2.0 * (A*B?A*B:1.0));
238 /* (A*B?A*B:1.0f) this takes care of potential divide by zero errors */
242 dphi = saacos((float)deler);
243 if( (dx1*dy2-dx2*dy1)>0.0 ) dphi= -dphi;
245 /* If the angle is zero, because of lack of precision close to the 1.0 value in acos
246 * approximate the angle with the opposite side of the normalized triangle
247 * This is a good approximation here since the smallest acos value seems to be around
248 * 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;
267 if(mi->precision) dphi = dphi/30.0f;
269 /* if no delta angle, don't update initial position */
272 mi->imval[0] = mval[0];
273 mi->imval[1] = mval[1];
281 void initMouseInput(TransInfo *UNUSED(t), MouseInput *mi, int center[2], short mval[2])
286 mi->center[0] = center[0];
287 mi->center[1] = center[1];
289 mi->imval[0] = mval[0];
290 mi->imval[1] = mval[1];
295 static void calcSpringFactor(MouseInput *mi)
297 mi->factor = (float)sqrt(
299 ((float)(mi->center[1] - mi->imval[1]))*((float)(mi->center[1] - mi->imval[1]))
301 ((float)(mi->center[0] - mi->imval[0]))*((float)(mi->center[0] - mi->imval[0]))
304 if (mi->factor==0.0f)
305 mi->factor= 1.0f; /* prevent Inf */
308 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
314 mi->apply = InputVector;
315 t->helpline = HLP_NONE;
318 calcSpringFactor(mi);
319 mi->apply = InputSpring;
320 t->helpline = HLP_SPRING;
322 case INPUT_SPRING_FLIP:
323 calcSpringFactor(mi);
324 mi->apply = InputSpringFlip;
325 t->helpline = HLP_SPRING;
328 mi->data = MEM_callocN(sizeof(double), "angle accumulator");
329 mi->apply = InputAngle;
330 t->helpline = HLP_ANGLE;
332 case INPUT_TRACKBALL:
333 /* factor has to become setting or so */
335 mi->apply = InputTrackBall;
336 t->helpline = HLP_TRACKBALL;
338 case INPUT_HORIZONTAL_RATIO:
339 mi->factor = (float)(mi->center[0] - mi->imval[0]);
340 mi->apply = InputHorizontalRatio;
341 t->helpline = HLP_HARROW;
343 case INPUT_HORIZONTAL_ABSOLUTE:
344 mi->apply = InputHorizontalAbsolute;
345 t->helpline = HLP_HARROW;
347 case INPUT_VERTICAL_RATIO:
348 mi->apply = InputVerticalRatio;
349 t->helpline = HLP_VARROW;
351 case INPUT_VERTICAL_ABSOLUTE:
352 mi->apply = InputVerticalAbsolute;
353 t->helpline = HLP_VARROW;
355 case INPUT_CUSTOM_RATIO:
356 mi->apply = InputCustomRatio;
357 t->helpline = HLP_NONE;
365 /* bootstrap mouse input with initial values */
366 applyMouseInput(t, mi, mi->imval, t->values);
369 void setInputPostFct(MouseInput *mi, void (*post)(struct TransInfo *, float [3]))
374 void applyMouseInput(TransInfo *t, MouseInput *mi, short mval[2], float output[3])
376 if (mi->apply != NULL)
378 mi->apply(t, mi, mval, output);
387 int handleMouseInput(TransInfo *t, MouseInput *mi, wmEvent *event)
389 int redraw = TREDRAW_NOTHING;
395 if (event->val==KM_PRESS)
397 t->modifiers |= MOD_PRECISION;
398 /* shift is modifier for higher precision transform
399 * store the mouse position where the normal movement ended */
400 mi->precision_mval[0] = event->x - t->ar->winrct.xmin;
401 mi->precision_mval[1] = event->y - t->ar->winrct.ymin;
406 t->modifiers &= ~MOD_PRECISION;
409 redraw = TREDRAW_HARD;