2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_view3d/view3d_edit.c
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"
43 #include "MEM_guardedalloc.h"
45 #include "BLI_blenlib.h"
48 #include "BLI_utildefines.h"
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 */
63 #include "BIF_glutil.h"
68 #include "RNA_access.h"
69 #include "RNA_define.h"
71 #include "ED_particle.h"
72 #include "ED_screen.h"
73 #include "ED_transform.h"
75 #include "ED_view3d.h"
78 #include "PIL_time.h" /* smoothview */
80 #include "view3d_intern.h" // own include
82 /* ********************** view3d_edit: view manipulations ********************* */
84 int ED_view3d_camera_lock_check(View3D *v3d, RegionView3D *rv3d)
86 return ((v3d->camera) &&
87 (v3d->camera->id.lib == NULL) &&
88 (v3d->flag2 & V3D_LOCK_CAMERA) &&
89 (rv3d->persp==RV3D_CAMOB));
92 void ED_view3d_camera_lock_init(View3D *v3d, RegionView3D *rv3d)
94 if(ED_view3d_camera_lock_check(v3d, rv3d)) {
95 ED_view3d_from_object(v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
99 /* return TRUE if the camera is moved */
100 int ED_view3d_camera_lock_sync(View3D *v3d, RegionView3D *rv3d)
102 if(ED_view3d_camera_lock_check(v3d, rv3d)) {
103 ObjectTfmProtectedChannels obtfm;
106 if((U.uiflag & USER_CAM_LOCK_NO_PARENT)==0 && (root_parent= v3d->camera->parent)) {
108 float view_mat[4][4];
109 float diff_mat[4][4];
110 float parent_mat[4][4];
112 while(root_parent->parent) {
113 root_parent= root_parent->parent;
116 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
118 invert_m4_m4(v3d->camera->imat, v3d->camera->obmat);
119 mult_m4_m4m4(diff_mat, view_mat, v3d->camera->imat);
121 mult_m4_m4m4(parent_mat, diff_mat, root_parent->obmat);
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);
127 ob_update= v3d->camera;
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;
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);
139 DAG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
140 WM_main_add_notifier(NC_OBJECT|ND_TRANSFORM, v3d->camera);
151 /* ********************* box view support ***************** */
153 static void view3d_boxview_clip(ScrArea *sa)
156 BoundBox *bb = MEM_callocN(sizeof(BoundBox), "clipbb");
158 float x1= 0.0f, y1= 0.0f, z1= 0.0f, ofs[3] = {0.0f, 0.0f, 0.0f};
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;
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;
171 if(ar->winx>ar->winy) y1= ar->winy*rv3d->dist/ar->winx;
173 copy_v2_v2(ofs, rv3d->ofs);
175 else if(ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
176 ofs[2]= rv3d->ofs[2];
178 if(ar->winx>ar->winy) z1= ar->winy*rv3d->dist/ar->winx;
185 for(val=0; val<8; val++) {
186 if(ELEM4(val, 0, 3, 4, 7))
187 bb->vec[val][0]= -x1 - ofs[0];
189 bb->vec[val][0]= x1 - ofs[0];
191 if(ELEM4(val, 0, 1, 4, 5))
192 bb->vec[val][1]= -y1 - ofs[1];
194 bb->vec[val][1]= y1 - ofs[1];
197 bb->vec[val][2]= -z1 - ofs[2];
199 bb->vec[val][2]= z1 - ofs[2];
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]);
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];
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];
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;
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);
232 /* sync center/zoom view of region to others, for view transforms */
233 static void view3d_boxview_sync(ScrArea *sa, ARegion *ar)
236 RegionView3D *rv3d= ar->regiondata;
239 for(artest= sa->regionbase.first; artest; artest= artest->next) {
240 if(artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
241 RegionView3D *rv3dtest= artest->regiondata;
243 if(rv3dtest->viewlock) {
244 rv3dtest->dist= rv3d->dist;
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];
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];
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];
265 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
267 ED_region_tag_redraw(artest);
273 view3d_boxview_clip(sa);
277 /* for home, center etc */
278 void view3d_boxview_copy(ScrArea *sa, ARegion *ar)
281 RegionView3D *rv3d= ar->regiondata;
284 for(artest= sa->regionbase.first; artest; artest= artest->next) {
285 if(artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
286 RegionView3D *rv3dtest= artest->regiondata;
288 if(rv3dtest->viewlock) {
289 rv3dtest->dist= rv3d->dist;
290 copy_v3_v3(rv3dtest->ofs, rv3d->ofs);
291 ED_region_tag_redraw(artest);
293 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
299 view3d_boxview_clip(sa);
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)
306 ARegion *arsync= NULL;
307 RegionView3D *rv3d= ar->regiondata;
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;
314 if((viewlock & RV3D_LOCKED)==0)
316 else if((viewlock & RV3D_BOXVIEW)==0) {
317 viewlock &= ~RV3D_BOXCLIP;
321 for(; ar; ar= ar->prev) {
322 if(ar->alignment==RGN_ALIGN_QSPLIT) {
323 rv3d= ar->regiondata;
324 rv3d->viewlock= viewlock;
326 if(do_clip && (viewlock & RV3D_BOXCLIP)==0) {
327 rv3d->rflag &= ~RV3D_BOXCLIP;
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 */
337 if(rv3d->viewlock & RV3D_BOXVIEW) {
338 view3d_boxview_copy(sa, arsync ? arsync : sa->regionbase.last);
341 ED_area_tag_redraw(sa);
344 /* ************************** init for view ops **********************************/
346 typedef struct ViewOpsData {
352 /* needed for continuous zoom */
354 double timer_lastdraw;
357 float viewquat[4]; /* working copy of rv3d->viewquat */
359 float mousevec[3]; /* dolly only */
360 float reverse, dist0;
362 short axis_snap; /* view rotate only */
364 /* use for orbit selection and auto-dist */
365 float ofs[3], dyn_ofs[3];
368 int origx, origy, oldx, oldy;
369 int origkey; /* the key that triggered the operator */
373 #define TRACKBALLSIZE (1.1)
375 static void calctrackballvec(rcti *rect, int mx, int my, float *vec)
377 float x, y, radius, d, z, t;
379 radius= TRACKBALLSIZE;
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);
388 if (d < radius * (float)M_SQRT1_2) { /* Inside sphere */
389 z= sqrt(radius*radius - d*d);
391 else { /* On hyperbola */
392 t= radius / (float)M_SQRT2;
398 vec[2]= -z; /* yah yah! */
402 static void viewops_data_create(bContext *C, wmOperator *op, wmEvent *event)
404 static float lastofs[3] = {0,0,0};
406 ViewOpsData *vod= MEM_callocN(sizeof(ViewOpsData), "viewops data");
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;
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);
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);
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);
433 else if (U.uiflag & USER_ORBIT_ZBUF) {
435 view3d_operator_needs_opengl(C); /* needed for zbuf drawing */
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 */
443 // locals for dist correction
447 negate_v3_v3(my_origin, rv3d->ofs); /* ofs is flipped */
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 */
452 /* remove dist value */
453 upvec[0] = upvec[1] = 0;
454 upvec[2] = rv3d->dist;
455 copy_m3_m4(mat, rv3d->viewinv);
457 mul_m3_v3(mat, upvec);
458 sub_v3_v3v3(my_pivot, rv3d->ofs, upvec);
459 negate_v3(my_pivot); /* ofs is flipped */
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);
465 negate_v3_v3(rv3d->ofs, dvec);
467 negate_v3(vod->dyn_ofs);
468 copy_v3_v3(vod->ofs, rv3d->ofs);
475 VECCOPY2D(mval_f, event->mval);
476 ED_view3d_win_to_vector(vod->ar, mval_f, vod->mousevec);
479 /* lookup, we dont pass on v3d to prevent confusement */
480 vod->grid= vod->v3d->grid;
481 vod->far= vod->v3d->far;
483 calctrackballvec(&vod->ar->winrct, event->x, event->y, vod->trackvec);
485 initgrabz(rv3d, -rv3d->ofs[0], -rv3d->ofs[1], -rv3d->ofs[2]);
488 if (rv3d->persmat[2][1] < 0.0f)
491 rv3d->rflag |= RV3D_NAVIGATING;
494 static void viewops_data_free(bContext *C, wmOperator *op)
497 Paint *p = paint_get_active(CTX_data_scene(C));
500 ViewOpsData *vod= op->customdata;
502 vod->rv3d->rflag &= ~RV3D_NAVIGATING;
505 WM_event_remove_timer(CTX_wm_manager(C), vod->timer->win, vod->timer);
508 op->customdata= NULL;
511 ar= CTX_wm_region(C);
514 if(p && (p->flags & PAINT_FAST_NAVIGATE))
515 ED_region_tag_redraw(ar);
518 /* ************************** viewrotate **********************************/
520 static const float thres = 0.93f; //cos(20 deg);
522 #define COS45 0.70710678118654746
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
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}
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
584 /* called in transform_ops.c, on each regeneration of keymaps */
585 void viewrotate_modal_keymap(wmKeyConfig *keyconf)
587 static EnumPropertyItem modal_items[] = {
588 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
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", ""},
593 {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
594 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
596 {0, NULL, 0, NULL, NULL}};
598 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Rotate Modal");
600 /* this function is called for each spacetype, only needs to add map once */
603 keymap= WM_modalkeymap_add(keyconf, "View3D Rotate Modal", modal_items);
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);
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);
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);
618 /* assign map to operators */
619 WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");
623 static void viewrotate_apply(ViewOpsData *vod, int x, int y)
625 RegionView3D *rv3d= vod->rv3d;
627 rv3d->view= RV3D_VIEW_USER; /* need to reset everytime because of view snapping */
629 if (U.flag & USER_TRACKBALL) {
630 float phi, si, q1[4], dvec[3], newvec[3];
632 calctrackballvec(&vod->ar->winrct, x, y, newvec);
634 sub_v3_v3v3(dvec, newvec, vod->trackvec);
636 si= sqrt(dvec[0]*dvec[0]+ dvec[1]*dvec[1]+ dvec[2]*dvec[2]);
637 si /= (float)(2.0 * TRACKBALLSIZE);
639 cross_v3_v3v3(q1+1, vod->trackvec, newvec);
642 /* Allow for rotation beyond the interval
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
652 phi = si * (float)(M_PI / 2.0);
655 mul_v3_fl(q1+1, sin(phi));
656 mul_qt_qtqt(vod->viewquat, q1, vod->oldquat);
658 if (vod->use_dyn_ofs) {
659 /* compute the post multiplication quat, to rotate the offset correctly */
660 copy_qt_qt(q1, vod->oldquat);
662 mul_qt_qtqt(q1, q1, vod->viewquat);
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);
672 /* New turntable view code by John Aughey */
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;
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);
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);
691 /* Perform the up/down rotation */
692 phi = sensitivity * -(y - vod->oldy);
694 mul_v3_v3fl(q1+1, xvec, sin(phi));
695 mul_qt_qtqt(vod->viewquat, vod->viewquat, q1);
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);
704 /* Perform the orbital rotation */
705 phi = sensitivity * vod->reverse * (x - vod->oldx);
709 mul_qt_qtqt(vod->viewquat, vod->viewquat, q1);
711 if (vod->use_dyn_ofs) {
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);
719 /* check for view snap */
722 float viewquat_inv[4];
723 float zaxis[3]={0,0,1};
724 invert_qt_qt(viewquat_inv, vod->viewquat);
726 mul_qt_v3(viewquat_inv, zaxis);
728 for (i = 0 ; i < 39; i++){
730 float view = (int)snapquats[i][4];
731 float viewquat_inv_test[4];
732 float zaxis_test[3]={0,0,1};
734 invert_qt_qt(viewquat_inv_test, snapquats[i]);
735 mul_qt_v3(viewquat_inv_test, zaxis_test);
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;
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);
754 for(j= 0; j<8; j++) {
756 float xaxis1[3]={1,0,0};
757 float xaxis2[3]={1,0,0};
758 float quat_final_inv[4];
760 axis_angle_to_quat(quat_roll, zaxis_test, (float)j * DEG2RADF(45.0f));
761 normalize_qt(quat_roll);
763 mul_qt_qtqt(quat_final, snapquats[i], quat_roll);
764 normalize_qt(quat_final);
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);
772 if(angle <= best_angle) {
774 copy_qt_qt(quat_best, quat_final);
775 if(j) view= 0; /* view grid assumes certain up axis */
779 copy_qt_qt(vod->viewquat, quat_best);
780 rv3d->view= view; /* if we snap to a rolled camera the grid is invalid */
789 /* avoid precision loss over time */
790 normalize_qt(vod->viewquat);
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);
796 ED_view3d_camera_lock_sync(vod->v3d, rv3d);
798 ED_region_tag_redraw(vod->ar);
801 static int viewrotate_modal(bContext *C, wmOperator *op, wmEvent *event)
803 ViewOpsData *vod= op->customdata;
804 short event_code= VIEW_PASS;
806 /* execute the events */
807 if(event->type==MOUSEMOVE) {
808 event_code= VIEW_APPLY;
810 else if(event->type==EVT_MODAL_MAP) {
811 switch (event->val) {
812 case VIEW_MODAL_CONFIRM:
813 event_code= VIEW_CONFIRM;
815 case VIEWROT_MODAL_AXIS_SNAP_ENABLE:
816 vod->axis_snap= TRUE;
817 event_code= VIEW_APPLY;
819 case VIEWROT_MODAL_AXIS_SNAP_DISABLE:
820 vod->axis_snap= FALSE;
821 event_code= VIEW_APPLY;
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;
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;
833 else if(event->type==vod->origkey && event->val==KM_RELEASE) {
834 event_code= VIEW_CONFIRM;
837 if(event_code==VIEW_APPLY) {
838 viewrotate_apply(vod, event->x, event->y);
840 else if (event_code==VIEW_CONFIRM) {
841 ED_view3d_depth_tag_update(vod->rv3d);
842 viewops_data_free(C, op);
844 return OPERATOR_FINISHED;
847 return OPERATOR_RUNNING_MODAL;
850 static int viewrotate_invoke(bContext *C, wmOperator *op, wmEvent *event)
855 /* makes op->customdata */
856 viewops_data_create(C, op, event);
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;
865 /* switch from camera view when: */
866 if(rv3d->persp != RV3D_PERSP) {
868 if (U.uiflag & USER_AUTOPERSP) {
869 if(!ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
870 rv3d->persp= RV3D_PERSP;
873 else if(rv3d->persp==RV3D_CAMOB) {
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);
880 if(!ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
881 rv3d->persp= rv3d->lpersp;
884 ED_region_tag_redraw(vod->ar);
887 if (event->type == MOUSEPAN) {
888 viewrotate_apply(vod, event->prevx, event->prevy);
889 ED_view3d_depth_tag_update(rv3d);
891 viewops_data_free(C, op);
893 return OPERATOR_FINISHED;
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);
900 viewops_data_free(C, op);
902 return OPERATOR_FINISHED;
905 /* add temp handler */
906 WM_event_add_modal_handler(C, op);
908 return OPERATOR_RUNNING_MODAL;
912 static int view3d_camera_active_poll(bContext *C)
914 if(ED_operator_view3d_active(C)) {
915 RegionView3D *rv3d= CTX_wm_region_view3d(C);
916 if(rv3d && rv3d->persp==RV3D_CAMOB) {
924 static int viewrotate_cancel(bContext *C, wmOperator *op)
926 viewops_data_free(C, op);
928 return OPERATOR_CANCELLED;
931 void VIEW3D_OT_rotate(wmOperatorType *ot)
935 ot->name= "Rotate view";
936 ot->description = "Rotate the view";
937 ot->idname= "VIEW3D_OT_rotate";
940 ot->invoke= viewrotate_invoke;
941 ot->modal= viewrotate_modal;
942 ot->poll= ED_operator_region_view3d_active;
943 ot->cancel= viewrotate_cancel;
946 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
949 // NDOF utility functions
950 // (should these functions live in this file?)
951 float ndof_to_axis_angle(struct wmNDOFMotionData* ndof, float axis[3])
953 return ndof->dt * normalize_v3_v3(axis, ndof->rvec);
956 void ndof_to_quat(struct wmNDOFMotionData* ndof, float q[4])
961 angle= ndof_to_axis_angle(ndof, axis);
962 axis_angle_to_quat(q, axis, angle);
965 /* -- "orbit" navigation (trackball/turntable)
967 * -- panning in rotationally-locked views
969 static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
971 if (event->type != NDOF_MOTION) {
972 return OPERATOR_CANCELLED;
975 View3D *v3d= CTX_wm_view3d(C);
976 RegionView3D* rv3d = CTX_wm_region_view3d(C);
977 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
979 ED_view3d_camera_lock_init(v3d, rv3d);
981 rv3d->rot_angle = 0.f; // off by default, until changed later this function
983 if (ndof->progress != P_FINISHING) {
984 const float dt = ndof->dt;
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;
991 // rather have bool, but...
992 int has_rotation = rv3d->viewlock != RV3D_LOCKED && !is_zero_v3(ndof->rvec);
995 invert_qt_qt(view_inv, rv3d->viewquat);
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);
1003 if (ndof->tvec[2]) {
1005 // velocity should be proportional to the linear velocity attained by rotational motion of same strength
1007 // proportional to arclength = radius * angle
1009 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tvec[2];
1011 if (U.ndof_flag & NDOF_ZOOM_INVERT)
1012 zoom_distance = -zoom_distance;
1014 rv3d->dist += zoom_distance;
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);
1022 /* transform motion from view to world coordinates */
1023 invert_qt_qt(view_inv, rv3d->viewquat);
1024 mul_qt_v3(view_inv, pan_vec);
1026 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1027 sub_v3_v3(rv3d->ofs, pan_vec);
1032 rv3d->view = RV3D_VIEW_USER;
1034 if (U.flag & USER_TRACKBALL) {
1037 float angle = rot_sensitivity * ndof_to_axis_angle(ndof, axis);
1039 if (U.ndof_flag & NDOF_ROLL_INVERT_AXIS)
1042 if (U.ndof_flag & NDOF_TILT_INVERT_AXIS)
1045 if (U.ndof_flag & NDOF_ROTATE_INVERT_AXIS)
1048 // transform rotation axis from view to world coordinates
1049 mul_qt_v3(view_inv, axis);
1051 // update the onscreen doo-dad
1052 rv3d->rot_angle = angle;
1053 copy_v3_v3(rv3d->rot_axis, axis);
1055 axis_angle_to_quat(rot, axis, angle);
1058 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
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};
1064 /* Determine the direction of the x vector (for rotating up and down) */
1065 mul_qt_v3(view_inv, xvec);
1067 /* Perform the up/down rotation */
1068 angle = rot_sensitivity * dt * ndof->rvec[0];
1069 if (U.ndof_flag & NDOF_TILT_INVERT_AXIS)
1071 rot[0] = cos(angle);
1072 mul_v3_v3fl(rot+1, xvec, sin(angle));
1073 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1075 /* Perform the orbital rotation */
1076 angle = rot_sensitivity * dt * ndof->rvec[1];
1077 if (U.ndof_flag & NDOF_ROTATE_INVERT_AXIS)
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;
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);
1094 ED_view3d_camera_lock_sync(v3d, rv3d);
1096 ED_region_tag_redraw(CTX_wm_region(C));
1098 return OPERATOR_FINISHED;
1102 void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot)
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";
1110 ot->invoke = ndof_orbit_invoke;
1111 ot->poll = ED_operator_view3d_active;
1117 /* -- "pan" navigation
1120 static int ndof_pan_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
1122 if (event->type != NDOF_MOTION) {
1123 return OPERATOR_CANCELLED;
1126 View3D *v3d= CTX_wm_view3d(C);
1127 RegionView3D* rv3d = CTX_wm_region_view3d(C);
1128 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
1130 ED_view3d_camera_lock_init(v3d, rv3d);
1132 rv3d->rot_angle = 0.f; // we're panning here! so erase any leftover rotation from other operators
1134 if (ndof->progress != P_FINISHING) {
1135 const float dt = ndof->dt;
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;
1142 float pan_vec[3] = {
1143 ndof->tx, ndof->ty, 0
1146 // "zoom in" or "translate"? depends on zoom mode in user settings?
1148 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tz;
1149 rv3d->dist += zoom_distance;
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
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;
1164 if (U.ndof_flag & NDOF_PANX_INVERT_AXIS)
1165 pan_vec[0] = -lateral_sensitivity * ndof->tvec[0];
1167 pan_vec[0] = lateral_sensitivity * ndof->tvec[0];
1169 if (U.ndof_flag & NDOF_PANZ_INVERT_AXIS)
1170 pan_vec[1] = -vertical_sensitivity * ndof->tvec[1];
1172 pan_vec[1] = vertical_sensitivity * ndof->tvec[1];
1174 if (U.ndof_flag & NDOF_PANY_INVERT_AXIS)
1175 pan_vec[2] = -forward_sensitivity * ndof->tvec[2];
1177 pan_vec[2] = forward_sensitivity * ndof->tvec[2];
1179 mul_v3_fl(pan_vec, speed * dt);
1181 /* transform motion from view to world coordinates */
1182 invert_qt_qt(view_inv, rv3d->viewquat);
1183 mul_qt_v3(view_inv, pan_vec);
1185 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1186 sub_v3_v3(rv3d->ofs, pan_vec);
1189 ED_view3d_camera_lock_sync(v3d, rv3d);
1191 ED_region_tag_redraw(CTX_wm_region(C));
1193 return OPERATOR_FINISHED;
1197 void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot)
1200 ot->name = "NDOF Pan View";
1201 ot->description = "Position your viewpoint with the 3D mouse";
1202 ot->idname = "VIEW3D_OT_ndof_pan";
1205 ot->invoke = ndof_pan_invoke;
1206 ot->poll = ED_operator_view3d_active;
1212 /* ************************ viewmove ******************************** */
1215 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
1217 /* called in transform_ops.c, on each regeneration of keymaps */
1218 void viewmove_modal_keymap(wmKeyConfig *keyconf)
1220 static EnumPropertyItem modal_items[] = {
1221 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
1223 {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
1224 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1226 {0, NULL, 0, NULL, NULL}};
1228 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Move Modal");
1230 /* this function is called for each spacetype, only needs to add map once */
1233 keymap= WM_modalkeymap_add(keyconf, "View3D Move Modal", modal_items);
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);
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);
1245 /* assign map to operators */
1246 WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
1250 static void viewmove_apply(ViewOpsData *vod, int x, int y)
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);
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);
1267 add_v3_v3(vod->rv3d->ofs, dvec);
1269 if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1270 view3d_boxview_sync(vod->sa, vod->ar);
1276 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1278 ED_region_tag_redraw(vod->ar);
1282 static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
1285 ViewOpsData *vod= op->customdata;
1286 short event_code= VIEW_PASS;
1288 /* execute the events */
1289 if(event->type==MOUSEMOVE) {
1290 event_code= VIEW_APPLY;
1292 else if(event->type==EVT_MODAL_MAP) {
1293 switch (event->val) {
1294 case VIEW_MODAL_CONFIRM:
1295 event_code= VIEW_CONFIRM;
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;
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;
1307 else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1308 event_code= VIEW_CONFIRM;
1311 if(event_code==VIEW_APPLY) {
1312 viewmove_apply(vod, event->x, event->y);
1314 else if (event_code==VIEW_CONFIRM) {
1315 ED_view3d_depth_tag_update(vod->rv3d);
1317 viewops_data_free(C, op);
1319 return OPERATOR_FINISHED;
1322 return OPERATOR_RUNNING_MODAL;
1325 static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
1329 /* makes op->customdata */
1330 viewops_data_create(C, op, event);
1331 vod= op->customdata;
1333 if (event->type == MOUSEPAN) {
1334 viewmove_apply(vod, event->prevx, event->prevy);
1335 ED_view3d_depth_tag_update(vod->rv3d);
1337 viewops_data_free(C, op);
1339 return OPERATOR_FINISHED;
1342 /* add temp handler */
1343 WM_event_add_modal_handler(C, op);
1345 return OPERATOR_RUNNING_MODAL;
1349 static int viewmove_cancel(bContext *C, wmOperator *op)
1351 viewops_data_free(C, op);
1353 return OPERATOR_CANCELLED;
1356 void VIEW3D_OT_move(wmOperatorType *ot)
1360 ot->name= "Move view";
1361 ot->description = "Move the view";
1362 ot->idname= "VIEW3D_OT_move";
1365 ot->invoke= viewmove_invoke;
1366 ot->modal= viewmove_modal;
1367 ot->poll= ED_operator_view3d_active;
1368 ot->cancel= viewmove_cancel;
1371 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1374 /* ************************ viewzoom ******************************** */
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)
1380 static EnumPropertyItem modal_items[] = {
1381 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
1383 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1384 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1386 {0, NULL, 0, NULL, NULL}};
1388 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Zoom Modal");
1390 /* this function is called for each spacetype, only needs to add map once */
1393 keymap= WM_modalkeymap_add(keyconf, "View3D Zoom Modal", modal_items);
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);
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);
1405 /* assign map to operators */
1406 WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
1409 static void view_zoom_mouseloc(ARegion *ar, float dfac, int mx, int my)
1411 RegionView3D *rv3d= ar->regiondata;
1413 if(U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
1420 negate_v3_v3(tpos, rv3d->ofs);
1422 /* Project cursor position into 3D space */
1423 initgrabz(rv3d, tpos[0], tpos[1], tpos[2]);
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);
1429 /* Calculate view target position for dolly */
1430 add_v3_v3v3(tvec, tpos, dvec);
1433 /* Offset to target position and dolly */
1434 new_dist = rv3d->dist * dfac;
1436 copy_v3_v3(rv3d->ofs, tvec);
1437 rv3d->dist = new_dist;
1439 /* Calculate final offset */
1440 madd_v3_v3v3fl(rv3d->ofs, tvec, dvec, dfac);
1447 static void viewzoom_apply(ViewOpsData *vod, int x, int y, const short viewzoom, const short zoom_invert)
1451 if(viewzoom==USER_ZOOM_CONT) {
1452 double time= PIL_check_seconds_timer();
1453 float time_step= (float)(time - vod->timer_lastdraw);
1456 if (U.uiflag & USER_ZOOM_HORIZ) {
1457 fac= (float)(x - vod->origx);
1460 fac= (float)(y - vod->origy);
1468 zfac = 1.0f + ((fac / 20.0f) * time_step);
1469 vod->timer_lastdraw= time;
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
1475 ctr[0] = (vod->ar->winrct.xmax + vod->ar->winrct.xmin)/2;
1476 ctr[1] = (vod->ar->winrct.ymax + vod->ar->winrct.ymin)/2;
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;
1481 zfac = vod->dist0 * ((float)len2/len1) / vod->rv3d->dist;
1483 else { /* USER_ZOOM_DOLLY */
1486 if (U.uiflag & USER_ZOOM_HORIZ) {
1487 len1 = (vod->ar->winrct.xmax - x) + 5;
1488 len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1491 len1 = (vod->ar->winrct.ymax - y) + 5;
1492 len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1495 SWAP(float, len1, len2);
1498 zfac = vod->dist0 * (2.0f * ((len2/len1)-1.0f) + 1.0f) / vod->rv3d->dist;
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);
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;
1509 if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1510 view3d_boxview_sync(vod->sa, vod->ar);
1512 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1514 ED_region_tag_redraw(vod->ar);
1518 static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
1520 ViewOpsData *vod= op->customdata;
1521 short event_code= VIEW_PASS;
1523 /* execute the events */
1524 if (event->type == TIMER && event->customdata == vod->timer) {
1525 /* continuous zoom */
1526 event_code= VIEW_APPLY;
1528 else if(event->type==MOUSEMOVE) {
1529 event_code= VIEW_APPLY;
1531 else if(event->type==EVT_MODAL_MAP) {
1532 switch (event->val) {
1533 case VIEW_MODAL_CONFIRM:
1534 event_code= VIEW_CONFIRM;
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;
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;
1546 else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1547 event_code= VIEW_CONFIRM;
1550 if(event_code==VIEW_APPLY) {
1551 viewzoom_apply(vod, event->x, event->y, U.viewzoom, (U.uiflag & USER_ZOOM_INVERT) != 0);
1553 else if (event_code==VIEW_CONFIRM) {
1554 ED_view3d_depth_tag_update(vod->rv3d);
1555 viewops_data_free(C, op);
1557 return OPERATOR_FINISHED;
1560 return OPERATOR_RUNNING_MODAL;
1563 static int viewzoom_exec(bContext *C, wmOperator *op)
1571 int delta= RNA_int_get(op->ptr, "delta");
1574 if(op->customdata) {
1575 ViewOpsData *vod= op->customdata;
1582 ar= CTX_wm_region(C);
1585 v3d= sa->spacedata.first;
1586 rv3d= ar->regiondata;
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;
1591 use_cam_zoom= (rv3d->persp==RV3D_CAMOB) && !(rv3d->is_persp && ED_view3d_camera_lock_check(v3d, rv3d));
1594 /* this min and max is also in viewmove() */
1597 if(rv3d->camzoom < RV3D_CAMZOOM_MIN) rv3d->camzoom= RV3D_CAMZOOM_MIN;
1599 else if(rv3d->dist < 10.0f * v3d->far) {
1600 view_zoom_mouseloc(ar, 1.2f, mx, my);
1606 if(rv3d->camzoom > RV3D_CAMZOOM_MAX) rv3d->camzoom= RV3D_CAMZOOM_MAX;
1608 else if(rv3d->dist> 0.001f * v3d->grid) {
1609 view_zoom_mouseloc(ar, .83333f, mx, my);
1613 if(rv3d->viewlock & RV3D_BOXVIEW)
1614 view3d_boxview_sync(sa, ar);
1616 ED_view3d_depth_tag_update(rv3d);
1618 ED_view3d_camera_lock_sync(v3d, rv3d);
1620 ED_region_tag_redraw(ar);
1622 viewops_data_free(C, op);
1624 return OPERATOR_FINISHED;
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)
1631 static EnumPropertyItem modal_items[] = {
1632 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
1634 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1635 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1637 {0, NULL, 0, NULL, NULL}};
1639 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Dolly Modal");
1641 /* this function is called for each spacetype, only needs to add map once */
1644 keymap= WM_modalkeymap_add(keyconf, "View3D Dolly Modal", modal_items);
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);
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);
1656 /* assign map to operators */
1657 WM_modalkeymap_assign(keymap, "VIEW3D_OT_dolly");
1660 /* viewdolly_invoke() copied this function, changes here may apply there */
1661 static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
1665 /* makes op->customdata */
1666 viewops_data_create(C, op, event);
1667 vod= op->customdata;
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);
1675 if(RNA_struct_property_is_set(op->ptr, "delta")) {
1676 viewzoom_exec(C, op);
1679 if (event->type == MOUSEZOOM) {
1680 /* Bypass Zoom invert flag for track pads (pass FALSE always) */
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);
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);
1691 ED_view3d_depth_tag_update(vod->rv3d);
1693 viewops_data_free(C, op);
1694 return OPERATOR_FINISHED;
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();
1703 /* add temp handler */
1704 WM_event_add_modal_handler(C, op);
1706 return OPERATOR_RUNNING_MODAL;
1709 return OPERATOR_FINISHED;
1712 static int viewzoom_cancel(bContext *C, wmOperator *op)
1714 viewops_data_free(C, op);
1716 return OPERATOR_CANCELLED;
1719 void VIEW3D_OT_zoom(wmOperatorType *ot)
1722 ot->name= "Zoom View";
1723 ot->description = "Zoom in/out in the view";
1724 ot->idname= "VIEW3D_OT_zoom";
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;
1734 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
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);
1742 /* ************************ viewdolly ******************************** */
1743 static void view_dolly_mouseloc(ARegion *ar, float orig_ofs[3], float dvec[3], float dfac)
1745 RegionView3D *rv3d= ar->regiondata;
1746 madd_v3_v3v3fl(rv3d->ofs, orig_ofs, dvec, -(1.0f - dfac));
1749 static void viewdolly_apply(ViewOpsData *vod, int x, int y, const short zoom_invert)
1756 if (U.uiflag & USER_ZOOM_HORIZ) {
1757 len1 = (vod->ar->winrct.xmax - x) + 5;
1758 len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1761 len1 = (vod->ar->winrct.ymax - y) + 5;
1762 len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1765 SWAP(float, len1, len2);
1767 zfac = 1.0f + ((len2 - len1) * 0.01f * vod->rv3d->dist);
1771 view_dolly_mouseloc(vod->ar, vod->ofs, vod->mousevec, zfac);
1773 if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1774 view3d_boxview_sync(vod->sa, vod->ar);
1776 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1778 ED_region_tag_redraw(vod->ar);
1782 static int viewdolly_modal(bContext *C, wmOperator *op, wmEvent *event)
1784 ViewOpsData *vod= op->customdata;
1785 short event_code= VIEW_PASS;
1787 /* execute the events */
1788 if(event->type==MOUSEMOVE) {
1789 event_code= VIEW_APPLY;
1791 else if(event->type==EVT_MODAL_MAP) {
1792 switch (event->val) {
1793 case VIEW_MODAL_CONFIRM:
1794 event_code= VIEW_CONFIRM;
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;
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;
1806 else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1807 event_code= VIEW_CONFIRM;
1810 if(event_code==VIEW_APPLY) {
1811 viewdolly_apply(vod, event->x, event->y, (U.uiflag & USER_ZOOM_INVERT) != 0);
1813 else if (event_code==VIEW_CONFIRM) {
1814 ED_view3d_depth_tag_update(vod->rv3d);
1815 viewops_data_free(C, op);
1817 return OPERATOR_FINISHED;
1820 return OPERATOR_RUNNING_MODAL;
1823 static int viewdolly_exec(bContext *C, wmOperator *op)
1831 int delta= RNA_int_get(op->ptr, "delta");
1833 if(op->customdata) {
1834 ViewOpsData *vod= op->customdata;
1838 copy_v3_v3(mousevec, vod->mousevec);
1842 ar= CTX_wm_region(C);
1843 negate_v3_v3(mousevec, ((RegionView3D *)ar->regiondata)->viewinv[2]);
1844 normalize_v3(mousevec);
1847 /* v3d= sa->spacedata.first; */ /* UNUSED */
1848 rv3d= ar->regiondata;
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]);
1856 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, 1.2f);
1859 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, .83333f);
1862 if(rv3d->viewlock & RV3D_BOXVIEW)
1863 view3d_boxview_sync(sa, ar);
1865 ED_view3d_depth_tag_update(rv3d);
1866 ED_region_tag_redraw(ar);
1868 viewops_data_free(C, op);
1870 return OPERATOR_FINISHED;
1873 /* copied from viewzoom_invoke(), changes here may apply there */
1874 static int viewdolly_invoke(bContext *C, wmOperator *op, wmEvent *event)
1878 /* makes op->customdata */
1879 viewops_data_create(C, op, event);
1880 vod= op->customdata;
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);
1888 if(RNA_struct_property_is_set(op->ptr, "delta")) {
1889 viewdolly_exec(C, op);
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);
1898 if (event->type == MOUSEZOOM) {
1899 /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1901 if (U.uiflag & USER_ZOOM_HORIZ) {
1902 vod->origx = vod->oldx = event->x;
1903 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
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);
1911 ED_view3d_depth_tag_update(vod->rv3d);
1913 viewops_data_free(C, op);
1914 return OPERATOR_FINISHED;
1917 /* add temp handler */
1918 WM_event_add_modal_handler(C, op);
1920 return OPERATOR_RUNNING_MODAL;
1923 return OPERATOR_FINISHED;
1926 /* like ED_operator_region_view3d_active but check its not in ortho view */
1927 static int viewdolly_poll(bContext *C)
1929 RegionView3D *rv3d= CTX_wm_region_view3d(C);
1932 if (rv3d->persp == RV3D_PERSP) {
1936 View3D *v3d= CTX_wm_view3d(C);
1937 if (ED_view3d_camera_lock_check(v3d, rv3d)) {
1945 static int viewdolly_cancel(bContext *C, wmOperator *op)
1947 viewops_data_free(C, op);
1949 return OPERATOR_CANCELLED;
1952 void VIEW3D_OT_dolly(wmOperatorType *ot)
1955 ot->name= "Dolly view";
1956 ot->description = "Dolly in/out in the view";
1957 ot->idname= "VIEW3D_OT_dolly";
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;
1967 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
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);
1976 static int view3d_all_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
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);
1984 const short skip_camera= ED_view3d_camera_lock_check(v3d, rv3d);
1986 int center= RNA_boolean_get(op->ptr, "center");
1988 float size, min[3], max[3], afm[3];
1989 int ok= 1, onedone=0;
1992 /* in 2.4x this also move the cursor to (0, 0, 0) (with shift+c). */
1993 curs= give_cursor(scene, v3d);
1999 INIT_MINMAX(min, max);
2002 for(base= scene->base.first; base; base= base->next) {
2003 if(BASE_VISIBLE(v3d, base)) {
2006 if(skip_camera && base->object == v3d->camera) {
2010 minmax_object(base->object, min, max);
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.
2022 return OPERATOR_FINISHED;
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;
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;
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;
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);
2050 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2053 // XXX BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2055 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
2057 return OPERATOR_FINISHED;
2061 void VIEW3D_OT_view_all(wmOperatorType *ot)
2064 ot->name= "View All";
2065 ot->description = "View all objects in scene";
2066 ot->idname= "VIEW3D_OT_view_all";
2069 ot->exec= view3d_all_exec;
2070 ot->poll= ED_operator_region_view3d_active;
2075 RNA_def_boolean(ot->srna, "center", 0, "Center", "");
2079 static int viewselected_exec(bContext *C, wmOperator *UNUSED(op)) /* like a localview without local!, was centerview() in 2.4x */
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);
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);
2095 INIT_MINMAX(min, max);
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... */
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)
2114 ok = minmax_verts(obedit, min, max); /* only selected */
2116 else if(ob && (ob->mode & OB_MODE_POSE)) {
2118 bArmature *arm= ob->data;
2119 bPoseChannel *pchan;
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;
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);
2136 else if (paint_facesel_test(ob)) {
2137 ok= paintface_minmax(ob, min, max);
2139 else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
2140 ok= PE_minmax(scene, min, max);
2144 for(base= FIRSTBASE; base; base = base->next) {
2145 if(TESTBASE(v3d, base)) {
2147 if(skip_camera && base->object == v3d->camera) {
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 */
2160 if(ok==0) return OPERATOR_FINISHED;
2162 sub_v3_v3v3(afm, max, min);
2163 size= MAX3(afm[0], afm[1], afm[2]);
2165 if(!rv3d->is_persp) {
2166 if(size < 0.0001f) { /* if its a sinble point. dont even re-scale */
2170 /* perspective should be a bit farther away to look nice */
2175 if(size <= v3d->near*1.5f) {
2176 size= v3d->near*1.5f;
2180 add_v3_v3v3(new_ofs, min, max);
2181 mul_v3_fl(new_ofs, -0.5f);
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;
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);
2197 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, ok_dist ? &new_dist : NULL, NULL);
2200 /* smooth view does viewlock RV3D_BOXVIEW copy */
2202 // XXX BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2204 return OPERATOR_FINISHED;
2207 void VIEW3D_OT_view_selected(wmOperatorType *ot)
2211 ot->name= "View Selected";
2212 ot->description = "Move the view to the selection center";
2213 ot->idname= "VIEW3D_OT_view_selected";
2216 ot->exec= viewselected_exec;
2217 ot->poll= ED_operator_region_view3d_active;
2223 static int viewcenter_cursor_exec(bContext *C, wmOperator *UNUSED(op))
2225 View3D *v3d = CTX_wm_view3d(C);
2226 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2227 Scene *scene= CTX_data_scene(C);
2230 ARegion *ar= CTX_wm_region(C);
2232 /* non camera center */
2234 negate_v3_v3(new_ofs, give_cursor(scene, v3d));
2235 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, NULL, NULL);
2237 /* smooth view does viewlock RV3D_BOXVIEW copy */
2240 return OPERATOR_FINISHED;
2243 void VIEW3D_OT_view_center_cursor(wmOperatorType *ot)
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";
2251 ot->exec= viewcenter_cursor_exec;
2252 ot->poll= ED_operator_view3d_active;
2258 static int view3d_center_camera_exec(bContext *C, wmOperator *UNUSED(op)) /* was view3d_home() in 2.4x */
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);
2267 rv3d->camdx= rv3d->camdy= 0.0f;
2269 ED_view3d_calc_camera_border_size(scene, ar, v3d, rv3d, size);
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);
2275 rv3d->camzoom= BKE_screen_view3d_zoom_from_fac(MIN2(xfac, yfac));
2276 CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2278 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2280 return OPERATOR_FINISHED;
2283 void VIEW3D_OT_view_center_camera(wmOperatorType *ot)
2286 ot->name= "View Camera Center";
2287 ot->description = "Center the camera view";
2288 ot->idname= "VIEW3D_OT_view_center_camera";
2291 ot->exec= view3d_center_camera_exec;
2292 ot->poll= view3d_camera_active_poll;
2298 /* ********************* Set render border operator ****************** */
2300 static int render_border_exec(bContext *C, wmOperator *op)
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);
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");
2316 /* calculate range */
2317 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &vb, FALSE);
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);
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);
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 ))
2337 scene->r.mode &= ~R_BORDER;
2339 scene->r.mode |= R_BORDER;
2342 WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, NULL);
2344 return OPERATOR_FINISHED;
2348 void VIEW3D_OT_render_border(wmOperatorType *ot)
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";
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;
2361 ot->poll= view3d_camera_active_poll;
2364 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
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);
2373 /* ********************* Border Zoom operator ****************** */
2375 static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
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);
2382 /* Zooms in on a border drawn by the user */
2384 float dvec[3], vb[2], xscale, yscale, scale;
2390 /* ZBuffer depth vars */
2392 float depth_close= FLT_MAX;
2393 double cent[2], p[3];
2395 /* note; otherwise opengl won't work */
2396 view3d_operator_needs_opengl(C);
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");
2404 /* Get Z Depths, needed for perspective, nice for ortho */
2405 bgl_get_mats(&mats);
2406 draw_depth(scene, ar, v3d, NULL);
2409 /* avoid allocating the whole depth buffer */
2410 ViewDepths depth_temp= {0};
2412 /* avoid view3d_update_depths() for speed. */
2413 view3d_update_depths_rect(ar, &depth_temp, &rect);
2415 /* find the closest Z pixel */
2416 depth_close= view3d_depth_near(&depth_temp);
2418 MEM_freeN(depth_temp.depths);
2421 cent[0] = (((double)rect.xmin)+((double)rect.xmax)) / 2;
2422 cent[1] = (((double)rect.ymin)+((double)rect.ymax)) / 2;
2424 if (rv3d->is_persp) {
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;
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;
2437 dvec[0] = p[0]-p_corner[0];
2438 dvec[1] = p[1]-p_corner[1];
2439 dvec[2] = p[2]-p_corner[2];
2441 new_dist = len_v3(dvec);
2442 if(new_dist <= v3d->near * 1.5f) new_dist= v3d->near * 1.5f;
2448 } else { /* othographic */
2449 /* find the current window width and height */
2453 new_dist = rv3d->dist;
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])) {
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);
2466 initgrabz(rv3d, -new_ofs[0], -new_ofs[1], -new_ofs[2]);
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);
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;
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;
2484 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2486 if(rv3d->viewlock & RV3D_BOXVIEW)
2487 view3d_boxview_sync(CTX_wm_area(C), ar);
2489 return OPERATOR_FINISHED;
2492 static int view3d_zoom_border_invoke(bContext *C, wmOperator *op, wmEvent *event)
2494 View3D *v3d= CTX_wm_view3d(C);
2495 RegionView3D *rv3d= CTX_wm_region_view3d(C);
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);
2501 return OPERATOR_PASS_THROUGH;
2504 void VIEW3D_OT_zoom_border(wmOperatorType *ot)
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";
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;
2517 ot->poll= ED_operator_region_view3d_active;
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);
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)
2533 RegionView3D *rv3d= ar->regiondata;
2535 int im_width= (scene->r.size*scene->r.xsch)/100;
2537 ED_view3d_calc_camera_border_size(scene, ar, v3d, rv3d, size);
2539 rv3d->camzoom= BKE_screen_view3d_zoom_from_fac((float)im_width/size[0]);
2540 CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2543 static int view3d_zoom_1_to_1_camera_exec(bContext *C, wmOperator *UNUSED(op))
2545 Scene *scene= CTX_data_scene(C);
2546 ARegion *ar= CTX_wm_region(C);
2548 view3d_set_1_to_1_viewborder(scene, ar, CTX_wm_view3d(C));
2550 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2552 return OPERATOR_FINISHED;
2555 void VIEW3D_OT_zoom_camera_1_to_1(wmOperatorType *ot)
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";
2563 ot->exec= view3d_zoom_1_to_1_camera_exec;
2564 ot->poll= view3d_camera_active_poll;
2570 /* ********************* Changing view operator ****************** */
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}};
2583 /* would like to make this a generic function - outside of transform */
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)
2587 RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2590 new_quat[0]= q1; new_quat[1]= q2;
2591 new_quat[2]= q3; new_quat[3]= q4;
2592 normalize_qt(new_quat);
2595 /* align to active object */
2596 Object *obact= CTX_data_active_object(C);
2598 /* no active object, ignore this option */
2599 align_active= FALSE;
2602 float obact_quat[4];
2605 /* same as transform manipulator when normal is set */
2606 ED_getTransformOrientationMatrix(C, twmat, FALSE);
2608 mat3_to_quat( obact_quat,twmat);
2609 invert_qt(obact_quat);
2610 mul_qt_qtqt(new_quat, new_quat, obact_quat);
2612 rv3d->view= view= RV3D_VIEW_USER;
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);
2632 if(rv3d->viewlock) {
2633 ED_region_tag_redraw(ar);
2637 if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
2639 if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
2640 else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
2642 smooth_view(C, v3d, ar, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
2646 if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
2647 else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
2649 smooth_view(C, v3d, ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2654 static int viewnumpad_exec(bContext *C, wmOperator *op)
2656 View3D *v3d = CTX_wm_view3d(C);
2657 ARegion *ar= ED_view3d_context_region_unlock(C);
2658 RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2659 Scene *scene= CTX_data_scene(C);
2660 static int perspo=RV3D_PERSP;
2661 int viewnum, align_active, nextperspo;
2663 viewnum = RNA_enum_get(op->ptr, "type");
2664 align_active = RNA_boolean_get(op->ptr, "align_active");
2666 /* set this to zero, gets handled in axis_set_view */
2670 /* Use this to test if we started out with a camera */
2672 if (rv3d->persp == RV3D_CAMOB) {
2673 nextperspo= rv3d->lpersp;
2679 case RV3D_VIEW_BOTTOM :
2680 axis_set_view(C, v3d, ar, 0.0, -1.0, 0.0, 0.0, viewnum, nextperspo, align_active);
2683 case RV3D_VIEW_BACK:
2684 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);
2687 case RV3D_VIEW_LEFT:
2688 axis_set_view(C, v3d, ar, 0.5, -0.5, 0.5, 0.5, viewnum, nextperspo, align_active);
2692 axis_set_view(C, v3d, ar, 1.0, 0.0, 0.0, 0.0, viewnum, nextperspo, align_active);
2695 case RV3D_VIEW_FRONT:
2696 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);
2699 case RV3D_VIEW_RIGHT:
2700 axis_set_view(C, v3d, ar, 0.5, -0.5, -0.5, -0.5, viewnum, nextperspo, align_active);
2703 case RV3D_VIEW_CAMERA:
2704 if(rv3d->viewlock==0) {
2707 if(rv3d->persp != RV3D_CAMOB) {
2710 if (!rv3d->smooth_timer) {
2711 /* store settings of current view before allowing overwriting with camera view
2712 * only if we're not currently in a view transition */
2713 copy_qt_qt(rv3d->lviewquat, rv3d->viewquat);
2714 rv3d->lview= rv3d->view;
2715 rv3d->lpersp= rv3d->persp;
2719 if(G.qual==LR_ALTKEY) {
2720 if(oldcamera && is_an_active_object(oldcamera)) {
2721 v3d->camera= oldcamera;
2723 handle_view3d_lock();
2727 /* first get the default camera for the view lock type */
2728 if(v3d->scenelock) {
2729 /* sets the camera view if available */
2730 v3d->camera= scene->camera;
2733 /* use scene camera if one is not set (even though we're unlocked) */
2734 if(v3d->camera==NULL) {
2735 v3d->camera= scene->camera;
2739 /* if the camera isnt found, check a number of options */
2740 if(v3d->camera==NULL && ob && ob->type==OB_CAMERA)
2743 if(v3d->camera==NULL)
2744 v3d->camera= scene_find_camera(scene);
2746 /* couldnt find any useful camera, bail out */
2747 if(v3d->camera==NULL)
2748 return OPERATOR_CANCELLED;
2750 /* important these dont get out of sync for locked scenes */
2752 scene->camera= v3d->camera;
2754 /* finally do snazzy view zooming */
2755 rv3d->persp= RV3D_CAMOB;
2756 smooth_view(C, v3d, ar, NULL, v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens);
2760 /* return to settings of last view */
2761 /* does smooth_view too */
2762 axis_set_view(C, v3d, ar, rv3d->lviewquat[0], rv3d->lviewquat[1], rv3d->lviewquat[2], rv3d->lviewquat[3], rv3d->lview, rv3d->lpersp, 0);
2771 if(rv3d->persp != RV3D_CAMOB) perspo= rv3d->persp;
2773 return OPERATOR_FINISHED;
2777 void VIEW3D_OT_viewnumpad(wmOperatorType *ot)
2780 ot->name= "View numpad";
2781 ot->description = "Set the view";
2782 ot->idname= "VIEW3D_OT_viewnumpad";
2785 ot->exec= viewnumpad_exec;
2786 ot->poll= ED_operator_rv3d_unlock_poll;
2791 RNA_def_enum(ot->srna, "type", prop_view_items, 0, "View", "The Type of view");
2792 RNA_def_boolean(ot->srna, "align_active", 0, "Align Active", "Align to the active object's axis");
2795 static EnumPropertyItem prop_view_orbit_items[] = {
2796 {V3D_VIEW_STEPLEFT, "ORBITLEFT", 0, "Orbit Left", "Orbit the view around to the Left"},
2797 {V3D_VIEW_STEPRIGHT, "ORBITRIGHT", 0, "Orbit Right", "Orbit the view around to the Right"},
2798 {V3D_VIEW_STEPUP, "ORBITUP", 0, "Orbit Up", "Orbit the view Up"},
2799 {V3D_VIEW_STEPDOWN, "ORBITDOWN", 0, "Orbit Down", "Orbit the view Down"},
2800 {0, NULL, 0, NULL, NULL}};
2802 static int vieworbit_exec(bContext *C, wmOperator *op)
2804 View3D *v3d= CTX_wm_view3d(C);
2805 ARegion *ar= ED_view3d_context_region_unlock(C);
2806 RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2807 float phi, q1[4], new_quat[4];
2810 orbitdir = RNA_enum_get(op->ptr, "type");
2812 if(rv3d->viewlock==0) {
2813 if((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d)) {
2814 if(orbitdir == V3D_VIEW_STEPLEFT || orbitdir == V3D_VIEW_STEPRIGHT) {
2817 phi= (float)(M_PI/360.0)*U.pad_rot_angle;
2818 if(orbitdir == V3D_VIEW_STEPRIGHT) phi= -phi;
2819 si= (float)sin(phi);
2820 q1[0]= (float)cos(phi);
2823 mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
2824 rv3d->view= RV3D_VIEW_USER;
2826 else if(orbitdir == V3D_VIEW_STEPDOWN || orbitdir == V3D_VIEW_STEPUP) {
2827 /* horizontal axis */
2828 copy_v3_v3(q1+1, rv3d->viewinv[0]);
2831 phi= (float)(M_PI/360.0)*U.pad_rot_angle;
2832 if(orbitdir == V3D_VIEW_STEPDOWN) phi= -phi;
2833 q1[0]= (float)cos(phi);
2834 mul_v3_fl(q1+1, sin(phi));
2835 mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
2836 rv3d->view= RV3D_VIEW_USER;
2839 smooth_view(C, CTX_wm_view3d(C), ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2843 return OPERATOR_FINISHED;
2846 void VIEW3D_OT_view_orbit(wmOperatorType *ot)
2849 ot->name= "View Orbit";
2850 ot->description = "Orbit the view";
2851 ot->idname= "VIEW3D_OT_view_orbit";
2854 ot->exec= vieworbit_exec;
2855 ot->poll= ED_operator_rv3d_unlock_poll;
2859 RNA_def_enum(ot->srna, "type", prop_view_orbit_items, 0, "Orbit", "Direction of View Orbit");
2862 static EnumPropertyItem prop_view_pan_items[] = {
2863 {V3D_VIEW_PANLEFT, "PANLEFT", 0, "Pan Left", "Pan the view to the Left"},
2864 {V3D_VIEW_PANRIGHT, "PANRIGHT", 0, "Pan Right", "Pan the view to the Right"},
2865 {V3D_VIEW_PANUP, "PANUP", 0, "Pan Up", "Pan the view Up"},
2866 {V3D_VIEW_PANDOWN, "PANDOWN", 0, "Pan Down", "Pan the view Down"},
2867 {0, NULL, 0, NULL, NULL}};
2869 static int viewpan_exec(bContext *C, wmOperator *op)
2871 ARegion *ar= CTX_wm_region(C);
2872 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2874 float mval_f[2]= {0.0f, 0.0f};
2877 pandir = RNA_enum_get(op->ptr, "type");
2879 initgrabz(rv3d, 0.0, 0.0, 0.0);
2880 if(pandir == V3D_VIEW_PANRIGHT) { mval_f[0]= -32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2881 else if(pandir == V3D_VIEW_PANLEFT) { mval_f[0]= 32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2882 else if(pandir == V3D_VIEW_PANUP) { mval_f[1]= -25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2883 else if(pandir == V3D_VIEW_PANDOWN) { mval_f[1]= 25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2884 add_v3_v3(rv3d->ofs, vec);
2886 if(rv3d->viewlock & RV3D_BOXVIEW)
2887 view3d_boxview_sync(CTX_wm_area(C), ar);
2889 ED_region_tag_redraw(ar);
2891 return OPERATOR_FINISHED;
2894 void VIEW3D_OT_view_pan(wmOperatorType *ot)
2897 ot->name= "View Pan";
2898 ot->description = "Pan the view";
2899 ot->idname= "VIEW3D_OT_view_pan";
2902 ot->exec= viewpan_exec;
2903 ot->poll= ED_operator_region_view3d_active;
2907 RNA_def_enum(ot->srna, "type", prop_view_pan_items, 0, "Pan", "Direction of View Pan");
2910 static int viewpersportho_exec(bContext *C, wmOperator *UNUSED(op))
2912 ARegion *ar= ED_view3d_context_region_unlock(C);
2913 RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2915 if(rv3d->viewlock==0) {
2916 if(rv3d->persp!=RV3D_ORTHO)
2917 rv3d->persp=RV3D_ORTHO;
2918 else rv3d->persp=RV3D_PERSP;
2919 ED_region_tag_redraw(ar);
2922 return OPERATOR_FINISHED;
2926 void VIEW3D_OT_view_persportho(wmOperatorType *ot)
2929 ot->name= "View Persp/Ortho";
2930 ot->description = "Switch the current view from perspective/orthographic";
2931 ot->idname= "VIEW3D_OT_view_persportho";
2934 ot->exec= viewpersportho_exec;
2935 ot->poll= ED_operator_rv3d_unlock_poll;
2942 /* ******************** add background image operator **************** */
2944 static BGpic *background_image_add(bContext *C)
2946 View3D *v3d= CTX_wm_view3d(C);
2948 return ED_view3D_background_image_new(v3d);
2951 static int background_image_add_exec(bContext *C, wmOperator *UNUSED(op))
2953 background_image_add(C);
2955 return OPERATOR_FINISHED;
2958 static int background_image_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2960 View3D *v3d= CTX_wm_view3d(C);
2963 char name[MAX_ID_NAME-2];
2965 /* check input variables */
2966 if(RNA_struct_property_is_set(op->ptr, "filepath")) {
2967 char path[FILE_MAX];
2969 RNA_string_get(op->ptr, "filepath", path);
2970 ima= BKE_add_image_file(path);
2972 else if(RNA_struct_property_is_set(op->ptr, "name")) {
2973 RNA_string_get(op->ptr, "name", name);
2974 ima= (Image *)find_id("IM", name);
2977 bgpic = background_image_add(C);
2982 if(ima->id.us==0) id_us_plus(&ima->id);
2983 else id_lib_extern(&ima->id);
2985 if (!(v3d->flag & V3D_DISPBGPICS))
2986 v3d->flag |= V3D_DISPBGPICS;
2989 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
2991 return OPERATOR_FINISHED;
2994 void VIEW3D_OT_background_image_add(wmOperatorType *ot)
2997 ot->name = "Add Background Image";
2998 ot->description= "Add a new background image";
2999 ot->idname = "VIEW3D_OT_background_image_add";
3002 ot->invoke = background_image_add_invoke;
3003 ot->exec = background_image_add_exec;
3004 ot->poll = ED_operator_view3d_active;
3010 RNA_def_string(ot->srna, "name", "Image", MAX_ID_NAME-2, "Name", "Image name to assign");
3011 RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
3015 /* ***** remove image operator ******* */
3016 static int background_image_remove_exec(bContext *C, wmOperator *op)
3018 View3D *v3d = CTX_wm_view3d(C);
3019 int index = RNA_int_get(op->ptr, "index");
3020 BGpic *bgpic_rem= BLI_findlink(&v3d->bgpicbase, index);
3023 ED_view3D_background_image_remove(v3d, bgpic_rem);
3024 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
3025 return OPERATOR_FINISHED;
3028 return OPERATOR_CANCELLED;
3033 void VIEW3D_OT_background_image_remove(wmOperatorType *ot)
3036 ot->name = "Remove Background Image";
3037 ot->description= "Remove a background image from the 3D view";
3038 ot->idname = "VIEW3D_OT_background_image_remove";
3041 ot->exec = background_image_remove_exec;
3042 ot->poll = ED_operator_view3d_active;
3047 RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Background image index to remove ", 0, INT_MAX);
3050 /* ********************* set clipping operator ****************** */
3052 static void calc_clipping_plane(float clip[6][4], BoundBox *clipbb)
3056 for(val=0; val<4; val++) {
3058 normal_tri_v3( clip[val],clipbb->vec[val], clipbb->vec[val==3?0:val+1], clipbb->vec[val+4]);
3061 - clip[val][0]*clipbb->vec[val][0]
3062 - clip[val][1]*clipbb->vec[val][1]
3063 - clip[val][2]*clipbb->vec[val][2];
3067 static void calc_local_clipping(float clip_local[][4], BoundBox *clipbb, float mat[][4])
3069 BoundBox clipbb_local;
3073 invert_m4_m4(imat, mat);
3075 for(i=0; i<8; i++) {
3076 mul_v3_m4v3(clipbb_local.vec[i], imat, clipbb->vec[i]);
3079 calc_clipping_plane(clip_local, &clipbb_local);
3082 void ED_view3d_local_clipping(RegionView3D *rv3d, float mat[][4])
3084 if(rv3d->rflag & RV3D_CLIPPING)
3085 calc_local_clipping(rv3d->clip_local, rv3d->clipbb, mat);
3088 static int view3d_clipping_exec(bContext *C, wmOperator *op)
3090 RegionView3D *rv3d= CTX_wm_region_view3d(C);
3095 rect.xmin= RNA_int_get(op->ptr, "xmin");
3096 rect.ymin= RNA_int_get(op->ptr, "ymin");
3097 rect.xmax= RNA_int_get(op->ptr, "xmax");
3098 rect.ymax= RNA_int_get(op->ptr, "ymax");
3100 rv3d->rflag |= RV3D_CLIPPING;
3101 rv3d->clipbb= MEM_callocN(sizeof(BoundBox), "clipbb");
3103 /* note; otherwise opengl won't work */
3104 view3d_operator_needs_opengl(C);
3106 view3d_set_viewcontext(C, &vc);
3107 view3d_get_transformation(vc.ar, vc.rv3d, NULL, &mats); /* NULL because we don't want it in object space */
3108 ED_view3d_calc_clipping(rv3d->clipbb, rv3d->clip, &mats, &rect);
3110 return OPERATOR_FINISHED;
3113 static int view3d_clipping_invoke(bContext *C, wmOperator *op, wmEvent *event)
3115 RegionView3D *rv3d= CTX_wm_region_view3d(C);
3116 ARegion *ar= CTX_wm_region(C);
3118 if(rv3d->rflag & RV3D_CLIPPING) {
3119 rv3d->rflag &= ~RV3D_CLIPPING;
3120 ED_region_tag_redraw(ar);
3121 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
3123 return OPERATOR_FINISHED;
3126 return WM_border_select_invoke(C, op, event);
3131 void VIEW3D_OT_clip_border(wmOperatorType *ot)
3135 ot->name= "Clipping Border";
3136 ot->description = "Set the view clipping border";
3137 ot->idname= "VIEW3D_OT_clip_border";
3140 ot->invoke= view3d_clipping_invoke;
3141 ot->exec= view3d_clipping_exec;