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