Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_view3d / view3d_fly.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/space_view3d/view3d_fly.c
24  *  \ingroup spview3d
25  */
26
27 /* defines VIEW3D_OT_fly modal operator */
28
29 #ifdef WITH_INPUT_NDOF
30 //#  define NDOF_FLY_DEBUG
31 //#  define NDOF_FLY_DRAW_TOOMUCH  /* is this needed for ndof? - commented so redraw doesnt thrash - campbell */
32 #endif /* WITH_INPUT_NDOF */
33
34 #include "DNA_object_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.h"
39 #include "BLI_blenlib.h"
40
41 #include "BKE_context.h"
42 #include "BKE_report.h"
43
44 #include "BLT_translation.h"
45
46 #include "BIF_gl.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 #include "ED_screen.h"
52 #include "ED_space_api.h"
53
54 #include "PIL_time.h" /* smoothview */
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58
59 #include "GPU_immediate.h"
60
61 #include "DEG_depsgraph.h"
62
63 #include "view3d_intern.h"  /* own include */
64
65 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
66 enum {
67         FLY_MODAL_CANCEL = 1,
68         FLY_MODAL_CONFIRM,
69         FLY_MODAL_ACCELERATE,
70         FLY_MODAL_DECELERATE,
71         FLY_MODAL_PAN_ENABLE,
72         FLY_MODAL_PAN_DISABLE,
73         FLY_MODAL_DIR_FORWARD,
74         FLY_MODAL_DIR_BACKWARD,
75         FLY_MODAL_DIR_LEFT,
76         FLY_MODAL_DIR_RIGHT,
77         FLY_MODAL_DIR_UP,
78         FLY_MODAL_DIR_DOWN,
79         FLY_MODAL_AXIS_LOCK_X,
80         FLY_MODAL_AXIS_LOCK_Z,
81         FLY_MODAL_PRECISION_ENABLE,
82         FLY_MODAL_PRECISION_DISABLE,
83         FLY_MODAL_FREELOOK_ENABLE,
84         FLY_MODAL_FREELOOK_DISABLE,
85         FLY_MODAL_SPEED,        /* mousepan typically */
86 };
87
88 /* relative view axis locking - xlock, zlock */
89 typedef enum eFlyPanState {
90         /* disabled */
91         FLY_AXISLOCK_STATE_OFF    = 0,
92
93         /* enabled but not checking because mouse hasn't moved outside the margin since locking was checked an not needed
94          * when the mouse moves, locking is set to 2 so checks are done. */
95         FLY_AXISLOCK_STATE_IDLE   = 1,
96
97         /* mouse moved and checking needed, if no view altering is done its changed back to #FLY_AXISLOCK_STATE_IDLE */
98         FLY_AXISLOCK_STATE_ACTIVE = 2
99 } eFlyPanState;
100
101 /* called in transform_ops.c, on each regeneration of keymaps  */
102 void fly_modal_keymap(wmKeyConfig *keyconf)
103 {
104         static const EnumPropertyItem modal_items[] = {
105                 {FLY_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
106                 {FLY_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
107                 {FLY_MODAL_ACCELERATE, "ACCELERATE", 0, "Accelerate", ""},
108                 {FLY_MODAL_DECELERATE, "DECELERATE", 0, "Decelerate", ""},
109
110                 {FLY_MODAL_PAN_ENABLE, "PAN_ENABLE", 0, "Pan Enable", ""},
111                 {FLY_MODAL_PAN_DISABLE, "PAN_DISABLE", 0, "Pan Disable", ""},
112
113                 {FLY_MODAL_DIR_FORWARD, "FORWARD", 0, "Fly Forward", ""},
114                 {FLY_MODAL_DIR_BACKWARD, "BACKWARD", 0, "Fly Backward", ""},
115                 {FLY_MODAL_DIR_LEFT, "LEFT", 0, "Fly Left", ""},
116                 {FLY_MODAL_DIR_RIGHT, "RIGHT", 0, "Fly Right", ""},
117                 {FLY_MODAL_DIR_UP, "UP", 0, "Fly Up", ""},
118                 {FLY_MODAL_DIR_DOWN, "DOWN", 0, "Fly Down", ""},
119
120                 {FLY_MODAL_AXIS_LOCK_X, "AXIS_LOCK_X", 0, "X Axis Correction", "X axis correction (toggle)"},
121                 {FLY_MODAL_AXIS_LOCK_Z, "AXIS_LOCK_Z", 0, "X Axis Correction", "Z axis correction (toggle)"},
122
123                 {FLY_MODAL_PRECISION_ENABLE, "PRECISION_ENABLE", 0, "Precision Enable", ""},
124                 {FLY_MODAL_PRECISION_DISABLE, "PRECISION_DISABLE", 0, "Precision Disable", ""},
125
126                 {FLY_MODAL_FREELOOK_ENABLE, "FREELOOK_ENABLE", 0, "Rotation Enable", ""},
127                 {FLY_MODAL_FREELOOK_DISABLE, "FREELOOK_DISABLE", 0, "Rotation Disable", ""},
128
129                 {0, NULL, 0, NULL, NULL}};
130
131         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Fly Modal");
132
133         /* this function is called for each spacetype, only needs to add map once */
134         if (keymap && keymap->modal_items)
135                 return;
136
137         keymap = WM_modalkeymap_add(keyconf, "View3D Fly Modal", modal_items);
138
139         /* items for modal map */
140         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_CANCEL);
141         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, FLY_MODAL_CANCEL);
142
143         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_ANY, KM_ANY, 0, FLY_MODAL_CONFIRM);
144         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_CONFIRM);
145         WM_modalkeymap_add_item(keymap, SPACEKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_CONFIRM);
146         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, KM_ANY, 0, FLY_MODAL_CONFIRM);
147
148         WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_ACCELERATE);
149         WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, KM_ANY, 0, FLY_MODAL_DECELERATE);
150         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_ANY, 0, FLY_MODAL_ACCELERATE);
151         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_ANY, 0, FLY_MODAL_DECELERATE);
152
153         WM_modalkeymap_add_item(keymap, MOUSEPAN, 0, 0, 0, FLY_MODAL_SPEED);
154
155         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, KM_ANY, 0, FLY_MODAL_PAN_ENABLE);
156         /* XXX - Bug in the event system, middle mouse release doesnt work */
157         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, FLY_MODAL_PAN_DISABLE);
158
159         /* WASD */
160         WM_modalkeymap_add_item(keymap, WKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_FORWARD);
161         WM_modalkeymap_add_item(keymap, SKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_BACKWARD);
162         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_LEFT);
163         WM_modalkeymap_add_item(keymap, DKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_RIGHT);
164         WM_modalkeymap_add_item(keymap, EKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_UP);
165         WM_modalkeymap_add_item(keymap, QKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_DOWN);
166
167         /* for legacy reasons, leave R/F working */
168         WM_modalkeymap_add_item(keymap, RKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_UP);
169         WM_modalkeymap_add_item(keymap, FKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_DOWN);
170
171         WM_modalkeymap_add_item(keymap, UPARROWKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_FORWARD);
172         WM_modalkeymap_add_item(keymap, DOWNARROWKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_BACKWARD);
173         WM_modalkeymap_add_item(keymap, LEFTARROWKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_LEFT);
174         WM_modalkeymap_add_item(keymap, RIGHTARROWKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_RIGHT);
175
176         WM_modalkeymap_add_item(keymap, XKEY, KM_PRESS, 0, 0, FLY_MODAL_AXIS_LOCK_X);
177         WM_modalkeymap_add_item(keymap, ZKEY, KM_PRESS, 0, 0, FLY_MODAL_AXIS_LOCK_Z);
178
179         WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_PRECISION_ENABLE);
180         WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_RELEASE, KM_ANY, 0, FLY_MODAL_PRECISION_DISABLE);
181
182         /* for legacy reasons, leave shift working */
183         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_PRECISION_ENABLE);
184         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, FLY_MODAL_PRECISION_DISABLE);
185
186         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_FREELOOK_ENABLE);
187         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, FLY_MODAL_FREELOOK_DISABLE);
188
189         /* assign map to operators */
190         WM_modalkeymap_assign(keymap, "VIEW3D_OT_fly");
191 }
192
193 typedef struct FlyInfo {
194         /* context stuff */
195         RegionView3D *rv3d;
196         View3D *v3d;
197         ARegion *ar;
198         struct Depsgraph *depsgraph;
199         Scene *scene;
200
201         wmTimer *timer; /* needed for redraws */
202
203         short state;
204         bool redraw;
205         bool use_precision;
206         /* if the user presses shift they can look about
207          * without moving the direction there looking */
208         bool use_freelook;
209
210         int mval[2]; /* latest 2D mouse values */
211         int center_mval[2]; /* center mouse values */
212         float width, height; /* camera viewport dimensions */
213
214 #ifdef WITH_INPUT_NDOF
215         wmNDOFMotionData *ndof;  /* latest 3D mouse values */
216 #endif
217
218         /* fly state state */
219         float speed; /* the speed the view is moving per redraw */
220         short axis; /* Axis index to move along by default Z to move along the view */
221         bool pan_view; /* when true, pan the view instead of rotating */
222
223         eFlyPanState xlock, zlock;
224         float xlock_momentum, zlock_momentum; /* nicer dynamics */
225         float grid; /* world scale 1.0 default */
226
227         /* compare between last state */
228         double time_lastwheel; /* used to accelerate when using the mousewheel a lot */
229         double time_lastdraw; /* time between draws */
230
231         void *draw_handle_pixel;
232
233         /* use for some lag */
234         float dvec_prev[3]; /* old for some lag */
235
236         struct View3DCameraControl *v3d_camera_control;
237
238 } FlyInfo;
239
240 static void drawFlyPixel(const struct bContext *UNUSED(C), ARegion *UNUSED(ar), void *arg)
241 {
242         FlyInfo *fly = arg;
243         rctf viewborder;
244         int xoff, yoff;
245         float x1, x2, y1, y2;
246
247         if (fly->scene->camera) {
248                 ED_view3d_calc_camera_border(fly->scene, fly->depsgraph, fly->ar, fly->v3d, fly->rv3d, &viewborder, false);
249                 xoff = viewborder.xmin;
250                 yoff = viewborder.ymin;
251         }
252         else {
253                 xoff = 0;
254                 yoff = 0;
255         }
256
257         /* draws 4 edge brackets that frame the safe area where the
258          * mouse can move during fly mode without spinning the view */
259
260         x1 = xoff + 0.45f * fly->width;
261         y1 = yoff + 0.45f * fly->height;
262         x2 = xoff + 0.55f * fly->width;
263         y2 = yoff + 0.55f * fly->height;
264
265         Gwn_VertFormat *format = immVertexFormat();
266         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
267
268         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
269
270         immUniformThemeColor(TH_VIEW_OVERLAY);
271
272         immBegin(GWN_PRIM_LINES, 16);
273
274         /* bottom left */
275         immVertex2f(pos, x1, y1);
276         immVertex2f(pos, x1, y1 + 5);
277
278         immVertex2f(pos, x1, y1);
279         immVertex2f(pos, x1 + 5, y1);
280
281         /* top right */
282         immVertex2f(pos, x2, y2);
283         immVertex2f(pos, x2, y2 - 5);
284
285         immVertex2f(pos, x2, y2);
286         immVertex2f(pos, x2 - 5, y2);
287
288         /* top left */
289         immVertex2f(pos, x1, y2);
290         immVertex2f(pos, x1, y2 - 5);
291
292         immVertex2f(pos, x1, y2);
293         immVertex2f(pos, x1 + 5, y2);
294
295         /* bottom right */
296         immVertex2f(pos, x2, y1);
297         immVertex2f(pos, x2, y1 + 5);
298
299         immVertex2f(pos, x2, y1);
300         immVertex2f(pos, x2 - 5, y1);
301
302         immEnd();
303         immUnbindProgram();
304 }
305
306 static void fly_update_header(bContext *C, wmOperator *op, FlyInfo *fly)
307 {
308         char header[UI_MAX_DRAW_STR];
309         char buf[UI_MAX_DRAW_STR];
310
311         char *p = buf;
312         int available_len = sizeof(buf);
313
314 #define WM_MODALKEY(_id) \
315         WM_modalkeymap_operator_items_to_string_buf(op->type, (_id), true, UI_MAX_SHORTCUT_STR, &available_len, &p)
316
317         BLI_snprintf(header, sizeof(header), IFACE_("%s: confirm, %s: cancel, "
318                                                     "%s: pan enable, "
319                                                     "%s|%s|%s|%s|%s|%s: direction, "
320                                                     "%s: slow, %s: free look, "
321                                                     "%s: Upright x axis (%s), "
322                                                     "%s: Upright z axis (%s), "
323                                                     "%s: increase  speed, %s: decrease speed"),
324                      WM_MODALKEY(FLY_MODAL_CONFIRM), WM_MODALKEY(FLY_MODAL_CANCEL),
325                      WM_MODALKEY(FLY_MODAL_PAN_ENABLE),
326                      WM_MODALKEY(FLY_MODAL_DIR_FORWARD), WM_MODALKEY(FLY_MODAL_DIR_LEFT),
327                      WM_MODALKEY(FLY_MODAL_DIR_BACKWARD), WM_MODALKEY(FLY_MODAL_DIR_RIGHT),
328                      WM_MODALKEY(FLY_MODAL_DIR_UP), WM_MODALKEY(FLY_MODAL_DIR_DOWN),
329                      WM_MODALKEY(FLY_MODAL_PRECISION_ENABLE), WM_MODALKEY(FLY_MODAL_FREELOOK_ENABLE),
330                      WM_MODALKEY(FLY_MODAL_AXIS_LOCK_X), WM_bool_as_string(fly->xlock != FLY_AXISLOCK_STATE_OFF),
331                      WM_MODALKEY(FLY_MODAL_AXIS_LOCK_Z), WM_bool_as_string(fly->zlock != FLY_AXISLOCK_STATE_OFF),
332                      WM_MODALKEY(FLY_MODAL_ACCELERATE), WM_MODALKEY(FLY_MODAL_DECELERATE));
333
334 #undef WM_MODALKEY
335
336         ED_area_headerprint(CTX_wm_area(C), header);
337 }
338
339 /* FlyInfo->state */
340 enum {
341         FLY_RUNNING     = 0,
342         FLY_CANCEL      = 1,
343         FLY_CONFIRM     = 2,
344 };
345
346 static bool initFlyInfo(bContext *C, FlyInfo *fly, wmOperator *op, const wmEvent *event)
347 {
348         wmWindow *win = CTX_wm_window(C);
349
350         rctf viewborder;
351
352         float upvec[3]; /* tmp */
353         float mat[3][3];
354
355         fly->rv3d = CTX_wm_region_view3d(C);
356         fly->v3d = CTX_wm_view3d(C);
357         fly->ar = CTX_wm_region(C);
358         fly->depsgraph = CTX_data_depsgraph(C);
359         fly->scene = CTX_data_scene(C);
360
361 #ifdef NDOF_FLY_DEBUG
362         puts("\n-- fly begin --");
363 #endif
364
365         /* sanity check: for rare but possible case (if lib-linking the camera fails) */
366         if ((fly->rv3d->persp == RV3D_CAMOB) && (fly->v3d->camera == NULL)) {
367                 fly->rv3d->persp = RV3D_PERSP;
368         }
369
370         if (fly->rv3d->persp == RV3D_CAMOB && ID_IS_LINKED(fly->v3d->camera)) {
371                 BKE_report(op->reports, RPT_ERROR, "Cannot fly a camera from an external library");
372                 return false;
373         }
374
375         if (ED_view3d_offset_lock_check(fly->v3d, fly->rv3d)) {
376                 BKE_report(op->reports, RPT_ERROR, "Cannot fly when the view offset is locked");
377                 return false;
378         }
379
380         if (fly->rv3d->persp == RV3D_CAMOB && fly->v3d->camera->constraints.first) {
381                 BKE_report(op->reports, RPT_ERROR, "Cannot fly an object with constraints");
382                 return false;
383         }
384
385         fly->state = FLY_RUNNING;
386         fly->speed = 0.0f;
387         fly->axis = 2;
388         fly->pan_view = false;
389         fly->xlock = FLY_AXISLOCK_STATE_OFF;
390         fly->zlock = FLY_AXISLOCK_STATE_OFF;
391         fly->xlock_momentum = 0.0f;
392         fly->zlock_momentum = 0.0f;
393         fly->grid = 1.0f;
394         fly->use_precision = false;
395         fly->use_freelook = false;
396
397 #ifdef NDOF_FLY_DRAW_TOOMUCH
398         fly->redraw = 1;
399 #endif
400         zero_v3(fly->dvec_prev);
401
402         fly->timer = WM_event_add_timer(CTX_wm_manager(C), win, TIMER, 0.01f);
403
404         copy_v2_v2_int(fly->mval, event->mval);
405
406 #ifdef WITH_INPUT_NDOF
407         fly->ndof = NULL;
408 #endif
409
410         fly->time_lastdraw = fly->time_lastwheel = PIL_check_seconds_timer();
411
412         fly->draw_handle_pixel = ED_region_draw_cb_activate(fly->ar->type, drawFlyPixel, fly, REGION_DRAW_POST_PIXEL);
413
414         fly->rv3d->rflag |= RV3D_NAVIGATING;
415
416         /* detect whether to start with Z locking */
417         copy_v3_fl3(upvec, 1.0f, 0.0f, 0.0f);
418         copy_m3_m4(mat, fly->rv3d->viewinv);
419         mul_m3_v3(mat, upvec);
420         if (fabsf(upvec[2]) < 0.1f) {
421                 fly->zlock = FLY_AXISLOCK_STATE_IDLE;
422         }
423
424         fly->v3d_camera_control = ED_view3d_cameracontrol_acquire(
425                 CTX_data_depsgraph(C), fly->scene, fly->v3d, fly->rv3d,
426                 (U.uiflag & USER_CAM_LOCK_NO_PARENT) == 0);
427
428         /* calculate center */
429         if (fly->scene->camera) {
430                 ED_view3d_calc_camera_border(fly->scene, fly->depsgraph, fly->ar, fly->v3d, fly->rv3d, &viewborder, false);
431
432                 fly->width = BLI_rctf_size_x(&viewborder);
433                 fly->height = BLI_rctf_size_y(&viewborder);
434
435                 fly->center_mval[0] = viewborder.xmin + fly->width / 2;
436                 fly->center_mval[1] = viewborder.ymin + fly->height / 2;
437         }
438         else {
439                 fly->width = fly->ar->winx;
440                 fly->height = fly->ar->winy;
441
442                 fly->center_mval[0] = fly->width / 2;
443                 fly->center_mval[1] = fly->height / 2;
444         }
445
446         /* center the mouse, probably the UI mafia are against this but without its quite annoying */
447         WM_cursor_warp(win, fly->ar->winrct.xmin + fly->center_mval[0], fly->ar->winrct.ymin + fly->center_mval[1]);
448
449         fly_update_header(C, op, fly);
450         return 1;
451 }
452
453 static int flyEnd(bContext *C, FlyInfo *fly)
454 {
455         wmWindow *win;
456         RegionView3D *rv3d;
457
458         if (fly->state == FLY_RUNNING)
459                 return OPERATOR_RUNNING_MODAL;
460
461 #ifdef NDOF_FLY_DEBUG
462         puts("\n-- fly end --");
463 #endif
464
465         win = CTX_wm_window(C);
466         rv3d = fly->rv3d;
467
468         WM_event_remove_timer(CTX_wm_manager(C), win, fly->timer);
469
470         ED_region_draw_cb_exit(fly->ar->type, fly->draw_handle_pixel);
471
472         ED_view3d_cameracontrol_release(fly->v3d_camera_control, fly->state == FLY_CANCEL);
473
474         rv3d->rflag &= ~RV3D_NAVIGATING;
475
476 #ifdef WITH_INPUT_NDOF
477         if (fly->ndof)
478                 MEM_freeN(fly->ndof);
479 #endif
480
481         if (fly->state == FLY_CONFIRM) {
482                 MEM_freeN(fly);
483                 return OPERATOR_FINISHED;
484         }
485
486         MEM_freeN(fly);
487         return OPERATOR_CANCELLED;
488 }
489
490 static void flyEvent(bContext *C, wmOperator *op, FlyInfo *fly, const wmEvent *event)
491 {
492         if (event->type == TIMER && event->customdata == fly->timer) {
493                 fly->redraw = 1;
494         }
495         else if (event->type == MOUSEMOVE) {
496                 copy_v2_v2_int(fly->mval, event->mval);
497         }
498 #ifdef WITH_INPUT_NDOF
499         else if (event->type == NDOF_MOTION) {
500                 /* do these automagically get delivered? yes. */
501                 // puts("ndof motion detected in fly mode!");
502                 // static const char *tag_name = "3D mouse position";
503
504                 const wmNDOFMotionData *incoming_ndof = event->customdata;
505                 switch (incoming_ndof->progress) {
506                         case P_STARTING:
507                                 /* start keeping track of 3D mouse position */
508 #  ifdef NDOF_FLY_DEBUG
509                                 puts("start keeping track of 3D mouse position");
510 #  endif
511                                 /* fall-through */
512                         case P_IN_PROGRESS:
513                                 /* update 3D mouse position */
514 #  ifdef NDOF_FLY_DEBUG
515                                 putchar('.'); fflush(stdout);
516 #  endif
517                                 if (fly->ndof == NULL) {
518                                         // fly->ndof = MEM_mallocN(sizeof(wmNDOFMotionData), tag_name);
519                                         fly->ndof = MEM_dupallocN(incoming_ndof);
520                                         // fly->ndof = malloc(sizeof(wmNDOFMotionData));
521                                 }
522                                 else {
523                                         memcpy(fly->ndof, incoming_ndof, sizeof(wmNDOFMotionData));
524                                 }
525                                 break;
526                         case P_FINISHING:
527                                 /* stop keeping track of 3D mouse position */
528 #  ifdef NDOF_FLY_DEBUG
529                                 puts("stop keeping track of 3D mouse position");
530 #  endif
531                                 if (fly->ndof) {
532                                         MEM_freeN(fly->ndof);
533                                         // free(fly->ndof);
534                                         fly->ndof = NULL;
535                                 }
536                                 /* update the time else the view will jump when 2D mouse/timer resume */
537                                 fly->time_lastdraw = PIL_check_seconds_timer();
538                                 break;
539                         default:
540                                 break; /* should always be one of the above 3 */
541                 }
542         }
543 #endif /* WITH_INPUT_NDOF */
544         /* handle modal keymap first */
545         else if (event->type == EVT_MODAL_MAP) {
546                 switch (event->val) {
547                         case FLY_MODAL_CANCEL:
548                                 fly->state = FLY_CANCEL;
549                                 break;
550                         case FLY_MODAL_CONFIRM:
551                                 fly->state = FLY_CONFIRM;
552                                 break;
553
554                         /* speed adjusting with mousepan (trackpad) */
555                         case FLY_MODAL_SPEED:
556                         {
557                                 float fac = 0.02f * (event->prevy - event->y);
558
559                                 /* allowing to brake immediate */
560                                 if (fac > 0.0f && fly->speed < 0.0f)
561                                         fly->speed = 0.0f;
562                                 else if (fac < 0.0f && fly->speed > 0.0f)
563                                         fly->speed = 0.0f;
564                                 else
565                                         fly->speed += fly->grid * fac;
566
567                                 break;
568                         }
569                         case FLY_MODAL_ACCELERATE:
570                         {
571                                 double time_currwheel;
572                                 float time_wheel;
573
574                                 /* not quite correct but avoids confusion WASD/arrow keys 'locking up' */
575                                 if (fly->axis == -1) {
576                                         fly->axis = 2;
577                                         fly->speed = fabsf(fly->speed);
578                                 }
579
580                                 time_currwheel = PIL_check_seconds_timer();
581                                 time_wheel = (float)(time_currwheel - fly->time_lastwheel);
582                                 fly->time_lastwheel = time_currwheel;
583                                 /* Mouse wheel delays range from (0.5 == slow) to (0.01 == fast) */
584                                 time_wheel = 1.0f + (10.0f - (20.0f * min_ff(time_wheel, 0.5f))); /* 0-0.5 -> 0-5.0 */
585
586                                 if (fly->speed < 0.0f) {
587                                         fly->speed = 0.0f;
588                                 }
589                                 else {
590                                         fly->speed += fly->grid * time_wheel * (fly->use_precision ? 0.1f : 1.0f);
591                                 }
592                                 break;
593                         }
594                         case FLY_MODAL_DECELERATE:
595                         {
596                                 double time_currwheel;
597                                 float time_wheel;
598
599                                 /* not quite correct but avoids confusion WASD/arrow keys 'locking up' */
600                                 if (fly->axis == -1) {
601                                         fly->axis = 2;
602                                         fly->speed = -fabsf(fly->speed);
603                                 }
604
605                                 time_currwheel = PIL_check_seconds_timer();
606                                 time_wheel = (float)(time_currwheel - fly->time_lastwheel);
607                                 fly->time_lastwheel = time_currwheel;
608                                 time_wheel = 1.0f + (10.0f - (20.0f * min_ff(time_wheel, 0.5f))); /* 0-0.5 -> 0-5.0 */
609
610                                 if (fly->speed > 0.0f) {
611                                         fly->speed = 0;
612                                 }
613                                 else {
614                                         fly->speed -= fly->grid * time_wheel * (fly->use_precision ? 0.1f : 1.0f);
615                                 }
616                                 break;
617                         }
618                         case FLY_MODAL_PAN_ENABLE:
619                                 fly->pan_view = true;
620                                 break;
621                         case FLY_MODAL_PAN_DISABLE:
622                                 fly->pan_view = false;
623                                 break;
624
625                         /* implement WASD keys,
626                          * comments only for 'forward '*/
627                         case FLY_MODAL_DIR_FORWARD:
628                                 if (fly->axis == 2 && fly->speed < 0.0f) { /* reverse direction stops, tap again to continue */
629                                         fly->axis = -1;
630                                 }
631                                 else {
632                                         /* flip speed rather than stopping, game like motion,
633                                          * else increase like mousewheel if were already moving in that direction */
634                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
635                                         else if (fly->axis == 2) fly->speed += fly->grid;
636                                         fly->axis = 2;
637                                 }
638                                 break;
639                         case FLY_MODAL_DIR_BACKWARD:
640                                 if (fly->axis == 2 && fly->speed > 0.0f) {
641                                         fly->axis = -1;
642                                 }
643                                 else {
644                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
645                                         else if (fly->axis == 2) fly->speed -= fly->grid;
646
647                                         fly->axis = 2;
648                                 }
649                                 break;
650                         case FLY_MODAL_DIR_LEFT:
651                                 if (fly->axis == 0 && fly->speed < 0.0f) {
652                                         fly->axis = -1;
653                                 }
654                                 else {
655                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
656                                         else if (fly->axis == 0) fly->speed += fly->grid;
657
658                                         fly->axis = 0;
659                                 }
660                                 break;
661                         case FLY_MODAL_DIR_RIGHT:
662                                 if (fly->axis == 0 && fly->speed > 0.0f) {
663                                         fly->axis = -1;
664                                 }
665                                 else {
666                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
667                                         else if (fly->axis == 0) fly->speed -= fly->grid;
668
669                                         fly->axis = 0;
670                                 }
671                                 break;
672                         case FLY_MODAL_DIR_DOWN:
673                                 if (fly->axis == 1 && fly->speed < 0.0f) {
674                                         fly->axis = -1;
675                                 }
676                                 else {
677                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
678                                         else if (fly->axis == 1) fly->speed += fly->grid;
679                                         fly->axis = 1;
680                                 }
681                                 break;
682                         case FLY_MODAL_DIR_UP:
683                                 if (fly->axis == 1 && fly->speed > 0.0f) {
684                                         fly->axis = -1;
685                                 }
686                                 else {
687                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
688                                         else if (fly->axis == 1) fly->speed -= fly->grid;
689                                         fly->axis = 1;
690                                 }
691                                 break;
692
693                         case FLY_MODAL_AXIS_LOCK_X:
694                                 if (fly->xlock != FLY_AXISLOCK_STATE_OFF)
695                                         fly->xlock = FLY_AXISLOCK_STATE_OFF;
696                                 else {
697                                         fly->xlock = FLY_AXISLOCK_STATE_ACTIVE;
698                                         fly->xlock_momentum = 0.0;
699                                 }
700                                 fly_update_header(C, op, fly);
701                                 break;
702                         case FLY_MODAL_AXIS_LOCK_Z:
703                                 if (fly->zlock != FLY_AXISLOCK_STATE_OFF)
704                                         fly->zlock = FLY_AXISLOCK_STATE_OFF;
705                                 else {
706                                         fly->zlock = FLY_AXISLOCK_STATE_ACTIVE;
707                                         fly->zlock_momentum = 0.0;
708                                 }
709                                 fly_update_header(C, op, fly);
710                                 break;
711
712                         case FLY_MODAL_PRECISION_ENABLE:
713                                 fly->use_precision = true;
714                                 break;
715                         case FLY_MODAL_PRECISION_DISABLE:
716                                 fly->use_precision = false;
717                                 break;
718
719                         case FLY_MODAL_FREELOOK_ENABLE:
720                                 fly->use_freelook = true;
721                                 break;
722                         case FLY_MODAL_FREELOOK_DISABLE:
723                                 fly->use_freelook = false;
724                                 break;
725                 }
726         }
727 }
728
729 static void flyMoveCamera(bContext *C, FlyInfo *fly,
730                           const bool do_rotate, const bool do_translate)
731 {
732         ED_view3d_cameracontrol_update(fly->v3d_camera_control, true, C, do_rotate, do_translate);
733 }
734
735 static int flyApply(bContext *C, FlyInfo *fly)
736 {
737 #define FLY_ROTATE_FAC 10.0f /* more is faster */
738 #define FLY_ZUP_CORRECT_FAC 0.1f /* amount to correct per step */
739 #define FLY_ZUP_CORRECT_ACCEL 0.05f /* increase upright momentum each step */
740 #define FLY_SMOOTH_FAC 20.0f  /* higher value less lag */
741
742         /* fly mode - Shift+F
743          * a fly loop where the user can move move the view as if they are flying
744          */
745         RegionView3D *rv3d = fly->rv3d;
746
747         float mat[3][3]; /* 3x3 copy of the view matrix so we can move along the view axis */
748         float dvec[3] = {0, 0, 0}; /* this is the direction thast added to the view offset per redraw */
749
750         /* Camera Uprighting variables */
751         float moffset[2]; /* mouse offset from the views center */
752         float tmp_quat[4]; /* used for rotating the view */
753
754         int xmargin, ymargin; /* x and y margin are define the safe area where the mouses movement wont rotate the view */
755
756 #ifdef NDOF_FLY_DEBUG
757         {
758                 static unsigned int iteration = 1;
759                 printf("fly timer %d\n", iteration++);
760         }
761 #endif
762
763         xmargin = fly->width / 20.0f;
764         ymargin = fly->height / 20.0f;
765
766         {
767
768                 /* mouse offset from the center */
769                 moffset[0] = fly->mval[0] - fly->center_mval[0];
770                 moffset[1] = fly->mval[1] - fly->center_mval[1];
771
772                 /* enforce a view margin */
773                 if      (moffset[0] >  xmargin) moffset[0] -= xmargin;
774                 else if (moffset[0] < -xmargin) moffset[0] += xmargin;
775                 else                            moffset[0] =  0;
776
777                 if      (moffset[1] >  ymargin) moffset[1] -= ymargin;
778                 else if (moffset[1] < -ymargin) moffset[1] += ymargin;
779                 else                            moffset[1] =  0;
780
781
782                 /* scale the mouse movement by this value - scales mouse movement to the view size
783                  * moffset[0] / (ar->winx-xmargin * 2) - window size minus margin (same for y)
784                  *
785                  * the mouse moves isn't linear */
786
787                 if (moffset[0]) {
788                         moffset[0] /= fly->width - (xmargin * 2);
789                         moffset[0] *= fabsf(moffset[0]);
790                 }
791
792                 if (moffset[1]) {
793                         moffset[1] /= fly->height - (ymargin * 2);
794                         moffset[1] *= fabsf(moffset[1]);
795                 }
796
797                 /* Should we redraw? */
798                 if ((fly->speed != 0.0f) ||
799                     moffset[0] || moffset[1] ||
800                     (fly->zlock != FLY_AXISLOCK_STATE_OFF) ||
801                     (fly->xlock != FLY_AXISLOCK_STATE_OFF) ||
802                     dvec[0] || dvec[1] || dvec[2])
803                 {
804                         float dvec_tmp[3];
805
806                         /* time how fast it takes for us to redraw,
807                          * this is so simple scenes don't fly too fast */
808                         double time_current;
809                         float time_redraw;
810                         float time_redraw_clamped;
811 #ifdef NDOF_FLY_DRAW_TOOMUCH
812                         fly->redraw = 1;
813 #endif
814                         time_current = PIL_check_seconds_timer();
815                         time_redraw = (float)(time_current - fly->time_lastdraw);
816                         time_redraw_clamped = min_ff(0.05f, time_redraw); /* clamp redraw time to avoid jitter in roll correction */
817                         fly->time_lastdraw = time_current;
818
819                         /* Scale the time to use shift to scale the speed down- just like
820                          * shift slows many other areas of blender down */
821                         if (fly->use_precision)
822                                 fly->speed = fly->speed * (1.0f - time_redraw_clamped);
823
824                         copy_m3_m4(mat, rv3d->viewinv);
825
826                         if (fly->pan_view == true) {
827                                 /* pan only */
828                                 copy_v3_fl3(dvec_tmp, -moffset[0], -moffset[1], 0.0f);
829
830                                 if (fly->use_precision) {
831                                         dvec_tmp[0] *= 0.1f;
832                                         dvec_tmp[1] *= 0.1f;
833                                 }
834
835                                 mul_m3_v3(mat, dvec_tmp);
836                                 mul_v3_fl(dvec_tmp, time_redraw * 200.0f * fly->grid);
837                         }
838                         else {
839                                 float roll; /* similar to the angle between the camera's up and the Z-up,
840                                              * but its very rough so just roll */
841
842                                 /* rotate about the X axis- look up/down */
843                                 if (moffset[1]) {
844                                         float upvec[3];
845                                         copy_v3_fl3(upvec, 1.0f, 0.0f, 0.0f);
846                                         mul_m3_v3(mat, upvec);
847                                         /* Rotate about the relative up vec */
848                                         axis_angle_to_quat(tmp_quat, upvec, moffset[1] * time_redraw * -FLY_ROTATE_FAC);
849                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
850
851                                         if (fly->xlock != FLY_AXISLOCK_STATE_OFF)
852                                                 fly->xlock = FLY_AXISLOCK_STATE_ACTIVE;  /* check for rotation */
853                                         if (fly->zlock != FLY_AXISLOCK_STATE_OFF)
854                                                 fly->zlock = FLY_AXISLOCK_STATE_ACTIVE;
855                                         fly->xlock_momentum = 0.0f;
856                                 }
857
858                                 /* rotate about the Y axis- look left/right */
859                                 if (moffset[0]) {
860                                         float upvec[3];
861                                         /* if we're upside down invert the moffset */
862                                         copy_v3_fl3(upvec, 0.0f, 1.0f, 0.0f);
863                                         mul_m3_v3(mat, upvec);
864
865                                         if (upvec[2] < 0.0f)
866                                                 moffset[0] = -moffset[0];
867
868                                         /* make the lock vectors */
869                                         if (fly->zlock) {
870                                                 copy_v3_fl3(upvec, 0.0f, 0.0f, 1.0f);
871                                         }
872                                         else {
873                                                 copy_v3_fl3(upvec, 0.0f, 1.0f, 0.0f);
874                                                 mul_m3_v3(mat, upvec);
875                                         }
876
877                                         /* Rotate about the relative up vec */
878                                         axis_angle_to_quat(tmp_quat, upvec, moffset[0] * time_redraw * FLY_ROTATE_FAC);
879                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
880
881                                         if (fly->xlock != FLY_AXISLOCK_STATE_OFF)
882                                                 fly->xlock = FLY_AXISLOCK_STATE_ACTIVE;  /* check for rotation */
883                                         if (fly->zlock != FLY_AXISLOCK_STATE_OFF)
884                                                 fly->zlock = FLY_AXISLOCK_STATE_ACTIVE;
885                                 }
886
887                                 if (fly->zlock == FLY_AXISLOCK_STATE_ACTIVE) {
888                                         float upvec[3];
889                                         copy_v3_fl3(upvec, 1.0f, 0.0f, 0.0f);
890                                         mul_m3_v3(mat, upvec);
891
892                                         /* make sure we have some z rolling */
893                                         if (fabsf(upvec[2]) > 0.00001f) {
894                                                 roll = upvec[2] * 5.0f;
895                                                 /* rotate the view about this axis */
896                                                 copy_v3_fl3(upvec, 0.0f, 0.0f, 1.0f);
897                                                 mul_m3_v3(mat, upvec);
898                                                 /* Rotate about the relative up vec */
899                                                 axis_angle_to_quat(tmp_quat, upvec,
900                                                                    roll * time_redraw_clamped * fly->zlock_momentum * FLY_ZUP_CORRECT_FAC);
901                                                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
902
903                                                 fly->zlock_momentum += FLY_ZUP_CORRECT_ACCEL;
904                                         }
905                                         else {
906                                                 fly->zlock = FLY_AXISLOCK_STATE_IDLE; /* don't check until the view rotates again */
907                                                 fly->zlock_momentum = 0.0f;
908                                         }
909                                 }
910
911                                 /* only apply xcorrect when mouse isn't applying x rot */
912                                 if (fly->xlock == FLY_AXISLOCK_STATE_ACTIVE && moffset[1] == 0) {
913                                         float upvec[3];
914                                         copy_v3_fl3(upvec, 0.0f, 0.0f, 1.0f);
915                                         mul_m3_v3(mat, upvec);
916                                         /* make sure we have some z rolling */
917                                         if (fabsf(upvec[2]) > 0.00001f) {
918                                                 roll = upvec[2] * -5.0f;
919                                                 /* rotate the view about this axis */
920                                                 copy_v3_fl3(upvec, 1.0f, 0.0f, 0.0f);
921                                                 mul_m3_v3(mat, upvec);
922
923                                                 /* Rotate about the relative up vec */
924                                                 axis_angle_to_quat(tmp_quat, upvec, roll * time_redraw_clamped * fly->xlock_momentum * 0.1f);
925                                                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
926
927                                                 fly->xlock_momentum += 0.05f;
928                                         }
929                                         else {
930                                                 fly->xlock = FLY_AXISLOCK_STATE_IDLE; /* see above */
931                                                 fly->xlock_momentum = 0.0f;
932                                         }
933                                 }
934
935                                 if (fly->axis == -1) {
936                                         /* pause */
937                                         zero_v3(dvec_tmp);
938                                 }
939                                 else if (!fly->use_freelook) {
940                                         /* Normal operation */
941                                         /* define dvec, view direction vector */
942                                         zero_v3(dvec_tmp);
943                                         /* move along the current axis */
944                                         dvec_tmp[fly->axis] = 1.0f;
945
946                                         mul_m3_v3(mat, dvec_tmp);
947                                 }
948                                 else {
949                                         normalize_v3_v3(dvec_tmp, fly->dvec_prev);
950                                         if (fly->speed < 0.0f) {
951                                                 negate_v3(dvec_tmp);
952                                         }
953                                 }
954
955                                 mul_v3_fl(dvec_tmp, fly->speed * time_redraw * 0.25f);
956                         }
957
958                         /* impose a directional lag */
959                         interp_v3_v3v3(dvec, dvec_tmp, fly->dvec_prev, (1.0f / (1.0f + (time_redraw * FLY_SMOOTH_FAC))));
960
961                         if (rv3d->persp == RV3D_CAMOB) {
962                                 Object *lock_ob = ED_view3d_cameracontrol_object_get(fly->v3d_camera_control);
963                                 if (lock_ob->protectflag & OB_LOCK_LOCX) dvec[0] = 0.0;
964                                 if (lock_ob->protectflag & OB_LOCK_LOCY) dvec[1] = 0.0;
965                                 if (lock_ob->protectflag & OB_LOCK_LOCZ) dvec[2] = 0.0;
966                         }
967
968                         add_v3_v3(rv3d->ofs, dvec);
969
970                         if (rv3d->persp == RV3D_CAMOB) {
971                                 const bool do_rotate = ((fly->xlock != FLY_AXISLOCK_STATE_OFF) ||
972                                                         (fly->zlock != FLY_AXISLOCK_STATE_OFF) ||
973                                                         ((moffset[0] || moffset[1]) && !fly->pan_view));
974                                 const bool do_translate = (fly->speed != 0.0f || fly->pan_view);
975                                 flyMoveCamera(C, fly, do_rotate, do_translate);
976                         }
977
978                 }
979                 else {
980                         /* we're not redrawing but we need to update the time else the view will jump */
981                         fly->time_lastdraw = PIL_check_seconds_timer();
982                 }
983                 /* end drawing */
984                 copy_v3_v3(fly->dvec_prev, dvec);
985         }
986
987         return OPERATOR_FINISHED;
988 }
989
990 #ifdef WITH_INPUT_NDOF
991 static void flyApply_ndof(bContext *C, FlyInfo *fly)
992 {
993         Object *lock_ob = ED_view3d_cameracontrol_object_get(fly->v3d_camera_control);
994         bool has_translate, has_rotate;
995
996         view3d_ndof_fly(fly->ndof,
997                         fly->v3d, fly->rv3d,
998                         fly->use_precision, lock_ob ? lock_ob->protectflag : 0,
999                         &has_translate, &has_rotate);
1000
1001         if (has_translate || has_rotate) {
1002                 fly->redraw = true;
1003
1004                 if (fly->rv3d->persp == RV3D_CAMOB) {
1005                         flyMoveCamera(C, fly, has_rotate, has_translate);
1006                 }
1007         }
1008 }
1009 #endif /* WITH_INPUT_NDOF */
1010
1011 static int fly_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1012 {
1013         RegionView3D *rv3d = CTX_wm_region_view3d(C);
1014         FlyInfo *fly;
1015
1016         if (rv3d->viewlock & RV3D_LOCKED)
1017                 return OPERATOR_CANCELLED;
1018
1019         fly = MEM_callocN(sizeof(FlyInfo), "FlyOperation");
1020
1021         op->customdata = fly;
1022
1023         if (initFlyInfo(C, fly, op, event) == false) {
1024                 MEM_freeN(op->customdata);
1025                 return OPERATOR_CANCELLED;
1026         }
1027
1028         flyEvent(C, op, fly, event);
1029
1030         WM_event_add_modal_handler(C, op);
1031
1032         return OPERATOR_RUNNING_MODAL;
1033 }
1034
1035 static void fly_cancel(bContext *C, wmOperator *op)
1036 {
1037         FlyInfo *fly = op->customdata;
1038
1039         fly->state = FLY_CANCEL;
1040         flyEnd(C, fly);
1041         op->customdata = NULL;
1042 }
1043
1044 static int fly_modal(bContext *C, wmOperator *op, const wmEvent *event)
1045 {
1046         int exit_code;
1047         bool do_draw = false;
1048         FlyInfo *fly = op->customdata;
1049         RegionView3D *rv3d = fly->rv3d;
1050         Object *fly_object = ED_view3d_cameracontrol_object_get(fly->v3d_camera_control);
1051
1052         fly->redraw = 0;
1053
1054         flyEvent(C, op, fly, event);
1055
1056 #ifdef WITH_INPUT_NDOF
1057         if (fly->ndof) { /* 3D mouse overrules [2D mouse + timer] */
1058                 if (event->type == NDOF_MOTION) {
1059                         flyApply_ndof(C, fly);
1060                 }
1061         }
1062         else
1063 #endif /* WITH_INPUT_NDOF */
1064         if (event->type == TIMER && event->customdata == fly->timer) {
1065                 flyApply(C, fly);
1066         }
1067
1068         do_draw |= fly->redraw;
1069
1070         exit_code = flyEnd(C, fly);
1071
1072         if (exit_code != OPERATOR_RUNNING_MODAL)
1073                 do_draw = true;
1074
1075         if (do_draw) {
1076                 if (rv3d->persp == RV3D_CAMOB) {
1077                         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, fly_object);
1078                 }
1079
1080                 // puts("redraw!"); // too frequent, commented with NDOF_FLY_DRAW_TOOMUCH for now
1081                 ED_region_tag_redraw(CTX_wm_region(C));
1082         }
1083
1084         if (ELEM(exit_code, OPERATOR_FINISHED, OPERATOR_CANCELLED))
1085                 ED_area_headerprint(CTX_wm_area(C), NULL);
1086
1087         return exit_code;
1088 }
1089
1090 void VIEW3D_OT_fly(wmOperatorType *ot)
1091 {
1092         /* identifiers */
1093         ot->name = "Fly Navigation";
1094         ot->description = "Interactively fly around the scene";
1095         ot->idname = "VIEW3D_OT_fly";
1096
1097         /* api callbacks */
1098         ot->invoke = fly_invoke;
1099         ot->cancel = fly_cancel;
1100         ot->modal = fly_modal;
1101         ot->poll = ED_operator_region_view3d_active;
1102
1103         /* flags */
1104         ot->flag = OPTYPE_BLOCKING;
1105 }