Blender V2.61 - r43446

transform_input.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): none yet.
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00028 #include <stdlib.h>
00029 #include <math.h>
00030 
00031 #include "DNA_screen_types.h"
00032 
00033 #include "BLI_math.h"
00034 #include "BLI_utildefines.h"
00035 
00036 #include "WM_types.h"
00037 
00038 #include "transform.h"
00039 
00040 #include "MEM_guardedalloc.h" 
00041 
00042 /* ************************** INPUT FROM MOUSE *************************** */
00043 
00044 static void InputVector(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
00045 {
00046     float vec[3], dvec[3];
00047     if(mi->precision)
00048     {
00049         /* calculate the main translation and the precise one separate */
00050         convertViewVec(t, dvec, (mval[0] - mi->precision_mval[0]), (mval[1] - mi->precision_mval[1]));
00051         mul_v3_fl(dvec, 0.1f);
00052         convertViewVec(t, vec, (mi->precision_mval[0] - t->imval[0]), (mi->precision_mval[1] - t->imval[1]));
00053         add_v3_v3v3(output, vec, dvec);
00054     }
00055     else
00056     {
00057         convertViewVec(t, output, (mval[0] - t->imval[0]), (mval[1] - t->imval[1]));
00058     }
00059 
00060 }
00061 
00062 static void InputSpring(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
00063 {
00064     float ratio, precise_ratio, dx, dy;
00065     if(mi->precision)
00066     {
00067         /* calculate ratio for shiftkey pos, and for total, and blend these for precision */
00068         dx = (float)(mi->center[0] - mi->precision_mval[0]);
00069         dy = (float)(mi->center[1] - mi->precision_mval[1]);
00070         ratio = (float)sqrt( dx*dx + dy*dy);
00071 
00072         dx= (float)(mi->center[0] - mval[0]);
00073         dy= (float)(mi->center[1] - mval[1]);
00074         precise_ratio = (float)sqrt( dx*dx + dy*dy);
00075 
00076         ratio = (ratio + (precise_ratio - ratio) / 10.0f) / mi->factor;
00077     }
00078     else
00079     {
00080         dx = (float)(mi->center[0] - mval[0]);
00081         dy = (float)(mi->center[1] - mval[1]);
00082         ratio = (float)sqrt( dx*dx + dy*dy) / mi->factor;
00083     }
00084 
00085     output[0] = ratio;
00086 }
00087 
00088 static void InputSpringFlip(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
00089 {
00090     InputSpring(t, mi, mval, output);
00091 
00092     /* flip scale */
00093     /* values can become really big when zoomed in so use longs [#26598] */
00094     if  ((long long int)(mi->center[0] - mval[0]) * (long long int)(mi->center[0] - mi->imval[0]) +
00095          (long long int)(mi->center[1] - mval[1]) * (long long int)(mi->center[1] - mi->imval[1]) < 0)
00096      {
00097         output[0] *= -1.0f;
00098      }
00099 }
00100 
00101 static void InputTrackBall(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
00102 {
00103 
00104     if(mi->precision)
00105     {
00106         output[0] = ( mi->imval[1] - mi->precision_mval[1] ) + ( mi->precision_mval[1] - mval[1] ) * 0.1f;
00107         output[1] = ( mi->precision_mval[0] - mi->imval[0] ) + ( mval[0] - mi->precision_mval[0] ) * 0.1f;
00108     }
00109     else
00110     {
00111         output[0] = (float)( mi->imval[1] - mval[1] );
00112         output[1] = (float)( mval[0] - mi->imval[0] );
00113     }
00114 
00115     output[0] *= mi->factor;
00116     output[1] *= mi->factor;
00117 }
00118 
00119 static void InputHorizontalRatio(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
00120 {
00121     float x, pad;
00122 
00123     pad = t->ar->winx / 10;
00124 
00125     if (mi->precision)
00126     {
00127         /* deal with Shift key by adding motion / 10 to motion before shift press */
00128         x = mi->precision_mval[0] + (float)(mval[0] - mi->precision_mval[0]) / 10.0f;
00129     }
00130     else {
00131         x = mval[0];
00132     }
00133 
00134     output[0] = (x - pad) / (t->ar->winx - 2 * pad);
00135 }
00136 
00137 static void InputHorizontalAbsolute(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
00138 {
00139     float vec[3];
00140 
00141     InputVector(t, mi, mval, vec);
00142     project_v3_v3v3(vec, vec, t->viewinv[0]);
00143 
00144     output[0] = dot_v3v3(t->viewinv[0], vec) * 2.0f;
00145 }
00146 
00147 static void InputVerticalRatio(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
00148 {
00149     float y, pad;
00150 
00151     pad = t->ar->winy / 10;
00152 
00153     if (mi->precision) {
00154         /* deal with Shift key by adding motion / 10 to motion before shift press */
00155         y = mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f;
00156     }
00157     else {
00158         y = mval[0];
00159     }
00160 
00161     output[0] = (y - pad) / (t->ar->winy - 2 * pad);
00162 }
00163 
00164 static void InputVerticalAbsolute(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
00165 {
00166     float vec[3];
00167 
00168     InputVector(t, mi, mval, vec);
00169     project_v3_v3v3(vec, vec, t->viewinv[1]);
00170 
00171     output[0] = dot_v3v3(t->viewinv[1], vec) * 2.0f;
00172 }
00173 
00174 void setCustomPoints(TransInfo *UNUSED(t), MouseInput *mi, int start[2], int end[2])
00175 {
00176     int *data;
00177 
00178     if (mi->data == NULL) {
00179         mi->data = MEM_callocN(sizeof(int) * 4, "custom points");
00180     }
00181     
00182     data = mi->data;
00183 
00184     data[0] = start[0];
00185     data[1] = start[1];
00186     data[2] = end[0];
00187     data[3] = end[1];
00188 }
00189 
00190 static void InputCustomRatio(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
00191 {
00192     double length;
00193     double distance;
00194     double dx, dy;
00195     int *data = mi->data;
00196     
00197     if (data) {
00198         dx = data[2] - data[0];
00199         dy = data[3] - data[1];
00200         
00201         length = sqrt(dx*dx + dy*dy);
00202         
00203         if (mi->precision) {
00204             /* deal with Shift key by adding motion / 10 to motion before shift press */
00205             int mdx, mdy;
00206             mdx = (mi->precision_mval[0] + (float)(mval[0] - mi->precision_mval[0]) / 10.0f) - data[2];
00207             mdy = (mi->precision_mval[1] + (float)(mval[1] - mi->precision_mval[1]) / 10.0f) - data[3];
00208 
00209             distance = (length != 0.0f)? (mdx*dx + mdy*dy) / length: 0.0f;
00210         }
00211         else {
00212             int mdx, mdy;
00213             mdx = mval[0] - data[2];
00214             mdy = mval[1] - data[3];
00215 
00216             distance = (length != 0.0f)? (mdx*dx + mdy*dy) / length: 0.0f;
00217         }
00218 
00219         output[0] = (float)((length != 0.0f)? distance / length: 0.0f);
00220     }
00221 }
00222 
00223 static void InputAngle(TransInfo *UNUSED(t), MouseInput *mi, const int mval[2], float output[3])
00224 {
00225     double dx2 = mval[0] - mi->center[0];
00226     double dy2 = mval[1] - mi->center[1];
00227     double B = sqrt(dx2*dx2+dy2*dy2);
00228 
00229     double dx1 = mi->imval[0] - mi->center[0];
00230     double dy1 = mi->imval[1] - mi->center[1];
00231     double A = sqrt(dx1*dx1+dy1*dy1);
00232 
00233     double dx3 = mval[0] - mi->imval[0];
00234     double dy3 = mval[1] - mi->imval[1];
00235 
00236     double *angle = mi->data;
00237 
00238     /* use doubles here, to make sure a "1.0" (no rotation) doesnt become 9.999999e-01, which gives 0.02 for acos */
00239     double deler = ((dx1*dx1+dy1*dy1)+(dx2*dx2+dy2*dy2)-(dx3*dx3+dy3*dy3))
00240         / (2.0 * ((A*B)?(A*B):1.0));
00241     /* ((A*B)?(A*B):1.0) this takes care of potential divide by zero errors */
00242 
00243     float dphi;
00244 
00245     dphi = saacos((float)deler);
00246     if( (dx1*dy2-dx2*dy1)>0.0 ) dphi= -dphi;
00247 
00248     /* If the angle is zero, because of lack of precision close to the 1.0 value in acos
00249      * approximate the angle with the opposite side of the normalized triangle
00250      * This is a good approximation here since the smallest acos value seems to be around
00251      * 0.02 degree and lower values don't even have a 0.01% error compared to the approximation
00252      * */
00253     if (dphi == 0)
00254     {
00255         double dx, dy;
00256 
00257         dx2 /= A;
00258         dy2 /= A;
00259 
00260         dx1 /= B;
00261         dy1 /= B;
00262 
00263         dx = dx1 - dx2;
00264         dy = dy1 - dy2;
00265 
00266         dphi = sqrt(dx*dx + dy*dy);
00267         if( (dx1*dy2-dx2*dy1)>0.0 ) dphi= -dphi;
00268     }
00269 
00270     if(mi->precision) dphi = dphi/30.0f;
00271 
00272     /* if no delta angle, don't update initial position */
00273     if (dphi != 0)
00274     {
00275         mi->imval[0] = mval[0];
00276         mi->imval[1] = mval[1];
00277     }
00278 
00279     *angle += (double)dphi;
00280 
00281     output[0] = *angle;
00282 }
00283 
00284 void initMouseInput(TransInfo *UNUSED(t), MouseInput *mi, int center[2], int mval[2])
00285 {
00286     mi->factor = 0;
00287     mi->precision = 0;
00288 
00289     mi->center[0] = center[0];
00290     mi->center[1] = center[1];
00291 
00292     mi->imval[0] = mval[0];
00293     mi->imval[1] = mval[1];
00294 
00295     mi->post = NULL;
00296 }
00297 
00298 static void calcSpringFactor(MouseInput *mi)
00299 {
00300     mi->factor = (float)sqrt(
00301         (
00302             ((float)(mi->center[1] - mi->imval[1]))*((float)(mi->center[1] - mi->imval[1]))
00303         +
00304             ((float)(mi->center[0] - mi->imval[0]))*((float)(mi->center[0] - mi->imval[0]))
00305         ) );
00306 
00307     if (mi->factor==0.0f)
00308         mi->factor= 1.0f; /* prevent Inf */
00309 }
00310 
00311 void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
00312 {
00313     /* may have been allocated previously */
00314     /* TODO, holding R-key can cause mem leak, but this causes [#28903]
00315      * disable for now. */
00316 #if 0
00317     if(mi->data) {
00318         MEM_freeN(mi->data);
00319         mi->data= NULL;
00320     }
00321 #endif
00322 
00323     switch(mode)
00324     {
00325     case INPUT_VECTOR:
00326         mi->apply = InputVector;
00327         t->helpline = HLP_NONE;
00328         break;
00329     case INPUT_SPRING:
00330         calcSpringFactor(mi);
00331         mi->apply = InputSpring;
00332         t->helpline = HLP_SPRING;
00333         break;
00334     case INPUT_SPRING_FLIP:
00335         calcSpringFactor(mi);
00336         mi->apply = InputSpringFlip;
00337         t->helpline = HLP_SPRING;
00338         break;
00339     case INPUT_ANGLE:
00340         mi->data = MEM_callocN(sizeof(double), "angle accumulator");
00341         mi->apply = InputAngle;
00342         t->helpline = HLP_ANGLE;
00343         break;
00344     case INPUT_TRACKBALL:
00345         /* factor has to become setting or so */
00346         mi->factor = 0.01f;
00347         mi->apply = InputTrackBall;
00348         t->helpline = HLP_TRACKBALL;
00349         break;
00350     case INPUT_HORIZONTAL_RATIO:
00351         mi->factor = (float)(mi->center[0] - mi->imval[0]);
00352         mi->apply = InputHorizontalRatio;
00353         t->helpline = HLP_HARROW;
00354         break;
00355     case INPUT_HORIZONTAL_ABSOLUTE:
00356         mi->apply = InputHorizontalAbsolute;
00357         t->helpline = HLP_HARROW;
00358         break;
00359     case INPUT_VERTICAL_RATIO:
00360         mi->apply = InputVerticalRatio;
00361         t->helpline = HLP_VARROW;
00362         break;
00363     case INPUT_VERTICAL_ABSOLUTE:
00364         mi->apply = InputVerticalAbsolute;
00365         t->helpline = HLP_VARROW;
00366         break;
00367     case INPUT_CUSTOM_RATIO:
00368         mi->apply = InputCustomRatio;
00369         t->helpline = HLP_NONE;
00370         break;
00371     case INPUT_NONE:
00372     default:
00373         mi->apply = NULL;
00374         break;
00375     }
00376 
00377     /* bootstrap mouse input with initial values */
00378     applyMouseInput(t, mi, mi->imval, t->values);
00379 }
00380 
00381 void setInputPostFct(MouseInput *mi, void   (*post)(struct TransInfo *, float [3]))
00382 {
00383     mi->post = post;
00384 }
00385 
00386 void applyMouseInput(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
00387 {
00388     if (mi->apply != NULL)
00389     {
00390         mi->apply(t, mi, mval, output);
00391     }
00392 
00393     if (mi->post)
00394     {
00395         mi->post(t, output);
00396     }
00397 }
00398 
00399 int handleMouseInput(TransInfo *t, MouseInput *mi, wmEvent *event)
00400 {
00401     int redraw = TREDRAW_NOTHING;
00402 
00403     switch (event->type)
00404     {
00405     case LEFTSHIFTKEY:
00406     case RIGHTSHIFTKEY:
00407         if (event->val==KM_PRESS)
00408         {
00409             t->modifiers |= MOD_PRECISION;
00410             /* shift is modifier for higher precision transform
00411              * store the mouse position where the normal movement ended */
00412             copy_v2_v2_int(mi->precision_mval, event->mval);
00413             mi->precision = 1;
00414         }
00415         else
00416         {
00417             t->modifiers &= ~MOD_PRECISION;
00418             mi->precision = 0;
00419         }
00420         redraw = TREDRAW_HARD;
00421         break;
00422     }
00423 
00424     return redraw;
00425 }