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