style cleanup
[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), struct 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 (fabs(upvec[2]) < 0.1)
337                 fly->zlock = 1;
338         upvec[0] = 0;
339         upvec[1] = 0;
340         upvec[2] = 0;
341
342         fly->persp_backup = fly->rv3d->persp;
343         fly->dist_backup = fly->rv3d->dist;
344
345         /* check for flying ortho camera - which we cant support well
346          * we _could_ also check for an ortho camera but this is easier */
347         if ((fly->rv3d->persp == RV3D_CAMOB) &&
348             (fly->v3d->camera != NULL) &&
349             (fly->rv3d->is_persp == FALSE))
350         {
351                 ((Camera *)fly->v3d->camera->data)->type = CAM_PERSP;
352                 fly->is_ortho_cam = TRUE;
353         }
354
355         if (fly->rv3d->persp == RV3D_CAMOB) {
356                 Object *ob_back;
357                 if ((U.uiflag & USER_CAM_LOCK_NO_PARENT) == 0 && (fly->root_parent = fly->v3d->camera->parent)) {
358                         while (fly->root_parent->parent)
359                                 fly->root_parent = fly->root_parent->parent;
360                         ob_back = fly->root_parent;
361                 }
362                 else {
363                         ob_back = fly->v3d->camera;
364                 }
365
366                 /* store the original camera loc and rot */
367                 /* TODO. axis angle etc */
368
369                 fly->obtfm = BKE_object_tfm_backup(ob_back);
370
371                 BKE_object_where_is_calc(fly->scene, fly->v3d->camera);
372                 negate_v3_v3(fly->rv3d->ofs, fly->v3d->camera->obmat[3]);
373
374                 fly->rv3d->dist = 0.0;
375         }
376         else {
377                 /* perspective or ortho */
378                 if (fly->rv3d->persp == RV3D_ORTHO)
379                         fly->rv3d->persp = RV3D_PERSP;  /* if ortho projection, make perspective */
380
381                 copy_qt_qt(fly->rot_backup, fly->rv3d->viewquat);
382                 copy_v3_v3(fly->ofs_backup, fly->rv3d->ofs);
383
384                 /* the dist defines a vector that is infront of the offset
385                  * to rotate the view about.
386                  * this is no good for fly mode because we
387                  * want to rotate about the viewers center.
388                  * but to correct the dist removal we must
389                  * alter offset so the view doesn't jump. */
390
391                 fly->rv3d->dist = 0.0f;
392
393                 upvec[2] = fly->dist_backup; /* x and y are 0 */
394                 mul_m3_v3(mat, upvec);
395                 sub_v3_v3(fly->rv3d->ofs, upvec);
396                 /* Done with correcting for the dist */
397         }
398
399         /* center the mouse, probably the UI mafia are against this but without its quite annoying */
400         WM_cursor_warp(win, fly->ar->winrct.xmin + fly->ar->winx / 2, fly->ar->winrct.ymin + fly->ar->winy / 2);
401
402         return 1;
403 }
404
405 static int flyEnd(bContext *C, FlyInfo *fly)
406 {
407         RegionView3D *rv3d = fly->rv3d;
408         View3D *v3d = fly->v3d;
409
410         float upvec[3];
411
412         if (fly->state == FLY_RUNNING)
413                 return OPERATOR_RUNNING_MODAL;
414
415 #ifdef NDOF_FLY_DEBUG
416         puts("\n-- fly end --");
417 #endif
418
419         WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), fly->timer);
420
421         ED_region_draw_cb_exit(fly->ar->type, fly->draw_handle_pixel);
422
423         rv3d->dist = fly->dist_backup;
424
425         if (fly->state == FLY_CANCEL) {
426                 /* Revert to original view? */
427                 if (fly->persp_backup == RV3D_CAMOB) { /* a camera view */
428                         Object *ob_back;
429                         ob_back = (fly->root_parent) ? fly->root_parent : fly->v3d->camera;
430
431                         /* store the original camera loc and rot */
432                         BKE_object_tfm_restore(ob_back, fly->obtfm);
433
434                         DAG_id_tag_update(&ob_back->id, OB_RECALC_OB);
435                 }
436                 else {
437                         /* Non Camera we need to reset the view back to the original location bacause the user canceled*/
438                         copy_qt_qt(rv3d->viewquat, fly->rot_backup);
439                         copy_v3_v3(rv3d->ofs, fly->ofs_backup);
440                         rv3d->persp = fly->persp_backup;
441                 }
442         }
443         else if (fly->persp_backup == RV3D_CAMOB) { /* camera */
444                 DAG_id_tag_update(fly->root_parent ? &fly->root_parent->id : &v3d->camera->id, OB_RECALC_OB);
445         }
446         else { /* not camera */
447
448                 /* Apply the fly mode view */
449                 /* restore the dist */
450                 float mat[3][3];
451                 upvec[0] = upvec[1] = 0;
452                 upvec[2] = fly->dist_backup; /* x and y are 0 */
453                 copy_m3_m4(mat, rv3d->viewinv);
454                 mul_m3_v3(mat, upvec);
455                 add_v3_v3(rv3d->ofs, upvec);
456                 /* Done with correcting for the dist */
457         }
458
459         if (fly->is_ortho_cam) {
460                 ((Camera *)fly->v3d->camera->data)->type = CAM_ORTHO;
461         }
462
463         rv3d->rflag &= ~RV3D_NAVIGATING;
464 //XXX2.5        BIF_view3d_previewrender_signal(fly->sa, PR_DBASE|PR_DISPRECT); /* not working at the moment not sure why */
465
466         if (fly->obtfm)
467                 MEM_freeN(fly->obtfm);
468         if (fly->ndof)
469                 MEM_freeN(fly->ndof);
470
471         if (fly->state == FLY_CONFIRM) {
472                 MEM_freeN(fly);
473                 return OPERATOR_FINISHED;
474         }
475
476         MEM_freeN(fly);
477         return OPERATOR_CANCELLED;
478 }
479
480 static void flyEvent(FlyInfo *fly, wmEvent *event)
481 {
482         if (event->type == TIMER && event->customdata == fly->timer) {
483                 fly->redraw = 1;
484         }
485         else if (event->type == MOUSEMOVE) {
486                 copy_v2_v2_int(fly->mval, event->mval);
487         }
488         else if (event->type == NDOF_MOTION) {
489                 /* do these automagically get delivered? yes. */
490                 // puts("ndof motion detected in fly mode!");
491                 // static const char* tag_name = "3D mouse position";
492
493                 wmNDOFMotionData *incoming_ndof = (wmNDOFMotionData *)event->customdata;
494                 switch (incoming_ndof->progress) {
495                         case P_STARTING:
496                                 /* start keeping track of 3D mouse position */
497 #ifdef NDOF_FLY_DEBUG
498                                 puts("start keeping track of 3D mouse position");
499 #endif
500                         /* fall through... */
501                         case P_IN_PROGRESS:
502                                 /* update 3D mouse position */
503 #ifdef NDOF_FLY_DEBUG
504                                 putchar('.'); fflush(stdout);
505 #endif
506                                 if (fly->ndof == NULL) {
507                                         // fly->ndof = MEM_mallocN(sizeof(wmNDOFMotionData), tag_name);
508                                         fly->ndof = MEM_dupallocN(incoming_ndof);
509                                         // fly->ndof = malloc(sizeof(wmNDOFMotionData));
510                                 }
511                                 else {
512                                         memcpy(fly->ndof, incoming_ndof, sizeof(wmNDOFMotionData));
513                                 }
514                                 break;
515                         case P_FINISHING:
516                                 // stop keeping track of 3D mouse position
517 #ifdef NDOF_FLY_DEBUG
518                                 puts("stop keeping track of 3D mouse position");
519 #endif
520                                 if (fly->ndof) {
521                                         MEM_freeN(fly->ndof);
522                                         // free(fly->ndof);
523                                         fly->ndof = NULL;
524                                 }
525                                 /* update the time else the view will jump when 2D mouse/timer resume */
526                                 fly->time_lastdraw = PIL_check_seconds_timer();
527                                 break;
528                         default:
529                                 break; /* should always be one of the above 3 */
530                 }
531         }
532         /* handle modal keymap first */
533         else if (event->type == EVT_MODAL_MAP) {
534                 switch (event->val) {
535                         case FLY_MODAL_CANCEL:
536                                 fly->state = FLY_CANCEL;
537                                 break;
538                         case FLY_MODAL_CONFIRM:
539                                 fly->state = FLY_CONFIRM;
540                                 break;
541
542                         case FLY_MODAL_ACCELERATE:
543                         {
544                                 double time_currwheel;
545                                 float time_wheel;
546
547                                 time_currwheel = PIL_check_seconds_timer();
548                                 time_wheel = (float)(time_currwheel - fly->time_lastwheel);
549                                 fly->time_lastwheel = time_currwheel;
550                                 /* Mouse wheel delays range from (0.5 == slow) to (0.01 == fast) */
551                                 time_wheel = 1.0f + (10.0f - (20.0f * MIN2(time_wheel, 0.5f))); /* 0-0.5 -> 0-5.0 */
552
553                                 if (fly->speed < 0.0f) {
554                                         fly->speed = 0.0f;
555                                 }
556                                 else {
557                                         fly->speed += fly->grid * time_wheel * (fly->use_precision ? 0.1f : 1.0f);
558                                 }
559                                 break;
560                         }
561                         case FLY_MODAL_DECELERATE:
562                         {
563                                 double time_currwheel;
564                                 float time_wheel;
565
566                                 time_currwheel = PIL_check_seconds_timer();
567                                 time_wheel = (float)(time_currwheel - fly->time_lastwheel);
568                                 fly->time_lastwheel = time_currwheel;
569                                 time_wheel = 1.0f + (10.0f - (20.0f * MIN2(time_wheel, 0.5f))); /* 0-0.5 -> 0-5.0 */
570
571                                 if (fly->speed > 0.0f) {
572                                         fly->speed = 0;
573                                 }
574                                 else {
575                                         fly->speed -= fly->grid * time_wheel * (fly->use_precision ? 0.1f : 1.0f);
576                                 }
577                                 break;
578                         }
579                         case FLY_MODAL_PAN_ENABLE:
580                                 fly->pan_view = TRUE;
581                                 break;
582                         case FLY_MODAL_PAN_DISABLE:
583 //XXX2.5                                WM_cursor_warp(CTX_wm_window(C), cent_orig[0], cent_orig[1]);
584                                 fly->pan_view = FALSE;
585                                 break;
586
587                         /* implement WASD keys,
588                          * comments only for 'forward '*/
589                         case FLY_MODAL_DIR_FORWARD:
590                                 if (fly->axis == 2 && fly->speed < 0.0f) { /* reverse direction stops, tap again to continue */
591                                         fly->axis = -1;
592                                 }
593                                 else {
594                                         /* flip speed rather than stopping, game like motion,
595                                          * else increase like mousewheel if were already moving in that direction */
596                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
597                                         else if (fly->axis == 2) fly->speed += fly->grid;
598                                         fly->axis = 2;
599                                 }
600                                 break;
601                         case FLY_MODAL_DIR_BACKWARD:
602                                 if (fly->axis == 2 && fly->speed > 0.0f) {
603                                         fly->axis = -1;
604                                 }
605                                 else {
606                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
607                                         else if (fly->axis == 2) fly->speed -= fly->grid;
608
609                                         fly->axis = 2;
610                                 }
611                                 break;
612                         case FLY_MODAL_DIR_LEFT:
613                                 if (fly->axis == 0 && fly->speed < 0.0f) {
614                                         fly->axis = -1;
615                                 }
616                                 else {
617                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
618                                         else if (fly->axis == 0) fly->speed += fly->grid;
619
620                                         fly->axis = 0;
621                                 }
622                                 break;
623                         case FLY_MODAL_DIR_RIGHT:
624                                 if (fly->axis == 0 && fly->speed > 0.0f) {
625                                         fly->axis = -1;
626                                 }
627                                 else {
628                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
629                                         else if (fly->axis == 0) fly->speed -= fly->grid;
630
631                                         fly->axis = 0;
632                                 }
633                                 break;
634                         case FLY_MODAL_DIR_DOWN:
635                                 if (fly->axis == 1 && fly->speed < 0.0f) {
636                                         fly->axis = -1;
637                                 }
638                                 else {
639                                         if (fly->speed < 0.0f)   fly->speed = -fly->speed;
640                                         else if (fly->axis == 1) fly->speed += fly->grid;
641                                         fly->axis = 1;
642                                 }
643                                 break;
644                         case FLY_MODAL_DIR_UP:
645                                 if (fly->axis == 1 && fly->speed > 0.0f) {
646                                         fly->axis = -1;
647                                 }
648                                 else {
649                                         if (fly->speed > 0.0f)   fly->speed = -fly->speed;
650                                         else if (fly->axis == 1) fly->speed -= fly->grid;
651                                         fly->axis = 1;
652                                 }
653                                 break;
654
655                         case FLY_MODAL_AXIS_LOCK_X:
656                                 if (fly->xlock)
657                                         fly->xlock = 0;
658                                 else {
659                                         fly->xlock = 2;
660                                         fly->xlock_momentum = 0.0;
661                                 }
662                                 break;
663                         case FLY_MODAL_AXIS_LOCK_Z:
664                                 if (fly->zlock)
665                                         fly->zlock = 0;
666                                 else {
667                                         fly->zlock = 2;
668                                         fly->zlock_momentum = 0.0;
669                                 }
670                                 break;
671
672                         case FLY_MODAL_PRECISION_ENABLE:
673                                 fly->use_precision = TRUE;
674                                 break;
675                         case FLY_MODAL_PRECISION_DISABLE:
676                                 fly->use_precision = FALSE;
677                                 break;
678
679                         case FLY_MODAL_FREELOOK_ENABLE:
680                                 fly->use_freelook = TRUE;
681                                 break;
682                         case FLY_MODAL_FREELOOK_DISABLE:
683                                 fly->use_freelook = FALSE;
684                                 break;
685                 }
686         }
687 }
688
689 static void move_camera(bContext *C, RegionView3D *rv3d, FlyInfo *fly, int orientationChanged, int positionChanged)
690 {
691         /* we are in camera view so apply the view ofs and quat to the view matrix and set the camera to the view */
692
693         View3D *v3d = fly->v3d;
694         Scene *scene = fly->scene;
695         ID *id_key;
696
697         /* transform the parent or the camera? */
698         if (fly->root_parent) {
699                 Object *ob_update;
700
701                 float view_mat[4][4];
702                 float prev_view_mat[4][4];
703                 float prev_view_imat[4][4];
704                 float diff_mat[4][4];
705                 float parent_mat[4][4];
706
707                 ED_view3d_to_m4(prev_view_mat, fly->rv3d->ofs, fly->rv3d->viewquat, fly->rv3d->dist);
708                 invert_m4_m4(prev_view_imat, prev_view_mat);
709                 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
710                 mult_m4_m4m4(diff_mat, view_mat, prev_view_imat);
711                 mult_m4_m4m4(parent_mat, diff_mat, fly->root_parent->obmat);
712                 BKE_object_apply_mat4(fly->root_parent, parent_mat, TRUE, FALSE);
713
714                 // BKE_object_where_is_calc(scene, fly->root_parent);
715
716                 ob_update = v3d->camera->parent;
717                 while (ob_update) {
718                         DAG_id_tag_update(&ob_update->id, OB_RECALC_OB);
719                         ob_update = ob_update->parent;
720                 }
721
722                 id_key = &fly->root_parent->id;
723         }
724         else {
725                 float view_mat[4][4];
726                 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
727                 BKE_object_apply_mat4(v3d->camera, view_mat, TRUE, FALSE);
728                 id_key = &v3d->camera->id;
729         }
730
731         /* record the motion */
732         if (autokeyframe_cfra_can_key(scene, id_key)) {
733                 ListBase dsources = {NULL, NULL};
734
735                 /* add datasource override for the camera object */
736                 ANIM_relative_keyingset_add_source(&dsources, id_key, NULL, NULL);
737
738                 /* insert keyframes 
739                  *      1) on the first frame
740                  *      2) on each subsequent frame
741                  *              TODO: need to check in future that frame changed before doing this 
742                  */
743                 if (orientationChanged) {
744                         KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_ROTATION_ID);
745                         ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
746                 }
747                 if (positionChanged) {
748                         KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_LOCATION_ID);
749                         ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
750                 }
751
752                 /* free temp data */
753                 BLI_freelistN(&dsources);
754         }
755 }
756
757 static int flyApply(bContext *C, FlyInfo *fly)
758 {
759 #define FLY_ROTATE_FAC 2.5f /* more is faster */
760 #define FLY_ZUP_CORRECT_FAC 0.1f /* amount to correct per step */
761 #define FLY_ZUP_CORRECT_ACCEL 0.05f /* increase upright momentum each step */
762
763         /* fly mode - Shift+F
764          * a fly loop where the user can move move the view as if they are flying
765          */
766         RegionView3D *rv3d = fly->rv3d;
767         ARegion *ar = fly->ar;
768
769         float mat[3][3]; /* 3x3 copy of the view matrix so we can move along the view axis */
770         float dvec[3] = {0, 0, 0}; /* this is the direction thast added to the view offset per redraw */
771
772         /* Camera Uprighting variables */
773         float upvec[3] = {0, 0, 0}; /* stores the view's up vector */
774
775         float moffset[2]; /* mouse offset from the views center */
776         float tmp_quat[4]; /* used for rotating the view */
777
778 //      int cent_orig[2], /* view center */
779 //XXX- can avoid using //   cent[2], /* view center modified */
780         int xmargin, ymargin; /* x and y margin are define the safe area where the mouses movement wont rotate the view */
781
782 #ifdef NDOF_FLY_DEBUG
783         {
784                 static unsigned int iteration = 1;
785                 printf("fly timer %d\n", iteration++);
786         }
787 #endif
788
789         xmargin = ar->winx / 20.0f;
790         ymargin = ar->winy / 20.0f;
791
792         // UNUSED
793         // cent_orig[0]= ar->winrct.xmin + ar->winx/2;
794         // cent_orig[1]= ar->winrct.ymin + ar->winy/2;
795
796         {
797
798                 /* mouse offset from the center */
799                 moffset[0] = fly->mval[0] - ar->winx / 2;
800                 moffset[1] = fly->mval[1] - ar->winy / 2;
801
802                 /* enforce a view margin */
803                 if      (moffset[0] >  xmargin) moffset[0] -= xmargin;
804                 else if (moffset[0] < -xmargin) moffset[0] += xmargin;
805                 else                            moffset[0] =  0;
806
807                 if      (moffset[1] >  ymargin) moffset[1] -= ymargin;
808                 else if (moffset[1] < -ymargin) moffset[1] += ymargin;
809                 else                            moffset[1] =  0;
810
811
812                 /* scale the mouse movement by this value - scales mouse movement to the view size
813                  * moffset[0]/(ar->winx-xmargin*2) - window size minus margin (same for y)
814                  *
815                  * the mouse moves isn't linear */
816
817                 if (moffset[0]) {
818                         moffset[0] /= ar->winx - (xmargin * 2);
819                         moffset[0] *= fabsf(moffset[0]);
820                 }
821
822                 if (moffset[1]) {
823                         moffset[1] /= ar->winy - (ymargin * 2);
824                         moffset[1] *= fabsf(moffset[1]);
825                 }
826
827                 /* Should we redraw? */
828                 if ((fly->speed != 0.0f) ||
829                     moffset[0] || moffset[1] ||
830                     fly->zlock || fly->xlock ||
831                     dvec[0] || dvec[1] || dvec[2])
832                 {
833                         float dvec_tmp[3];
834
835                         /* time how fast it takes for us to redraw,
836                          * this is so simple scenes don't fly too fast */
837                         double time_current;
838                         float time_redraw;
839                         float time_redraw_clamped;
840 #ifdef NDOF_FLY_DRAW_TOOMUCH
841                         fly->redraw = 1;
842 #endif
843                         time_current = PIL_check_seconds_timer();
844                         time_redraw = (float)(time_current - fly->time_lastdraw);
845                         time_redraw_clamped = MIN2(0.05f, time_redraw); /* clamp redraw time to avoid jitter in roll correction */
846                         fly->time_lastdraw = time_current;
847
848                         /* Scale the time to use shift to scale the speed down- just like
849                          * shift slows many other areas of blender down */
850                         if (fly->use_precision)
851                                 fly->speed = fly->speed * (1.0f - time_redraw_clamped);
852
853                         copy_m3_m4(mat, rv3d->viewinv);
854
855                         if (fly->pan_view == TRUE) {
856                                 /* pan only */
857                                 dvec_tmp[0] = -moffset[0];
858                                 dvec_tmp[1] = -moffset[1];
859                                 dvec_tmp[2] = 0;
860
861                                 if (fly->use_precision) {
862                                         dvec_tmp[0] *= 0.1f;
863                                         dvec_tmp[1] *= 0.1f;
864                                 }
865
866                                 mul_m3_v3(mat, dvec_tmp);
867                                 mul_v3_fl(dvec_tmp, time_redraw * 200.0f * fly->grid);
868                         }
869                         else {
870                                 float roll; /* similar to the angle between the camera's up and the Z-up,
871                                              * but its very rough so just roll */
872
873                                 /* rotate about the X axis- look up/down */
874                                 if (moffset[1]) {
875                                         upvec[0] = 1;
876                                         upvec[1] = 0;
877                                         upvec[2] = 0;
878                                         mul_m3_v3(mat, upvec);
879                                         /* Rotate about the relative up vec */
880                                         axis_angle_to_quat(tmp_quat, upvec, (float)moffset[1] * time_redraw * -FLY_ROTATE_FAC);
881                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
882
883                                         if (fly->xlock)
884                                                 fly->xlock = 2;  /* check for rotation */
885                                         if (fly->zlock)
886                                                 fly->zlock = 2;
887                                         fly->xlock_momentum = 0.0f;
888                                 }
889
890                                 /* rotate about the Y axis- look left/right */
891                                 if (moffset[0]) {
892
893                                         /* if we're upside down invert the moffset */
894                                         upvec[0] = 0.0f;
895                                         upvec[1] = 1.0f;
896                                         upvec[2] = 0.0f;
897                                         mul_m3_v3(mat, upvec);
898
899                                         if (upvec[2] < 0.0f)
900                                                 moffset[0] = -moffset[0];
901
902                                         /* make the lock vectors */
903                                         if (fly->zlock) {
904                                                 upvec[0] = 0.0f;
905                                                 upvec[1] = 0.0f;
906                                                 upvec[2] = 1.0f;
907                                         }
908                                         else {
909                                                 upvec[0] = 0.0f;
910                                                 upvec[1] = 1.0f;
911                                                 upvec[2] = 0.0f;
912                                                 mul_m3_v3(mat, upvec);
913                                         }
914
915                                         /* Rotate about the relative up vec */
916                                         axis_angle_to_quat(tmp_quat, upvec, (float)moffset[0] * time_redraw * FLY_ROTATE_FAC);
917                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
918
919                                         if (fly->xlock)
920                                                 fly->xlock = 2;  /* check for rotation */
921                                         if (fly->zlock)
922                                                 fly->zlock = 2;
923                                 }
924
925                                 if (fly->zlock == 2) {
926                                         upvec[0] = 1.0f;
927                                         upvec[1] = 0.0f;
928                                         upvec[2] = 0.0f;
929                                         mul_m3_v3(mat, upvec);
930
931                                         /* make sure we have some z rolling */
932                                         if (fabsf(upvec[2]) > 0.00001f) {
933                                                 roll = upvec[2] * 5.0f;
934                                                 upvec[0] = 0.0f; /* rotate the view about this axis */
935                                                 upvec[1] = 0.0f;
936                                                 upvec[2] = 1.0f;
937
938                                                 mul_m3_v3(mat, upvec);
939                                                 /* Rotate about the relative up vec */
940                                                 axis_angle_to_quat(tmp_quat, upvec,
941                                                                    roll * time_redraw_clamped * fly->zlock_momentum * FLY_ZUP_CORRECT_FAC);
942                                                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
943
944                                                 fly->zlock_momentum += FLY_ZUP_CORRECT_ACCEL;
945                                         }
946                                         else {
947                                                 fly->zlock = 1; /* don't check until the view rotates again */
948                                                 fly->zlock_momentum = 0.0f;
949                                         }
950                                 }
951
952                                 if (fly->xlock == 2 && moffset[1] == 0) { /* only apply xcorrect when mouse isn't applying x rot */
953                                         upvec[0] = 0;
954                                         upvec[1] = 0;
955                                         upvec[2] = 1;
956                                         mul_m3_v3(mat, upvec);
957                                         /* make sure we have some z rolling */
958                                         if (fabsf(upvec[2]) > 0.00001f) {
959                                                 roll = upvec[2] * -5.0f;
960
961                                                 upvec[0] = 1.0f; /* rotate the view about this axis */
962                                                 upvec[1] = 0.0f;
963                                                 upvec[2] = 0.0f;
964
965                                                 mul_m3_v3(mat, upvec);
966
967                                                 /* Rotate about the relative up vec */
968                                                 axis_angle_to_quat(tmp_quat, upvec, roll * time_redraw_clamped * fly->xlock_momentum * 0.1f);
969                                                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
970
971                                                 fly->xlock_momentum += 0.05f;
972                                         }
973                                         else {
974                                                 fly->xlock = 1; /* see above */
975                                                 fly->xlock_momentum = 0.0f;
976                                         }
977                                 }
978
979                                 if (fly->axis == -1) {
980                                         /* pause */
981                                         zero_v3(dvec_tmp);
982                                 }
983                                 else if (!fly->use_freelook) {
984                                         /* Normal operation */
985                                         /* define dvec, view direction vector */
986                                         zero_v3(dvec_tmp);
987                                         /* move along the current axis */
988                                         dvec_tmp[fly->axis] = 1.0f;
989
990                                         mul_m3_v3(mat, dvec_tmp);
991                                 }
992                                 else {
993                                         normalize_v3_v3(dvec_tmp, fly->dvec_prev);
994                                         if (fly->speed < 0.0f) {
995                                                 negate_v3(dvec_tmp);
996                                         }
997                                 }
998
999                                 mul_v3_fl(dvec_tmp, fly->speed * time_redraw * 0.25f);
1000                         }
1001
1002                         /* impose a directional lag */
1003                         interp_v3_v3v3(dvec, dvec_tmp, fly->dvec_prev, (1.0f / (1.0f + (time_redraw * 5.0f))));
1004
1005                         if (rv3d->persp == RV3D_CAMOB) {
1006                                 Object *lock_ob = fly->root_parent ? fly->root_parent : fly->v3d->camera;
1007                                 if (lock_ob->protectflag & OB_LOCK_LOCX) dvec[0] = 0.0;
1008                                 if (lock_ob->protectflag & OB_LOCK_LOCY) dvec[1] = 0.0;
1009                                 if (lock_ob->protectflag & OB_LOCK_LOCZ) dvec[2] = 0.0;
1010                         }
1011
1012                         add_v3_v3(rv3d->ofs, dvec);
1013
1014                         if (rv3d->persp == RV3D_CAMOB)
1015                                 move_camera(C, rv3d, fly, (fly->xlock || fly->zlock || moffset[0] || moffset[1]), fly->speed);
1016
1017                 }
1018                 else {
1019                         /* we're not redrawing but we need to update the time else the view will jump */
1020                         fly->time_lastdraw = PIL_check_seconds_timer();
1021                 }
1022                 /* end drawing */
1023                 copy_v3_v3(fly->dvec_prev, dvec);
1024         }
1025
1026         return OPERATOR_FINISHED;
1027 }
1028
1029 static int flyApply_ndof(bContext *C, FlyInfo *fly)
1030 {
1031         /* shorthand for oft-used variables */
1032         wmNDOFMotionData *ndof = fly->ndof;
1033         const float dt = ndof->dt;
1034         RegionView3D *rv3d = fly->rv3d;
1035         const int flag = U.ndof_flag;
1036
1037 #if 0
1038         int shouldRotate = (flag & NDOF_SHOULD_ROTATE) && (fly->pan_view == FALSE),
1039             shouldTranslate = (flag & (NDOF_SHOULD_PAN | NDOF_SHOULD_ZOOM));
1040 #endif
1041
1042         int shouldRotate = (fly->pan_view == FALSE);
1043         int shouldTranslate = TRUE;
1044
1045         float view_inv[4];
1046         invert_qt_qt(view_inv, rv3d->viewquat);
1047
1048         rv3d->rot_angle = 0.0f; /* disable onscreen rotation doo-dad */
1049
1050         if (shouldTranslate) {
1051                 const float forward_sensitivity  = 1.0f;
1052                 const float vertical_sensitivity = 0.4f;
1053                 const float lateral_sensitivity  = 0.6f;
1054
1055                 float speed = 10.0f; /* blender units per second */
1056                 /* ^^ this is ok for default cube scene, but should scale with.. something */
1057
1058                 float trans[3] = {lateral_sensitivity  * ndof->tvec[0],
1059                                   vertical_sensitivity * ndof->tvec[1],
1060                                   forward_sensitivity  * ndof->tvec[2]};
1061
1062                 if (fly->use_precision)
1063                         speed *= 0.2f;
1064
1065                 mul_v3_fl(trans, speed * dt);
1066
1067                 /* transform motion from view to world coordinates */
1068                 mul_qt_v3(view_inv, trans);
1069
1070                 if (flag & NDOF_FLY_HELICOPTER) {
1071                         /* replace world z component with device y (yes it makes sense) */
1072                         trans[2] = speed * dt * vertical_sensitivity * ndof->tvec[1];
1073                 }
1074
1075                 if (rv3d->persp == RV3D_CAMOB) {
1076                         /* respect camera position locks */
1077                         Object *lock_ob = fly->root_parent ? fly->root_parent : fly->v3d->camera;
1078                         if (lock_ob->protectflag & OB_LOCK_LOCX) trans[0] = 0.0f;
1079                         if (lock_ob->protectflag & OB_LOCK_LOCY) trans[1] = 0.0f;
1080                         if (lock_ob->protectflag & OB_LOCK_LOCZ) trans[2] = 0.0f;
1081                 }
1082
1083                 if (!is_zero_v3(trans)) {
1084                         /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1085                         sub_v3_v3(rv3d->ofs, trans);
1086                         shouldTranslate = TRUE;
1087                 }
1088                 else {
1089                         shouldTranslate = FALSE;
1090                 }
1091         }
1092
1093         if (shouldRotate) {
1094                 const float turn_sensitivity = 1.0f;
1095
1096                 float rotation[4];
1097                 float axis[3];
1098                 float angle = turn_sensitivity * ndof_to_axis_angle(ndof, axis);
1099
1100                 if (fabsf(angle) > 0.0001f) {
1101                         shouldRotate = TRUE;
1102
1103                         if (fly->use_precision)
1104                                 angle *= 0.2f;
1105
1106                         /* transform rotation axis from view to world coordinates */
1107                         mul_qt_v3(view_inv, axis);
1108
1109                         /* apply rotation to view */
1110                         axis_angle_to_quat(rotation, axis, angle);
1111                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
1112
1113                         if (flag & NDOF_LOCK_HORIZON) {
1114                                 /* force an upright viewpoint
1115                                  * TODO: make this less... sudden */
1116                                 float view_horizon[3] = {1.0f, 0.0f, 0.0f}; /* view +x */
1117                                 float view_direction[3] = {0.0f, 0.0f, -1.0f}; /* view -z (into screen) */
1118
1119                                 /* find new inverse since viewquat has changed */
1120                                 invert_qt_qt(view_inv, rv3d->viewquat);
1121                                 /* could apply reverse rotation to existing view_inv to save a few cycles */
1122
1123                                 /* transform view vectors to world coordinates */
1124                                 mul_qt_v3(view_inv, view_horizon);
1125                                 mul_qt_v3(view_inv, view_direction);
1126
1127                                 /* find difference between view & world horizons
1128                                  * true horizon lives in world xy plane, so look only at difference in z */
1129                                 angle = -asinf(view_horizon[2]);
1130
1131 #ifdef NDOF_FLY_DEBUG
1132                                 printf("lock horizon: adjusting %.1f degrees\n\n", RAD2DEG(angle));
1133 #endif
1134
1135                                 /* rotate view so view horizon = world horizon */
1136                                 axis_angle_to_quat(rotation, view_direction, angle);
1137                                 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rotation);
1138                         }
1139
1140                         rv3d->view = RV3D_VIEW_USER;
1141                 }
1142                 else {
1143                         shouldRotate = FALSE;
1144                 }
1145         }
1146
1147         if (shouldTranslate || shouldRotate) {
1148                 fly->redraw = TRUE;
1149
1150                 if (rv3d->persp == RV3D_CAMOB) {
1151                         move_camera(C, rv3d, fly, shouldRotate, shouldTranslate);
1152                 }
1153         }
1154
1155         return OPERATOR_FINISHED;
1156 }
1157
1158 static int fly_invoke(bContext *C, wmOperator *op, wmEvent *event)
1159 {
1160         RegionView3D *rv3d = CTX_wm_region_view3d(C);
1161         FlyInfo *fly;
1162
1163         if (rv3d->viewlock)
1164                 return OPERATOR_CANCELLED;
1165
1166         fly = MEM_callocN(sizeof(FlyInfo), "FlyOperation");
1167
1168         op->customdata = fly;
1169
1170         if (initFlyInfo(C, fly, op, event) == FALSE) {
1171                 MEM_freeN(op->customdata);
1172                 return OPERATOR_CANCELLED;
1173         }
1174
1175         flyEvent(fly, event);
1176
1177         WM_event_add_modal_handler(C, op);
1178
1179         return OPERATOR_RUNNING_MODAL;
1180 }
1181
1182 static int fly_cancel(bContext *C, wmOperator *op)
1183 {
1184         FlyInfo *fly = op->customdata;
1185
1186         fly->state = FLY_CANCEL;
1187         flyEnd(C, fly);
1188         op->customdata = NULL;
1189
1190         return OPERATOR_CANCELLED;
1191 }
1192
1193 static int fly_modal(bContext *C, wmOperator *op, wmEvent *event)
1194 {
1195         int exit_code;
1196         short do_draw = FALSE;
1197         FlyInfo *fly = op->customdata;
1198         RegionView3D *rv3d = fly->rv3d;
1199         Object *fly_object = fly->root_parent ? fly->root_parent : fly->v3d->camera;
1200
1201         fly->redraw = 0;
1202
1203         flyEvent(fly, event);
1204
1205         if (fly->ndof) { /* 3D mouse overrules [2D mouse + timer] */
1206                 if (event->type == NDOF_MOTION) {
1207                         flyApply_ndof(C, fly);
1208                 }
1209         }
1210         else if (event->type == TIMER && event->customdata == fly->timer) {
1211                 flyApply(C, fly);
1212         }
1213
1214         do_draw |= fly->redraw;
1215
1216         exit_code = flyEnd(C, fly);
1217
1218         if (exit_code != OPERATOR_RUNNING_MODAL)
1219                 do_draw = TRUE;
1220
1221         if (do_draw) {
1222                 if (rv3d->persp == RV3D_CAMOB) {
1223                         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, fly_object);
1224                 }
1225
1226                 // puts("redraw!"); // too frequent, commented with NDOF_FLY_DRAW_TOOMUCH for now
1227                 ED_region_tag_redraw(CTX_wm_region(C));
1228         }
1229
1230         return exit_code;
1231 }
1232
1233 void VIEW3D_OT_fly(wmOperatorType *ot)
1234 {
1235         /* identifiers */
1236         ot->name = "Fly Navigation";
1237         ot->description = "Interactively fly around the scene";
1238         ot->idname = "VIEW3D_OT_fly";
1239
1240         /* api callbacks */
1241         ot->invoke = fly_invoke;
1242         ot->cancel = fly_cancel;
1243         ot->modal = fly_modal;
1244         ot->poll = ED_operator_view3d_active;
1245
1246         /* flags */
1247         ot->flag = OPTYPE_BLOCKING;
1248 }