Blender V4.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
9#include "BLI_blenlib.h"
10#include "BLI_dial_2d.h"
11#include "BLI_math_rotation.h"
12#include "BLI_math_vector.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/* -------------------------------------------------------------------- */
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
85static int viewroll_modal(bContext *C, wmOperator *op, const wmEvent *event)
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;
102 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, nullptr, event);
103 event_code = VIEW_CONFIRM;
104 break;
106 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, nullptr, event);
107 event_code = VIEW_CONFIRM;
108 break;
109 }
110 }
111 else {
112 if (event->type == MOUSEMOVE) {
113 event_code = VIEW_APPLY;
114 }
115 else if (event->type == vod->init.event_type) {
116 /* Check `vod->init.event_type` first in case RMB was used to invoke.
117 * in this case confirming takes precedence over canceling, see: #102937. */
118 if (event->val == KM_RELEASE) {
119 event_code = VIEW_CONFIRM;
120 }
121 }
122 else if (event->type == EVT_ESCKEY) {
123 if (event->val == KM_PRESS) {
124 event_code = VIEW_CANCEL;
125 }
126 }
127 }
128
129 switch (event_code) {
130 case VIEW_APPLY: {
131 viewroll_apply(vod, event->xy[0], event->xy[1]);
133 use_autokey = true;
134 }
135 break;
136 }
137 case VIEW_CONFIRM: {
138 use_autokey = true;
140 break;
141 }
142 case VIEW_CANCEL: {
143 vod->state_restore();
145 break;
146 }
147 }
148
149 if (use_autokey) {
150 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, false);
151 }
152
153 if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
154 viewops_data_free(C, static_cast<ViewOpsData *>(op->customdata));
155 op->customdata = nullptr;
156 }
157
158 return ret;
159}
160
161enum {
164};
165
167 {0, "ANGLE", 0, "Roll Angle", "Roll the view using an angle value"},
168 {V3D_VIEW_STEPLEFT, "LEFT", 0, "Roll Left", "Roll the view around to the left"},
169 {V3D_VIEW_STEPRIGHT, "RIGHT", 0, "Roll Right", "Roll the view around to the right"},
170 {0, nullptr, 0, nullptr, nullptr},
171};
172
174{
175 ViewOpsData *vod;
176 if (op->customdata) {
177 vod = static_cast<ViewOpsData *>(op->customdata);
178 }
179 else {
180 vod = new ViewOpsData();
182 vod->rv3d = static_cast<RegionView3D *>(vod->region->regiondata);
183 }
184
185 const bool is_camera_lock = ED_view3d_camera_lock_check(vod->v3d, vod->rv3d);
186 if (vod->rv3d->persp == RV3D_CAMOB && !is_camera_lock) {
187 viewops_data_free(C, vod);
188 op->customdata = nullptr;
189 return OPERATOR_CANCELLED;
190 }
191
192 if (vod->depsgraph == nullptr) {
194 vod->init_navigation(C, nullptr, &ViewOpsType_roll);
195 }
196
197 int type = RNA_enum_get(op->ptr, "type");
198 float angle = (type == 0) ? RNA_float_get(op->ptr, "angle") : DEG2RADF(U.pad_rot_angle);
199 float mousevec[3];
200 float quat_new[4];
201
202 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
203
204 if (type == V3D_VIEW_STEPLEFT) {
205 angle = -angle;
206 }
207
208 normalize_v3_v3(mousevec, vod->rv3d->viewinv[2]);
209 negate_v3(mousevec);
210 view_roll_angle(vod->region, quat_new, vod->rv3d->viewquat, mousevec, angle, true);
211
212 V3D_SmoothParams sview_params = {};
213 sview_params.quat = quat_new;
214 /* Group as successive roll may run by holding a key. */
215 sview_params.undo_str = op->type->name;
216 sview_params.undo_grouped = true;
217
218 if (vod->use_dyn_ofs) {
219 sview_params.dyn_ofs = vod->dyn_ofs;
220 }
221
222 ED_view3d_smooth_view(C, vod->v3d, vod->region, smooth_viewtx, &sview_params);
223
224 viewops_data_free(C, vod);
225 op->customdata = nullptr;
226 return OPERATOR_FINISHED;
227}
228
229static int viewroll_invoke(bContext *C, wmOperator *op, const wmEvent *event)
230{
231 ViewOpsData *vod;
232
233 bool use_angle = RNA_enum_get(op->ptr, "type") != 0;
234
235 if (use_angle || RNA_struct_property_is_set(op->ptr, "angle")) {
236 viewroll_exec(C, op);
237 }
238 else {
239 /* makes op->customdata */
240 vod = viewops_data_create(C, event, &ViewOpsType_roll, false);
241 const float start_position[2] = {float(BLI_rcti_cent_x(&vod->region->winrct)),
243 vod->init.dial = BLI_dial_init(start_position, FLT_EPSILON);
244 op->customdata = vod;
245
247
248 /* overwrite the mouse vector with the view direction */
249 normalize_v3_v3(vod->init.mousevec, vod->rv3d->viewinv[2]);
250 negate_v3(vod->init.mousevec);
251
252 if (event->type == MOUSEROTATE) {
253 vod->init.event_xy[0] = vod->prev.event_xy[0] = event->xy[0];
254 viewroll_apply(vod, event->prev_xy[0], event->prev_xy[1]);
255
256 viewops_data_free(C, static_cast<ViewOpsData *>(op->customdata));
257 op->customdata = nullptr;
258 return OPERATOR_FINISHED;
259 }
260
261 /* add temp handler */
264 }
265 return OPERATOR_FINISHED;
266}
267
269{
270 PropertyRNA *prop;
271
272 /* identifiers */
273 ot->name = "View Roll";
274 ot->description = "Roll the view";
276
277 /* api callbacks */
283
284 /* flags */
285 ot->flag = 0;
286
287 /* properties */
288 ot->prop = prop = RNA_def_float(
289 ot->srna, "angle", 0, -FLT_MAX, FLT_MAX, "Roll", "", -FLT_MAX, FLT_MAX);
291 prop = RNA_def_enum(ot->srna,
292 "type",
294 0,
295 "Roll Angle Source",
296 "How roll angle is calculated");
298}
299
303 /*flag*/ (VIEWOPS_FLAG_ORBIT_SELECT),
304 /*idname*/ "VIEW3D_OT_view_roll",
305 /*poll_fn*/ nullptr,
306 /*init_fn*/ nullptr,
307 /*apply_fn*/ nullptr,
308};
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
Dial * BLI_dial_init(const float start_position[2], float threshold)
Definition BLI_dial_2d.c:37
float BLI_dial_angle(Dial *dial, const float current_position[2])
Definition BLI_dial_2d.c:47
#define M_PI_2
float normalize_qt(float q[4])
#define DEG2RADF(_deg)
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:176
BLI_INLINE int BLI_rcti_cent_x(const struct rcti *rct)
Definition BLI_rect.h:172
#define RV3D_VIEW_IS_AXIS(view)
#define RV3D_LOCK_FLAGS(rv3d)
@ RV3D_CAMOB
@ RV3D_BOXVIEW
@ RV3D_VIEW_USER
@ OPERATOR_RUNNING_MODAL
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:634
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:125
@ PROP_SKIP_SAVE
Definition RNA_types.hh:245
@ KM_PRESS
Definition WM_types.hh:284
@ KM_RELEASE
Definition WM_types.hh:285
@ WM_OP_INVOKE_DEFAULT
Definition WM_types.hh:218
unsigned int U
Definition btGjkEpa3.h:78
#define fabsf(x)
draw_view in_light_buf[] float
@ VIEW_CONFIRM
Definition image_ops.cc:587
@ VIEW_PASS
Definition image_ops.cc:585
@ VIEW_APPLY
Definition image_ops.cc:586
return ret
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
struct ViewOpsData::@543 init
Depsgraph * depsgraph
ARegion * region
struct ViewOpsData::@544 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)
const char * idname
short val
Definition WM_types.hh:724
int xy[2]
Definition WM_types.hh:726
int prev_xy[2]
Definition WM_types.hh:785
short type
Definition WM_types.hh:722
const char * name
Definition WM_types.hh:990
bool(* poll)(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1042
const char * idname
Definition WM_types.hh:992
int(* modal)(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1036
int(* invoke)(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1022
int(* exec)(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1006
const char * description
Definition WM_types.hh:996
PropertyRNA * prop
Definition WM_types.hh:1092
StructRNA * srna
Definition WM_types.hh:1080
void(* cancel)(bContext *C, wmOperator *op)
Definition WM_types.hh:1028
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
@ VIEWROT_MODAL_SWITCH_ROTATE
@ VIEW_MODAL_CANCEL
@ VIEWROT_MODAL_SWITCH_MOVE
@ VIEW_MODAL_CONFIRM
void ED_view3d_smooth_view_force_finish(bContext *C, View3D *v3d, ARegion *region)
void VIEW3D_OT_view_roll(wmOperatorType *ot)
static const EnumPropertyItem prop_view_roll_items[]
const ViewOpsType ViewOpsType_roll
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)
static int viewroll_modal(bContext *C, wmOperator *op, const wmEvent *event)
static int viewroll_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static int viewroll_exec(bContext *C, wmOperator *op)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
int WM_operator_name_call(bContext *C, const char *opstring, wmOperatorCallContext context, PointerRNA *properties, const wmEvent *event)
@ EVT_MODAL_MAP
@ MOUSEROTATE
@ MOUSEMOVE
@ EVT_ESCKEY
wmOperatorType * ot
Definition wm_files.cc:4125
int WM_operator_smooth_viewtx_get(const wmOperator *op)