code cleanup: replace MIN2/MAX2 with minf/maxf
[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 //#define NDOF_FLY_DEBUG
30 //#define NDOF_FLY_DRAW_TOOMUCH // is this needed for ndof? - commented so redraw doesnt thrash - campbell
31 #include "DNA_anim_types.h"
32 #include "DNA_scene_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_camera_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_context.h"
43 #include "BKE_object.h"
44 #include "BKE_report.h"
45
46 #include "BKE_depsgraph.h" /* for fly mode updating */
47
48 #include "BIF_gl.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "ED_keyframing.h"
54 #include "ED_screen.h"
55 #include "ED_space_api.h"
56
57 #include "PIL_time.h" /* smoothview */
58
59 #include "view3d_intern.h"  /* own include */
60 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
61 enum {
62         FLY_MODAL_CANCEL = 1,
63         FLY_MODAL_CONFIRM,
64         FLY_MODAL_ACCELERATE,
65         FLY_MODAL_DECELERATE,
66         FLY_MODAL_PAN_ENABLE,
67         FLY_MODAL_PAN_DISABLE,
68         FLY_MODAL_DIR_FORWARD,
69         FLY_MODAL_DIR_BACKWARD,
70         FLY_MODAL_DIR_LEFT,
71         FLY_MODAL_DIR_RIGHT,
72         FLY_MODAL_DIR_UP,
73         FLY_MODAL_DIR_DOWN,
74         FLY_MODAL_AXIS_LOCK_X,
75         FLY_MODAL_AXIS_LOCK_Z,
76         FLY_MODAL_PRECISION_ENABLE,
77         FLY_MODAL_PRECISION_DISABLE,
78         FLY_MODAL_FREELOOK_ENABLE,
79         FLY_MODAL_FREELOOK_DISABLE
80
81 };
82
83 /* called in transform_ops.c, on each regeneration of keymaps  */
84 void fly_modal_keymap(wmKeyConfig *keyconf)
85 {
86         static EnumPropertyItem modal_items[] = {
87                 {FLY_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
88                 {FLY_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
89                 {FLY_MODAL_ACCELERATE, "ACCELERATE", 0, "Accelerate", ""},
90                 {FLY_MODAL_DECELERATE, "DECELERATE", 0, "Decelerate", ""},
91
92                 {FLY_MODAL_PAN_ENABLE, "PAN_ENABLE", 0, "Pan Enable", ""},
93                 {FLY_MODAL_PAN_DISABLE, "PAN_DISABLE", 0, "Pan Disable", ""},
94
95                 {FLY_MODAL_DIR_FORWARD, "FORWARD", 0, "Fly Forward", ""},
96                 {FLY_MODAL_DIR_BACKWARD, "BACKWARD", 0, "Fly Backward", ""},
97                 {FLY_MODAL_DIR_LEFT, "LEFT", 0, "Fly Left", ""},
98                 {FLY_MODAL_DIR_RIGHT, "RIGHT", 0, "Fly Right", ""},
99                 {FLY_MODAL_DIR_UP, "UP", 0, "Fly Up", ""},
100                 {FLY_MODAL_DIR_DOWN, "DOWN", 0, "Fly Down", ""},
101
102                 {FLY_MODAL_AXIS_LOCK_X, "AXIS_LOCK_X", 0, "X Axis Correction", "X axis correction (toggle)"},
103                 {FLY_MODAL_AXIS_LOCK_Z, "AXIS_LOCK_Z", 0, "X Axis Correction", "Z axis correction (toggle)"},
104
105                 {FLY_MODAL_PRECISION_ENABLE, "PRECISION_ENABLE", 0, "Precision Enable", ""},
106                 {FLY_MODAL_PRECISION_DISABLE, "PRECISION_DISABLE", 0, "Precision Disable", ""},
107
108                 {FLY_MODAL_FREELOOK_ENABLE, "FREELOOK_ENABLE", 0, "Rotation Enable", ""},
109                 {FLY_MODAL_FREELOOK_DISABLE, "FREELOOK_DISABLE", 0, "Rotation Disable", ""},
110
111                 {0, NULL, 0, NULL, NULL}};
112
113         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Fly Modal");
114
115         /* this function is called for each spacetype, only needs to add map once */
116         if (keymap && keymap->modal_items)
117                 return;
118
119         keymap = WM_modalkeymap_add(keyconf, "View3D Fly Modal", modal_items);
120
121         /* items for modal map */
122         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_CANCEL);
123         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, FLY_MODAL_CANCEL);
124
125         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_ANY, KM_ANY, 0, FLY_MODAL_CONFIRM);
126         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_CONFIRM);
127         WM_modalkeymap_add_item(keymap, SPACEKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_CONFIRM);
128         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, KM_ANY, 0, FLY_MODAL_CONFIRM);
129
130         WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_ACCELERATE);
131         WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, KM_ANY, 0, FLY_MODAL_DECELERATE);
132         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_ANY, 0, FLY_MODAL_ACCELERATE);
133         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_ANY, 0, FLY_MODAL_DECELERATE);
134
135         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_PRESS, KM_ANY, 0, FLY_MODAL_PAN_ENABLE);
136         /* XXX - Bug in the event system, middle mouse release doesnt work */
137         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, FLY_MODAL_PAN_DISABLE);
138
139         /* WASD */
140         WM_modalkeymap_add_item(keymap, WKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_FORWARD);
141         WM_modalkeymap_add_item(keymap, SKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_BACKWARD);
142         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_LEFT);
143         WM_modalkeymap_add_item(keymap, DKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_RIGHT);
144         WM_modalkeymap_add_item(keymap, RKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_UP);
145         WM_modalkeymap_add_item(keymap, FKEY, KM_PRESS, 0, 0, FLY_MODAL_DIR_DOWN);
146
147         WM_modalkeymap_add_item(keymap, XKEY, KM_PRESS, 0, 0, FLY_MODAL_AXIS_LOCK_X);
148         WM_modalkeymap_add_item(keymap, ZKEY, KM_PRESS, 0, 0, FLY_MODAL_AXIS_LOCK_Z);
149
150         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_PRECISION_ENABLE);
151         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, FLY_MODAL_PRECISION_DISABLE);
152
153         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, FLY_MODAL_FREELOOK_ENABLE);
154         WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, FLY_MODAL_FREELOOK_DISABLE);
155
156         /* assign map to operators */
157         WM_modalkeymap_assign(keymap, "VIEW3D_OT_fly");
158 }
159
160 typedef struct FlyInfo {
161         /* context stuff */
162         RegionView3D *rv3d;
163         View3D *v3d;
164         ARegion *ar;
165         Scene *scene;
166
167         wmTimer *timer; /* needed for redraws */
168
169         short state;
170         short redraw;
171         unsigned char use_precision;
172         /* if the user presses shift they can look about
173          * without moving the direction there looking */
174         unsigned char use_freelook;
175
176         int mval[2]; /* latest 2D mouse values */
177         wmNDOFMotionData *ndof;  /* latest 3D mouse values */
178
179         /* fly state state */
180         float speed; /* the speed the view is moving per redraw */
181         short axis; /* Axis index to move along by default Z to move along the view */
182         short pan_view; /* when true, pan the view instead of rotating */
183
184         /* relative view axis locking - xlock, zlock
185          * 0) disabled
186          * 1) enabled but not checking because mouse hasn't moved outside the margin since locking was checked an not needed
187          *    when the mouse moves, locking is set to 2 so checks are done.
188          * 2) mouse moved and checking needed, if no view altering is done its changed back to 1 */
189         short xlock, zlock;
190         float xlock_momentum, zlock_momentum; /* nicer dynamics */
191         float grid; /* world scale 1.0 default */
192
193         /* root most parent */
194         Object *root_parent;
195
196         /* backup values */
197         float dist_backup; /* backup the views distance since we use a zero dist for fly mode */
198         float ofs_backup[3]; /* backup the views offset in case the user cancels flying in non camera mode */
199
200         /* backup the views quat in case the user cancels flying in non camera mode.
201          * (quat for view, eul for camera) */
202         float rot_backup[4];
203         short persp_backup; /* remember if were ortho or not, only used for restoring the view if it was a ortho view */
204
205         /* are we flying an ortho camera in perspective view,
206          * which was originall in ortho view?
207          * could probably figure it out but better be explicit */
208         short is_ortho_cam;
209         void *obtfm; /* backup the objects transform */
210
211         /* compare between last state */
212         double time_lastwheel; /* used to accelerate when using the mousewheel a lot */
213         double time_lastdraw; /* time between draws */
214
215         void *draw_handle_pixel;
216
217         /* use for some lag */
218         float dvec_prev[3]; /* old for some lag */
219
220 } FlyInfo;
221
222 static void drawFlyPixel(const struct bContext *UNUSED(C), ARegion *UNUSED(ar), void *arg)
223 {
224         FlyInfo *fly = arg;
225
226         /* draws 4 edge brackets that frame the safe area where the
227          * mouse can move during fly mode without spinning the view */
228         float x1, x2, y1, y2;
229
230         x1 = 0.45f * (float)fly->ar->winx;
231         y1 = 0.45f * (float)fly->ar->winy;
232         x2 = 0.55f * (float)fly->ar->winx;
233         y2 = 0.55f * (float)fly->ar->winy;
234         cpack(0);
235
236         glBegin(GL_LINES);
237         /* bottom left */
238         glVertex2f(x1, y1);
239         glVertex2f(x1, y1 + 5);
240
241         glVertex2f(x1, y1);
242         glVertex2f(x1 + 5, y1);
243
244         /* top right */
245         glVertex2f(x2, y2);
246         glVertex2f(x2, y2 - 5);
247
248         glVertex2f(x2, y2);
249         glVertex2f(x2 - 5, y2);
250
251         /* top left */
252         glVertex2f(x1, y2);
253         glVertex2f(x1, y2 - 5);
254
255         glVertex2f(x1, y2);
256         glVertex2f(x1 + 5, y2);
257
258         /* bottom right */
259         glVertex2f(x2, y1);
260         glVertex2f(x2, y1 + 5);
261
262         glVertex2f(x2, y1);
263         glVertex2f(x2 - 5, y1);
264         glEnd();
265 }
266
267 /* FlyInfo->state */
268 #define FLY_RUNNING     0
269 #define FLY_CANCEL      1
270 #define FLY_CONFIRM     2
271
272 static int initFlyInfo(bContext *C, FlyInfo *fly, wmOperator *op, wmEvent *event)
273 {
274         wmWindow *win = CTX_wm_window(C);
275         float upvec[3]; /* tmp */
276         float mat[3][3];
277
278         fly->rv3d = CTX_wm_region_view3d(C);
279         fly->v3d = CTX_wm_view3d(C);
280         fly->ar = CTX_wm_region(C);
281         fly->scene = CTX_data_scene(C);
282
283 #ifdef NDOF_FLY_DEBUG
284         puts("\n-- fly begin --");
285 #endif
286
287         if (fly->rv3d->persp == RV3D_CAMOB && fly->v3d->camera->id.lib) {
288                 BKE_report(op->reports, RPT_ERROR, "Cannot fly a camera from an external library");
289                 return FALSE;
290         }
291
292         if (fly->v3d->ob_centre) {
293                 BKE_report(op->reports, RPT_ERROR, "Cannot fly when the view is locked to an object");
294                 return FALSE;
295         }
296
297         if (fly->rv3d->persp == RV3D_CAMOB && fly->v3d->camera->constraints.first) {
298                 BKE_report(op->reports, RPT_ERROR, "Cannot fly an object with constraints");
299                 return FALSE;
300         }
301
302         fly->state = FLY_RUNNING;
303         fly->speed = 0.0f;
304         fly->axis = 2;
305         fly->pan_view = FALSE;
306         fly->xlock = FALSE;
307         fly->zlock = FALSE;
308         fly->xlock_momentum = 0.0f;
309         fly->zlock_momentum = 0.0f;
310         fly->grid = 1.0f;
311         fly->use_precision = FALSE;
312         fly->use_freelook = FALSE;
313
314 #ifdef NDOF_FLY_DRAW_TOOMUCH
315         fly->redraw = 1;
316 #endif
317         zero_v3(fly->dvec_prev);
318
319         fly->timer = WM_event_add_timer(CTX_wm_manager(C), win, TIMER, 0.01f);
320
321         copy_v2_v2_int(fly->mval, event->mval);
322         fly->ndof = NULL;
323
324         fly->time_lastdraw = fly->time_lastwheel = PIL_check_seconds_timer();
325
326         fly->draw_handle_pixel = ED_region_draw_cb_activate(fly->ar->type, drawFlyPixel, fly, REGION_DRAW_POST_PIXEL);
327
328         fly->rv3d->rflag |= RV3D_NAVIGATING; /* so we draw the corner margins */
329
330         /* detect weather to start with Z locking */
331         upvec[0] = 1.0f;
332         upvec[1] = 0.0f;
333         upvec[2] = 0.0f;
334         copy_m3_m4(mat, fly->rv3d->viewinv);
335         mul_m3_v3(mat, upvec);
336         if (fabsf(upvec[2]) < 0.1f) {
337                 fly->zlock = 1;
338         }
339         upvec[0] = 0;
340         upvec[1] = 0;
341         upvec[2] = 0;
342
343         fly->persp_backup = fly->rv3d->persp;
344         fly->dist_backup = fly->rv3d->dist;
345
346         /* check for flying ortho camera - which we cant support well
347          * we _could_ also check for an ortho camera but this is easier */
348         if ((fly->rv3d->persp == RV3D_CAMOB) &&
349             (fly->v3d->camera != NULL) &&
350             (fly->rv3d->is_persp == FALSE))
351         {
352                 ((Camera *)fly->v3d->camera->data)->type = CAM_PERSP;
353                 fly->is_ortho_cam = TRUE;
354         }
355
356         if (fly->rv3d->persp == RV3D_CAMOB) {
357                 Object *ob_back;
358                 if ((U.uiflag & USER_CAM_LOCK_NO_PARENT) == 0 && (fly->root_parent = fly->v3d->camera->parent)) {
359                         while (fly->root_parent->parent)
360                                 fly->root_parent = fly->root_parent->parent;
361                         ob_back = fly->root_parent;
362                 }
363                 else {
364                         ob_back = fly->v3d->camera;
365                 }
366
367                 /* store the original camera loc and rot */
368                 /* TODO. axis angle etc */
369
370                 fly->obtfm = BKE_object_tfm_backup(ob_back);
371
372                 BKE_object_where_is_calc(fly->scene, fly->v3d->camera);
373                 negate_v3_v3(fly->rv3d->ofs, fly->v3d->camera->obmat[3]);
374
375                 fly->rv3d->dist = 0.0;
376         }
377         else {
378                 /* perspective or ortho */
379                 if (fly->rv3d->persp == RV3D_ORTHO)
380                         fly->rv3d->persp = RV3D_PERSP;  /* if ortho projection, make perspective */
381
382                 copy_qt_qt(fly->rot_backup, fly->rv3d->viewquat);
383                 copy_v3_v3(fly->ofs_backup, fly->rv3d->ofs);
384
385                 /* the dist defines a vector that is infront of the offset
386                  * to rotate the view about.
387                  * this is no good for fly mode because we
388                  * want to rotate about the viewers center.
389                  * but to correct the dist removal we must
390                  * alter offset so the view doesn't jump. */
391
392                 fly->rv3d->dist = 0.0f;
393
394                 upvec[2] = fly->dist_backup; /* x and y are 0 */
395                 mul_m3_v3(mat, upvec);
396                 sub_v3_v3(fly->rv3d->ofs, upvec);
397                 /* Done with correcting for the dist */
398         }
399
400         /* center the mouse, probably the UI mafia are against this but without its quite annoying */
401         WM_cursor_warp(win, fly->ar->winrct.xmin + fly->ar->winx / 2, fly->ar->winrct.ymin + fly->ar->winy / 2);
402
403         return 1;
404 }
405
406 static int flyEnd(bContext *C, FlyInfo *fly)
407 {
408         RegionView3D *rv3d = fly->rv3d;
409         View3D *v3d = fly->v3d;
410
411         float upvec[3];
412
413         if (fly->state == FLY_RUNNING)
414                 return OPERATOR_RUNNING_MODAL;
415
416 #ifdef NDOF_FLY_DEBUG
417         puts("\n-- fly end --");
418 #endif
419
420         WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), fly->timer);
421
422         ED_region_draw_cb_exit(fly->ar->type, fly->draw_handle_pixel);
423
424         rv3d->dist = fly->dist_backup;
425
426         if (fly->state == FLY_CANCEL) {
427                 /* Revert to original view? */
428                 if (fly->persp_backup == RV3D_CAMOB) { /* a camera view */
429                         Object *ob_back;
430                         ob_back = (fly->root_parent) ? fly->root_parent : fly->v3d->camera;
431
432                         /* store the original camera loc and rot */
433                         BKE_object_tfm_restore(ob_back, fly->obtfm);
434
435                         DAG_id_tag_update(&ob_back->id, OB_RECALC_OB);
436                 }
437                 else {
438                         /* Non Camera we need to reset the view back to the original location bacause the user canceled*/
439                         copy_qt_qt(rv3d->viewquat, fly->rot_backup);
440                         copy_v3_v3(rv3d->ofs, fly->ofs_backup);
441                         rv3d->persp = fly->persp_backup;
442                 }
443         }
444         else if (fly->persp_backup == RV3D_CAMOB) { /* camera */
445                 DAG_id_tag_update(fly->root_parent ? &fly->root_parent->id : &v3d->camera->id, OB_RECALC_OB);
446         }
447         else { /* not camera */
448
449                 /* Apply the fly mode view */
450                 /* restore the dist */
451                 float mat[3][3];
452                 upvec[0] = upvec[1] = 0;
453                 upvec[2] = fly->dist_backup; /* x and y are 0 */
454                 copy_m3_m4(mat, rv3d->viewinv);
455                 mul_m3_v3(mat, upvec);
456                 add_v3_v3(rv3d->ofs, upvec);
457                 /* Done with correcting for the dist */
458         }
459
460         if (fly->is_ortho_cam) {
461                 ((Camera *)fly->v3d->camera->data)->type = CAM_ORTHO;
462         }
463
464         rv3d->rflag &= ~RV3D_NAVIGATING;
465 //XXX2.5        BIF_view3d_previewrender_signal(fly->sa, PR_DBASE|PR_DISPRECT); /* not working at the moment not sure why */
466
467         if (fly->obtfm)
468                 MEM_freeN(fly->obtfm);
469         if (fly->ndof)
470                 MEM_freeN(fly->ndof);
471
472         if (fly->state == FLY_CONFIRM) {
473                 MEM_freeN(fly);
474                 return OPERATOR_FINISHED;
475         }
476
477         MEM_freeN(fly);
478         return OPERATOR_CANCELLED;
479 }
480
481 static void flyEvent(FlyInfo *fly, wmEvent *event)
482 {
483         if (event->type == TIMER && event->customdata == fly->timer) {
484                 fly->redraw = 1;
485         }
486         else if (event->type == MOUSEMOVE) {
487                 copy_v2_v2_int(fly->mval, event->mval);
488         }
489         else if (event->type == NDOF_MOTION) {
490                 /* do these automagically get delivered? yes. */
491                 // puts("ndof motion detected in fly mode!");
492                 // static const char* tag_name = "3D mouse position";
493
494                 wmNDOFMotionData *incoming_ndof = (wmNDOFMotionData *)event->customdata;
495                 switch (incoming_ndof->progress) {
496                         case P_STARTING:
497                                 /* start keeping track of 3D mouse position */
498 #ifdef NDOF_FLY_DEBUG
499                                 puts("start keeping track of 3D mouse position");
500 #endif
501                         /* fall through... */
502                         case P_IN_PROGRESS:
503                                 /* update 3D mouse position */
504 #ifdef NDOF_FLY_DEBUG
505                                 putchar('.'); fflush(stdout);
506 #endif
507                                 if (fly->ndof == NULL) {
508                                         // fly->ndof = MEM_mallocN(sizeof(wmNDOFMotionData), tag_name);
509                                         fly->ndof = MEM_dupallocN(incoming_ndof);
510                                         // fly->ndof = malloc(sizeof(wmNDOFMotionData));
511                                 }
512                                 else {
513                                         memcpy(fly->ndof, incoming_ndof, sizeof(wmNDOFMotionData));
514                                 }
515                                 break;
516                         case P_FINISHING:
517                                 /* stop keeping track of 3D mouse position */
518 #ifdef NDOF_FLY_DEBUG
519                                 puts("stop keeping track of 3D mouse position");
520 #endif
521                                 if (fly->ndof) {
522                                         MEM_freeN(fly->ndof);
523                                         // free(fly->ndof);
524                                         fly->ndof = NULL;
525                                 }
526                                 /* update the time else the view will jump when 2D mouse/timer resume */
527                                 fly->time_lastdraw = PIL_check_seconds_timer();
528                                 break;
529                         default:
530                                 break; /* should always be one of the above 3 */
531                 }
532         }
533         /* handle modal keymap first */
534         else if (event->type == EVT_MODAL_MAP) {
535                 switch (event->val) {
536                         case FLY_MODAL_CANCEL:
537                                 fly->state = FLY_CANCEL;
538                                 break;
539                         case FLY_MODAL_CONFIRM:
540                                 fly->state = FLY_CONFIRM;
541                                 break;
542
543                         case FLY_MODAL_ACCELERATE:
544                         {
545                                 double time_currwheel;
546                                 float time_wheel;
547
548                                 time_currwheel = PIL_check_seconds_timer();
549                                 time_wheel = (float)(time_currwheel - fly->time_lastwheel);
550                                 fly->time_lastwheel = time_currwheel;
551                                 /* Mouse wheel delays range from (0.5 == slow) to (0.01 == fast) */
552                                 time_wheel = 1.0f + (10.0f - (20.0f * minf(time_wheel, 0.5f))); /* 0-0.5 -> 0-5.0 */
553
554                                 if (fly->speed < 0.0f) {
555                                         fly->speed = 0.0f;
556                                 }
557                                 else {
558                                         fly->speed += fly->grid * time_wheel * (fly->use_precision ? 0.1f : 1.0f);
559                                 }
560                                 break;
561                         }
562                         case FLY_MODAL_DECELERATE:
563                         {
564                                 double time_currwheel;
565                                 float time_wheel;
566
567                                 time_currwheel = PIL_check_seconds_timer();
568                                 time_wheel = (float)(time_currwheel - fly->time_lastwheel);
569                                 fly->time_lastwheel = time_currwheel;
570                                 time_wheel = 1.0f + (10.0f - (20.0f * minf(time_wheel, 0.5f))); /* 0-0.5 -> 0-5.0 */
571
572                                 if (fly->speed > 0.0f) {
573                                         fly->speed = 0;
574                                 }
575                                 else {
576                                         fly->speed -= fly->grid * time_wheel * (fly->use_precision ? 0.1f : 1.0f);
577                                 }
578                                 break;
579                         }
580                         case FLY_MODAL_PAN_ENABLE:
581                                 fly->pan_view = TRUE;
582                                 break;
583                         case FLY_MODAL_PAN_DISABLE:
584 //XXX2.5                                WM_cursor_warp(CTX_wm_window(C), cent_orig[0], cent_orig[1]);
585                                 fly->pan_view = FALSE;
586                                 break;
587
588                         /* implement WASD keys,
589                          * comments only for 'forward '*/
590                         case FLY_MODAL_DIR_FORWARD:
591                                 if (fly->axis == 2 && fly->speed < 0.0f) { /* reverse direction stops, tap again to continue */
592                                         fly->axis = -1;
593                                 }
594                                 else {
595                                         /* flip speed rather than stopping, game like motion,
596                                          * else increase like mousewheel if were already moving in that direction */
597                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
598                                         else if (fly->axis == 2) fly->speed += fly->grid;
599                                         fly->axis = 2;
600                                 }
601                                 break;
602                         case FLY_MODAL_DIR_BACKWARD:
603                                 if (fly->axis == 2 && fly->speed > 0.0f) {
604                                         fly->axis = -1;
605                                 }
606                                 else {
607                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
608                                         else if (fly->axis == 2) fly->speed -= fly->grid;
609
610                                         fly->axis = 2;
611                                 }
612                                 break;
613                         case FLY_MODAL_DIR_LEFT:
614                                 if (fly->axis == 0 && fly->speed < 0.0f) {
615                                         fly->axis = -1;
616                                 }
617                                 else {
618                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
619                                         else if (fly->axis == 0) fly->speed += fly->grid;
620
621                                         fly->axis = 0;
622                                 }
623                                 break;
624                         case FLY_MODAL_DIR_RIGHT:
625                                 if (fly->axis == 0 && fly->speed > 0.0f) {
626                                         fly->axis = -1;
627                                 }
628                                 else {
629                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
630                                         else if (fly->axis == 0) fly->speed -= fly->grid;
631
632                                         fly->axis = 0;
633                                 }
634                                 break;
635                         case FLY_MODAL_DIR_DOWN:
636                                 if (fly->axis == 1 && fly->speed < 0.0f) {
637                                         fly->axis = -1;
638                                 }
639                                 else {
640                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
641                                         else if (fly->axis == 1) fly->speed += fly->grid;
642                                         fly->axis = 1;
643                                 }
644                                 break;
645                         case FLY_MODAL_DIR_UP:
646                                 if (fly->axis == 1 && fly->speed > 0.0f) {
647                                         fly->axis = -1;
648                                 }
649                                 else {
650                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
651                                         else if (fly->axis == 1) fly->speed -= fly->grid;
652                                         fly->axis = 1;
653                                 }
654                                 break;
655
656                         case FLY_MODAL_AXIS_LOCK_X:
657                                 if (fly->xlock)
658                                         fly->xlock = 0;
659                                 else {
660                                         fly->xlock = 2;
661                                         fly->xlock_momentum = 0.0;
662                                 }
663                                 break;
664                         case FLY_MODAL_AXIS_LOCK_Z:
665                                 if (fly->zlock)
666                                         fly->zlock = 0;
667                                 else {
668                                         fly->zlock = 2;
669                                         fly->zlock_momentum = 0.0;
670                                 }
671                                 break;
672
673                         case FLY_MODAL_PRECISION_ENABLE:
674                                 fly->use_precision = TRUE;
675                                 break;
676                         case FLY_MODAL_PRECISION_DISABLE:
677                                 fly->use_precision = FALSE;
678                                 break;
679
680                         case FLY_MODAL_FREELOOK_ENABLE:
681                                 fly->use_freelook = TRUE;
682                                 break;
683                         case FLY_MODAL_FREELOOK_DISABLE:
684                                 fly->use_freelook = FALSE;
685                                 break;
686                 }
687         }
688 }
689
690 static void move_camera(bContext *C, RegionView3D *rv3d, FlyInfo *fly, int orientationChanged, int positionChanged)
691 {
692         /* we are in camera view so apply the view ofs and quat to the view matrix and set the camera to the view */
693
694         View3D *v3d = fly->v3d;
695         Scene *scene = fly->scene;
696         ID *id_key;
697
698         /* transform the parent or the camera? */
699         if (fly->root_parent) {
700                 Object *ob_update;
701
702                 float view_mat[4][4];
703                 float prev_view_mat[4][4];
704                 float prev_view_imat[4][4];
705                 float diff_mat[4][4];
706                 float parent_mat[4][4];
707
708                 ED_view3d_to_m4(prev_view_mat, fly->rv3d->ofs, fly->rv3d->viewquat, fly->rv3d->dist);
709                 invert_m4_m4(prev_view_imat, prev_view_mat);
710                 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
711                 mult_m4_m4m4(diff_mat, view_mat, prev_view_imat);
712                 mult_m4_m4m4(parent_mat, diff_mat, fly->root_parent->obmat);
713                 BKE_object_apply_mat4(fly->root_parent, parent_mat, TRUE, FALSE);
714
715                 // BKE_object_where_is_calc(scene, fly->root_parent);
716
717                 ob_update = v3d->camera->parent;
718                 while (ob_update) {
719                         DAG_id_tag_update(&ob_update->id, OB_RECALC_OB);
720                         ob_update = ob_update->parent;
721                 }
722
723                 id_key = &fly->root_parent->id;
724         }
725         else {
726                 float view_mat[4][4];
727                 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
728                 BKE_object_apply_mat4(v3d->camera, view_mat, TRUE, FALSE);
729                 id_key = &v3d->camera->id;
730         }
731
732         /* record the motion */
733         if (autokeyframe_cfra_can_key(scene, id_key)) {
734                 ListBase dsources = {NULL, NULL};
735
736                 /* add datasource override for the camera object */
737                 ANIM_relative_keyingset_add_source(&dsources, id_key, NULL, NULL);
738
739                 /* insert keyframes 
740                  *      1) on the first frame
741                  *      2) on each subsequent frame
742                  *              TODO: need to check in future that frame changed before doing this 
743                  */
744                 if (orientationChanged) {
745                         KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_ROTATION_ID);
746                         ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
747                 }
748                 if (positionChanged) {
749                         KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_LOCATION_ID);
750                         ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
751                 }
752
753                 /* free temp data */
754                 BLI_freelistN(&dsources);
755         }
756 }
757
758 static int flyApply(bContext *C, FlyInfo *fly)
759 {
760 #define FLY_ROTATE_FAC 2.5f /* more is faster */
761 #define FLY_ZUP_CORRECT_FAC 0.1f /* amount to correct per step */
762 #define FLY_ZUP_CORRECT_ACCEL 0.05f /* increase upright momentum each step */
763
764         /* fly mode - Shift+F
765          * a fly loop where the user can move move the view as if they are flying
766          */
767         RegionView3D *rv3d = fly->rv3d;
768         ARegion *ar = fly->ar;
769
770         float mat[3][3]; /* 3x3 copy of the view matrix so we can move along the view axis */
771         float dvec[3] = {0, 0, 0}; /* this is the direction thast added to the view offset per redraw */
772
773         /* Camera Uprighting variables */
774         float upvec[3] = {0, 0, 0}; /* stores the view's up vector */
775
776         float moffset[2]; /* mouse offset from the views center */
777         float tmp_quat[4]; /* used for rotating the view */
778
779 //      int cent_orig[2], /* view center */
780 //XXX- can avoid using //   cent[2], /* view center modified */
781         int xmargin, ymargin; /* x and y margin are define the safe area where the mouses movement wont rotate the view */
782
783 #ifdef NDOF_FLY_DEBUG
784         {
785                 static unsigned int iteration = 1;
786                 printf("fly timer %d\n", iteration++);
787         }
788 #endif
789
790         xmargin = ar->winx / 20.0f;
791         ymargin = ar->winy / 20.0f;
792
793         // UNUSED
794         // cent_orig[0]= ar->winrct.xmin + ar->winx/2;
795         // cent_orig[1]= ar->winrct.ymin + ar->winy/2;
796
797         {
798
799                 /* mouse offset from the center */
800                 moffset[0] = fly->mval[0] - ar->winx / 2;
801                 moffset[1] = fly->mval[1] - ar->winy / 2;
802
803                 /* enforce a view margin */
804                 if      (moffset[0] >  xmargin) moffset[0] -= xmargin;
805                 else if (moffset[0] < -xmargin) moffset[0] += xmargin;
806                 else                            moffset[0] =  0;
807
808                 if      (moffset[1] >  ymargin) moffset[1] -= ymargin;
809                 else if (moffset[1] < -ymargin) moffset[1] += ymargin;
810                 else                            moffset[1] =  0;
811
812
813                 /* scale the mouse movement by this value - scales mouse movement to the view size
814                  * moffset[0]/(ar->winx-xmargin*2) - window size minus margin (same for y)
815                  *
816                  * the mouse moves isn't linear */
817
818                 if (moffset[0]) {
819                         moffset[0] /= ar->winx - (xmargin * 2);
820                         moffset[0] *= fabsf(moffset[0]);
821                 }
822
823                 if (moffset[1]) {
824                         moffset[1] /= ar->winy - (ymargin * 2);
825                         moffset[1] *= fabsf(moffset[1]);
826                 }
827
828                 /* Should we redraw? */
829                 if ((fly->speed != 0.0f) ||
830                     moffset[0] || moffset[1] ||
831                     fly->zlock || fly->xlock ||
832                     dvec[0] || dvec[1] || dvec[2])
833                 {
834                         float dvec_tmp[3];
835
836                         /* time how fast it takes for us to redraw,
837                          * this is so simple scenes don't fly too fast */
838                         double time_current;
839                         float time_redraw;
840                         float time_redraw_clamped;
841 #ifdef NDOF_FLY_DRAW_TOOMUCH
842                         fly->redraw = 1;
843 #endif
844                         time_current = PIL_check_seconds_timer();
845                         time_redraw = (float)(time_current - fly->time_lastdraw);
846                         time_redraw_clamped = minf(0.05f, time_redraw); /* clamp redraw time to avoid jitter in roll correction */
847                         fly->time_lastdraw = time_current;
848
849                         /* Scale the time to use shift to scale the speed down- just like
850                          * shift slows many other areas of blender down */
851                         if (fly->use_precision)
852                                 fly->speed = fly->speed * (1.0f - time_redraw_clamped);
853
854                         copy_m3_m4(mat, rv3d->viewinv);
855
856                         if (fly->pan_view == TRUE) {
857                                 /* pan only */
858                                 dvec_tmp[0] = -moffset[0];
859                                 dvec_tmp[1] = -moffset[1];
860                                 dvec_tmp[2] = 0;
861
862                                 if (fly->use_precision) {
863                                         dvec_tmp[0] *= 0.1f;
864                                         dvec_tmp[1] *= 0.1f;
865                                 }
866
867                                 mul_m3_v3(mat, dvec_tmp);
868                                 mul_v3_fl(dvec_tmp, time_redraw * 200.0f * fly->grid);
869                         }
870                         else {
871                                 float roll; /* similar to the angle between the camera's up and the Z-up,
872                                              * but its very rough so just roll */
873
874                                 /* rotate about the X axis- look up/down */
875                                 if (moffset[1]) {
876                                         upvec[0] = 1;
877                                         upvec[1] = 0;
878                                         upvec[2] = 0;
879                                         mul_m3_v3(mat, upvec);
880                                         /* Rotate about the relative up vec */
881                                         axis_angle_to_quat(tmp_quat, upvec, (float)moffset[1] * time_redraw * -FLY_ROTATE_FAC);
882                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
883
884                                         if (fly->xlock)
885                                                 fly->xlock = 2;  /* check for rotation */
886                                         if (fly->zlock)
887                                                 fly->zlock = 2;
888                                         fly->xlock_momentum = 0.0f;
889                                 }
890
891                                 /* rotate about the Y axis- look left/right */
892                                 if (moffset[0]) {
893
894                                         /* if we're upside down invert the moffset */
895                                         upvec[0] = 0.0f;
896                                         upvec[1] = 1.0f;
897                                         upvec[2] = 0.0f;
898                                         mul_m3_v3(mat, upvec);
899
900                                         if (upvec[2] < 0.0f)
901                                                 moffset[0] = -moffset[0];
902
903                                         /* make the lock vectors */
904                                         if (fly->zlock) {
905                                                 upvec[0] = 0.0f;
906                                                 upvec[1] = 0.0f;
907                                                 upvec[2] = 1.0f;
908                                         }
909                                         else {
910                                                 upvec[0] = 0.0f;
911                                                 upvec[1] = 1.0f;
912                                                 upvec[2] = 0.0f;
913                                                 mul_m3_v3(mat, upvec);
914                                         }
915
916                                         /* Rotate about the relative up vec */
917                                         axis_angle_to_quat(tmp_quat, upvec, (float)moffset[0] * time_redraw * FLY_ROTATE_FAC);
918                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
919
920                                         if (fly->xlock)
921                                                 fly->xlock = 2;  /* check for rotation */
922                                         if (fly->zlock)
923                                                 fly->zlock = 2;
924                                 }
925
926                                 if (fly->zlock == 2) {
927                                         upvec[0] = 1.0f;
928                                         upvec[1] = 0.0f;
929                                         upvec[2] = 0.0f;
930                                         mul_m3_v3(mat, upvec);
931
932                                         /* make sure we have some z rolling */
933                                         if (fabsf(upvec[2]) > 0.00001f) {
934                                                 roll = upvec[2] * 5.0f;
935                                                 upvec[0] = 0.0f; /* rotate the view about this axis */
936                                                 upvec[1] = 0.0f;
937                                                 upvec[2] = 1.0f;
938
939                                                 mul_m3_v3(mat, upvec);
940                                                 /* Rotate about the relative up vec */
941                                                 axis_angle_to_quat(tmp_quat, upvec,
942                                                                    roll * time_redraw_clamped * fly->zlock_momentum * FLY_ZUP_CORRECT_FAC);
943                                                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
944
945                                                 fly->zlock_momentum += FLY_ZUP_CORRECT_ACCEL;
946                                         }
947                                         else {
948                                                 fly->zlock = 1; /* don't check until the view rotates again */
949                                                 fly->zlock_momentum = 0.0f;
950                                         }
951                                 }
952
953                                 if (fly->xlock == 2 && moffset[1] == 0) { /* only apply xcorrect when mouse isn't applying x rot */
954                                         upvec[0] = 0;
955                                         upvec[1] = 0;
956                                         upvec[2] = 1;
957                                         mul_m3_v3(mat, upvec);
958                                         /* make sure we have some z rolling */
959                                         if (fabsf(upvec[2]) > 0.00001f) {
960                                                 roll = upvec[2] * -5.0f;
961
962                                                 upvec[0] = 1.0f; /* rotate the view about this axis */
963                                                 upvec[1] = 0.0f;
964                                                 upvec[2] = 0.0f;
965
966                                                 mul_m3_v3(mat, upvec);
967
968                                                 /* Rotate about the relative up vec */
969                                                 axis_angle_to_quat(tmp_quat, upvec, roll * time_redraw_clamped * fly->xlock_momentum * 0.1f);
970                                                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
971
972                                                 fly->xlock_momentum += 0.05f;
973                                         }
974                                         else {
975                                                 fly->xlock = 1; /* see above */
976                                                 fly->xlock_momentum = 0.0f;
977                                         }
978                                 }
979
980                                 if (fly->axis == -1) {
981                                         /* pause */
982                                         zero_v3(dvec_tmp);
983                                 }
984                                 else if (!fly->use_freelook) {
985                                         /* Normal operation */
986                                         /* define dvec, view direction vector */
987                                         zero_v3(dvec_tmp);
988                                         /* move along the current axis */
989                                         dvec_tmp[fly->axis] = 1.0f;
990
991                                         mul_m3_v3(mat, dvec_tmp);
992                                 }
993                                 else {
994                                         normalize_v3_v3(dvec_tmp, fly->dvec_prev);
995                                         if (fly->speed < 0.0f) {
996                                                 negate_v3(dvec_tmp);
997                                         }
998                                 }
999
1000                                 mul_v3_fl(dvec_tmp, fly->speed * time_redraw * 0.25f);
1001                         }
1002
1003                         /* impose a directional lag */
1004                         interp_v3_v3v3(dvec, dvec_tmp, fly->dvec_prev, (1.0f / (1.0f + (time_redraw * 5.0f))));
1005
1006                         if (rv3d->persp == RV3D_CAMOB) {
1007                                 Object *lock_ob = fly->root_parent ? fly->root_parent : fly->v3d->camera;
1008                                 if (lock_ob->protectflag & OB_LOCK_LOCX) dvec[0] = 0.0;
1009                                 if (lock_ob->protectflag & OB_LOCK_LOCY) dvec[1] = 0.0;
1010                                 if (lock_ob->protectflag & OB_LOCK_LOCZ) dvec[2] = 0.0;
1011                         }
1012
1013                         add_v3_v3(rv3d->ofs, dvec);
1014
1015                         if (rv3d->persp == RV3D_CAMOB)
1016                                 move_camera(C, rv3d, fly, (fly->xlock || fly->zlock || moffset[0] || moffset[1]), fly->speed);
1017
1018                 }
1019                 else {
1020                         /* we're not redrawing but we need to update the time else the view will jump */
1021                         fly->time_lastdraw = PIL_check_seconds_timer();
1022                 }
1023                 /* end drawing */
1024                 copy_v3_v3(fly->dvec_prev, dvec);
1025         }
1026
1027         return OPERATOR_FINISHED;
1028 }
1029
1030 static int flyApply_ndof(bContext *C, FlyInfo *fly)
1031 {
1032         /* shorthand for oft-used variables */
1033         wmNDOFMotionData *ndof = fly->ndof;
1034         const float dt = ndof->dt;
1035         RegionView3D *rv3d = fly->rv3d;
1036         const int flag = U.ndof_flag;
1037
1038 #if 0
1039         int shouldRotate = (flag & NDOF_SHOULD_ROTATE) && (fly->pan_view == FALSE),
1040             shouldTranslate = (flag & (NDOF_SHOULD_PAN | NDOF_SHOULD_ZOOM));
1041 #endif
1042
1043         int shouldRotate = (fly->pan_view == FALSE);
1044         int shouldTranslate = TRUE;
1045
1046         float view_inv[4];
1047         invert_qt_qt(view_inv, rv3d->viewquat);
1048
1049         rv3d->rot_angle = 0.0f; /* disable onscreen rotation doo-dad */
1050
1051         if (shouldTranslate) {
1052                 const float forward_sensitivity  = 1.0f;
1053                 const float vertical_sensitivity = 0.4f;
1054                 const float lateral_sensitivity  = 0.6f;
1055
1056                 float speed = 10.0f; /* blender units per second */
1057                 /* ^^ this is ok for default cube scene, but should scale with.. something */
1058
1059                 float trans[3] = {lateral_sensitivity  * ndof->tvec[0],
1060                                   vertical_sensitivity * ndof->tvec[1],
1061                                   forward_sensitivity  * ndof->tvec[2]};
1062
1063                 if (fly->use_precision)
1064                         speed *= 0.2f;
1065
1066                 mul_v3_fl(trans, speed * dt);
1067
1068                 /* transform motion from view to world coordinates */
1069                 mul_qt_v3(view_inv, trans);
1070
1071                 if (flag & NDOF_FLY_HELICOPTER) {
1072                         /* replace world z component with device y (yes it makes sense) */
1073                         trans[2] = speed * dt * vertical_sensitivity * ndof->tvec[1];
1074                 }
1075
1076                 if (rv3d->persp == RV3D_CAMOB) {
1077                         /* respect camera position locks */
1078                         Object *lock_ob = fly->root_parent ? fly->root_parent : fly->v3d->camera;
1079                         if (lock_ob->protectflag & OB_LOCK_LOCX) trans[0] = 0.0f;
1080                         if (lock_ob->protectflag & OB_LOCK_LOCY) trans[1] = 0.0f;
1081                         if (lock_ob->protectflag & OB_LOCK_LOCZ) trans[2] = 0.0f;
1082                 }
1083
1084                 if (!is_zero_v3(trans)) {
1085                         /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1086                         sub_v3_v3(rv3d->ofs, trans);
1087                         shouldTranslate = TRUE;
1088                 }
1089                 else {
1090                         shouldTranslate = FALSE;
1091                 }
1092         }
1093
1094         if (shouldRotate) {
1095                 const float turn_sensitivity = 1.0f;
1096
1097                 float rotation[4];
1098                 float axis[3];
1099                 float angle = turn_sensitivity * ndof_to_axis_angle(ndof, axis);
1100
1101                 if (fabsf(angle) > 0.0001f) {
1102                         shouldRotate = TRUE;
1103
1104                         if (fly->use_precision)
1105                                 angle *= 0.2f;
1106
1107                         /* transform rotation axis from view to world coordinates */
1108                         mul_qt_v3(view_inv, axis);
1109
1110                         /* apply rotation to view */
1111                         axis_angle_to_quat(rotation, axis, angle);
1112                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
1113
1114                         if (flag & NDOF_LOCK_HORIZON) {
1115                                 /* force an upright viewpoint
1116                                  * TODO: make this less... sudden */
1117                                 float view_horizon[3] = {1.0f, 0.0f, 0.0f}; /* view +x */
1118                                 float view_direction[3] = {0.0f, 0.0f, -1.0f}; /* view -z (into screen) */
1119
1120                                 /* find new inverse since viewquat has changed */
1121                                 invert_qt_qt(view_inv, rv3d->viewquat);
1122                                 /* could apply reverse rotation to existing view_inv to save a few cycles */
1123
1124                                 /* transform view vectors to world coordinates */
1125                                 mul_qt_v3(view_inv, view_horizon);
1126                                 mul_qt_v3(view_inv, view_direction);
1127
1128                                 /* find difference between view & world horizons
1129                                  * true horizon lives in world xy plane, so look only at difference in z */
1130                                 angle = -asinf(view_horizon[2]);
1131
1132 #ifdef NDOF_FLY_DEBUG
1133                                 printf("lock horizon: adjusting %.1f degrees\n\n", RAD2DEG(angle));
1134 #endif
1135
1136                                 /* rotate view so view horizon = world horizon */
1137                                 axis_angle_to_quat(rotation, view_direction, angle);
1138                                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
1139                         }
1140
1141                         rv3d->view = RV3D_VIEW_USER;
1142                 }
1143                 else {
1144                         shouldRotate = FALSE;
1145                 }
1146         }
1147
1148         if (shouldTranslate || shouldRotate) {
1149                 fly->redraw = TRUE;
1150
1151                 if (rv3d->persp == RV3D_CAMOB) {
1152                         move_camera(C, rv3d, fly, shouldRotate, shouldTranslate);
1153                 }
1154         }
1155
1156         return OPERATOR_FINISHED;
1157 }
1158
1159 static int fly_invoke(bContext *C, wmOperator *op, wmEvent *event)
1160 {
1161         RegionView3D *rv3d = CTX_wm_region_view3d(C);
1162         FlyInfo *fly;
1163
1164         if (rv3d->viewlock)
1165                 return OPERATOR_CANCELLED;
1166
1167         fly = MEM_callocN(sizeof(FlyInfo), "FlyOperation");
1168
1169         op->customdata = fly;
1170
1171         if (initFlyInfo(C, fly, op, event) == FALSE) {
1172                 MEM_freeN(op->customdata);
1173                 return OPERATOR_CANCELLED;
1174         }
1175
1176         flyEvent(fly, event);
1177
1178         WM_event_add_modal_handler(C, op);
1179
1180         return OPERATOR_RUNNING_MODAL;
1181 }
1182
1183 static int fly_cancel(bContext *C, wmOperator *op)
1184 {
1185         FlyInfo *fly = op->customdata;
1186
1187         fly->state = FLY_CANCEL;
1188         flyEnd(C, fly);
1189         op->customdata = NULL;
1190
1191         return OPERATOR_CANCELLED;
1192 }
1193
1194 static int fly_modal(bContext *C, wmOperator *op, wmEvent *event)
1195 {
1196         int exit_code;
1197         short do_draw = FALSE;
1198         FlyInfo *fly = op->customdata;
1199         RegionView3D *rv3d = fly->rv3d;
1200         Object *fly_object = fly->root_parent ? fly->root_parent : fly->v3d->camera;
1201
1202         fly->redraw = 0;
1203
1204         flyEvent(fly, event);
1205
1206         if (fly->ndof) { /* 3D mouse overrules [2D mouse + timer] */
1207                 if (event->type == NDOF_MOTION) {
1208                         flyApply_ndof(C, fly);
1209                 }
1210         }
1211         else if (event->type == TIMER && event->customdata == fly->timer) {
1212                 flyApply(C, fly);
1213         }
1214
1215         do_draw |= fly->redraw;
1216
1217         exit_code = flyEnd(C, fly);
1218
1219         if (exit_code != OPERATOR_RUNNING_MODAL)
1220                 do_draw = TRUE;
1221
1222         if (do_draw) {
1223                 if (rv3d->persp == RV3D_CAMOB) {
1224                         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, fly_object);
1225                 }
1226
1227                 // puts("redraw!"); // too frequent, commented with NDOF_FLY_DRAW_TOOMUCH for now
1228                 ED_region_tag_redraw(CTX_wm_region(C));
1229         }
1230
1231         return exit_code;
1232 }
1233
1234 void VIEW3D_OT_fly(wmOperatorType *ot)
1235 {
1236         /* identifiers */
1237         ot->name = "Fly Navigation";
1238         ot->description = "Interactively fly around the scene";
1239         ot->idname = "VIEW3D_OT_fly";
1240
1241         /* api callbacks */
1242         ot->invoke = fly_invoke;
1243         ot->cancel = fly_cancel;
1244         ot->modal = fly_modal;
1245         ot->poll = ED_operator_view3d_active;
1246
1247         /* flags */
1248         ot->flag = OPTYPE_BLOCKING;
1249 }