Blender V5.3
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.hh"
11#include "BLI_math_vector_c.hh"
12#include "BLI_rect.hh"
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
26namespace blender {
27
28/* -------------------------------------------------------------------- */
31
37static void view_roll_angle(ARegion *region,
38 float quat[4],
39 const float orig_quat[4],
40 const float dvec[3],
41 float angle,
42 bool use_axis_view)
43{
44 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
45 float quat_mul[4];
46
47 /* camera axis */
48 axis_angle_normalized_to_quat(quat_mul, dvec, angle);
49
50 mul_qt_qtqt(quat, orig_quat, quat_mul);
51
52 /* avoid precision loss over time */
53 normalize_qt(quat);
54
55 if (use_axis_view && RV3D_VIEW_IS_AXIS(rv3d->view) && (fabsf(angle) == float(M_PI_2))) {
57 }
58 else {
59 rv3d->view = RV3D_VIEW_USER;
60 }
61}
62
63static void viewroll_apply(ViewOpsData *vod, int x, int y)
64{
65 const float current_position[2] = {float(x), float(y)};
66 float angle = BLI_dial_angle(vod->init.dial, current_position);
67
68 if (vod->rv3d->persp == RV3D_CAMOB && (vod->rv3d->rflag & RV3D_FLIP_X) != 0) {
69 angle = -angle;
70 }
71
72 if (angle != 0.0f) {
74 vod->region, vod->rv3d->viewquat, vod->init.quat, vod->init.mousevec, angle, false);
75
76 if (vod->rv3d->persp == RV3D_CAMOB) {
77 const bool is_camera_lock = ED_view3d_camera_lock_check(vod->v3d, vod->rv3d);
78 if (!is_camera_lock) {
79 /* Wrap to prevent poor precision at large values. */
80 vod->rv3d->camroll = angle_wrap_rad(vod->init.camroll - angle);
81 }
82 }
83 }
84
85 if (vod->use_dyn_ofs) {
87 vod->rv3d->ofs, vod->init.ofs, vod->init.quat, vod->rv3d->viewquat, vod->dyn_ofs);
88 }
89
90 if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_BOXVIEW) {
92 }
93
95
97}
98
100{
101 ViewOpsData *vod = static_cast<ViewOpsData *>(op->customdata);
102 short event_code = VIEW_PASS;
103 bool use_autokey = false;
105
106 /* Execute the events. */
107 if (event->type == EVT_MODAL_MAP) {
108 switch (event->val) {
110 event_code = VIEW_CONFIRM;
111 break;
113 event_code = VIEW_CANCEL;
114 break;
117 C, "VIEW3D_OT_move", wm::OpCallContext::InvokeDefault, nullptr, event);
118 event_code = VIEW_CONFIRM;
119 break;
122 C, "VIEW3D_OT_rotate", wm::OpCallContext::InvokeDefault, nullptr, event);
123 event_code = VIEW_CONFIRM;
124 break;
125 }
126 }
127 else {
128 if (event->type == MOUSEMOVE) {
129 event_code = VIEW_APPLY;
130 }
131 else if (event->type == vod->init.event_type) {
132 /* Check `vod->init.event_type` first in case RMB was used to invoke.
133 * in this case confirming takes precedence over canceling, see: #102937. */
134 if (event->val == KM_RELEASE) {
135 event_code = VIEW_CONFIRM;
136 }
137 }
138 else if (event->type == EVT_ESCKEY) {
139 if (event->val == KM_PRESS) {
140 event_code = VIEW_CANCEL;
141 }
142 }
143 }
144
145 switch (event_code) {
146 case VIEW_APPLY: {
147 viewroll_apply(vod, event->xy[0], event->xy[1]);
149 use_autokey = true;
150 }
151 break;
152 }
153 case VIEW_CONFIRM: {
154 use_autokey = true;
156 break;
157 }
158 case VIEW_CANCEL: {
159 vod->state_restore();
161 break;
162 }
163 }
164
165 if (use_autokey) {
166 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, false);
167 }
168
169 if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
170 viewops_data_free(C, static_cast<ViewOpsData *>(op->customdata));
171 op->customdata = nullptr;
172 }
173
174 return ret;
175}
176
177enum {
180};
181
183 {0, "ANGLE", 0, "Roll Angle", "Roll the view using an angle value"},
184 {V3D_VIEW_STEPLEFT, "LEFT", 0, "Roll Left", "Roll the view around to the left"},
185 {V3D_VIEW_STEPRIGHT, "RIGHT", 0, "Roll Right", "Roll the view around to the right"},
186 {0, nullptr, 0, nullptr, nullptr},
187};
188
190{
191 ViewOpsData *vod;
192 if (op->customdata) {
193 vod = static_cast<ViewOpsData *>(op->customdata);
194 }
195 else {
196 vod = new ViewOpsData();
198 vod->rv3d = static_cast<RegionView3D *>(vod->region->regiondata);
199 }
200
202
203 if (vod->depsgraph == nullptr) {
205 vod->init_navigation(C, nullptr, &ViewOpsType_roll);
206 }
207
208 int type = RNA_enum_get(op->ptr, "type");
209 float angle = (type == 0) ? RNA_float_get(op->ptr, "angle") : DEG2RADF(U.pad_rot_angle);
210 float mousevec[3];
211 float quat_new[4];
212
213 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
214
215 if (type == V3D_VIEW_STEPLEFT) {
216 angle = -angle;
217 }
218
219 if (vod->rv3d->persp == RV3D_CAMOB && (vod->rv3d->rflag & RV3D_FLIP_X) != 0) {
220 angle = -angle;
221 }
222
223 normalize_v3_v3(mousevec, vod->rv3d->viewinv[2]);
224 negate_v3(mousevec);
225 view_roll_angle(vod->region, quat_new, vod->rv3d->viewquat, mousevec, angle, true);
226
227 if (vod->rv3d->persp == RV3D_CAMOB) {
228 const bool is_camera_lock = ED_view3d_camera_lock_check(vod->v3d, vod->rv3d);
229 if (!is_camera_lock) {
230 /* Wrap to prevent poor precision at large values. */
231 vod->rv3d->camroll = angle_wrap_rad(vod->init.camroll - angle);
232 }
233 }
234
235 V3D_SmoothParams sview_params = {};
236 sview_params.quat = quat_new;
237 /* Group as successive roll may run by holding a key. */
238 sview_params.undo_str = op->type->name;
239 sview_params.undo_grouped = true;
240
241 if (vod->use_dyn_ofs) {
242 sview_params.dyn_ofs = vod->dyn_ofs;
243 }
244
245 ED_view3d_smooth_view(C, vod->v3d, vod->region, smooth_viewtx, &sview_params);
246
247 viewops_data_free(C, vod);
248 op->customdata = nullptr;
249 return OPERATOR_FINISHED;
250}
251
253{
254 ViewOpsData *vod;
255
256 bool use_angle = RNA_enum_get(op->ptr, "type") != 0;
257
258 if (use_angle || RNA_struct_property_is_set(op->ptr, "angle")) {
259 viewroll_exec(C, op);
260 }
261 else {
262 /* The equivalent functionality for orbiting the view: #VIEW3D_OT_orbit & #VIEW3D_OT_rotate are
263 * separate operators with different poll functions [which are only permissive for non-locked
264 * views]. This operator however mixes modal-interaction & instant-stepping into the same
265 * operator and its current poll function permissively finds the non-locked region in quad
266 * view. For instant-stepping into the operator via keyboard shortcuts, forwarding to the
267 * non-locked view (when in a locked view) makes sense, but modal-interaction with the locked
268 * view forwarding to a different view doesn't (hence the check). */
269 {
270 ARegion *region = CTX_wm_region(C);
271 if (region->regiontype == RGN_TYPE_WINDOW) {
272 const RegionView3D *rv3d = static_cast<const RegionView3D *>(region->regiondata);
273 if (rv3d->viewlock & RV3D_LOCK_ROTATION) {
275 }
276 }
277 }
278
279 /* makes op->customdata */
280 vod = viewops_data_create(C, event, &ViewOpsType_roll, false);
281
282 const float start_position[2] = {float(BLI_rcti_cent_x(&vod->region->winrct)),
284 vod->init.dial = BLI_dial_init(start_position, FLT_EPSILON);
285 op->customdata = vod;
286
288
289 /* overwrite the mouse vector with the view direction */
290 normalize_v3_v3(vod->init.mousevec, vod->rv3d->viewinv[2]);
291 negate_v3(vod->init.mousevec);
292
293 if (event->type == MOUSEROTATE) {
294 vod->init.event_xy[0] = vod->prev.event_xy[0] = event->xy[0];
295 viewroll_apply(vod, event->prev_xy[0], event->prev_xy[1]);
296
297 viewops_data_free(C, static_cast<ViewOpsData *>(op->customdata));
298 op->customdata = nullptr;
299 return OPERATOR_FINISHED;
300 }
301
302 /* add temp handler */
305 }
306 return OPERATOR_FINISHED;
307}
308
310{
311 PropertyRNA *prop;
312
313 /* identifiers */
314 ot->name = "View Roll";
315 ot->description = "Roll the view";
317
318 /* API callbacks. */
320 ot->exec = viewroll_exec;
321 ot->modal = viewroll_modal;
324
325 /* flags */
326 ot->flag = 0;
327
328 /* properties */
329 ot->prop = prop = RNA_def_float(
330 ot->srna, "angle", 0, -FLT_MAX, FLT_MAX, "Roll", "", -FLT_MAX, FLT_MAX);
332 prop = RNA_def_enum(ot->srna,
333 "type",
335 0,
336 "Roll Angle Source",
337 "How roll angle is calculated");
339}
340
342
344 /*flag*/ (VIEWOPS_FLAG_ORBIT_SELECT),
345 /*idname*/ "VIEW3D_OT_view_roll",
346 /*poll_fn*/ nullptr,
347 /*init_fn*/ nullptr,
348 /*apply_fn*/ nullptr,
349};
350
351} // namespace blender
#define DEG2RADF(_deg)
#define M_PI_2
#define RV3D_VIEW_IS_AXIS(view)
#define RV3D_LOCK_FLAGS(rv3d)
#define C
Definition RandGen.cpp:29
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define FLT_MAX
#define FLT_EPSILON
BMesh Iterators.
const ViewOpsType ViewOpsType_roll
bool ED_view3d_context_user_region(bContext *C, View3D **r_v3d, ARegion **r_region)
bool ED_view3d_camera_lock_autokey(View3D *v3d, RegionView3D *rv3d, bContext *C, bool do_rotate, bool do_translate)
UserDef U
Definition blender.cc:58
bool ED_view3d_quat_to_axis_view_and_reset_quat(float quat[4], float epsilon, eRegionView3D_View *r_view, eRegionView3D_ViewAxisRoll *r_view_axis_roll)
bool ED_view3d_camera_lock_check(const View3D *v3d, const RegionView3D *rv3d)
int RNA_enum_get(PointerRNA *ptr, const char *name)
bool ED_operator_rv3d_user_region_poll(bContext *C)
void viewops_data_free(bContext *C, ViewOpsData *vod)
void axis_angle_normalized_to_quat(float r[4], const float axis[3], float angle)
ViewOpsData * viewops_data_create(bContext *C, const wmEvent *event, const ViewOpsType *nav_type, const bool use_cursor_init)
float normalize_qt(float q[4])
bool ED_view3d_camera_lock_sync(const Depsgraph *depsgraph, View3D *v3d, RegionView3D *rv3d)
int WM_operator_smooth_viewtx_get(const wmOperator *op)
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 mul_qt_qtqt(float q[4], const float a[4], const float b[4])
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
BLI_INLINE int BLI_rcti_cent_y(const struct rcti *rct)
Definition BLI_rect.hh:190
static void viewroll_apply(ViewOpsData *vod, int x, int y)
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
MINLINE void negate_v3(float r[3])
@ VIEW_CONFIRM
Definition image_ops.cc:652
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
@ VIEWOPS_FLAG_ORBIT_SELECT
MINLINE float normalize_v3_v3(float r[3], const float a[3])
static wmOperatorStatus viewroll_invoke(bContext *C, wmOperator *op, const wmEvent *event)
@ VIEWROT_MODAL_SWITCH_MOVE
@ VIEWROT_MODAL_SWITCH_ROTATE
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void view3d_navigate_cancel_fn(bContext *C, wmOperator *op)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
void ED_view3d_smooth_view_force_finish(bContext *C, View3D *v3d, ARegion *region)
Dial * BLI_dial_init(const float start_position[2], float threshold)
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])
@ KM_RELEASE
Definition WM_types.hh:325
static wmOperatorStatus viewroll_modal(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus viewroll_exec(bContext *C, wmOperator *op)
void VIEW3D_OT_view_roll(wmOperatorType *ot)
float BLI_dial_angle(Dial *dial, const float current_position[2])
BLI_INLINE int BLI_rcti_cent_x(const struct rcti *rct)
Definition BLI_rect.hh:186
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)
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)
ARegion * CTX_wm_region(const bContext *C)
void ED_view3d_smooth_view(bContext *C, View3D *v3d, ARegion *region, int smooth_viewtx, const V3D_SmoothParams *sview)
void view3d_boxview_sync(ScrArea *area, ARegion *region)
float RNA_float_get(PointerRNA *ptr, const char *name)
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
static const EnumPropertyItem prop_view_roll_items[]
float angle_wrap_rad(float angle)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegion_Type regiontype
eRegionView3D_Flag rflag
eRegionView3D_ViewAxisRoll view_axis_roll
eRegionView3D_Persp persp
eRegionView3D_ViewLock viewlock
eRegionView3D_View view
struct blender::ViewOpsData::@013213354235237006300365026145043140112021271057 prev
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 blender::ViewOpsData::@130157306301264131203124134151333300254160116071 init
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct wmOperatorType * type