Cleanup: Split View3D navigation code into specific compilation units

The view3d_edit.c file is already getting big (5436 lines) and mixes
operators of different uses.

Splitting the code makes it easier to read and simplifies the
implementation of new features.

Differential Revision: https://developer.blender.org/D13976
This commit is contained in:
Germano Cavalcante 2022-01-31 13:37:04 -03:00 committed by Germano Cavalcante
parent ff9dc1986e
commit 1963ad52ce
18 changed files with 5089 additions and 4690 deletions

View File

@ -60,8 +60,17 @@ set(SRC
view3d_gizmo_tool_generic.c
view3d_header.c
view3d_iterators.c
view3d_navigate.c
view3d_navigate_dolly.c
view3d_navigate_fly.c
view3d_navigate_move.c
view3d_navigate_ndof.c
view3d_navigate_roll.c
view3d_navigate_rotate.c
view3d_navigate_smoothview.c
view3d_navigate_walk.c
view3d_navigate_zoom.c
view3d_navigate_zoom_border.c
view3d_ops.c
view3d_placement.c
view3d_project.c
@ -71,6 +80,7 @@ set(SRC
view3d_view.c
view3d_intern.h
view3d_navigate.h
)
set(LIB

View File

@ -89,6 +89,7 @@
#include "DEG_depsgraph_build.h"
#include "view3d_intern.h" /* own include */
#include "view3d_navigate.h"
/* ******************** manage regions ********************* */

File diff suppressed because it is too large Load Diff

View File

@ -32,6 +32,8 @@ struct ARegionType;
struct BoundBox;
struct Depsgraph;
struct Object;
struct Scene;
struct ViewContext;
struct ViewLayer;
struct bContext;
struct wmGizmoGroupType;
@ -47,84 +49,24 @@ void VIEW3D_OT_toggle_matcap_flip(struct wmOperatorType *ot);
void view3d_operatortypes(void);
/* view3d_edit.c */
void VIEW3D_OT_zoom(struct wmOperatorType *ot);
void VIEW3D_OT_dolly(struct wmOperatorType *ot);
void VIEW3D_OT_zoom_camera_1_to_1(struct wmOperatorType *ot);
void VIEW3D_OT_move(struct wmOperatorType *ot);
void VIEW3D_OT_rotate(struct wmOperatorType *ot);
#ifdef WITH_INPUT_NDOF
void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot);
void VIEW3D_OT_ndof_orbit_zoom(struct wmOperatorType *ot);
void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot);
void VIEW3D_OT_ndof_all(struct wmOperatorType *ot);
#endif /* WITH_INPUT_NDOF */
void VIEW3D_OT_view_all(struct wmOperatorType *ot);
void VIEW3D_OT_view_axis(struct wmOperatorType *ot);
void VIEW3D_OT_view_camera(struct wmOperatorType *ot);
void VIEW3D_OT_view_selected(struct wmOperatorType *ot);
void VIEW3D_OT_view_lock_clear(struct wmOperatorType *ot);
void VIEW3D_OT_view_lock_to_active(struct wmOperatorType *ot);
void VIEW3D_OT_view_center_cursor(struct wmOperatorType *ot);
void VIEW3D_OT_view_center_pick(struct wmOperatorType *ot);
void VIEW3D_OT_view_center_camera(struct wmOperatorType *ot);
void VIEW3D_OT_view_center_lock(struct wmOperatorType *ot);
void VIEW3D_OT_view_pan(struct wmOperatorType *ot);
void VIEW3D_OT_view_persportho(struct wmOperatorType *ot);
void VIEW3D_OT_navigate(struct wmOperatorType *ot);
void VIEW3D_OT_background_image_add(struct wmOperatorType *ot);
void VIEW3D_OT_background_image_remove(struct wmOperatorType *ot);
void VIEW3D_OT_drop_world(struct wmOperatorType *ot);
void VIEW3D_OT_view_orbit(struct wmOperatorType *ot);
void VIEW3D_OT_view_roll(struct wmOperatorType *ot);
void VIEW3D_OT_clip_border(struct wmOperatorType *ot);
void VIEW3D_OT_cursor3d(struct wmOperatorType *ot);
void VIEW3D_OT_render_border(struct wmOperatorType *ot);
void VIEW3D_OT_clear_render_border(struct wmOperatorType *ot);
void VIEW3D_OT_zoom_border(struct wmOperatorType *ot);
void VIEW3D_OT_toggle_shading(struct wmOperatorType *ot);
void VIEW3D_OT_toggle_xray(struct wmOperatorType *ot);
/**
* For home, center etc.
*/
void view3d_boxview_copy(struct ScrArea *area, struct ARegion *region);
/**
* Sync center/zoom view of region to others, for view transforms.
*/
void view3d_boxview_sync(struct ScrArea *area, struct ARegion *region);
void view3d_orbit_apply_dyn_ofs(float r_ofs[3],
const float ofs_old[3],
const float viewquat_old[4],
const float viewquat_new[4],
const float dyn_ofs[3]);
#ifdef WITH_INPUT_NDOF
struct wmNDOFMotionData;
/**
* Called from both fly mode and walk mode,
*/
void view3d_ndof_fly(const struct wmNDOFMotionData *ndof,
struct View3D *v3d,
struct RegionView3D *rv3d,
bool use_precision,
short protectflag,
bool *r_has_translate,
bool *r_has_rotate);
#endif /* WITH_INPUT_NDOF */
/* view3d_navigate_fly.c */
void view3d_keymap(struct wmKeyConfig *keyconf);
void VIEW3D_OT_fly(struct wmOperatorType *ot);
/* view3d_navigate_walk.c */
void VIEW3D_OT_walk(struct wmOperatorType *ot);
/* view3d_draw.c */
void view3d_main_region_draw(const struct bContext *C, struct ARegion *region);
/**
* Information drawn on top of the solid plates and composed data.
@ -134,16 +76,16 @@ void view3d_draw_region_info(const struct bContext *C, struct ARegion *region);
/* view3d_draw_legacy.c */
void ED_view3d_draw_select_loop(struct Depsgraph *depsgraph,
ViewContext *vc,
Scene *scene,
struct ViewContext *vc,
struct Scene *scene,
struct ViewLayer *view_layer,
View3D *v3d,
struct View3D *v3d,
struct ARegion *region,
bool use_obedit_skip,
bool use_nearest);
void ED_view3d_draw_depth_loop(struct Depsgraph *depsgraph,
Scene *scene,
struct Scene *scene,
struct ARegion *region,
View3D *v3d);
@ -161,57 +103,27 @@ void VIEW3D_OT_select_lasso(struct wmOperatorType *ot);
void VIEW3D_OT_select_menu(struct wmOperatorType *ot);
void VIEW3D_OT_bone_select_menu(struct wmOperatorType *ot);
/* view3d_view.c */
void VIEW3D_OT_smoothview(struct wmOperatorType *ot);
void VIEW3D_OT_camera_to_view(struct wmOperatorType *ot);
void VIEW3D_OT_camera_to_view_selected(struct wmOperatorType *ot);
void VIEW3D_OT_object_as_camera(struct wmOperatorType *ot);
void VIEW3D_OT_localview(struct wmOperatorType *ot);
void VIEW3D_OT_localview_remove_from(struct wmOperatorType *ot);
/* view3d_utils.c */
/**
* For home, center etc.
*/
void view3d_boxview_copy(struct ScrArea *area, struct ARegion *region);
/**
* Sync center/zoom view of region to others, for view transforms.
*/
void view3d_boxview_sync(struct ScrArea *area, struct ARegion *region);
bool ED_view3d_boundbox_clip_ex(const RegionView3D *rv3d,
const struct BoundBox *bb,
float obmat[4][4]);
bool ED_view3d_boundbox_clip(RegionView3D *rv3d, const struct BoundBox *bb);
/**
* Parameters for setting the new 3D Viewport state.
*
* Each of the struct members may be NULL to signify they aren't to be adjusted.
*/
typedef struct V3D_SmoothParams {
struct Object *camera_old, *camera;
const float *ofs, *quat, *dist, *lens;
/** Alternate rotation center, when set `ofs` must be NULL. */
const float *dyn_ofs;
} V3D_SmoothParams;
/**
* The arguments are the desired situation.
*/
void ED_view3d_smooth_view_ex(const struct Depsgraph *depsgraph,
struct wmWindowManager *wm,
struct wmWindow *win,
struct ScrArea *area,
struct View3D *v3d,
struct ARegion *region,
int smooth_viewtx,
const V3D_SmoothParams *sview);
void ED_view3d_smooth_view(struct bContext *C,
struct View3D *v3d,
struct ARegion *region,
int smooth_viewtx,
const V3D_SmoothParams *sview);
/**
* Apply the smooth-view immediately, use when we need to start a new view operation.
* (so we don't end up half-applying a view operation when pressing keys quickly).
*/
void ED_view3d_smooth_view_force_finish(struct bContext *C,
struct View3D *v3d,
struct ARegion *region);
/* view3d_view.c */
void VIEW3D_OT_camera_to_view(struct wmOperatorType *ot);
void VIEW3D_OT_camera_to_view_selected(struct wmOperatorType *ot);
void VIEW3D_OT_object_as_camera(struct wmOperatorType *ot);
void VIEW3D_OT_localview(struct wmOperatorType *ot);
void VIEW3D_OT_localview_remove_from(struct wmOperatorType *ot);
/**
* \param rect: optional for picking (can be NULL).
@ -240,12 +152,7 @@ void view3d_viewmatrix_set(struct Depsgraph *depsgraph,
/* Called in transform_ops.c, on each regeneration of key-maps. */
void fly_modal_keymap(struct wmKeyConfig *keyconf);
void walk_modal_keymap(struct wmKeyConfig *keyconf);
void viewrotate_modal_keymap(struct wmKeyConfig *keyconf);
void viewmove_modal_keymap(struct wmKeyConfig *keyconf);
void viewzoom_modal_keymap(struct wmKeyConfig *keyconf);
void viewdolly_modal_keymap(struct wmKeyConfig *keyconf);
/* view3d_placement.c */
void viewplace_modal_keymap(struct wmKeyConfig *keyconf);
/* view3d_buttons.c */
@ -260,7 +167,7 @@ void view3d_buttons_register(struct ARegionType *art);
* the view for first-person style navigation.
*/
struct View3DCameraControl *ED_view3d_cameracontrol_acquire(struct Depsgraph *depsgraph,
Scene *scene,
struct Scene *scene,
View3D *v3d,
RegionView3D *rv3d);
/**

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,282 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2008 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup spview3d
*/
#pragma once
/**
* Size of the sphere being dragged for trackball rotation within the view bounds.
* also affects speed (smaller is faster).
*/
#define TRACKBALLSIZE (1.1f)
struct ARegion;
struct bContext;
struct Depsgraph;
struct Dial;
struct Main;
struct rcti;
struct RegionView3D;
struct Scene;
struct ScrArea;
struct View3D;
struct wmEvent;
struct wmOperator;
enum eV3D_OpPropFlag {
V3D_OP_PROP_MOUSE_CO = (1 << 0),
V3D_OP_PROP_DELTA = (1 << 1),
V3D_OP_PROP_USE_ALL_REGIONS = (1 << 2),
V3D_OP_PROP_USE_MOUSE_INIT = (1 << 3),
};
enum {
VIEW_PASS = 0,
VIEW_APPLY,
VIEW_CONFIRM,
};
/* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
enum {
VIEW_MODAL_CONFIRM = 1, /* used for all view operations */
VIEWROT_MODAL_AXIS_SNAP_ENABLE = 2,
VIEWROT_MODAL_AXIS_SNAP_DISABLE = 3,
VIEWROT_MODAL_SWITCH_ZOOM = 4,
VIEWROT_MODAL_SWITCH_MOVE = 5,
VIEWROT_MODAL_SWITCH_ROTATE = 6,
};
enum eViewOpsFlag {
/** When enabled, rotate around the selection. */
VIEWOPS_FLAG_ORBIT_SELECT = (1 << 0),
/** When enabled, use the depth under the cursor for navigation. */
VIEWOPS_FLAG_DEPTH_NAVIGATE = (1 << 1),
/**
* When enabled run #ED_view3d_persp_ensure this may switch out of camera view
* when orbiting or switch from orthographic to perspective when auto-perspective is enabled.
* Some operations don't require this (view zoom/pan or NDOF where subtle rotation is common
* so we don't want it to trigger auto-perspective). */
VIEWOPS_FLAG_PERSP_ENSURE = (1 << 2),
/** When set, ignore any options that depend on initial cursor location. */
VIEWOPS_FLAG_USE_MOUSE_INIT = (1 << 3),
};
/** Generic View Operator Custom-Data */
typedef struct ViewOpsData {
/** Context pointers (assigned by #viewops_data_alloc). */
struct Main *bmain;
struct Scene *scene;
struct ScrArea *area;
struct ARegion *region;
struct View3D *v3d;
struct RegionView3D *rv3d;
struct Depsgraph *depsgraph;
/** Needed for continuous zoom. */
struct wmTimer *timer;
/** Viewport state on initialization, don't change afterwards. */
struct {
float dist;
float camzoom;
float quat[4];
/** #wmEvent.xy. */
int event_xy[2];
/** Offset to use when #VIEWOPS_FLAG_USE_MOUSE_INIT is not set.
* so we can simulate pressing in the middle of the screen. */
int event_xy_offset[2];
/** #wmEvent.type that triggered the operator. */
int event_type;
float ofs[3];
/** Initial distance to 'ofs'. */
float zfac;
/** Trackball rotation only. */
float trackvec[3];
/** Dolly only. */
float mousevec[3];
/**
* #RegionView3D.persp set after auto-perspective is applied.
* If we want the value before running the operator, add a separate member.
*/
char persp;
/** Used for roll */
struct Dial *dial;
} init;
/** Previous state (previous modal event handled). */
struct {
int event_xy[2];
/** For operators that use time-steps (continuous zoom). */
double time;
} prev;
/** Current state. */
struct {
/** Working copy of #RegionView3D.viewquat, needed for rotation calculation
* so we can apply snap to the 3D Viewport while keeping the unsnapped rotation
* here to use when snap is disabled and for continued calculation. */
float viewquat[4];
} curr;
float reverse;
bool axis_snap; /* view rotate only */
/** Use for orbit selection and auto-dist. */
float dyn_ofs[3];
bool use_dyn_ofs;
} ViewOpsData;
/* view3d_navigate.c */
bool view3d_zoom_or_dolly_poll(struct bContext *C);
enum eViewOpsFlag viewops_flag_from_prefs(void);
void calctrackballvec(const struct rcti *rect, const int event_xy[2], float r_dir[3]);
void viewmove_apply(ViewOpsData *vod, int x, int y);
void view3d_orbit_apply_dyn_ofs(float r_ofs[3],
const float ofs_old[3],
const float viewquat_old[4],
const float viewquat_new[4],
const float dyn_ofs[3]);
void viewrotate_apply_dyn_ofs(ViewOpsData *vod, const float viewquat_new[4]);
bool view3d_orbit_calc_center(struct bContext *C, float r_dyn_ofs[3]);
void view3d_operator_properties_common(struct wmOperatorType *ot, const enum eV3D_OpPropFlag flag);
/**
* Allocate and fill in context pointers for #ViewOpsData
*/
void viewops_data_alloc(struct bContext *C, struct wmOperator *op);
void viewops_data_free(struct bContext *C, struct wmOperator *op);
/**
* Calculate the values for #ViewOpsData
*/
void viewops_data_create(struct bContext *C,
struct wmOperator *op,
const struct wmEvent *event,
enum eViewOpsFlag viewops_flag);
void VIEW3D_OT_view_all(struct wmOperatorType *ot);
void VIEW3D_OT_view_selected(struct wmOperatorType *ot);
void VIEW3D_OT_view_center_cursor(struct wmOperatorType *ot);
void VIEW3D_OT_view_center_pick(struct wmOperatorType *ot);
void VIEW3D_OT_view_axis(struct wmOperatorType *ot);
void VIEW3D_OT_view_camera(struct wmOperatorType *ot);
void VIEW3D_OT_view_orbit(struct wmOperatorType *ot);
void VIEW3D_OT_view_pan(struct wmOperatorType *ot);
/* view3d_navigate_dolly.c */
void viewdolly_modal_keymap(struct wmKeyConfig *keyconf);
void VIEW3D_OT_dolly(struct wmOperatorType *ot);
/* view3d_navigate_fly.c */
void fly_modal_keymap(struct wmKeyConfig *keyconf);
void view3d_keymap(struct wmKeyConfig *keyconf);
void VIEW3D_OT_fly(struct wmOperatorType *ot);
/* view3d_navigate_move.c */
void viewmove_modal_keymap(struct wmKeyConfig *keyconf);
void VIEW3D_OT_move(struct wmOperatorType *ot);
/* view3d_navigate_ndof.c */
#ifdef WITH_INPUT_NDOF
struct wmNDOFMotionData;
/**
* Called from both fly mode and walk mode,
*/
void view3d_ndof_fly(const struct wmNDOFMotionData *ndof,
struct View3D *v3d,
struct RegionView3D *rv3d,
bool use_precision,
short protectflag,
bool *r_has_translate,
bool *r_has_rotate);
void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot);
void VIEW3D_OT_ndof_orbit_zoom(struct wmOperatorType *ot);
void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot);
void VIEW3D_OT_ndof_all(struct wmOperatorType *ot);
#endif /* WITH_INPUT_NDOF */
/* view3d_navigate_roll.c */
void VIEW3D_OT_view_roll(struct wmOperatorType *ot);
/* view3d_navigate_rotate.c */
void viewrotate_modal_keymap(struct wmKeyConfig *keyconf);
void VIEW3D_OT_rotate(struct wmOperatorType *ot);
/* view3d_navigate_smoothview.c */
/**
* Parameters for setting the new 3D Viewport state.
*
* Each of the struct members may be NULL to signify they aren't to be adjusted.
*/
typedef struct V3D_SmoothParams {
struct Object *camera_old, *camera;
const float *ofs, *quat, *dist, *lens;
/** Alternate rotation center, when set `ofs` must be NULL. */
const float *dyn_ofs;
} V3D_SmoothParams;
/**
* The arguments are the desired situation.
*/
void ED_view3d_smooth_view_ex(const struct Depsgraph *depsgraph,
struct wmWindowManager *wm,
struct wmWindow *win,
struct ScrArea *area,
struct View3D *v3d,
struct ARegion *region,
int smooth_viewtx,
const V3D_SmoothParams *sview);
void ED_view3d_smooth_view(struct bContext *C,
struct View3D *v3d,
struct ARegion *region,
int smooth_viewtx,
const V3D_SmoothParams *sview);
/**
* Apply the smooth-view immediately, use when we need to start a new view operation.
* (so we don't end up half-applying a view operation when pressing keys quickly).
*/
void ED_view3d_smooth_view_force_finish(struct bContext *C,
struct View3D *v3d,
struct ARegion *region);
void VIEW3D_OT_smoothview(struct wmOperatorType *ot);
/* view3d_navigate_walk.c */
void walk_modal_keymap(struct wmKeyConfig *keyconf);
void VIEW3D_OT_walk(struct wmOperatorType *ot);
/* view3d_navigate_zoom.c */
void viewzoom_modal_keymap(struct wmKeyConfig *keyconf);
void VIEW3D_OT_zoom(struct wmOperatorType *ot);
/* view3d_navigate_zoom_border.c */
void VIEW3D_OT_zoom_border(struct wmOperatorType *ot);

View File

@ -0,0 +1,343 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spview3d
*/
#include "BLI_math.h"
#include "BKE_context.h"
#include "BKE_report.h"
#include "DEG_depsgraph.h"
#include "WM_api.h"
#include "RNA_access.h"
#include "ED_screen.h"
#include "view3d_intern.h"
#include "view3d_navigate.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name View Dolly Operator
*
* Like zoom but translates the view offset along the view direction
* which avoids #RegionView3D.dist approaching zero.
* \{ */
/* This is an exact copy of #viewzoom_modal_keymap. */
void viewdolly_modal_keymap(wmKeyConfig *keyconf)
{
static const EnumPropertyItem modal_items[] = {
{VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
{VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
{VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
{0, NULL, 0, NULL, NULL},
};
wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "View3D Dolly Modal");
/* this function is called for each spacetype, only needs to add map once */
if (keymap && keymap->modal_items) {
return;
}
keymap = WM_modalkeymap_ensure(keyconf, "View3D Dolly Modal", modal_items);
/* disabled mode switching for now, can re-implement better, later on */
#if 0
WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
#endif
/* assign map to operators */
WM_modalkeymap_assign(keymap, "VIEW3D_OT_dolly");
}
static bool viewdolly_offset_lock_check(bContext *C, wmOperator *op)
{
View3D *v3d = CTX_wm_view3d(C);
RegionView3D *rv3d = CTX_wm_region_view3d(C);
if (ED_view3d_offset_lock_check(v3d, rv3d)) {
BKE_report(op->reports, RPT_WARNING, "Cannot dolly when the view offset is locked");
return true;
}
return false;
}
static void view_dolly_to_vector_3d(ARegion *region,
const float orig_ofs[3],
const float dvec[3],
float dfac)
{
RegionView3D *rv3d = region->regiondata;
madd_v3_v3v3fl(rv3d->ofs, orig_ofs, dvec, -(1.0f - dfac));
}
static void viewdolly_apply(ViewOpsData *vod, const int xy[2], const bool zoom_invert)
{
float zfac = 1.0;
{
float len1, len2;
if (U.uiflag & USER_ZOOM_HORIZ) {
len1 = (vod->region->winrct.xmax - xy[0]) + 5;
len2 = (vod->region->winrct.xmax - vod->init.event_xy[0]) + 5;
}
else {
len1 = (vod->region->winrct.ymax - xy[1]) + 5;
len2 = (vod->region->winrct.ymax - vod->init.event_xy[1]) + 5;
}
if (zoom_invert) {
SWAP(float, len1, len2);
}
zfac = 1.0f + ((len1 - len2) * 0.01f * vod->rv3d->dist);
}
if (zfac != 1.0f) {
view_dolly_to_vector_3d(vod->region, vod->init.ofs, vod->init.mousevec, zfac);
}
if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_BOXVIEW) {
view3d_boxview_sync(vod->area, vod->region);
}
ED_view3d_camera_lock_sync(vod->depsgraph, vod->v3d, vod->rv3d);
ED_region_tag_redraw(vod->region);
}
static int viewdolly_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod = op->customdata;
short event_code = VIEW_PASS;
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_MOVE:
WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_ROTATE:
WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
}
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
viewdolly_apply(vod, event->xy, (U.uiflag & USER_ZOOM_INVERT) != 0);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
}
if (ret & OPERATOR_FINISHED) {
viewops_data_free(C, op);
}
return ret;
}
static int viewdolly_exec(bContext *C, wmOperator *op)
{
View3D *v3d;
RegionView3D *rv3d;
ScrArea *area;
ARegion *region;
float mousevec[3];
const int delta = RNA_int_get(op->ptr, "delta");
if (op->customdata) {
ViewOpsData *vod = op->customdata;
area = vod->area;
region = vod->region;
copy_v3_v3(mousevec, vod->init.mousevec);
}
else {
area = CTX_wm_area(C);
region = CTX_wm_region(C);
negate_v3_v3(mousevec, ((RegionView3D *)region->regiondata)->viewinv[2]);
normalize_v3(mousevec);
}
v3d = area->spacedata.first;
rv3d = region->regiondata;
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
/* overwrite the mouse vector with the view direction (zoom into the center) */
if ((use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)) == 0) {
normalize_v3_v3(mousevec, rv3d->viewinv[2]);
negate_v3(mousevec);
}
view_dolly_to_vector_3d(region, rv3d->ofs, mousevec, delta < 0 ? 1.8f : 0.2f);
if (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW) {
view3d_boxview_sync(area, region);
}
ED_view3d_camera_lock_sync(CTX_data_ensure_evaluated_depsgraph(C), v3d, rv3d);
ED_region_tag_redraw(region);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
/* copied from viewzoom_invoke(), changes here may apply there */
static int viewdolly_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod;
if (viewdolly_offset_lock_check(C, op)) {
return OPERATOR_CANCELLED;
}
/* makes op->customdata */
viewops_data_alloc(C, op);
vod = op->customdata;
/* poll should check but in some cases fails, see poll func for details */
if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_LOCK_ROTATION) {
viewops_data_free(C, op);
return OPERATOR_PASS_THROUGH;
}
ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->region);
/* needs to run before 'viewops_data_create' so the backup 'rv3d->ofs' is correct */
/* switch from camera view when: */
if (vod->rv3d->persp != RV3D_PERSP) {
if (vod->rv3d->persp == RV3D_CAMOB) {
/* ignore rv3d->lpersp because dolly only makes sense in perspective mode */
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
ED_view3d_persp_switch_from_camera(depsgraph, vod->v3d, vod->rv3d, RV3D_PERSP);
}
else {
vod->rv3d->persp = RV3D_PERSP;
}
ED_region_tag_redraw(vod->region);
}
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
viewops_data_create(C,
op,
event,
(viewops_flag_from_prefs() & ~VIEWOPS_FLAG_ORBIT_SELECT) |
(use_cursor_init ? VIEWOPS_FLAG_USE_MOUSE_INIT : 0));
/* if one or the other zoom position aren't set, set from event */
if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
RNA_int_set(op->ptr, "mx", event->xy[0]);
RNA_int_set(op->ptr, "my", event->xy[1]);
}
if (RNA_struct_property_is_set(op->ptr, "delta")) {
viewdolly_exec(C, op);
}
else {
/* overwrite the mouse vector with the view direction (zoom into the center) */
if ((use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)) == 0) {
negate_v3_v3(vod->init.mousevec, vod->rv3d->viewinv[2]);
normalize_v3(vod->init.mousevec);
}
if (event->type == MOUSEZOOM) {
/* Bypass Zoom invert flag for track pads (pass false always) */
if (U.uiflag & USER_ZOOM_HORIZ) {
vod->init.event_xy[0] = vod->prev.event_xy[0] = event->xy[0];
}
else {
/* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
vod->init.event_xy[1] = vod->prev.event_xy[1] = vod->init.event_xy[1] + event->xy[0] -
event->prev_xy[0];
}
viewdolly_apply(vod, event->prev_xy, (U.uiflag & USER_ZOOM_INVERT) == 0);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
/* add temp handler */
WM_event_add_modal_handler(C, op);
return OPERATOR_RUNNING_MODAL;
}
return OPERATOR_FINISHED;
}
static void viewdolly_cancel(bContext *C, wmOperator *op)
{
viewops_data_free(C, op);
}
void VIEW3D_OT_dolly(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Dolly View";
ot->description = "Dolly in/out in the view";
ot->idname = "VIEW3D_OT_dolly";
/* api callbacks */
ot->invoke = viewdolly_invoke;
ot->exec = viewdolly_exec;
ot->modal = viewdolly_modal;
ot->poll = ED_operator_region_view3d_active;
ot->cancel = viewdolly_cancel;
/* flags */
ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR_XY;
/* properties */
view3d_operator_properties_common(
ot, V3D_OP_PROP_DELTA | V3D_OP_PROP_MOUSE_CO | V3D_OP_PROP_USE_MOUSE_INIT);
}
/** \} */

View File

@ -58,6 +58,7 @@
#include "DEG_depsgraph.h"
#include "view3d_intern.h" /* own include */
#include "view3d_navigate.h"
/* -------------------------------------------------------------------- */
/** \name Modal Key-map
@ -1102,9 +1103,9 @@ static int fly_modal(bContext *C, wmOperator *op, const wmEvent *event)
}
else
#endif /* WITH_INPUT_NDOF */
if (event->type == TIMER && event->customdata == fly->timer) {
flyApply(C, fly, false);
}
if (event->type == TIMER && event->customdata == fly->timer) {
flyApply(C, fly, false);
}
do_draw |= fly->redraw;

View File

@ -0,0 +1,192 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spview3d
*/
#include "BKE_context.h"
#include "WM_api.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "ED_screen.h"
#include "view3d_intern.h"
#include "view3d_navigate.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name View Move (Pan) Operator
* \{ */
/* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
void viewmove_modal_keymap(wmKeyConfig *keyconf)
{
static const EnumPropertyItem modal_items[] = {
{VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
{VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
{VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
{0, NULL, 0, NULL, NULL},
};
wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "View3D Move Modal");
/* this function is called for each spacetype, only needs to add map once */
if (keymap && keymap->modal_items) {
return;
}
keymap = WM_modalkeymap_ensure(keyconf, "View3D Move Modal", modal_items);
/* items for modal map */
WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
WM_modalkeymap_add_item(keymap, EVT_ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
/* disabled mode switching for now, can re-implement better, later on */
#if 0
WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
WM_modalkeymap_add_item(
keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
#endif
/* assign map to operators */
WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
}
static int viewmove_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod = op->customdata;
short event_code = VIEW_PASS;
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_ZOOM:
WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_ROTATE:
WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
}
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
viewmove_apply(vod, event->xy[0], event->xy[1]);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
}
if (ret & OPERATOR_FINISHED) {
viewops_data_free(C, op);
}
return ret;
}
static int viewmove_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod;
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
/* makes op->customdata */
viewops_data_alloc(C, op);
vod = op->customdata;
if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_LOCK_LOCATION) {
viewops_data_free(C, op);
return OPERATOR_PASS_THROUGH;
}
viewops_data_create(C,
op,
event,
(viewops_flag_from_prefs() & ~VIEWOPS_FLAG_ORBIT_SELECT) |
(use_cursor_init ? VIEWOPS_FLAG_USE_MOUSE_INIT : 0));
ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->region);
if (event->type == MOUSEPAN) {
/* invert it, trackpad scroll follows same principle as 2d windows this way */
viewmove_apply(
vod, 2 * event->xy[0] - event->prev_xy[0], 2 * event->xy[1] - event->prev_xy[1]);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
/* add temp handler */
WM_event_add_modal_handler(C, op);
return OPERATOR_RUNNING_MODAL;
}
static void viewmove_cancel(bContext *C, wmOperator *op)
{
viewops_data_free(C, op);
}
void VIEW3D_OT_move(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Pan View";
ot->description = "Move the view";
ot->idname = "VIEW3D_OT_move";
/* api callbacks */
ot->invoke = viewmove_invoke;
ot->modal = viewmove_modal;
ot->poll = ED_operator_region_view3d_active;
ot->cancel = viewmove_cancel;
/* flags */
ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR_XY;
/* properties */
view3d_operator_properties_common(ot, V3D_OP_PROP_USE_MOUSE_INIT);
}
/** \} */

View File

@ -0,0 +1,662 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spview3d
*/
#include "BLI_math.h"
#include "BKE_context.h"
#include "DEG_depsgraph.h"
#include "WM_api.h"
#include "ED_screen.h"
#include "view3d_intern.h"
#include "view3d_navigate.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name NDOF Utility Functions
* \{ */
#ifdef WITH_INPUT_NDOF
enum {
HAS_TRANSLATE = (1 << 0),
HAS_ROTATE = (1 << 0),
};
static bool ndof_has_translate(const wmNDOFMotionData *ndof,
const View3D *v3d,
const RegionView3D *rv3d)
{
return !is_zero_v3(ndof->tvec) && (!ED_view3d_offset_lock_check(v3d, rv3d));
}
static bool ndof_has_rotate(const wmNDOFMotionData *ndof, const RegionView3D *rv3d)
{
return !is_zero_v3(ndof->rvec) && ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0);
}
/**
* \param depth_pt: A point to calculate the depth (in perspective mode)
*/
static float view3d_ndof_pan_speed_calc_ex(RegionView3D *rv3d, const float depth_pt[3])
{
float speed = rv3d->pixsize * NDOF_PIXELS_PER_SECOND;
if (rv3d->is_persp) {
speed *= ED_view3d_calc_zfac(rv3d, depth_pt, NULL);
}
return speed;
}
static float view3d_ndof_pan_speed_calc_from_dist(RegionView3D *rv3d, const float dist)
{
float viewinv[4];
float tvec[3];
BLI_assert(dist >= 0.0f);
copy_v3_fl3(tvec, 0.0f, 0.0f, dist);
/* rv3d->viewinv isn't always valid */
# if 0
mul_mat3_m4_v3(rv3d->viewinv, tvec);
# else
invert_qt_qt_normalized(viewinv, rv3d->viewquat);
mul_qt_v3(viewinv, tvec);
# endif
return view3d_ndof_pan_speed_calc_ex(rv3d, tvec);
}
static float view3d_ndof_pan_speed_calc(RegionView3D *rv3d)
{
float tvec[3];
negate_v3_v3(tvec, rv3d->ofs);
return view3d_ndof_pan_speed_calc_ex(rv3d, tvec);
}
/**
* Zoom and pan in the same function since sometimes zoom is interpreted as dolly (pan forward).
*
* \param has_zoom: zoom, otherwise dolly,
* often `!rv3d->is_persp` since it doesn't make sense to dolly in ortho.
*/
static void view3d_ndof_pan_zoom(const struct wmNDOFMotionData *ndof,
ScrArea *area,
ARegion *region,
const bool has_translate,
const bool has_zoom)
{
RegionView3D *rv3d = region->regiondata;
float view_inv[4];
float pan_vec[3];
if (has_translate == false && has_zoom == false) {
return;
}
WM_event_ndof_pan_get(ndof, pan_vec, false);
if (has_zoom) {
/* zoom with Z */
/* Zoom!
* velocity should be proportional to the linear velocity attained by rotational motion
* of same strength [got that?] proportional to `arclength = radius * angle`.
*/
pan_vec[2] = 0.0f;
/* "zoom in" or "translate"? depends on zoom mode in user settings? */
if (ndof->tvec[2]) {
float zoom_distance = rv3d->dist * ndof->dt * ndof->tvec[2];
if (U.ndof_flag & NDOF_ZOOM_INVERT) {
zoom_distance = -zoom_distance;
}
rv3d->dist += zoom_distance;
}
}
else {
/* dolly with Z */
/* all callers must check */
if (has_translate) {
BLI_assert(ED_view3d_offset_lock_check((View3D *)area->spacedata.first, rv3d) == false);
}
}
if (has_translate) {
const float speed = view3d_ndof_pan_speed_calc(rv3d);
mul_v3_fl(pan_vec, speed * ndof->dt);
/* transform motion from view to world coordinates */
invert_qt_qt_normalized(view_inv, rv3d->viewquat);
mul_qt_v3(view_inv, pan_vec);
/* move center of view opposite of hand motion (this is camera mode, not object mode) */
sub_v3_v3(rv3d->ofs, pan_vec);
if (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW) {
view3d_boxview_sync(area, region);
}
}
}
static void view3d_ndof_orbit(const struct wmNDOFMotionData *ndof,
ScrArea *area,
ARegion *region,
ViewOpsData *vod,
const bool apply_dyn_ofs)
{
View3D *v3d = area->spacedata.first;
RegionView3D *rv3d = region->regiondata;
float view_inv[4];
BLI_assert((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0);
ED_view3d_persp_ensure(vod->depsgraph, v3d, region);
rv3d->view = RV3D_VIEW_USER;
invert_qt_qt_normalized(view_inv, rv3d->viewquat);
if (U.ndof_flag & NDOF_TURNTABLE) {
float rot[3];
/* Turntable view code adapted for 3D mouse use. */
float angle, quat[4];
float xvec[3] = {1, 0, 0};
/* only use XY, ignore Z */
WM_event_ndof_rotate_get(ndof, rot);
/* Determine the direction of the x vector (for rotating up and down) */
mul_qt_v3(view_inv, xvec);
/* Perform the up/down rotation */
angle = ndof->dt * rot[0];
axis_angle_to_quat(quat, xvec, angle);
mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
/* Perform the orbital rotation */
angle = ndof->dt * rot[1];
/* update the onscreen doo-dad */
rv3d->rot_angle = angle;
rv3d->rot_axis[0] = 0;
rv3d->rot_axis[1] = 0;
rv3d->rot_axis[2] = 1;
axis_angle_to_quat_single(quat, 'Z', angle);
mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
}
else {
float quat[4];
float axis[3];
float angle = WM_event_ndof_to_axis_angle(ndof, axis);
/* transform rotation axis from view to world coordinates */
mul_qt_v3(view_inv, axis);
/* update the onscreen doo-dad */
rv3d->rot_angle = angle;
copy_v3_v3(rv3d->rot_axis, axis);
axis_angle_to_quat(quat, axis, angle);
/* apply rotation */
mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, quat);
}
if (apply_dyn_ofs) {
viewrotate_apply_dyn_ofs(vod, rv3d->viewquat);
}
}
void view3d_ndof_fly(const wmNDOFMotionData *ndof,
View3D *v3d,
RegionView3D *rv3d,
const bool use_precision,
const short protectflag,
bool *r_has_translate,
bool *r_has_rotate)
{
bool has_translate = ndof_has_translate(ndof, v3d, rv3d);
bool has_rotate = ndof_has_rotate(ndof, rv3d);
float view_inv[4];
invert_qt_qt_normalized(view_inv, rv3d->viewquat);
rv3d->rot_angle = 0.0f; /* disable onscreen rotation doo-dad */
if (has_translate) {
/* ignore real 'dist' since fly has its own speed settings,
* also its overwritten at this point. */
float speed = view3d_ndof_pan_speed_calc_from_dist(rv3d, 1.0f);
float trans[3], trans_orig_y;
if (use_precision) {
speed *= 0.2f;
}
WM_event_ndof_pan_get(ndof, trans, false);
mul_v3_fl(trans, speed * ndof->dt);
trans_orig_y = trans[1];
if (U.ndof_flag & NDOF_FLY_HELICOPTER) {
trans[1] = 0.0f;
}
/* transform motion from view to world coordinates */
mul_qt_v3(view_inv, trans);
if (U.ndof_flag & NDOF_FLY_HELICOPTER) {
/* replace world z component with device y (yes it makes sense) */
trans[2] = trans_orig_y;
}
if (rv3d->persp == RV3D_CAMOB) {
/* respect camera position locks */
if (protectflag & OB_LOCK_LOCX) {
trans[0] = 0.0f;
}
if (protectflag & OB_LOCK_LOCY) {
trans[1] = 0.0f;
}
if (protectflag & OB_LOCK_LOCZ) {
trans[2] = 0.0f;
}
}
if (!is_zero_v3(trans)) {
/* move center of view opposite of hand motion
* (this is camera mode, not object mode) */
sub_v3_v3(rv3d->ofs, trans);
has_translate = true;
}
else {
has_translate = false;
}
}
if (has_rotate) {
const float turn_sensitivity = 1.0f;
float rotation[4];
float axis[3];
float angle = turn_sensitivity * WM_event_ndof_to_axis_angle(ndof, axis);
if (fabsf(angle) > 0.0001f) {
has_rotate = true;
if (use_precision) {
angle *= 0.2f;
}
/* transform rotation axis from view to world coordinates */
mul_qt_v3(view_inv, axis);
/* apply rotation to view */
axis_angle_to_quat(rotation, axis, angle);
mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
if (U.ndof_flag & NDOF_LOCK_HORIZON) {
/* force an upright viewpoint
* TODO: make this less... sudden */
float view_horizon[3] = {1.0f, 0.0f, 0.0f}; /* view +x */
float view_direction[3] = {0.0f, 0.0f, -1.0f}; /* view -z (into screen) */
/* find new inverse since viewquat has changed */
invert_qt_qt_normalized(view_inv, rv3d->viewquat);
/* could apply reverse rotation to existing view_inv to save a few cycles */
/* transform view vectors to world coordinates */
mul_qt_v3(view_inv, view_horizon);
mul_qt_v3(view_inv, view_direction);
/* find difference between view & world horizons
* true horizon lives in world xy plane, so look only at difference in z */
angle = -asinf(view_horizon[2]);
/* rotate view so view horizon = world horizon */
axis_angle_to_quat(rotation, view_direction, angle);
mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
}
rv3d->view = RV3D_VIEW_USER;
}
else {
has_rotate = false;
}
}
*r_has_translate = has_translate;
*r_has_rotate = has_rotate;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name NDOF Orbit/Translate Operator
* \{ */
static int ndof_orbit_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
if (event->type != NDOF_MOTION) {
return OPERATOR_CANCELLED;
}
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
ViewOpsData *vod;
View3D *v3d;
RegionView3D *rv3d;
char xform_flag = 0;
const wmNDOFMotionData *ndof = event->customdata;
viewops_data_alloc(C, op);
viewops_data_create(C, op, event, (viewops_flag_from_prefs() & ~VIEWOPS_FLAG_DEPTH_NAVIGATE));
vod = op->customdata;
ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->region);
v3d = vod->v3d;
rv3d = vod->rv3d;
/* off by default, until changed later this function */
rv3d->rot_angle = 0.0f;
ED_view3d_camera_lock_init_ex(depsgraph, v3d, rv3d, false);
if (ndof->progress != P_FINISHING) {
const bool has_rotation = ndof_has_rotate(ndof, rv3d);
/* if we can't rotate, fallback to translate (locked axis views) */
const bool has_translate = ndof_has_translate(ndof, v3d, rv3d) &&
(RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION);
const bool has_zoom = (ndof->tvec[2] != 0.0f) && !rv3d->is_persp;
if (has_translate || has_zoom) {
view3d_ndof_pan_zoom(ndof, vod->area, vod->region, has_translate, has_zoom);
xform_flag |= HAS_TRANSLATE;
}
if (has_rotation) {
view3d_ndof_orbit(ndof, vod->area, vod->region, vod, true);
xform_flag |= HAS_ROTATE;
}
}
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
if (xform_flag) {
ED_view3d_camera_lock_autokey(
v3d, rv3d, C, xform_flag & HAS_ROTATE, xform_flag & HAS_TRANSLATE);
}
ED_region_tag_redraw(vod->region);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot)
{
/* identifiers */
ot->name = "NDOF Orbit View";
ot->description = "Orbit the view using the 3D mouse";
ot->idname = "VIEW3D_OT_ndof_orbit";
/* api callbacks */
ot->invoke = ndof_orbit_invoke;
ot->poll = ED_operator_view3d_active;
/* flags */
ot->flag = 0;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name NDOF Orbit/Zoom Operator
* \{ */
static int ndof_orbit_zoom_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
if (event->type != NDOF_MOTION) {
return OPERATOR_CANCELLED;
}
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
ViewOpsData *vod;
View3D *v3d;
RegionView3D *rv3d;
char xform_flag = 0;
const wmNDOFMotionData *ndof = event->customdata;
viewops_data_alloc(C, op);
viewops_data_create(C, op, event, (viewops_flag_from_prefs() & ~VIEWOPS_FLAG_DEPTH_NAVIGATE));
vod = op->customdata;
ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->region);
v3d = vod->v3d;
rv3d = vod->rv3d;
/* off by default, until changed later this function */
rv3d->rot_angle = 0.0f;
ED_view3d_camera_lock_init_ex(depsgraph, v3d, rv3d, false);
if (ndof->progress == P_FINISHING) {
/* pass */
}
else if ((rv3d->persp == RV3D_ORTHO) && RV3D_VIEW_IS_AXIS(rv3d->view)) {
/* if we can't rotate, fallback to translate (locked axis views) */
const bool has_translate = ndof_has_translate(ndof, v3d, rv3d);
const bool has_zoom = (ndof->tvec[2] != 0.0f) && ED_view3d_offset_lock_check(v3d, rv3d);
if (has_translate || has_zoom) {
view3d_ndof_pan_zoom(ndof, vod->area, vod->region, has_translate, true);
xform_flag |= HAS_TRANSLATE;
}
}
else {
/* NOTE: based on feedback from T67579, users want to have pan and orbit enabled at once.
* It's arguable that orbit shouldn't pan (since we have a pan only operator),
* so if there are users who like to separate orbit/pan operations - it can be a preference. */
const bool is_orbit_around_pivot = (U.ndof_flag & NDOF_MODE_ORBIT) ||
ED_view3d_offset_lock_check(v3d, rv3d);
const bool has_rotation = ndof_has_rotate(ndof, rv3d);
bool has_translate, has_zoom;
if (is_orbit_around_pivot) {
/* Orbit preference or forced lock (Z zooms). */
has_translate = !is_zero_v2(ndof->tvec) && ndof_has_translate(ndof, v3d, rv3d);
has_zoom = (ndof->tvec[2] != 0.0f);
}
else {
/* Free preference (Z translates). */
has_translate = ndof_has_translate(ndof, v3d, rv3d);
has_zoom = false;
}
/* Rotation first because dynamic offset resets offset otherwise (and disables panning). */
if (has_rotation) {
const float dist_backup = rv3d->dist;
if (!is_orbit_around_pivot) {
ED_view3d_distance_set(rv3d, 0.0f);
}
view3d_ndof_orbit(ndof, vod->area, vod->region, vod, is_orbit_around_pivot);
xform_flag |= HAS_ROTATE;
if (!is_orbit_around_pivot) {
ED_view3d_distance_set(rv3d, dist_backup);
}
}
if (has_translate || has_zoom) {
view3d_ndof_pan_zoom(ndof, vod->area, vod->region, has_translate, has_zoom);
xform_flag |= HAS_TRANSLATE;
}
}
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
if (xform_flag) {
ED_view3d_camera_lock_autokey(
v3d, rv3d, C, xform_flag & HAS_ROTATE, xform_flag & HAS_TRANSLATE);
}
ED_region_tag_redraw(vod->region);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
void VIEW3D_OT_ndof_orbit_zoom(struct wmOperatorType *ot)
{
/* identifiers */
ot->name = "NDOF Orbit View with Zoom";
ot->description = "Orbit and zoom the view using the 3D mouse";
ot->idname = "VIEW3D_OT_ndof_orbit_zoom";
/* api callbacks */
ot->invoke = ndof_orbit_zoom_invoke;
ot->poll = ED_operator_view3d_active;
/* flags */
ot->flag = 0;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name NDOF Pan/Zoom Operator
* \{ */
static int ndof_pan_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
{
if (event->type != NDOF_MOTION) {
return OPERATOR_CANCELLED;
}
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
View3D *v3d = CTX_wm_view3d(C);
RegionView3D *rv3d = CTX_wm_region_view3d(C);
const wmNDOFMotionData *ndof = event->customdata;
char xform_flag = 0;
const bool has_translate = ndof_has_translate(ndof, v3d, rv3d);
const bool has_zoom = (ndof->tvec[2] != 0.0f) && !rv3d->is_persp;
/* we're panning here! so erase any leftover rotation from other operators */
rv3d->rot_angle = 0.0f;
if (!(has_translate || has_zoom)) {
return OPERATOR_CANCELLED;
}
ED_view3d_camera_lock_init_ex(depsgraph, v3d, rv3d, false);
if (ndof->progress != P_FINISHING) {
ScrArea *area = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
if (has_translate || has_zoom) {
view3d_ndof_pan_zoom(ndof, area, region, has_translate, has_zoom);
xform_flag |= HAS_TRANSLATE;
}
}
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
if (xform_flag) {
ED_view3d_camera_lock_autokey(v3d, rv3d, C, false, xform_flag & HAS_TRANSLATE);
}
ED_region_tag_redraw(CTX_wm_region(C));
return OPERATOR_FINISHED;
}
void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot)
{
/* identifiers */
ot->name = "NDOF Pan View";
ot->description = "Pan the view with the 3D mouse";
ot->idname = "VIEW3D_OT_ndof_pan";
/* api callbacks */
ot->invoke = ndof_pan_invoke;
ot->poll = ED_operator_view3d_active;
/* flags */
ot->flag = 0;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name NDOF Transform All Operator
* \{ */
/**
* wraps #ndof_orbit_zoom but never restrict to orbit.
*/
static int ndof_all_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
/* weak!, but it works */
const int ndof_flag = U.ndof_flag;
int ret;
U.ndof_flag &= ~NDOF_MODE_ORBIT;
ret = ndof_orbit_zoom_invoke(C, op, event);
U.ndof_flag = ndof_flag;
return ret;
}
void VIEW3D_OT_ndof_all(struct wmOperatorType *ot)
{
/* identifiers */
ot->name = "NDOF Transform View";
ot->description = "Pan and rotate the view with the 3D mouse";
ot->idname = "VIEW3D_OT_ndof_all";
/* api callbacks */
ot->invoke = ndof_all_invoke;
ot->poll = ED_operator_view3d_active;
/* flags */
ot->flag = 0;
}
#endif /* WITH_INPUT_NDOF */
/** \} */

View File

@ -0,0 +1,288 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spview3d
*/
#include "BLI_blenlib.h"
#include "BLI_dial_2d.h"
#include "BLI_math.h"
#include "BKE_context.h"
#include "WM_api.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "ED_screen.h"
#include "view3d_intern.h"
#include "view3d_navigate.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name View Roll Operator
* \{ */
static void view_roll_angle(
ARegion *region, float quat[4], const float orig_quat[4], const float dvec[3], float angle)
{
RegionView3D *rv3d = region->regiondata;
float quat_mul[4];
/* camera axis */
axis_angle_normalized_to_quat(quat_mul, dvec, angle);
mul_qt_qtqt(quat, orig_quat, quat_mul);
/* avoid precision loss over time */
normalize_qt(quat);
rv3d->view = RV3D_VIEW_USER;
}
static void viewroll_apply(ViewOpsData *vod, int x, int y)
{
float angle = BLI_dial_angle(vod->init.dial, (const float[2]){x, y});
if (angle != 0.0f) {
view_roll_angle(vod->region, vod->rv3d->viewquat, vod->init.quat, vod->init.mousevec, angle);
}
if (vod->use_dyn_ofs) {
view3d_orbit_apply_dyn_ofs(
vod->rv3d->ofs, vod->init.ofs, vod->init.quat, vod->rv3d->viewquat, vod->dyn_ofs);
}
if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_BOXVIEW) {
view3d_boxview_sync(vod->area, vod->region);
}
ED_view3d_camera_lock_sync(vod->depsgraph, vod->v3d, vod->rv3d);
ED_region_tag_redraw(vod->region);
}
static int viewroll_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod = op->customdata;
short event_code = VIEW_PASS;
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_MOVE:
WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_ROTATE:
WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
}
}
else if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
/* Note this does not remove auto-keys on locked cameras. */
copy_qt_qt(vod->rv3d->viewquat, vod->init.quat);
ED_view3d_camera_lock_sync(vod->depsgraph, vod->v3d, vod->rv3d);
viewops_data_free(C, op);
return OPERATOR_CANCELLED;
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
viewroll_apply(vod, event->xy[0], event->xy[1]);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, false);
}
if (ret & OPERATOR_FINISHED) {
viewops_data_free(C, op);
}
return ret;
}
enum {
V3D_VIEW_STEPLEFT = 1,
V3D_VIEW_STEPRIGHT,
};
static const EnumPropertyItem prop_view_roll_items[] = {
{0, "ANGLE", 0, "Roll Angle", "Roll the view using an angle value"},
{V3D_VIEW_STEPLEFT, "LEFT", 0, "Roll Left", "Roll the view around to the left"},
{V3D_VIEW_STEPRIGHT, "RIGHT", 0, "Roll Right", "Roll the view around to the right"},
{0, NULL, 0, NULL, NULL},
};
static int viewroll_exec(bContext *C, wmOperator *op)
{
View3D *v3d;
RegionView3D *rv3d;
ARegion *region;
if (op->customdata) {
ViewOpsData *vod = op->customdata;
region = vod->region;
v3d = vod->v3d;
}
else {
ED_view3d_context_user_region(C, &v3d, &region);
}
rv3d = region->regiondata;
if ((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d)) {
ED_view3d_smooth_view_force_finish(C, v3d, region);
int type = RNA_enum_get(op->ptr, "type");
float angle = (type == 0) ? RNA_float_get(op->ptr, "angle") : DEG2RADF(U.pad_rot_angle);
float mousevec[3];
float quat_new[4];
const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
if (type == V3D_VIEW_STEPLEFT) {
angle = -angle;
}
normalize_v3_v3(mousevec, rv3d->viewinv[2]);
negate_v3(mousevec);
view_roll_angle(region, quat_new, rv3d->viewquat, mousevec, angle);
const float *dyn_ofs_pt = NULL;
float dyn_ofs[3];
if (U.uiflag & USER_ORBIT_SELECTION) {
if (view3d_orbit_calc_center(C, dyn_ofs)) {
negate_v3(dyn_ofs);
dyn_ofs_pt = dyn_ofs;
}
}
ED_view3d_smooth_view(C,
v3d,
region,
smooth_viewtx,
&(const V3D_SmoothParams){
.quat = quat_new,
.dyn_ofs = dyn_ofs_pt,
});
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
viewops_data_free(C, op);
return OPERATOR_CANCELLED;
}
static int viewroll_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod;
bool use_angle = RNA_enum_get(op->ptr, "type") != 0;
if (use_angle || RNA_struct_property_is_set(op->ptr, "angle")) {
viewroll_exec(C, op);
}
else {
/* makes op->customdata */
viewops_data_alloc(C, op);
viewops_data_create(C, op, event, viewops_flag_from_prefs());
vod = op->customdata;
vod->init.dial = BLI_dial_init((const float[2]){BLI_rcti_cent_x(&vod->region->winrct),
BLI_rcti_cent_y(&vod->region->winrct)},
FLT_EPSILON);
ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->region);
/* overwrite the mouse vector with the view direction */
normalize_v3_v3(vod->init.mousevec, vod->rv3d->viewinv[2]);
negate_v3(vod->init.mousevec);
if (event->type == MOUSEROTATE) {
vod->init.event_xy[0] = vod->prev.event_xy[0] = event->xy[0];
viewroll_apply(vod, event->prev_xy[0], event->prev_xy[1]);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
/* add temp handler */
WM_event_add_modal_handler(C, op);
return OPERATOR_RUNNING_MODAL;
}
return OPERATOR_FINISHED;
}
static void viewroll_cancel(bContext *C, wmOperator *op)
{
viewops_data_free(C, op);
}
void VIEW3D_OT_view_roll(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "View Roll";
ot->description = "Roll the view";
ot->idname = "VIEW3D_OT_view_roll";
/* api callbacks */
ot->invoke = viewroll_invoke;
ot->exec = viewroll_exec;
ot->modal = viewroll_modal;
ot->poll = ED_operator_rv3d_user_region_poll;
ot->cancel = viewroll_cancel;
/* flags */
ot->flag = 0;
/* properties */
ot->prop = prop = RNA_def_float(
ot->srna, "angle", 0, -FLT_MAX, FLT_MAX, "Roll", "", -FLT_MAX, FLT_MAX);
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
prop = RNA_def_enum(ot->srna,
"type",
prop_view_roll_items,
0,
"Roll Angle Source",
"How roll angle is calculated");
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
}
/** \} */

View File

@ -0,0 +1,458 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spview3d
*/
#include "BLI_math.h"
#include "BKE_context.h"
#include "WM_api.h"
#include "RNA_access.h"
#include "ED_screen.h"
#include "view3d_intern.h"
#include "view3d_navigate.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name View Rotate Operator
* \{ */
void viewrotate_modal_keymap(wmKeyConfig *keyconf)
{
static const EnumPropertyItem modal_items[] = {
{VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
{VIEWROT_MODAL_AXIS_SNAP_ENABLE, "AXIS_SNAP_ENABLE", 0, "Axis Snap", ""},
{VIEWROT_MODAL_AXIS_SNAP_DISABLE, "AXIS_SNAP_DISABLE", 0, "Axis Snap (Off)", ""},
{VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
{VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
{0, NULL, 0, NULL, NULL},
};
wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "View3D Rotate Modal");
/* this function is called for each spacetype, only needs to add map once */
if (keymap && keymap->modal_items) {
return;
}
keymap = WM_modalkeymap_ensure(keyconf, "View3D Rotate Modal", modal_items);
/* disabled mode switching for now, can re-implement better, later on */
#if 0
WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
#endif
/* assign map to operators */
WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");
}
static void viewrotate_apply_snap(ViewOpsData *vod)
{
const float axis_limit = DEG2RADF(45 / 3);
RegionView3D *rv3d = vod->rv3d;
float viewquat_inv[4];
float zaxis[3] = {0, 0, 1};
float zaxis_best[3];
int x, y, z;
bool found = false;
invert_qt_qt_normalized(viewquat_inv, vod->curr.viewquat);
mul_qt_v3(viewquat_inv, zaxis);
normalize_v3(zaxis);
for (x = -1; x < 2; x++) {
for (y = -1; y < 2; y++) {
for (z = -1; z < 2; z++) {
if (x || y || z) {
float zaxis_test[3] = {x, y, z};
normalize_v3(zaxis_test);
if (angle_normalized_v3v3(zaxis_test, zaxis) < axis_limit) {
copy_v3_v3(zaxis_best, zaxis_test);
found = true;
}
}
}
}
}
if (found) {
/* find the best roll */
float quat_roll[4], quat_final[4], quat_best[4], quat_snap[4];
float viewquat_align[4]; /* viewquat aligned to zaxis_best */
float viewquat_align_inv[4]; /* viewquat aligned to zaxis_best */
float best_angle = axis_limit;
int j;
/* viewquat_align is the original viewquat aligned to the snapped axis
* for testing roll */
rotation_between_vecs_to_quat(viewquat_align, zaxis_best, zaxis);
normalize_qt(viewquat_align);
mul_qt_qtqt(viewquat_align, vod->curr.viewquat, viewquat_align);
normalize_qt(viewquat_align);
invert_qt_qt_normalized(viewquat_align_inv, viewquat_align);
vec_to_quat(quat_snap, zaxis_best, OB_NEGZ, OB_POSY);
normalize_qt(quat_snap);
invert_qt_normalized(quat_snap);
/* check if we can find the roll */
found = false;
/* find best roll */
for (j = 0; j < 8; j++) {
float angle;
float xaxis1[3] = {1, 0, 0};
float xaxis2[3] = {1, 0, 0};
float quat_final_inv[4];
axis_angle_to_quat(quat_roll, zaxis_best, (float)j * DEG2RADF(45.0f));
normalize_qt(quat_roll);
mul_qt_qtqt(quat_final, quat_snap, quat_roll);
normalize_qt(quat_final);
/* compare 2 vector angles to find the least roll */
invert_qt_qt_normalized(quat_final_inv, quat_final);
mul_qt_v3(viewquat_align_inv, xaxis1);
mul_qt_v3(quat_final_inv, xaxis2);
angle = angle_v3v3(xaxis1, xaxis2);
if (angle <= best_angle) {
found = true;
best_angle = angle;
copy_qt_qt(quat_best, quat_final);
}
}
if (found) {
/* lock 'quat_best' to an axis view if we can */
ED_view3d_quat_to_axis_view(quat_best, 0.01f, &rv3d->view, &rv3d->view_axis_roll);
if (rv3d->view != RV3D_VIEW_USER) {
ED_view3d_quat_from_axis_view(rv3d->view, rv3d->view_axis_roll, quat_best);
}
}
else {
copy_qt_qt(quat_best, viewquat_align);
}
copy_qt_qt(rv3d->viewquat, quat_best);
viewrotate_apply_dyn_ofs(vod, rv3d->viewquat);
if (U.uiflag & USER_AUTOPERSP) {
if (RV3D_VIEW_IS_AXIS(rv3d->view)) {
if (rv3d->persp == RV3D_PERSP) {
rv3d->persp = RV3D_ORTHO;
}
}
}
}
else if (U.uiflag & USER_AUTOPERSP) {
rv3d->persp = vod->init.persp;
}
}
static void viewrotate_apply(ViewOpsData *vod, const int event_xy[2])
{
RegionView3D *rv3d = vod->rv3d;
rv3d->view = RV3D_VIEW_USER; /* need to reset every time because of view snapping */
if (U.flag & USER_TRACKBALL) {
float axis[3], q1[4], dvec[3], newvec[3];
float angle;
{
const int event_xy_offset[2] = {
event_xy[0] + vod->init.event_xy_offset[0],
event_xy[1] + vod->init.event_xy_offset[1],
};
calctrackballvec(&vod->region->winrct, event_xy_offset, newvec);
}
sub_v3_v3v3(dvec, newvec, vod->init.trackvec);
angle = (len_v3(dvec) / (2.0f * TRACKBALLSIZE)) * (float)M_PI;
/* Before applying the sensitivity this is rotating 1:1,
* where the cursor would match the surface of a sphere in the view. */
angle *= U.view_rotate_sensitivity_trackball;
/* Allow for rotation beyond the interval [-pi, pi] */
angle = angle_wrap_rad(angle);
/* This relation is used instead of the actual angle between vectors
* so that the angle of rotation is linearly proportional to
* the distance that the mouse is dragged. */
cross_v3_v3v3(axis, vod->init.trackvec, newvec);
axis_angle_to_quat(q1, axis, angle);
mul_qt_qtqt(vod->curr.viewquat, q1, vod->init.quat);
viewrotate_apply_dyn_ofs(vod, vod->curr.viewquat);
}
else {
float quat_local_x[4], quat_global_z[4];
float m[3][3];
float m_inv[3][3];
const float zvec_global[3] = {0.0f, 0.0f, 1.0f};
float xaxis[3];
/* Radians per-pixel. */
const float sensitivity = U.view_rotate_sensitivity_turntable / U.dpi_fac;
/* Get the 3x3 matrix and its inverse from the quaternion */
quat_to_mat3(m, vod->curr.viewquat);
invert_m3_m3(m_inv, m);
/* Avoid Gimbal Lock
*
* Even though turn-table mode is in use, this can occur when the user exits the camera view
* or when aligning the view to a rotated object.
*
* We have gimbal lock when the user's view is rotated +/- 90 degrees along the view axis.
* In this case the vertical rotation is the same as the sideways turntable motion.
* Making it impossible to get out of the gimbal locked state without resetting the view.
*
* The logic below lets the user exit out of this state without any abrupt 'fix'
* which would be disorienting.
*
* This works by blending two horizons:
* - Rotated-horizon: `cross_v3_v3v3(xaxis, zvec_global, m_inv[2])`
* When only this is used, this turntable rotation works - but it's side-ways
* (as if the entire turn-table has been placed on its side)
* While there is no gimbal lock, it's also awkward to use.
* - Un-rotated-horizon: `m_inv[0]`
* When only this is used, the turntable rotation can have gimbal lock.
*
* The solution used here is to blend between these two values,
* so the severity of the gimbal lock is used to blend the rotated horizon.
* Blending isn't essential, it just makes the transition smoother.
*
* This allows sideways turn-table rotation on a Z axis that isn't world-space Z,
* While up-down turntable rotation eventually corrects gimbal lock. */
#if 1
if (len_squared_v3v3(zvec_global, m_inv[2]) > 0.001f) {
float fac;
cross_v3_v3v3(xaxis, zvec_global, m_inv[2]);
if (dot_v3v3(xaxis, m_inv[0]) < 0) {
negate_v3(xaxis);
}
fac = angle_normalized_v3v3(zvec_global, m_inv[2]) / (float)M_PI;
fac = fabsf(fac - 0.5f) * 2;
fac = fac * fac;
interp_v3_v3v3(xaxis, xaxis, m_inv[0], fac);
}
else {
copy_v3_v3(xaxis, m_inv[0]);
}
#else
copy_v3_v3(xaxis, m_inv[0]);
#endif
/* Determine the direction of the x vector (for rotating up and down) */
/* This can likely be computed directly from the quaternion. */
/* Perform the up/down rotation */
axis_angle_to_quat(quat_local_x, xaxis, sensitivity * -(event_xy[1] - vod->prev.event_xy[1]));
mul_qt_qtqt(quat_local_x, vod->curr.viewquat, quat_local_x);
/* Perform the orbital rotation */
axis_angle_to_quat_single(
quat_global_z, 'Z', sensitivity * vod->reverse * (event_xy[0] - vod->prev.event_xy[0]));
mul_qt_qtqt(vod->curr.viewquat, quat_local_x, quat_global_z);
viewrotate_apply_dyn_ofs(vod, vod->curr.viewquat);
}
/* avoid precision loss over time */
normalize_qt(vod->curr.viewquat);
/* use a working copy so view rotation locking doesn't overwrite the locked
* rotation back into the view we calculate with */
copy_qt_qt(rv3d->viewquat, vod->curr.viewquat);
/* Check for view snap,
* NOTE: don't apply snap to `vod->viewquat` so the view won't jam up. */
if (vod->axis_snap) {
viewrotate_apply_snap(vod);
}
vod->prev.event_xy[0] = event_xy[0];
vod->prev.event_xy[1] = event_xy[1];
ED_view3d_camera_lock_sync(vod->depsgraph, vod->v3d, rv3d);
ED_region_tag_redraw(vod->region);
}
static int viewrotate_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod = op->customdata;
short event_code = VIEW_PASS;
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_AXIS_SNAP_ENABLE:
vod->axis_snap = true;
event_code = VIEW_APPLY;
break;
case VIEWROT_MODAL_AXIS_SNAP_DISABLE:
vod->rv3d->persp = vod->init.persp;
vod->axis_snap = false;
event_code = VIEW_APPLY;
break;
case VIEWROT_MODAL_SWITCH_ZOOM:
WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_MOVE:
WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
}
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
viewrotate_apply(vod, event->xy);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, true);
}
if (ret & OPERATOR_FINISHED) {
viewops_data_free(C, op);
}
return ret;
}
static int viewrotate_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod;
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
/* makes op->customdata */
viewops_data_alloc(C, op);
vod = op->customdata;
/* poll should check but in some cases fails, see poll func for details */
if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_LOCK_ROTATION) {
viewops_data_free(C, op);
return OPERATOR_PASS_THROUGH;
}
ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->region);
viewops_data_create(C,
op,
event,
viewops_flag_from_prefs() | VIEWOPS_FLAG_PERSP_ENSURE |
(use_cursor_init ? VIEWOPS_FLAG_USE_MOUSE_INIT : 0));
if (ELEM(event->type, MOUSEPAN, MOUSEROTATE)) {
/* Rotate direction we keep always same */
int event_xy[2];
if (event->type == MOUSEPAN) {
if (event->is_direction_inverted) {
event_xy[0] = 2 * event->xy[0] - event->prev_xy[0];
event_xy[1] = 2 * event->xy[1] - event->prev_xy[1];
}
else {
copy_v2_v2_int(event_xy, event->prev_xy);
}
}
else {
/* MOUSEROTATE performs orbital rotation, so y axis delta is set to 0 */
copy_v2_v2_int(event_xy, event->prev_xy);
}
viewrotate_apply(vod, event_xy);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
/* add temp handler */
WM_event_add_modal_handler(C, op);
return OPERATOR_RUNNING_MODAL;
}
static void viewrotate_cancel(bContext *C, wmOperator *op)
{
viewops_data_free(C, op);
}
void VIEW3D_OT_rotate(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Rotate View";
ot->description = "Rotate the view";
ot->idname = "VIEW3D_OT_rotate";
/* api callbacks */
ot->invoke = viewrotate_invoke;
ot->modal = viewrotate_modal;
ot->poll = ED_operator_region_view3d_active;
ot->cancel = viewrotate_cancel;
/* flags */
ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR_XY;
view3d_operator_properties_common(ot, V3D_OP_PROP_USE_MOUSE_INIT);
}
/** \} */

View File

@ -0,0 +1,406 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spview3d
*/
#include "DNA_camera_types.h"
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "BKE_context.h"
#include "DEG_depsgraph_query.h"
#include "WM_api.h"
#include "ED_screen.h"
#include "view3d_intern.h"
#include "view3d_navigate.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name Smooth View Operator & Utilities
*
* Use for view transitions to have smooth (animated) transitions.
* \{ */
/* This operator is one of the 'timer refresh' ones like animation playback */
struct SmoothView3DState {
float dist;
float lens;
float quat[4];
float ofs[3];
};
struct SmoothView3DStore {
/* Source. */
struct SmoothView3DState src; /* source */
struct SmoothView3DState dst; /* destination */
struct SmoothView3DState org; /* original */
bool to_camera;
bool use_dyn_ofs;
float dyn_ofs[3];
/* When smooth-view is enabled, store the 'rv3d->view' here,
* assign back when the view motion is completed. */
char org_view;
double time_allowed;
};
static void view3d_smooth_view_state_backup(struct SmoothView3DState *sms_state,
const View3D *v3d,
const RegionView3D *rv3d)
{
copy_v3_v3(sms_state->ofs, rv3d->ofs);
copy_qt_qt(sms_state->quat, rv3d->viewquat);
sms_state->dist = rv3d->dist;
sms_state->lens = v3d->lens;
}
static void view3d_smooth_view_state_restore(const struct SmoothView3DState *sms_state,
View3D *v3d,
RegionView3D *rv3d)
{
copy_v3_v3(rv3d->ofs, sms_state->ofs);
copy_qt_qt(rv3d->viewquat, sms_state->quat);
rv3d->dist = sms_state->dist;
v3d->lens = sms_state->lens;
}
/* will start timer if appropriate */
void ED_view3d_smooth_view_ex(
/* avoid passing in the context */
const Depsgraph *depsgraph,
wmWindowManager *wm,
wmWindow *win,
ScrArea *area,
View3D *v3d,
ARegion *region,
const int smooth_viewtx,
const V3D_SmoothParams *sview)
{
RegionView3D *rv3d = region->regiondata;
struct SmoothView3DStore sms = {{0}};
/* initialize sms */
view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d);
view3d_smooth_view_state_backup(&sms.src, v3d, rv3d);
/* If smooth-view runs multiple times. */
if (rv3d->sms == NULL) {
view3d_smooth_view_state_backup(&sms.org, v3d, rv3d);
}
else {
sms.org = rv3d->sms->org;
}
sms.org_view = rv3d->view;
/* sms.to_camera = false; */ /* initialized to zero anyway */
/* note on camera locking, this is a little confusing but works ok.
* we may be changing the view 'as if' there is no active camera, but in fact
* there is an active camera which is locked to the view.
*
* In the case where smooth view is moving _to_ a camera we don't want that
* camera to be moved or changed, so only when the camera is not being set should
* we allow camera option locking to initialize the view settings from the camera.
*/
if (sview->camera == NULL && sview->camera_old == NULL) {
ED_view3d_camera_lock_init(depsgraph, v3d, rv3d);
}
/* store the options we want to end with */
if (sview->ofs) {
copy_v3_v3(sms.dst.ofs, sview->ofs);
}
if (sview->quat) {
copy_qt_qt(sms.dst.quat, sview->quat);
}
if (sview->dist) {
sms.dst.dist = *sview->dist;
}
if (sview->lens) {
sms.dst.lens = *sview->lens;
}
if (sview->dyn_ofs) {
BLI_assert(sview->ofs == NULL);
BLI_assert(sview->quat != NULL);
copy_v3_v3(sms.dyn_ofs, sview->dyn_ofs);
sms.use_dyn_ofs = true;
/* calculate the final destination offset */
view3d_orbit_apply_dyn_ofs(sms.dst.ofs, sms.src.ofs, sms.src.quat, sms.dst.quat, sms.dyn_ofs);
}
if (sview->camera) {
Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, sview->camera);
if (sview->ofs != NULL) {
sms.dst.dist = ED_view3d_offset_distance(
ob_camera_eval->obmat, sview->ofs, VIEW3D_DIST_FALLBACK);
}
ED_view3d_from_object(ob_camera_eval, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens);
sms.to_camera = true; /* restore view3d values in end */
}
if ((sview->camera_old == sview->camera) && /* Camera. */
(sms.dst.dist == rv3d->dist) && /* Distance. */
(sms.dst.lens == v3d->lens) && /* Lens. */
equals_v3v3(sms.dst.ofs, rv3d->ofs) && /* Offset. */
equals_v4v4(sms.dst.quat, rv3d->viewquat) /* Rotation. */
) {
/* Early return if nothing changed. */
return;
}
/* Skip smooth viewing for external render engine draw. */
if (smooth_viewtx && !(v3d->shading.type == OB_RENDER && rv3d->render_engine)) {
/* original values */
if (sview->camera_old) {
Object *ob_camera_old_eval = DEG_get_evaluated_object(depsgraph, sview->camera_old);
if (sview->ofs != NULL) {
sms.src.dist = ED_view3d_offset_distance(ob_camera_old_eval->obmat, sview->ofs, 0.0f);
}
ED_view3d_from_object(
ob_camera_old_eval, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens);
}
/* grid draw as floor */
if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0) {
/* use existing if exists, means multiple calls to smooth view
* won't lose the original 'view' setting */
rv3d->view = RV3D_VIEW_USER;
}
sms.time_allowed = (double)smooth_viewtx / 1000.0;
/* If this is view rotation only we can decrease the time allowed by the angle between quats
* this means small rotations won't lag. */
if (sview->quat && !sview->ofs && !sview->dist) {
/* scale the time allowed by the rotation */
/* 180deg == 1.0 */
sms.time_allowed *= (double)fabsf(angle_signed_normalized_qtqt(sms.dst.quat, sms.src.quat)) /
M_PI;
}
/* ensure it shows correct */
if (sms.to_camera) {
/* use ortho if we move from an ortho view to an ortho camera */
Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, sview->camera);
rv3d->persp = (((rv3d->is_persp == false) && (ob_camera_eval->type == OB_CAMERA) &&
(((Camera *)ob_camera_eval->data)->type == CAM_ORTHO)) ?
RV3D_ORTHO :
RV3D_PERSP);
}
rv3d->rflag |= RV3D_NAVIGATING;
/* not essential but in some cases the caller will tag the area for redraw, and in that
* case we can get a flicker of the 'org' user view but we want to see 'src' */
view3d_smooth_view_state_restore(&sms.src, v3d, rv3d);
/* keep track of running timer! */
if (rv3d->sms == NULL) {
rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d");
}
*rv3d->sms = sms;
if (rv3d->smooth_timer) {
WM_event_remove_timer(wm, win, rv3d->smooth_timer);
}
/* #TIMER1 is hard-coded in key-map. */
rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0);
}
else {
/* Animation is disabled, apply immediately. */
if (sms.to_camera == false) {
copy_v3_v3(rv3d->ofs, sms.dst.ofs);
copy_qt_qt(rv3d->viewquat, sms.dst.quat);
rv3d->dist = sms.dst.dist;
v3d->lens = sms.dst.lens;
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
}
if (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW) {
view3d_boxview_copy(area, region);
}
ED_region_tag_redraw(region);
WM_event_add_mousemove(win);
}
}
void ED_view3d_smooth_view(bContext *C,
View3D *v3d,
ARegion *region,
const int smooth_viewtx,
const struct V3D_SmoothParams *sview)
{
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
wmWindowManager *wm = CTX_wm_manager(C);
wmWindow *win = CTX_wm_window(C);
ScrArea *area = CTX_wm_area(C);
ED_view3d_smooth_view_ex(depsgraph, wm, win, area, v3d, region, smooth_viewtx, sview);
}
/* only meant for timer usage */
static void view3d_smoothview_apply(bContext *C, View3D *v3d, ARegion *region, bool sync_boxview)
{
wmWindowManager *wm = CTX_wm_manager(C);
RegionView3D *rv3d = region->regiondata;
struct SmoothView3DStore *sms = rv3d->sms;
float step, step_inv;
if (sms->time_allowed != 0.0) {
step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed);
}
else {
step = 1.0f;
}
/* end timer */
if (step >= 1.0f) {
wmWindow *win = CTX_wm_window(C);
/* if we went to camera, store the original */
if (sms->to_camera) {
rv3d->persp = RV3D_CAMOB;
view3d_smooth_view_state_restore(&sms->org, v3d, rv3d);
}
else {
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d);
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
}
if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0) {
rv3d->view = sms->org_view;
}
MEM_freeN(rv3d->sms);
rv3d->sms = NULL;
WM_event_remove_timer(wm, win, rv3d->smooth_timer);
rv3d->smooth_timer = NULL;
rv3d->rflag &= ~RV3D_NAVIGATING;
/* Event handling won't know if a UI item has been moved under the pointer. */
WM_event_add_mousemove(win);
}
else {
/* ease in/out */
step = (3.0f * step * step - 2.0f * step * step * step);
step_inv = 1.0f - step;
interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step);
if (sms->use_dyn_ofs) {
view3d_orbit_apply_dyn_ofs(
rv3d->ofs, sms->src.ofs, sms->src.quat, rv3d->viewquat, sms->dyn_ofs);
}
else {
interp_v3_v3v3(rv3d->ofs, sms->src.ofs, sms->dst.ofs, step);
}
rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv;
v3d->lens = sms->dst.lens * step + sms->src.lens * step_inv;
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
if (ED_screen_animation_playing(wm)) {
ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
}
}
if (sync_boxview && (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW)) {
view3d_boxview_copy(CTX_wm_area(C), region);
}
/* NOTE: this doesn't work right because the v3d->lens is now used in ortho mode r51636,
* when switching camera in quad-view the other ortho views would zoom & reset.
*
* For now only redraw all regions when smooth-view finishes.
*/
if (step >= 1.0f) {
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
}
else {
ED_region_tag_redraw(region);
}
}
static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
{
View3D *v3d = CTX_wm_view3d(C);
ARegion *region = CTX_wm_region(C);
RegionView3D *rv3d = region->regiondata;
/* escape if not our timer */
if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata) {
return OPERATOR_PASS_THROUGH;
}
view3d_smoothview_apply(C, v3d, region, true);
return OPERATOR_FINISHED;
}
void ED_view3d_smooth_view_force_finish(bContext *C, View3D *v3d, ARegion *region)
{
RegionView3D *rv3d = region->regiondata;
if (rv3d && rv3d->sms) {
rv3d->sms->time_allowed = 0.0; /* force finishing */
view3d_smoothview_apply(C, v3d, region, false);
/* force update of view matrix so tools that run immediately after
* can use them without redrawing first */
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Scene *scene = CTX_data_scene(C);
ED_view3d_update_viewmat(depsgraph, scene, v3d, region, NULL, NULL, NULL, false);
}
}
void VIEW3D_OT_smoothview(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Smooth View";
ot->idname = "VIEW3D_OT_smoothview";
/* api callbacks */
ot->invoke = view3d_smoothview_invoke;
/* flags */
ot->flag = OPTYPE_INTERNAL;
ot->poll = ED_operator_view3d_active;
}
/** \} */

View File

@ -58,6 +58,7 @@
#include "DEG_depsgraph.h"
#include "view3d_intern.h" /* own include */
#include "view3d_navigate.h"
#ifdef WITH_INPUT_NDOF
//# define NDOF_WALK_DEBUG
@ -1486,9 +1487,9 @@ static int walk_modal(bContext *C, wmOperator *op, const wmEvent *event)
}
else
#endif /* WITH_INPUT_NDOF */
if (event->type == TIMER && event->customdata == walk->timer) {
walkApply(C, walk, false);
}
if (event->type == TIMER && event->customdata == walk->timer) {
walkApply(C, walk, false);
}
do_draw |= walk->redraw;

View File

@ -0,0 +1,598 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spview3d
*/
#include "BLI_math.h"
#include "BLI_rect.h"
#include "BKE_context.h"
#include "BKE_screen.h"
#include "DEG_depsgraph_query.h"
#include "WM_api.h"
#include "RNA_access.h"
#include "ED_screen.h"
#include "PIL_time.h"
#include "view3d_intern.h"
#include "view3d_navigate.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name View Zoom Operator
* \{ */
/* #viewdolly_modal_keymap has an exact copy of this, apply fixes to both. */
void viewzoom_modal_keymap(wmKeyConfig *keyconf)
{
static const EnumPropertyItem modal_items[] = {
{VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
{VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
{VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
{0, NULL, 0, NULL, NULL},
};
wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "View3D Zoom Modal");
/* this function is called for each spacetype, only needs to add map once */
if (keymap && keymap->modal_items) {
return;
}
keymap = WM_modalkeymap_ensure(keyconf, "View3D Zoom Modal", modal_items);
/* disabled mode switching for now, can re-implement better, later on */
#if 0
WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
#endif
/* assign map to operators */
WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
}
/**
* \param zoom_xy: Optionally zoom to window location
* (coords compatible w/ #wmEvent.xy). Use when not NULL.
*/
static void view_zoom_to_window_xy_camera(Scene *scene,
Depsgraph *depsgraph,
View3D *v3d,
ARegion *region,
float dfac,
const int zoom_xy[2])
{
RegionView3D *rv3d = region->regiondata;
const float zoomfac = BKE_screen_view3d_zoom_to_fac(rv3d->camzoom);
const float zoomfac_new = clamp_f(
zoomfac * (1.0f / dfac), RV3D_CAMZOOM_MIN_FACTOR, RV3D_CAMZOOM_MAX_FACTOR);
const float camzoom_new = BKE_screen_view3d_zoom_from_fac(zoomfac_new);
if (zoom_xy != NULL) {
float zoomfac_px;
rctf camera_frame_old;
rctf camera_frame_new;
const float pt_src[2] = {zoom_xy[0], zoom_xy[1]};
float pt_dst[2];
float delta_px[2];
ED_view3d_calc_camera_border(scene, depsgraph, region, v3d, rv3d, &camera_frame_old, false);
BLI_rctf_translate(&camera_frame_old, region->winrct.xmin, region->winrct.ymin);
rv3d->camzoom = camzoom_new;
CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
ED_view3d_calc_camera_border(scene, depsgraph, region, v3d, rv3d, &camera_frame_new, false);
BLI_rctf_translate(&camera_frame_new, region->winrct.xmin, region->winrct.ymin);
BLI_rctf_transform_pt_v(&camera_frame_new, &camera_frame_old, pt_dst, pt_src);
sub_v2_v2v2(delta_px, pt_dst, pt_src);
/* translate the camera offset using pixel space delta
* mapped back to the camera (same logic as panning in camera view) */
zoomfac_px = BKE_screen_view3d_zoom_to_fac(rv3d->camzoom) * 2.0f;
rv3d->camdx += delta_px[0] / (region->winx * zoomfac_px);
rv3d->camdy += delta_px[1] / (region->winy * zoomfac_px);
CLAMP(rv3d->camdx, -1.0f, 1.0f);
CLAMP(rv3d->camdy, -1.0f, 1.0f);
}
else {
rv3d->camzoom = camzoom_new;
CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
}
}
/**
* \param zoom_xy: Optionally zoom to window location
* (coords compatible w/ #wmEvent.xy). Use when not NULL.
*/
static void view_zoom_to_window_xy_3d(ARegion *region, float dfac, const int zoom_xy[2])
{
RegionView3D *rv3d = region->regiondata;
const float dist_new = rv3d->dist * dfac;
if (zoom_xy != NULL) {
float dvec[3];
float tvec[3];
float tpos[3];
float mval_f[2];
float zfac;
negate_v3_v3(tpos, rv3d->ofs);
mval_f[0] = (float)(((zoom_xy[0] - region->winrct.xmin) * 2) - region->winx) / 2.0f;
mval_f[1] = (float)(((zoom_xy[1] - region->winrct.ymin) * 2) - region->winy) / 2.0f;
/* Project cursor position into 3D space */
zfac = ED_view3d_calc_zfac(rv3d, tpos, NULL);
ED_view3d_win_to_delta(region, mval_f, dvec, zfac);
/* Calculate view target position for dolly */
add_v3_v3v3(tvec, tpos, dvec);
negate_v3(tvec);
/* Offset to target position and dolly */
copy_v3_v3(rv3d->ofs, tvec);
rv3d->dist = dist_new;
/* Calculate final offset */
madd_v3_v3v3fl(rv3d->ofs, tvec, dvec, dfac);
}
else {
rv3d->dist = dist_new;
}
}
static float viewzoom_scale_value(const rcti *winrct,
const eViewZoom_Style viewzoom,
const bool zoom_invert,
const bool zoom_invert_force,
const int xy_curr[2],
const int xy_init[2],
const float val,
const float val_orig,
double *r_timer_lastdraw)
{
float zfac;
if (viewzoom == USER_ZOOM_CONTINUE) {
double time = PIL_check_seconds_timer();
float time_step = (float)(time - *r_timer_lastdraw);
float fac;
if (U.uiflag & USER_ZOOM_HORIZ) {
fac = (float)(xy_init[0] - xy_curr[0]);
}
else {
fac = (float)(xy_init[1] - xy_curr[1]);
}
fac /= U.dpi_fac;
if (zoom_invert != zoom_invert_force) {
fac = -fac;
}
zfac = 1.0f + ((fac / 20.0f) * time_step);
*r_timer_lastdraw = time;
}
else if (viewzoom == USER_ZOOM_SCALE) {
/* method which zooms based on how far you move the mouse */
const int ctr[2] = {
BLI_rcti_cent_x(winrct),
BLI_rcti_cent_y(winrct),
};
float len_new = (5 * U.dpi_fac) + ((float)len_v2v2_int(ctr, xy_curr) / U.dpi_fac);
float len_old = (5 * U.dpi_fac) + ((float)len_v2v2_int(ctr, xy_init) / U.dpi_fac);
/* intentionally ignore 'zoom_invert' for scale */
if (zoom_invert_force) {
SWAP(float, len_new, len_old);
}
zfac = val_orig * (len_old / max_ff(len_new, 1.0f)) / val;
}
else { /* USER_ZOOM_DOLLY */
float len_new = 5 * U.dpi_fac;
float len_old = 5 * U.dpi_fac;
if (U.uiflag & USER_ZOOM_HORIZ) {
len_new += (winrct->xmax - (xy_curr[0])) / U.dpi_fac;
len_old += (winrct->xmax - (xy_init[0])) / U.dpi_fac;
}
else {
len_new += (winrct->ymax - (xy_curr[1])) / U.dpi_fac;
len_old += (winrct->ymax - (xy_init[1])) / U.dpi_fac;
}
if (zoom_invert != zoom_invert_force) {
SWAP(float, len_new, len_old);
}
zfac = val_orig * (2.0f * ((len_new / max_ff(len_old, 1.0f)) - 1.0f) + 1.0f) / val;
}
return zfac;
}
static float viewzoom_scale_value_offset(const rcti *winrct,
const eViewZoom_Style viewzoom,
const bool zoom_invert,
const bool zoom_invert_force,
const int xy_curr[2],
const int xy_init[2],
const int xy_offset[2],
const float val,
const float val_orig,
double *r_timer_lastdraw)
{
const int xy_curr_offset[2] = {
xy_curr[0] + xy_offset[0],
xy_curr[1] + xy_offset[1],
};
const int xy_init_offset[2] = {
xy_init[0] + xy_offset[0],
xy_init[1] + xy_offset[1],
};
return viewzoom_scale_value(winrct,
viewzoom,
zoom_invert,
zoom_invert_force,
xy_curr_offset,
xy_init_offset,
val,
val_orig,
r_timer_lastdraw);
}
static void viewzoom_apply_camera(ViewOpsData *vod,
const int xy[2],
const eViewZoom_Style viewzoom,
const bool zoom_invert,
const bool zoom_to_pos)
{
float zfac;
float zoomfac_prev = BKE_screen_view3d_zoom_to_fac(vod->init.camzoom) * 2.0f;
float zoomfac = BKE_screen_view3d_zoom_to_fac(vod->rv3d->camzoom) * 2.0f;
zfac = viewzoom_scale_value_offset(&vod->region->winrct,
viewzoom,
zoom_invert,
true,
xy,
vod->init.event_xy,
vod->init.event_xy_offset,
zoomfac,
zoomfac_prev,
&vod->prev.time);
if (!ELEM(zfac, 1.0f, 0.0f)) {
/* calculate inverted, then invert again (needed because of camera zoom scaling) */
zfac = 1.0f / zfac;
view_zoom_to_window_xy_camera(vod->scene,
vod->depsgraph,
vod->v3d,
vod->region,
zfac,
zoom_to_pos ? vod->prev.event_xy : NULL);
}
ED_region_tag_redraw(vod->region);
}
static void viewzoom_apply_3d(ViewOpsData *vod,
const int xy[2],
const eViewZoom_Style viewzoom,
const bool zoom_invert,
const bool zoom_to_pos)
{
float zfac;
float dist_range[2];
ED_view3d_dist_range_get(vod->v3d, dist_range);
zfac = viewzoom_scale_value_offset(&vod->region->winrct,
viewzoom,
zoom_invert,
false,
xy,
vod->init.event_xy,
vod->init.event_xy_offset,
vod->rv3d->dist,
vod->init.dist,
&vod->prev.time);
if (zfac != 1.0f) {
const float zfac_min = dist_range[0] / vod->rv3d->dist;
const float zfac_max = dist_range[1] / vod->rv3d->dist;
CLAMP(zfac, zfac_min, zfac_max);
view_zoom_to_window_xy_3d(vod->region, zfac, zoom_to_pos ? vod->prev.event_xy : NULL);
}
/* these limits were in old code too */
CLAMP(vod->rv3d->dist, dist_range[0], dist_range[1]);
if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_BOXVIEW) {
view3d_boxview_sync(vod->area, vod->region);
}
ED_view3d_camera_lock_sync(vod->depsgraph, vod->v3d, vod->rv3d);
ED_region_tag_redraw(vod->region);
}
static void viewzoom_apply(ViewOpsData *vod,
const int xy[2],
const eViewZoom_Style viewzoom,
const bool zoom_invert,
const bool zoom_to_pos)
{
if ((vod->rv3d->persp == RV3D_CAMOB) &&
(vod->rv3d->is_persp && ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) == 0) {
viewzoom_apply_camera(vod, xy, viewzoom, zoom_invert, zoom_to_pos);
}
else {
viewzoom_apply_3d(vod, xy, viewzoom, zoom_invert, zoom_to_pos);
}
}
static int viewzoom_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod = op->customdata;
short event_code = VIEW_PASS;
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == TIMER && event->customdata == vod->timer) {
/* continuous zoom */
event_code = VIEW_APPLY;
}
else if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_MOVE:
WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
case VIEWROT_MODAL_SWITCH_ROTATE:
WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
event_code = VIEW_CONFIRM;
break;
}
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
viewzoom_apply(vod,
event->xy,
(eViewZoom_Style)U.viewzoom,
(U.uiflag & USER_ZOOM_INVERT) != 0,
(use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)));
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
}
if (ret & OPERATOR_FINISHED) {
viewops_data_free(C, op);
}
return ret;
}
static int viewzoom_exec(bContext *C, wmOperator *op)
{
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Scene *scene = CTX_data_scene(C);
View3D *v3d;
RegionView3D *rv3d;
ScrArea *area;
ARegion *region;
bool use_cam_zoom;
float dist_range[2];
const int delta = RNA_int_get(op->ptr, "delta");
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
if (op->customdata) {
ViewOpsData *vod = op->customdata;
area = vod->area;
region = vod->region;
}
else {
area = CTX_wm_area(C);
region = CTX_wm_region(C);
}
v3d = area->spacedata.first;
rv3d = region->regiondata;
use_cam_zoom = (rv3d->persp == RV3D_CAMOB) &&
!(rv3d->is_persp && ED_view3d_camera_lock_check(v3d, rv3d));
int zoom_xy_buf[2];
const int *zoom_xy = NULL;
if (use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)) {
zoom_xy_buf[0] = RNA_struct_property_is_set(op->ptr, "mx") ? RNA_int_get(op->ptr, "mx") :
region->winx / 2;
zoom_xy_buf[1] = RNA_struct_property_is_set(op->ptr, "my") ? RNA_int_get(op->ptr, "my") :
region->winy / 2;
zoom_xy = zoom_xy_buf;
}
ED_view3d_dist_range_get(v3d, dist_range);
if (delta < 0) {
const float step = 1.2f;
/* this min and max is also in viewmove() */
if (use_cam_zoom) {
view_zoom_to_window_xy_camera(scene, depsgraph, v3d, region, step, zoom_xy);
}
else {
if (rv3d->dist < dist_range[1]) {
view_zoom_to_window_xy_3d(region, step, zoom_xy);
}
}
}
else {
const float step = 1.0f / 1.2f;
if (use_cam_zoom) {
view_zoom_to_window_xy_camera(scene, depsgraph, v3d, region, step, zoom_xy);
}
else {
if (rv3d->dist > dist_range[0]) {
view_zoom_to_window_xy_3d(region, step, zoom_xy);
}
}
}
if (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW) {
view3d_boxview_sync(area, region);
}
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
ED_view3d_camera_lock_autokey(v3d, rv3d, C, false, true);
ED_region_tag_redraw(region);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
/* viewdolly_invoke() copied this function, changes here may apply there */
static int viewzoom_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
ViewOpsData *vod;
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
/* makes op->customdata */
viewops_data_alloc(C, op);
viewops_data_create(C,
op,
event,
(viewops_flag_from_prefs() & ~VIEWOPS_FLAG_ORBIT_SELECT) |
(use_cursor_init ? VIEWOPS_FLAG_USE_MOUSE_INIT : 0));
vod = op->customdata;
ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->region);
/* if one or the other zoom position aren't set, set from event */
if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
RNA_int_set(op->ptr, "mx", event->xy[0]);
RNA_int_set(op->ptr, "my", event->xy[1]);
}
if (RNA_struct_property_is_set(op->ptr, "delta")) {
viewzoom_exec(C, op);
}
else {
if (ELEM(event->type, MOUSEZOOM, MOUSEPAN)) {
if (U.uiflag & USER_ZOOM_HORIZ) {
vod->init.event_xy[0] = vod->prev.event_xy[0] = event->xy[0];
}
else {
/* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
vod->init.event_xy[1] = vod->prev.event_xy[1] = vod->init.event_xy[1] + event->xy[0] -
event->prev_xy[0];
}
viewzoom_apply(vod,
event->prev_xy,
USER_ZOOM_DOLLY,
(U.uiflag & USER_ZOOM_INVERT) != 0,
(use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)));
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
viewops_data_free(C, op);
return OPERATOR_FINISHED;
}
if (U.viewzoom == USER_ZOOM_CONTINUE) {
/* needs a timer to continue redrawing */
vod->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
vod->prev.time = PIL_check_seconds_timer();
}
/* add temp handler */
WM_event_add_modal_handler(C, op);
return OPERATOR_RUNNING_MODAL;
}
return OPERATOR_FINISHED;
}
static void viewzoom_cancel(bContext *C, wmOperator *op)
{
viewops_data_free(C, op);
}
void VIEW3D_OT_zoom(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Zoom View";
ot->description = "Zoom in/out in the view";
ot->idname = "VIEW3D_OT_zoom";
/* api callbacks */
ot->invoke = viewzoom_invoke;
ot->exec = viewzoom_exec;
ot->modal = viewzoom_modal;
ot->poll = view3d_zoom_or_dolly_poll;
ot->cancel = viewzoom_cancel;
/* flags */
ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_CURSOR_XY;
/* properties */
view3d_operator_properties_common(
ot, V3D_OP_PROP_DELTA | V3D_OP_PROP_MOUSE_CO | V3D_OP_PROP_USE_MOUSE_INIT);
}
/** \} */

View File

@ -0,0 +1,221 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spview3d
*/
#include "DNA_camera_types.h"
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "BLI_rect.h"
#include "BKE_context.h"
#include "BKE_report.h"
#include "DEG_depsgraph_query.h"
#include "WM_api.h"
#include "RNA_access.h"
#include "view3d_intern.h"
#include "view3d_navigate.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name Border Zoom Operator
* \{ */
static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
{
ARegion *region = CTX_wm_region(C);
View3D *v3d = CTX_wm_view3d(C);
RegionView3D *rv3d = CTX_wm_region_view3d(C);
const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
/* Zooms in on a border drawn by the user */
rcti rect;
float dvec[3], vb[2], xscale, yscale;
float dist_range[2];
/* SMOOTHVIEW */
float new_dist;
float new_ofs[3];
/* ZBuffer depth vars */
float depth_close = FLT_MAX;
float cent[2], p[3];
/* NOTE: otherwise opengl won't work. */
view3d_operator_needs_opengl(C);
/* get box select values using rna */
WM_operator_properties_border_to_rcti(op, &rect);
/* check if zooming in/out view */
const bool zoom_in = !RNA_boolean_get(op->ptr, "zoom_out");
ED_view3d_dist_range_get(v3d, dist_range);
ED_view3d_depth_override(
CTX_data_ensure_evaluated_depsgraph(C), region, v3d, NULL, V3D_DEPTH_NO_GPENCIL, NULL);
{
/* avoid allocating the whole depth buffer */
ViewDepths depth_temp = {0};
/* avoid view3d_update_depths() for speed. */
view3d_depths_rect_create(region, &rect, &depth_temp);
/* find the closest Z pixel */
depth_close = view3d_depth_near(&depth_temp);
MEM_SAFE_FREE(depth_temp.depths);
}
/* Resize border to the same ratio as the window. */
{
const float region_aspect = (float)region->winx / (float)region->winy;
if (((float)BLI_rcti_size_x(&rect) / (float)BLI_rcti_size_y(&rect)) < region_aspect) {
BLI_rcti_resize_x(&rect, (int)(BLI_rcti_size_y(&rect) * region_aspect));
}
else {
BLI_rcti_resize_y(&rect, (int)(BLI_rcti_size_x(&rect) / region_aspect));
}
}
cent[0] = (((float)rect.xmin) + ((float)rect.xmax)) / 2;
cent[1] = (((float)rect.ymin) + ((float)rect.ymax)) / 2;
if (rv3d->is_persp) {
float p_corner[3];
/* no depths to use, we can't do anything! */
if (depth_close == FLT_MAX) {
BKE_report(op->reports, RPT_ERROR, "Depth too large");
return OPERATOR_CANCELLED;
}
/* convert border to 3d coordinates */
if ((!ED_view3d_unproject_v3(region, cent[0], cent[1], depth_close, p)) ||
(!ED_view3d_unproject_v3(region, rect.xmin, rect.ymin, depth_close, p_corner))) {
return OPERATOR_CANCELLED;
}
sub_v3_v3v3(dvec, p, p_corner);
negate_v3_v3(new_ofs, p);
new_dist = len_v3(dvec);
/* Account for the lens, without this a narrow lens zooms in too close. */
new_dist *= (v3d->lens / DEFAULT_SENSOR_WIDTH);
/* ignore dist_range min */
dist_range[0] = v3d->clip_start * 1.5f;
}
else { /* orthographic */
/* find the current window width and height */
vb[0] = region->winx;
vb[1] = region->winy;
new_dist = rv3d->dist;
/* convert the drawn rectangle into 3d space */
if (depth_close != FLT_MAX &&
ED_view3d_unproject_v3(region, cent[0], cent[1], depth_close, p)) {
negate_v3_v3(new_ofs, p);
}
else {
float mval_f[2];
float zfac;
/* We can't use the depth, fallback to the old way that doesn't set the center depth */
copy_v3_v3(new_ofs, rv3d->ofs);
{
float tvec[3];
negate_v3_v3(tvec, new_ofs);
zfac = ED_view3d_calc_zfac(rv3d, tvec, NULL);
}
mval_f[0] = (rect.xmin + rect.xmax - vb[0]) / 2.0f;
mval_f[1] = (rect.ymin + rect.ymax - vb[1]) / 2.0f;
ED_view3d_win_to_delta(region, mval_f, dvec, zfac);
/* center the view to the center of the rectangle */
sub_v3_v3(new_ofs, dvec);
}
/* work out the ratios, so that everything selected fits when we zoom */
xscale = (BLI_rcti_size_x(&rect) / vb[0]);
yscale = (BLI_rcti_size_y(&rect) / vb[1]);
new_dist *= max_ff(xscale, yscale);
}
if (!zoom_in) {
sub_v3_v3v3(dvec, new_ofs, rv3d->ofs);
new_dist = rv3d->dist * (rv3d->dist / new_dist);
add_v3_v3v3(new_ofs, rv3d->ofs, dvec);
}
/* clamp after because we may have been zooming out */
CLAMP(new_dist, dist_range[0], dist_range[1]);
/* TODO(campbell): 'is_camera_lock' not currently working well. */
const bool is_camera_lock = ED_view3d_camera_lock_check(v3d, rv3d);
if ((rv3d->persp == RV3D_CAMOB) && (is_camera_lock == false)) {
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
ED_view3d_persp_switch_from_camera(depsgraph, v3d, rv3d, RV3D_PERSP);
}
ED_view3d_smooth_view(C,
v3d,
region,
smooth_viewtx,
&(const V3D_SmoothParams){
.ofs = new_ofs,
.dist = &new_dist,
});
if (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW) {
view3d_boxview_sync(CTX_wm_area(C), region);
}
return OPERATOR_FINISHED;
}
void VIEW3D_OT_zoom_border(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Zoom to Border";
ot->description = "Zoom in the view to the nearest object contained in the border";
ot->idname = "VIEW3D_OT_zoom_border";
/* api callbacks */
ot->invoke = WM_gesture_box_invoke;
ot->exec = view3d_zoom_border_exec;
ot->modal = WM_gesture_box_modal;
ot->cancel = WM_gesture_box_cancel;
ot->poll = view3d_zoom_or_dolly_poll;
/* flags */
ot->flag = 0;
/* properties */
WM_operator_properties_gesture_box_zoom(ot);
}
/** \} */

View File

@ -50,6 +50,7 @@
#include "ED_transform.h"
#include "view3d_intern.h"
#include "view3d_navigate.h"
#ifdef WIN32
# include "BLI_math_base.h" /* M_PI */

View File

@ -21,21 +21,14 @@
* \ingroup spview3d
*/
#include "DNA_camera_types.h"
#include "DNA_gpencil_modifier_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "MEM_guardedalloc.h"
#include "BLI_linklist.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_rect.h"
#include "BLI_utildefines.h"
#include "BKE_action.h"
#include "BKE_camera.h"
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_gpencil_modifier.h"
@ -47,7 +40,6 @@
#include "BKE_report.h"
#include "BKE_scene.h"
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
#include "UI_resources.h"
@ -57,7 +49,6 @@
#include "GPU_state.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_object.h"
#include "ED_screen.h"
@ -68,376 +59,7 @@
#include "RNA_define.h"
#include "view3d_intern.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name Smooth View Operator & Utilities
*
* Use for view transitions to have smooth (animated) transitions.
* \{ */
/* This operator is one of the 'timer refresh' ones like animation playback */
struct SmoothView3DState {
float dist;
float lens;
float quat[4];
float ofs[3];
};
struct SmoothView3DStore {
/* Source. */
struct SmoothView3DState src; /* source */
struct SmoothView3DState dst; /* destination */
struct SmoothView3DState org; /* original */
bool to_camera;
bool use_dyn_ofs;
float dyn_ofs[3];
/* When smooth-view is enabled, store the 'rv3d->view' here,
* assign back when the view motion is completed. */
char org_view;
double time_allowed;
};
static void view3d_smooth_view_state_backup(struct SmoothView3DState *sms_state,
const View3D *v3d,
const RegionView3D *rv3d)
{
copy_v3_v3(sms_state->ofs, rv3d->ofs);
copy_qt_qt(sms_state->quat, rv3d->viewquat);
sms_state->dist = rv3d->dist;
sms_state->lens = v3d->lens;
}
static void view3d_smooth_view_state_restore(const struct SmoothView3DState *sms_state,
View3D *v3d,
RegionView3D *rv3d)
{
copy_v3_v3(rv3d->ofs, sms_state->ofs);
copy_qt_qt(rv3d->viewquat, sms_state->quat);
rv3d->dist = sms_state->dist;
v3d->lens = sms_state->lens;
}
/* will start timer if appropriate */
void ED_view3d_smooth_view_ex(
/* avoid passing in the context */
const Depsgraph *depsgraph,
wmWindowManager *wm,
wmWindow *win,
ScrArea *area,
View3D *v3d,
ARegion *region,
const int smooth_viewtx,
const V3D_SmoothParams *sview)
{
RegionView3D *rv3d = region->regiondata;
struct SmoothView3DStore sms = {{0}};
/* initialize sms */
view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d);
view3d_smooth_view_state_backup(&sms.src, v3d, rv3d);
/* If smooth-view runs multiple times. */
if (rv3d->sms == NULL) {
view3d_smooth_view_state_backup(&sms.org, v3d, rv3d);
}
else {
sms.org = rv3d->sms->org;
}
sms.org_view = rv3d->view;
/* sms.to_camera = false; */ /* initialized to zero anyway */
/* note on camera locking, this is a little confusing but works ok.
* we may be changing the view 'as if' there is no active camera, but in fact
* there is an active camera which is locked to the view.
*
* In the case where smooth view is moving _to_ a camera we don't want that
* camera to be moved or changed, so only when the camera is not being set should
* we allow camera option locking to initialize the view settings from the camera.
*/
if (sview->camera == NULL && sview->camera_old == NULL) {
ED_view3d_camera_lock_init(depsgraph, v3d, rv3d);
}
/* store the options we want to end with */
if (sview->ofs) {
copy_v3_v3(sms.dst.ofs, sview->ofs);
}
if (sview->quat) {
copy_qt_qt(sms.dst.quat, sview->quat);
}
if (sview->dist) {
sms.dst.dist = *sview->dist;
}
if (sview->lens) {
sms.dst.lens = *sview->lens;
}
if (sview->dyn_ofs) {
BLI_assert(sview->ofs == NULL);
BLI_assert(sview->quat != NULL);
copy_v3_v3(sms.dyn_ofs, sview->dyn_ofs);
sms.use_dyn_ofs = true;
/* calculate the final destination offset */
view3d_orbit_apply_dyn_ofs(sms.dst.ofs, sms.src.ofs, sms.src.quat, sms.dst.quat, sms.dyn_ofs);
}
if (sview->camera) {
Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, sview->camera);
if (sview->ofs != NULL) {
sms.dst.dist = ED_view3d_offset_distance(
ob_camera_eval->obmat, sview->ofs, VIEW3D_DIST_FALLBACK);
}
ED_view3d_from_object(ob_camera_eval, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens);
sms.to_camera = true; /* restore view3d values in end */
}
if ((sview->camera_old == sview->camera) && /* Camera. */
(sms.dst.dist == rv3d->dist) && /* Distance. */
(sms.dst.lens == v3d->lens) && /* Lens. */
equals_v3v3(sms.dst.ofs, rv3d->ofs) && /* Offset. */
equals_v4v4(sms.dst.quat, rv3d->viewquat) /* Rotation. */
) {
/* Early return if nothing changed. */
return;
}
/* Skip smooth viewing for external render engine draw. */
if (smooth_viewtx && !(v3d->shading.type == OB_RENDER && rv3d->render_engine)) {
/* original values */
if (sview->camera_old) {
Object *ob_camera_old_eval = DEG_get_evaluated_object(depsgraph, sview->camera_old);
if (sview->ofs != NULL) {
sms.src.dist = ED_view3d_offset_distance(ob_camera_old_eval->obmat, sview->ofs, 0.0f);
}
ED_view3d_from_object(
ob_camera_old_eval, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens);
}
/* grid draw as floor */
if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0) {
/* use existing if exists, means multiple calls to smooth view
* won't lose the original 'view' setting */
rv3d->view = RV3D_VIEW_USER;
}
sms.time_allowed = (double)smooth_viewtx / 1000.0;
/* If this is view rotation only we can decrease the time allowed by the angle between quats
* this means small rotations won't lag. */
if (sview->quat && !sview->ofs && !sview->dist) {
/* scale the time allowed by the rotation */
/* 180deg == 1.0 */
sms.time_allowed *= (double)fabsf(angle_signed_normalized_qtqt(sms.dst.quat, sms.src.quat)) /
M_PI;
}
/* ensure it shows correct */
if (sms.to_camera) {
/* use ortho if we move from an ortho view to an ortho camera */
Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, sview->camera);
rv3d->persp = (((rv3d->is_persp == false) && (ob_camera_eval->type == OB_CAMERA) &&
(((Camera *)ob_camera_eval->data)->type == CAM_ORTHO)) ?
RV3D_ORTHO :
RV3D_PERSP);
}
rv3d->rflag |= RV3D_NAVIGATING;
/* not essential but in some cases the caller will tag the area for redraw, and in that
* case we can get a flicker of the 'org' user view but we want to see 'src' */
view3d_smooth_view_state_restore(&sms.src, v3d, rv3d);
/* keep track of running timer! */
if (rv3d->sms == NULL) {
rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d");
}
*rv3d->sms = sms;
if (rv3d->smooth_timer) {
WM_event_remove_timer(wm, win, rv3d->smooth_timer);
}
/* #TIMER1 is hard-coded in key-map. */
rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0);
}
else {
/* Animation is disabled, apply immediately. */
if (sms.to_camera == false) {
copy_v3_v3(rv3d->ofs, sms.dst.ofs);
copy_qt_qt(rv3d->viewquat, sms.dst.quat);
rv3d->dist = sms.dst.dist;
v3d->lens = sms.dst.lens;
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
}
if (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW) {
view3d_boxview_copy(area, region);
}
ED_region_tag_redraw(region);
WM_event_add_mousemove(win);
}
}
void ED_view3d_smooth_view(bContext *C,
View3D *v3d,
ARegion *region,
const int smooth_viewtx,
const struct V3D_SmoothParams *sview)
{
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
wmWindowManager *wm = CTX_wm_manager(C);
wmWindow *win = CTX_wm_window(C);
ScrArea *area = CTX_wm_area(C);
ED_view3d_smooth_view_ex(depsgraph, wm, win, area, v3d, region, smooth_viewtx, sview);
}
/* only meant for timer usage */
static void view3d_smoothview_apply(bContext *C, View3D *v3d, ARegion *region, bool sync_boxview)
{
wmWindowManager *wm = CTX_wm_manager(C);
RegionView3D *rv3d = region->regiondata;
struct SmoothView3DStore *sms = rv3d->sms;
float step, step_inv;
if (sms->time_allowed != 0.0) {
step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed);
}
else {
step = 1.0f;
}
/* end timer */
if (step >= 1.0f) {
wmWindow *win = CTX_wm_window(C);
/* if we went to camera, store the original */
if (sms->to_camera) {
rv3d->persp = RV3D_CAMOB;
view3d_smooth_view_state_restore(&sms->org, v3d, rv3d);
}
else {
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d);
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
}
if ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0) {
rv3d->view = sms->org_view;
}
MEM_freeN(rv3d->sms);
rv3d->sms = NULL;
WM_event_remove_timer(wm, win, rv3d->smooth_timer);
rv3d->smooth_timer = NULL;
rv3d->rflag &= ~RV3D_NAVIGATING;
/* Event handling won't know if a UI item has been moved under the pointer. */
WM_event_add_mousemove(win);
}
else {
/* ease in/out */
step = (3.0f * step * step - 2.0f * step * step * step);
step_inv = 1.0f - step;
interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step);
if (sms->use_dyn_ofs) {
view3d_orbit_apply_dyn_ofs(
rv3d->ofs, sms->src.ofs, sms->src.quat, rv3d->viewquat, sms->dyn_ofs);
}
else {
interp_v3_v3v3(rv3d->ofs, sms->src.ofs, sms->dst.ofs, step);
}
rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv;
v3d->lens = sms->dst.lens * step + sms->src.lens * step_inv;
const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
if (ED_screen_animation_playing(wm)) {
ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
}
}
if (sync_boxview && (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW)) {
view3d_boxview_copy(CTX_wm_area(C), region);
}
/* NOTE: this doesn't work right because the v3d->lens is now used in ortho mode r51636,
* when switching camera in quad-view the other ortho views would zoom & reset.
*
* For now only redraw all regions when smooth-view finishes.
*/
if (step >= 1.0f) {
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
}
else {
ED_region_tag_redraw(region);
}
}
static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
{
View3D *v3d = CTX_wm_view3d(C);
ARegion *region = CTX_wm_region(C);
RegionView3D *rv3d = region->regiondata;
/* escape if not our timer */
if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata) {
return OPERATOR_PASS_THROUGH;
}
view3d_smoothview_apply(C, v3d, region, true);
return OPERATOR_FINISHED;
}
void ED_view3d_smooth_view_force_finish(bContext *C, View3D *v3d, ARegion *region)
{
RegionView3D *rv3d = region->regiondata;
if (rv3d && rv3d->sms) {
rv3d->sms->time_allowed = 0.0; /* force finishing */
view3d_smoothview_apply(C, v3d, region, false);
/* force update of view matrix so tools that run immediately after
* can use them without redrawing first */
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Scene *scene = CTX_data_scene(C);
ED_view3d_update_viewmat(depsgraph, scene, v3d, region, NULL, NULL, NULL, false);
}
}
void VIEW3D_OT_smoothview(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Smooth View";
ot->idname = "VIEW3D_OT_smoothview";
/* api callbacks */
ot->invoke = view3d_smoothview_invoke;
/* flags */
ot->flag = OPTYPE_INTERNAL;
ot->poll = ED_operator_view3d_active;
}
/** \} */
#include "view3d_navigate.h"
/* -------------------------------------------------------------------- */
/** \name Camera to View Operator