Blender V5.3
view3d_navigate_view_dolly.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 "BKE_context.hh"
10#include "BKE_report.hh"
11
12#include "BLI_math_vector_c.hh"
13
14#include "WM_api.hh"
15
16#include "RNA_access.hh"
17
18#include "ED_screen.hh"
19
20#include "view3d_intern.hh"
21#include "view3d_navigate.hh" /* own include */
22
23namespace blender {
24
25/* -------------------------------------------------------------------- */
31
33{
34 /* NOTE: This is an exact copy of #viewzoom_modal_keymap. */
35
36 static const EnumPropertyItem modal_items[] = {
37 {VIEW_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
38 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
39
40 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
41 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
42
43 {0, nullptr, 0, nullptr, nullptr},
44 };
45
46 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "View3D Dolly Modal");
47
48 /* This function is called for each space-type, only needs to add map once. */
49 if (keymap && keymap->modal_items) {
50 return;
51 }
52
53 keymap = WM_modalkeymap_ensure(keyconf, "View3D Dolly Modal", modal_items);
54
55 /* assign map to operators */
56 WM_modalkeymap_assign(keymap, "VIEW3D_OT_dolly");
57}
58
60{
61 View3D *v3d = CTX_wm_view3d(C);
63 if (ED_view3d_offset_lock_check(v3d, rv3d)) {
64 BKE_report(op->reports, RPT_WARNING, "Cannot dolly when the view offset is locked");
65 return true;
66 }
67 return false;
68}
69
70static void view_dolly_to_vector_3d(ARegion *region,
71 const float orig_ofs[3],
72 const float dvec[3],
73 float dfac)
74{
75 RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
76 madd_v3_v3v3fl(rv3d->ofs, orig_ofs, dvec, -(1.0f - dfac));
77}
78
79static void viewdolly_apply(ViewOpsData *vod, const int xy[2], const bool zoom_invert)
80{
81 float zfac = 1.0;
82
83 {
84 float len1, len2;
85
86 if (U.uiflag & USER_ZOOM_HORIZ) {
87 len1 = (vod->region->winrct.xmax - xy[0]) + 5;
88 len2 = (vod->region->winrct.xmax - vod->init.event_xy[0]) + 5;
89 }
90 else {
91 len1 = (vod->region->winrct.ymax - xy[1]) + 5;
92 len2 = (vod->region->winrct.ymax - vod->init.event_xy[1]) + 5;
93 }
94 if (zoom_invert) {
95 std::swap(len1, len2);
96 }
97
98 zfac = 1.0f + ((len1 - len2) * 0.01f * vod->rv3d->dist);
99 }
100
101 if (zfac != 1.0f) {
102 view_dolly_to_vector_3d(vod->region, vod->init.ofs, vod->init.mousevec, zfac);
103 }
104
105 if (RV3D_LOCK_FLAGS(vod->rv3d) & RV3D_BOXVIEW) {
106 view3d_boxview_sync(vod->area, vod->region);
107 }
108
110
112}
113
115{
116 ViewOpsData *vod = static_cast<ViewOpsData *>(op->customdata);
117 short event_code = VIEW_PASS;
118 bool use_autokey = false;
120
121 /* Execute the events. */
122 if (event->type == EVT_MODAL_MAP) {
123 switch (event->val) {
125 event_code = VIEW_CONFIRM;
126 break;
129 C, "VIEW3D_OT_move", wm::OpCallContext::InvokeDefault, nullptr, event);
130 event_code = VIEW_CONFIRM;
131 break;
134 C, "VIEW3D_OT_rotate", wm::OpCallContext::InvokeDefault, nullptr, event);
135 event_code = VIEW_CONFIRM;
136 break;
137 }
138 }
139 else {
140 if (event->type == MOUSEMOVE) {
141 event_code = VIEW_APPLY;
142 }
143 else if (event->type == vod->init.event_type) {
144 if (event->val == KM_RELEASE) {
145 event_code = VIEW_CONFIRM;
146 }
147 }
148 else if (event->type == EVT_ESCKEY) {
149 if (event->val == KM_PRESS) {
150 event_code = VIEW_CANCEL;
151 }
152 }
153 }
154
155 switch (event_code) {
156 case VIEW_APPLY: {
157 viewdolly_apply(vod, event->xy, (U.uiflag & USER_ZOOM_INVERT) != 0);
159 use_autokey = true;
160 }
161 break;
162 }
163 case VIEW_CONFIRM: {
164 use_autokey = true;
166 break;
167 }
168 case VIEW_CANCEL: {
169 vod->state_restore();
171 break;
172 }
173 }
174
175 if (use_autokey) {
176 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
177 }
178
179 if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
180 if (ret & OPERATOR_FINISHED) {
182 }
183 viewops_data_free(C, vod);
184 op->customdata = nullptr;
185 }
186
187 return ret;
188}
189
191{
192 View3D *v3d;
193 RegionView3D *rv3d;
194 ScrArea *area;
195 ARegion *region;
196 float mousevec[3];
197
198 const int delta = RNA_int_get(op->ptr, "delta");
199
200 if (op->customdata) {
201 ViewOpsData *vod = static_cast<ViewOpsData *>(op->customdata);
202
203 area = vod->area;
204 region = vod->region;
205 copy_v3_v3(mousevec, vod->init.mousevec);
206 }
207 else {
208 area = CTX_wm_area(C);
209 region = CTX_wm_region(C);
210 negate_v3_v3(mousevec, static_cast<RegionView3D *>(region->regiondata)->viewinv[2]);
211 normalize_v3(mousevec);
212 }
213
214 v3d = area->spacedata.first_as<View3D>();
215 rv3d = static_cast<RegionView3D *>(region->regiondata);
216
217 const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
218
219 /* overwrite the mouse vector with the view direction (zoom into the center) */
220 if ((use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)) == 0) {
221 normalize_v3_v3(mousevec, rv3d->viewinv[2]);
222 negate_v3(mousevec);
223 }
224
225 view_dolly_to_vector_3d(region, rv3d->ofs, mousevec, delta < 0 ? 1.8f : 0.2f);
226
227 if (RV3D_LOCK_FLAGS(rv3d) & RV3D_BOXVIEW) {
228 view3d_boxview_sync(area, region);
229 }
230
232
233 ED_region_tag_redraw(region);
234
235 viewops_data_free(C, static_cast<ViewOpsData *>(op->customdata));
236 op->customdata = nullptr;
237
238 return OPERATOR_FINISHED;
239}
240
242{
243 /* Near duplicate logic in #viewzoom_invoke(), changes here may apply there too. */
244
245 ViewOpsData *vod;
246
248 return OPERATOR_CANCELLED;
249 }
250
251 const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
252
253 vod = viewops_data_create(C, event, &ViewOpsType_dolly, use_cursor_init);
254 op->customdata = vod;
255
257
258 /* Rationale for enforcing a perspective projection:
259 *
260 * While translating the view center (the #RegionView3D::ofs) is possible,
261 * in most cases there is no user feedback that anything is changing,
262 * because only "panning" the view is shown in orthographic projections.
263 *
264 * From a user perspective it seems like a bug when interactive operators appear to do nothing,
265 * so force a perspective view.
266 *
267 * There are some exceptions where users would notice (mentioning for completeness),
268 * but they're obscure enough for the logic to stay as-is.
269 *
270 * - With a small far-clip plane "dolly" may move contents in/out of the visible clipping range.
271 * - With quad-view and "Sync Zoom/Pan" enabled, "dolly" will be visible other views.
272 * We could even make an exception for this and allow dolly however even in this case
273 * the user might as well pan the other views directly.
274 *
275 * NOTE: needs to run before #viewops_data_create so the backup `rv3d->ofs` is correct.
276 */
277 if (vod->rv3d->persp != RV3D_PERSP) {
278 if (vod->rv3d->persp == RV3D_CAMOB) {
279 /* ignore rv3d->lpersp because dolly only makes sense in perspective mode */
280 const Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
282 }
283 else {
284 vod->rv3d->persp = RV3D_PERSP;
285 }
287 }
288
289 /* if one or the other zoom position aren't set, set from event */
290 if (!RNA_struct_property_is_set(op->ptr, "mx") || !RNA_struct_property_is_set(op->ptr, "my")) {
291 RNA_int_set(op->ptr, "mx", event->xy[0]);
292 RNA_int_set(op->ptr, "my", event->xy[1]);
293 }
294
295 if (RNA_struct_property_is_set(op->ptr, "delta")) {
296 viewdolly_exec(C, op);
297 }
298 else {
299 /* overwrite the mouse vector with the view direction (zoom into the center) */
300 if ((use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)) == 0) {
301 negate_v3_v3(vod->init.mousevec, vod->rv3d->viewinv[2]);
302 normalize_v3(vod->init.mousevec);
303 }
304
305 if (event->type == MOUSEZOOM) {
306 /* Bypass Zoom invert flag for track pads (pass false always) */
307
308 if (U.uiflag & USER_ZOOM_HORIZ) {
309 vod->init.event_xy[0] = vod->prev.event_xy[0] = event->xy[0];
310 }
311 else {
312 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
313 vod->init.event_xy[1] = vod->prev.event_xy[1] = vod->init.event_xy[1] + event->xy[0] -
314 event->prev_xy[0];
315 }
316 viewdolly_apply(vod, event->prev_xy, (U.uiflag & USER_ZOOM_INVERT) == 0);
317
318 viewops_data_free(C, static_cast<ViewOpsData *>(op->customdata));
319 op->customdata = nullptr;
320 return OPERATOR_FINISHED;
321 }
322
323 /* add temp handler */
326 }
327 return OPERATOR_FINISHED;
328}
329
331{
332 /* identifiers */
333 ot->name = "Dolly View";
334 ot->description = "Dolly in/out in the view";
336
337 /* API callbacks. */
339 ot->exec = viewdolly_exec;
341 /* Check rotation because this operator switches from orthographic to perspective view.
342 * See inline code-comments for details. */
345
346 /* flags */
348
349 /* properties */
352}
353
355
358 /*idname*/ "VIEW3D_OT_dolly",
359 /*poll_fn*/ nullptr,
360 /*init_fn*/ nullptr,
361 /*apply_fn*/ nullptr,
362};
363
364} // namespace blender
#define RV3D_LOCK_FLAGS(rv3d)
#define C
Definition RandGen.cpp:29
BMesh Iterators.
void viewdolly_modal_keymap(wmKeyConfig *keyconf)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
ViewOpsType ViewOpsType_dolly
bool ED_view3d_camera_lock_autokey(View3D *v3d, RegionView3D *rv3d, bContext *C, bool do_rotate, bool do_translate)
UserDef U
Definition blender.cc:58
MINLINE void negate_v3_v3(float r[3], const float a[3])
ScrArea * CTX_wm_area(const bContext *C)
static wmOperatorStatus viewdolly_modal(bContext *C, wmOperator *op, const wmEvent *event)
@ RPT_WARNING
Definition BKE_report.hh:40
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
MINLINE void copy_v3_v3(float r[3], const float a[3])
void viewops_data_free(bContext *C, ViewOpsData *vod)
void view3d_operator_properties_common(wmOperatorType *ot, const enum eV3D_OpPropFlag flag)
ViewOpsData * viewops_data_create(bContext *C, const wmEvent *event, const ViewOpsType *nav_type, const bool use_cursor_init)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
bool ED_view3d_camera_lock_sync(const Depsgraph *depsgraph, View3D *v3d, RegionView3D *rv3d)
bool ED_view3d_camera_lock_undo_push(const char *str, const View3D *v3d, const RegionView3D *rv3d, bContext *C)
int RNA_int_get(PointerRNA *ptr, const char *name)
void ED_view3d_persp_switch_from_camera(const Depsgraph *depsgraph, View3D *v3d, RegionView3D *rv3d, eRegionView3D_Persp persp)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
static bool viewdolly_offset_lock_check(bContext *C, wmOperator *op)
bool view3d_zoom_or_dolly_or_rotation_poll(bContext *C)
static void view_dolly_to_vector_3d(ARegion *region, const float orig_ofs[3], const float dvec[3], float dfac)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
static wmOperatorStatus viewdolly_invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void VIEW3D_OT_dolly(wmOperatorType *ot)
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)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
@ VIEWOPS_FLAG_ZOOM_TO_MOUSE
@ VIEWOPS_FLAG_DEPTH_NAVIGATE
MINLINE float normalize_v3_v3(float r[3], const float a[3])
@ VIEWROT_MODAL_SWITCH_MOVE
@ VIEWROT_MODAL_SWITCH_ROTATE
MINLINE float normalize_v3(float n[3])
bool ED_view3d_offset_lock_check(const View3D *v3d, const RegionView3D *rv3d)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
static void viewdolly_apply(ViewOpsData *vod, const int xy[2], const bool zoom_invert)
void view3d_navigate_cancel_fn(bContext *C, wmOperator *op)
void ED_view3d_smooth_view_force_finish(bContext *C, View3D *v3d, ARegion *region)
@ KM_RELEASE
Definition WM_types.hh:325
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
@ V3D_OP_PROP_USE_MOUSE_INIT
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
@ OPTYPE_GRAB_CURSOR_XY
Definition WM_types.hh:196
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
ARegion * CTX_wm_region(const bContext *C)
void view3d_boxview_sync(ScrArea *area, ARegion *region)
View3D * CTX_wm_view3d(const bContext *C)
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
static wmOperatorStatus viewdolly_exec(bContext *C, wmOperator *op)
eRegionView3D_Persp persp
ListBaseT< SpaceLink > spacedata
struct blender::ViewOpsData::@013213354235237006300365026145043140112021271057 prev
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 ReportList * reports
struct wmOperatorType * type