Blender V5.0
view3d_navigate_view_roll.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_dial_2d.h"
10#include "BLI_math_rotation.h"
11#include "BLI_math_vector.h"
12#include "BLI_rect.h"
13
14#include "BKE_context.hh"
15
16#include "WM_api.hh"
17
18#include "RNA_access.hh"
19#include "RNA_define.hh"
20
21#include "ED_screen.hh"
22
23#include "view3d_intern.hh"
24#include "view3d_navigate.hh" /* own include */
25
26/* -------------------------------------------------------------------- */
29
35static void view_roll_angle(ARegion *region,
36 float quat[4],
37 const float orig_quat[4],
38 const float dvec[3],
39 float angle,
40 bool use_axis_view)
41{
42 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
43 float quat_mul[4];
44
45 /* camera axis */
46 axis_angle_normalized_to_quat(quat_mul, dvec, angle);
47
48 mul_qt_qtqt(quat, orig_quat, quat_mul);
49
50 /* avoid precision loss over time */
51 normalize_qt(quat);
52
53 if (use_axis_view && RV3D_VIEW_IS_AXIS(rv3d->view) && (fabsf(angle) == float(M_PI_2))) {
55 }
56 else {
57 rv3d->view = RV3D_VIEW_USER;
58 }
59}
60
61static void viewroll_apply(ViewOpsData *vod, int x, int y)
62{
63 const float current_position[2] = {float(x), float(y)};
64 float angle = BLI_dial_angle(vod->init.dial, current_position);
65
66 if (angle != 0.0f) {
68 vod->region, vod->rv3d->viewquat, vod->init.quat, vod->init.mousevec, angle, false);
69 }
70
71 if (vod->use_dyn_ofs) {
73 vod->rv3d->ofs, vod->init.ofs, vod->init.quat, vod->rv3d->viewquat, vod->dyn_ofs);
74 }
75
76 if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_BOXVIEW) {
78 }
79
81
83}
84
86{
87 ViewOpsData *vod = static_cast<ViewOpsData *>(op->customdata);
88 short event_code = VIEW_PASS;
89 bool use_autokey = false;
91
92 /* Execute the events. */
93 if (event->type == EVT_MODAL_MAP) {
94 switch (event->val) {
96 event_code = VIEW_CONFIRM;
97 break;
99 event_code = VIEW_CANCEL;
100 break;
103 C, "VIEW3D_OT_move", blender::wm::OpCallContext::InvokeDefault, nullptr, event);
104 event_code = VIEW_CONFIRM;
105 break;
108 C, "VIEW3D_OT_rotate", blender::wm::OpCallContext::InvokeDefault, nullptr, event);
109 event_code = VIEW_CONFIRM;
110 break;
111 }
112 }
113 else {
114 if (event->type == MOUSEMOVE) {
115 event_code = VIEW_APPLY;
116 }
117 else if (event->type == vod->init.event_type) {
118 /* Check `vod->init.event_type` first in case RMB was used to invoke.
119 * in this case confirming takes precedence over canceling, see: #102937. */
120 if (event->val == KM_RELEASE) {
121 event_code = VIEW_CONFIRM;
122 }
123 }
124 else if (event->type == EVT_ESCKEY) {
125 if (event->val == KM_PRESS) {
126 event_code = VIEW_CANCEL;
127 }
128 }
129 }
130
131 switch (event_code) {
132 case VIEW_APPLY: {
133 viewroll_apply(vod, event->xy[0], event->xy[1]);
135 use_autokey = true;
136 }
137 break;
138 }
139 case VIEW_CONFIRM: {
140 use_autokey = true;
142 break;
143 }
144 case VIEW_CANCEL: {
145 vod->state_restore();
147 break;
148 }
149 }
150
151 if (use_autokey) {
152 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, false);
153 }
154
155 if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
156 viewops_data_free(C, static_cast<ViewOpsData *>(op->customdata));
157 op->customdata = nullptr;
158 }
159
160 return ret;
161}
162
163enum {
166};
167
169 {0, "ANGLE", 0, "Roll Angle", "Roll the view using an angle value"},
170 {V3D_VIEW_STEPLEFT, "LEFT", 0, "Roll Left", "Roll the view around to the left"},
171 {V3D_VIEW_STEPRIGHT, "RIGHT", 0, "Roll Right", "Roll the view around to the right"},
172 {0, nullptr, 0, nullptr, nullptr},
173};
174
176{
177 ViewOpsData *vod;
178 if (op->customdata) {
179 vod = static_cast<ViewOpsData *>(op->customdata);
180 }
181 else {
182 vod = new ViewOpsData();
184 vod->rv3d = static_cast<RegionView3D *>(vod->region->regiondata);
185 }
186
188
189 const bool is_camera_lock = ED_view3d_camera_lock_check(vod->v3d, vod->rv3d);
190 if (vod->rv3d->persp == RV3D_CAMOB && !is_camera_lock) {
191 viewops_data_free(C, vod);
192 op->customdata = nullptr;
193 return OPERATOR_CANCELLED;
194 }
195
196 if (vod->depsgraph == nullptr) {
198 vod->init_navigation(C, nullptr, &ViewOpsType_roll);
199 }
200
201 int type = RNA_enum_get(op->ptr, "type");
202 float angle = (type == 0) ? RNA_float_get(op->ptr, "angle") : DEG2RADF(U.pad_rot_angle);
203 float mousevec[3];
204 float quat_new[4];
205
206 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
207
208 if (type == V3D_VIEW_STEPLEFT) {
209 angle = -angle;
210 }
211
212 normalize_v3_v3(mousevec, vod->rv3d->viewinv[2]);
213 negate_v3(mousevec);
214 view_roll_angle(vod->region, quat_new, vod->rv3d->viewquat, mousevec, angle, true);
215
216 V3D_SmoothParams sview_params = {};
217 sview_params.quat = quat_new;
218 /* Group as successive roll may run by holding a key. */
219 sview_params.undo_str = op->type->name;
220 sview_params.undo_grouped = true;
221
222 if (vod->use_dyn_ofs) {
223 sview_params.dyn_ofs = vod->dyn_ofs;
224 }
225
226 ED_view3d_smooth_view(C, vod->v3d, vod->region, smooth_viewtx, &sview_params);
227
228 viewops_data_free(C, vod);
229 op->customdata = nullptr;
230 return OPERATOR_FINISHED;
231}
232
234{
235 ViewOpsData *vod;
236
237 bool use_angle = RNA_enum_get(op->ptr, "type") != 0;
238
239 if (use_angle || RNA_struct_property_is_set(op->ptr, "angle")) {
240 viewroll_exec(C, op);
241 }
242 else {
243 /* The equivalent functionality for orbiting the view: #VIEW3D_OT_orbit & #VIEW3D_OT_rotate are
244 * separate operators with different poll functions [which are only permissive for non-locked
245 * views]. This operator however mixes modal-interaction & instant-stepping into the same
246 * operator and its current poll function permissively finds the non-locked region in quad
247 * view. For instant-stepping into the operator via keyboard shortcuts, forwarding to the
248 * non-locked view (when in a locked view) makes sense, but modal-interaction with the locked
249 * view forwarding to a different view doesn't (hence the check). */
250 {
251 ARegion *region = CTX_wm_region(C);
252 if (region->regiontype == RGN_TYPE_WINDOW) {
253 const RegionView3D *rv3d = static_cast<const RegionView3D *>(region->regiondata);
254 if (rv3d->viewlock & RV3D_LOCK_ROTATION) {
256 }
257 }
258 }
259
260 /* makes op->customdata */
261 vod = viewops_data_create(C, event, &ViewOpsType_roll, false);
262
263 const float start_position[2] = {float(BLI_rcti_cent_x(&vod->region->winrct)),
265 vod->init.dial = BLI_dial_init(start_position, FLT_EPSILON);
266 op->customdata = vod;
267
269
270 /* overwrite the mouse vector with the view direction */
271 normalize_v3_v3(vod->init.mousevec, vod->rv3d->viewinv[2]);
272 negate_v3(vod->init.mousevec);
273
274 if (event->type == MOUSEROTATE) {
275 vod->init.event_xy[0] = vod->prev.event_xy[0] = event->xy[0];
276 viewroll_apply(vod, event->prev_xy[0], event->prev_xy[1]);
277
278 viewops_data_free(C, static_cast<ViewOpsData *>(op->customdata));
279 op->customdata = nullptr;
280 return OPERATOR_FINISHED;
281 }
282
283 /* add temp handler */
286 }
287 return OPERATOR_FINISHED;
288}
289
291{
292 PropertyRNA *prop;
293
294 /* identifiers */
295 ot->name = "View Roll";
296 ot->description = "Roll the view";
297 ot->idname = ViewOpsType_roll.idname;
298
299 /* API callbacks. */
300 ot->invoke = viewroll_invoke;
301 ot->exec = viewroll_exec;
302 ot->modal = viewroll_modal;
305
306 /* flags */
307 ot->flag = 0;
308
309 /* properties */
310 ot->prop = prop = RNA_def_float(
311 ot->srna, "angle", 0, -FLT_MAX, FLT_MAX, "Roll", "", -FLT_MAX, FLT_MAX);
313 prop = RNA_def_enum(ot->srna,
314 "type",
316 0,
317 "Roll Angle Source",
318 "How roll angle is calculated");
320}
321
323
325 /*flag*/ (VIEWOPS_FLAG_ORBIT_SELECT),
326 /*idname*/ "VIEW3D_OT_view_roll",
327 /*poll_fn*/ nullptr,
328 /*init_fn*/ nullptr,
329 /*apply_fn*/ nullptr,
330};
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
Dial * BLI_dial_init(const float start_position[2], float threshold)
float BLI_dial_angle(Dial *dial, const float current_position[2])
#define DEG2RADF(_deg)
#define M_PI_2
float normalize_qt(float q[4])
void axis_angle_normalized_to_quat(float r[4], const float axis[3], float angle)
void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
MINLINE void negate_v3(float r[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
BLI_INLINE int BLI_rcti_cent_y(const struct rcti *rct)
Definition BLI_rect.h:181
BLI_INLINE int BLI_rcti_cent_x(const struct rcti *rct)
Definition BLI_rect.h:177
@ RGN_TYPE_WINDOW
#define RV3D_VIEW_IS_AXIS(view)
@ RV3D_VIEW_USER
#define RV3D_LOCK_FLAGS(rv3d)
@ RV3D_CAMOB
@ RV3D_LOCK_ROTATION
@ RV3D_BOXVIEW
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
@ OPERATOR_PASS_THROUGH
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:618
bool ED_view3d_camera_lock_sync(const Depsgraph *depsgraph, View3D *v3d, RegionView3D *rv3d)
bool ED_view3d_context_user_region(bContext *C, View3D **r_v3d, ARegion **r_region)
bool ED_view3d_quat_to_axis_view_and_reset_quat(float quat[4], float epsilon, char *r_view, char *r_view_axis_roll)
bool ED_view3d_camera_lock_check(const View3D *v3d, const RegionView3D *rv3d)
bool ED_view3d_camera_lock_autokey(View3D *v3d, RegionView3D *rv3d, bContext *C, bool do_rotate, bool do_translate)
bool ED_operator_rv3d_user_region_poll(bContext *C)
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
@ PROP_SKIP_SAVE
Definition RNA_types.hh:344
#define C
Definition RandGen.cpp:29
@ KM_PRESS
Definition WM_types.hh:311
@ KM_RELEASE
Definition WM_types.hh:312
#define U
nullptr float
@ VIEW_CONFIRM
Definition image_ops.cc:601
@ VIEW_PASS
Definition image_ops.cc:599
@ VIEW_APPLY
Definition image_ops.cc:600
return ret
#define fabsf
float RNA_float_get(PointerRNA *ptr, const char *name)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
#define FLT_MAX
Definition stdcycles.h:14
void * regiondata
float viewinv[4][4]
const char * undo_str
const float * dyn_ofs
Depsgraph * depsgraph
blender::int2 event_xy
struct ViewOpsData::@244345216304223004112237167211144027240265370072 prev
RegionView3D * rv3d
void init_navigation(bContext *C, const wmEvent *event, const ViewOpsType *nav_type, const float dyn_ofs_override[3]=nullptr, const bool use_cursor_init=false)
struct ViewOpsData::@010113264246165170144271002355152266226235365232 init
VecBase< T, 2 > xy() const
wmEventType type
Definition WM_types.hh:757
short val
Definition WM_types.hh:759
int xy[2]
Definition WM_types.hh:761
int prev_xy[2]
Definition WM_types.hh:820
const char * name
Definition WM_types.hh:1033
struct wmOperatorType * type
struct PointerRNA * ptr
void view3d_boxview_sync(ScrArea *area, ARegion *region)
void view3d_navigate_cancel_fn(bContext *C, wmOperator *op)
void viewops_data_free(bContext *C, ViewOpsData *vod)
ViewOpsData * viewops_data_create(bContext *C, const wmEvent *event, const ViewOpsType *nav_type, const bool use_cursor_init)
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 ED_view3d_smooth_view(bContext *C, View3D *v3d, ARegion *region, int smooth_viewtx, const V3D_SmoothParams *sview)
@ VIEWOPS_FLAG_ORBIT_SELECT
@ VIEW_CANCEL
const ViewOpsType ViewOpsType_roll
void ED_view3d_smooth_view_force_finish(bContext *C, View3D *v3d, ARegion *region)
@ VIEWROT_MODAL_SWITCH_ROTATE
@ VIEW_MODAL_CANCEL
@ VIEWROT_MODAL_SWITCH_MOVE
@ VIEW_MODAL_CONFIRM
void VIEW3D_OT_view_roll(wmOperatorType *ot)
static wmOperatorStatus viewroll_modal(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus viewroll_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem prop_view_roll_items[]
static wmOperatorStatus viewroll_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void view_roll_angle(ARegion *region, float quat[4], const float orig_quat[4], const float dvec[3], float angle, bool use_axis_view)
static void viewroll_apply(ViewOpsData *vod, int x, int y)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, blender::wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
@ EVT_MODAL_MAP
@ MOUSEROTATE
@ MOUSEMOVE
@ EVT_ESCKEY
wmOperatorType * ot
Definition wm_files.cc:4237
int WM_operator_smooth_viewtx_get(const wmOperator *op)