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 = (float)sqrt( dx*dx + dy*dy);
69 dx= (float)(mi->center[0] - mval[0]);
70 dy= (float)(mi->center[1] - mval[1]);
71 precise_ratio = (float)sqrt( 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 = (float)sqrt( 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, int start[2], int end[2])
171 if (mi->data == NULL) {
172 mi->data = MEM_callocN(sizeof(int) * 4, "custom points");
183 static void InputCustomRatio(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
188 int *data = mi->data;
191 dx = data[2] - data[0];
192 dy = data[3] - data[1];
194 length = sqrt(dx*dx + dy*dy);
197 /* deal with Shift key by adding motion / 10 to motion before shift press */
199 mdx = (mi->precision_mval[0] + (float)(mval[0] - mi->precision_mval[0]) / 10.0f) - data[2];
200 mdy = (mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f) - data[3];
202 distance = (length != 0.0f)? (mdx*dx + mdy*dy) / length: 0.0f;
206 mdx = mval[0] - data[2];
207 mdy = mval[1] - data[3];
209 distance = (length != 0.0f)? (mdx*dx + mdy*dy) / length: 0.0f;
212 output[0] = (float)((length != 0.0f)? distance / length: 0.0f);
216 static void InputAngle(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
218 double dx2 = mval[0] - mi->center[0];
219 double dy2 = mval[1] - mi->center[1];
220 double B = sqrt(dx2*dx2+dy2*dy2);
222 double dx1 = mi->imval[0] - mi->center[0];
223 double dy1 = mi->imval[1] - mi->center[1];
224 double A = sqrt(dx1*dx1+dy1*dy1);
226 double dx3 = mval[0] - mi->imval[0];
227 double dy3 = mval[1] - mi->imval[1];
229 double *angle = mi->data;
231 /* use doubles here, to make sure a "1.0" (no rotation) doesnt become 9.999999e-01, which gives 0.02 for acos */
232 double deler = ((dx1*dx1+dy1*dy1)+(dx2*dx2+dy2*dy2)-(dx3*dx3+dy3*dy3))
233 / (2.0 * ((A*B)?(A*B):1.0));
234 /* ((A*B)?(A*B):1.0) this takes care of potential divide by zero errors */
238 dphi = saacos((float)deler);
239 if ( (dx1*dy2-dx2*dy1)>0.0 ) dphi= -dphi;
241 /* If the angle is zero, because of lack of precision close to the 1.0 value in acos
242 * approximate the angle with the opposite side of the normalized triangle
243 * This is a good approximation here since the smallest acos value seems to be around
244 * 0.02 degree and lower values don't even have a 0.01% error compared to the approximation
258 dphi = sqrt(dx*dx + dy*dy);
259 if ( (dx1*dy2-dx2*dy1)>0.0 ) dphi= -dphi;
266 /* if no delta angle, don't update initial position */
268 mi->imval[0] = mval[0];
269 mi->imval[1] = mval[1];
272 *angle += (double)dphi;
277 void initMouseInput(TransInfo *UNUSED(t), MouseInput *mi, int center[2], int mval[2])
282 mi->center[0] = center[0];
283 mi->center[1] = center[1];
285 mi->imval[0] = mval[0];
286 mi->imval[1] = mval[1];
291 static void calcSpringFactor(MouseInput *mi)
293 mi->factor = (float)sqrt(
295 ((float)(mi->center[1] - mi->imval[1]))*((float)(mi->center[1] - mi->imval[1]))
297 ((float)(mi->center[0] - mi->imval[0]))*((float)(mi->center[0] - mi->imval[0]))
300 if (mi->factor==0.0f)
301 mi->factor= 1.0f; /* prevent Inf */
304 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
306 /* may have been allocated previously */
307 /* TODO, holding R-key can cause mem leak, but this causes [#28903]
308 * disable for now. */
318 mi->apply = InputVector;
319 t->helpline = HLP_NONE;
322 calcSpringFactor(mi);
323 mi->apply = InputSpring;
324 t->helpline = HLP_SPRING;
326 case INPUT_SPRING_FLIP:
327 calcSpringFactor(mi);
328 mi->apply = InputSpringFlip;
329 t->helpline = HLP_SPRING;
332 mi->data = MEM_callocN(sizeof(double), "angle accumulator");
333 mi->apply = InputAngle;
334 t->helpline = HLP_ANGLE;
336 case INPUT_TRACKBALL:
337 /* factor has to become setting or so */
339 mi->apply = InputTrackBall;
340 t->helpline = HLP_TRACKBALL;
342 case INPUT_HORIZONTAL_RATIO:
343 mi->factor = (float)(mi->center[0] - mi->imval[0]);
344 mi->apply = InputHorizontalRatio;
345 t->helpline = HLP_HARROW;
347 case INPUT_HORIZONTAL_ABSOLUTE:
348 mi->apply = InputHorizontalAbsolute;
349 t->helpline = HLP_HARROW;
351 case INPUT_VERTICAL_RATIO:
352 mi->apply = InputVerticalRatio;
353 t->helpline = HLP_VARROW;
355 case INPUT_VERTICAL_ABSOLUTE:
356 mi->apply = InputVerticalAbsolute;
357 t->helpline = HLP_VARROW;
359 case INPUT_CUSTOM_RATIO:
360 mi->apply = InputCustomRatio;
361 t->helpline = HLP_NONE;
369 /* bootstrap mouse input with initial values */
370 applyMouseInput(t, mi, mi->imval, t->values);
373 void setInputPostFct(MouseInput *mi, void (*post)(struct TransInfo *, float [3]))
378 void applyMouseInput(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
380 if (mi->apply != NULL) {
381 mi->apply(t, mi, mval, output);
389 int handleMouseInput(TransInfo *t, MouseInput *mi, wmEvent *event)
391 int redraw = TREDRAW_NOTHING;
393 switch (event->type) {
396 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 copy_v2_v2_int(mi->precision_mval, event->mval);
404 t->modifiers &= ~MOD_PRECISION;
407 redraw = TREDRAW_HARD;