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