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