code cleanup: replace cos(M_PI / 4) and sin(M_PI / 4) with M_SQRT1_2 define
[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_ORBIT_ZBUF) {
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 (ndof->tz) {
1040                                 /* Zoom!
1041                                  * velocity should be proportional to the linear velocity attained by rotational motion of same strength
1042                                  * [got that?]
1043                                  * proportional to arclength = radius * angle
1044                                  */
1045                                 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tz;
1046
1047                                 if (U.ndof_flag & NDOF_ZOOM_INVERT)
1048                                         zoom_distance = -zoom_distance;
1049
1050                                 rv3d->dist += zoom_distance;
1051                         }
1052
1053                         if (rv3d->viewlock == RV3D_LOCKED) {
1054                                 /* rotation not allowed -- explore panning options instead */
1055                                 float pan_vec[3] = {ndof->tx, ndof->ty, 0.0f};
1056                                 mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
1057
1058                                 /* transform motion from view to world coordinates */
1059                                 invert_qt_qt(view_inv, rv3d->viewquat);
1060                                 mul_qt_v3(view_inv, pan_vec);
1061
1062                                 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1063                                 sub_v3_v3(rv3d->ofs, pan_vec);
1064                         }
1065
1066                         if (has_rotation) {
1067
1068                                 rv3d->view = RV3D_VIEW_USER;
1069
1070                                 if (U.flag & USER_TRACKBALL) {
1071                                         float rot[4];
1072                                         float axis[3];
1073                                         float angle = rot_sensitivity * ndof_to_axis_angle(ndof, axis);
1074
1075                                         if (U.ndof_flag & NDOF_ROLL_INVERT_AXIS)
1076                                                 axis[2] = -axis[2];
1077
1078                                         if (U.ndof_flag & NDOF_TILT_INVERT_AXIS)
1079                                                 axis[0] = -axis[0];
1080
1081                                         if (U.ndof_flag & NDOF_ROTATE_INVERT_AXIS)
1082                                                 axis[1] = -axis[1];
1083
1084                                         /* transform rotation axis from view to world coordinates */
1085                                         mul_qt_v3(view_inv, axis);
1086
1087                                         /* update the onscreen doo-dad */
1088                                         rv3d->rot_angle = angle;
1089                                         copy_v3_v3(rv3d->rot_axis, axis);
1090
1091                                         axis_angle_to_quat(rot, axis, angle);
1092
1093                                         /* apply rotation */
1094                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1095
1096                                 }
1097                                 else {
1098
1099                                         /* turntable view code by John Aughey, adapted for 3D mouse by [mce] */
1100                                         float angle, rot[4];
1101                                         float xvec[3] = {1, 0, 0};
1102
1103                                         /* Determine the direction of the x vector (for rotating up and down) */
1104                                         mul_qt_v3(view_inv, xvec);
1105
1106                                         /* Perform the up/down rotation */
1107                                         angle = rot_sensitivity * dt * ndof->rx;
1108                                         if (U.ndof_flag & NDOF_TILT_INVERT_AXIS)
1109                                                 angle = -angle;
1110                                         rot[0] = cos(angle);
1111                                         mul_v3_v3fl(rot + 1, xvec, sin(angle));
1112                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1113
1114                                         /* Perform the orbital rotation */
1115                                         angle = rot_sensitivity * dt * ndof->ry;
1116                                         if (U.ndof_flag & NDOF_ROTATE_INVERT_AXIS)
1117                                                 angle = -angle;
1118
1119                                         /* update the onscreen doo-dad */
1120                                         rv3d->rot_angle = angle;
1121                                         rv3d->rot_axis[0] = 0;
1122                                         rv3d->rot_axis[1] = 0;
1123                                         rv3d->rot_axis[2] = 1;
1124
1125                                         rot[0] = cos(angle);
1126                                         rot[1] = rot[2] = 0.0;
1127                                         rot[3] = sin(angle);
1128                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1129                                 }
1130                         }
1131                 }
1132
1133                 ED_view3d_camera_lock_sync(v3d, rv3d);
1134
1135                 ED_region_tag_redraw(CTX_wm_region(C));
1136
1137                 return OPERATOR_FINISHED;
1138         }
1139 }
1140
1141 void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot)
1142 {
1143         /* identifiers */
1144         ot->name = "NDOF Orbit View";
1145         ot->description = "Explore every angle of an object using the 3D mouse";
1146         ot->idname = "VIEW3D_OT_ndof_orbit";
1147
1148         /* api callbacks */
1149         ot->invoke = ndof_orbit_invoke;
1150         ot->poll = ED_operator_view3d_active;
1151
1152         /* flags */
1153         ot->flag = 0;
1154 }
1155
1156 /* -- "pan" navigation
1157  * -- zoom or dolly?
1158  */
1159 static int ndof_pan_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
1160 {
1161         if (event->type != NDOF_MOTION)
1162                 return OPERATOR_CANCELLED;
1163         else {
1164                 View3D *v3d = CTX_wm_view3d(C);
1165                 RegionView3D *rv3d = CTX_wm_region_view3d(C);
1166                 wmNDOFMotionData *ndof = (wmNDOFMotionData *) event->customdata;
1167
1168                 ED_view3d_camera_lock_init(v3d, rv3d);
1169
1170                 rv3d->rot_angle = 0.f; /* we're panning here! so erase any leftover rotation from other operators */
1171
1172                 if (ndof->progress != P_FINISHING) {
1173                         const float dt = ndof->dt;
1174                         float view_inv[4];
1175 #if 0 /* ------------------------------------------- zoom with Z */
1176                         /* tune these until everything feels right */
1177                         const float zoom_sensitivity = 1.f;
1178                         const float pan_sensitivity = 1.f;
1179
1180                         float pan_vec[3] = {
1181                                 ndof->tx, ndof->ty, 0
1182                         };
1183
1184                         /* "zoom in" or "translate"? depends on zoom mode in user settings? */
1185                         if (ndof->tz) {
1186                                 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tz;
1187                                 rv3d->dist += zoom_distance;
1188                         }
1189
1190                         mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
1191 #else /* ------------------------------------------------------- dolly with Z */
1192                         float speed = 10.f; /* blender units per second */
1193                         /* ^^ this is ok for default cube scene, but should scale with.. something */
1194
1195                         /* tune these until everything feels right */
1196                         const float forward_sensitivity = 1.f;
1197                         const float vertical_sensitivity = 0.4f;
1198                         const float lateral_sensitivity = 0.6f;
1199
1200                         float pan_vec[3];
1201
1202                         if (U.ndof_flag & NDOF_PANX_INVERT_AXIS)
1203                                 pan_vec[0] = -lateral_sensitivity * ndof->tvec[0];
1204                         else
1205                                 pan_vec[0] = lateral_sensitivity * ndof->tvec[0];
1206
1207                         if (U.ndof_flag & NDOF_PANZ_INVERT_AXIS)
1208                                 pan_vec[1] = -vertical_sensitivity * ndof->tvec[1];
1209                         else
1210                                 pan_vec[1] = vertical_sensitivity * ndof->tvec[1];
1211
1212                         if (U.ndof_flag & NDOF_PANY_INVERT_AXIS)
1213                                 pan_vec[2] = -forward_sensitivity * ndof->tvec[2];
1214                         else
1215                                 pan_vec[2] = forward_sensitivity * ndof->tvec[2];
1216
1217                         mul_v3_fl(pan_vec, speed * dt);
1218 #endif
1219                         /* transform motion from view to world coordinates */
1220                         invert_qt_qt(view_inv, rv3d->viewquat);
1221                         mul_qt_v3(view_inv, pan_vec);
1222
1223                         /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1224                         sub_v3_v3(rv3d->ofs, pan_vec);
1225                 }
1226
1227                 ED_view3d_camera_lock_sync(v3d, rv3d);
1228
1229                 ED_region_tag_redraw(CTX_wm_region(C));
1230
1231                 return OPERATOR_FINISHED;
1232         }
1233 }
1234
1235 void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot)
1236 {
1237         /* identifiers */
1238         ot->name = "NDOF Pan View";
1239         ot->description = "Position your viewpoint with the 3D mouse";
1240         ot->idname = "VIEW3D_OT_ndof_pan";
1241
1242         /* api callbacks */
1243         ot->invoke = ndof_pan_invoke;
1244         ot->poll = ED_operator_view3d_active;
1245
1246         /* flags */
1247         ot->flag = 0;
1248 }
1249
1250 /* ************************ viewmove ******************************** */
1251
1252
1253 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
1254
1255 /* called in transform_ops.c, on each regeneration of keymaps  */
1256 void viewmove_modal_keymap(wmKeyConfig *keyconf)
1257 {
1258         static EnumPropertyItem modal_items[] = {
1259                 {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1260                 
1261                 {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
1262                 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1263
1264                 {0, NULL, 0, NULL, NULL}
1265         };
1266
1267         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Move Modal");
1268
1269         /* this function is called for each spacetype, only needs to add map once */
1270         if (keymap && keymap->modal_items) return;
1271
1272         keymap = WM_modalkeymap_add(keyconf, "View3D Move Modal", modal_items);
1273
1274         /* items for modal map */
1275         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1276         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1277
1278         /* disabled mode switching for now, can re-implement better, later on */
1279 #if 0
1280         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1281         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1282         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1283 #endif
1284         
1285         /* assign map to operators */
1286         WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
1287 }
1288
1289
1290 static void viewmove_apply(ViewOpsData *vod, int x, int y)
1291 {
1292         if ((vod->rv3d->persp == RV3D_CAMOB) && !ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
1293                 const float zoomfac = BKE_screen_view3d_zoom_to_fac((float)vod->rv3d->camzoom) * 2.0f;
1294                 vod->rv3d->camdx += (vod->oldx - x) / (vod->ar->winx * zoomfac);
1295                 vod->rv3d->camdy += (vod->oldy - y) / (vod->ar->winy * zoomfac);
1296                 CLAMP(vod->rv3d->camdx, -1.0f, 1.0f);
1297                 CLAMP(vod->rv3d->camdy, -1.0f, 1.0f);
1298         }
1299         else {
1300                 float dvec[3];
1301                 float mval_f[2];
1302
1303                 mval_f[0] = x - vod->oldx;
1304                 mval_f[1] = y - vod->oldy;
1305                 ED_view3d_win_to_delta(vod->ar, mval_f, dvec);
1306
1307                 add_v3_v3(vod->rv3d->ofs, dvec);
1308
1309                 if (vod->rv3d->viewlock & RV3D_BOXVIEW)
1310                         view3d_boxview_sync(vod->sa, vod->ar);
1311         }
1312
1313         vod->oldx = x;
1314         vod->oldy = y;
1315
1316         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1317
1318         ED_region_tag_redraw(vod->ar);
1319 }
1320
1321
1322 static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
1323 {
1324
1325         ViewOpsData *vod = op->customdata;
1326         short event_code = VIEW_PASS;
1327
1328         /* execute the events */
1329         if (event->type == MOUSEMOVE) {
1330                 event_code = VIEW_APPLY;
1331         }
1332         else if (event->type == EVT_MODAL_MAP) {
1333                 switch (event->val) {
1334                         case VIEW_MODAL_CONFIRM:
1335                                 event_code = VIEW_CONFIRM;
1336                                 break;
1337                         case VIEWROT_MODAL_SWITCH_ZOOM:
1338                                 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
1339                                 event_code = VIEW_CONFIRM;
1340                                 break;
1341                         case VIEWROT_MODAL_SWITCH_ROTATE:
1342                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1343                                 event_code = VIEW_CONFIRM;
1344                                 break;
1345                 }
1346         }
1347         else if (event->type == vod->origkey && event->val == KM_RELEASE) {
1348                 event_code = VIEW_CONFIRM;
1349         }
1350
1351         if (event_code == VIEW_APPLY) {
1352                 viewmove_apply(vod, event->x, event->y);
1353         }
1354         else if (event_code == VIEW_CONFIRM) {
1355                 ED_view3d_depth_tag_update(vod->rv3d);
1356
1357                 viewops_data_free(C, op);
1358
1359                 return OPERATOR_FINISHED;
1360         }
1361
1362         return OPERATOR_RUNNING_MODAL;
1363 }
1364
1365 static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
1366 {
1367         ViewOpsData *vod;
1368
1369         /* makes op->customdata */
1370         viewops_data_create(C, op, event);
1371         vod = op->customdata;
1372
1373         if (event->type == MOUSEPAN) {
1374                 viewmove_apply(vod, event->prevx, event->prevy);
1375                 ED_view3d_depth_tag_update(vod->rv3d);
1376                 
1377                 viewops_data_free(C, op);               
1378                 
1379                 return OPERATOR_FINISHED;
1380         }
1381         else {
1382                 /* add temp handler */
1383                 WM_event_add_modal_handler(C, op);
1384
1385                 return OPERATOR_RUNNING_MODAL;
1386         }
1387 }
1388
1389 static int viewmove_cancel(bContext *C, wmOperator *op)
1390 {
1391         viewops_data_free(C, op);
1392
1393         return OPERATOR_CANCELLED;
1394 }
1395
1396 void VIEW3D_OT_move(wmOperatorType *ot)
1397 {
1398
1399         /* identifiers */
1400         ot->name = "Move view";
1401         ot->description = "Move the view";
1402         ot->idname = "VIEW3D_OT_move";
1403
1404         /* api callbacks */
1405         ot->invoke = viewmove_invoke;
1406         ot->modal = viewmove_modal;
1407         ot->poll = ED_operator_view3d_active;
1408         ot->cancel = viewmove_cancel;
1409
1410         /* flags */
1411         ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
1412 }
1413
1414 /* ************************ viewzoom ******************************** */
1415
1416 /* viewdolly_modal_keymap has an exact copy of this, apply fixes to both */
1417 /* called in transform_ops.c, on each regeneration of keymaps  */
1418 void viewzoom_modal_keymap(wmKeyConfig *keyconf)
1419 {
1420         static EnumPropertyItem modal_items[] = {
1421                 {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1422                 
1423                 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1424                 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1425
1426                 {0, NULL, 0, NULL, NULL}
1427         };
1428
1429         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Zoom Modal");
1430
1431         /* this function is called for each spacetype, only needs to add map once */
1432         if (keymap && keymap->modal_items) return;
1433
1434         keymap = WM_modalkeymap_add(keyconf, "View3D Zoom Modal", modal_items);
1435
1436         /* items for modal map */
1437         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1438         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1439
1440         /* disabled mode switching for now, can re-implement better, later on */
1441 #if 0
1442         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1443         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1444         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
1445 #endif
1446         
1447         /* assign map to operators */
1448         WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
1449 }
1450
1451 static void view_zoom_mouseloc(ARegion *ar, float dfac, int mx, int my)
1452 {
1453         RegionView3D *rv3d = ar->regiondata;
1454
1455         if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
1456                 float dvec[3];
1457                 float tvec[3];
1458                 float tpos[3];
1459                 float mval_f[2];
1460                 float new_dist;
1461
1462                 negate_v3_v3(tpos, rv3d->ofs);
1463
1464                 /* Project cursor position into 3D space */
1465                 initgrabz(rv3d, tpos[0], tpos[1], tpos[2]);
1466
1467                 mval_f[0] = (float)(((mx - ar->winrct.xmin) * 2) - ar->winx) / 2.0f;
1468                 mval_f[1] = (float)(((my - ar->winrct.ymin) * 2) - ar->winy) / 2.0f;
1469                 ED_view3d_win_to_delta(ar, mval_f, dvec);
1470
1471                 /* Calculate view target position for dolly */
1472                 add_v3_v3v3(tvec, tpos, dvec);
1473                 negate_v3(tvec);
1474
1475                 /* Offset to target position and dolly */
1476                 new_dist = rv3d->dist * dfac;
1477
1478                 copy_v3_v3(rv3d->ofs, tvec);
1479                 rv3d->dist = new_dist;
1480
1481                 /* Calculate final offset */
1482                 madd_v3_v3v3fl(rv3d->ofs, tvec, dvec, dfac);
1483         }
1484         else {
1485                 rv3d->dist *= dfac;
1486         }
1487 }
1488
1489
1490 static void viewzoom_apply(ViewOpsData *vod, int x, int y, const short viewzoom, const short zoom_invert)
1491 {
1492         float zfac = 1.0;
1493         short use_cam_zoom;
1494
1495         use_cam_zoom = (vod->rv3d->persp == RV3D_CAMOB) && !(vod->rv3d->is_persp && ED_view3d_camera_lock_check(vod->v3d, vod->rv3d));
1496
1497         if (use_cam_zoom) {
1498                 float delta;
1499                 delta = (x - vod->origx + y - vod->origy) / 10.0f;
1500                 vod->rv3d->camzoom = vod->camzoom0 - delta;
1501
1502                 CLAMP(vod->rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
1503         }
1504         if (viewzoom == USER_ZOOM_CONT) {
1505                 double time = PIL_check_seconds_timer();
1506                 float time_step = (float)(time - vod->timer_lastdraw);
1507                 float fac;
1508
1509                 if (U.uiflag & USER_ZOOM_HORIZ) {
1510                         fac = (float)(x - vod->origx);
1511                 }
1512                 else {
1513                         fac = (float)(y - vod->origy);
1514                 }
1515
1516                 if (zoom_invert) {
1517                         fac = -fac;
1518                 }
1519
1520                 /* oldstyle zoom */
1521                 zfac = 1.0f + ((fac / 20.0f) * time_step);
1522                 vod->timer_lastdraw = time;
1523         }
1524         else if (viewzoom == USER_ZOOM_SCALE) {
1525                 int ctr[2], len1, len2;
1526                 /* method which zooms based on how far you move the mouse */
1527
1528                 ctr[0] = (vod->ar->winrct.xmax + vod->ar->winrct.xmin) / 2;
1529                 ctr[1] = (vod->ar->winrct.ymax + vod->ar->winrct.ymin) / 2;
1530
1531                 len1 = (int)sqrt((ctr[0] - x) * (ctr[0] - x) + (ctr[1] - y) * (ctr[1] - y)) + 5;
1532                 len2 = (int)sqrt((ctr[0] - vod->origx) * (ctr[0] - vod->origx) + (ctr[1] - vod->origy) * (ctr[1] - vod->origy)) + 5;
1533
1534                 zfac = vod->dist0 * ((float)len2 / len1) / vod->rv3d->dist;
1535         }
1536         else {  /* USER_ZOOM_DOLLY */
1537                 float len1, len2;
1538                 
1539                 if (U.uiflag & USER_ZOOM_HORIZ) {
1540                         len1 = (vod->ar->winrct.xmax - x) + 5;
1541                         len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1542                 }
1543                 else {
1544                         len1 = (vod->ar->winrct.ymax - y) + 5;
1545                         len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1546                 }
1547                 if (zoom_invert) {
1548                         SWAP(float, len1, len2);
1549                 }
1550                 
1551                 if (use_cam_zoom) {
1552                         zfac = vod->camzoom0 * (2.0f * ((len2 / len1) - 1.0f) + 1.0f) / vod->rv3d->camzoom;
1553                         zfac = 0;
1554                 }
1555                 else {
1556                         zfac = vod->dist0 * (2.0f * ((len2 / len1) - 1.0f) + 1.0f) / vod->rv3d->dist;
1557                 }
1558         }
1559
1560         if (!use_cam_zoom) {
1561                 if (zfac != 1.0f && zfac * vod->rv3d->dist > 0.001f * vod->grid &&
1562                     zfac * vod->rv3d->dist < 10.0f * vod->far)
1563                 {
1564                         view_zoom_mouseloc(vod->ar, zfac, vod->oldx, vod->oldy);
1565                 }
1566         }
1567
1568         /* these limits were in old code too */
1569         if (vod->rv3d->dist < 0.001f * vod->grid) vod->rv3d->dist = 0.001f * vod->grid;
1570         if (vod->rv3d->dist > 10.0f * vod->far) vod->rv3d->dist = 10.0f * vod->far;
1571
1572         if (vod->rv3d->viewlock & RV3D_BOXVIEW)
1573                 view3d_boxview_sync(vod->sa, vod->ar);
1574
1575         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1576
1577         ED_region_tag_redraw(vod->ar);
1578 }
1579
1580
1581 static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
1582 {
1583         ViewOpsData *vod = op->customdata;
1584         short event_code = VIEW_PASS;
1585
1586         /* execute the events */
1587         if (event->type == TIMER && event->customdata == vod->timer) {
1588                 /* continuous zoom */
1589                 event_code = VIEW_APPLY;
1590         }
1591         else if (event->type == MOUSEMOVE) {
1592                 event_code = VIEW_APPLY;
1593         }
1594         else if (event->type == EVT_MODAL_MAP) {
1595                 switch (event->val) {
1596                         case VIEW_MODAL_CONFIRM:
1597                                 event_code = VIEW_CONFIRM;
1598                                 break;
1599                         case VIEWROT_MODAL_SWITCH_MOVE:
1600                                 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1601                                 event_code = VIEW_CONFIRM;
1602                                 break;
1603                         case VIEWROT_MODAL_SWITCH_ROTATE:
1604                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1605                                 event_code = VIEW_CONFIRM;
1606                                 break;
1607                 }
1608         }
1609         else if (event->type == vod->origkey && event->val == KM_RELEASE) {
1610                 event_code = VIEW_CONFIRM;
1611         }
1612
1613         if (event_code == VIEW_APPLY) {
1614                 viewzoom_apply(vod, event->x, event->y, U.viewzoom, (U.uiflag & USER_ZOOM_INVERT) != 0);
1615         }
1616         else if (event_code == VIEW_CONFIRM) {
1617                 ED_view3d_depth_tag_update(vod->rv3d);
1618                 viewops_data_free(C, op);
1619
1620                 return OPERATOR_FINISHED;
1621         }
1622
1623         return OPERATOR_RUNNING_MODAL;
1624 }
1625
1626 static int viewzoom_exec(bContext *C, wmOperator *op)
1627 {
1628         View3D *v3d;
1629         RegionView3D *rv3d;
1630         ScrArea *sa;
1631         ARegion *ar;
1632         short use_cam_zoom;
1633
1634         int delta = RNA_int_get(op->ptr, "delta");
1635         int mx, my;
1636
1637         if (op->customdata) {
1638                 ViewOpsData *vod = op->customdata;
1639
1640                 sa = vod->sa;
1641                 ar = vod->ar;
1642         }
1643         else {
1644                 sa = CTX_wm_area(C);
1645                 ar = CTX_wm_region(C);
1646         }
1647
1648         v3d = sa->spacedata.first;
1649         rv3d = ar->regiondata;
1650
1651         mx = RNA_struct_property_is_set(op->ptr, "mx") ? RNA_int_get(op->ptr, "mx") : ar->winx / 2;
1652         my = RNA_struct_property_is_set(op->ptr, "my") ? RNA_int_get(op->ptr, "my") : ar->winy / 2;
1653
1654         use_cam_zoom = (rv3d->persp == RV3D_CAMOB) && !(rv3d->is_persp && ED_view3d_camera_lock_check(v3d, rv3d));
1655
1656         if (delta < 0) {
1657                 /* this min and max is also in viewmove() */
1658                 if (use_cam_zoom) {
1659                         rv3d->camzoom -= 10.0f;
1660                         if (rv3d->camzoom < RV3D_CAMZOOM_MIN) rv3d->camzoom = RV3D_CAMZOOM_MIN;
1661                 }
1662                 else if (rv3d->dist < 10.0f * v3d->far) {
1663                         view_zoom_mouseloc(ar, 1.2f, mx, my);
1664                 }
1665         }
1666         else {
1667                 if (use_cam_zoom) {
1668                         rv3d->camzoom += 10.0f;
1669                         if (rv3d->camzoom > RV3D_CAMZOOM_MAX) rv3d->camzoom = RV3D_CAMZOOM_MAX;
1670                 }
1671                 else if (rv3d->dist > 0.001f * v3d->grid) {
1672                         view_zoom_mouseloc(ar, 0.83333f, mx, my);
1673                 }
1674         }
1675
1676         if (rv3d->viewlock & RV3D_BOXVIEW)
1677                 view3d_boxview_sync(sa, ar);
1678
1679         ED_view3d_depth_tag_update(rv3d);
1680
1681         ED_view3d_camera_lock_sync(v3d, rv3d);
1682
1683         ED_region_tag_redraw(ar);
1684
1685         viewops_data_free(C, op);
1686
1687         return OPERATOR_FINISHED;
1688 }
1689
1690 /* this is an exact copy of viewzoom_modal_keymap */
1691 /* called in transform_ops.c, on each regeneration of keymaps  */
1692 void viewdolly_modal_keymap(wmKeyConfig *keyconf)
1693 {
1694         static EnumPropertyItem modal_items[] = {
1695                 {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Confirm", ""},
1696
1697                 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1698                 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1699
1700                 {0, NULL, 0, NULL, NULL}
1701         };
1702
1703         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Dolly Modal");
1704
1705         /* this function is called for each spacetype, only needs to add map once */
1706         if (keymap && keymap->modal_items) return;
1707
1708         keymap = WM_modalkeymap_add(keyconf, "View3D Dolly Modal", modal_items);
1709
1710         /* items for modal map */
1711         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1712         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1713
1714         /* disabled mode switching for now, can re-implement better, later on */
1715 #if 0
1716         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1717         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1718         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
1719 #endif
1720         
1721         /* assign map to operators */
1722         WM_modalkeymap_assign(keymap, "VIEW3D_OT_dolly");
1723 }
1724
1725 /* viewdolly_invoke() copied this function, changes here may apply there */
1726 static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
1727 {
1728         ViewOpsData *vod;
1729
1730         /* makes op->customdata */
1731         viewops_data_create(C, op, event);
1732         vod = op->customdata;
1733
1734         /* if one or the other zoom position aren't set, set from event */
1735         if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
1736                 RNA_int_set(op->ptr, "mx", event->x);
1737                 RNA_int_set(op->ptr, "my", event->y);
1738         }
1739
1740         if (RNA_struct_property_is_set(op->ptr, "delta")) {
1741                 viewzoom_exec(C, op);
1742         }
1743         else {
1744                 if (event->type == MOUSEZOOM) {
1745                         /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1746
1747                         if (U.uiflag & USER_ZOOM_HORIZ) {
1748                                 vod->origx = vod->oldx = event->x;
1749                                 viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY, FALSE);
1750                         }
1751                         else {
1752                                 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
1753                                 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
1754                                 viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY, FALSE);
1755                         }
1756                         ED_view3d_depth_tag_update(vod->rv3d);
1757                         
1758                         viewops_data_free(C, op);
1759                         return OPERATOR_FINISHED;
1760                 }
1761                 else {
1762                         if (U.viewzoom == USER_ZOOM_CONT) {
1763                                 /* needs a timer to continue redrawing */
1764                                 vod->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
1765                                 vod->timer_lastdraw = PIL_check_seconds_timer();
1766                         }
1767
1768                         /* add temp handler */
1769                         WM_event_add_modal_handler(C, op);
1770
1771                         return OPERATOR_RUNNING_MODAL;
1772                 }
1773         }
1774         return OPERATOR_FINISHED;
1775 }
1776
1777 static int viewzoom_cancel(bContext *C, wmOperator *op)
1778 {
1779         viewops_data_free(C, op);
1780
1781         return OPERATOR_CANCELLED;
1782 }
1783
1784 void VIEW3D_OT_zoom(wmOperatorType *ot)
1785 {
1786         /* identifiers */
1787         ot->name = "Zoom View";
1788         ot->description = "Zoom in/out in the view";
1789         ot->idname = "VIEW3D_OT_zoom";
1790
1791         /* api callbacks */
1792         ot->invoke = viewzoom_invoke;
1793         ot->exec = viewzoom_exec;
1794         ot->modal = viewzoom_modal;
1795         ot->poll = ED_operator_region_view3d_active;
1796         ot->cancel = viewzoom_cancel;
1797
1798         /* flags */
1799         ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
1800
1801         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1802         RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
1803         RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
1804 }
1805
1806
1807 /* ************************ viewdolly ******************************** */
1808 static void view_dolly_mouseloc(ARegion *ar, float orig_ofs[3], float dvec[3], float dfac)
1809 {
1810         RegionView3D *rv3d = ar->regiondata;
1811         madd_v3_v3v3fl(rv3d->ofs, orig_ofs, dvec, -(1.0f - dfac));
1812 }
1813
1814 static void viewdolly_apply(ViewOpsData *vod, int x, int y, const short zoom_invert)
1815 {
1816         float zfac = 1.0;
1817
1818         {
1819                 float len1, len2;
1820
1821                 if (U.uiflag & USER_ZOOM_HORIZ) {
1822                         len1 = (vod->ar->winrct.xmax - x) + 5;
1823                         len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1824                 }
1825                 else {
1826                         len1 = (vod->ar->winrct.ymax - y) + 5;
1827                         len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1828                 }
1829                 if (zoom_invert)
1830                         SWAP(float, len1, len2);
1831
1832                 zfac =  1.0f + ((len2 - len1) * 0.01f * vod->rv3d->dist);
1833         }
1834
1835         if (zfac != 1.0f)
1836                 view_dolly_mouseloc(vod->ar, vod->ofs, vod->mousevec, zfac);
1837
1838         if (vod->rv3d->viewlock & RV3D_BOXVIEW)
1839                 view3d_boxview_sync(vod->sa, vod->ar);
1840
1841         ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1842
1843         ED_region_tag_redraw(vod->ar);
1844 }
1845
1846
1847 static int viewdolly_modal(bContext *C, wmOperator *op, wmEvent *event)
1848 {
1849         ViewOpsData *vod = op->customdata;
1850         short event_code = VIEW_PASS;
1851
1852         /* execute the events */
1853         if (event->type == MOUSEMOVE) {
1854                 event_code = VIEW_APPLY;
1855         }
1856         else if (event->type == EVT_MODAL_MAP) {
1857                 switch (event->val) {
1858                         case VIEW_MODAL_CONFIRM:
1859                                 event_code = VIEW_CONFIRM;
1860                                 break;
1861                         case VIEWROT_MODAL_SWITCH_MOVE:
1862                                 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1863                                 event_code = VIEW_CONFIRM;
1864                                 break;
1865                         case VIEWROT_MODAL_SWITCH_ROTATE:
1866                                 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1867                                 event_code = VIEW_CONFIRM;
1868                                 break;
1869                 }
1870         }
1871         else if (event->type == vod->origkey && event->val == KM_RELEASE) {
1872                 event_code = VIEW_CONFIRM;
1873         }
1874
1875         if (event_code == VIEW_APPLY) {
1876                 viewdolly_apply(vod, event->x, event->y, (U.uiflag & USER_ZOOM_INVERT) != 0);
1877         }
1878         else if (event_code == VIEW_CONFIRM) {
1879                 ED_view3d_depth_tag_update(vod->rv3d);
1880                 viewops_data_free(C, op);
1881
1882                 return OPERATOR_FINISHED;
1883         }
1884
1885         return OPERATOR_RUNNING_MODAL;
1886 }
1887
1888 static int viewdolly_exec(bContext *C, wmOperator *op)
1889 {
1890         /* View3D *v3d; */
1891         RegionView3D *rv3d;
1892         ScrArea *sa;
1893         ARegion *ar;
1894         float mousevec[3];
1895
1896         int delta = RNA_int_get(op->ptr, "delta");
1897
1898         if (op->customdata) {
1899                 ViewOpsData *vod = op->customdata;
1900
1901                 sa = vod->sa;
1902                 ar = vod->ar;
1903                 copy_v3_v3(mousevec, vod->mousevec);
1904         }
1905         else {
1906                 sa = CTX_wm_area(C);
1907                 ar = CTX_wm_region(C);
1908                 negate_v3_v3(mousevec, ((RegionView3D *)ar->regiondata)->viewinv[2]);
1909                 normalize_v3(mousevec);
1910         }
1911
1912         /* v3d= sa->spacedata.first; */ /* UNUSED */
1913         rv3d = ar->regiondata;
1914
1915         /* overwrite the mouse vector with the view direction (zoom into the center) */
1916         if ((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
1917                 normalize_v3_v3(mousevec, rv3d->viewinv[2]);
1918         }
1919
1920         if (delta < 0) {
1921                 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, 1.2f);
1922         }
1923         else {
1924                 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, 0.83333f);
1925         }
1926
1927         if (rv3d->viewlock & RV3D_BOXVIEW)
1928                 view3d_boxview_sync(sa, ar);
1929
1930         ED_view3d_depth_tag_update(rv3d);
1931         ED_region_tag_redraw(ar);
1932
1933         viewops_data_free(C, op);
1934
1935         return OPERATOR_FINISHED;
1936 }
1937
1938 /* copied from viewzoom_invoke(), changes here may apply there */
1939 static int viewdolly_invoke(bContext *C, wmOperator *op, wmEvent *event)
1940 {
1941         ViewOpsData *vod;
1942
1943         /* makes op->customdata */
1944         viewops_data_create(C, op, event);
1945         vod = op->customdata;
1946
1947         /* if one or the other zoom position aren't set, set from event */
1948         if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
1949                 RNA_int_set(op->ptr, "mx", event->x);
1950                 RNA_int_set(op->ptr, "my", event->y);
1951         }
1952
1953         if (RNA_struct_property_is_set(op->ptr, "delta")) {
1954                 viewdolly_exec(C, op);
1955         }
1956         else {
1957                 /* overwrite the mouse vector with the view direction (zoom into the center) */
1958                 if ((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
1959                         negate_v3_v3(vod->mousevec, vod->rv3d->viewinv[2]);
1960                         normalize_v3(vod->mousevec);
1961                 }
1962
1963                 if (event->type == MOUSEZOOM) {
1964                         /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1965
1966                         if (U.uiflag & USER_ZOOM_HORIZ) {
1967                                 vod->origx = vod->oldx = event->x;
1968                                 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
1969                         }
1970                         else {
1971
1972                                 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
1973                                 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
1974                                 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
1975                         }
1976                         ED_view3d_depth_tag_update(vod->rv3d);
1977
1978                         viewops_data_free(C, op);
1979                         return OPERATOR_FINISHED;
1980                 }
1981                 else {
1982                         /* add temp handler */
1983                         WM_event_add_modal_handler(C, op);
1984
1985                         return OPERATOR_RUNNING_MODAL;
1986                 }
1987         }
1988         return OPERATOR_FINISHED;
1989 }
1990
1991 /* like ED_operator_region_view3d_active but check its not in ortho view */
1992 static int viewdolly_poll(bContext *C)
1993 {
1994         RegionView3D *rv3d = CTX_wm_region_view3d(C);
1995
1996         if (rv3d) {
1997                 if (rv3d->persp == RV3D_PERSP) {
1998                         return 1;
1999                 }
2000                 else {
2001                         View3D *v3d = CTX_wm_view3d(C);
2002                         if (ED_view3d_camera_lock_check(v3d, rv3d)) {
2003                                 return 1;
2004                         }
2005                 }
2006         }
2007         return 0;
2008 }
2009
2010 static int viewdolly_cancel(bContext *C, wmOperator *op)
2011 {
2012         viewops_data_free(C, op);
2013
2014         return OPERATOR_CANCELLED;
2015 }
2016
2017 void VIEW3D_OT_dolly(wmOperatorType *ot)
2018 {
2019         /* identifiers */
2020         ot->name = "Dolly view";
2021         ot->description = "Dolly in/out in the view";
2022         ot->idname = "VIEW3D_OT_dolly";
2023
2024         /* api callbacks */
2025         ot->invoke = viewdolly_invoke;
2026         ot->exec = viewdolly_exec;
2027         ot->modal = viewdolly_modal;
2028         ot->poll = viewdolly_poll;
2029         ot->cancel = viewdolly_cancel;
2030
2031         /* flags */
2032         ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
2033
2034         RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
2035         RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
2036         RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
2037 }
2038
2039
2040
2041 static int view3d_all_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
2042 {
2043         ARegion *ar = CTX_wm_region(C);
2044         View3D *v3d = CTX_wm_view3d(C);
2045         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2046         Scene *scene = CTX_data_scene(C);
2047         Base *base;
2048         float *curs;
2049         const short skip_camera = ED_view3d_camera_lock_check(v3d, rv3d);
2050
2051         int center = RNA_boolean_get(op->ptr, "center");
2052
2053         float size, min[3], max[3], afm[3];
2054         int ok = 1, onedone = FALSE;
2055
2056         if (center) {
2057                 /* in 2.4x this also move the cursor to (0, 0, 0) (with shift+c). */
2058                 curs = give_cursor(scene, v3d);
2059                 zero_v3(min);
2060                 zero_v3(max);
2061                 zero_v3(curs);
2062         }
2063         else {
2064                 INIT_MINMAX(min, max);
2065         }
2066
2067         for (base = scene->base.first; base; base = base->next) {
2068                 if (BASE_VISIBLE(v3d, base)) {
2069                         onedone = TRUE;
2070
2071                         if (skip_camera && base->object == v3d->camera) {
2072                                 continue;
2073                         }
2074
2075                         BKE_object_minmax(base->object, min, max);
2076                 }
2077         }
2078         if (!onedone) {
2079                 ED_region_tag_redraw(ar);
2080                 /* TODO - should this be cancel?
2081                  * I think no, because we always move the cursor, with or without
2082                  * object, but in this case there is no change in the scene,
2083                  * only the cursor so I choice a ED_region_tag like
2084                  * smooth_view do for the center_cursor.
2085                  * See bug #22640
2086                  */
2087                 return OPERATOR_FINISHED;
2088         }
2089
2090         sub_v3_v3v3(afm, max, min);
2091         size = 0.7f * MAX3(afm[0], afm[1], afm[2]);
2092         if (size == 0.0f) ok = 0;
2093
2094         if (ok) {
2095                 float new_dist;
2096                 float new_ofs[3];
2097
2098                 new_dist = size;
2099                 new_ofs[0] = -(min[0] + max[0]) / 2.0f;
2100                 new_ofs[1] = -(min[1] + max[1]) / 2.0f;
2101                 new_ofs[2] = -(min[2] + max[2]) / 2.0f;
2102
2103                 /* correction for window aspect ratio */
2104                 if (ar->winy > 2 && ar->winx > 2) {
2105                         size = (float)ar->winx / (float)ar->winy;
2106                         if (size < 1.0f) size = 1.0f / size;
2107                         new_dist *= size;
2108                 }
2109
2110                 if ((rv3d->persp == RV3D_CAMOB) && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2111                         rv3d->persp = RV3D_PERSP;
2112                         smooth_view(C, v3d, ar, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
2113                 }
2114                 else {
2115                         smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2116                 }
2117         }
2118 // XXX  BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2119
2120         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
2121
2122         return OPERATOR_FINISHED;
2123 }
2124
2125
2126 void VIEW3D_OT_view_all(wmOperatorType *ot)
2127 {
2128         /* identifiers */
2129         ot->name = "View All";
2130         ot->description = "View all objects in scene";
2131         ot->idname = "VIEW3D_OT_view_all";
2132
2133         /* api callbacks */
2134         ot->exec = view3d_all_exec;
2135         ot->poll = ED_operator_region_view3d_active;
2136
2137         /* flags */
2138         ot->flag = 0;
2139
2140         RNA_def_boolean(ot->srna, "center", 0, "Center", "");
2141 }
2142
2143 /* like a localview without local!, was centerview() in 2.4x */
2144 static int viewselected_exec(bContext *C, wmOperator *UNUSED(op))
2145 {
2146         ARegion *ar = CTX_wm_region(C);
2147         View3D *v3d = CTX_wm_view3d(C);
2148         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2149         Scene *scene = CTX_data_scene(C);
2150         Object *ob = OBACT;
2151         Object *obedit = CTX_data_edit_object(C);
2152         float size, min[3], max[3], afm[3];
2153         int ok = 0, ok_dist = 1;
2154         const short skip_camera = ED_view3d_camera_lock_check(v3d, rv3d);
2155
2156         /* SMOOTHVIEW */
2157         float new_ofs[3];
2158         float new_dist;
2159
2160         INIT_MINMAX(min, max);
2161
2162         if (ob && (ob->mode & OB_MODE_WEIGHT_PAINT)) {
2163                 /* hard-coded exception, we look for the one selected armature */
2164                 /* this is weak code this way, we should make a generic active/selection callback interface once... */
2165                 Base *base;
2166                 for (base = scene->base.first; base; base = base->next) {
2167                         if (TESTBASELIB(v3d, base)) {
2168                                 if (base->object->type == OB_ARMATURE)
2169                                         if (base->object->mode & OB_MODE_POSE)
2170                                                 break;
2171                         }
2172                 }
2173                 if (base)
2174                         ob = base->object;
2175         }
2176
2177
2178         if (obedit) {
2179                 ok = ED_view3d_minmax_verts(obedit, min, max);    /* only selected */
2180         }
2181         else if (ob && (ob->mode & OB_MODE_POSE)) {
2182                 if (ob->pose) {
2183                         bArmature *arm = ob->data;
2184                         bPoseChannel *pchan;
2185                         float vec[3];
2186
2187                         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2188                                 if (pchan->bone->flag & BONE_SELECTED) {
2189                                         if (pchan->bone->layer & arm->layer) {
2190                                                 bPoseChannel *pchan_tx = pchan->custom_tx ? pchan->custom_tx : pchan;
2191                                                 ok = 1;
2192                                                 mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_head);
2193                                                 minmax_v3v3_v3(min, max, vec);
2194                                                 mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_tail);
2195                                                 minmax_v3v3_v3(min, max, vec);
2196                                         }
2197                                 }
2198                         }
2199                 }
2200         }
2201         else if (paint_facesel_test(ob)) {
2202                 ok = paintface_minmax(ob, min, max);
2203         }
2204         else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
2205                 ok = PE_minmax(scene, min, max);
2206         }
2207         else if (ob && (ob->mode & OB_MODE_SCULPT)) {
2208                 ok = ED_sculpt_minmax(C, min, max);
2209                 ok_dist = 0; /* don't zoom */
2210         }
2211         else {
2212                 Base *base;
2213                 for (base = FIRSTBASE; base; base = base->next) {
2214                         if (TESTBASE(v3d, base)) {
2215
2216                                 if (skip_camera && base->object == v3d->camera) {
2217                                         continue;
2218                                 }
2219
2220                                 /* account for duplis */
2221                                 if (BKE_object_minmax_dupli(scene, base->object, min, max) == 0)
2222                                         BKE_object_minmax(base->object, min, max);  /* use if duplis not found */
2223
2224                                 ok = 1;
2225                         }
2226                 }
2227         }
2228
2229         if (ok == 0) return OPERATOR_FINISHED;
2230
2231         sub_v3_v3v3(afm, max, min);
2232         size = MAX3(afm[0], afm[1], afm[2]);
2233
2234         if (ok_dist) {
2235                 /* fix up zoom distance if needed */
2236
2237                 if (rv3d->is_persp) {
2238                         if (size <= v3d->near * 1.5f) {
2239                                 /* do not zoom closer than the near clipping plane */
2240                                 size = v3d->near * 1.5f;
2241                         }
2242                 }
2243                 else { /* ortho */
2244                         if (size < 0.0001f) {
2245                                 /* bounding box was a single point so do not zoom */
2246                                 ok_dist = 0;
2247                         }
2248                         else {
2249                                 /* adjust zoom so it looks nicer */
2250                                 size *= 0.7f;
2251                         }
2252                 }
2253         }
2254
2255         add_v3_v3v3(new_ofs, min, max);
2256         mul_v3_fl(new_ofs, -0.5f);
2257
2258         new_dist = size;
2259
2260         /* correction for window aspect ratio */
2261         if (ar->winy > 2 && ar->winx > 2) {
2262                 size = (float)ar->winx / (float)ar->winy;
2263                 if (size < 1.0f) size = 1.0f / size;
2264                 new_dist *= size;
2265         }
2266
2267         if (rv3d->persp == RV3D_CAMOB && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2268                 rv3d->persp = RV3D_PERSP;
2269                 smooth_view(C, v3d, ar, v3d->camera, NULL, new_ofs, NULL, ok_dist ? &new_dist : NULL, NULL);
2270         }
2271         else {
2272                 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, ok_dist ? &new_dist : NULL, NULL);
2273         }
2274
2275         /* smooth view does viewlock RV3D_BOXVIEW copy */
2276         
2277 // XXX  BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2278
2279         return OPERATOR_FINISHED;
2280 }
2281
2282 void VIEW3D_OT_view_selected(wmOperatorType *ot)
2283 {
2284
2285         /* identifiers */
2286         ot->name = "View Selected";
2287         ot->description = "Move the view to the selection center";
2288         ot->idname = "VIEW3D_OT_view_selected";
2289
2290         /* api callbacks */
2291         ot->exec = viewselected_exec;
2292         ot->poll = ED_operator_region_view3d_active;
2293
2294         /* flags */
2295         ot->flag = 0;
2296 }
2297
2298 static int view_lock_clear_exec(bContext *C, wmOperator *UNUSED(op))
2299 {
2300         View3D *v3d = CTX_wm_view3d(C);
2301
2302         if (v3d) {
2303                 ED_view3D_lock_clear(v3d);
2304
2305                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
2306
2307                 return OPERATOR_FINISHED;
2308         }
2309         else {
2310                 return OPERATOR_CANCELLED;
2311         }
2312 }
2313
2314 void VIEW3D_OT_view_lock_clear(wmOperatorType *ot)
2315 {
2316
2317         /* identifiers */
2318         ot->name = "View Lock Clear";
2319         ot->description = "Clear all view locking";
2320         ot->idname = "VIEW3D_OT_view_lock_clear";
2321
2322         /* api callbacks */
2323         ot->exec = view_lock_clear_exec;
2324         ot->poll = ED_operator_region_view3d_active;
2325
2326         /* flags */
2327         ot->flag = 0;
2328 }
2329
2330 static int view_lock_to_active_exec(bContext *C, wmOperator *UNUSED(op))
2331 {
2332         View3D *v3d = CTX_wm_view3d(C);
2333         Object *obact = CTX_data_active_object(C);
2334
2335         if (v3d) {
2336
2337                 ED_view3D_lock_clear(v3d);
2338
2339                 v3d->ob_centre = obact; /* can be NULL */
2340
2341                 if (obact && obact->type == OB_ARMATURE) {
2342                         if (obact->mode & OB_MODE_POSE) {
2343                                 bPoseChannel *pcham_act = BKE_pose_channel_active(obact);
2344                                 if (pcham_act) {
2345                                         BLI_strncpy(v3d->ob_centre_bone, pcham_act->name, sizeof(v3d->ob_centre_bone));
2346                                 }
2347                         }
2348                         else {
2349                                 EditBone *ebone_act = ((bArmature *)obact->data)->act_edbone;
2350                                 if (ebone_act) {
2351                                         BLI_strncpy(v3d->ob_centre_bone, ebone_act->name, sizeof(v3d->ob_centre_bone));
2352                                 }
2353                         }
2354                 }
2355
2356                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
2357
2358                 return OPERATOR_FINISHED;
2359         }
2360         else {
2361                 return OPERATOR_CANCELLED;
2362         }
2363 }
2364
2365 void VIEW3D_OT_view_lock_to_active(wmOperatorType *ot)
2366 {
2367
2368         /* identifiers */
2369         ot->name = "View Lock to Active";
2370         ot->description = "Lock the view to the active object/bone";
2371         ot->idname = "VIEW3D_OT_view_lock_to_active";
2372
2373         /* api callbacks */
2374         ot->exec = view_lock_to_active_exec;
2375         ot->poll = ED_operator_region_view3d_active;
2376
2377         /* flags */
2378         ot->flag = 0;
2379 }
2380
2381 static int viewcenter_cursor_exec(bContext *C, wmOperator *UNUSED(op))
2382 {
2383         View3D *v3d = CTX_wm_view3d(C);
2384         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2385         Scene *scene = CTX_data_scene(C);
2386         
2387         if (rv3d) {
2388                 ARegion *ar = CTX_wm_region(C);
2389
2390                 /* non camera center */
2391                 float new_ofs[3];
2392                 negate_v3_v3(new_ofs, give_cursor(scene, v3d));
2393                 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, NULL, NULL);
2394                 
2395                 /* smooth view does viewlock RV3D_BOXVIEW copy */
2396         }
2397         
2398         return OPERATOR_FINISHED;
2399 }
2400
2401 void VIEW3D_OT_view_center_cursor(wmOperatorType *ot)
2402 {
2403         /* identifiers */
2404         ot->name = "Center View to Cursor";
2405         ot->description = "Center the view so that the cursor is in the middle of the view";
2406         ot->idname = "VIEW3D_OT_view_center_cursor";
2407         
2408         /* api callbacks */
2409         ot->exec = viewcenter_cursor_exec;
2410         ot->poll = ED_operator_view3d_active;
2411         
2412         /* flags */
2413         ot->flag = 0;
2414 }
2415
2416 static int view3d_center_camera_exec(bContext *C, wmOperator *UNUSED(op)) /* was view3d_home() in 2.4x */
2417 {
2418         Scene *scene = CTX_data_scene(C);
2419         float xfac, yfac;
2420         float size[2];
2421
2422         View3D *v3d;
2423         ARegion *ar;
2424         RegionView3D *rv3d;
2425
2426         /* no NULL check is needed, poll checks */
2427         ED_view3d_context_user_region(C, &v3d, &ar);
2428         rv3d = ar->regiondata;
2429
2430         rv3d->camdx = rv3d->camdy = 0.0f;
2431
2432         ED_view3d_calc_camera_border_size(scene, ar, v3d, rv3d, size);
2433
2434         /* 4px is just a little room from the edge of the area */
2435         xfac = (float)ar->winx / (float)(size[0] + 4);
2436         yfac = (float)ar->winy / (float)(size[1] + 4);
2437
2438         rv3d->camzoom = BKE_screen_view3d_zoom_from_fac(MIN2(xfac, yfac));
2439         CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2440
2441         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2442
2443         return OPERATOR_FINISHED;
2444 }
2445
2446 void VIEW3D_OT_view_center_camera(wmOperatorType *ot)
2447 {
2448         /* identifiers */
2449         ot->name = "View Camera Center";
2450         ot->description = "Center the camera view";
2451         ot->idname = "VIEW3D_OT_view_center_camera";
2452
2453         /* api callbacks */
2454         ot->exec = view3d_center_camera_exec;
2455         ot->poll = view3d_camera_user_poll;
2456
2457         /* flags */
2458         ot->flag = 0;
2459 }
2460
2461 /* ********************* Set render border operator ****************** */
2462
2463 static int render_border_exec(bContext *C, wmOperator *op)
2464 {
2465         View3D *v3d = CTX_wm_view3d(C);
2466         ARegion *ar = CTX_wm_region(C);
2467         RegionView3D *rv3d = ED_view3d_context_rv3d(C);
2468         Scene *scene = CTX_data_scene(C);
2469
2470         rcti rect;
2471         rctf vb;
2472
2473         /* get border select values using rna */
2474         rect.xmin = RNA_int_get(op->ptr, "xmin");
2475         rect.ymin = RNA_int_get(op->ptr, "ymin");
2476         rect.xmax = RNA_int_get(op->ptr, "xmax");
2477         rect.ymax = RNA_int_get(op->ptr, "ymax");
2478
2479         /* calculate range */
2480         ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &vb, FALSE);
2481
2482         scene->r.border.xmin = ((float)rect.xmin - vb.xmin) / (vb.xmax - vb.xmin);
2483         scene->r.border.ymin = ((float)rect.ymin - vb.ymin) / (vb.ymax - vb.ymin);
2484         scene->r.border.xmax = ((float)rect.xmax - vb.xmin) / (vb.xmax - vb.xmin);
2485         scene->r.border.ymax = ((float)rect.ymax - vb.ymin) / (vb.ymax - vb.ymin);
2486
2487         /* actually set border */
2488         CLAMP(scene->r.border.xmin, 0.0f, 1.0f);
2489         CLAMP(scene->r.border.ymin, 0.0f, 1.0f);
2490         CLAMP(scene->r.border.xmax, 0.0f, 1.0f);
2491         CLAMP(scene->r.border.ymax, 0.0f, 1.0f);
2492
2493         /* drawing a border surrounding the entire camera view switches off border rendering
2494          * or the border covers no pixels */
2495         if ((scene->r.border.xmin <= 0.0f && scene->r.border.xmax >= 1.0f &&
2496              scene->r.border.ymin <= 0.0f && scene->r.border.ymax >= 1.0f) ||
2497             (scene->r.border.xmin == scene->r.border.xmax ||
2498              scene->r.border.ymin == scene->r.border.ymax))
2499         {
2500                 scene->r.mode &= ~R_BORDER;
2501         }
2502         else {
2503                 scene->r.mode |= R_BORDER;
2504         }
2505         
2506         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, NULL);
2507
2508         return OPERATOR_FINISHED;
2509
2510 }
2511
2512 void VIEW3D_OT_render_border(wmOperatorType *ot)
2513 {
2514         /* identifiers */
2515         ot->name = "Set Render Border";
2516         ot->description = "Set the boundaries of the border render and enables border render";
2517         ot->idname = "VIEW3D_OT_render_border";
2518
2519         /* api callbacks */
2520         ot->invoke = WM_border_select_invoke;
2521         ot->exec = render_border_exec;
2522         ot->modal = WM_border_select_modal;
2523         ot->cancel = WM_border_select_cancel;
2524
2525         ot->poll = view3d_camera_active_poll;
2526
2527         /* flags */
2528         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2529
2530         /* rna */
2531         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2532         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2533         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2534         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2535
2536 }
2537 /* ********************* Border Zoom operator ****************** */
2538
2539 static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
2540 {
2541         ARegion *ar = CTX_wm_region(C);
2542         View3D *v3d = CTX_wm_view3d(C);
2543         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2544         Scene *scene = CTX_data_scene(C);
2545
2546         /* Zooms in on a border drawn by the user */
2547         rcti rect;
2548         float dvec[3], vb[2], xscale, yscale, scale;
2549
2550         /* SMOOTHVIEW */
2551         float new_dist;
2552         float new_ofs[3];
2553
2554         /* ZBuffer depth vars */
2555         bglMats mats;
2556         float depth_close = FLT_MAX;
2557         double cent[2],  p[3];
2558
2559         /* note; otherwise opengl won't work */
2560         view3d_operator_needs_opengl(C);
2561
2562         /* get border select values using rna */
2563         rect.xmin = RNA_int_get(op->ptr, "xmin");
2564         rect.ymin = RNA_int_get(op->ptr, "ymin");
2565         rect.xmax = RNA_int_get(op->ptr, "xmax");
2566         rect.ymax = RNA_int_get(op->ptr, "ymax");
2567
2568         /* Get Z Depths, needed for perspective, nice for ortho */
2569         bgl_get_mats(&mats);
2570         draw_depth(scene, ar, v3d, NULL);
2571         
2572         {
2573                 /* avoid allocating the whole depth buffer */
2574                 ViewDepths depth_temp = {0};
2575
2576                 /* avoid view3d_update_depths() for speed. */
2577                 view3d_update_depths_rect(ar, &depth_temp, &rect);
2578         
2579                 /* find the closest Z pixel */
2580                 depth_close = view3d_depth_near(&depth_temp);
2581         
2582                 MEM_freeN(depth_temp.depths);
2583         }
2584
2585         cent[0] = (((double)rect.xmin) + ((double)rect.xmax)) / 2;
2586         cent[1] = (((double)rect.ymin) + ((double)rect.ymax)) / 2;
2587
2588         if (rv3d->is_persp) {
2589                 double p_corner[3];
2590
2591                 /* no depths to use, we cant do anything! */
2592                 if (depth_close == FLT_MAX) {
2593                         BKE_report(op->reports, RPT_ERROR, "Depth Too Large");
2594                         return OPERATOR_CANCELLED;
2595                 }
2596                 /* convert border to 3d coordinates */
2597                 if ( (!gluUnProject(cent[0], cent[1], depth_close,
2598                                     mats.modelview, mats.projection, (GLint *)mats.viewport,
2599                                     &p[0], &p[1], &p[2])) ||
2600                      (!gluUnProject((double)rect.xmin, (double)rect.ymin, depth_close,
2601                                     mats.modelview, mats.projection, (GLint *)mats.viewport,
2602                                     &p_corner[0], &p_corner[1], &p_corner[2])))
2603                 {
2604                         return OPERATOR_CANCELLED;
2605                 }
2606
2607                 dvec[0] = p[0] - p_corner[0];
2608                 dvec[1] = p[1] - p_corner[1];
2609                 dvec[2] = p[2] - p_corner[2];
2610
2611                 new_dist = len_v3(dvec);
2612                 if (new_dist <= v3d->near * 1.5f) new_dist = v3d->near * 1.5f;
2613
2614                 new_ofs[0] = -p[0];
2615                 new_ofs[1] = -p[1];
2616                 new_ofs[2] = -p[2];
2617
2618         }
2619         else { /* othographic */
2620                    /* find the current window width and height */
2621                 vb[0] = ar->winx;
2622                 vb[1] = ar->winy;
2623
2624                 new_dist = rv3d->dist;
2625
2626                 /* convert the drawn rectangle into 3d space */
2627                 if (depth_close != FLT_MAX && gluUnProject(cent[0], cent[1], depth_close,
2628                                                            mats.modelview, mats.projection, (GLint *)mats.viewport,
2629                                                            &p[0], &p[1], &p[2]))
2630                 {
2631                         new_ofs[0] = -p[0];
2632                         new_ofs[1] = -p[1];
2633                         new_ofs[2] = -p[2];
2634                 }
2635                 else {
2636                         float mval_f[2];
2637                         /* We cant use the depth, fallback to the old way that dosnt set the center depth */
2638                         copy_v3_v3(new_ofs, rv3d->ofs);
2639
2640                         initgrabz(rv3d, -new_ofs[0], -new_ofs[1], -new_ofs[2]);
2641
2642                         mval_f[0] = (rect.xmin + rect.xmax - vb[0]) / 2.0f;
2643                         mval_f[1] = (rect.ymin + rect.ymax - vb[1]) / 2.0f;
2644                         ED_view3d_win_to_delta(ar, mval_f, dvec);
2645                         /* center the view to the center of the rectangle */
2646                         sub_v3_v3(new_ofs, dvec);
2647                 }
2648
2649                 /* work out the ratios, so that everything selected fits when we zoom */
2650                 xscale = ((rect.xmax - rect.xmin) / vb[0]);
2651                 yscale = ((rect.ymax - rect.ymin) / vb[1]);
2652                 scale = (xscale >= yscale) ? xscale : yscale;
2653
2654                 /* zoom in as required, or as far as we can go */
2655                 new_dist = ((new_dist * scale) >= 0.001f * v3d->grid) ? new_dist * scale : 0.001f * v3d->grid;
2656         }
2657
2658         smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2659
2660         if (rv3d->viewlock & RV3D_BOXVIEW)
2661                 view3d_boxview_sync(CTX_wm_area(C), ar);
2662
2663         return OPERATOR_FINISHED;
2664 }
2665
2666 static int view3d_zoom_border_invoke(bContext *C, wmOperator *op, wmEvent *event)
2667 {
2668         View3D *v3d = CTX_wm_view3d(C);
2669         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2670
2671         /* if in camera view do not exec the operator so we do not conflict with set render border*/
2672         if ((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d))
2673                 return WM_border_select_invoke(C, op, event);
2674         else
2675                 return OPERATOR_PASS_THROUGH;
2676 }
2677
2678 void VIEW3D_OT_zoom_border(wmOperatorType *ot)
2679 {
2680         /* identifiers */
2681         ot->name = "Border Zoom";
2682         ot->description = "Zoom in the view to the nearest object contained in the border";
2683         ot->idname = "VIEW3D_OT_zoom_border";
2684
2685         /* api callbacks */
2686         ot->invoke = view3d_zoom_border_invoke;
2687         ot->exec = view3d_zoom_border_exec;
2688         ot->modal = WM_border_select_modal;
2689         ot->cancel = WM_border_select_cancel;
2690
2691         ot->poll = ED_operator_region_view3d_active;
2692
2693         /* flags */
2694         ot->flag = 0;
2695
2696         /* rna */
2697         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2698         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2699         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2700         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2701
2702 }
2703
2704 /* sets the view to 1:1 camera/render-pixel */
2705 static void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar, View3D *v3d)
2706 {
2707         RegionView3D *rv3d = ar->regiondata;
2708         float size[2];
2709         int im_width = (scene->r.size * scene->r.xsch) / 100;
2710         
2711         ED_view3d_calc_camera_border_size(scene, ar, v3d, rv3d, size);
2712
2713         rv3d->camzoom = BKE_screen_view3d_zoom_from_fac((float)im_width / size[0]);
2714         CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2715 }
2716
2717 static int view3d_zoom_1_to_1_camera_exec(bContext *C, wmOperator *UNUSED(op))
2718 {
2719         Scene *scene = CTX_data_scene(C);
2720
2721         View3D *v3d;
2722         ARegion *ar;
2723
2724         /* no NULL check is needed, poll checks */
2725         ED_view3d_context_user_region(C, &v3d, &ar);
2726
2727         view3d_set_1_to_1_viewborder(scene, ar, v3d);
2728
2729         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
2730
2731         return OPERATOR_FINISHED;
2732 }
2733
2734 void VIEW3D_OT_zoom_camera_1_to_1(wmOperatorType *ot)
2735 {
2736         /* identifiers */
2737         ot->name = "Zoom Camera 1:1";
2738         ot->description = "Match the camera to 1:1 to the render output";
2739         ot->idname = "VIEW3D_OT_zoom_camera_1_to_1";
2740
2741         /* api callbacks */
2742         ot->exec = view3d_zoom_1_to_1_camera_exec;
2743         ot->poll = view3d_camera_user_poll;
2744
2745         /* flags */
2746         ot->flag = 0;
2747 }
2748
2749 /* ********************* Changing view operator ****************** */
2750
2751 static EnumPropertyItem prop_view_items[] = {
2752         {RV3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
2753         {RV3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
2754         {RV3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
2755         {RV3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
2756         {RV3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
2757         {RV3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
2758         {RV3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active camera"},
2759         {0, NULL, 0, NULL, NULL}
2760 };
2761
2762
2763 /* would like to make this a generic function - outside of transform */
2764
2765 static void axis_set_view(bContext *C, View3D *v3d, ARegion *ar,
2766                           float q1, float q2, float q3, float q4,
2767                           short view, int perspo, int align_active)
2768 {
2769         RegionView3D *rv3d = ar->regiondata; /* no NULL check is needed, poll checks */
2770         float new_quat[4];
2771
2772         new_quat[0] = q1; new_quat[1] = q2;
2773         new_quat[2] = q3; new_quat[3] = q4;
2774         normalize_qt(new_quat);
2775
2776         if (align_active) {
2777                 /* align to active object */
2778                 Object *obact = CTX_data_active_object(C);
2779                 if (obact == NULL) {
2780                         /* no active object, ignore this option */
2781                         align_active = FALSE;
2782                 }
2783                 else {
2784                         float obact_quat[4];
2785                         float twmat[3][3];
2786
2787                         /* same as transform manipulator when normal is set */
2788                         ED_getTransformOrientationMatrix(C, twmat, FALSE);
2789
2790                         mat3_to_quat(obact_quat, twmat);
2791                         invert_qt(obact_quat);
2792                         mul_qt_qtqt(new_quat, new_quat, obact_quat);
2793
2794                         rv3d->view = view = RV3D_VIEW_USER;
2795                 }
2796         }
2797
2798         if (align_active == FALSE) {
2799                 /* normal operation */
2800                 if (rv3d->viewlock) {
2801                         /* only pass on if */
2802
2803                         /* nice confusing if-block */
2804                         if (!((rv3d->view == RV3D_VIEW_FRONT  && view == RV3D_VIEW_BACK)  ||
2805                               (rv3d->view == RV3D_VIEW_BACK   && view == RV3D_VIEW_FRONT) ||
2806                               (rv3d->view == RV3D_VIEW_RIGHT  && view == RV3D_VIEW_LEFT)  ||
2807                               (rv3d->view == RV3D_VIEW_LEFT   && view == RV3D_VIEW_RIGHT) ||
2808                               (rv3d->view == RV3D_VIEW_BOTTOM && view == RV3D_VIEW_TOP)   ||
2809                               (rv3d->view == RV3D_VIEW_TOP    && view == RV3D_VIEW_BOTTOM)))
2810                         {
2811                                 return;
2812                         }
2813                 }
2814
2815                 rv3d->view = view;
2816         }
2817
2818         if (rv3d->viewlock) {
2819                 ED_region_tag_redraw(ar);
2820                 return;
2821         }
2822
2823         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
2824
2825                 if (U.uiflag & USER_AUTOPERSP) rv3d->persp = view ? RV3D_ORTHO : RV3D_PERSP;
2826                 else if (rv3d->persp == RV3D_CAMOB) rv3d->persp = perspo;
2827
2828                 smooth_view(C, v3d, ar, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
2829         }
2830         else {
2831
2832                 if (U.uiflag & USER_AUTOPERSP) rv3d->persp = view ? RV3D_ORTHO : RV3D_PERSP;
2833                 else if (rv3d->persp == RV3D_CAMOB) rv3d->persp = perspo;
2834
2835                 smooth_view(C, v3d, ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2836         }
2837
2838 }
2839
2840 static int viewnumpad_exec(bContext *C, wmOperator *op)
2841 {
2842         View3D *v3d;
2843         ARegion *ar;
2844         RegionView3D *rv3d;
2845         Scene *scene = CTX_data_scene(C);
2846         static int perspo = RV3D_PERSP;
2847         int viewnum, align_active, nextperspo;
2848
2849         /* no NULL check is needed, poll checks */
2850         ED_view3d_context_user_region(C, &v3d, &ar);
2851         rv3d = ar->regiondata;
2852
2853         viewnum = RNA_enum_get(op->ptr, "type");
2854         align_active = RNA_boolean_get(op->ptr, "align_active");
2855
2856         /* set this to zero, gets handled in axis_set_view */
2857         if (rv3d->viewlock)
2858                 align_active = 0;
2859
2860         /* Use this to test if we started out with a camera */
2861
2862         if (rv3d->persp == RV3D_CAMOB) {
2863                 nextperspo = rv3d->lpersp;
2864         }
2865         else {
2866                 nextperspo = perspo;
2867         }
2868
2869         switch (viewnum) {
2870                 case RV3D_VIEW_BOTTOM:
2871                         axis_set_view(C, v3d, ar, 0.0, -1.0, 0.0, 0.0,
2872                                       viewnum, nextperspo, align_active);
2873                         break;
2874
2875                 case RV3D_VIEW_BACK:
2876                         axis_set_view(C, v3d, ar, 0.0, 0.0, -M_SQRT1_2, -M_SQRT1_2,
2877                                       viewnum, nextperspo, align_active);
2878                         break;
2879
2880                 case RV3D_VIEW_LEFT:
2881                         axis_set_view(C, v3d, ar, 0.5, -0.5, 0.5, 0.5,
2882                                       viewnum, nextperspo, align_active);
2883                         break;
2884
2885                 case RV3D_VIEW_TOP:
2886                         axis_set_view(C, v3d, ar, 1.0, 0.0, 0.0, 0.0,
2887                                       viewnum, nextperspo, align_active);
2888                         break;
2889
2890                 case RV3D_VIEW_FRONT:
2891                         axis_set_view(C, v3d, ar, M_SQRT1_2, -M_SQRT1_2, 0.0, 0.0,
2892                                       viewnum, nextperspo, align_active);
2893                         break;
2894
2895                 case RV3D_VIEW_RIGHT:
2896                         axis_set_view(C, v3d, ar, 0.5, -0.5, -0.5, -0.5,
2897                                       viewnum, nextperspo, align_active);
2898                         break;
2899
2900                 case RV3D_VIEW_CAMERA:
2901                         if (rv3d->viewlock == 0) {
2902                                 /* lastview -  */
2903
2904                                 if (rv3d->persp != RV3D_CAMOB) {
2905                                         Object *ob = OBACT;
2906
2907                                         if (!rv3d->smooth_timer) {
2908                                                 /* store settings of current view before allowing overwriting with camera view
2909                                                  * only if we're not currently in a view transition */
2910                                                 copy_qt_qt(rv3d->lviewquat, rv3d->viewquat);
2911                                                 rv3d->lview = rv3d->view;
2912                                                 rv3d->lpersp = rv3d->persp;
2913                                         }
2914
2915         #if 0
2916                                         if (G.qual == LR_ALTKEY) {
2917                                                 if (oldcamera && is_an_active_object(oldcamera)) {
2918                                                         v3d->camera = oldcamera;
2919                                                 }
2920                                                 handle_view3d_lock();
2921                                         }
2922         #endif
2923                                         
2924                                         /* first get the default camera for the view lock type */
2925                                         if (v3d->scenelock) {
2926                                                 /* sets the camera view if available */
2927                                                 v3d->camera = scene->camera;
2928                                         }
2929                                         else {
2930                                                 /* use scene camera if one is not set (even though we're unlocked) */
2931                                                 if (v3d->camera == NULL) {
2932                                                         v3d->camera = scene->camera;
2933                                                 }
2934                                         }
2935
2936                                         /* if the camera isn't found, check a number of options */
2937                                         if (v3d->camera == NULL && ob && ob->type == OB_CAMERA)
2938                                                 v3d->camera = ob;
2939                                         
2940                                         if (v3d->camera == NULL)
2941                                                 v3d->camera = BKE_scene_camera_find(scene);
2942
2943                                         /* couldnt find any useful camera, bail out */
2944                                         if (v3d->camera == NULL)
2945                                                 return OPERATOR_CANCELLED;
2946                                         
2947                                         /* important these don't get out of sync for locked scenes */
2948                                         if (v3d->scenelock)
2949                                                 scene->camera = v3d->camera;
2950
2951                                         /* finally do snazzy view zooming */
2952                                         rv3d->persp = RV3D_CAMOB;
2953                                         smooth_view(C, v3d, ar, NULL, v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens);
2954
2955                                 }
2956                                 else {
2957                                         /* return to settings of last view */
2958                                         /* does smooth_view too */
2959                                         axis_set_view(C, v3d, ar,
2960                                                       rv3d->lviewquat[0], rv3d->lviewquat[1], rv3d->lviewquat[2], rv3d->lviewquat[3],
2961                                                       rv3d->lview, rv3d->lpersp, 0);
2962                                 }
2963                         }
2964                         break;
2965
2966                 default:
2967                         break;
2968         }
2969
2970         if (rv3d->persp != RV3D_CAMOB) perspo = rv3d->persp;
2971
2972         return OPERATOR_FINISHED;
2973 }
2974
2975
2976 void VIEW3D_OT_viewnumpad(wmOperatorType *ot)
2977 {
2978         PropertyRNA *prop;
2979
2980         /* identifiers */
2981         ot->name = "View numpad";
2982         ot->description = "Set the view";
2983         ot->idname = "VIEW3D_OT_viewnumpad";
2984
2985         /* api callbacks */
2986         ot->exec = viewnumpad_exec;
2987         ot->poll = ED_operator_rv3d_user_region_poll;
2988
2989         /* flags */
2990         ot->flag = 0;
2991
2992         prop = RNA_def_enum(ot->srna, "type", prop_view_items, 0, "View", "The Type of view");
2993         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2994         prop = RNA_def_boolean(ot->srna, "align_active", 0, "Align Active", "Align to the active object's axis");
2995         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2996 }
2997
2998 static EnumPropertyItem prop_view_orbit_items[] = {
2999         {V3D_VIEW_STEPLEFT, "ORBITLEFT", 0, "Orbit Left", "Orbit the view around to the Left"},
3000         {V3D_VIEW_STEPRIGHT, "ORBITRIGHT", 0, "Orbit Right", "Orbit the view around to the Right"},
3001         {V3D_VIEW_STEPUP, "ORBITUP", 0, "Orbit Up", "Orbit the view Up"},
3002         {V3D_VIEW_STEPDOWN, "ORBITDOWN", 0, "Orbit Down", "Orbit the view Down"},
3003         {0, NULL, 0, NULL, NULL}
3004 };
3005
3006 static int vieworbit_exec(bContext *C, wmOperator *op)
3007 {
3008         View3D *v3d;
3009         ARegion *ar;
3010         RegionView3D *rv3d;
3011         int orbitdir;
3012
3013         /* no NULL check is needed, poll checks */
3014         ED_view3d_context_user_region(C, &v3d, &ar);
3015         rv3d = ar->regiondata;
3016
3017         orbitdir = RNA_enum_get(op->ptr, "type");
3018
3019         if (rv3d->viewlock == 0) {
3020                 if ((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d)) {
3021                         float angle = DEG2RADF((float)U.pad_rot_angle);
3022                         float quat_mul[4];
3023                         float quat_new[4];
3024
3025                         if (ELEM(orbitdir, V3D_VIEW_STEPLEFT, V3D_VIEW_STEPRIGHT)) {
3026                                 const float zvec[3] = {0.0f, 0.0f, 1.0f};
3027
3028                                 if (orbitdir == V3D_VIEW_STEPRIGHT) {
3029                                         angle = -angle;
3030                                 }
3031
3032                                 /* z-axis */
3033                                 axis_angle_to_quat(quat_mul, zvec, angle);
3034                         }
3035                         else {
3036
3037                                 if (orbitdir == V3D_VIEW_STEPDOWN) {
3038                                         angle = -angle;
3039                                 }
3040
3041                                 /* horizontal axis */
3042                                 axis_angle_to_quat(quat_mul, rv3d->viewinv[0], angle);
3043                         }
3044
3045                         mul_qt_qtqt(quat_new, rv3d->viewquat, quat_mul);
3046                         rv3d->view = RV3D_VIEW_USER;
3047
3048                         smooth_view(C, CTX_wm_view3d(C), ar, NULL, NULL, NULL, quat_new, NULL, NULL);
3049
3050                         return OPERATOR_FINISHED;
3051                 }
3052         }
3053
3054         return OPERATOR_CANCELLED;
3055 }
3056
3057 void VIEW3D_OT_view_orbit(wmOperatorType *ot)
3058 {
3059         /* identifiers */
3060         ot->name = "View Orbit";
3061         ot->description = "Orbit the view";
3062         ot->idname = "VIEW3D_OT_view_orbit";
3063
3064         /* api callbacks */
3065         ot->exec = vieworbit_exec;
3066         ot->poll = ED_operator_rv3d_user_region_poll;
3067
3068         /* flags */
3069         ot->flag = 0;
3070         RNA_def_enum(ot->srna, "type", prop_view_orbit_items, 0, "Orbit", "Direction of View Orbit");
3071 }
3072
3073 static EnumPropertyItem prop_view_pan_items[] = {
3074         {V3D_VIEW_PANLEFT, "PANLEFT", 0, "Pan Left", "Pan the view to the Left"},
3075         {V3D_VIEW_PANRIGHT, "PANRIGHT", 0, "Pan Right", "Pan the view to the Right"},
3076         {V3D_VIEW_PANUP, "PANUP", 0, "Pan Up", "Pan the view Up"},
3077         {V3D_VIEW_PANDOWN, "PANDOWN", 0, "Pan Down", "Pan the view Down"},
3078         {0, NULL, 0, NULL, NULL}
3079 };
3080
3081 static int viewpan_exec(bContext *C, wmOperator *op)
3082 {
3083         ARegion *ar = CTX_wm_region(C);
3084         RegionView3D *rv3d = CTX_wm_region_view3d(C);
3085         float vec[3];
3086         float mval_f[2] = {0.0f, 0.0f};
3087         int pandir;
3088
3089         pandir = RNA_enum_get(op->ptr, "type");
3090
3091         initgrabz(rv3d, 0.0, 0.0, 0.0);
3092         if      (pandir == V3D_VIEW_PANRIGHT)  { mval_f[0] = -32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
3093         else if (pandir == V3D_VIEW_PANLEFT)   { mval_f[0] =  32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
3094         else if (pandir == V3D_VIEW_PANUP)     { mval_f[1] = -25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
3095         else if (pandir == V3D_VIEW_PANDOWN)   { mval_f[1] =  25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
3096         add_v3_v3(rv3d->ofs, vec);
3097
3098         if (rv3d->viewlock & RV3D_BOXVIEW)
3099                 view3d_boxview_sync(CTX_wm_area(C), ar);
3100
3101         ED_region_tag_redraw(ar);
3102
3103         return OPERATOR_FINISHED;
3104 }
3105
3106 void VIEW3D_OT_view_pan(wmOperatorType *ot)
3107 {
3108         /* identifiers */
3109         ot->name = "View Pan";
3110         ot->description = "Pan