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