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
36 #include "DNA_armature_types.h"
37 #include "DNA_curve_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_gpencil_types.h"
42 #include "MEM_guardedalloc.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_kdopbvh.h"
47 #include "BLI_utildefines.h"
49 #include "BKE_armature.h"
50 #include "BKE_camera.h"
51 #include "BKE_context.h"
53 #include "BKE_library.h"
54 #include "BKE_object.h"
55 #include "BKE_paint.h"
56 #include "BKE_report.h"
57 #include "BKE_scene.h"
58 #include "BKE_screen.h"
59 #include "BKE_action.h"
60 #include "BKE_depsgraph.h" /* for ED_view3d_camera_lock_sync */
64 #include "BIF_glutil.h"
69 #include "RNA_access.h"
70 #include "RNA_define.h"
72 #include "ED_armature.h"
73 #include "ED_particle.h"
74 #include "ED_keyframing.h"
75 #include "ED_screen.h"
76 #include "ED_transform.h"
78 #include "ED_gpencil.h"
79 #include "ED_view3d.h"
81 #include "UI_resources.h"
83 #include "PIL_time.h" /* smoothview */
85 #include "view3d_intern.h" /* own include */
87 bool ED_view3d_offset_lock_check(const View3D *v3d, const RegionView3D *rv3d)
89 return (rv3d->persp != RV3D_CAMOB) && (v3d->ob_centre_cursor || v3d->ob_centre);
92 static bool view3d_operator_offset_lock_check(bContext *C, wmOperator *op)
94 View3D *v3d = CTX_wm_view3d(C);
95 RegionView3D *rv3d = CTX_wm_region_view3d(C);
96 if (ED_view3d_offset_lock_check(v3d, rv3d)) {
97 BKE_report(op->reports, RPT_WARNING, "View offset is locked");
105 /* ********************** view3d_edit: view manipulations ********************* */
108 * \return true when the view-port is locked to its camera.
110 bool ED_view3d_camera_lock_check(const View3D *v3d, const RegionView3D *rv3d)
112 return ((v3d->camera) &&
113 (v3d->camera->id.lib == NULL) &&
114 (v3d->flag2 & V3D_LOCK_CAMERA) &&
115 (rv3d->persp == RV3D_CAMOB));
119 * Apply the camera object transformation to the view-port.
120 * (needed so we can use regular view-port manipulation operators, that sync back to the camera).
122 void ED_view3d_camera_lock_init_ex(View3D *v3d, RegionView3D *rv3d, const bool calc_dist)
124 if (ED_view3d_camera_lock_check(v3d, rv3d)) {
126 /* using a fallback dist is OK here since ED_view3d_from_object() compensates for it */
127 rv3d->dist = ED_view3d_offset_distance(v3d->camera->obmat, rv3d->ofs, VIEW3D_DIST_FALLBACK);
129 ED_view3d_from_object(v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
133 void ED_view3d_camera_lock_init(View3D *v3d, RegionView3D *rv3d)
135 ED_view3d_camera_lock_init_ex(v3d, rv3d, true);
139 * Apply the view-port transformation back to the camera object.
141 * \return true if the camera is moved.
143 bool ED_view3d_camera_lock_sync(View3D *v3d, RegionView3D *rv3d)
145 if (ED_view3d_camera_lock_check(v3d, rv3d)) {
146 ObjectTfmProtectedChannels obtfm;
149 if ((U.uiflag & USER_CAM_LOCK_NO_PARENT) == 0 && (root_parent = v3d->camera->parent)) {
153 float view_mat[4][4];
154 float diff_mat[4][4];
155 float parent_mat[4][4];
157 while (root_parent->parent) {
158 root_parent = root_parent->parent;
161 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
163 normalize_m4_m4(tmat, v3d->camera->obmat);
165 invert_m4_m4(imat, tmat);
166 mul_m4_m4m4(diff_mat, view_mat, imat);
168 mul_m4_m4m4(parent_mat, diff_mat, root_parent->obmat);
170 BKE_object_tfm_protected_backup(root_parent, &obtfm);
171 BKE_object_apply_mat4(root_parent, parent_mat, true, false);
172 BKE_object_tfm_protected_restore(root_parent, &obtfm, root_parent->protectflag);
174 ob_update = v3d->camera;
176 DAG_id_tag_update(&ob_update->id, OB_RECALC_OB);
177 WM_main_add_notifier(NC_OBJECT | ND_TRANSFORM, ob_update);
178 ob_update = ob_update->parent;
182 /* always maintain the same scale */
183 const short protect_scale_all = (OB_LOCK_SCALEX | OB_LOCK_SCALEY | OB_LOCK_SCALEZ);
184 BKE_object_tfm_protected_backup(v3d->camera, &obtfm);
185 ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
186 BKE_object_tfm_protected_restore(v3d->camera, &obtfm, v3d->camera->protectflag | protect_scale_all);
188 DAG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
189 WM_main_add_notifier(NC_OBJECT | ND_TRANSFORM, v3d->camera);
199 bool ED_view3d_camera_autokey(
200 Scene *scene, ID *id_key,
201 struct bContext *C, const bool do_rotate, const bool do_translate)
203 if (autokeyframe_cfra_can_key(scene, id_key)) {
204 const float cfra = (float)CFRA;
205 ListBase dsources = {NULL, NULL};
207 /* add data-source override for the camera object */
208 ANIM_relative_keyingset_add_source(&dsources, id_key, NULL, NULL);
211 * 1) on the first frame
212 * 2) on each subsequent frame
213 * TODO: need to check in future that frame changed before doing this
216 struct KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, ANIM_KS_ROTATION_ID);
217 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
220 struct KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, ANIM_KS_LOCATION_ID);
221 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, cfra);
225 BLI_freelistN(&dsources);
235 * Call after modifying a locked view.
237 * \note Not every view edit currently auto-keys (numpad for eg),
238 * this is complicated because of smoothview.
240 bool ED_view3d_camera_lock_autokey(
241 View3D *v3d, RegionView3D *rv3d,
242 struct bContext *C, const bool do_rotate, const bool do_translate)
244 /* similar to ED_view3d_cameracontrol_update */
245 if (ED_view3d_camera_lock_check(v3d, rv3d)) {
246 Scene *scene = CTX_data_scene(C);
249 if ((U.uiflag & USER_CAM_LOCK_NO_PARENT) == 0 && (root_parent = v3d->camera->parent)) {
250 while (root_parent->parent) {
251 root_parent = root_parent->parent;
253 id_key = &root_parent->id;
256 id_key = &v3d->camera->id;
259 return ED_view3d_camera_autokey(scene, id_key, C, do_rotate, do_translate);
267 * For viewport operators that exit camera persp.
269 * \note This differs from simply setting ``rv3d->persp = persp`` because it
270 * sets the ``ofs`` and ``dist`` values of the viewport so it matches the camera,
271 * otherwise switching out of camera view may jump to a different part of the scene.
273 static void view3d_persp_switch_from_camera(View3D *v3d, RegionView3D *rv3d, const char persp)
275 BLI_assert(rv3d->persp == RV3D_CAMOB);
276 BLI_assert(persp != RV3D_CAMOB);
279 rv3d->dist = ED_view3d_offset_distance(v3d->camera->obmat, rv3d->ofs, VIEW3D_DIST_FALLBACK);
280 ED_view3d_from_object(v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
283 if (!ED_view3d_camera_lock_check(v3d, rv3d)) {
288 /* ********************* box view support ***************** */
290 static void view3d_boxview_clip(ScrArea *sa)
293 BoundBox *bb = MEM_callocN(sizeof(BoundBox), "clipbb");
295 float x1 = 0.0f, y1 = 0.0f, z1 = 0.0f, ofs[3] = {0.0f, 0.0f, 0.0f};
298 /* create bounding box */
299 for (ar = sa->regionbase.first; ar; ar = ar->next) {
300 if (ar->regiontype == RGN_TYPE_WINDOW) {
301 RegionView3D *rv3d = ar->regiondata;
303 if (rv3d->viewlock & RV3D_BOXCLIP) {
304 if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
305 if (ar->winx > ar->winy) x1 = rv3d->dist;
306 else x1 = ar->winx * rv3d->dist / ar->winy;
308 if (ar->winx > ar->winy) y1 = ar->winy * rv3d->dist / ar->winx;
309 else y1 = rv3d->dist;
310 copy_v2_v2(ofs, rv3d->ofs);
312 else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
313 ofs[2] = rv3d->ofs[2];
315 if (ar->winx > ar->winy) z1 = ar->winy * rv3d->dist / ar->winx;
316 else z1 = rv3d->dist;
322 for (val = 0; val < 8; val++) {
323 if (ELEM(val, 0, 3, 4, 7))
324 bb->vec[val][0] = -x1 - ofs[0];
326 bb->vec[val][0] = x1 - ofs[0];
328 if (ELEM(val, 0, 1, 4, 5))
329 bb->vec[val][1] = -y1 - ofs[1];
331 bb->vec[val][1] = y1 - ofs[1];
334 bb->vec[val][2] = -z1 - ofs[2];
336 bb->vec[val][2] = z1 - ofs[2];
339 /* normals for plane equations */
340 normal_tri_v3(clip[0], bb->vec[0], bb->vec[1], bb->vec[4]);
341 normal_tri_v3(clip[1], bb->vec[1], bb->vec[2], bb->vec[5]);
342 normal_tri_v3(clip[2], bb->vec[2], bb->vec[3], bb->vec[6]);
343 normal_tri_v3(clip[3], bb->vec[3], bb->vec[0], bb->vec[7]);
344 normal_tri_v3(clip[4], bb->vec[4], bb->vec[5], bb->vec[6]);
345 normal_tri_v3(clip[5], bb->vec[0], bb->vec[2], bb->vec[1]);
347 /* then plane equations */
348 for (val = 0; val < 6; val++) {
349 clip[val][3] = -dot_v3v3(clip[val], bb->vec[val % 5]);
352 /* create bounding box */
353 for (ar = sa->regionbase.first; ar; ar = ar->next) {
354 if (ar->regiontype == RGN_TYPE_WINDOW) {
355 RegionView3D *rv3d = ar->regiondata;
357 if (rv3d->viewlock & RV3D_BOXCLIP) {
358 rv3d->rflag |= RV3D_CLIPPING;
359 memcpy(rv3d->clip, clip, sizeof(clip));
360 if (rv3d->clipbb) MEM_freeN(rv3d->clipbb);
361 rv3d->clipbb = MEM_dupallocN(bb);
369 * Find which axis values are shared between both views and copy to \a rv3d_dst
370 * taking axis flipping into account.
372 static void view3d_boxview_sync_axis(RegionView3D *rv3d_dst, RegionView3D *rv3d_src)
374 /* absolute axis values above this are considered to be set (will be ~1.0f) */
375 const float axis_eps = 0.5f;
378 /* use the view rotation to identify which axis to sync on */
379 float view_axis_all[4][3] = {
385 float *view_src_x = &view_axis_all[0][0];
386 float *view_src_y = &view_axis_all[1][0];
388 float *view_dst_x = &view_axis_all[2][0];
389 float *view_dst_y = &view_axis_all[3][0];
393 /* we could use rv3d->viewinv, but better not depend on view matrix being updated */
394 if (UNLIKELY(ED_view3d_quat_from_axis_view(rv3d_src->view, viewinv) == false)) {
397 invert_qt_normalized(viewinv);
398 mul_qt_v3(viewinv, view_src_x);
399 mul_qt_v3(viewinv, view_src_y);
401 if (UNLIKELY(ED_view3d_quat_from_axis_view(rv3d_dst->view, viewinv) == false)) {
404 invert_qt_normalized(viewinv);
405 mul_qt_v3(viewinv, view_dst_x);
406 mul_qt_v3(viewinv, view_dst_y);
408 /* check source and dest have a matching axis */
409 for (i = 0; i < 3; i++) {
410 if (((fabsf(view_src_x[i]) > axis_eps) || (fabsf(view_src_y[i]) > axis_eps)) &&
411 ((fabsf(view_dst_x[i]) > axis_eps) || (fabsf(view_dst_y[i]) > axis_eps)))
413 rv3d_dst->ofs[i] = rv3d_src->ofs[i];
418 /* sync center/zoom view of region to others, for view transforms */
419 static void view3d_boxview_sync(ScrArea *sa, ARegion *ar)
422 RegionView3D *rv3d = ar->regiondata;
425 for (artest = sa->regionbase.first; artest; artest = artest->next) {
426 if (artest != ar && artest->regiontype == RGN_TYPE_WINDOW) {
427 RegionView3D *rv3dtest = artest->regiondata;
429 if (rv3dtest->viewlock & RV3D_LOCKED) {
430 rv3dtest->dist = rv3d->dist;
431 view3d_boxview_sync_axis(rv3dtest, rv3d);
432 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
434 ED_region_tag_redraw(artest);
440 view3d_boxview_clip(sa);
444 /* for home, center etc */
445 void view3d_boxview_copy(ScrArea *sa, ARegion *ar)
448 RegionView3D *rv3d = ar->regiondata;
451 for (artest = sa->regionbase.first; artest; artest = artest->next) {
452 if (artest != ar && artest->regiontype == RGN_TYPE_WINDOW) {
453 RegionView3D *rv3dtest = artest->regiondata;
455 if (rv3dtest->viewlock) {
456 rv3dtest->dist = rv3d->dist;
457 copy_v3_v3(rv3dtest->ofs, rv3d->ofs);
458 ED_region_tag_redraw(artest);
460 clip |= ((rv3dtest->viewlock & RV3D_BOXCLIP) != 0);
466 view3d_boxview_clip(sa);
470 /* 'clip' is used to know if our clip setting has changed */
471 void ED_view3d_quadview_update(ScrArea *sa, ARegion *ar, bool do_clip)
473 ARegion *ar_sync = NULL;
474 RegionView3D *rv3d = ar->regiondata;
476 /* this function copies flags from the first of the 3 other quadview
477 * regions to the 2 other, so it assumes this is the region whose
478 * properties are always being edited, weak */
479 viewlock = rv3d->viewlock;
481 if ((viewlock & RV3D_LOCKED) == 0) {
482 do_clip = (viewlock & RV3D_BOXCLIP) != 0;
485 else if ((viewlock & RV3D_BOXVIEW) == 0 && (viewlock & RV3D_BOXCLIP) != 0) {
487 viewlock &= ~RV3D_BOXCLIP;
490 for (; ar; ar = ar->prev) {
491 if (ar->alignment == RGN_ALIGN_QSPLIT) {
492 rv3d = ar->regiondata;
493 rv3d->viewlock = viewlock;
495 if (do_clip && (viewlock & RV3D_BOXCLIP) == 0) {
496 rv3d->rflag &= ~RV3D_BOXCLIP;
499 /* use ar_sync so we sync with one of the aligned views below
500 * else the view jumps on changing view settings like 'clip'
501 * since it copies from the perspective view */
506 if (rv3d->viewlock & RV3D_BOXVIEW) {
507 view3d_boxview_sync(sa, ar_sync ? ar_sync : sa->regionbase.last);
510 /* ensure locked regions have an axis, locked user views don't make much sense */
511 if (viewlock & RV3D_LOCKED) {
512 int index_qsplit = 0;
513 for (ar = sa->regionbase.first; ar; ar = ar->next) {
514 if (ar->alignment == RGN_ALIGN_QSPLIT) {
515 rv3d = ar->regiondata;
516 if (rv3d->viewlock) {
517 if (!RV3D_VIEW_IS_AXIS(rv3d->view)) {
518 rv3d->view = ED_view3d_lock_view_from_index(index_qsplit);
519 rv3d->persp = RV3D_ORTHO;
520 ED_view3d_lock(rv3d);
528 ED_area_tag_redraw(sa);
531 /* ************************** init for view ops **********************************/
533 typedef struct ViewOpsData {
534 /* context pointers (assigned by viewops_data_alloc) */
541 /* needed for continuous zoom */
543 double timer_lastdraw;
546 float viewquat[4]; /* working copy of rv3d->viewquat */
548 float mousevec[3]; /* dolly only */
550 float dist_prev, camzoom_prev;
552 bool axis_snap; /* view rotate only */
555 /* use for orbit selection and auto-dist */
556 float ofs[3], dyn_ofs[3];
559 int origx, origy, oldx, oldy;
560 int origkey; /* the key that triggered the operator */
564 #define TRACKBALLSIZE (1.1f)
566 static void calctrackballvec(const rcti *rect, int mx, int my, float vec[3])
568 float x, y, radius, d, z, t;
570 radius = TRACKBALLSIZE;
572 /* normalize x and y */
573 x = BLI_rcti_cent_x(rect) - mx;
574 x /= (float)(BLI_rcti_size_x(rect) / 4);
575 y = BLI_rcti_cent_y(rect) - my;
576 y /= (float)(BLI_rcti_size_y(rect) / 2);
578 d = sqrtf(x * x + y * y);
579 if (d < radius * (float)M_SQRT1_2) { /* Inside sphere */
580 z = sqrtf(radius * radius - d * d);
582 else { /* On hyperbola */
583 t = radius / (float)M_SQRT2;
589 vec[2] = -z; /* yah yah! */
593 /* -------------------------------------------------------------------- */
596 /** \name Generic View Operator Custom-Data.
600 * Allocate and fill in context pointers for #ViewOpsData
602 static void viewops_data_alloc(bContext *C, wmOperator *op)
604 ViewOpsData *vod = MEM_callocN(sizeof(ViewOpsData), "viewops data");
607 op->customdata = vod;
608 vod->scene = CTX_data_scene(C);
609 vod->sa = CTX_wm_area(C);
610 vod->ar = CTX_wm_region(C);
611 vod->v3d = vod->sa->spacedata.first;
612 vod->rv3d = vod->ar->regiondata;
615 void view3d_orbit_apply_dyn_ofs(
616 float r_ofs[3], const float ofs_old[3], const float viewquat_old[4],
617 const float viewquat_new[4], const float dyn_ofs[3])
620 invert_qt_qt_normalized(q, viewquat_old);
621 mul_qt_qtqt(q, q, viewquat_new);
623 invert_qt_normalized(q);
625 sub_v3_v3v3(r_ofs, ofs_old, dyn_ofs);
627 add_v3_v3(r_ofs, dyn_ofs);
630 static bool view3d_orbit_calc_center(bContext *C, float r_dyn_ofs[3])
632 static float lastofs[3] = {0, 0, 0};
635 Scene *scene = CTX_data_scene(C);
636 Object *ob_act = OBACT;
638 if (ob_act && (ob_act->mode & OB_MODE_ALL_PAINT) &&
639 /* with weight-paint + pose-mode, fall through to using calculateTransformCenter */
640 ((ob_act->mode & OB_MODE_WEIGHT_PAINT) && BKE_object_pose_armature_get(ob_act)) == 0)
642 /* in case of sculpting use last average stroke position as a rotation
643 * center, in other cases it's not clear what rotation center shall be
644 * so just rotate around object origin
646 if (ob_act->mode & (OB_MODE_SCULPT | OB_MODE_TEXTURE_PAINT | OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT)) {
648 BKE_paint_stroke_get_average(scene, ob_act, stroke);
649 copy_v3_v3(lastofs, stroke);
652 copy_v3_v3(lastofs, ob_act->obmat[3]);
656 else if (ob_act && (ob_act->mode & OB_MODE_EDIT) && (ob_act->type == OB_FONT)) {
657 Curve *cu = ob_act->data;
658 EditFont *ef = cu->editfont;
662 for (i = 0; i < 4; i++) {
663 add_v2_v2(lastofs, ef->textcurs[i]);
665 mul_v2_fl(lastofs, 1.0f / 4.0f);
667 mul_m4_v3(ob_act->obmat, lastofs);
671 else if (ob_act == NULL || ob_act->mode == OB_MODE_OBJECT) {
672 /* object mode use boundbox centers */
673 View3D *v3d = CTX_wm_view3d(C);
675 unsigned int tot = 0;
676 float select_center[3];
678 zero_v3(select_center);
679 for (base = FIRSTBASE; base; base = base->next) {
680 if (TESTBASE(v3d, base)) {
681 /* use the boundbox if we can */
682 Object *ob = base->object;
684 if (ob->bb && !(ob->bb->flag & BOUNDBOX_DIRTY)) {
687 BKE_boundbox_calc_center_aabb(ob->bb, cent);
689 mul_m4_v3(ob->obmat, cent);
690 add_v3_v3(select_center, cent);
693 add_v3_v3(select_center, ob->obmat[3]);
699 mul_v3_fl(select_center, 1.0f / (float)tot);
700 copy_v3_v3(lastofs, select_center);
705 /* If there's no selection, lastofs is unmodified and last value since static */
706 is_set = calculateTransformCenter(C, V3D_AROUND_CENTER_MEAN, lastofs, NULL);
709 copy_v3_v3(r_dyn_ofs, lastofs);
715 * Calculate the values for #ViewOpsData
717 static void viewops_data_create_ex(bContext *C, wmOperator *op, const wmEvent *event,
718 const bool use_orbit_select,
719 const bool use_orbit_zbuf)
721 ViewOpsData *vod = op->customdata;
722 RegionView3D *rv3d = vod->rv3d;
724 /* set the view from the camera, if view locking is enabled.
725 * we may want to make this optional but for now its needed always */
726 ED_view3d_camera_lock_init(vod->v3d, vod->rv3d);
728 vod->dist_prev = rv3d->dist;
729 vod->camzoom_prev = rv3d->camzoom;
730 copy_qt_qt(vod->viewquat, rv3d->viewquat);
731 copy_qt_qt(vod->oldquat, rv3d->viewquat);
732 vod->origx = vod->oldx = event->x;
733 vod->origy = vod->oldy = event->y;
734 vod->origkey = event->type; /* the key that triggered the operator. */
735 vod->use_dyn_ofs = false;
736 copy_v3_v3(vod->ofs, rv3d->ofs);
738 if (use_orbit_select) {
740 vod->use_dyn_ofs = true;
742 view3d_orbit_calc_center(C, vod->dyn_ofs);
744 negate_v3(vod->dyn_ofs);
746 else if (use_orbit_zbuf) {
747 Scene *scene = CTX_data_scene(C);
748 float fallback_depth_pt[3];
750 view3d_operator_needs_opengl(C); /* needed for zbuf drawing */
752 negate_v3_v3(fallback_depth_pt, rv3d->ofs);
754 if ((vod->use_dyn_ofs = ED_view3d_autodist(scene, vod->ar, vod->v3d,
755 event->mval, vod->dyn_ofs, true, fallback_depth_pt)))
757 if (rv3d->is_persp) {
758 float my_origin[3]; /* original G.vd->ofs */
759 float my_pivot[3]; /* view */
762 /* locals for dist correction */
766 negate_v3_v3(my_origin, rv3d->ofs); /* ofs is flipped */
768 /* Set the dist value to be the distance from this 3d point
769 * this means youll always be able to zoom into it and panning wont go bad when dist was zero */
771 /* remove dist value */
772 upvec[0] = upvec[1] = 0;
773 upvec[2] = rv3d->dist;
774 copy_m3_m4(mat, rv3d->viewinv);
776 mul_m3_v3(mat, upvec);
777 sub_v3_v3v3(my_pivot, rv3d->ofs, upvec);
778 negate_v3(my_pivot); /* ofs is flipped */
780 /* find a new ofs value that is along the view axis (rather than the mouse location) */
781 closest_to_line_v3(dvec, vod->dyn_ofs, my_pivot, my_origin);
782 vod->dist_prev = rv3d->dist = len_v3v3(my_pivot, dvec);
784 negate_v3_v3(rv3d->ofs, dvec);
787 const float mval_ar_mid[2] = {
788 (float)vod->ar->winx / 2.0f,
789 (float)vod->ar->winy / 2.0f};
791 ED_view3d_win_to_3d(vod->ar, vod->dyn_ofs, mval_ar_mid, rv3d->ofs);
792 negate_v3(rv3d->ofs);
794 negate_v3(vod->dyn_ofs);
795 copy_v3_v3(vod->ofs, rv3d->ofs);
801 const float mval_f[2] = {(float)event->mval[0],
802 (float)event->mval[1]};
803 ED_view3d_win_to_vector(vod->ar, mval_f, vod->mousevec);
806 /* lookup, we don't pass on v3d to prevent confusement */
807 vod->grid = vod->v3d->grid;
808 vod->far = vod->v3d->far;
810 calctrackballvec(&vod->ar->winrct, event->x, event->y, vod->trackvec);
814 negate_v3_v3(tvec, rv3d->ofs);
815 vod->zfac = ED_view3d_calc_zfac(rv3d, tvec, NULL);
819 if (rv3d->persmat[2][1] < 0.0f)
820 vod->reverse = -1.0f;
822 rv3d->rflag |= RV3D_NAVIGATING;
825 static void viewops_data_create(bContext *C, wmOperator *op, const wmEvent *event)
827 viewops_data_create_ex(
829 (U.uiflag & USER_ORBIT_SELECTION) != 0,
830 (U.uiflag & USER_ZBUF_ORBIT) != 0);
833 static void viewops_data_free(bContext *C, wmOperator *op)
836 Paint *p = BKE_paint_get_active_from_context(C);
838 if (op->customdata) {
839 ViewOpsData *vod = op->customdata;
841 vod->rv3d->rflag &= ~RV3D_NAVIGATING;
844 WM_event_remove_timer(CTX_wm_manager(C), vod->timer->win, vod->timer);
847 op->customdata = NULL;
850 ar = CTX_wm_region(C);
853 if (p && (p->flags & PAINT_FAST_NAVIGATE))
854 ED_region_tag_redraw(ar);
859 /* ************************** viewrotate **********************************/
867 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
868 #define VIEW_MODAL_CONFIRM 1 /* used for all view operations */
869 #define VIEWROT_MODAL_AXIS_SNAP_ENABLE 2
870 #define VIEWROT_MODAL_AXIS_SNAP_DISABLE 3
871 #define VIEWROT_MODAL_SWITCH_ZOOM 4
872 #define VIEWROT_MODAL_SWITCH_MOVE 5
873 #define VIEWROT_MODAL_SWITCH_ROTATE 6
875 /* called in transform_ops.c, on each regeneration of keymaps */
876 void viewrotate_modal_keymap(wmKeyConfig *keyconf)
878 static EnumPropertyItem modal_items[] = {
879 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
881 {VIEWROT_MODAL_AXIS_SNAP_ENABLE, "AXIS_SNAP_ENABLE", 0, "Enable Axis Snap", ""},
882 {VIEWROT_MODAL_AXIS_SNAP_DISABLE, "AXIS_SNAP_DISABLE", 0, "Disable Axis Snap", ""},
884 {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
885 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
887 {0, NULL, 0, NULL, NULL}
890 wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Rotate Modal");
892 /* this function is called for each spacetype, only needs to add map once */
893 if (keymap && keymap->modal_items) return;
895 keymap = WM_modalkeymap_add(keyconf, "View3D Rotate Modal", modal_items);
897 /* items for modal map */
898 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
899 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
901 WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_ENABLE);
902 WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_DISABLE);
904 /* disabled mode switching for now, can re-implement better, later on */
906 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
907 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
908 WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
911 /* assign map to operators */
912 WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");
916 static void viewrotate_apply_dyn_ofs(ViewOpsData *vod, const float viewquat_new[4])
918 if (vod->use_dyn_ofs) {
919 RegionView3D *rv3d = vod->rv3d;
920 view3d_orbit_apply_dyn_ofs(rv3d->ofs, vod->ofs, vod->oldquat, viewquat_new, vod->dyn_ofs);
924 static void viewrotate_apply_snap(ViewOpsData *vod)
926 const float axis_limit = DEG2RADF(45 / 3);
928 RegionView3D *rv3d = vod->rv3d;
930 float viewquat_inv[4];
931 float zaxis[3] = {0, 0, 1};
936 invert_qt_qt_normalized(viewquat_inv, vod->viewquat);
938 mul_qt_v3(viewquat_inv, zaxis);
942 for (x = -1; x < 2; x++) {
943 for (y = -1; y < 2; y++) {
944 for (z = -1; z < 2; z++) {
946 float zaxis_test[3] = {x, y, z};
948 normalize_v3(zaxis_test);
950 if (angle_normalized_v3v3(zaxis_test, zaxis) < axis_limit) {
951 copy_v3_v3(zaxis_best, zaxis_test);
961 /* find the best roll */
962 float quat_roll[4], quat_final[4], quat_best[4], quat_snap[4];
963 float viewquat_align[4]; /* viewquat aligned to zaxis_best */
964 float viewquat_align_inv[4]; /* viewquat aligned to zaxis_best */
965 float best_angle = axis_limit;
968 /* viewquat_align is the original viewquat aligned to the snapped axis
969 * for testing roll */
970 rotation_between_vecs_to_quat(viewquat_align, zaxis_best, zaxis);
971 normalize_qt(viewquat_align);
972 mul_qt_qtqt(viewquat_align, vod->viewquat, viewquat_align);
973 normalize_qt(viewquat_align);
974 invert_qt_qt_normalized(viewquat_align_inv, viewquat_align);
976 vec_to_quat(quat_snap, zaxis_best, OB_NEGZ, OB_POSY);
977 normalize_qt(quat_snap);
978 invert_qt_normalized(quat_snap);
980 /* check if we can find the roll */
984 for (j = 0; j < 8; j++) {
986 float xaxis1[3] = {1, 0, 0};
987 float xaxis2[3] = {1, 0, 0};
988 float quat_final_inv[4];
990 axis_angle_to_quat(quat_roll, zaxis_best, (float)j * DEG2RADF(45.0f));
991 normalize_qt(quat_roll);
993 mul_qt_qtqt(quat_final, quat_snap, quat_roll);
994 normalize_qt(quat_final);
996 /* compare 2 vector angles to find the least roll */
997 invert_qt_qt_normalized(quat_final_inv, quat_final);
998 mul_qt_v3(viewquat_align_inv, xaxis1);
999 mul_qt_v3(quat_final_inv, xaxis2);
1000 angle = angle_v3v3(xaxis1, xaxis2);
1002 if (angle <= best_angle) {
1005 copy_qt_qt(quat_best, quat_final);
1010 /* lock 'quat_best' to an axis view if we can */
1011 rv3d->view = ED_view3d_quat_to_axis_view(quat_best, 0.01f);
1012 if (rv3d->view != RV3D_VIEW_USER) {
1013 ED_view3d_quat_from_axis_view(rv3d->view, quat_best);
1017 copy_qt_qt(quat_best, viewquat_align);
1020 copy_qt_qt(rv3d->viewquat, quat_best);
1022 viewrotate_apply_dyn_ofs(vod, rv3d->viewquat);
1026 static void viewrotate_apply(ViewOpsData *vod, int x, int y)
1028 RegionView3D *rv3d = vod->rv3d;
1030 rv3d->view = RV3D_VIEW_USER; /* need to reset every time because of view snapping */
1032 if (U.flag & USER_TRACKBALL) {
1033 float axis[3], q1[4], dvec[3], newvec[3];
1036 calctrackballvec(&vod->ar->winrct, x, y, newvec);
1038 sub_v3_v3v3(dvec, newvec, vod->trackvec);
1040 angle = (len_v3(dvec) / (2.0f * TRACKBALLSIZE)) * (float)M_PI;
1042 /* Allow for rotation beyond the interval [-pi, pi] */
1043 angle = angle_wrap_rad(angle);
1045 /* This relation is used instead of the actual angle between vectors
1046 * so that the angle of rotation is linearly proportional to
1047 * the distance that the mouse is dragged. */
1049 cross_v3_v3v3(axis, vod->trackvec, newvec);
1050 axis_angle_to_quat(q1, axis, angle);
1052 mul_qt_qtqt(vod->viewquat, q1, vod->oldquat);
1054 viewrotate_apply_dyn_ofs(vod, vod->viewquat);
1057 /* New turntable view code by John Aughey */
1058 float quat_local_x[4], quat_global_z[4];
1061 const float zvec_global[3] = {0.0f, 0.0f, 1.0f};
1064 /* Sensitivity will control how fast the viewport rotates. 0.007 was
1065 * obtained experimentally by looking at viewport rotation sensitivities
1066 * on other modeling programs. */
1067 /* Perhaps this should be a configurable user parameter. */
1068 const float sensitivity = 0.007f;
1070 /* Get the 3x3 matrix and its inverse from the quaternion */
1071 quat_to_mat3(m, vod->viewquat);
1072 invert_m3_m3(m_inv, m);
1074 /* avoid gimble lock */
1076 if (len_squared_v3v3(zvec_global, m_inv[2]) > 0.001f) {
1078 cross_v3_v3v3(xaxis, zvec_global, m_inv[2]);
1079 if (dot_v3v3(xaxis, m_inv[0]) < 0) {
1082 fac = angle_normalized_v3v3(zvec_global, m_inv[2]) / (float)M_PI;
1083 fac = fabsf(fac - 0.5f) * 2;
1085 interp_v3_v3v3(xaxis, xaxis, m_inv[0], fac);
1088 copy_v3_v3(xaxis, m_inv[0]);
1091 copy_v3_v3(xaxis, m_inv[0]);
1094 /* Determine the direction of the x vector (for rotating up and down) */
1095 /* This can likely be computed directly from the quaternion. */
1097 /* Perform the up/down rotation */
1098 axis_angle_to_quat(quat_local_x, xaxis, sensitivity * -(y - vod->oldy));
1099 mul_qt_qtqt(quat_local_x, vod->viewquat, quat_local_x);
1101 /* Perform the orbital rotation */
1102 axis_angle_to_quat_single(quat_global_z, 'Z', sensitivity * vod->reverse * (x - vod->oldx));
1103 mul_qt_qtqt(vod->viewquat, quat_local_x, quat_global_z);
1105 viewrotate_apply_dyn_ofs(vod, vod->viewquat);
1108 /* avoid precision loss over time */
1109 normalize_qt(vod->viewquat);
1111 /* use a working copy so view rotation locking doesnt overwrite the locked
1112 * rotation back into the view we calculate with */
1113 copy_qt_qt(rv3d->viewquat, vod->viewquat);
1115 /* check for view snap,
1116 * note: don't apply snap to vod->viewquat so the view wont jam up */
1117 if (vod->axis_snap) {
1118 viewrotate_apply_snap(vod);
1123 ED_view3d_camera_lock_sync(vod->v3d, rv3d);
1125 ED_region_tag_redraw(vod->ar);
1128 static int viewrotate_modal(bContext *C, wmOperator *op, const wmEvent *event)
1130 ViewOpsData *vod = op->customdata;
1131 short event_code = VIEW_PASS;
1132 bool use_autokey = false;
1133 int ret = OPERATOR_RUNNING_MODAL;
1135 /* execute the events */
1136 if (event->type == MOUSEMOVE) {
1137 event_code = VIEW_APPLY;
1139 else if (event->type == EVT_MODAL_MAP) {
1140 switch (event->val) {
1141 case VIEW_MODAL_CONFIRM:
1142 event_code = VIEW_CONFIRM;
1144 case VIEWROT_MODAL_AXIS_SNAP_ENABLE:
1145 vod->axis_snap = true;
1146 event_code = VIEW_APPLY;
1148 case VIEWROT_MODAL_AXIS_SNAP_DISABLE:
1149 vod->axis_snap = false;
1150 event_code = VIEW_APPLY;
1152 case VIEWROT_MODAL_SWITCH_ZOOM:
1153 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
1154 event_code = VIEW_CONFIRM;
1156 case VIEWROT_MODAL_SWITCH_MOVE:
1157 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1158 event_code = VIEW_CONFIRM;
1162 else if (event->type == vod->origkey && event->val == KM_RELEASE) {
1163 event_code = VIEW_CONFIRM;
1166 if (event_code == VIEW_APPLY) {
1167 viewrotate_apply(vod, event->x, event->y);
1168 if (ED_screen_animation_playing(CTX_wm_manager(C))) {
1172 else if (event_code == VIEW_CONFIRM) {
1173 ED_view3d_depth_tag_update(vod->rv3d);
1175 ret = OPERATOR_FINISHED;
1179 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, true);
1182 if (ret & OPERATOR_FINISHED) {
1183 viewops_data_free(C, op);
1190 * Action to take when rotating the view,
1191 * handle auto-persp and logic for switching out of views.
1195 static bool view3d_ensure_persp(struct View3D *v3d, ARegion *ar)
1197 RegionView3D *rv3d = ar->regiondata;
1198 const bool autopersp = (U.uiflag & USER_AUTOPERSP) != 0;
1200 BLI_assert((rv3d->viewlock & RV3D_LOCKED) == 0);
1202 if (ED_view3d_camera_lock_check(v3d, rv3d))
1205 if (rv3d->persp != RV3D_PERSP) {
1206 if (rv3d->persp == RV3D_CAMOB) {
1207 /* If autopersp and previous view was an axis one, switch back to PERSP mode, else reuse previous mode. */
1208 char persp = (autopersp && RV3D_VIEW_IS_AXIS(rv3d->lview)) ? RV3D_PERSP : rv3d->lpersp;
1209 view3d_persp_switch_from_camera(v3d, rv3d, persp);
1211 else if (autopersp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
1212 rv3d->persp = RV3D_PERSP;
1220 static int viewrotate_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1224 /* makes op->customdata */
1225 viewops_data_alloc(C, op);
1226 vod = op->customdata;
1228 /* poll should check but in some cases fails, see poll func for details */
1229 if (vod->rv3d->viewlock & RV3D_LOCKED) {
1230 viewops_data_free(C, op);
1231 return OPERATOR_PASS_THROUGH;
1234 ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar);
1236 /* switch from camera view when: */
1237 if (view3d_ensure_persp(vod->v3d, vod->ar)) {
1238 /* If we're switching from camera view to the perspective one,
1239 * need to tag viewport update, so camera vuew and borders
1240 * are properly updated.
1242 ED_region_tag_redraw(vod->ar);
1245 viewops_data_create(C, op, event);
1247 if (ELEM(event->type, MOUSEPAN, MOUSEROTATE)) {
1248 /* Rotate direction we keep always same */
1251 if (event->type == MOUSEPAN) {
1252 if (U.uiflag2 & USER_TRACKPAD_NATURAL) {
1253 x = 2 * event->x - event->prevx;
1254 y = 2 * event->y - event->prevy;
1262 /* MOUSEROTATE performs orbital rotation, so y axis delta is set to 0 */
1267 viewrotate_apply(vod, x, y);
1268 ED_view3d_depth_tag_update(vod->rv3d);
1270 viewops_data_free(C, op);
1272 return OPERATOR_FINISHED;
1275 /* add temp handler */
1276 WM_event_add_modal_handler(C, op);
1278 return OPERATOR_RUNNING_MODAL;
1282 /* test for unlocked camera view in quad view */
1283 static int view3d_camera_user_poll(bContext *C)
1288 if (ED_view3d_context_user_region(C, &v3d, &ar)) {
1289 RegionView3D *rv3d = ar->regiondata;
1290 if (rv3d->persp == RV3D_CAMOB) {
1298 static int view3d_lock_poll(bContext *C)
1300 View3D *v3d = CTX_wm_view3d(C);
1302 RegionView3D *rv3d = CTX_wm_region_view3d(C);
1304 return ED_view3d_offset_lock_check(v3d, rv3d);
1310 static void viewrotate_cancel(bContext *C, wmOperator *op)
1312 viewops_data_free(C, op);
1315 void VIEW3D_OT_rotate(wmOperatorType *ot)
1318 ot->name = "Rotate View";
1319 ot->description = "Rotate the view";
1320 ot->idname = "VIEW3D_OT_rotate";
1323 ot->invoke = viewrotate_invoke;
1324 ot->modal = viewrotate_modal;
1325 ot->poll = ED_operator_region_view3d_active;
1326 ot->cancel = viewrotate_cancel;
1329 ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR;
1332 /** \name NDOF Utility Functions
1335 #define NDOF_HAS_TRANSLATE ((!ED_view3d_offset_lock_check(v3d, rv3d)) && !is_zero_v3(ndof->tvec))
1336 #define NDOF_HAS_ROTATE (((rv3d->viewlock & RV3D_LOCKED) == 0) && !is_zero_v3(ndof->rvec))
1339 * \param depth_pt: A point to calculate the depth (in perspective mode)
1341 static float view3d_ndof_pan_speed_calc_ex(RegionView3D *rv3d, const float depth_pt[3])
1343 float speed = rv3d->pixsize * NDOF_PIXELS_PER_SECOND;
1345 if (rv3d->is_persp) {
1346 speed *= ED_view3d_calc_zfac(rv3d, depth_pt, NULL);
1352 static float view3d_ndof_pan_speed_calc_from_dist(RegionView3D *rv3d, const float dist)
1357 BLI_assert(dist >= 0.0f);
1359 copy_v3_fl3(tvec, 0.0f, 0.0f, dist);
1360 /* rv3d->viewinv isn't always valid */
1362 mul_mat3_m4_v3(rv3d->viewinv, tvec);
1364 invert_qt_qt_normalized(viewinv, rv3d->viewquat);
1365 mul_qt_v3(viewinv, tvec);
1368 return view3d_ndof_pan_speed_calc_ex(rv3d, tvec);
1371 static float view3d_ndof_pan_speed_calc(RegionView3D *rv3d)
1374 negate_v3_v3(tvec, rv3d->ofs);
1376 return view3d_ndof_pan_speed_calc_ex(rv3d, tvec);
1380 * Zoom and pan in the same function since sometimes zoom is interpreted as dolly (pan forward).
1382 * \param has_zoom zoom, otherwise dolly, often `!rv3d->is_persp` since it doesnt make sense to dolly in ortho.
1384 static void view3d_ndof_pan_zoom(const struct wmNDOFMotionData *ndof, ScrArea *sa, ARegion *ar,
1385 const bool has_translate, const bool has_zoom)
1387 RegionView3D *rv3d = ar->regiondata;
1391 if (has_translate == false && has_zoom == false) {
1395 WM_event_ndof_pan_get(ndof, pan_vec, false);
1401 * velocity should be proportional to the linear velocity attained by rotational motion of same strength
1403 * proportional to arclength = radius * angle
1408 /* "zoom in" or "translate"? depends on zoom mode in user settings? */
1409 if (ndof->tvec[2]) {
1410 float zoom_distance = rv3d->dist * ndof->dt * ndof->tvec[2];
1412 if (U.ndof_flag & NDOF_ZOOM_INVERT)
1413 zoom_distance = -zoom_distance;
1415 rv3d->dist += zoom_distance;
1421 /* all callers must check */
1422 if (has_translate) {
1423 BLI_assert(ED_view3d_offset_lock_check((View3D *)sa->spacedata.first, rv3d) == false);
1427 if (has_translate) {
1428 const float speed = view3d_ndof_pan_speed_calc(rv3d);
1430 mul_v3_fl(pan_vec, speed * ndof->dt);
1432 /* transform motion from view to world coordinates */
1433 invert_qt_qt_normalized(view_inv, rv3d->viewquat);
1434 mul_qt_v3(view_inv, pan_vec);
1436 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1437 sub_v3_v3(rv3d->ofs, pan_vec);
1439 if (rv3d->viewlock & RV3D_BOXVIEW) {
1440 view3d_boxview_sync(sa, ar);
1446 static void view3d_ndof_orbit(const struct wmNDOFMotionData *ndof, ScrArea *sa, ARegion *ar,
1447 /* optional, can be NULL*/
1450 View3D *v3d = sa->spacedata.first;
1451 RegionView3D *rv3d = ar->regiondata;
1455 BLI_assert((rv3d->viewlock & RV3D_LOCKED) == 0);
1457 view3d_ensure_persp(v3d, ar);
1459 rv3d->view = RV3D_VIEW_USER;
1461 invert_qt_qt_normalized(view_inv, rv3d->viewquat);
1463 if (U.ndof_flag & NDOF_TURNTABLE) {
1466 /* turntable view code by John Aughey, adapted for 3D mouse by [mce] */
1467 float angle, quat[4];
1468 float xvec[3] = {1, 0, 0};
1470 /* only use XY, ignore Z */
1471 WM_event_ndof_rotate_get(ndof, rot);
1473 /* Determine the direction of the x vector (for rotating up and down) */
1474 mul_qt_v3(view_inv, xvec);
1476 /* Perform the up/down rotation */
1477 angle = ndof->dt * rot[0];
1478 axis_angle_to_quat(quat, xvec, angle);
1479 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
1481 /* Perform the orbital rotation */
1482 angle = ndof->dt * rot[1];
1484 /* update the onscreen doo-dad */
1485 rv3d->rot_angle = angle;
1486 rv3d->rot_axis[0] = 0;
1487 rv3d->rot_axis[1] = 0;
1488 rv3d->rot_axis[2] = 1;
1490 axis_angle_to_quat_single(quat, 'Z', angle);
1491 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
1497 float angle = WM_event_ndof_to_axis_angle(ndof, axis);
1499 /* transform rotation axis from view to world coordinates */
1500 mul_qt_v3(view_inv, axis);
1502 /* update the onscreen doo-dad */
1503 rv3d->rot_angle = angle;
1504 copy_v3_v3(rv3d->rot_axis, axis);
1506 axis_angle_to_quat(quat, axis, angle);
1508 /* apply rotation */
1509 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
1513 viewrotate_apply_dyn_ofs(vod, rv3d->viewquat);
1518 * Called from both fly mode and walk mode,
1520 void view3d_ndof_fly(
1521 const wmNDOFMotionData *ndof,
1522 View3D *v3d, RegionView3D *rv3d,
1523 const bool use_precision, const short protectflag,
1524 bool *r_has_translate, bool *r_has_rotate)
1526 bool has_translate = NDOF_HAS_TRANSLATE;
1527 bool has_rotate = NDOF_HAS_ROTATE;
1530 invert_qt_qt_normalized(view_inv, rv3d->viewquat);
1532 rv3d->rot_angle = 0.0f; /* disable onscreen rotation doo-dad */
1534 if (has_translate) {
1535 /* ignore real 'dist' since fly has its own speed settings,
1536 * also its overwritten at this point. */
1537 float speed = view3d_ndof_pan_speed_calc_from_dist(rv3d, 1.0f);
1538 float trans[3], trans_orig_y;
1543 WM_event_ndof_pan_get(ndof, trans, false);
1544 mul_v3_fl(trans, speed * ndof->dt);
1545 trans_orig_y = trans[1];
1547 if (U.ndof_flag & NDOF_FLY_HELICOPTER) {
1551 /* transform motion from view to world coordinates */
1552 mul_qt_v3(view_inv, trans);
1554 if (U.ndof_flag & NDOF_FLY_HELICOPTER) {
1555 /* replace world z component with device y (yes it makes sense) */
1556 trans[2] = trans_orig_y;
1559 if (rv3d->persp == RV3D_CAMOB) {
1560 /* respect camera position locks */
1561 if (protectflag & OB_LOCK_LOCX) trans[0] = 0.0f;
1562 if (protectflag & OB_LOCK_LOCY) trans[1] = 0.0f;
1563 if (protectflag & OB_LOCK_LOCZ) trans[2] = 0.0f;
1566 if (!is_zero_v3(trans)) {
1567 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1568 sub_v3_v3(rv3d->ofs, trans);
1569 has_translate = true;
1572 has_translate = false;
1577 const float turn_sensitivity = 1.0f;
1581 float angle = turn_sensitivity * WM_event_ndof_to_axis_angle(ndof, axis);
1583 if (fabsf(angle) > 0.0001f) {
1589 /* transform rotation axis from view to world coordinates */
1590 mul_qt_v3(view_inv, axis);
1592 /* apply rotation to view */
1593 axis_angle_to_quat(rotation, axis, angle);
1594 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
1596 if (U.ndof_flag & NDOF_LOCK_HORIZON) {
1597 /* force an upright viewpoint
1598 * TODO: make this less... sudden */
1599 float view_horizon[3] = {1.0f, 0.0f, 0.0f}; /* view +x */
1600 float view_direction[3] = {0.0f, 0.0f, -1.0f}; /* view -z (into screen) */
1602 /* find new inverse since viewquat has changed */
1603 invert_qt_qt_normalized(view_inv, rv3d->viewquat);
1604 /* could apply reverse rotation to existing view_inv to save a few cycles */
1606 /* transform view vectors to world coordinates */
1607 mul_qt_v3(view_inv, view_horizon);
1608 mul_qt_v3(view_inv, view_direction);
1611 /* find difference between view & world horizons
1612 * true horizon lives in world xy plane, so look only at difference in z */
1613 angle = -asinf(view_horizon[2]);
1615 /* rotate view so view horizon = world horizon */
1616 axis_angle_to_quat(rotation, view_direction, angle);
1617 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
1620 rv3d->view = RV3D_VIEW_USER;
1627 *r_has_translate = has_translate;
1628 *r_has_rotate = has_rotate;
1634 /* -- "orbit" navigation (trackball/turntable)
1636 * -- panning in rotationally-locked views
1638 static int ndof_orbit_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1641 if (event->type != NDOF_MOTION) {
1642 return OPERATOR_CANCELLED;
1649 const wmNDOFMotionData *ndof = event->customdata;
1651 viewops_data_alloc(C, op);
1652 viewops_data_create_ex(C, op, event,
1653 (U.uiflag & USER_ORBIT_SELECTION) != 0, false);
1654 vod = op->customdata;
1656 ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar);
1661 /* off by default, until changed later this function */
1662 rv3d->rot_angle = 0.0f;
1664 ED_view3d_camera_lock_init_ex(v3d, rv3d, false);
1666 if (ndof->progress != P_FINISHING) {
1667 const bool has_rotation = NDOF_HAS_ROTATE;
1668 /* if we can't rotate, fallback to translate (locked axis views) */
1669 const bool has_translate = NDOF_HAS_TRANSLATE && (rv3d->viewlock & RV3D_LOCKED);
1670 const bool has_zoom = (ndof->tvec[2] != 0.0f) && !rv3d->is_persp;
1672 if (has_translate || has_zoom) {
1673 view3d_ndof_pan_zoom(ndof, vod->sa, vod->ar, has_translate, has_zoom);
1677 view3d_ndof_orbit(ndof, vod->sa, vod->ar, vod);
1681 ED_view3d_camera_lock_sync(v3d, rv3d);
1683 ED_region_tag_redraw(vod->ar);
1685 viewops_data_free(C, op);
1687 return OPERATOR_FINISHED;
1691 void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot)
1694 ot->name = "NDOF Orbit View";
1695 ot->description = "Orbit the view using the 3D mouse";
1696 ot->idname = "VIEW3D_OT_ndof_orbit";
1699 ot->invoke = ndof_orbit_invoke;
1700 ot->poll = ED_operator_view3d_active;
1706 static int ndof_orbit_zoom_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1709 if (event->type != NDOF_MOTION) {
1710 return OPERATOR_CANCELLED;
1717 const wmNDOFMotionData *ndof = event->customdata;
1719 viewops_data_alloc(C, op);
1720 viewops_data_create_ex(C, op, event,
1721 (U.uiflag & USER_ORBIT_SELECTION) != 0, false);
1723 vod = op->customdata;
1725 ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar);
1730 /* off by default, until changed later this function */
1731 rv3d->rot_angle = 0.0f;
1733 ED_view3d_camera_lock_init_ex(v3d, rv3d, false);
1735 if (ndof->progress == P_FINISHING) {
1738 else if ((rv3d->persp == RV3D_ORTHO) && RV3D_VIEW_IS_AXIS(rv3d->view)) {
1739 /* if we can't rotate, fallback to translate (locked axis views) */
1740 const bool has_translate = NDOF_HAS_TRANSLATE;
1741 const bool has_zoom = (ndof->tvec[2] != 0.0f) && ED_view3d_offset_lock_check(v3d, rv3d);
1743 if (has_translate || has_zoom) {
1744 view3d_ndof_pan_zoom(ndof, vod->sa, vod->ar, has_translate, true);
1747 else if ((U.ndof_flag & NDOF_MODE_ORBIT) ||
1748 ED_view3d_offset_lock_check(v3d, rv3d))
1750 const bool has_rotation = NDOF_HAS_ROTATE;
1751 const bool has_zoom = (ndof->tvec[2] != 0.0f);
1754 view3d_ndof_pan_zoom(ndof, vod->sa, vod->ar, false, has_zoom);
1758 view3d_ndof_orbit(ndof, vod->sa, vod->ar, vod);
1761 else { /* free/explore (like fly mode) */
1762 const bool has_rotation = NDOF_HAS_ROTATE;
1763 const bool has_translate = NDOF_HAS_TRANSLATE;
1764 const bool has_zoom = (ndof->tvec[2] != 0.0f) && !rv3d->is_persp;
1768 if (has_translate || has_zoom) {
1769 view3d_ndof_pan_zoom(ndof, vod->sa, vod->ar, has_translate, has_zoom);
1772 dist_backup = rv3d->dist;
1773 ED_view3d_distance_set(rv3d, 0.0f);
1776 view3d_ndof_orbit(ndof, vod->sa, vod->ar, NULL);
1779 ED_view3d_distance_set(rv3d, dist_backup);
1782 ED_view3d_camera_lock_sync(v3d, rv3d);
1784 ED_region_tag_redraw(vod->ar);
1786 viewops_data_free(C, op);
1788 return OPERATOR_FINISHED;
1792 void VIEW3D_OT_ndof_orbit_zoom(struct wmOperatorType *ot)
1795 ot->name = "NDOF Orbit View with Zoom";
1796 ot->description = "Orbit and zoom the view using the 3D mouse";
1797 ot->idname = "VIEW3D_OT_ndof_orbit_zoom";
1800 ot->invoke = ndof_orbit_zoom_invoke;
1801 ot->poll = ED_operator_view3d_active;
1807 /* -- "pan" navigation
1810 static int ndof_pan_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
1812 if (event->type != NDOF_MOTION) {
1813 return OPERATOR_CANCELLED;
1816 View3D *v3d = CTX_wm_view3d(C);
1817 RegionView3D *rv3d = CTX_wm_region_view3d(C);
1818 const wmNDOFMotionData *ndof = event->customdata;
1820 const bool has_translate = NDOF_HAS_TRANSLATE;
1821 const bool has_zoom = (ndof->tvec[2] != 0.0f) && !rv3d->is_persp;
1823 /* we're panning here! so erase any leftover rotation from other operators */
1824 rv3d->rot_angle = 0.0f;
1826 if (!(has_translate || has_zoom))
1827 return OPERATOR_CANCELLED;
1829 ED_view3d_camera_lock_init_ex(v3d, rv3d, false);
1831 if (ndof->progress != P_FINISHING) {
1832 ScrArea *sa = CTX_wm_area(C);
1833 ARegion *ar = CTX_wm_region(C);
1835 if (has_translate || has_zoom) {
1836 view3d_ndof_pan_zoom(ndof, sa, ar, has_translate, has_zoom);
1840 ED_view3d_camera_lock_sync(v3d, rv3d);
1842 ED_region_tag_redraw(CTX_wm_region(C));
1844 return OPERATOR_FINISHED;
1848 void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot)
1851 ot->name = "NDOF Pan View";
1852 ot->description = "Pan the view with the 3D mouse";
1853 ot->idname = "VIEW3D_OT_ndof_pan";
1856 ot->invoke = ndof_pan_invoke;
1857 ot->poll = ED_operator_view3d_active;
1865 * wraps #ndof_orbit_zoom but never restrict to orbit.
1867 static int ndof_all_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1869 /* weak!, but it works */
1870 const int ndof_flag = U.ndof_flag;
1873 U.ndof_flag &= ~NDOF_MODE_ORBIT;
1875 ret = ndof_orbit_zoom_invoke(C, op, event);
1877 U.ndof_flag = ndof_flag;
1882 void VIEW3D_OT_ndof_all(struct wmOperatorType *ot)
1885 ot->name = "NDOF Move View";
1886 ot->description = "Pan and rotate the view with the 3D mouse";
1887 ot->idname = "VIEW3D_OT_ndof_all";
1890 ot->invoke = ndof_all_invoke;
1891 ot->poll = ED_operator_view3d_active;
1897 /* ************************ viewmove ******************************** */
1900 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
1902 /* called in transform_ops.c, on each regeneration of keymaps */
1903 void viewmove_modal_keymap(wmKeyConfig *keyconf)
1905 static EnumPropertyItem modal_items[] = {
1906 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
1908 {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
1909 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1911 {0, NULL, 0, NULL, NULL}
1914 wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Move Modal");
1916 /* this function is called for each spacetype, only needs to add map once */
1917 if (keymap && keymap->modal_items) return;
1919 keymap = WM_modalkeymap_add(keyconf, "View3D Move Modal", modal_items);
1921 /* items for modal map */
1922 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1923 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1925 /* disabled mode switching for now, can re-implement better, later on */
1927 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1928 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1929 WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1932 /* assign map to operators */
1933 WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
1937 static void viewmove_apply(ViewOpsData *vod, int x, int y)
1939 if (ED_view3d_offset_lock_check(vod->v3d, vod->rv3d)) {
1940 vod->rv3d->ofs_lock[0] -= ((vod->oldx - x) * 2.0f) / (float)vod->ar->winx;
1941 vod->rv3d->ofs_lock[1] -= ((vod->oldy - y) * 2.0f) / (float)vod->ar->winy;
1943 else if ((vod->rv3d->persp == RV3D_CAMOB) && !ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
1944 const float zoomfac = BKE_screen_view3d_zoom_to_fac(vod->rv3d->camzoom) * 2.0f;
1945 vod->rv3d->camdx += (vod->oldx - x) / (vod->ar->winx * zoomfac);
1946 vod->rv3d->camdy += (vod->oldy - y) / (vod->ar->winy * zoomfac);
1947 CLAMP(vod->rv3d->camdx, -1.0f, 1.0f);
1948 CLAMP(vod->rv3d->camdy, -1.0f, 1.0f);
1954 mval_f[0] = x - vod->oldx;
1955 mval_f[1] = y - vod->oldy;
1956 ED_view3d_win_to_delta(vod->ar, mval_f, dvec, vod->zfac);
1958 add_v3_v3(vod->rv3d->ofs, dvec);
1960 if (vod->rv3d->viewlock & RV3D_BOXVIEW)
1961 view3d_boxview_sync(vod->sa, vod->ar);
1967 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1969 ED_region_tag_redraw(vod->ar);
1973 static int viewmove_modal(bContext *C, wmOperator *op, const wmEvent *event)
1976 ViewOpsData *vod = op->customdata;
1977 short event_code = VIEW_PASS;
1978 bool use_autokey = false;
1979 int ret = OPERATOR_RUNNING_MODAL;
1981 /* execute the events */
1982 if (event->type == MOUSEMOVE) {
1983 event_code = VIEW_APPLY;
1985 else if (event->type == EVT_MODAL_MAP) {
1986 switch (event->val) {
1987 case VIEW_MODAL_CONFIRM:
1988 event_code = VIEW_CONFIRM;
1990 case VIEWROT_MODAL_SWITCH_ZOOM:
1991 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
1992 event_code = VIEW_CONFIRM;
1994 case VIEWROT_MODAL_SWITCH_ROTATE:
1995 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1996 event_code = VIEW_CONFIRM;
2000 else if (event->type == vod->origkey && event->val == KM_RELEASE) {
2001 event_code = VIEW_CONFIRM;
2004 if (event_code == VIEW_APPLY) {
2005 viewmove_apply(vod, event->x, event->y);
2006 if (ED_screen_animation_playing(CTX_wm_manager(C))) {
2010 else if (event_code == VIEW_CONFIRM) {
2011 ED_view3d_depth_tag_update(vod->rv3d);
2013 ret = OPERATOR_FINISHED;
2017 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
2020 if (ret & OPERATOR_FINISHED) {
2021 viewops_data_free(C, op);
2027 static int viewmove_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2031 /* makes op->customdata */
2032 viewops_data_alloc(C, op);
2033 viewops_data_create(C, op, event);
2034 vod = op->customdata;
2036 ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar);
2038 if (event->type == MOUSEPAN) {
2039 /* invert it, trackpad scroll follows same principle as 2d windows this way */
2040 viewmove_apply(vod, 2 * event->x - event->prevx, 2 * event->y - event->prevy);
2041 ED_view3d_depth_tag_update(vod->rv3d);
2043 viewops_data_free(C, op);
2045 return OPERATOR_FINISHED;
2048 /* add temp handler */
2049 WM_event_add_modal_handler(C, op);
2051 return OPERATOR_RUNNING_MODAL;
2055 static void viewmove_cancel(bContext *C, wmOperator *op)
2057 viewops_data_free(C, op);
2060 void VIEW3D_OT_move(wmOperatorType *ot)
2064 ot->name = "Move View";
2065 ot->description = "Move the view";
2066 ot->idname = "VIEW3D_OT_move";
2069 ot->invoke = viewmove_invoke;
2070 ot->modal = viewmove_modal;
2071 ot->poll = ED_operator_view3d_active;
2072 ot->cancel = viewmove_cancel;
2075 ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR;
2078 /* ************************ viewzoom ******************************** */
2080 /* viewdolly_modal_keymap has an exact copy of this, apply fixes to both */
2081 /* called in transform_ops.c, on each regeneration of keymaps */
2082 void viewzoom_modal_keymap(wmKeyConfig *keyconf)
2084 static EnumPropertyItem modal_items[] = {
2085 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
2087 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
2088 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
2090 {0, NULL, 0, NULL, NULL}
2093 wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Zoom Modal");
2095 /* this function is called for each spacetype, only needs to add map once */
2096 if (keymap && keymap->modal_items) return;
2098 keymap = WM_modalkeymap_add(keyconf, "View3D Zoom Modal", modal_items);
2100 /* items for modal map */
2101 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
2102 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
2104 /* disabled mode switching for now, can re-implement better, later on */
2106 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
2107 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
2108 WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
2111 /* assign map to operators */
2112 WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
2115 static void view_zoom_mouseloc_camera(
2116 Scene *scene, View3D *v3d,
2117 ARegion *ar, float dfac, int mx, int my)
2119 RegionView3D *rv3d = ar->regiondata;
2120 const float zoomfac = BKE_screen_view3d_zoom_to_fac(rv3d->camzoom);
2121 const float zoomfac_new = CLAMPIS(zoomfac * (1.0f / dfac), RV3D_CAMZOOM_MIN_FACTOR, RV3D_CAMZOOM_MAX_FACTOR);
2122 const float camzoom_new = BKE_screen_view3d_zoom_from_fac(zoomfac_new);
2125 if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
2127 rctf camera_frame_old;
2128 rctf camera_frame_new;
2130 const float pt_src[2] = {mx, my};
2134 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &camera_frame_old, false);
2135 BLI_rctf_translate(&camera_frame_old, ar->winrct.xmin, ar->winrct.ymin);
2137 rv3d->camzoom = camzoom_new;
2138 CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2140 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &camera_frame_new, false);
2141 BLI_rctf_translate(&camera_frame_new, ar->winrct.xmin, ar->winrct.ymin);
2143 BLI_rctf_transform_pt_v(&camera_frame_new, &camera_frame_old, pt_dst, pt_src);
2144 sub_v2_v2v2(delta_px, pt_dst, pt_src);
2146 /* translate the camera offset using pixel space delta
2147 * mapped back to the camera (same logic as panning in camera view) */
2148 zoomfac_px = BKE_screen_view3d_zoom_to_fac(rv3d->camzoom) * 2.0f;
2150 rv3d->camdx += delta_px[0] / (ar->winx * zoomfac_px);
2151 rv3d->camdy += delta_px[1] / (ar->winy * zoomfac_px);
2152 CLAMP(rv3d->camdx, -1.0f, 1.0f);
2153 CLAMP(rv3d->camdy, -1.0f, 1.0f);
2156 rv3d->camzoom = camzoom_new;
2157 CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2161 static void view_zoom_mouseloc_3d(ARegion *ar, float dfac, int mx, int my)
2163 RegionView3D *rv3d = ar->regiondata;
2164 const float dist_new = rv3d->dist * dfac;
2166 if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
2174 negate_v3_v3(tpos, rv3d->ofs);
2176 mval_f[0] = (float)(((mx - ar->winrct.xmin) * 2) - ar->winx) / 2.0f;
2177 mval_f[1] = (float)(((my - ar->winrct.ymin) * 2) - ar->winy) / 2.0f;
2179 /* Project cursor position into 3D space */
2180 zfac = ED_view3d_calc_zfac(rv3d, tpos, NULL);
2181 ED_view3d_win_to_delta(ar, mval_f, dvec, zfac);
2183 /* Calculate view target position for dolly */
2184 add_v3_v3v3(tvec, tpos, dvec);
2187 /* Offset to target position and dolly */
2188 copy_v3_v3(rv3d->ofs, tvec);
2189 rv3d->dist = dist_new;
2191 /* Calculate final offset */
2192 madd_v3_v3v3fl(rv3d->ofs, tvec, dvec, dfac);
2195 rv3d->dist = dist_new;
2199 static float viewzoom_scale_value(
2201 const short viewzoom,
2202 const bool zoom_invert, const bool zoom_invert_force,
2203 const int xy[2], const int xy_orig[2],
2204 const float val, const float val_orig,
2205 double *r_timer_lastdraw)
2209 if (viewzoom == USER_ZOOM_CONT) {
2210 double time = PIL_check_seconds_timer();
2211 float time_step = (float)(time - *r_timer_lastdraw);
2214 if (U.uiflag & USER_ZOOM_HORIZ) {
2215 fac = (float)(xy_orig[0] - xy[0]);
2218 fac = (float)(xy_orig[1] - xy[1]);
2221 if (zoom_invert != zoom_invert_force) {
2226 zfac = 1.0f + ((fac / 20.0f) * time_step);
2227 *r_timer_lastdraw = time;
2229 else if (viewzoom == USER_ZOOM_SCALE) {
2230 /* method which zooms based on how far you move the mouse */
2232 const int ctr[2] = {
2233 BLI_rcti_cent_x(winrct),
2234 BLI_rcti_cent_y(winrct),
2236 float len_new = 5 + len_v2v2_int(ctr, xy);
2237 float len_old = 5 + len_v2v2_int(ctr, xy_orig);
2239 /* intentionally ignore 'zoom_invert' for scale */
2240 if (zoom_invert_force) {
2241 SWAP(float, len_new, len_old);
2244 zfac = val_orig * (len_old / max_ff(len_new, 1.0f)) / val;
2246 else { /* USER_ZOOM_DOLLY */
2250 if (U.uiflag & USER_ZOOM_HORIZ) {
2251 len_new += (winrct->xmax - xy[0]);
2252 len_old += (winrct->xmax - xy_orig[0]);
2255 len_new += (winrct->ymax - xy[1]);
2256 len_old += (winrct->ymax - xy_orig[1]);
2259 if (zoom_invert != zoom_invert_force) {
2260 SWAP(float, len_new, len_old);
2263 zfac = val_orig * (2.0f * ((len_new / max_ff(len_old, 1.0f)) - 1.0f) + 1.0f) / val;
2270 static void viewzoom_apply_camera(
2271 ViewOpsData *vod, const int xy[2],
2272 const short viewzoom, const bool zoom_invert)
2275 float zoomfac_prev = BKE_screen_view3d_zoom_to_fac(vod->camzoom_prev) * 2.0f;
2276 float zoomfac = BKE_screen_view3d_zoom_to_fac(vod->rv3d->camzoom) * 2.0f;
2278 zfac = viewzoom_scale_value(
2279 &vod->ar->winrct, viewzoom, zoom_invert, true, xy, &vod->origx,
2280 zoomfac, zoomfac_prev,
2281 &vod->timer_lastdraw);
2283 if (zfac != 1.0f && zfac != 0.0f) {
2284 /* calculate inverted, then invert again (needed because of camera zoom scaling) */
2286 view_zoom_mouseloc_camera(
2287 vod->scene, vod->v3d,
2288 vod->ar, zfac, vod->oldx, vod->oldy);
2291 ED_region_tag_redraw(vod->ar);
2294 static void viewzoom_apply_3d(
2295 ViewOpsData *vod, const int xy[2],
2296 const short viewzoom, const bool zoom_invert)
2299 float dist_range[2];
2301 ED_view3d_dist_range_get(vod->v3d, dist_range);
2303 zfac = viewzoom_scale_value(
2304 &vod->ar->winrct, viewzoom, zoom_invert, false, xy, &vod->origx,
2305 vod->rv3d->dist, vod->dist_prev,
2306 &vod->timer_lastdraw);
2309 const float zfac_min = dist_range[0] / vod->rv3d->dist;
2310 const float zfac_max = dist_range[1] / vod->rv3d->dist;
2311 CLAMP(zfac, zfac_min, zfac_max);
2313 view_zoom_mouseloc_3d(
2314 vod->ar, zfac, vod->oldx, vod->oldy);
2317 /* these limits were in old code too */
2318 CLAMP(vod->rv3d->dist, dist_range[0], dist_range[1]);
2320 if (vod->rv3d->viewlock & RV3D_BOXVIEW)
2321 view3d_boxview_sync(vod->sa, vod->ar);
2323 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
2325 ED_region_tag_redraw(vod->ar);
2328 static void viewzoom_apply(
2329 ViewOpsData *vod, const int xy[2],
2330 const short viewzoom, const bool zoom_invert)
2332 if ((vod->rv3d->persp == RV3D_CAMOB) &&
2333 (vod->rv3d->is_persp && ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) == 0)
2335 viewzoom_apply_camera(vod, xy, viewzoom, zoom_invert);
2338 viewzoom_apply_3d(vod, xy, viewzoom, zoom_invert);
2342 static int viewzoom_modal(bContext *C, wmOperator *op, const wmEvent *event)
2344 ViewOpsData *vod = op->customdata;
2345 short event_code = VIEW_PASS;
2346 bool use_autokey = false;
2347 int ret = OPERATOR_RUNNING_MODAL;
2349 /* execute the events */
2350 if (event->type == TIMER && event->customdata == vod->timer) {
2351 /* continuous zoom */
2352 event_code = VIEW_APPLY;
2354 else if (event->type == MOUSEMOVE) {
2355 event_code = VIEW_APPLY;
2357 else if (event->type == EVT_MODAL_MAP) {
2358 switch (event->val) {
2359 case VIEW_MODAL_CONFIRM:
2360 event_code = VIEW_CONFIRM;
2362 case VIEWROT_MODAL_SWITCH_MOVE:
2363 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
2364 event_code = VIEW_CONFIRM;
2366 case VIEWROT_MODAL_SWITCH_ROTATE:
2367 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
2368 event_code = VIEW_CONFIRM;
2372 else if (event->type == vod->origkey && event->val == KM_RELEASE) {
2373 event_code = VIEW_CONFIRM;
2376 if (event_code == VIEW_APPLY) {
2377 viewzoom_apply(vod, &event->x, U.viewzoom, (U.uiflag & USER_ZOOM_INVERT) != 0);
2378 if (ED_screen_animation_playing(CTX_wm_manager(C))) {
2382 else if (event_code == VIEW_CONFIRM) {
2383 ED_view3d_depth_tag_update(vod->rv3d);
2385 ret = OPERATOR_FINISHED;
2389 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
2392 if (ret & OPERATOR_FINISHED) {
2393 viewops_data_free(C, op);
2399 static int viewzoom_exec(bContext *C, wmOperator *op)
2401 Scene *scene = CTX_data_scene(C);
2407 float dist_range[2];
2409 const int delta = RNA_int_get(op->ptr, "delta");
2412 if (op->customdata) {
2413 ViewOpsData *vod = op->customdata;
2419 sa = CTX_wm_area(C);
2420 ar = CTX_wm_region(C);
2423 v3d = sa->spacedata.first;
2424 rv3d = ar->regiondata;
2426 mx = RNA_struct_property_is_set(op->ptr, "mx") ? RNA_int_get(op->ptr, "mx") : ar->winx / 2;
2427 my = RNA_struct_property_is_set(op->ptr, "my") ? RNA_int_get(op->ptr, "my") : ar->winy / 2;
2429 use_cam_zoom = (rv3d->persp == RV3D_CAMOB) && !(rv3d->is_persp && ED_view3d_camera_lock_check(v3d, rv3d));
2431 ED_view3d_dist_range_get(v3d, dist_range);
2434 const float step = 1.2f;
2435 /* this min and max is also in viewmove() */
2437 view_zoom_mouseloc_camera(scene, v3d, ar, step, mx, my);
2440 if (rv3d->dist < dist_range[1]) {
2441 view_zoom_mouseloc_3d(ar, step, mx, my);
2446 const float step = 1.0f / 1.2f;
2448 view_zoom_mouseloc_camera(scene, v3d, ar, step, mx, my);
2451 if (rv3d->dist > dist_range[0]) {
2452 view_zoom_mouseloc_3d(ar, step, mx, my);
2457 if (rv3d->viewlock & RV3D_BOXVIEW)
2458 view3d_boxview_sync(sa, ar);
2460 ED_view3d_depth_tag_update(rv3d);
2462 ED_view3d_camera_lock_sync(v3d, rv3d);
2463 ED_view3d_camera_lock_autokey(v3d, rv3d, C, false, true);
2465 ED_region_tag_redraw(ar);
2467 viewops_data_free(C, op);
2469 return OPERATOR_FINISHED;
2472 /* this is an exact copy of viewzoom_modal_keymap */
2473 /* called in transform_ops.c, on each regeneration of keymaps */
2474 void viewdolly_modal_keymap(wmKeyConfig *keyconf)
2476 static EnumPropertyItem modal_items[] = {
2477 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
2479 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
2480 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
2482 {0, NULL, 0, NULL, NULL}
2485 wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Dolly Modal");
2487 /* this function is called for each spacetype, only needs to add map once */
2488 if (keymap && keymap->modal_items) return;
2490 keymap = WM_modalkeymap_add(keyconf, "View3D Dolly Modal", modal_items);
2492 /* items for modal map */
2493 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
2494 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
2496 /* disabled mode switching for now, can re-implement better, later on */
2498 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
2499 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
2500 WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
2503 /* assign map to operators */
2504 WM_modalkeymap_assign(keymap, "VIEW3D_OT_dolly");
2507 /* viewdolly_invoke() copied this function, changes here may apply there */
2508 static int viewzoom_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2512 /* makes op->customdata */
2513 viewops_data_alloc(C, op);
2514 viewops_data_create(C, op, event);
2515 vod = op->customdata;
2517 ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar);
2519 /* if one or the other zoom position aren't set, set from event */
2520 if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
2521 RNA_int_set(op->ptr, "mx", event->x);
2522 RNA_int_set(op->ptr, "my", event->y);
2525 if (RNA_struct_property_is_set(op->ptr, "delta")) {
2526 viewzoom_exec(C, op);
2529 if (event->type == MOUSEZOOM || event->type == MOUSEPAN) {
2531 if (U.uiflag & USER_ZOOM_HORIZ) {
2532 vod->origx = vod->oldx = event->x;
2533 viewzoom_apply(vod, &event->prevx, USER_ZOOM_DOLLY, (U.uiflag & USER_ZOOM_INVERT) != 0);
2536 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
2537 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
2538 viewzoom_apply(vod, &event->prevx, USER_ZOOM_DOLLY, (U.uiflag & USER_ZOOM_INVERT) != 0);
2540 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
2542 ED_view3d_depth_tag_update(vod->rv3d);
2544 viewops_data_free(C, op);
2545 return OPERATOR_FINISHED;
2548 if (U.viewzoom == USER_ZOOM_CONT) {
2549 /* needs a timer to continue redrawing */
2550 vod->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
2551 vod->timer_lastdraw = PIL_check_seconds_timer();
2554 /* add temp handler */
2555 WM_event_add_modal_handler(C, op);
2557 return OPERATOR_RUNNING_MODAL;
2560 return OPERATOR_FINISHED;
2563 static void viewzoom_cancel(bContext *C, wmOperator *op)
2565 viewops_data_free(C, op);
2568 void VIEW3D_OT_zoom(wmOperatorType *ot)
2573 ot->name = "Zoom View";
2574 ot->description = "Zoom in/out in the view";
2575 ot->idname = "VIEW3D_OT_zoom";
2578 ot->invoke = viewzoom_invoke;
2579 ot->exec = viewzoom_exec;
2580 ot->modal = viewzoom_modal;
2581 ot->poll = ED_operator_region_view3d_active;
2582 ot->cancel = viewzoom_cancel;
2585 ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR;
2587 RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
2588 prop = RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
2589 RNA_def_property_flag(prop, PROP_HIDDEN);
2590 prop = RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
2591 RNA_def_property_flag(prop, PROP_HIDDEN);
2595 /* ************************ viewdolly ******************************** */
2596 static void view_dolly_mouseloc(ARegion *ar, float orig_ofs[3], float dvec[3], float dfac)
2598 RegionView3D *rv3d = ar->regiondata;
2599 madd_v3_v3v3fl(rv3d->ofs, orig_ofs, dvec, -(1.0f - dfac));
2602 static void viewdolly_apply(ViewOpsData *vod, int x, int y, const short zoom_invert)
2609 if (U.uiflag & USER_ZOOM_HORIZ) {
2610 len1 = (vod->ar->winrct.xmax - x) + 5;
2611 len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
2614 len1 = (vod->ar->winrct.ymax - y) + 5;
2615 len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
2618 SWAP(float, len1, len2);
2620 zfac = 1.0f + ((len1 - len2) * 0.01f * vod->rv3d->dist);
2624 view_dolly_mouseloc(vod->ar, vod->ofs, vod->mousevec, zfac);
2626 if (vod->rv3d->viewlock & RV3D_BOXVIEW)
2627 view3d_boxview_sync(vod->sa, vod->ar);
2629 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
2631 ED_region_tag_redraw(vod->ar);
2635 static int viewdolly_modal(bContext *C, wmOperator *op, const wmEvent *event)
2637 ViewOpsData *vod = op->customdata;
2638 short event_code = VIEW_PASS;
2639 bool use_autokey = false;
2640 int ret = OPERATOR_RUNNING_MODAL;
2642 /* execute the events */
2643 if (event->type == MOUSEMOVE) {
2644 event_code = VIEW_APPLY;
2646 else if (event->type == EVT_MODAL_MAP) {
2647 switch (event->val) {
2648 case VIEW_MODAL_CONFIRM:
2649 event_code = VIEW_CONFIRM;
2651 case VIEWROT_MODAL_SWITCH_MOVE:
2652 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
2653 event_code = VIEW_CONFIRM;
2655 case VIEWROT_MODAL_SWITCH_ROTATE:
2656 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
2657 event_code = VIEW_CONFIRM;
2661 else if (event->type == vod->origkey && event->val == KM_RELEASE) {
2662 event_code = VIEW_CONFIRM;
2665 if (event_code == VIEW_APPLY) {
2666 viewdolly_apply(vod, event->x, event->y, (U.uiflag & USER_ZOOM_INVERT) != 0);
2667 if (ED_screen_animation_playing(CTX_wm_manager(C))) {
2671 else if (event_code == VIEW_CONFIRM) {
2672 ED_view3d_depth_tag_update(vod->rv3d);
2674 ret = OPERATOR_FINISHED;
2678 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
2681 if (ret & OPERATOR_FINISHED) {
2682 viewops_data_free(C, op);
2688 static int viewdolly_exec(bContext *C, wmOperator *op)
2696 const int delta = RNA_int_get(op->ptr, "delta");
2698 if (op->customdata) {
2699 ViewOpsData *vod = op->customdata;
2703 copy_v3_v3(mousevec, vod->mousevec);
2706 sa = CTX_wm_area(C);
2707 ar = CTX_wm_region(C);
2708 negate_v3_v3(mousevec, ((RegionView3D *)ar->regiondata)->viewinv[2]);
2709 normalize_v3(mousevec);
2712 v3d = sa->spacedata.first;
2713 rv3d = ar->regiondata;
2715 /* overwrite the mouse vector with the view direction (zoom into the center) */
2716 if ((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
2717 normalize_v3_v3(mousevec, rv3d->viewinv[2]);
2721 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, 0.2f);
2724 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, 1.8f);
2727 if (rv3d->viewlock & RV3D_BOXVIEW)
2728 view3d_boxview_sync(sa, ar);
2730 ED_view3d_depth_tag_update(rv3d);
2732 ED_view3d_camera_lock_sync(v3d, rv3d);
2734 ED_region_tag_redraw(ar);
2736 viewops_data_free(C, op);
2738 return OPERATOR_FINISHED;
2741 /* copied from viewzoom_invoke(), changes here may apply there */
2742 static int viewdolly_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2746 if (view3d_operator_offset_lock_check(C, op))
2747 return OPERATOR_CANCELLED;
2749 /* makes op->customdata */
2750 viewops_data_alloc(C, op);
2751 vod = op->customdata;
2753 /* poll should check but in some cases fails, see poll func for details */
2754 if (vod->rv3d->viewlock & RV3D_LOCKED) {
2755 viewops_data_free(C, op);
2756 return OPERATOR_PASS_THROUGH;
2759 ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar);
2761 /* needs to run before 'viewops_data_create' so the backup 'rv3d->ofs' is correct */
2762 /* switch from camera view when: */
2763 if (vod->rv3d->persp != RV3D_PERSP) {
2764 if (vod->rv3d->persp == RV3D_CAMOB) {
2765 /* ignore rv3d->lpersp because dolly only makes sense in perspective mode */
2766 view3d_persp_switch_from_camera(vod->v3d, vod->rv3d, RV3D_PERSP);
2769 vod->rv3d->persp = RV3D_PERSP;
2771 ED_region_tag_redraw(vod->ar);
2774 viewops_data_create(C, op, event);
2777 /* if one or the other zoom position aren't set, set from event */
2778 if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
2779 RNA_int_set(op->ptr, "mx", event->x);
2780 RNA_int_set(op->ptr, "my", event->y);
2783 if (RNA_struct_property_is_set(op->ptr, "delta")) {
2784 viewdolly_exec(C, op);
2787 /* overwrite the mouse vector with the view direction (zoom into the center) */
2788 if ((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
2789 negate_v3_v3(vod->mousevec, vod->rv3d->viewinv[2]);
2790 normalize_v3(vod->mousevec);
2793 if (event->type == MOUSEZOOM) {
2794 /* Bypass Zoom invert flag for track pads (pass false always) */
2796 if (U.uiflag & USER_ZOOM_HORIZ) {
2797 vod->origx = vod->oldx = event->x;
2798 viewdolly_apply(vod, event->prevx, event->prevy, (U.uiflag & USER_ZOOM_INVERT) == 0);
2802 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
2803 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
2804 viewdolly_apply(vod, event->prevx, event->prevy, (U.uiflag & USER_ZOOM_INVERT) == 0);
2806 ED_view3d_depth_tag_update(vod->rv3d);
2808 viewops_data_free(C, op);
2809 return OPERATOR_FINISHED;
2812 /* add temp handler */
2813 WM_event_add_modal_handler(C, op);
2815 return OPERATOR_RUNNING_MODAL;
2818 return OPERATOR_FINISHED;
2821 static void viewdolly_cancel(bContext *C, wmOperator *op)
2823 viewops_data_free(C, op);
2826 void VIEW3D_OT_dolly(wmOperatorType *ot)
2829 ot->name = "Dolly View";
2830 ot->description = "Dolly in/out in the view";
2831 ot->idname = "VIEW3D_OT_dolly";
2834 ot->invoke = viewdolly_invoke;
2835 ot->exec = viewdolly_exec;
2836 ot->modal = viewdolly_modal;
2837 ot->poll = ED_operator_region_view3d_active;
2838 ot->cancel = viewdolly_cancel;
2841 ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR;
2843 RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
2844 RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
2845 RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
2848 static void view3d_from_minmax(bContext *C, View3D *v3d, ARegion *ar,
2849 const float min[3], const float max[3],
2850 bool ok_dist, const int smooth_viewtx)
2852 RegionView3D *rv3d = ar->regiondata;
2856 ED_view3d_smooth_view_force_finish(C, v3d, ar);
2862 sub_v3_v3v3(afm, max, min);
2863 size = max_fff(afm[0], afm[1], afm[2]);
2868 if (rv3d->is_persp) {
2869 if (rv3d->persp == RV3D_CAMOB && ED_view3d_camera_lock_check(v3d, rv3d)) {
2877 if (size < 0.0001f) {
2878 /* bounding box was a single point so do not zoom */
2882 /* adjust zoom so it looks nicer */
2888 new_dist = ED_view3d_radius_to_dist(v3d, ar, persp, true, (size / 2) * VIEW3D_MARGIN);
2889 if (rv3d->is_persp) {
2890 /* don't zoom closer than the near clipping plane */
2891 new_dist = max_ff(new_dist, v3d->near * 1.5f);
2896 mid_v3_v3v3(new_ofs, min, max);
2899 if (rv3d->persp == RV3D_CAMOB && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2900 rv3d->persp = RV3D_PERSP;
2901 ED_view3d_smooth_view(
2902 C, v3d, ar, smooth_viewtx,
2903 &(const V3D_SmoothParams) {
2904 .camera_old = v3d->camera, .ofs = new_ofs,
2905 .dist = ok_dist ? &new_dist : NULL});
2908 ED_view3d_smooth_view(
2909 C, v3d, ar, smooth_viewtx,
2910 &(const V3D_SmoothParams) {.ofs = new_ofs, .dist = ok_dist ? &new_dist : NULL});
2913 /* smooth view does viewlock RV3D_BOXVIEW copy */
2916 /* same as view3d_from_minmax but for all regions (except cameras) */
2917 static void view3d_from_minmax_multi(bContext *C, View3D *v3d,
2918 const float min[3], const float max[3],
2919 const bool ok_dist, const int smooth_viewtx)
2921 ScrArea *sa = CTX_wm_area(C);
2923 for (ar = sa->regionbase.first; ar; ar = ar->next) {
2924 if (ar->regiontype == RGN_TYPE_WINDOW) {
2925 RegionView3D *rv3d = ar->regiondata;
2926 /* when using all regions, don't jump out of camera view,
2927 * but _do_ allow locked cameras to be moved */
2928 if ((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d)) {
2929 view3d_from_minmax(C, v3d, ar, min, max, ok_dist, smooth_viewtx);
2935 static int view3d_all_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
2937 ARegion *ar = CTX_wm_region(C);
2938 View3D *v3d = CTX_wm_view3d(C);
2939 Scene *scene = CTX_data_scene(C);
2942 const bool use_all_regions = RNA_boolean_get(op->ptr, "use_all_regions");
2943 const bool skip_camera = (ED_view3d_camera_lock_check(v3d, ar->regiondata) ||
2944 /* any one of the regions may be locked */
2945 (use_all_regions && v3d->flag2 & V3D_LOCK_CAMERA));
2946 const bool center = RNA_boolean_get(op->ptr, "center");
2947 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
2949 float min[3], max[3];
2950 bool changed = false;
2953 /* in 2.4x this also move the cursor to (0, 0, 0) (with shift+c). */
2954 curs = ED_view3d_cursor3d_get(scene, v3d);
2960 INIT_MINMAX(min, max);
2963 for (base = scene->base.first; base; base = base->next) {
2964 if (BASE_VISIBLE(v3d, base)) {
2967 if (skip_camera && base->object == v3d->camera) {
2971 BKE_object_minmax(base->object, min, max, false);
2975 ED_region_tag_redraw(ar);
2976 /* TODO - should this be cancel?
2977 * I think no, because we always move the cursor, with or without
2978 * object, but in this case there is no change in the scene,
2979 * only the cursor so I choice a ED_region_tag like
2980 * view3d_smooth_view do for the center_cursor.
2983 return OPERATOR_FINISHED;
2986 if (use_all_regions) {
2987 view3d_from_minmax_multi(C, v3d, min, max, true, smooth_viewtx);
2990 view3d_from_minmax(C, v3d, ar, min, max, true, smooth_viewtx);
2993 return OPERATOR_FINISHED;
2997 void VIEW3D_OT_view_all(wmOperatorType *ot)
3002 ot->name = "View All";
3003 ot->description = "View all objects in scene";
3004 ot->idname = "VIEW3D_OT_view_all";
3007 ot->exec = view3d_all_exec;
3008 ot->poll = ED_operator_region_view3d_active;
3013 prop = RNA_def_boolean(ot->srna, "use_all_regions", 0, "All Regions", "View selected for all regions");
3014 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3015 RNA_def_boolean(ot->srna, "center", 0, "Center", "");
3018 /* like a localview without local!, was centerview() in 2.4x */
3019 static int viewselected_exec(bContext *C, wmOperator *op)
3021 ARegion *ar = CTX_wm_region(C);
3022 View3D *v3d = CTX_wm_view3d(C);
3023 Scene *scene = CTX_data_scene(C);
3024 bGPdata *gpd = CTX_data_gpencil_data(C);
3025 const bool is_gp_edit = ((gpd) && (gpd->flag & GP_DATA_STROKE_EDITMODE));
3027 Object *obedit = CTX_data_edit_object(C);
3028 float min[3], max[3];
3029 bool ok = false, ok_dist = true;
3030 const bool use_all_regions = RNA_boolean_get(op->ptr, "use_all_regions");
3031 const bool skip_camera = (ED_view3d_camera_lock_check(v3d, ar->regiondata) ||
3032 /* any one of the regions may be locked */
3033 (use_all_regions && v3d->flag2 & V3D_LOCK_CAMERA));
3034 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
3036 INIT_MINMAX(min, max);
3042 if (ob && (ob->mode & OB_MODE_WEIGHT_PAINT)) {
3043 /* hard-coded exception, we look for the one selected armature */
3044 /* this is weak code this way, we should make a generic active/selection callback interface once... */
3046 for (base = scene->base.first; base; base = base->next) {
3047 if (TESTBASELIB(v3d, base)) {
3048 if (base->object->type == OB_ARMATURE)
3049 if (base->object->mode & OB_MODE_POSE)
3059 CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
3061 /* we're only interested in selected points here... */
3062 if ((gps->flag & GP_STROKE_SELECT) && (gps->flag & GP_STROKE_3DSPACE)) {
3063 if (ED_gpencil_stroke_minmax(gps, true, min, max)) {
3071 ok = ED_view3d_minmax_verts(obedit, min, max); /* only selected */
3073 else if (ob && (ob->mode & OB_MODE_POSE)) {
3074 ok = BKE_pose_minmax(ob, min, max, true, true);
3076 else if (BKE_paint_select_face_test(ob)) {
3077 ok = paintface_minmax(ob, min, max);
3079 else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
3080 ok = PE_minmax(scene, min, max);
3082 else if (ob && (ob->mode & (OB_MODE_SCULPT | OB_MODE_TEXTURE_PAINT))) {
3083 BKE_paint_stroke_get_average(scene, ob, min);
3084 copy_v3_v3(max, min);
3086 ok_dist = 0; /* don't zoom */
3090 for (base = FIRSTBASE; base; base = base->next) {
3091 if (TESTBASE(v3d, base)) {
3093 if (skip_camera && base->object == v3d->camera) {
3097 /* account for duplis */
3098 if (BKE_object_minmax_dupli(scene, base->object, min, max, false) == 0)
3099 BKE_object_minmax(base->object, min, max, false); /* use if duplis not found */
3107 return OPERATOR_FINISHED;
3110 if (use_all_regions) {
3111 view3d_from_minmax_multi(C, v3d, min, max, ok_dist, smooth_viewtx);
3114 view3d_from_minmax(C, v3d, ar, min, max, ok_dist, smooth_viewtx);
3117 return OPERATOR_FINISHED;
3120 void VIEW3D_OT_view_selected(wmOperatorType *ot)
3125 ot->name = "View Selected";
3126 ot->description = "Move the view to the selection center";
3127 ot->idname = "VIEW3D_OT_view_selected";
3130 ot->exec = viewselected_exec;
3131 ot->poll = ED_operator_region_view3d_active;
3137 prop = RNA_def_boolean(ot->srna, "use_all_regions", 0, "All Regions", "View selected for all regions");
3138 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
3141 static int view_lock_clear_exec(bContext *C, wmOperator *UNUSED(op))
3143 View3D *v3d = CTX_wm_view3d(C);
3146 ED_view3D_lock_clear(v3d);
3148 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
3150 return OPERATOR_FINISHED;
3153 return OPERATOR_CANCELLED;
3157 void VIEW3D_OT_view_lock_clear(wmOperatorType *ot)
3161 ot->name = "View Lock Clear";
3162 ot->description = "Clear all view locking";
3163 ot->idname = "VIEW3D_OT_view_lock_clear";
3166 ot->exec = view_lock_clear_exec;
3167 ot->poll = ED_operator_region_view3d_active;
3173 static int view_lock_to_active_exec(bContext *C, wmOperator *UNUSED(op))
3175 View3D *v3d = CTX_wm_view3d(C);
3176 Object *obact = CTX_data_active_object(C);
3180 ED_view3D_lock_clear(v3d);
3182 v3d->ob_centre = obact; /* can be NULL */
3184 if (obact && obact->type == OB_ARMATURE) {
3185 if (obact->mode & OB_MODE_POSE) {
3186 bPoseChannel *pcham_act = BKE_pose_channel_active(obact);
3188 BLI_strncpy(v3d->ob_centre_bone, pcham_act->name, sizeof(v3d->ob_centre_bone));
3192 EditBone *ebone_act = ((bArmature *)obact->data)->act_edbone;