"Fix" [#30431] UI string spelling & similar fixes.
[blender.git] / source / blender / editors / space_view3d / view3d_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_edit.c
28  *  \ingroup spview3d
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include <float.h>
36
37 #include "DNA_armature_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_camera_types.h"
41 #include "DNA_lamp_types.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_math.h"
47 #include "BLI_rand.h"
48 #include "BLI_utildefines.h"
49
50 #include "BKE_camera.h"
51 #include "BKE_context.h"
52 #include "BKE_image.h"
53 #include "BKE_library.h"
54 #include "BKE_object.h"
55 #include "BKE_paint.h"
56 #include "BKE_report.h"
57 #include "BKE_scene.h"
58 #include "BKE_screen.h"
59 #include "BKE_depsgraph.h" /* for ED_view3d_camera_lock_sync */
60
61
62 #include "BIF_gl.h"
63 #include "BIF_glutil.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "RNA_access.h"
69 #include "RNA_define.h"
70
71 #include "ED_particle.h"
72 #include "ED_screen.h"
73 #include "ED_transform.h"
74 #include "ED_mesh.h"
75 #include "ED_view3d.h"
76
77
78 #include "PIL_time.h" /* smoothview */
79
80 #include "view3d_intern.h"      // own include
81
82 /* ********************** view3d_edit: view manipulations ********************* */
83
84 int ED_view3d_camera_lock_check(View3D *v3d, RegionView3D *rv3d)
85 {
86         return ((v3d->camera) &&
87                 (v3d->camera->id.lib == NULL) &&
88                 (v3d->flag2 & V3D_LOCK_CAMERA) &&
89                 (rv3d->persp==RV3D_CAMOB));
90 }
91
92 void ED_view3d_camera_lock_init(View3D *v3d, RegionView3D *rv3d)
93 {
94         if (ED_view3d_camera_lock_check(v3d, rv3d)) {
95                 ED_view3d_from_object(v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
96         }
97 }
98
99 /* return TRUE if the camera is moved */
100 int ED_view3d_camera_lock_sync(View3D *v3d, RegionView3D *rv3d)
101 {
102         if (ED_view3d_camera_lock_check(v3d, rv3d)) {
103                 ObjectTfmProtectedChannels obtfm;
104                 Object *root_parent;
105
106                 if ((U.uiflag & USER_CAM_LOCK_NO_PARENT)==0 && (root_parent= v3d->camera->parent)) {
107                         Object *ob_update;
108                         float view_mat[4][4];
109                         float diff_mat[4][4];
110                         float parent_mat[4][4];
111
112                         while (root_parent->parent) {
113                                 root_parent= root_parent->parent;
114                         }
115
116                         ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
117
118                         invert_m4_m4(v3d->camera->imat, v3d->camera->obmat);
119                         mult_m4_m4m4(diff_mat, view_mat, v3d->camera->imat);
120
121                         mult_m4_m4m4(parent_mat, diff_mat, root_parent->obmat);
122
123                         object_tfm_protected_backup(root_parent, &obtfm);
124                         object_apply_mat4(root_parent, parent_mat, TRUE, FALSE);
125                         object_tfm_protected_restore(root_parent, &obtfm, root_parent->protectflag);
126
127                         ob_update= v3d->camera;
128                         while (ob_update) {
129                                 DAG_id_tag_update(&ob_update->id, OB_RECALC_OB);
130                                 WM_main_add_notifier(NC_OBJECT|ND_TRANSFORM, ob_update);
131                                 ob_update= ob_update->parent;
132                         }
133                 }
134                 else {
135                         object_tfm_protected_backup(v3d->camera, &obtfm);
136                         ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
137                         object_tfm_protected_restore(v3d->camera, &obtfm, v3d->camera->protectflag);
138
139                         DAG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
140                         WM_main_add_notifier(NC_OBJECT|ND_TRANSFORM, v3d->camera);
141                 }
142
143                 return TRUE;
144         }
145         else {
146                 return FALSE;
147         }
148 }
149
150
151 /* ********************* box view support ***************** */
152
153 static void view3d_boxview_clip(ScrArea *sa)
154 {
155         ARegion *ar;
156         BoundBox *bb = MEM_callocN(sizeof(BoundBox), "clipbb");
157         float clip[6][4];
158         float x1= 0.0f, y1= 0.0f, z1= 0.0f, ofs[3] = {0.0f, 0.0f, 0.0f};
159         int val;
160
161         /* create bounding box */
162         for (ar= sa->regionbase.first; ar; ar= ar->next) {
163                 if (ar->regiontype==RGN_TYPE_WINDOW) {
164                         RegionView3D *rv3d= ar->regiondata;
165
166                         if (rv3d->viewlock & RV3D_BOXCLIP) {
167                                 if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
168                                         if (ar->winx>ar->winy) x1= rv3d->dist;
169                                         else x1= ar->winx*rv3d->dist/ar->winy;
170
171                                         if (ar->winx>ar->winy) y1= ar->winy*rv3d->dist/ar->winx;
172                                         else y1= rv3d->dist;
173                                         copy_v2_v2(ofs, rv3d->ofs);
174                                 }
175                                 else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
176                                         ofs[2]= rv3d->ofs[2];
177
178                                         if (ar->winx>ar->winy) z1= ar->winy*rv3d->dist/ar->winx;
179                                         else z1= rv3d->dist;
180                                 }
181                         }
182                 }
183         }
184
185         for (val=0; val<8; val++) {
186                 if (ELEM4(val, 0, 3, 4, 7))
187                         bb->vec[val][0]= -x1 - ofs[0];
188                 else
189                         bb->vec[val][0]=  x1 - ofs[0];
190
191                 if (ELEM4(val, 0, 1, 4, 5))
192                         bb->vec[val][1]= -y1 - ofs[1];
193                 else
194                         bb->vec[val][1]=  y1 - ofs[1];
195
196                 if (val > 3)
197                         bb->vec[val][2]= -z1 - ofs[2];
198                 else
199                         bb->vec[val][2]=  z1 - ofs[2];
200         }
201
202         /* normals for plane equations */
203         normal_tri_v3( clip[0],bb->vec[0], bb->vec[1], bb->vec[4]);
204         normal_tri_v3( clip[1],bb->vec[1], bb->vec[2], bb->vec[5]);
205         normal_tri_v3( clip[2],bb->vec[2], bb->vec[3], bb->vec[6]);
206         normal_tri_v3( clip[3],bb->vec[3], bb->vec[0], bb->vec[7]);
207         normal_tri_v3( clip[4],bb->vec[4], bb->vec[5], bb->vec[6]);
208         normal_tri_v3( clip[5],bb->vec[0], bb->vec[2], bb->vec[1]);
209
210         /* then plane equations */
211         for (val=0; val<6; val++) {
212                 clip[val][3] = -dot_v3v3(clip[val], bb->vec[val % 5]);
213         }
214
215         /* create bounding box */
216         for (ar= sa->regionbase.first; ar; ar= ar->next) {
217                 if (ar->regiontype==RGN_TYPE_WINDOW) {
218                         RegionView3D *rv3d= ar->regiondata;
219
220                         if (rv3d->viewlock & RV3D_BOXCLIP) {
221                                 rv3d->rflag |= RV3D_CLIPPING;
222                                 memcpy(rv3d->clip, clip, sizeof(clip));
223                                 if (rv3d->clipbb) MEM_freeN(rv3d->clipbb);
224                                 rv3d->clipbb= MEM_dupallocN(bb);
225                         }
226                 }
227         }
228         MEM_freeN(bb);
229 }
230
231 /* sync center/zoom view of region to others, for view transforms */
232 static void view3d_boxview_sync(ScrArea *sa, ARegion *ar)
233 {
234         ARegion *artest;
235         RegionView3D *rv3d= ar->regiondata;
236         short clip= 0;
237
238         for (artest= sa->regionbase.first; artest; artest= artest->next) {
239                 if (artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
240                         RegionView3D *rv3dtest= artest->regiondata;
241
242                         if (rv3dtest->viewlock) {
243                                 rv3dtest->dist= rv3d->dist;
244
245                                 if ( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM) ) {
246                                         if ( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
247                                                 rv3dtest->ofs[0]= rv3d->ofs[0];
248                                         else if ( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
249                                                 rv3dtest->ofs[1]= rv3d->ofs[1];
250                                 }
251                                 else if ( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK) ) {
252                                         if ( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
253                                                 rv3dtest->ofs[0]= rv3d->ofs[0];
254                                         else if ( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
255                                                 rv3dtest->ofs[2]= rv3d->ofs[2];
256                                 }
257                                 else if ( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT) ) {
258                                         if ( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
259                                                 rv3dtest->ofs[1]= rv3d->ofs[1];
260                                         if ( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
261                                                 rv3dtest->ofs[2]= rv3d->ofs[2];
262                                 }
263
264                                 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
265
266                                 ED_region_tag_redraw(artest);
267                         }
268                 }
269         }
270
271         if (clip) {
272                 view3d_boxview_clip(sa);
273         }
274 }
275
276 /* for home, center etc */
277 void view3d_boxview_copy(ScrArea *sa, ARegion *ar)
278 {
279         ARegion *artest;
280         RegionView3D *rv3d= ar->regiondata;
281         short clip= 0;
282
283         for (artest= sa->regionbase.first; artest; artest= artest->next) {
284                 if (artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
285                         RegionView3D *rv3dtest= artest->regiondata;
286
287                         if (rv3dtest->viewlock) {
288                                 rv3dtest->dist= rv3d->dist;
289                                 copy_v3_v3(rv3dtest->ofs, rv3d->ofs);
290                                 ED_region_tag_redraw(artest);
291
292                                 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
293                         }
294                 }
295         }
296
297         if (clip) {
298                 view3d_boxview_clip(sa);
299         }
300 }
301
302 /* 'clip' is used to know if our clip setting has changed */
303 void ED_view3d_quadview_update(ScrArea *sa, ARegion *ar, short do_clip)
304 {
305         ARegion *arsync= NULL;
306         RegionView3D *rv3d= ar->regiondata;
307         short viewlock;
308         /* this function copies flags from the first of the 3 other quadview
309            regions to the 2 other, so it assumes this is the region whose
310            properties are always being edited, weak */
311         viewlock= rv3d->viewlock;
312
313         if ((viewlock & RV3D_LOCKED)==0)
314                 viewlock= 0;
315         else if ((viewlock & RV3D_BOXVIEW)==0) {
316                 viewlock &= ~RV3D_BOXCLIP;
317                 do_clip= TRUE;
318         }
319
320         for (; ar; ar= ar->prev) {
321                 if (ar->alignment==RGN_ALIGN_QSPLIT) {
322                         rv3d= ar->regiondata;
323                         rv3d->viewlock= viewlock;
324
325                         if (do_clip && (viewlock & RV3D_BOXCLIP)==0) {
326                                 rv3d->rflag &= ~RV3D_BOXCLIP;
327                         }
328
329                         /* use arsync so we sync with one of the aligned views below
330                          * else the view jumps on changing view settings like 'clip'
331                          * since it copies from the perspective view */
332                         arsync= ar;
333                 }
334         }
335
336         if (rv3d->viewlock & RV3D_BOXVIEW) {
337                 view3d_boxview_copy(sa, arsync ? arsync : sa->regionbase.last);
338         }
339
340         ED_area_tag_redraw(sa);
341 }
342
343 /* ************************** init for view ops **********************************/
344
345 typedef struct ViewOpsData {
346         ScrArea *sa;
347         ARegion *ar;
348         View3D *v3d;
349         RegionView3D *rv3d;
350
351         /* needed for continuous zoom */
352         wmTimer *timer;
353         double timer_lastdraw;
354
355         float oldquat[4];
356         float viewquat[4]; /* working copy of rv3d->viewquat */
357         float trackvec[3];
358         float mousevec[3]; /* dolly only */
359         float reverse, dist0;
360         float grid, far;
361         short axis_snap; /* view rotate only */
362
363         /* use for orbit selection and auto-dist */
364         float ofs[3], dyn_ofs[3];
365         short use_dyn_ofs;
366
367         int origx, origy, oldx, oldy;
368         int origkey; /* the key that triggered the operator */
369
370 } ViewOpsData;
371
372 #define TRACKBALLSIZE  (1.1)
373
374 static void calctrackballvec(rcti *rect, int mx, int my, float vec[3])
375 {
376         float x, y, radius, d, z, t;
377
378         radius= TRACKBALLSIZE;
379
380         /* normalize x and y */
381         x= (rect->xmax + rect->xmin)/2 - mx;
382         x/= (float)((rect->xmax - rect->xmin)/4);
383         y= (rect->ymax + rect->ymin)/2 - my;
384         y/= (float)((rect->ymax - rect->ymin)/2);
385
386         d = sqrt(x*x + y*y);
387         if (d < radius * (float)M_SQRT1_2) { /* Inside sphere */
388                 z= sqrt(radius*radius - d*d);
389         }
390         else { /* On hyperbola */
391                 t= radius / (float)M_SQRT2;
392                 z= t*t / d;
393         }
394
395         vec[0]= x;
396         vec[1]= y;
397         vec[2]= -z;             /* yah yah! */
398 }
399
400
401 static void viewops_data_create(bContext *C, wmOperator *op, wmEvent *event)
402 {
403         static float lastofs[3] = {0,0,0};
404         RegionView3D *rv3d;
405         ViewOpsData *vod= MEM_callocN(sizeof(ViewOpsData), "viewops data");
406
407         /* store data */
408         op->customdata= vod;
409         vod->sa= CTX_wm_area(C);
410         vod->ar= CTX_wm_region(C);
411         vod->v3d= vod->sa->spacedata.first;
412         vod->rv3d= rv3d= vod->ar->regiondata;
413
414         /* set the view from the camera, if view locking is enabled.
415          * we may want to make this optional but for now its needed always */
416         ED_view3d_camera_lock_init(vod->v3d, vod->rv3d);
417
418         vod->dist0= rv3d->dist;
419         copy_qt_qt(vod->viewquat, rv3d->viewquat);
420         copy_qt_qt(vod->oldquat, rv3d->viewquat);
421         vod->origx= vod->oldx= event->x;
422         vod->origy= vod->oldy= event->y;
423         vod->origkey= event->type; /* the key that triggered the operator.  */
424         vod->use_dyn_ofs= (U.uiflag & USER_ORBIT_SELECTION) ? 1:0;
425         copy_v3_v3(vod->ofs, rv3d->ofs);
426
427         if (vod->use_dyn_ofs) {
428                 /* If there's no selection, lastofs is unmodified and last value since static */
429                 calculateTransformCenter(C, V3D_CENTROID, lastofs);
430                 negate_v3_v3(vod->dyn_ofs, lastofs);
431         }
432         else if (U.uiflag & USER_ORBIT_ZBUF) {
433
434                 view3d_operator_needs_opengl(C); /* needed for zbuf drawing */
435
436                 if ((vod->use_dyn_ofs=ED_view3d_autodist(CTX_data_scene(C), vod->ar, vod->v3d, event->mval, vod->dyn_ofs))) {
437                         if (rv3d->is_persp) {
438                                 float my_origin[3]; /* original G.vd->ofs */
439                                 float my_pivot[3]; /* view */
440                                 float dvec[3];
441
442                                 // locals for dist correction
443                                 float mat[3][3];
444                                 float upvec[3];
445
446                                 negate_v3_v3(my_origin, rv3d->ofs);                             /* ofs is flipped */
447
448                                 /* Set the dist value to be the distance from this 3d point */
449                                 /* this means youll always be able to zoom into it and panning wont go bad when dist was zero */
450
451                                 /* remove dist value */
452                                 upvec[0] = upvec[1] = 0;
453                                 upvec[2] = rv3d->dist;
454                                 copy_m3_m4(mat, rv3d->viewinv);
455
456                                 mul_m3_v3(mat, upvec);
457                                 sub_v3_v3v3(my_pivot, rv3d->ofs, upvec);
458                                 negate_v3(my_pivot);                            /* ofs is flipped */
459
460                                 /* find a new ofs value that is along the view axis (rather than the mouse location) */
461                                 closest_to_line_v3(dvec, vod->dyn_ofs, my_pivot, my_origin);
462                                 vod->dist0 = rv3d->dist = len_v3v3(my_pivot, dvec);
463
464                                 negate_v3_v3(rv3d->ofs, dvec);
465                         }
466                         negate_v3(vod->dyn_ofs);
467                         copy_v3_v3(vod->ofs, rv3d->ofs);
468                 }
469         }
470
471         {
472                 /* for dolly */
473                 float mval_f[2];
474                 VECCOPY2D(mval_f, event->mval);
475                 ED_view3d_win_to_vector(vod->ar, mval_f, vod->mousevec);
476         }
477
478         /* lookup, we dont pass on v3d to prevent confusement */
479         vod->grid= vod->v3d->grid;
480         vod->far= vod->v3d->far;
481
482         calctrackballvec(&vod->ar->winrct, event->x, event->y, vod->trackvec);
483
484         initgrabz(rv3d, -rv3d->ofs[0], -rv3d->ofs[1], -rv3d->ofs[2]);
485
486         vod->reverse= 1.0f;
487         if (rv3d->persmat[2][1] < 0.0f)
488                 vod->reverse= -1.0f;
489
490         rv3d->rflag |= RV3D_NAVIGATING;
491 }
492
493 static void viewops_data_free(bContext *C, wmOperator *op)
494 {
495         ARegion *ar;
496         Paint *p = paint_get_active(CTX_data_scene(C));
497
498         if (op->customdata) {
499                 ViewOpsData *vod= op->customdata;
500                 ar= vod->ar;
501                 vod->rv3d->rflag &= ~RV3D_NAVIGATING;
502
503                 if (vod->timer)
504                         WM_event_remove_timer(CTX_wm_manager(C), vod->timer->win, vod->timer);
505
506                 MEM_freeN(vod);
507                 op->customdata= NULL;
508         }
509         else {
510                 ar= CTX_wm_region(C);
511         }
512
513         if (p && (p->flags & PAINT_FAST_NAVIGATE))
514                 ED_region_tag_redraw(ar);
515 }
516
517 /* ************************** viewrotate **********************************/
518
519 static const float thres = 0.93f; //cos(20 deg);
520
521 #define COS45 0.7071068
522 #define SIN45 COS45
523
524 #define NUM_SNAP_QUATS 39
525
526 static const float snapquats[NUM_SNAP_QUATS][5] = {
527         /*{q0, q1, q3, q4, view}*/
528         {COS45, -SIN45, 0.0,    0.0,   RV3D_VIEW_FRONT},
529         {0.0,    0.0,  -SIN45, -SIN45, RV3D_VIEW_BACK},
530         {1.0,    0.0,   0.0,    0.0,   RV3D_VIEW_TOP},
531         {0.0,   -1.0,   0.0,    0.0,   RV3D_VIEW_BOTTOM},
532         {0.5,   -0.5,  -0.5,   -0.5,   RV3D_VIEW_RIGHT},
533         {0.5,   -0.5,   0.5,    0.5,   RV3D_VIEW_LEFT},
534
535         /* some more 45 deg snaps */
536         { 0.6532815, -0.6532815, 0.2705981, 0.2705981, 0},
537         { 0.9238795,  0.0,       0.0,       0.3826834, 0},
538         { 0.0,       -0.9238795, 0.3826834, 0.0,       0},
539         { 0.3535534, -0.8535534, 0.3535534, 0.1464466, 0},
540         { 0.8535534, -0.3535534, 0.1464466, 0.3535534, 0},
541         { 0.4999999, -0.4999999, 0.5,       0.5,       0},
542         { 0.2705980, -0.6532815, 0.6532815, 0.2705980, 0},
543         { 0.6532815, -0.2705980, 0.2705980, 0.6532815, 0},
544         { 0.2705978, -0.2705980, 0.6532814, 0.6532814, 0},
545         { 0.3826834,  0.0,       0.0,       0.9238794, 0},
546         { 0.0,       -0.3826834, 0.9238794, 0.0,       0},
547         { 0.1464466, -0.3535534, 0.8535534, 0.3535534, 0},
548         { 0.3535534, -0.1464466, 0.3535534, 0.8535534, 0},
549         { 0.0,        0.0,       0.9238794, 0.3826834, 0},
550         {-0.0,        0.0,       0.3826834, 0.9238794, 0},
551         {-0.2705980,  0.2705980, 0.6532813, 0.6532813, 0},
552         {-0.3826834,  0.0,       0.0,       0.9238794, 0},
553         { 0.0,        0.3826834, 0.9238794, 0.0,       0},
554         {-0.1464466,  0.3535534, 0.8535533, 0.3535533, 0},
555         {-0.3535534,  0.1464466, 0.3535533, 0.8535533, 0},
556         {-0.4999999,  0.4999999, 0.4999999, 0.4999999, 0},
557         {-0.2705980,  0.6532815, 0.6532814, 0.2705980, 0},
558         {-0.6532815,  0.2705980, 0.2705980, 0.6532814, 0},
559         {-0.6532813,  0.6532813, 0.2705979, 0.2705979, 0},
560         {-0.9238793,  0.0,       0.0,       0.3826833, 0},
561         { 0.0,        0.9238793, 0.3826833, 0.0,       0},
562         {-0.3535533,  0.8535533, 0.3535533, 0.1464466, 0},
563         {-0.8535533,  0.3535533, 0.1464466, 0.3535533, 0},
564         {-0.3826833,  0.9238794, 0.0,       0.0,       0},
565         {-0.9238794,  0.3826833, 0.0,       0.0,       0},
566         {-COS45,      0.0,       0.0,       SIN45,     0},
567         { COS45,      0.0,       0.0,       SIN45,     0},
568         { 0.0,        0.0,       0.0,       1.0,       0}
569 };
570
571 enum {
572         VIEW_PASS= 0,
573         VIEW_APPLY,
574         VIEW_CONFIRM
575 };
576
577 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
578 #define VIEW_MODAL_CONFIRM                              1 /* used for all view operations */
579 #define VIEWROT_MODAL_AXIS_SNAP_ENABLE  2
580 #define VIEWROT_MODAL_AXIS_SNAP_DISABLE 3
581 #define VIEWROT_MODAL_SWITCH_ZOOM               4
582 #define VIEWROT_MODAL_SWITCH_MOVE               5
583 #define VIEWROT_MODAL_SWITCH_ROTATE             6
584
585 /* called in transform_ops.c, on each regeneration of keymaps  */
586 void viewrotate_modal_keymap(wmKeyConfig *keyconf)
587 {
588         static EnumPropertyItem modal_items[] = {
589         {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
590
591         {VIEWROT_MODAL_AXIS_SNAP_ENABLE,        "AXIS_SNAP_ENABLE", 0, "Enable Axis Snap", ""},
592         {VIEWROT_MODAL_AXIS_SNAP_DISABLE,       "AXIS_SNAP_DISABLE", 0, "Disable Axis Snap", ""},
593                 
594         {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
595         {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
596
597         {0, NULL, 0, NULL, NULL}};
598
599         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Rotate Modal");
600
601         /* this function is called for each spacetype, only needs to add map once */
602         if (keymap) return;
603
604         keymap= WM_modalkeymap_add(keyconf, "View3D Rotate Modal", modal_items);
605
606         /* items for modal map */
607         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
608         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
609
610         WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_ENABLE);
611         WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_DISABLE);
612
613         /* disabled mode switching for now, can re-implement better, later on
614         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
615         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
616         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
617         */
618         
619         /* assign map to operators */
620         WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");
621
622 }
623
624 static void viewrotate_apply(ViewOpsData *vod, int x, int y)
625 {
626         RegionView3D *rv3d= vod->rv3d;
627
628         rv3d->view= RV3D_VIEW_USER; /* need to reset everytime because of view snapping */
629
630         if (U.flag & USER_TRACKBALL) {
631                 float phi, si, q1[4], dvec[3], newvec[3];
632
633                 calctrackballvec(&vod->ar->winrct, x, y, newvec);
634
635                 sub_v3_v3v3(dvec, newvec, vod->trackvec);
636
637                 si = len_v3(dvec);
638                 si /= (float)(2.0 * TRACKBALLSIZE);
639
640                 cross_v3_v3v3(q1+1, vod->trackvec, newvec);
641                 normalize_v3(q1+1);
642
643                 /* Allow for rotation beyond the interval
644                         * [-pi, pi] */
645                 while (si > 1.0f)
646                         si -= 2.0f;
647
648                 /* This relation is used instead of
649                         * phi = asin(si) so that the angle
650                         * of rotation is linearly proportional
651                         * to the distance that the mouse is
652                         * dragged. */
653                 phi = si * (float)(M_PI / 2.0);
654
655                 q1[0]= cos(phi);
656                 mul_v3_fl(q1+1, sin(phi));
657                 mul_qt_qtqt(vod->viewquat, q1, vod->oldquat);
658
659                 if (vod->use_dyn_ofs) {
660                         /* compute the post multiplication quat, to rotate the offset correctly */
661                         copy_qt_qt(q1, vod->oldquat);
662                         conjugate_qt(q1);
663                         mul_qt_qtqt(q1, q1, vod->viewquat);
664
665                         conjugate_qt(q1); /* conj == inv for unit quat */
666                         copy_v3_v3(rv3d->ofs, vod->ofs);
667                         sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
668                         mul_qt_v3(q1, rv3d->ofs);
669                         add_v3_v3(rv3d->ofs, vod->dyn_ofs);
670                 }
671         }
672         else {
673                 /* New turntable view code by John Aughey */
674                 float phi, q1[4];
675                 float m[3][3];
676                 float m_inv[3][3];
677                 float xvec[3] = {1.0f, 0.0f, 0.0f};
678                 /* Sensitivity will control how fast the viewport rotates.  0.0035 was
679                         obtained experimentally by looking at viewport rotation sensitivities
680                         on other modeling programs. */
681                 /* Perhaps this should be a configurable user parameter. */
682                 const float sensitivity = 0.0035f;
683
684                 /* Get the 3x3 matrix and its inverse from the quaternion */
685                 quat_to_mat3( m,vod->viewquat);
686                 invert_m3_m3(m_inv,m);
687
688                 /* Determine the direction of the x vector (for rotating up and down) */
689                 /* This can likely be computed directly from the quaternion. */
690                 mul_m3_v3(m_inv,xvec);
691
692                 /* Perform the up/down rotation */
693                 phi = sensitivity * -(y - vod->oldy);
694                 q1[0] = cos(phi);
695                 mul_v3_v3fl(q1+1, xvec, sin(phi));
696                 mul_qt_qtqt(vod->viewquat, vod->viewquat, q1);
697
698                 if (vod->use_dyn_ofs) {
699                         conjugate_qt(q1); /* conj == inv for unit quat */
700                         sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
701                         mul_qt_v3(q1, rv3d->ofs);
702                         add_v3_v3(rv3d->ofs, vod->dyn_ofs);
703                 }
704
705                 /* Perform the orbital rotation */
706                 phi = sensitivity * vod->reverse * (x - vod->oldx);
707                 q1[0] = cos(phi);
708                 q1[1] = q1[2] = 0.0;
709                 q1[3] = sin(phi);
710                 mul_qt_qtqt(vod->viewquat, vod->viewquat, q1);
711
712                 if (vod->use_dyn_ofs) {
713                         conjugate_qt(q1);
714                         sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
715                         mul_qt_v3(q1, rv3d->ofs);
716                         add_v3_v3(rv3d->ofs, vod->dyn_ofs);
717                 }
718         }
719
720         /* check for view snap */
721         if (vod->axis_snap) {
722                 int i;
723                 float viewquat_inv[4];
724                 float zaxis[3]={0,0,1};
725                 invert_qt_qt(viewquat_inv, vod->viewquat);
726
727                 mul_qt_v3(viewquat_inv, zaxis);
728
729                 for (i = 0 ; i < NUM_SNAP_QUATS; i++) {
730
731                         float view = (int)snapquats[i][4];
732                         float viewquat_inv_test[4];
733                         float zaxis_test[3]={0,0,1};
734
735                         invert_qt_qt(viewquat_inv_test, snapquats[i]);
736                         mul_qt_v3(viewquat_inv_test, zaxis_test);
737                         
738                         if (angle_v3v3(zaxis_test, zaxis) < DEG2RADF(45/3)) {
739                                 /* find the best roll */
740                                 float quat_roll[4], quat_final[4], quat_best[4];
741                                 float viewquat_align[4]; /* viewquat aligned to zaxis_test */
742                                 float viewquat_align_inv[4]; /* viewquat aligned to zaxis_test */
743                                 float best_angle = FLT_MAX;
744                                 int j;
745
746                                 /* viewquat_align is the original viewquat aligned to the snapped axis
747                                  * for testing roll */
748                                 rotation_between_vecs_to_quat(viewquat_align, zaxis_test, zaxis);
749                                 normalize_qt(viewquat_align);
750                                 mul_qt_qtqt(viewquat_align, vod->viewquat, viewquat_align);
751                                 normalize_qt(viewquat_align);
752                                 invert_qt_qt(viewquat_align_inv, viewquat_align);
753
754                                 /* find best roll */
755                                 for (j= 0; j<8; j++) {
756                                         float angle;
757                                         float xaxis1[3]={1,0,0};
758                                         float xaxis2[3]={1,0,0};
759                                         float quat_final_inv[4];
760
761                                         axis_angle_to_quat(quat_roll, zaxis_test, (float)j * DEG2RADF(45.0f));
762                                         normalize_qt(quat_roll);
763
764                                         mul_qt_qtqt(quat_final, snapquats[i], quat_roll);
765                                         normalize_qt(quat_final);
766                                         
767                                         /* compare 2 vector angles to find the least roll */
768                                         invert_qt_qt(quat_final_inv, quat_final);
769                                         mul_qt_v3(viewquat_align_inv, xaxis1);
770                                         mul_qt_v3(quat_final_inv, xaxis2);
771                                         angle= angle_v3v3(xaxis1, xaxis2);
772
773                                         if (angle <= best_angle) {
774                                                 best_angle= angle;
775                                                 copy_qt_qt(quat_best, quat_final);
776                                                 if (j) view= 0; /* view grid assumes certain up axis */
777                                         }
778                                 }
779
780                                 copy_qt_qt(vod->viewquat, quat_best);
781                                 rv3d->view= view; /* if we snap to a rolled camera the grid is invalid */
782
783                                 break;
784                         }
785                 }
786         }
787         vod->oldx= x;
788         vod->oldy= y;
789
790         /* avoid precision loss over time */
791         normalize_qt(vod->viewquat);
792
793         /* use a working copy so view rotation locking doesnt overwrite the locked
794          * rotation back into the view we calculate with */
795         copy_qt_qt(rv3d->viewquat, vod->viewquat);
796
797         ED_view3d_camera_lock_sync(vod->v3d, rv3d);
798
799         ED_region_tag_redraw(vod->ar);
800 }
801
802 static int viewrotate_modal(bContext *C, wmOperator *op, wmEvent *event)
803 {
804         ViewOpsData *vod= op->customdata;
805         short event_code= VIEW_PASS;
806
807         /* execute the events */
808         if (event->type==MOUSEMOVE) {
809                 event_code= VIEW_APPLY;
810         }
811         else if (event->type==EVT_MODAL_MAP) {
812                 switch (event->val) {
813                         case VIEW_MODAL_CONFIRM:
814                                 event_code= VIEW_CONFIRM;
815                                 break;
816                         case VIEWROT_MODAL_AXIS_SNAP_ENABLE:
817                                 vod->axis_snap= TRUE;
818                                 event_code= VIEW_APPLY;
819                                 break;
820                         case VIEWROT_MODAL_AXIS_SNAP_DISABLE:
821                                 vod->axis_snap= FALSE;
822                                 event_code= VIEW_APPLY;
823                                 break;
824                         case VIEWROT_MODAL_SWITCH_ZOOM:
825                                 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
826                                 event_code= VIEW_CONFIRM;
827                                 break;
828                         case VIEWROT_MODAL_SWITCH_MOVE:
829                                 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
830                                 event_code= VIEW_CONFIRM;
831                                 break;
832                 }
833         }
834         else if (event->type==vod->origkey && event->val==KM_RELEASE) {
835                 event_code= VIEW_CONFIRM;
836         }
837
838         if (event_code==VIEW_APPLY) {
839                 viewrotate_apply(vod, event->x, event->y);
840         }
841         else if (event_code==VIEW_CONFIRM) {
842                 ED_view3d_depth_tag_update(vod->rv3d);
843                 viewops_data_free(C, op);
844
845                 return OPERATOR_FINISHED;
846         }
847
848         return OPERATOR_RUNNING_MODAL;
849 }
850
851 static int viewrotate_invoke(bContext *C, wmOperator *op, wmEvent *event)
852 {
853         ViewOpsData *vod;
854         RegionView3D *rv3d;
855
856         /* makes op->customdata */
857         viewops_data_create(C, op, event);
858         vod= op->customdata;
859         rv3d= vod->rv3d;
860
861         if (rv3d->viewlock) { /* poll should check but in some cases fails, see poll func for details */
862                 viewops_data_free(C, op);
863                 return OPERATOR_PASS_THROUGH;
864         }
865
866         /* switch from camera view when: */
867         if (rv3d->persp != RV3D_PERSP) {
868
869                 if (U.uiflag & USER_AUTOPERSP) {
870                         if (!ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
871                                 rv3d->persp= RV3D_PERSP;
872                         }
873                 }
874                 else if (rv3d->persp==RV3D_CAMOB) {
875
876                         /* changed since 2.4x, use the camera view */
877                         if (vod->v3d->camera) {
878                                 ED_view3d_from_object(vod->v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
879                         }
880
881                         if (!ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
882                                 rv3d->persp= rv3d->lpersp;
883                         }
884                 }
885                 ED_region_tag_redraw(vod->ar);
886         }
887         
888         if (event->type == MOUSEPAN) {
889                 viewrotate_apply(vod, event->prevx, event->prevy);
890                 ED_view3d_depth_tag_update(rv3d);
891                 
892                 viewops_data_free(C, op);
893                 
894                 return OPERATOR_FINISHED;
895         }
896         else if (event->type == MOUSEROTATE) {
897                 /* MOUSEROTATE performs orbital rotation, so y axis delta is set to 0 */
898                 viewrotate_apply(vod, event->prevx, event->y);
899                 ED_view3d_depth_tag_update(rv3d);
900                 
901                 viewops_data_free(C, op);
902                 
903                 return OPERATOR_FINISHED;
904         }
905         else {          
906                 /* add temp handler */
907                 WM_event_add_modal_handler(C, op);
908
909                 return OPERATOR_RUNNING_MODAL;
910         }
911 }
912
913 static int view3d_camera_active_poll(bContext *C)
914 {
915         if (ED_operator_view3d_active(C)) {
916                 RegionView3D *rv3d= CTX_wm_region_view3d(C);
917                 if (rv3d && rv3d->persp==RV3D_CAMOB) {
918                         return 1;
919                 }
920         }
921
922         return 0;
923 }
924
925 /* test for unlocked camera view in quad view */
926 static int view3d_camera_user_poll(bContext *C)
927 {
928         View3D *v3d;
929         ARegion *ar;
930
931         if (ED_view3d_context_user_region(C, &v3d, &ar)) {
932                 RegionView3D *rv3d = ar->regiondata;
933                 if (rv3d->persp==RV3D_CAMOB) {
934                         return 1;
935                 }
936         }
937
938         return 0;
939 }
940
941 static int viewrotate_cancel(bContext *C, wmOperator *op)
942 {
943         viewops_data_free(C, op);
944
945         return OPERATOR_CANCELLED;
946 }
947
948 void VIEW3D_OT_rotate(wmOperatorType *ot)
949 {
950
951         /* identifiers */
952         ot->name= "Rotate view";
953         ot->description = "Rotate the view";
954         ot->idname= "VIEW3D_OT_rotate";
955
956         /* api callbacks */
957         ot->invoke= viewrotate_invoke;
958         ot->modal= viewrotate_modal;
959         ot->poll= ED_operator_region_view3d_active;
960         ot->cancel= viewrotate_cancel;
961
962         /* flags */
963         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
964 }
965
966 /* NDOF utility functions
967  * (should these functions live in this file?)
968  */
969 float ndof_to_axis_angle(struct wmNDOFMotionData* ndof, float axis[3])
970 {
971         return ndof->dt * normalize_v3_v3(axis, ndof->rvec);
972 }
973
974 void ndof_to_quat(struct wmNDOFMotionData* ndof, float q[4])
975 {
976         float axis[3];
977         float angle;
978
979         angle = ndof_to_axis_angle(ndof, axis);
980         axis_angle_to_quat(q, axis, angle);
981 }
982
983 /* -- "orbit" navigation (trackball/turntable)
984  * -- zooming
985  * -- panning in rotationally-locked views
986  */
987 static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
988 {
989         if (event->type != NDOF_MOTION)
990                 return OPERATOR_CANCELLED;
991         else {
992                 View3D *v3d = CTX_wm_view3d(C);
993                 RegionView3D* rv3d = CTX_wm_region_view3d(C);
994                 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
995
996                 ED_view3d_camera_lock_init(v3d, rv3d);
997
998                 rv3d->rot_angle = 0.f; /* off by default, until changed later this function */
999
1000                 if (ndof->progress != P_FINISHING) {
1001                         const float dt = ndof->dt;
1002
1003                         /* tune these until everything feels right */
1004                         const float rot_sensitivity = 1.f;
1005                         const float zoom_sensitivity = 1.f;
1006                         const float pan_sensitivity = 1.f;
1007
1008                         const int has_rotation = rv3d->viewlock != RV3D_LOCKED && !is_zero_v3(ndof->rvec);
1009
1010                         float view_inv[4];
1011                         invert_qt_qt(view_inv, rv3d->viewquat);
1012
1013                         /* #define DEBUG_NDOF_MOTION */
1014                         #ifdef DEBUG_NDOF_MOTION
1015                         printf("ndof: T=(%.2f,%.2f,%.2f) R=(%.2f,%.2f,%.2f) dt=%.3f delivered to 3D view\n",
1016                                 ndof->tx, ndof->ty, ndof->tz, ndof->rx, ndof->ry, ndof->rz, ndof->dt);
1017                         #endif
1018
1019                         if (ndof->tz) {
1020                                 /* Zoom!
1021                                  * velocity should be proportional to the linear velocity attained by rotational motion of same strength
1022                                  * [got that?]
1023                                  * proportional to arclength = radius * angle
1024                                  */
1025                                 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tz;
1026
1027                                 if (U.ndof_flag & NDOF_ZOOM_INVERT)
1028                                         zoom_distance = -zoom_distance;
1029
1030                                 rv3d->dist += zoom_distance;
1031                         }
1032
1033                         if (rv3d->viewlock == RV3D_LOCKED) {
1034                                 /* rotation not allowed -- explore panning options instead */
1035                                 float pan_vec[3] = {ndof->tx, ndof->ty, 0.0f};
1036                                 mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
1037
1038                                 /* transform motion from view to world coordinates */
1039                                 invert_qt_qt(view_inv, rv3d->viewquat);
1040                                 mul_qt_v3(view_inv, pan_vec);
1041
1042                                 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1043                                 sub_v3_v3(rv3d->ofs, pan_vec);
1044                         }
1045
1046                         if (has_rotation) {
1047
1048                                 rv3d->view = RV3D_VIEW_USER;
1049
1050                                 if (U.flag & USER_TRACKBALL) {
1051                                         float rot[4];
1052                                         float axis[3];
1053                                         float angle = rot_sensitivity * ndof_to_axis_angle(ndof, axis);
1054
1055                                         if (U.ndof_flag & NDOF_ROLL_INVERT_AXIS)
1056                                                 axis[2] = -axis[2];
1057
1058                                         if (U.ndof_flag & NDOF_TILT_INVERT_AXIS)
1059                                                 axis[0] = -axis[0];
1060
1061                                         if (U.ndof_flag & NDOF_ROTATE_INVERT_AXIS)
1062                                                 axis[1] = -axis[1];
1063
1064                                         /* transform rotation axis from view to world coordinates */
1065                                         mul_qt_v3(view_inv, axis);
1066
1067                                         /* update the onscreen doo-dad */
1068                                         rv3d->rot_angle = angle;
1069                                         copy_v3_v3(rv3d->rot_axis, axis);
1070
1071                                         axis_angle_to_quat(rot, axis, angle);
1072
1073                                         /* apply rotation */
1074                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1075
1076                                 }
1077                                 else {
1078
1079                                         /* turntable view code by John Aughey, adapted for 3D mouse by [mce] */
1080                                         float angle, rot[4];
1081                                         float xvec[3] = {1,0,0};
1082
1083                                         /* Determine the direction of the x vector (for rotating up and down) */
1084                                         mul_qt_v3(view_inv, xvec);
1085
1086                                         /* Perform the up/down rotation */
1087                                         angle = rot_sensitivity * dt * ndof->rx;
1088                                         if (U.ndof_flag & NDOF_TILT_INVERT_AXIS)
1089                                                 angle = -angle;
1090                                         rot[0] = cos(angle);
1091                                         mul_v3_v3fl(rot+1, xvec, sin(angle));
1092                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1093
1094                                         /* Perform the orbital rotation */
1095                                         angle = rot_sensitivity * dt * ndof->ry;
1096                                         if (U.ndof_flag & NDOF_ROTATE_INVERT_AXIS)
1097                                                 angle = -angle;
1098
1099                                         /* update the onscreen doo-dad */
1100                                         rv3d->rot_angle = angle;
1101                                         rv3d->rot_axis[0] = 0;
1102                                         rv3d->rot_axis[1] = 0;
1103                                         rv3d->rot_axis[2] = 1;
1104
1105                                         rot[0] = cos(angle);
1106                                         rot[1] = rot[2] = 0.0;
1107                                         rot[3] = sin(angle);
1108                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1109                                 }
1110                         }
1111                 }
1112
1113                 ED_view3d_camera_lock_sync(v3d, rv3d);
1114
1115                 ED_region_tag_redraw(CTX_wm_region(C));
1116
1117                 return OPERATOR_FINISHED;
1118         }
1119 }
1120
1121 void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot)
1122 {
1123         /* identifiers */
1124         ot->name = "NDOF Orbit View";
1125         ot->description = "Explore every angle of an object using the 3D mouse";
1126         ot->idname = "VIEW3D_OT_ndof_orbit";
1127
1128         /* api callbacks */
1129         ot->invoke = ndof_orbit_invoke;
1130         ot->poll = ED_operator_view3d_active;
1131
1132         /* flags */
1133         ot->flag = 0;
1134 }
1135
1136 /* -- "pan" navigation
1137  * -- zoom or dolly?
1138  */
1139 static int ndof_pan_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
1140 {
1141         if (event->type != NDOF_MOTION)
1142                 return OPERATOR_CANCELLED;
1143         else {
1144                 View3D *v3d= CTX_wm_view3d(C);
1145                 RegionView3D* rv3d = CTX_wm_region_view3d(C);
1146                 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
1147
1148                 ED_view3d_camera_lock_init(v3d, rv3d);
1149
1150                 rv3d->rot_angle = 0.f; /* we're panning here! so erase any leftover rotation from other operators */
1151
1152                 if (ndof->progress != P_FINISHING) {
1153                         const float dt = ndof->dt;
1154                         float view_inv[4];
1155 #if 0 /* ------------------------------------------- zoom with Z */
1156                         /* tune these until everything feels right */
1157                         const float zoom_sensitivity = 1.f;
1158                         const float pan_sensitivity = 1.f;
1159
1160                         float pan_vec[3] = {
1161                                 ndof->tx, ndof->ty, 0
1162                                 };
1163
1164                         /* "zoom in" or "translate"? depends on zoom mode in user settings? */
1165                         if (ndof->tz) {
1166                                 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tz;
1167                                 rv3d->dist += zoom_distance;
1168                         }
1169
1170                         mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
1171 #else /* ------------------------------------------------------- dolly with Z */
1172                         float speed = 10.f; /* blender units per second */
1173                         /* ^^ this is ok for default cube scene, but should scale with.. something */
1174
1175                         /* tune these until everything feels right */
1176                         const float forward_sensitivity = 1.f;
1177                         const float vertical_sensitivity = 0.4f;
1178                         const float lateral_sensitivity = 0.6f;
1179
1180                         float pan_vec[3];
1181
1182                         if (U.ndof_flag & NDOF_PANX_INVERT_AXIS)
1183                                 pan_vec[0] = -lateral_sensitivity * ndof->tvec[0];
1184                         else
1185                                 pan_vec[0] = lateral_sensitivity * ndof->tvec[0];
1186
1187                         if (U.ndof_flag & NDOF_PANZ_INVERT_AXIS)
1188                                 pan_vec[1] = -vertical_sensitivity * ndof->tvec[1];
1189                         else
1190                                 pan_vec[1] = vertical_sensitivity * ndof->tvec[1];
1191
1192                         if (U.ndof_flag & NDOF_PANY_INVERT_AXIS)
1193                                 pan_vec[2] = -forward_sensitivity * ndof->tvec[2];
1194                         else
1195                                 pan_vec[2] = forward_sensitivity * ndof->tvec[2];
1196
1197                         mul_v3_fl(pan_vec, speed * dt);
1198 #endif
1199                         /* transform motion from view to world coordinates */
1200                         invert_qt_qt(view_inv, rv3d->viewquat);
1201                         mul_qt_v3(view_inv, pan_vec);
1202
1203                         /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1204                         sub_v3_v3(rv3d->ofs, pan_vec);
1205                 }
1206
1207                 ED_view3d_camera_lock_sync(v3d, rv3d);
1208
1209                 ED_region_tag_redraw(CTX_wm_region(C));
1210
1211                 return OPERATOR_FINISHED;
1212         }
1213 }
1214
1215 void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot)
1216 {
1217         /* identifiers */
1218         ot->name = "NDOF Pan View";
1219         ot->description = "Position your viewpoint with the 3D mouse";
1220         ot->idname = "VIEW3D_OT_ndof_pan";
1221
1222         /* api callbacks */
1223         ot->invoke = ndof_pan_invoke;
1224         ot->poll = ED_operator_view3d_active;
1225
1226         /* flags */
1227         ot->flag = 0;
1228 }
1229
1230 /* ************************ viewmove ******************************** */
1231
1232
1233 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
1234
1235 /* called in transform_ops.c, on each regeneration of keymaps  */
1236 void viewmove_modal_keymap(wmKeyConfig *keyconf)
1237 {
1238         static EnumPropertyItem modal_items[] = {
1239         {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1240                 
1241         {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
1242         {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1243
1244         {0, NULL, 0, NULL, NULL}};
1245
1246         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Move Modal");
1247
1248         /* this function is called for each spacetype, only needs to add map once */
1249         if (keymap) return;
1250
1251         keymap= WM_modalkeymap_add(keyconf, "View3D Move Modal", modal_items);
1252
1253         /* items for modal map */
1254         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1255         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1256
1257         /* disabled mode switching for now, can re-implement better, later on
1258         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1259         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1260         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1261         */
1262         
1263         /* assign map to operators */
1264         WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
1265 }
1266
1267
1268 static void viewmove_apply(ViewOpsData *vod, int x, int y)
1269 {
1270         if ((vod->rv3d->persp==RV3D_CAMOB) && !ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
1271                 const float zoomfac= BKE_screen_view3d_zoom_to_fac((float)vod->rv3d->camzoom) * 2.0f;
1272                 vod->rv3d->camdx += (vod->oldx - x)/(vod->ar->winx * zoomfac);
1273                 vod->rv3d->camdy += (vod->oldy - y)/(vod->ar->winy * zoomfac);
1274                 CLAMP(vod->rv3d->camdx, -1.0f, 1.0f);
1275                 CLAMP(vod->rv3d->camdy, -1.0f, 1.0f);
1276         }
1277         else {
1278                 float dvec[3];
1279                 float mval_f[2];
1280
1281                 mval_f[0]= x - vod->oldx;
1282                 mval_f[1]= y - vod->oldy;
1283                 ED_view3d_win_to_delta(vod->ar, mval_f, dvec);
1284
1285                 add_v3_v3(vod->rv3d->ofs, dvec);
1286
1287                 if (vod->rv3d->viewlock & RV3D_BOXVIEW)
1288                         view3d_boxview_sync(vod->sa, vod->ar);
1289         }
1290
1291         vod->oldx= x;
1292         vod->oldy= y;
1293
1294         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1295
1296         ED_region_tag_redraw(vod->ar);
1297 }
1298
1299
1300 static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
1301 {
1302
1303         ViewOpsData *vod= op->customdata;
1304         short event_code= VIEW_PASS;
1305
1306         /* execute the events */
1307         if (event->type==MOUSEMOVE) {
1308                 event_code= VIEW_APPLY;
1309         }
1310         else if (event->type==EVT_MODAL_MAP) {
1311                 switch (event->val) {
1312                         case VIEW_MODAL_CONFIRM:
1313                                 event_code= VIEW_CONFIRM;
1314                                 break;
1315                         case VIEWROT_MODAL_SWITCH_ZOOM:
1316                                 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
1317                                 event_code= VIEW_CONFIRM;
1318                                 break;
1319                         case VIEWROT_MODAL_SWITCH_ROTATE:
1320                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1321                                 event_code= VIEW_CONFIRM;
1322                                 break;
1323                 }
1324         }
1325         else if (event->type==vod->origkey && event->val==KM_RELEASE) {
1326                 event_code= VIEW_CONFIRM;
1327         }
1328
1329         if (event_code==VIEW_APPLY) {
1330                 viewmove_apply(vod, event->x, event->y);
1331         }
1332         else if (event_code==VIEW_CONFIRM) {
1333                 ED_view3d_depth_tag_update(vod->rv3d);
1334
1335                 viewops_data_free(C, op);
1336
1337                 return OPERATOR_FINISHED;
1338         }
1339
1340         return OPERATOR_RUNNING_MODAL;
1341 }
1342
1343 static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
1344 {
1345         ViewOpsData *vod;
1346
1347         /* makes op->customdata */
1348         viewops_data_create(C, op, event);
1349         vod= op->customdata;
1350
1351         if (event->type == MOUSEPAN) {
1352                 viewmove_apply(vod, event->prevx, event->prevy);
1353                 ED_view3d_depth_tag_update(vod->rv3d);
1354                 
1355                 viewops_data_free(C, op);               
1356                 
1357                 return OPERATOR_FINISHED;
1358         }
1359         else {
1360                 /* add temp handler */
1361                 WM_event_add_modal_handler(C, op);
1362
1363                 return OPERATOR_RUNNING_MODAL;
1364         }
1365 }
1366
1367 static int viewmove_cancel(bContext *C, wmOperator *op)
1368 {
1369         viewops_data_free(C, op);
1370
1371         return OPERATOR_CANCELLED;
1372 }
1373
1374 void VIEW3D_OT_move(wmOperatorType *ot)
1375 {
1376
1377         /* identifiers */
1378         ot->name= "Move view";
1379         ot->description = "Move the view";
1380         ot->idname= "VIEW3D_OT_move";
1381
1382         /* api callbacks */
1383         ot->invoke= viewmove_invoke;
1384         ot->modal= viewmove_modal;
1385         ot->poll= ED_operator_view3d_active;
1386         ot->cancel= viewmove_cancel;
1387
1388         /* flags */
1389         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1390 }
1391
1392 /* ************************ viewzoom ******************************** */
1393
1394 /* viewdolly_modal_keymap has an exact copy of this, apply fixes to both */
1395 /* called in transform_ops.c, on each regeneration of keymaps  */
1396 void viewzoom_modal_keymap(wmKeyConfig *keyconf)
1397 {
1398         static EnumPropertyItem modal_items[] = {
1399         {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1400                 
1401         {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1402         {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1403
1404         {0, NULL, 0, NULL, NULL}};
1405
1406         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Zoom Modal");
1407
1408         /* this function is called for each spacetype, only needs to add map once */
1409         if (keymap) return;
1410
1411         keymap= WM_modalkeymap_add(keyconf, "View3D Zoom Modal", modal_items);
1412
1413         /* items for modal map */
1414         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1415         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1416
1417         /* disabled mode switching for now, can re-implement better, later on
1418         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1419         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1420         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
1421          */
1422         
1423         /* assign map to operators */
1424         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
1425 }
1426
1427 static void view_zoom_mouseloc(ARegion *ar, float dfac, int mx, int my)
1428 {
1429         RegionView3D *rv3d= ar->regiondata;
1430
1431         if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
1432                 float dvec[3];
1433                 float tvec[3];
1434                 float tpos[3];
1435                 float mval_f[2];
1436                 float new_dist;
1437
1438                 negate_v3_v3(tpos, rv3d->ofs);
1439
1440                 /* Project cursor position into 3D space */
1441                 initgrabz(rv3d, tpos[0], tpos[1], tpos[2]);
1442
1443                 mval_f[0]= (float)(((mx - ar->winrct.xmin) * 2) - ar->winx) / 2.0f;
1444                 mval_f[1]= (float)(((my - ar->winrct.ymin) * 2) - ar->winy) / 2.0f;
1445                 ED_view3d_win_to_delta(ar, mval_f, dvec);
1446
1447                 /* Calculate view target position for dolly */
1448                 add_v3_v3v3(tvec, tpos, dvec);
1449                 negate_v3(tvec);
1450
1451                 /* Offset to target position and dolly */
1452                 new_dist = rv3d->dist * dfac;
1453
1454                 copy_v3_v3(rv3d->ofs, tvec);
1455                 rv3d->dist = new_dist;
1456
1457                 /* Calculate final offset */
1458                 madd_v3_v3v3fl(rv3d->ofs, tvec, dvec, dfac);
1459         }
1460         else {
1461                 rv3d->dist *= dfac;
1462         }
1463 }
1464
1465
1466 static void viewzoom_apply(ViewOpsData *vod, int x, int y, const short viewzoom, const short zoom_invert)
1467 {
1468         float zfac=1.0;
1469
1470         if (viewzoom==USER_ZOOM_CONT) {
1471                 double time= PIL_check_seconds_timer();
1472                 float time_step= (float)(time - vod->timer_lastdraw);
1473                 float fac;
1474
1475                 if (U.uiflag & USER_ZOOM_HORIZ) {
1476                         fac= (float)(x - vod->origx);
1477                 }
1478                 else {
1479                         fac= (float)(y - vod->origy);
1480                 }
1481
1482                 if (zoom_invert) {
1483                         fac= -fac;
1484                 }
1485
1486                 // oldstyle zoom
1487                 zfac = 1.0f + ((fac / 20.0f) * time_step);
1488                 vod->timer_lastdraw= time;
1489         }
1490         else if (viewzoom==USER_ZOOM_SCALE) {
1491                 int ctr[2], len1, len2;
1492                 // method which zooms based on how far you move the mouse
1493
1494                 ctr[0] = (vod->ar->winrct.xmax + vod->ar->winrct.xmin)/2;
1495                 ctr[1] = (vod->ar->winrct.ymax + vod->ar->winrct.ymin)/2;
1496
1497                 len1 = (int)sqrt((ctr[0] - x)*(ctr[0] - x) + (ctr[1] - y)*(ctr[1] - y)) + 5;
1498                 len2 = (int)sqrt((ctr[0] - vod->origx)*(ctr[0] - vod->origx) + (ctr[1] - vod->origy)*(ctr[1] - vod->origy)) + 5;
1499
1500                 zfac = vod->dist0 * ((float)len2/len1) / vod->rv3d->dist;
1501         }
1502         else {  /* USER_ZOOM_DOLLY */
1503                 float len1, len2;
1504                 
1505                 if (U.uiflag & USER_ZOOM_HORIZ) {
1506                         len1 = (vod->ar->winrct.xmax - x) + 5;
1507                         len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1508                 }
1509                 else {
1510                         len1 = (vod->ar->winrct.ymax - y) + 5;
1511                         len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1512                 }
1513                 if (zoom_invert) {
1514                         SWAP(float, len1, len2);
1515                 }
1516                 
1517                 zfac = vod->dist0 * (2.0f * ((len2/len1)-1.0f) + 1.0f) / vod->rv3d->dist;
1518         }
1519
1520         if (zfac != 1.0f && zfac*vod->rv3d->dist > 0.001f * vod->grid &&
1521                         zfac * vod->rv3d->dist < 10.0f * vod->far)
1522                 view_zoom_mouseloc(vod->ar, zfac, vod->oldx, vod->oldy);
1523
1524         /* these limits were in old code too */
1525         if (vod->rv3d->dist<0.001f * vod->grid) vod->rv3d->dist= 0.001f * vod->grid;
1526         if (vod->rv3d->dist>10.0f * vod->far) vod->rv3d->dist=10.0f * vod->far;
1527
1528         if (vod->rv3d->viewlock & RV3D_BOXVIEW)
1529                 view3d_boxview_sync(vod->sa, vod->ar);
1530
1531         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1532
1533         ED_region_tag_redraw(vod->ar);
1534 }
1535
1536
1537 static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
1538 {
1539         ViewOpsData *vod= op->customdata;
1540         short event_code= VIEW_PASS;
1541
1542         /* execute the events */
1543         if (event->type == TIMER && event->customdata == vod->timer) {
1544                 /* continuous zoom */
1545                 event_code= VIEW_APPLY;
1546         }
1547         else if (event->type==MOUSEMOVE) {
1548                 event_code= VIEW_APPLY;
1549         }
1550         else if (event->type==EVT_MODAL_MAP) {
1551                 switch (event->val) {
1552                         case VIEW_MODAL_CONFIRM:
1553                                 event_code= VIEW_CONFIRM;
1554                                 break;
1555                         case VIEWROT_MODAL_SWITCH_MOVE:
1556                                 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1557                                 event_code= VIEW_CONFIRM;
1558                                 break;
1559                         case VIEWROT_MODAL_SWITCH_ROTATE:
1560                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1561                                 event_code= VIEW_CONFIRM;
1562                                 break;
1563                 }
1564         }
1565         else if (event->type==vod->origkey && event->val==KM_RELEASE) {
1566                 event_code= VIEW_CONFIRM;
1567         }
1568
1569         if (event_code==VIEW_APPLY) {
1570                 viewzoom_apply(vod, event->x, event->y, U.viewzoom, (U.uiflag & USER_ZOOM_INVERT) != 0);
1571         }
1572         else if (event_code==VIEW_CONFIRM) {
1573                 ED_view3d_depth_tag_update(vod->rv3d);
1574                 viewops_data_free(C, op);
1575
1576                 return OPERATOR_FINISHED;
1577         }
1578
1579         return OPERATOR_RUNNING_MODAL;
1580 }
1581
1582 static int viewzoom_exec(bContext *C, wmOperator *op)
1583 {
1584         View3D *v3d;
1585         RegionView3D *rv3d;
1586         ScrArea *sa;
1587         ARegion *ar;
1588         short use_cam_zoom;
1589
1590         int delta= RNA_int_get(op->ptr, "delta");
1591         int mx, my;
1592
1593         if (op->customdata) {
1594                 ViewOpsData *vod= op->customdata;
1595
1596                 sa= vod->sa;
1597                 ar= vod->ar;
1598         }
1599         else {
1600                 sa= CTX_wm_area(C);
1601                 ar= CTX_wm_region(C);
1602         }
1603
1604         v3d= sa->spacedata.first;
1605         rv3d= ar->regiondata;
1606
1607         mx= RNA_struct_property_is_set(op->ptr, "mx") ? RNA_int_get(op->ptr, "mx") : ar->winx / 2;
1608         my= RNA_struct_property_is_set(op->ptr, "my") ? RNA_int_get(op->ptr, "my") : ar->winy / 2;
1609
1610         use_cam_zoom= (rv3d->persp==RV3D_CAMOB) && !(rv3d->is_persp && ED_view3d_camera_lock_check(v3d, rv3d));
1611
1612         if (delta < 0) {
1613                 /* this min and max is also in viewmove() */
1614                 if (use_cam_zoom) {
1615                         rv3d->camzoom-= 10;
1616                         if (rv3d->camzoom < RV3D_CAMZOOM_MIN) rv3d->camzoom= RV3D_CAMZOOM_MIN;
1617                 }
1618                 else if (rv3d->dist < 10.0f * v3d->far) {
1619                         view_zoom_mouseloc(ar, 1.2f, mx, my);
1620                 }
1621         }
1622         else {
1623                 if (use_cam_zoom) {
1624                         rv3d->camzoom+= 10;
1625                         if (rv3d->camzoom > RV3D_CAMZOOM_MAX) rv3d->camzoom= RV3D_CAMZOOM_MAX;
1626                 }
1627                 else if (rv3d->dist> 0.001f * v3d->grid) {
1628                         view_zoom_mouseloc(ar, .83333f, mx, my);
1629                 }
1630         }
1631
1632         if (rv3d->viewlock & RV3D_BOXVIEW)
1633                 view3d_boxview_sync(sa, ar);
1634
1635         ED_view3d_depth_tag_update(rv3d);
1636
1637         ED_view3d_camera_lock_sync(v3d, rv3d);
1638
1639         ED_region_tag_redraw(ar);
1640
1641         viewops_data_free(C, op);
1642
1643         return OPERATOR_FINISHED;
1644 }
1645
1646 /* this is an exact copy of viewzoom_modal_keymap */
1647 /* called in transform_ops.c, on each regeneration of keymaps  */
1648 void viewdolly_modal_keymap(wmKeyConfig *keyconf)
1649 {
1650         static EnumPropertyItem modal_items[] = {
1651         {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1652
1653         {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1654         {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1655
1656         {0, NULL, 0, NULL, NULL}};
1657
1658         wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Dolly Modal");
1659
1660         /* this function is called for each spacetype, only needs to add map once */
1661         if (keymap) return;
1662
1663         keymap= WM_modalkeymap_add(keyconf, "View3D Dolly Modal", modal_items);
1664
1665         /* items for modal map */
1666         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1667         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1668
1669         /* disabled mode switching for now, can re-implement better, later on
1670         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1671         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1672         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
1673          */
1674         
1675         /* assign map to operators */
1676         WM_modalkeymap_assign(keymap, "VIEW3D_OT_dolly");
1677 }
1678
1679 /* viewdolly_invoke() copied this function, changes here may apply there */
1680 static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
1681 {
1682         ViewOpsData *vod;
1683
1684         /* makes op->customdata */
1685         viewops_data_create(C, op, event);
1686         vod= op->customdata;
1687
1688         /* if one or the other zoom position aren't set, set from event */
1689         if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
1690                 RNA_int_set(op->ptr, "mx", event->x);
1691                 RNA_int_set(op->ptr, "my", event->y);
1692         }
1693
1694         if (RNA_struct_property_is_set(op->ptr, "delta")) {
1695                 viewzoom_exec(C, op);
1696         }
1697         else {
1698                 if (event->type == MOUSEZOOM) {
1699                         /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1700
1701                         if (U.uiflag & USER_ZOOM_HORIZ) {
1702                                 vod->origx = vod->oldx = event->x;
1703                                 viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY, FALSE);
1704                         }
1705                         else {
1706                                 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
1707                                 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
1708                                 viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY, FALSE);
1709                         }
1710                         ED_view3d_depth_tag_update(vod->rv3d);
1711                         
1712                         viewops_data_free(C, op);
1713                         return OPERATOR_FINISHED;
1714                 }
1715                 else {
1716                         if (U.viewzoom == USER_ZOOM_CONT) {
1717                                 /* needs a timer to continue redrawing */
1718                                 vod->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
1719                                 vod->timer_lastdraw= PIL_check_seconds_timer();
1720                         }
1721
1722                         /* add temp handler */
1723                         WM_event_add_modal_handler(C, op);
1724
1725                         return OPERATOR_RUNNING_MODAL;
1726                 }
1727         }
1728         return OPERATOR_FINISHED;
1729 }
1730
1731 static int viewzoom_cancel(bContext *C, wmOperator *op)
1732 {
1733         viewops_data_free(C, op);
1734
1735         return OPERATOR_CANCELLED;
1736 }
1737
1738 void VIEW3D_OT_zoom(wmOperatorType *ot)
1739 {
1740         /* identifiers */
1741         ot->name= "Zoom View";
1742         ot->description = "Zoom in/out in the view";
1743         ot->idname= "VIEW3D_OT_zoom";
1744
1745         /* api callbacks */
1746         ot->invoke= viewzoom_invoke;
1747         ot->exec= viewzoom_exec;
1748         ot->modal= viewzoom_modal;
1749         ot->poll= ED_operator_region_view3d_active;
1750         ot->cancel= viewzoom_cancel;
1751
1752         /* flags */
1753         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1754
1755         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1756         RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
1757         RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
1758 }
1759
1760
1761 /* ************************ viewdolly ******************************** */
1762 static void view_dolly_mouseloc(ARegion *ar, float orig_ofs[3], float dvec[3], float dfac)
1763 {
1764         RegionView3D *rv3d= ar->regiondata;
1765         madd_v3_v3v3fl(rv3d->ofs, orig_ofs, dvec, -(1.0f - dfac));
1766 }
1767
1768 static void viewdolly_apply(ViewOpsData *vod, int x, int y, const short zoom_invert)
1769 {
1770         float zfac=1.0;
1771
1772         {
1773                 float len1, len2;
1774
1775                 if (U.uiflag & USER_ZOOM_HORIZ) {
1776                         len1 = (vod->ar->winrct.xmax - x) + 5;
1777                         len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1778                 }
1779                 else {
1780                         len1 = (vod->ar->winrct.ymax - y) + 5;
1781                         len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1782                 }
1783                 if (zoom_invert)
1784                         SWAP(float, len1, len2);
1785
1786                 zfac =  1.0f + ((len2 - len1) * 0.01f * vod->rv3d->dist);
1787         }
1788
1789         if (zfac != 1.0f)
1790                 view_dolly_mouseloc(vod->ar, vod->ofs, vod->mousevec, zfac);
1791
1792         if (vod->rv3d->viewlock & RV3D_BOXVIEW)
1793                 view3d_boxview_sync(vod->sa, vod->ar);
1794
1795         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1796
1797         ED_region_tag_redraw(vod->ar);
1798 }
1799
1800
1801 static int viewdolly_modal(bContext *C, wmOperator *op, wmEvent *event)
1802 {
1803         ViewOpsData *vod= op->customdata;
1804         short event_code= VIEW_PASS;
1805
1806         /* execute the events */
1807         if (event->type==MOUSEMOVE) {
1808                 event_code= VIEW_APPLY;
1809         }
1810         else if (event->type==EVT_MODAL_MAP) {
1811                 switch (event->val) {
1812                         case VIEW_MODAL_CONFIRM:
1813                                 event_code= VIEW_CONFIRM;
1814                                 break;
1815                         case VIEWROT_MODAL_SWITCH_MOVE:
1816                                 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1817                                 event_code= VIEW_CONFIRM;
1818                                 break;
1819                         case VIEWROT_MODAL_SWITCH_ROTATE:
1820                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1821                                 event_code= VIEW_CONFIRM;
1822                                 break;
1823                 }
1824         }
1825         else if (event->type==vod->origkey && event->val==KM_RELEASE) {
1826                 event_code= VIEW_CONFIRM;
1827         }
1828
1829         if (event_code==VIEW_APPLY) {
1830                 viewdolly_apply(vod, event->x, event->y, (U.uiflag & USER_ZOOM_INVERT) != 0);
1831         }
1832         else if (event_code==VIEW_CONFIRM) {
1833                 ED_view3d_depth_tag_update(vod->rv3d);
1834                 viewops_data_free(C, op);
1835
1836                 return OPERATOR_FINISHED;
1837         }
1838
1839         return OPERATOR_RUNNING_MODAL;
1840 }
1841
1842 static int viewdolly_exec(bContext *C, wmOperator *op)
1843 {
1844         /* View3D *v3d; */
1845         RegionView3D *rv3d;
1846         ScrArea *sa;
1847         ARegion *ar;
1848         float mousevec[3];
1849
1850         int delta= RNA_int_get(op->ptr, "delta");
1851
1852         if (op->customdata) {
1853                 ViewOpsData *vod= op->customdata;
1854
1855                 sa= vod->sa;
1856                 ar= vod->ar;
1857                 copy_v3_v3(mousevec, vod->mousevec);
1858         }
1859         else {
1860                 sa= CTX_wm_area(C);
1861                 ar= CTX_wm_region(C);
1862                 negate_v3_v3(mousevec, ((RegionView3D *)ar->regiondata)->viewinv[2]);
1863                 normalize_v3(mousevec);
1864         }
1865
1866         /* v3d= sa->spacedata.first; */ /* UNUSED */
1867         rv3d= ar->regiondata;
1868
1869         /* overwrite the mouse vector with the view direction (zoom into the center) */
1870         if ((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
1871                 normalize_v3_v3(mousevec, rv3d->viewinv[2]);
1872         }
1873
1874         if (delta < 0) {
1875                 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, 1.2f);
1876         }
1877         else {
1878                 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, .83333f);
1879         }
1880
1881         if (rv3d->viewlock & RV3D_BOXVIEW)
1882                 view3d_boxview_sync(sa, ar);
1883
1884         ED_view3d_depth_tag_update(rv3d);
1885         ED_region_tag_redraw(ar);
1886
1887         viewops_data_free(C, op);
1888
1889         return OPERATOR_FINISHED;
1890 }
1891
1892 /* copied from viewzoom_invoke(), changes here may apply there */
1893 static int viewdolly_invoke(bContext *C, wmOperator *op, wmEvent *event)
1894 {
1895         ViewOpsData *vod;
1896
1897         /* makes op->customdata */
1898         viewops_data_create(C, op, event);
1899         vod= op->customdata;
1900
1901         /* if one or the other zoom position aren't set, set from event */
1902         if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
1903                 RNA_int_set(op->ptr, "mx", event->x);
1904                 RNA_int_set(op->ptr, "my", event->y);
1905         }
1906
1907         if (RNA_struct_property_is_set(op->ptr, "delta")) {
1908                 viewdolly_exec(C, op);
1909         }
1910         else {
1911                 /* overwrite the mouse vector with the view direction (zoom into the center) */
1912                 if ((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
1913                         negate_v3_v3(vod->mousevec, vod->rv3d->viewinv[2]);
1914                         normalize_v3(vod->mousevec);
1915                 }
1916
1917                 if (event->type == MOUSEZOOM) {
1918                         /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1919
1920                         if (U.uiflag & USER_ZOOM_HORIZ) {
1921                                 vod->origx = vod->oldx = event->x;
1922                                 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
1923                         }
1924                         else {
1925
1926                                 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
1927                                 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
1928                                 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
1929                         }
1930                         ED_view3d_depth_tag_update(vod->rv3d);
1931
1932                         viewops_data_free(C, op);
1933                         return OPERATOR_FINISHED;
1934                 }
1935                 else {
1936                         /* add temp handler */
1937                         WM_event_add_modal_handler(C, op);
1938
1939                         return OPERATOR_RUNNING_MODAL;
1940                 }
1941         }
1942         return OPERATOR_FINISHED;
1943 }
1944
1945 /* like ED_operator_region_view3d_active but check its not in ortho view */
1946 static int viewdolly_poll(bContext *C)
1947 {
1948         RegionView3D *rv3d= CTX_wm_region_view3d(C);
1949
1950         if (rv3d) {
1951                 if (rv3d->persp == RV3D_PERSP) {
1952                         return 1;
1953                 }
1954                 else {
1955                         View3D *v3d= CTX_wm_view3d(C);
1956                         if (ED_view3d_camera_lock_check(v3d, rv3d)) {
1957                                 return 1;
1958                         }
1959                 }
1960         }
1961         return 0;
1962 }
1963
1964 static int viewdolly_cancel(bContext *C, wmOperator *op)
1965 {
1966         viewops_data_free(C, op);
1967
1968         return OPERATOR_CANCELLED;
1969 }
1970
1971 void VIEW3D_OT_dolly(wmOperatorType *ot)
1972 {
1973         /* identifiers */
1974         ot->name= "Dolly view";
1975         ot->description = "Dolly in/out in the view";
1976         ot->idname= "VIEW3D_OT_dolly";
1977
1978         /* api callbacks */
1979         ot->invoke= viewdolly_invoke;
1980         ot->exec= viewdolly_exec;
1981         ot->modal= viewdolly_modal;
1982         ot->poll= viewdolly_poll;
1983         ot->cancel= viewdolly_cancel;
1984
1985         /* flags */
1986         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1987
1988         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1989         RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
1990         RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
1991 }
1992
1993
1994
1995 static int view3d_all_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
1996 {
1997         ARegion *ar= CTX_wm_region(C);
1998         View3D *v3d = CTX_wm_view3d(C);
1999         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2000         Scene *scene= CTX_data_scene(C);
2001         Base *base;
2002         float *curs;
2003         const short skip_camera= ED_view3d_camera_lock_check(v3d, rv3d);
2004
2005         int center= RNA_boolean_get(op->ptr, "center");
2006
2007         float size, min[3], max[3], afm[3];
2008         int ok= 1, onedone=0;
2009
2010         if (center) {
2011                 /* in 2.4x this also move the cursor to (0, 0, 0) (with shift+c). */
2012                 curs= give_cursor(scene, v3d);
2013                 zero_v3(min);
2014                 zero_v3(max);
2015                 zero_v3(curs);
2016         }
2017         else {
2018                 INIT_MINMAX(min, max);
2019         }
2020
2021         for (base= scene->base.first; base; base= base->next) {
2022                 if (BASE_VISIBLE(v3d, base)) {
2023                         onedone= 1;
2024
2025                         if (skip_camera && base->object == v3d->camera) {
2026                                 continue;
2027                         }
2028
2029                         minmax_object(base->object, min, max);
2030                 }
2031         }
2032         if (!onedone) {
2033                 ED_region_tag_redraw(ar);
2034                 /* TODO - should this be cancel?
2035                  * I think no, because we always move the cursor, with or without
2036                  * object, but in this case there is no change in the scene,
2037                  * only the cursor so I choice a ED_region_tag like
2038                  * smooth_view do for the center_cursor.
2039                  * See bug #22640
2040                  */
2041                 return OPERATOR_FINISHED;
2042         }
2043
2044         sub_v3_v3v3(afm, max, min);
2045         size= 0.7f*MAX3(afm[0], afm[1], afm[2]);
2046         if (size == 0.0f) ok= 0;
2047
2048         if (ok) {
2049                 float new_dist;
2050                 float new_ofs[3];
2051
2052                 new_dist = size;
2053                 new_ofs[0]= -(min[0]+max[0])/2.0f;
2054                 new_ofs[1]= -(min[1]+max[1])/2.0f;
2055                 new_ofs[2]= -(min[2]+max[2])/2.0f;
2056
2057                 // correction for window aspect ratio
2058                 if (ar->winy>2 && ar->winx>2) {
2059                         size= (float)ar->winx/(float)ar->winy;
2060                         if (size < 1.0f) size= 1.0f/size;
2061                         new_dist*= size;
2062                 }
2063
2064                 if ((rv3d->persp==RV3D_CAMOB) && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2065                         rv3d->persp= RV3D_PERSP;
2066                         smooth_view(C, v3d, ar, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
2067                 }
2068                 else {
2069                         smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2070                 }
2071         }
2072 // XXX  BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2073
2074         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
2075
2076         return OPERATOR_FINISHED;
2077 }
2078
2079
2080 void VIEW3D_OT_view_all(wmOperatorType *ot)
2081 {
2082         /* identifiers */
2083         ot->name= "View All";
2084         ot->description = "View all objects in scene";
2085         ot->idname= "VIEW3D_OT_view_all";
2086
2087         /* api callbacks */
2088         ot->exec= view3d_all_exec;
2089         ot->poll= ED_operator_region_view3d_active;
2090
2091         /* flags */
2092         ot->flag= 0;
2093
2094         RNA_def_boolean(ot->srna, "center", 0, "Center", "");
2095 }
2096
2097 /* like a localview without local!, was centerview() in 2.4x */
2098 static int viewselected_exec(bContext *C, wmOperator *UNUSED(op))
2099 {
2100         ARegion *ar= CTX_wm_region(C);
2101         View3D *v3d = CTX_wm_view3d(C);
2102         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2103         Scene *scene= CTX_data_scene(C);
2104         Object *ob= OBACT;
2105         Object *obedit= CTX_data_edit_object(C);
2106         float size, min[3], max[3], afm[3];
2107         int ok=0, ok_dist=1;
2108         const short skip_camera= ED_view3d_camera_lock_check(v3d, rv3d);
2109
2110         /* SMOOTHVIEW */
2111         float new_ofs[3];
2112         float new_dist;
2113
2114         INIT_MINMAX(min, max);
2115
2116         if (ob && ob->mode & OB_MODE_WEIGHT_PAINT) {
2117                 /* hardcoded exception, we look for the one selected armature */
2118                 /* this is weak code this way, we should make a generic active/selection callback interface once... */
2119                 Base *base;
2120                 for (base=scene->base.first; base; base= base->next) {
2121                         if (TESTBASELIB(v3d, base)) {
2122                                 if (base->object->type==OB_ARMATURE)
2123                                         if (base->object->mode & OB_MODE_POSE)
2124                                                 break;
2125                         }
2126                 }
2127                 if (base)
2128                         ob= base->object;
2129         }
2130
2131
2132         if (obedit) {
2133                 ok = minmax_verts(obedit, min, max);    /* only selected */
2134         }
2135         else if (ob && (ob->mode & OB_MODE_POSE)) {
2136                 if (ob->pose) {
2137                         bArmature *arm= ob->data;
2138                         bPoseChannel *pchan;
2139                         float vec[3];
2140
2141                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2142                                 if (pchan->bone->flag & BONE_SELECTED) {
2143                                         if (pchan->bone->layer & arm->layer) {
2144                                                 bPoseChannel *pchan_tx= pchan->custom_tx ? pchan->custom_tx : pchan;
2145                                                 ok= 1;
2146                                                 mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_head);
2147                                                 DO_MINMAX(vec, min, max);
2148                                                 mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_tail);
2149                                                 DO_MINMAX(vec, min, max);
2150                                         }
2151                                 }
2152                         }
2153                 }
2154         }
2155         else if (paint_facesel_test(ob)) {
2156                 ok= paintface_minmax(ob, min, max);
2157         }
2158         else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
2159                 ok= PE_minmax(scene, min, max);
2160         }
2161         else {
2162                 Base *base;
2163                 for (base= FIRSTBASE; base; base = base->next) {
2164                         if (TESTBASE(v3d, base)) {
2165
2166                                 if (skip_camera && base->object == v3d->camera) {
2167                                         continue;
2168                                 }
2169
2170                                 /* account for duplis */
2171                                 if (minmax_object_duplis(scene, base->object, min, max)==0)
2172                                         minmax_object(base->object, min, max); /* use if duplis not found */
2173
2174                                 ok= 1;
2175                         }
2176                 }
2177         }
2178
2179         if (ok==0) return OPERATOR_FINISHED;
2180
2181         sub_v3_v3v3(afm, max, min);
2182         size= MAX3(afm[0], afm[1], afm[2]);
2183
2184         if (!rv3d->is_persp) {
2185                 if (size < 0.0001f) { /* if its a sinble point. dont even re-scale */
2186                         ok_dist= 0;
2187                 }
2188                 else {
2189                         /* perspective should be a bit farther away to look nice */
2190                         size*= 0.7f;
2191                 }
2192         }
2193         else {
2194                 if (size <= v3d->near*1.5f) {
2195                         size= v3d->near*1.5f;
2196                 }
2197         }
2198
2199         add_v3_v3v3(new_ofs, min, max);
2200         mul_v3_fl(new_ofs, -0.5f);
2201
2202         new_dist = size;
2203
2204         /* correction for window aspect ratio */
2205         if (ar->winy>2 && ar->winx>2) {
2206                 size= (float)ar->winx/(float)ar->winy;
2207                 if (size<1.0f) size= 1.0f/size;
2208                 new_dist*= size;
2209         }
2210
2211         if (rv3d->persp==RV3D_CAMOB && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2212                 rv3d->persp= RV3D_PERSP;
2213                 smooth_view(C, v3d, ar, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
2214         }
2215         else {
2216                 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, ok_dist ? &new_dist : NULL, NULL);
2217         }
2218
2219         /* smooth view does viewlock RV3D_BOXVIEW copy */
2220         
2221 // XXX  BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2222
2223         return OPERATOR_FINISHED;
2224 }
2225
2226 void VIEW3D_OT_view_selected(wmOperatorType *ot)
2227 {
2228
2229         /* identifiers */
2230         ot->name= "View Selected";
2231         ot->description = "Move the view to the selection center";
2232         ot->idname= "VIEW3D_OT_view_selected";
2233
2234         /* api callbacks */
2235         ot->exec= viewselected_exec;
2236         ot->poll= ED_operator_region_view3d_active;
2237
2238         /* flags */
2239         ot->flag= 0;
2240 }
2241
2242 static int viewcenter_cursor_exec(bContext *C, wmOperator *UNUSED(op))
2243 {
2244         View3D *v3d = CTX_wm_view3d(C);
2245         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2246         Scene *scene= CTX_data_scene(C);
2247         
2248         if (rv3d) {
2249                 ARegion *ar= CTX_wm_region(C);
2250
2251                 /* non camera center */
2252                 float new_ofs[3];
2253                 negate_v3_v3(new_ofs, give_cursor(scene, v3d));
2254                 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, NULL, NULL);
2255                 
2256                 /* smooth view does viewlock RV3D_BOXVIEW copy */
2257         }
2258         
2259         return OPERATOR_FINISHED;
2260 }
2261
2262 void VIEW3D_OT_view_center_cursor(wmOperatorType *ot)
2263 {
2264         /* identifiers */
2265         ot->name= "Center View to Cursor";
2266         ot->description= "Center the view so that the cursor is in the middle of the view";
2267         ot->idname= "VIEW3D_OT_view_center_cursor";
2268         
2269         /* api callbacks */
2270         ot->exec= viewcenter_cursor_exec;
2271         ot->poll= ED_operator_view3d_active;
2272         
2273         /* flags */
2274         ot->flag= 0;
2275 }
2276
2277 static int view3d_center_camera_exec(bContext *C, wmOperator *UNUSED(op)) /* was view3d_home() in 2.4x */
2278 {
2279         Scene *scene= CTX_data_scene(C);
2280         float xfac, yfac;
2281         float size[2];
2282
2283         View3D *v3d;
2284         ARegion *ar;
2285         RegionView3D *rv3d;
2286
2287         /* no NULL check is needed, poll checks */
2288         ED_view3d_context_user_region(C, &v3d, &ar);
2289         rv3d = ar->regiondata;
2290
2291         rv3d->camdx= rv3d->camdy= 0.0f;
2292
2293         ED_view3d_calc_camera_border_size(scene, ar, v3d, rv3d, size);
2294
2295         /* 4px is just a little room from the edge of the area */
2296         xfac= (float)ar->winx / (float)(size[0] + 4);
2297         yfac= (float)ar->winy / (float)(size[1] + 4);
2298
2299         rv3d->camzoom= BKE_screen_view3d_zoom_from_fac(MIN2(xfac, yfac));
2300         CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2301
2302         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2303
2304         return OPERATOR_FINISHED;
2305 }
2306
2307 void VIEW3D_OT_view_center_camera(wmOperatorType *ot)
2308 {
2309         /* identifiers */
2310         ot->name= "View Camera Center";
2311         ot->description = "Center the camera view";
2312         ot->idname= "VIEW3D_OT_view_center_camera";
2313
2314         /* api callbacks */
2315         ot->exec= view3d_center_camera_exec;
2316         ot->poll= view3d_camera_user_poll;
2317
2318         /* flags */
2319         ot->flag= 0;
2320 }
2321
2322 /* ********************* Set render border operator ****************** */
2323
2324 static int render_border_exec(bContext *C, wmOperator *op)
2325 {
2326         View3D *v3d = CTX_wm_view3d(C);
2327         ARegion *ar= CTX_wm_region(C);
2328         RegionView3D *rv3d= ED_view3d_context_rv3d(C);
2329         Scene *scene= CTX_data_scene(C);
2330
2331         rcti rect;
2332         rctf vb;
2333
2334         /* get border select values using rna */
2335         rect.xmin= RNA_int_get(op->ptr, "xmin");
2336         rect.ymin= RNA_int_get(op->ptr, "ymin");
2337         rect.xmax= RNA_int_get(op->ptr, "xmax");
2338         rect.ymax= RNA_int_get(op->ptr, "ymax");
2339
2340         /* calculate range */
2341         ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &vb, FALSE);
2342
2343         scene->r.border.xmin= ((float)rect.xmin-vb.xmin)/(vb.xmax-vb.xmin);
2344         scene->r.border.ymin= ((float)rect.ymin-vb.ymin)/(vb.ymax-vb.ymin);
2345         scene->r.border.xmax= ((float)rect.xmax-vb.xmin)/(vb.xmax-vb.xmin);
2346         scene->r.border.ymax= ((float)rect.ymax-vb.ymin)/(vb.ymax-vb.ymin);
2347
2348         /* actually set border */
2349         CLAMP(scene->r.border.xmin, 0.0f, 1.0f);
2350         CLAMP(scene->r.border.ymin, 0.0f, 1.0f);
2351         CLAMP(scene->r.border.xmax, 0.0f, 1.0f);
2352         CLAMP(scene->r.border.ymax, 0.0f, 1.0f);
2353
2354         /* drawing a border surrounding the entire camera view switches off border rendering
2355          * or the border covers no pixels */
2356         if ((scene->r.border.xmin <= 0.0f && scene->r.border.xmax >= 1.0f &&
2357                 scene->r.border.ymin <= 0.0f && scene->r.border.ymax >= 1.0f) ||
2358            (scene->r.border.xmin == scene->r.border.xmax ||
2359                 scene->r.border.ymin == scene->r.border.ymax ))
2360         {
2361                 scene->r.mode &= ~R_BORDER;
2362         }
2363         else {
2364                 scene->r.mode |= R_BORDER;
2365         }
2366         
2367         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, NULL);
2368
2369         return OPERATOR_FINISHED;
2370
2371 }
2372
2373 void VIEW3D_OT_render_border(wmOperatorType *ot)
2374 {
2375         /* identifiers */
2376         ot->name= "Set Render Border";
2377         ot->description = "Set the boundaries of the border render and enables border render";
2378         ot->idname= "VIEW3D_OT_render_border";
2379
2380         /* api callbacks */
2381         ot->invoke= WM_border_select_invoke;
2382         ot->exec= render_border_exec;
2383         ot->modal= WM_border_select_modal;
2384         ot->cancel= WM_border_select_cancel;
2385
2386         ot->poll= view3d_camera_active_poll;
2387
2388         /* flags */
2389         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2390
2391         /* rna */
2392         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2393         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2394         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2395         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2396
2397 }
2398 /* ********************* Border Zoom operator ****************** */
2399
2400 static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
2401 {
2402         ARegion *ar= CTX_wm_region(C);
2403         View3D *v3d = CTX_wm_view3d(C);
2404         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2405         Scene *scene= CTX_data_scene(C);
2406
2407         /* Zooms in on a border drawn by the user */
2408         rcti rect;
2409         float dvec[3], vb[2], xscale, yscale, scale;
2410
2411         /* SMOOTHVIEW */
2412         float new_dist;
2413         float new_ofs[3];
2414
2415         /* ZBuffer depth vars */
2416         bglMats mats;
2417         float depth_close= FLT_MAX;
2418         double cent[2],  p[3];
2419
2420         /* note; otherwise opengl won't work */
2421         view3d_operator_needs_opengl(C);
2422
2423         /* get border select values using rna */
2424         rect.xmin= RNA_int_get(op->ptr, "xmin");
2425         rect.ymin= RNA_int_get(op->ptr, "ymin");
2426         rect.xmax= RNA_int_get(op->ptr, "xmax");
2427         rect.ymax= RNA_int_get(op->ptr, "ymax");
2428
2429         /* Get Z Depths, needed for perspective, nice for ortho */
2430         bgl_get_mats(&mats);
2431         draw_depth(scene, ar, v3d, NULL);
2432         
2433         {
2434                 /* avoid allocating the whole depth buffer */
2435                 ViewDepths depth_temp= {0};
2436
2437                 /* avoid view3d_update_depths() for speed. */
2438                 view3d_update_depths_rect(ar, &depth_temp, &rect);
2439         
2440                 /* find the closest Z pixel */
2441                 depth_close= view3d_depth_near(&depth_temp);
2442         
2443                 MEM_freeN(depth_temp.depths);
2444         }
2445
2446         cent[0] = (((double)rect.xmin)+((double)rect.xmax)) / 2;
2447         cent[1] = (((double)rect.ymin)+((double)rect.ymax)) / 2;
2448
2449         if (rv3d->is_persp) {
2450                 double p_corner[3];
2451
2452                 /* no depths to use, we cant do anything! */
2453                 if (depth_close==FLT_MAX) {
2454                         BKE_report(op->reports, RPT_ERROR, "Depth Too Large");
2455                         return OPERATOR_CANCELLED;
2456                 }
2457                 /* convert border to 3d coordinates */
2458                 if ( (!gluUnProject(cent[0], cent[1], depth_close,
2459                                     mats.modelview, mats.projection, (GLint *)mats.viewport,
2460                                     &p[0], &p[1], &p[2])) ||
2461                      (!gluUnProject((double)rect.xmin, (double)rect.ymin, depth_close,
2462                                         mats.modelview, mats.projection, (GLint *)mats.viewport,
2463                                         &p_corner[0], &p_corner[1], &p_corner[2])))
2464                 {
2465                         return OPERATOR_CANCELLED;
2466                 }
2467
2468                 dvec[0] = p[0]-p_corner[0];
2469                 dvec[1] = p[1]-p_corner[1];
2470                 dvec[2] = p[2]-p_corner[2];
2471
2472                 new_dist = len_v3(dvec);
2473                 if (new_dist <= v3d->near * 1.5f) new_dist= v3d->near * 1.5f;
2474
2475                 new_ofs[0] = -p[0];
2476                 new_ofs[1] = -p[1];
2477                 new_ofs[2] = -p[2];
2478
2479         }
2480         else { /* othographic */
2481                 /* find the current window width and height */
2482                 vb[0] = ar->winx;
2483                 vb[1] = ar->winy;
2484
2485                 new_dist = rv3d->dist;
2486
2487                 /* convert the drawn rectangle into 3d space */
2488                 if (depth_close != FLT_MAX && gluUnProject(cent[0], cent[1], depth_close,
2489                                                            mats.modelview, mats.projection, (GLint *)mats.viewport,
2490                                                            &p[0], &p[1], &p[2]))
2491                 {
2492                         new_ofs[0] = -p[0];
2493                         new_ofs[1] = -p[1];
2494                         new_ofs[2] = -p[2];
2495                 }
2496                 else {
2497                         float mval_f[2];
2498                         /* We cant use the depth, fallback to the old way that dosnt set the center depth */
2499                         copy_v3_v3(new_ofs, rv3d->ofs);
2500
2501                         initgrabz(rv3d, -new_ofs[0], -new_ofs[1], -new_ofs[2]);
2502
2503                         mval_f[0]= (rect.xmin + rect.xmax - vb[0]) / 2.0f;
2504                         mval_f[1]= (rect.ymin + rect.ymax - vb[1]) / 2.0f;
2505                         ED_view3d_win_to_delta(ar, mval_f, dvec);
2506                         /* center the view to the center of the rectangle */
2507                         sub_v3_v3(new_ofs, dvec);
2508                 }
2509
2510                 /* work out the ratios, so that everything selected fits when we zoom */
2511                 xscale = ((rect.xmax-rect.xmin)/vb[0]);
2512                 yscale = ((rect.ymax-rect.ymin)/vb[1]);
2513                 scale = (xscale >= yscale)?xscale:yscale;
2514
2515                 /* zoom in as required, or as far as we can go */
2516                 new_dist = ((new_dist*scale) >= 0.001f * v3d->grid)? new_dist*scale:0.001f * v3d->grid;
2517         }
2518
2519         smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2520
2521         if (rv3d->viewlock & RV3D_BOXVIEW)
2522                 view3d_boxview_sync(CTX_wm_area(C), ar);
2523
2524         return OPERATOR_FINISHED;
2525 }
2526
2527 static int view3d_zoom_border_invoke(bContext *C, wmOperator *op, wmEvent *event)
2528 {
2529         View3D *v3d= CTX_wm_view3d(C);
2530         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2531
2532         /* if in camera view do not exec the operator so we do not conflict with set render border*/
2533         if ((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d))
2534                 return WM_border_select_invoke(C, op, event);
2535         else
2536                 return OPERATOR_PASS_THROUGH;
2537 }
2538
2539 void VIEW3D_OT_zoom_border(wmOperatorType *ot)
2540 {
2541         /* identifiers */
2542         ot->name= "Border Zoom";
2543         ot->description = "Zoom in the view to the nearest object contained in the border";
2544         ot->idname= "VIEW3D_OT_zoom_border";
2545
2546         /* api callbacks */
2547         ot->invoke= view3d_zoom_border_invoke;
2548         ot->exec= view3d_zoom_border_exec;
2549         ot->modal= WM_border_select_modal;
2550         ot->cancel= WM_border_select_cancel;
2551
2552         ot->poll= ED_operator_region_view3d_active;
2553
2554         /* flags */
2555         ot->flag= 0;
2556
2557         /* rna */
2558         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2559         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2560         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2561         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2562
2563 }
2564
2565 /* sets the view to 1:1 camera/render-pixel */
2566 static void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar, View3D *v3d)
2567 {
2568         RegionView3D *rv3d= ar->regiondata;
2569         float size[2];
2570         int im_width= (scene->r.size*scene->r.xsch)/100;
2571         
2572         ED_view3d_calc_camera_border_size(scene, ar, v3d, rv3d, size);
2573
2574         rv3d->camzoom= BKE_screen_view3d_zoom_from_fac((float)im_width/size[0]);
2575         CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2576 }
2577
2578 static int view3d_zoom_1_to_1_camera_exec(bContext *C, wmOperator *UNUSED(op))
2579 {
2580         Scene *scene= CTX_data_scene(C);
2581
2582         View3D *v3d;
2583         ARegion *ar;
2584
2585         /* no NULL check is needed, poll checks */
2586         ED_view3d_context_user_region(C, &v3d, &ar);
2587
2588         view3d_set_1_to_1_viewborder(scene, ar, v3d);
2589
2590         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
2591
2592         return OPERATOR_FINISHED;
2593 }
2594
2595 void VIEW3D_OT_zoom_camera_1_to_1(wmOperatorType *ot)
2596 {
2597         /* identifiers */
2598         ot->name= "Zoom Camera 1:1";
2599         ot->description = "Match the camera to 1:1 to the render output";
2600         ot->idname= "VIEW3D_OT_zoom_camera_1_to_1";
2601
2602         /* api callbacks */
2603         ot->exec= view3d_zoom_1_to_1_camera_exec;
2604         ot->poll= view3d_camera_user_poll;
2605
2606         /* flags */
2607         ot->flag= 0;
2608 }
2609
2610 /* ********************* Changing view operator ****************** */
2611
2612 static EnumPropertyItem prop_view_items[] = {
2613         {RV3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
2614         {RV3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
2615         {RV3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
2616         {RV3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
2617         {RV3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
2618         {RV3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
2619         {RV3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active camera"},
2620         {0, NULL, 0, NULL, NULL}};
2621
2622
2623 /* would like to make this a generic function - outside of transform */
2624
2625 static void axis_set_view(bContext *C, View3D *v3d, ARegion *ar,
2626                           float q1, float q2, float q3, float q4,
2627                           short view, int perspo, int align_active)
2628 {
2629         RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2630         float new_quat[4];
2631
2632         new_quat[0]= q1; new_quat[1]= q2;
2633         new_quat[2]= q3; new_quat[3]= q4;
2634         normalize_qt(new_quat);
2635
2636         if (align_active) {
2637                 /* align to active object */
2638                 Object *obact= CTX_data_active_object(C);
2639                 if (obact==NULL) {
2640                         /* no active object, ignore this option */
2641                         align_active= FALSE;
2642                 }
2643                 else {
2644                         float obact_quat[4];
2645                         float twmat[3][3];
2646
2647                         /* same as transform manipulator when normal is set */
2648                         ED_getTransformOrientationMatrix(C, twmat, FALSE);
2649
2650                         mat3_to_quat( obact_quat,twmat);
2651                         invert_qt(obact_quat);
2652                         mul_qt_qtqt(new_quat, new_quat, obact_quat);
2653
2654                         rv3d->view= view= RV3D_VIEW_USER;
2655                 }
2656         }
2657
2658         if (align_active==FALSE) {
2659                 /* normal operation */
2660                 if (rv3d->viewlock) {
2661                         /* only pass on if */
2662                         if (rv3d->view==RV3D_VIEW_FRONT && view==RV3D_VIEW_BACK);
2663                         else if (rv3d->view==RV3D_VIEW_BACK && view==RV3D_VIEW_FRONT);
2664                         else if (rv3d->view==RV3D_VIEW_RIGHT && view==RV3D_VIEW_LEFT);
2665                         else if (rv3d->view==RV3D_VIEW_LEFT && view==RV3D_VIEW_RIGHT);
2666                         else if (rv3d->view==RV3D_VIEW_BOTTOM && view==RV3D_VIEW_TOP);
2667                         else if (rv3d->view==RV3D_VIEW_TOP && view==RV3D_VIEW_BOTTOM);
2668                         else return;
2669                 }
2670
2671                 rv3d->view= view;
2672         }
2673
2674         if (rv3d->viewlock) {
2675                 ED_region_tag_redraw(ar);
2676                 return;
2677         }
2678
2679         if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
2680
2681                 if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
2682                 else if (rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
2683
2684                 smooth_view(C, v3d, ar, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
2685         }
2686         else {
2687
2688                 if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
2689                 else if (rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
2690
2691                 smooth_view(C, v3d, ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2692         }
2693
2694 }
2695
2696 static int viewnumpad_exec(bContext *C, wmOperator *op)
2697 {
2698         View3D *v3d;
2699         ARegion *ar;
2700         RegionView3D *rv3d;
2701         Scene *scene= CTX_data_scene(C);
2702         static int perspo = RV3D_PERSP;
2703         int viewnum, align_active, nextperspo;
2704
2705         /* no NULL check is needed, poll checks */
2706         ED_view3d_context_user_region(C, &v3d, &ar);
2707         rv3d = ar->regiondata;
2708
2709         viewnum = RNA_enum_get(op->ptr, "type");
2710         align_active = RNA_boolean_get(op->ptr, "align_active");
2711
2712         /* set this to zero, gets handled in axis_set_view */
2713         if (rv3d->viewlock)
2714                 align_active= 0;
2715
2716         /* Use this to test if we started out with a camera */
2717
2718         if (rv3d->persp == RV3D_CAMOB) {
2719                 nextperspo= rv3d->lpersp;
2720         }
2721         else {
2722                 nextperspo= perspo;
2723         }
2724
2725         switch (viewnum) {
2726                 case RV3D_VIEW_BOTTOM :
2727                         axis_set_view(C, v3d, ar, 0.0, -1.0, 0.0, 0.0,
2728                                       viewnum, nextperspo, align_active);
2729                         break;
2730
2731                 case RV3D_VIEW_BACK:
2732                         axis_set_view(C, v3d, ar, 0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0),
2733                                       viewnum, nextperspo, align_active);
2734                         break;
2735
2736                 case RV3D_VIEW_LEFT:
2737                         axis_set_view(C, v3d, ar, 0.5, -0.5, 0.5, 0.5,
2738                                       viewnum, nextperspo, align_active);
2739                         break;
2740
2741                 case RV3D_VIEW_TOP:
2742                         axis_set_view(C, v3d, ar, 1.0, 0.0, 0.0, 0.0,
2743                                       viewnum, nextperspo, align_active);
2744                         break;
2745
2746                 case RV3D_VIEW_FRONT:
2747                         axis_set_view(C, v3d, ar, (float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0,
2748                                       viewnum, nextperspo, align_active);
2749                         break;
2750
2751                 case RV3D_VIEW_RIGHT:
2752                         axis_set_view(C, v3d, ar, 0.5, -0.5, -0.5, -0.5,
2753                                       viewnum, nextperspo, align_active);
2754                         break;
2755
2756                 case RV3D_VIEW_CAMERA:
2757                         if (rv3d->viewlock==0) {
2758                                 /* lastview -  */
2759
2760                                 if (rv3d->persp != RV3D_CAMOB) {
2761                                         Object *ob= OBACT;
2762
2763                                         if (!rv3d->smooth_timer) {
2764                                                 /* store settings of current view before allowing overwriting with camera view
2765                                                  * only if we're not currently in a view transition */
2766                                                 copy_qt_qt(rv3d->lviewquat, rv3d->viewquat);
2767                                                 rv3d->lview= rv3d->view;
2768                                                 rv3d->lpersp= rv3d->persp;
2769                                         }
2770
2771         #if 0
2772                                         if (G.qual==LR_ALTKEY) {
2773                                                 if (oldcamera && is_an_active_object(oldcamera)) {
2774                                                         v3d->camera= oldcamera;
2775                                                 }
2776                                                 handle_view3d_lock();
2777                                         }
2778         #endif
2779                                         
2780                                         /* first get the default camera for the view lock type */
2781                                         if (v3d->scenelock) {
2782                                                 /* sets the camera view if available */
2783                                                 v3d->camera= scene->camera;                                             
2784                                         }
2785                                         else {
2786                                                 /* use scene camera if one is not set (even though we're unlocked) */
2787                                                 if (v3d->camera==NULL) {
2788                                                         v3d->camera= scene->camera;
2789                                                 }
2790                                         }
2791
2792                                         /* if the camera isnt found, check a number of options */
2793                                         if (v3d->camera==NULL && ob && ob->type==OB_CAMERA)
2794                                                 v3d->camera= ob;
2795                                         
2796                                         if (v3d->camera==NULL)
2797                                                 v3d->camera= scene_find_camera(scene);          
2798
2799                                         /* couldnt find any useful camera, bail out */
2800                                         if (v3d->camera==NULL)
2801                                                 return OPERATOR_CANCELLED;
2802                                         
2803                                         /* important these dont get out of sync for locked scenes */
2804                                         if (v3d->scenelock)
2805                                                 scene->camera= v3d->camera;
2806
2807                                         /* finally do snazzy view zooming */
2808                                         rv3d->persp= RV3D_CAMOB;
2809                                         smooth_view(C, v3d, ar, NULL, v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens);
2810
2811                                 }
2812                                 else{
2813                                         /* return to settings of last view */
2814                                         /* does smooth_view too */
2815                                         axis_set_view(C, v3d, ar,
2816                                                       rv3d->lviewquat[0], rv3d->lviewquat[1], rv3d->lviewquat[2], rv3d->lviewquat[3],
2817                                                       rv3d->lview, rv3d->lpersp, 0);
2818                                 }
2819                         }
2820                         break;
2821
2822                 default :
2823                         break;
2824         }
2825
2826         if (rv3d->persp != RV3D_CAMOB) perspo= rv3d->persp;
2827
2828         return OPERATOR_FINISHED;
2829 }
2830
2831
2832 void VIEW3D_OT_viewnumpad(wmOperatorType *ot)
2833 {
2834         /* identifiers */
2835         ot->name= "View numpad";
2836         ot->description = "Set the view";
2837         ot->idname= "VIEW3D_OT_viewnumpad";
2838
2839         /* api callbacks */
2840         ot->exec= viewnumpad_exec;
2841         ot->poll= ED_operator_rv3d_user_region_poll;
2842
2843         /* flags */
2844         ot->flag= 0;
2845
2846         RNA_def_enum(ot->srna, "type", prop_view_items, 0, "View", "The Type of view");
2847         RNA_def_boolean(ot->srna, "align_active", 0, "Align Active", "Align to the active object's axis");
2848 }
2849
2850 static EnumPropertyItem prop_view_orbit_items[] = {
2851         {V3D_VIEW_STEPLEFT, "ORBITLEFT", 0, "Orbit Left", "Orbit the view around to the Left"},
2852         {V3D_VIEW_STEPRIGHT, "ORBITRIGHT", 0, "Orbit Right", "Orbit the view around to the Right"},
2853         {V3D_VIEW_STEPUP, "ORBITUP", 0, "Orbit Up", "Orbit the view Up"},
2854         {V3D_VIEW_STEPDOWN, "ORBITDOWN", 0, "Orbit Down", "Orbit the view Down"},
2855         {0, NULL, 0, NULL, NULL}};
2856
2857 static int vieworbit_exec(bContext *C, wmOperator *op)
2858 {
2859         View3D *v3d;
2860         ARegion *ar;
2861         RegionView3D *rv3d;
2862         float phi, q1[4], new_quat[4];
2863         int orbitdir;
2864
2865         /* no NULL check is needed, poll checks */
2866         ED_view3d_context_user_region(C, &v3d, &ar);
2867         rv3d = ar->regiondata;
2868
2869         orbitdir = RNA_enum_get(op->ptr, "type");
2870
2871         if (rv3d->viewlock==0) {
2872                 if ((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d)) {
2873                         if (orbitdir == V3D_VIEW_STEPLEFT || orbitdir == V3D_VIEW_STEPRIGHT) {
2874                                 float si;
2875                                 /* z-axis */
2876                                 phi= (float)(M_PI/360.0)*U.pad_rot_angle;
2877                                 if (orbitdir == V3D_VIEW_STEPRIGHT) phi= -phi;
2878                                 si= (float)sin(phi);
2879                                 q1[0]= (float)cos(phi);
2880                                 q1[1]= q1[2]= 0.0;
2881                                 q1[3]= si;
2882                                 mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
2883                                 rv3d->view= RV3D_VIEW_USER;
2884                         }
2885                         else if (orbitdir == V3D_VIEW_STEPDOWN || orbitdir == V3D_VIEW_STEPUP) {
2886                                 /* horizontal axis */
2887                                 copy_v3_v3(q1+1, rv3d->viewinv[0]);
2888
2889                                 normalize_v3(q1+1);
2890                                 phi= (float)(M_PI/360.0)*U.pad_rot_angle;
2891                                 if (orbitdir == V3D_VIEW_STEPDOWN) phi= -phi;
2892                                 q1[0]= (float)cos(phi);
2893                                 mul_v3_fl(q1+1, sin(phi));
2894                                 mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
2895                                 rv3d->view= RV3D_VIEW_USER;
2896                         }
2897
2898                         smooth_view(C, CTX_wm_view3d(C), ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2899                 }
2900         }
2901
2902         return OPERATOR_FINISHED;
2903 }
2904
2905 void VIEW3D_OT_view_orbit(wmOperatorType *ot)
2906 {
2907         /* identifiers */
2908         ot->name= "View Orbit";
2909         ot->description = "Orbit the view";
2910         ot->idname= "VIEW3D_OT_view_orbit";
2911
2912         /* api callbacks */
2913         ot->exec= vieworbit_exec;
2914         ot->poll= ED_operator_rv3d_user_region_poll;
2915
2916         /* flags */
2917         ot->flag= 0;
2918         RNA_def_enum(ot->srna, "type", prop_view_orbit_items, 0, "Orbit", "Direction of View Orbit");
2919 }
2920
2921 static EnumPropertyItem prop_view_pan_items[] = {
2922         {V3D_VIEW_PANLEFT, "PANLEFT", 0, "Pan Left", "Pan the view to the Left"},
2923         {V3D_VIEW_PANRIGHT, "PANRIGHT", 0, "Pan Right", "Pan the view to the Right"},
2924         {V3D_VIEW_PANUP, "PANUP", 0, "Pan Up", "Pan the view Up"},
2925         {V3D_VIEW_PANDOWN, "PANDOWN", 0, "Pan Down", "Pan the view Down"},
2926         {0, NULL, 0, NULL, NULL}};
2927
2928 static int viewpan_exec(bContext *C, wmOperator *op)
2929 {
2930         ARegion *ar= CTX_wm_region(C);
2931         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2932         float vec[3];
2933         float mval_f[2]= {0.0f, 0.0f};
2934         int pandir;
2935
2936         pandir = RNA_enum_get(op->ptr, "type");
2937
2938         initgrabz(rv3d, 0.0, 0.0, 0.0);
2939         if (pandir == V3D_VIEW_PANRIGHT)                { mval_f[0]= -32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2940         else if (pandir == V3D_VIEW_PANLEFT)    { mval_f[0]=  32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2941         else if (pandir == V3D_VIEW_PANUP)      { mval_f[1]= -25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2942         else if (pandir == V3D_VIEW_PANDOWN)    { mval_f[1]=  25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2943         add_v3_v3(rv3d->ofs, vec);
2944
2945         if (rv3d->viewlock & RV3D_BOXVIEW)
2946                 view3d_boxview_sync(CTX_wm_area(C), ar);
2947
2948         ED_region_tag_redraw(ar);
2949
2950         return OPERATOR_FINISHED;
2951 }
2952
2953 void VIEW3D_OT_view_pan(wmOperatorType *ot)
2954 {
2955         /* identifiers */
2956         ot->name= "View Pan";
2957         ot->description = "Pan the view";
2958         ot->idname= "VIEW3D_OT_view_pan";
2959
2960         /* api callbacks */
2961         ot->exec= viewpan_exec;
2962         ot->poll= ED_operator_region_view3d_active;
2963
2964         /* flags */
2965         ot->flag= 0;
2966         RNA_def_enum(ot->srna, "type", prop_view_pan_items, 0, "Pan", "Direction of View Pan");
2967 }
2968
2969 static int viewpersportho_exec(bContext *C, wmOperator *UNUSED(op))
2970 {
2971         View3D *v3d_dummy;
2972         ARegion *ar;
2973         RegionView3D *rv3d;
2974
2975         /* no NULL check is needed, poll checks */
2976         ED_view3d_context_user_region(C, &v3d_dummy, &ar);
2977         rv3d = ar->regiondata;
2978
2979         if (rv3d->viewlock==0) {
2980                 if (rv3d->persp!=RV3D_ORTHO)
2981                         rv3d->persp=RV3D_ORTHO;
2982                 else rv3d->persp=RV3D_PERSP;
2983                 ED_region_tag_redraw(ar);
2984         }
2985
2986         return OPERATOR_FINISHED;
2987
2988 }
2989
2990 void VIEW3D_OT_view_persportho(wmOperatorType *ot)
2991 {
2992         /* identifiers */
2993         ot->name= "View Persp/Ortho";
2994         ot->description = "Switch the current view from perspective/orthographic";
2995         ot->idname= "VIEW3D_OT_view_persportho";
2996
2997         /* api callbacks */
2998         ot->exec= viewpersportho_exec;
2999         ot->poll= ED_operator_rv3d_user_region_poll;
3000
3001         /* flags */
3002         ot->flag= 0;
3003 }
3004
3005
3006 /* ******************** add background image operator **************** */
3007
3008 static BGpic *background_image_add(bContext *C)
3009 {
3010         View3D *v3d= CTX_wm_view3d(C);
3011
3012         return ED_view3D_background_image_new(v3d);
3013 }
3014
3015 static int background_image_add_exec(bContext *C, wmOperator *UNUSED(op))
3016 {
3017         background_image_add(C);
3018
3019         return OPERATOR_FINISHED;
3020 }
3021
3022 static int background_image_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
3023 {
3024         View3D *v3d= CTX_wm_view3d(C);
3025         Image *ima= NULL;
3026         BGpic *bgpic;
3027         char name[MAX_ID_NAME-2];
3028         
3029         /* check input variables */
3030         if (RNA_struct_property_is_set(op->ptr, "filepath")) {
3031                 char path[FILE_MAX];
3032                 
3033                 RNA_string_get(op->ptr, "filepath", path);
3034                 ima= BKE_add_image_file(path);
3035         }
3036         else if (RNA_struct_property_is_set(op->ptr, "name")) {
3037                 RNA_string_get(op->ptr, "name", name);
3038                 ima= (Image *)find_id("IM", name);
3039         }
3040         
3041         bgpic = background_image_add(C);
3042         
3043         if (ima) {
3044                 bgpic->ima = ima;
3045                 
3046                 if (ima->id.us==0) id_us_plus(&ima->id);
3047                 else id_lib_extern(&ima->id);
3048                 
3049                 if (!(v3d->flag & V3D_DISPBGPICS))
3050                         v3d->flag |= V3D_DISPBGPICS;
3051         }
3052         
3053         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
3054         
3055         return OPERATOR_FINISHED;
3056 }
3057
3058 void VIEW3D_OT_background_image_add(wmOperatorType *ot)
3059 {
3060         /* identifiers */
3061         ot->name   = "Add Background Image";
3062         ot->description= "Add a new background image";
3063         ot->idname = "VIEW3D_OT_background_image_add";
3064
3065         /* api callbacks */
3066         ot->invoke = background_image_add_invoke;
3067         ot->exec   = background_image_add_exec;
3068         ot->poll   = ED_operator_view3d_active;
3069
3070         /* flags */
3071         ot->flag   = 0;
3072         
3073         /* properties */
3074         RNA_def_string(ot->srna, "name", "Image", MAX_ID_NAME-2, "Name", "Image name to assign");
3075         RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
3076 }
3077
3078
3079 /* ***** remove image operator ******* */
3080 static int background_image_remove_exec(bContext *C, wmOperator *op)
3081 {
3082         View3D *v3d = CTX_wm_view3d(C);
3083         int index = RNA_int_get(op->ptr, "index");
3084         BGpic *bgpic_rem= BLI_findlink(&v3d->bgpicbase, index);
3085
3086         if (bgpic_rem) {
3087                 ED_view3D_background_image_remove(v3d, bgpic_rem);
3088                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
3089                 return OPERATOR_FINISHED;
3090         }
3091         else {
3092                 return OPERATOR_CANCELLED;
3093         }
3094
3095 }
3096
3097 void VIEW3D_OT_background_image_remove(wmOperatorType *ot)
3098 {
3099         /* identifiers */
3100         ot->name   = "Remove Background Image";
3101         ot->description= "Remove a background image from the 3D view";
3102         ot->idname = "VIEW3D_OT_background_image_remove";
3103
3104         /* api callbacks */
3105         ot->exec   = background_image_remove_exec;
3106         ot->poll   = ED_operator_view3d_active;
3107
3108         /* flags */
3109         ot->flag   = 0;
3110
3111         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Background image index to remove", 0, INT_MAX);
3112 }
3113
3114 /* ********************* set clipping operator ****************** */
3115
3116 static void calc_clipping_plane(float clip[6][4], BoundBox *clipbb)
3117 {
3118         int val;
3119
3120         for (val=0; val<4; val++) {
3121
3122                 normal_tri_v3( clip[val],clipbb->vec[val], clipbb->vec[val==3?0:val+1], clipbb->vec[val+4]);
3123
3124                 clip[val][3]=
3125                         - clip[val][0]*clipbb->vec[val][0]
3126                         - clip[val][1]*clipbb->vec[val][1]
3127                         - clip[val][2]*clipbb->vec[val][2];
3128         }
3129 }
3130
3131 static void calc_local_clipping(float clip_local[][4], BoundBox *clipbb, float mat[][4])
3132 {
3133         BoundBox clipbb_local;
3134         float imat[4][4];
3135    &n