NDOF: compile 3D mouse code only if WITH_INPUT_NDOF
[blender-staging.git] / source / blender / editors / space_view3d / view3d_walk.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): Dalai Felinto, Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/space_view3d/view3d_walk.c
24  *  \ingroup spview3d
25  */
26
27 /* defines VIEW3D_OT_navigate - walk modal operator */
28
29 #include "DNA_scene_types.h"
30 #include "DNA_object_types.h"
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_math.h"
35 #include "BLI_blenlib.h"
36 #include "BLI_kdopbvh.h"
37 #include "BLI_utildefines.h"
38
39 #include "BKE_context.h"
40 #include "BKE_report.h"
41
42 #include "BLT_translation.h"
43
44
45 #include "BIF_gl.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "ED_screen.h"
51 #include "ED_space_api.h"
52 #include "ED_transform.h"
53 #include "ED_transform_snap_object_context.h"
54
55 #include "PIL_time.h" /* smoothview */
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59
60 #include "view3d_intern.h"  /* own include */
61
62 #ifdef WITH_INPUT_NDOF
63 //#  define NDOF_WALK_DEBUG
64 //#  define NDOF_WALK_DRAW_TOOMUCH  /* is this needed for ndof? - commented so redraw doesnt thrash - campbell */
65 #endif
66
67 #define USE_TABLET_SUPPORT
68
69 /* ensure the target position is one we can reach, see: T45771 */
70 #define USE_PIXELSIZE_NATIVE_SUPPORT
71
72 /* prototypes */
73 static float getVelocityZeroTime(const float gravity, const float velocity);
74
75 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
76 enum {
77         WALK_MODAL_CANCEL = 1,
78         WALK_MODAL_CONFIRM,
79         WALK_MODAL_DIR_FORWARD,
80         WALK_MODAL_DIR_FORWARD_STOP,
81         WALK_MODAL_DIR_BACKWARD,
82         WALK_MODAL_DIR_BACKWARD_STOP,
83         WALK_MODAL_DIR_LEFT,
84         WALK_MODAL_DIR_LEFT_STOP,
85         WALK_MODAL_DIR_RIGHT,
86         WALK_MODAL_DIR_RIGHT_STOP,
87         WALK_MODAL_DIR_UP,
88         WALK_MODAL_DIR_UP_STOP,
89         WALK_MODAL_DIR_DOWN,
90         WALK_MODAL_DIR_DOWN_STOP,
91         WALK_MODAL_FAST_ENABLE,
92         WALK_MODAL_FAST_DISABLE,
93         WALK_MODAL_SLOW_ENABLE,
94         WALK_MODAL_SLOW_DISABLE,
95         WALK_MODAL_JUMP,
96         WALK_MODAL_JUMP_STOP,
97         WALK_MODAL_TELEPORT,
98         WALK_MODAL_TOGGLE,
99         WALK_MODAL_ACCELERATE,
100         WALK_MODAL_DECELERATE,
101 };
102
103 enum {
104         WALK_BIT_FORWARD  = 1 << 0,
105         WALK_BIT_BACKWARD = 1 << 1,
106         WALK_BIT_LEFT     = 1 << 2,
107         WALK_BIT_RIGHT    = 1 << 3,
108         WALK_BIT_UP       = 1 << 4,
109         WALK_BIT_DOWN     = 1 << 5,
110 };
111
112 typedef enum eWalkTeleportState {
113         WALK_TELEPORT_STATE_OFF = 0,
114         WALK_TELEPORT_STATE_ON,
115 } eWalkTeleportState;
116
117 typedef enum eWalkMethod {
118         WALK_MODE_FREE    = 0,
119         WALK_MODE_GRAVITY,
120 } eWalkMethod;
121
122 typedef enum eWalkGravityState {
123         WALK_GRAVITY_STATE_OFF = 0,
124         WALK_GRAVITY_STATE_JUMP,
125         WALK_GRAVITY_STATE_START,
126         WALK_GRAVITY_STATE_ON,
127 } eWalkGravityState;
128
129 /* called in transform_ops.c, on each regeneration of keymaps  */
130 void walk_modal_keymap(wmKeyConfig *keyconf)
131 {
132         static EnumPropertyItem modal_items[] = {
133                 {WALK_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
134                 {WALK_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
135
136                 {WALK_MODAL_ACCELERATE, "ACCELERATE", 0, "Accelerate", ""},
137                 {WALK_MODAL_DECELERATE, "DECELERATE", 0, "Decelerate", ""},
138
139                 {WALK_MODAL_DIR_FORWARD, "FORWARD", 0, "Move Forward", ""},
140                 {WALK_MODAL_DIR_BACKWARD, "BACKWARD", 0, "Move Backward", ""},
141                 {WALK_MODAL_DIR_LEFT, "LEFT", 0, "Move Left (Strafe)", ""},
142                 {WALK_MODAL_DIR_RIGHT, "RIGHT", 0, "Move Right (Strafe)", ""},
143                 {WALK_MODAL_DIR_UP, "UP", 0, "Move Up", ""},
144                 {WALK_MODAL_DIR_DOWN, "DOWN", 0, "Move Down", ""},
145
146                 {WALK_MODAL_DIR_FORWARD_STOP, "FORWARD_STOP", 0, "Stop Move Forward", ""},
147                 {WALK_MODAL_DIR_BACKWARD_STOP, "BACKWARD_STOP", 0, "Stop Mode Backward", ""},
148                 {WALK_MODAL_DIR_LEFT_STOP, "LEFT_STOP", 0, "Stop Move Left", ""},
149                 {WALK_MODAL_DIR_RIGHT_STOP, "RIGHT_STOP", 0, "Stop Mode Right", ""},
150                 {WALK_MODAL_DIR_UP_STOP, "UP_STOP", 0, "Stop Move Up", ""},
151                 {WALK_MODAL_DIR_DOWN_STOP, "DOWN_STOP", 0, "Stop Mode Down", ""},
152
153                 {WALK_MODAL_TELEPORT, "TELEPORT", 0, "Teleport", "Move forward a few units at once"},
154
155                 {WALK_MODAL_FAST_ENABLE, "FAST_ENABLE", 0, "Fast Enable", "Move faster (walk or fly)"},
156                 {WALK_MODAL_FAST_DISABLE, "FAST_DISABLE", 0, "Fast Disable", "Resume regular speed"},
157
158                 {WALK_MODAL_SLOW_ENABLE, "SLOW_ENABLE", 0, "Slow Enable", "Move slower (walk or fly)"},
159                 {WALK_MODAL_SLOW_DISABLE, "SLOW_DISABLE", 0, "Slow Disable", "Resume regular speed"},
160
161                 {WALK_MODAL_JUMP, "JUMP", 0, "Jump", "Jump when in walk mode"},
162                 {WALK_MODAL_JUMP_STOP, "JUMP_STOP", 0, "Jump Stop", "Stop pushing jump"},
163
164                 {WALK_MODAL_TOGGLE, "GRAVITY_TOGGLE", 0, "Toggle Gravity", "Toggle gravity effect"},
165
166                 {0, NULL, 0, NULL, NULL}};
167
168         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "View3D Walk Modal");
169
170         /* this function is called for each spacetype, only needs to add map once */
171         if (keymap && keymap->modal_items)
172                 return;
173
174         keymap = WM_modalkeymap_add(keyconf, "View3D Walk Modal", modal_items);
175
176         /* items for modal map */
177         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_CANCEL);
178         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_ANY, KM_ANY, 0, WALK_MODAL_CANCEL);
179
180         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_ANY, KM_ANY, 0, WALK_MODAL_CONFIRM);
181         WM_modalkeymap_add_item(keymap, RETKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_CONFIRM);
182         WM_modalkeymap_add_item(keymap, PADENTER, KM_PRESS, KM_ANY, 0, WALK_MODAL_CONFIRM);
183
184         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_FAST_ENABLE);
185         WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_FAST_DISABLE);
186
187         WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_SLOW_ENABLE);
188         WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_SLOW_DISABLE);
189
190         /* WASD */
191         WM_modalkeymap_add_item(keymap, WKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_DIR_FORWARD);
192         WM_modalkeymap_add_item(keymap, SKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_DIR_BACKWARD);
193         WM_modalkeymap_add_item(keymap, AKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_DIR_LEFT);
194         WM_modalkeymap_add_item(keymap, DKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_DIR_RIGHT);
195         WM_modalkeymap_add_item(keymap, EKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_DIR_UP);
196         WM_modalkeymap_add_item(keymap, QKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_DIR_DOWN);
197
198         WM_modalkeymap_add_item(keymap, WKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_FORWARD_STOP);
199         WM_modalkeymap_add_item(keymap, SKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_BACKWARD_STOP);
200         WM_modalkeymap_add_item(keymap, AKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_LEFT_STOP);
201         WM_modalkeymap_add_item(keymap, DKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_RIGHT_STOP);
202         WM_modalkeymap_add_item(keymap, EKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_UP_STOP);
203         WM_modalkeymap_add_item(keymap, QKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_DOWN_STOP);
204
205         WM_modalkeymap_add_item(keymap, UPARROWKEY, KM_PRESS, 0, 0, WALK_MODAL_DIR_FORWARD);
206         WM_modalkeymap_add_item(keymap, DOWNARROWKEY, KM_PRESS, 0, 0, WALK_MODAL_DIR_BACKWARD);
207         WM_modalkeymap_add_item(keymap, LEFTARROWKEY, KM_PRESS, 0, 0, WALK_MODAL_DIR_LEFT);
208         WM_modalkeymap_add_item(keymap, RIGHTARROWKEY, KM_PRESS, 0, 0, WALK_MODAL_DIR_RIGHT);
209
210         WM_modalkeymap_add_item(keymap, UPARROWKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_FORWARD_STOP);
211         WM_modalkeymap_add_item(keymap, DOWNARROWKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_BACKWARD_STOP);
212         WM_modalkeymap_add_item(keymap, LEFTARROWKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_LEFT_STOP);
213         WM_modalkeymap_add_item(keymap, RIGHTARROWKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_DIR_RIGHT_STOP);
214
215         WM_modalkeymap_add_item(keymap, TABKEY, KM_PRESS, 0, 0, WALK_MODAL_TOGGLE);
216         WM_modalkeymap_add_item(keymap, GKEY, KM_PRESS, 0, 0, WALK_MODAL_TOGGLE);
217
218         WM_modalkeymap_add_item(keymap, VKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_JUMP);
219         WM_modalkeymap_add_item(keymap, VKEY, KM_RELEASE, KM_ANY, 0, WALK_MODAL_JUMP_STOP);
220
221         WM_modalkeymap_add_item(keymap, SPACEKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_TELEPORT);
222         WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_ANY, KM_ANY, 0, WALK_MODAL_TELEPORT);
223
224         WM_modalkeymap_add_item(keymap, PADPLUSKEY, KM_PRESS, KM_ANY, 0, WALK_MODAL_ACCELERATE);
225         WM_modalkeymap_add_item(keymap, PADMINUS, KM_PRESS, KM_ANY, 0, WALK_MODAL_DECELERATE);
226         WM_modalkeymap_add_item(keymap, WHEELUPMOUSE, KM_PRESS, KM_ANY, 0, WALK_MODAL_ACCELERATE);
227         WM_modalkeymap_add_item(keymap, WHEELDOWNMOUSE, KM_PRESS, KM_ANY, 0, WALK_MODAL_DECELERATE);
228
229         /* assign map to operators */
230         WM_modalkeymap_assign(keymap, "VIEW3D_OT_walk");
231 }
232
233
234 typedef struct WalkTeleport {
235         eWalkTeleportState state;
236         float duration; /* from user preferences */
237         float origin[3];
238         float direction[3];
239         double initial_time;
240         eWalkMethod navigation_mode; /* teleport always set FREE mode on */
241
242 } WalkTeleport;
243
244 typedef struct WalkInfo {
245         /* context stuff */
246         RegionView3D *rv3d;
247         View3D *v3d;
248         ARegion *ar;
249         Scene *scene;
250
251         wmTimer *timer; /* needed for redraws */
252
253         short state;
254         bool redraw;
255
256         int prev_mval[2]; /* previous 2D mouse values */
257         int center_mval[2]; /* center mouse values */
258         int moffset[2];
259
260 #ifdef WITH_INPUT_NDOF
261         wmNDOFMotionData *ndof;  /* latest 3D mouse values */
262 #endif
263
264         /* walk state state */
265         float base_speed; /* the base speed without run/slow down modifications */
266         float speed; /* the speed the view is moving per redraw */
267         float grid; /* world scale 1.0 default */
268
269         /* compare between last state */
270         double time_lastdraw; /* time between draws */
271
272         void *draw_handle_pixel;
273
274         /* use for some lag */
275         float dvec_prev[3]; /* old for some lag */
276
277         /* walk/fly */
278         eWalkMethod navigation_mode;
279
280         /* teleport */
281         WalkTeleport teleport;
282
283         /* look speed factor - user preferences */
284         float mouse_speed;
285
286         /* speed adjustments */
287         bool is_fast;
288         bool is_slow;
289
290         /* mouse reverse */
291         bool is_reversed;
292
293 #ifdef USE_TABLET_SUPPORT
294         /* check if we had a cursor event before */
295         bool is_cursor_first;
296
297         /* tablet devices (we can't relocate the cursor) */
298         bool is_cursor_absolute;
299 #endif
300
301         /* gravity system */
302         eWalkGravityState gravity_state;
303         float gravity;
304
305         /* height to use in walk mode */
306         float view_height;
307
308         /* counting system to allow movement to continue if a direction (WASD) key is still pressed */
309         int active_directions;
310
311         float speed_jump;
312         float jump_height; /* maximum jump height */
313         float speed_factor; /* to use for fast/slow speeds */
314
315         struct SnapObjectContext *snap_context;
316
317         struct View3DCameraControl *v3d_camera_control;
318
319 } WalkInfo;
320
321 static void drawWalkPixel(const struct bContext *UNUSED(C), ARegion *ar, void *arg)
322 {
323         /* draws an aim/cross in the center */
324         WalkInfo *walk = arg;
325
326         const int outter_length = 24;
327         const int inner_length = 14;
328         int xoff, yoff;
329         rctf viewborder;
330
331         if (walk->scene->camera) {
332                 ED_view3d_calc_camera_border(walk->scene, ar, walk->v3d, walk->rv3d, &viewborder, false);
333                 xoff = viewborder.xmin + BLI_rctf_size_x(&viewborder) * 0.5f;
334                 yoff = viewborder.ymin + BLI_rctf_size_y(&viewborder) * 0.5f;
335         }
336         else {
337                 xoff = walk->ar->winx / 2;
338                 yoff = walk->ar->winy / 2;
339         }
340
341         UI_ThemeColor(TH_VIEW_OVERLAY);
342         glBegin(GL_LINES);
343         /* North */
344         glVertex2i(xoff, yoff + inner_length);
345         glVertex2i(xoff, yoff + outter_length);
346
347         /* East */
348         glVertex2i(xoff + inner_length, yoff);
349         glVertex2i(xoff + outter_length, yoff);
350
351         /* South */
352         glVertex2i(xoff, yoff - inner_length);
353         glVertex2i(xoff, yoff - outter_length);
354
355         /* West */
356         glVertex2i(xoff - inner_length, yoff);
357         glVertex2i(xoff - outter_length, yoff);
358         glEnd();
359 }
360
361 static void walk_update_header(bContext *C, wmOperator *op, WalkInfo *walk)
362 {
363         const bool gravity = (walk->navigation_mode == WALK_MODE_GRAVITY) ||
364                              ((walk->teleport.state == WALK_TELEPORT_STATE_ON) &&
365                               (walk->teleport.navigation_mode == WALK_MODE_GRAVITY));
366         char header[UI_MAX_DRAW_STR];
367         char buf[UI_MAX_DRAW_STR];
368
369         char *p = buf;
370         int available_len = sizeof(buf);
371
372 #define WM_MODALKEY(_id) \
373         WM_modalkeymap_operator_items_to_string_buf(op->type, (_id), true, UI_MAX_SHORTCUT_STR, &available_len, &p)
374
375         BLI_snprintf(header, sizeof(header), IFACE_("%s: confirm, %s: cancel, "
376                                                     "%s: gravity (%s), "
377                                                     "%s|%s|%s|%s: move around, "
378                                                     "%s: fast, %s: slow, "
379                                                     "%s|%s: up and down, "
380                                                     "%s: teleport, %s: jump, "
381                                                     "%s: increase speed, %s: decrease speed"),
382                      WM_MODALKEY(WALK_MODAL_CONFIRM), WM_MODALKEY(WALK_MODAL_CANCEL),
383                      WM_MODALKEY(WALK_MODAL_TOGGLE), WM_bool_as_string(gravity),
384                      WM_MODALKEY(WALK_MODAL_DIR_FORWARD), WM_MODALKEY(WALK_MODAL_DIR_LEFT),
385                      WM_MODALKEY(WALK_MODAL_DIR_BACKWARD), WM_MODALKEY(WALK_MODAL_DIR_RIGHT),
386                      WM_MODALKEY(WALK_MODAL_FAST_ENABLE), WM_MODALKEY(WALK_MODAL_SLOW_ENABLE),
387                      WM_MODALKEY(WALK_MODAL_DIR_UP), WM_MODALKEY(WALK_MODAL_DIR_DOWN),
388                      WM_MODALKEY(WALK_MODAL_TELEPORT), WM_MODALKEY(WALK_MODAL_JUMP),
389                      WM_MODALKEY(WALK_MODAL_ACCELERATE), WM_MODALKEY(WALK_MODAL_DECELERATE));
390
391 #undef WM_MODALKEY
392
393         ED_area_headerprint(CTX_wm_area(C), header);
394 }
395
396 static void walk_navigation_mode_set(bContext *C, wmOperator *op, WalkInfo *walk, eWalkMethod mode)
397 {
398         if (mode == WALK_MODE_FREE) {
399                 walk->navigation_mode = WALK_MODE_FREE;
400                 walk->gravity_state = WALK_GRAVITY_STATE_OFF;
401         }
402         else { /* WALK_MODE_GRAVITY */
403                 walk->navigation_mode = WALK_MODE_GRAVITY;
404                 walk->gravity_state = WALK_GRAVITY_STATE_START;
405         }
406
407         walk_update_header(C, op, walk);
408 }
409
410 /**
411  * \param r_distance  Distance to the hit point
412  */
413 static bool walk_floor_distance_get(
414         RegionView3D *rv3d, WalkInfo *walk, const float dvec[3],
415         float *r_distance)
416 {
417         float ray_normal[3] = {0, 0, -1}; /* down */
418         float ray_start[3];
419         float r_location[3];
420         float r_normal_dummy[3];
421         float dvec_tmp[3];
422         bool ret;
423
424         *r_distance = BVH_RAYCAST_DIST_MAX;
425
426         copy_v3_v3(ray_start, rv3d->viewinv[3]);
427
428         mul_v3_v3fl(dvec_tmp, dvec, walk->grid);
429         add_v3_v3(ray_start, dvec_tmp);
430
431         ret = ED_transform_snap_object_project_ray(
432                 walk->snap_context,
433                 &(const struct SnapObjectParams){
434                     .snap_select = SNAP_ALL,
435                 },
436                 ray_start, ray_normal, r_distance,
437                 r_location, r_normal_dummy);
438
439         /* artifically scale the distance to the scene size */
440         *r_distance /= walk->grid;
441         return ret;
442 }
443
444 /**
445  * \param ray_distance  Distance to the hit point
446  * \param r_location  Location of the hit point
447  * \param r_normal  Normal of the hit surface, transformed to always face the camera
448  */
449 static bool walk_ray_cast(
450         RegionView3D *rv3d, WalkInfo *walk,
451         float r_location[3], float r_normal[3], float *ray_distance)
452 {
453         float ray_normal[3] = {0, 0, -1}; /* forward */
454         float ray_start[3];
455         bool ret;
456
457         *ray_distance = BVH_RAYCAST_DIST_MAX;
458
459         copy_v3_v3(ray_start, rv3d->viewinv[3]);
460
461         mul_mat3_m4_v3(rv3d->viewinv, ray_normal);
462
463         normalize_v3(ray_normal);
464
465         ret = ED_transform_snap_object_project_ray(
466                 walk->snap_context,
467                 &(const struct SnapObjectParams){
468                     .snap_select = SNAP_ALL,
469                 },
470                 ray_start, ray_normal, NULL,
471                 r_location, r_normal);
472
473         /* dot is positive if both rays are facing the same direction */
474         if (dot_v3v3(ray_normal, r_normal) > 0) {
475                 negate_v3(r_normal);
476         }
477
478         /* artifically scale the distance to the scene size */
479         *ray_distance /= walk->grid;
480
481         return ret;
482 }
483
484 /* WalkInfo->state */
485 enum {
486         WALK_RUNNING     = 0,
487         WALK_CANCEL      = 1,
488         WALK_CONFIRM     = 2,
489 };
490
491 /* keep the previous speed until user changes userpreferences */
492 static float base_speed = -1.f;
493 static float userdef_speed = -1.f;
494
495 static bool initWalkInfo(bContext *C, WalkInfo *walk, wmOperator *op)
496 {
497         wmWindow *win = CTX_wm_window(C);
498
499         walk->rv3d = CTX_wm_region_view3d(C);
500         walk->v3d = CTX_wm_view3d(C);
501         walk->ar = CTX_wm_region(C);
502         walk->scene = CTX_data_scene(C);
503
504 #ifdef NDOF_WALK_DEBUG
505         puts("\n-- walk begin --");
506 #endif
507
508         /* sanity check: for rare but possible case (if lib-linking the camera fails) */
509         if ((walk->rv3d->persp == RV3D_CAMOB) && (walk->v3d->camera == NULL)) {
510                 walk->rv3d->persp = RV3D_PERSP;
511         }
512
513         if (walk->rv3d->persp == RV3D_CAMOB && ID_IS_LINKED_DATABLOCK(walk->v3d->camera)) {
514                 BKE_report(op->reports, RPT_ERROR, "Cannot navigate a camera from an external library");
515                 return false;
516         }
517
518         if (ED_view3d_offset_lock_check(walk->v3d, walk->rv3d)) {
519                 BKE_report(op->reports, RPT_ERROR, "Cannot navigate when the view offset is locked");
520                 return false;
521         }
522
523         if (walk->rv3d->persp == RV3D_CAMOB && walk->v3d->camera->constraints.first) {
524                 BKE_report(op->reports, RPT_ERROR, "Cannot navigate an object with constraints");
525                 return false;
526         }
527
528         walk->state = WALK_RUNNING;
529
530         if (fabsf(U.walk_navigation.walk_speed - userdef_speed) > 0.1f) {
531                 base_speed = U.walk_navigation.walk_speed;
532                 userdef_speed = U.walk_navigation.walk_speed;
533         }
534
535         walk->speed = 0.0f;
536         walk->is_fast = false;
537         walk->is_slow = false;
538         walk->grid = (walk->scene->unit.system == USER_UNIT_NONE) ? 1.f : 1.f / walk->scene->unit.scale_length;
539
540         /* user preference settings */
541         walk->teleport.duration = U.walk_navigation.teleport_time;
542         walk->mouse_speed = U.walk_navigation.mouse_speed;
543
544         if ((U.walk_navigation.flag & USER_WALK_GRAVITY))
545                 walk_navigation_mode_set(C, op, walk, WALK_MODE_GRAVITY);
546         else
547                 walk_navigation_mode_set(C, op, walk, WALK_MODE_FREE);
548
549         walk->view_height = U.walk_navigation.view_height;
550         walk->jump_height = U.walk_navigation.jump_height;
551         walk->speed = U.walk_navigation.walk_speed;
552         walk->speed_factor = U.walk_navigation.walk_speed_factor;
553
554         walk->gravity_state = WALK_GRAVITY_STATE_OFF;
555
556         if ((walk->scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY)) {
557                 walk->gravity = fabsf(walk->scene->physics_settings.gravity[2]);
558         }
559         else {
560                 walk->gravity = 9.80668f; /* m/s2 */
561         }
562
563         walk->is_reversed = ((U.walk_navigation.flag & USER_WALK_MOUSE_REVERSE) != 0);
564
565 #ifdef USE_TABLET_SUPPORT
566         walk->is_cursor_first = true;
567
568         walk->is_cursor_absolute = false;
569 #endif
570
571         walk->active_directions = 0;
572
573 #ifdef NDOF_WALK_DRAW_TOOMUCH
574         walk->redraw = 1;
575 #endif
576         zero_v3(walk->dvec_prev);
577
578         walk->timer = WM_event_add_timer(CTX_wm_manager(C), win, TIMER, 0.01f);
579
580 #ifdef WITH_INPUT_NDOF
581         walk->ndof = NULL;
582 #endif
583
584         walk->time_lastdraw = PIL_check_seconds_timer();
585
586         walk->draw_handle_pixel = ED_region_draw_cb_activate(walk->ar->type, drawWalkPixel, walk, REGION_DRAW_POST_PIXEL);
587
588         walk->rv3d->rflag |= RV3D_NAVIGATING;
589
590         walk->snap_context = ED_transform_snap_object_context_create_view3d(
591                 CTX_data_main(C), walk->scene, SNAP_OBJECT_USE_CACHE,
592                 walk->ar, walk->v3d);
593
594         walk->v3d_camera_control = ED_view3d_cameracontrol_acquire(
595                 walk->scene, walk->v3d, walk->rv3d,
596                 (U.uiflag & USER_CAM_LOCK_NO_PARENT) == 0);
597
598         /* center the mouse */
599         walk->center_mval[0] = walk->ar->winx * 0.5f;
600         walk->center_mval[1] = walk->ar->winy * 0.5f;
601
602 #ifdef USE_PIXELSIZE_NATIVE_SUPPORT
603         walk->center_mval[0] += walk->ar->winrct.xmin;
604         walk->center_mval[1] += walk->ar->winrct.ymin;
605
606         WM_cursor_compatible_xy(win, &walk->center_mval[0], &walk->center_mval[1]);
607
608         walk->center_mval[0] -= walk->ar->winrct.xmin;
609         walk->center_mval[1] -= walk->ar->winrct.ymin;
610 #endif
611
612         copy_v2_v2_int(walk->prev_mval, walk->center_mval);
613
614         WM_cursor_warp(win,
615                        walk->ar->winrct.xmin + walk->center_mval[0],
616                        walk->ar->winrct.ymin + walk->center_mval[1]);
617
618         /* remove the mouse cursor temporarily */
619         WM_cursor_modal_set(win, CURSOR_NONE);
620
621         return 1;
622 }
623
624 static int walkEnd(bContext *C, WalkInfo *walk)
625 {
626         wmWindow *win;
627         RegionView3D *rv3d;
628
629         if (walk->state == WALK_RUNNING)
630                 return OPERATOR_RUNNING_MODAL;
631
632 #ifdef NDOF_WALK_DEBUG
633         puts("\n-- walk end --");
634 #endif
635
636         win = CTX_wm_window(C);
637         rv3d = walk->rv3d;
638
639         WM_event_remove_timer(CTX_wm_manager(C), win, walk->timer);
640
641         ED_region_draw_cb_exit(walk->ar->type, walk->draw_handle_pixel);
642
643         ED_transform_snap_object_context_destroy(walk->snap_context);
644
645         ED_view3d_cameracontrol_release(walk->v3d_camera_control, walk->state == WALK_CANCEL);
646
647         rv3d->rflag &= ~RV3D_NAVIGATING;
648
649 #ifdef WITH_INPUT_NDOF
650         if (walk->ndof)
651                 MEM_freeN(walk->ndof);
652 #endif
653
654         /* restore the cursor */
655         WM_cursor_modal_restore(win);
656
657 #ifdef USE_TABLET_SUPPORT
658         if (walk->is_cursor_absolute == false)
659 #endif
660         {
661                 /* center the mouse */
662                 WM_cursor_warp(
663                         win,
664                         walk->ar->winrct.xmin + walk->center_mval[0],
665                         walk->ar->winrct.ymin + walk->center_mval[1]);
666         }
667
668         if (walk->state == WALK_CONFIRM) {
669                 MEM_freeN(walk);
670                 return OPERATOR_FINISHED;
671         }
672
673         MEM_freeN(walk);
674         return OPERATOR_CANCELLED;
675 }
676
677 static bool wm_event_is_last_mousemove(const wmEvent *event)
678 {
679         while ((event = event->next)) {
680                 if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
681                         return false;
682                 }
683         }
684         return true;
685 }
686
687 static void walkEvent(bContext *C, wmOperator *op, WalkInfo *walk, const wmEvent *event)
688 {
689         if (event->type == TIMER && event->customdata == walk->timer) {
690                 walk->redraw = true;
691         }
692         else if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
693
694 #ifdef USE_TABLET_SUPPORT
695                 if (walk->is_cursor_first) {
696                         /* wait until we get the 'warp' event */
697                         if ((walk->center_mval[0] == event->mval[0]) &&
698                             (walk->center_mval[1] == event->mval[1]))
699                         {
700                                 walk->is_cursor_first = false;
701                         }
702                         else {
703                                 /* note, its possible the system isn't giving us the warp event
704                                  * ideally we shouldn't have to worry about this, see: T45361 */
705                                 wmWindow *win = CTX_wm_window(C);
706                                 WM_cursor_warp(win,
707                                                walk->ar->winrct.xmin + walk->center_mval[0],
708                                                walk->ar->winrct.ymin + walk->center_mval[1]);
709                         }
710                         return;
711                 }
712
713                 if ((walk->is_cursor_absolute == false) && WM_event_is_absolute(event)) {
714                         walk->is_cursor_absolute = true;
715                         copy_v2_v2_int(walk->prev_mval, event->mval);
716                         copy_v2_v2_int(walk->center_mval, event->mval);
717                         /* without this we can't turn 180d */
718                         CLAMP_MIN(walk->mouse_speed, 4.0f);
719                 }
720 #endif  /* USE_TABLET_SUPPORT */
721
722
723                 walk->moffset[0] += event->mval[0] - walk->prev_mval[0];
724                 walk->moffset[1] += event->mval[1] - walk->prev_mval[1];
725
726                 copy_v2_v2_int(walk->prev_mval, event->mval);
727
728                 if ((walk->center_mval[0] != event->mval[0]) ||
729                     (walk->center_mval[1] != event->mval[1]))
730                 {
731                         walk->redraw = true;
732
733 #ifdef USE_TABLET_SUPPORT
734                         if (walk->is_cursor_absolute) {
735                                 /* pass */
736                         }
737                         else
738 #endif
739                         if (wm_event_is_last_mousemove(event)) {
740                                 wmWindow *win = CTX_wm_window(C);
741
742 #ifdef __APPLE__
743                                 if ((abs(walk->prev_mval[0] - walk->center_mval[0]) > walk->center_mval[0] / 2) ||
744                                     (abs(walk->prev_mval[1] - walk->center_mval[1]) > walk->center_mval[1] / 2))
745 #endif
746                                 {
747                                         WM_cursor_warp(win,
748                                                        walk->ar->winrct.xmin + walk->center_mval[0],
749                                                        walk->ar->winrct.ymin + walk->center_mval[1]);
750                                         copy_v2_v2_int(walk->prev_mval, walk->center_mval);
751                                 }
752                         }
753                 }
754         }
755 #ifdef WITH_INPUT_NDOF
756         else if (event->type == NDOF_MOTION) {
757                 /* do these automagically get delivered? yes. */
758                 // puts("ndof motion detected in walk mode!");
759                 // static const char *tag_name = "3D mouse position";
760
761                 const wmNDOFMotionData *incoming_ndof = event->customdata;
762                 switch (incoming_ndof->progress) {
763                         case P_STARTING:
764                                 /* start keeping track of 3D mouse position */
765 #  ifdef NDOF_WALK_DEBUG
766                                 puts("start keeping track of 3D mouse position");
767 #  endif
768                                 /* fall-through */
769                         case P_IN_PROGRESS:
770                                 /* update 3D mouse position */
771 #  ifdef NDOF_WALK_DEBUG
772                                 putchar('.'); fflush(stdout);
773 #  endif
774                                 if (walk->ndof == NULL) {
775                                         // walk->ndof = MEM_mallocN(sizeof(wmNDOFMotionData), tag_name);
776                                         walk->ndof = MEM_dupallocN(incoming_ndof);
777                                         // walk->ndof = malloc(sizeof(wmNDOFMotionData));
778                                 }
779                                 else {
780                                         memcpy(walk->ndof, incoming_ndof, sizeof(wmNDOFMotionData));
781                                 }
782                                 break;
783                         case P_FINISHING:
784                                 /* stop keeping track of 3D mouse position */
785 #  ifdef NDOF_WALK_DEBUG
786                                 puts("stop keeping track of 3D mouse position");
787 #  endif
788                                 if (walk->ndof) {
789                                         MEM_freeN(walk->ndof);
790                                         // free(walk->ndof);
791                                         walk->ndof = NULL;
792                                 }
793
794                                 /* update the time else the view will jump when 2D mouse/timer resume */
795                                 walk->time_lastdraw = PIL_check_seconds_timer();
796
797                                 break;
798                         default:
799                                 break; /* should always be one of the above 3 */
800                 }
801         }
802 #endif /* WITH_INPUT_NDOF */
803         /* handle modal keymap first */
804         else if (event->type == EVT_MODAL_MAP) {
805                 switch (event->val) {
806                         case WALK_MODAL_CANCEL:
807                                 walk->state = WALK_CANCEL;
808                                 break;
809                         case WALK_MODAL_CONFIRM:
810                                 walk->state = WALK_CONFIRM;
811                                 break;
812
813                         case WALK_MODAL_ACCELERATE:
814                                 base_speed *= 1.0f + (walk->is_slow ? 0.01f : 0.1f);
815                                 break;
816                         case WALK_MODAL_DECELERATE:
817                                 base_speed /= 1.0f + (walk->is_slow ? 0.01f : 0.1f);
818                                 break;
819
820                         /* implement WASD keys */
821                         case WALK_MODAL_DIR_FORWARD:
822                                 walk->active_directions |= WALK_BIT_FORWARD;
823                                 break;
824                         case WALK_MODAL_DIR_BACKWARD:
825                                 walk->active_directions |= WALK_BIT_BACKWARD;
826                                 break;
827                         case WALK_MODAL_DIR_LEFT:
828                                 walk->active_directions |= WALK_BIT_LEFT;
829                                 break;
830                         case WALK_MODAL_DIR_RIGHT:
831                                 walk->active_directions |= WALK_BIT_RIGHT;
832                                 break;
833                         case WALK_MODAL_DIR_UP:
834                                 walk->active_directions |= WALK_BIT_UP;
835                                 break;
836                         case WALK_MODAL_DIR_DOWN:
837                                 walk->active_directions |= WALK_BIT_DOWN;
838                                 break;
839
840                         case WALK_MODAL_DIR_FORWARD_STOP:
841                                 walk->active_directions &= ~WALK_BIT_FORWARD;
842                                 break;
843                         case WALK_MODAL_DIR_BACKWARD_STOP:
844                                 walk->active_directions &= ~WALK_BIT_BACKWARD;
845                                 break;
846                         case WALK_MODAL_DIR_LEFT_STOP:
847                                 walk->active_directions &= ~WALK_BIT_LEFT;
848                                 break;
849                         case WALK_MODAL_DIR_RIGHT_STOP:
850                                 walk->active_directions &= ~WALK_BIT_RIGHT;
851                                 break;
852                         case WALK_MODAL_DIR_UP_STOP:
853                                 walk->active_directions &= ~WALK_BIT_UP;
854                                 break;
855                         case WALK_MODAL_DIR_DOWN_STOP:
856                                 walk->active_directions &= ~WALK_BIT_DOWN;
857                                 break;
858
859                         case WALK_MODAL_FAST_ENABLE:
860                                 walk->is_fast = true;
861                                 break;
862                         case WALK_MODAL_FAST_DISABLE:
863                                 walk->is_fast = false;
864                                 break;
865                         case WALK_MODAL_SLOW_ENABLE:
866                                 walk->is_slow = true;
867                                 break;
868                         case WALK_MODAL_SLOW_DISABLE:
869                                 walk->is_slow = false;
870                                 break;
871
872 #define JUMP_SPEED_MIN 1.0f
873 #define JUMP_TIME_MAX 0.2f /* s */
874 #define JUMP_SPEED_MAX sqrtf(2.0f * walk->gravity * walk->jump_height)
875
876                         case WALK_MODAL_JUMP_STOP:
877                                 if (walk->gravity_state == WALK_GRAVITY_STATE_JUMP) {
878                                         float t;
879
880                                         /* delta time */
881                                         t = (float)(PIL_check_seconds_timer() - walk->teleport.initial_time);
882
883                                         /* reduce the veolocity, if JUMP wasn't hold for long enough */
884                                         t = min_ff(t, JUMP_TIME_MAX);
885                                         walk->speed_jump = JUMP_SPEED_MIN + t * (JUMP_SPEED_MAX - JUMP_SPEED_MIN) / JUMP_TIME_MAX;
886
887                                         /* when jumping, duration is how long it takes before we start going down */
888                                         walk->teleport.duration = getVelocityZeroTime(walk->gravity, walk->speed_jump);
889
890                                         /* no more increase of jump speed */
891                                         walk->gravity_state = WALK_GRAVITY_STATE_ON;
892                                 }
893                                 break;
894                         case WALK_MODAL_JUMP:
895                                 if ((walk->navigation_mode == WALK_MODE_GRAVITY) &&
896                                     (walk->gravity_state == WALK_GRAVITY_STATE_OFF) &&
897                                     (walk->teleport.state == WALK_TELEPORT_STATE_OFF))
898                                 {
899                                         /* no need to check for ground,
900                                          * walk->gravity wouldn't be off
901                                          * if we were over a hole */
902                                         walk->gravity_state = WALK_GRAVITY_STATE_JUMP;
903                                         walk->speed_jump = JUMP_SPEED_MAX;
904
905                                         walk->teleport.initial_time = PIL_check_seconds_timer();
906                                         copy_v3_v3(walk->teleport.origin, walk->rv3d->viewinv[3]);
907
908                                         /* using previous vec because WASD keys are not called when SPACE is */
909                                         copy_v2_v2(walk->teleport.direction, walk->dvec_prev);
910
911                                         /* when jumping, duration is how long it takes before we start going down */
912                                         walk->teleport.duration = getVelocityZeroTime(walk->gravity, walk->speed_jump);
913                                 }
914
915                                 break;
916
917                         case WALK_MODAL_TELEPORT:
918                         {
919                                 float loc[3], nor[3];
920                                 float distance;
921                                 bool ret = walk_ray_cast(walk->rv3d, walk, loc, nor, &distance);
922
923                                 /* in case we are teleporting middle way from a jump */
924                                 walk->speed_jump = 0.0f;
925
926                                 if (ret) {
927                                         WalkTeleport *teleport = &walk->teleport;
928                                         teleport->state = WALK_TELEPORT_STATE_ON;
929                                         teleport->initial_time = PIL_check_seconds_timer();
930                                         teleport->duration = U.walk_navigation.teleport_time;
931
932                                         teleport->navigation_mode = walk->navigation_mode;
933                                         walk_navigation_mode_set(C, op, walk, WALK_MODE_FREE);
934
935                                         copy_v3_v3(teleport->origin, walk->rv3d->viewinv[3]);
936
937                                         /* stop the camera from a distance (camera height) */
938                                         normalize_v3_length(nor, walk->view_height);
939                                         add_v3_v3(loc, nor);
940
941                                         sub_v3_v3v3(teleport->direction, loc, teleport->origin);
942                                 }
943                                 else {
944                                         walk->teleport.state = WALK_TELEPORT_STATE_OFF;
945                                 }
946                                 break;
947                         }
948
949 #undef JUMP_SPEED_MAX
950 #undef JUMP_TIME_MAX
951 #undef JUMP_SPEED_MIN
952
953                         case WALK_MODAL_TOGGLE:
954                                 if (walk->navigation_mode == WALK_MODE_GRAVITY) {
955                                         walk_navigation_mode_set(C, op, walk, WALK_MODE_FREE);
956                                 }
957                                 else { /* WALK_MODE_FREE */
958                                         walk_navigation_mode_set(C, op, walk, WALK_MODE_GRAVITY);
959                                 }
960                                 break;
961                 }
962         }
963 }
964
965 static void walkMoveCamera(bContext *C, WalkInfo *walk,
966                             const bool do_rotate, const bool do_translate)
967 {
968         ED_view3d_cameracontrol_update(walk->v3d_camera_control, true, C, do_rotate, do_translate);
969 }
970
971 static float getFreeFallDistance(const float gravity, const float time)
972 {
973         return gravity * (time * time) * 0.5f;
974 }
975
976 static float getVelocityZeroTime(const float gravity, const float velocity)
977 {
978         return velocity / gravity;
979 }
980
981 static int walkApply(bContext *C, wmOperator *op, WalkInfo *walk)
982 {
983 #define WALK_ROTATE_FAC 2.2f /* more is faster */
984 #define WALK_TOP_LIMIT DEG2RADF(85.0f)
985 #define WALK_BOTTOM_LIMIT DEG2RADF(-80.0f)
986 #define WALK_MOVE_SPEED base_speed
987 #define WALK_BOOST_FACTOR ((void)0, walk->speed_factor)
988
989         /* walk mode - Ctrl+Shift+F
990          * a walk loop where the user can move move the view as if they are in a walk game
991          */
992         RegionView3D *rv3d = walk->rv3d;
993         ARegion *ar = walk->ar;
994
995         float mat[3][3]; /* 3x3 copy of the view matrix so we can move along the view axis */
996         float dvec[3] = {0.0f, 0.0f, 0.0f}; /* this is the direction that's added to the view offset per redraw */
997
998         int moffset[2]; /* mouse offset from the views center */
999         float tmp_quat[4]; /* used for rotating the view */
1000
1001 #ifdef NDOF_WALK_DEBUG
1002         {
1003                 static unsigned int iteration = 1;
1004                 printf("walk timer %d\n", iteration++);
1005         }
1006 #endif
1007
1008         {
1009                 /* mouse offset from the center */
1010                 copy_v2_v2_int(moffset, walk->moffset);
1011
1012                 /* apply moffset so we can re-accumulate */
1013                 walk->moffset[0] = 0;
1014                 walk->moffset[1] = 0;
1015
1016                 /* revert mouse */
1017                 if (walk->is_reversed) {
1018                         moffset[1] = -moffset[1];
1019                 }
1020
1021                 /* Should we redraw? */
1022                 if ((walk->active_directions) ||
1023                     moffset[0] || moffset[1] ||
1024                     walk->teleport.state == WALK_TELEPORT_STATE_ON ||
1025                     walk->gravity_state != WALK_GRAVITY_STATE_OFF)
1026                 {
1027                         float dvec_tmp[3];
1028
1029                         /* time how fast it takes for us to redraw,
1030                          * this is so simple scenes don't walk too fast */
1031                         double time_current;
1032                         float time_redraw;
1033 #ifdef NDOF_WALK_DRAW_TOOMUCH
1034                         walk->redraw = 1;
1035 #endif
1036                         time_current = PIL_check_seconds_timer();
1037                         time_redraw = (float)(time_current - walk->time_lastdraw);
1038
1039                         walk->time_lastdraw = time_current;
1040
1041                         /* base speed in m/s */
1042                         walk->speed = WALK_MOVE_SPEED;
1043
1044                         if (walk->is_fast) {
1045                                 walk->speed *= WALK_BOOST_FACTOR;
1046                         }
1047                         else if (walk->is_slow) {
1048                                 walk->speed *= 1.0f / WALK_BOOST_FACTOR;
1049                         }
1050
1051                         copy_m3_m4(mat, rv3d->viewinv);
1052
1053                         {
1054                                 /* rotate about the X axis- look up/down */
1055                                 if (moffset[1]) {
1056                                         float upvec[3];
1057                                         float angle;
1058                                         float y;
1059
1060                                         /* relative offset */
1061                                         y = (float) moffset[1] / ar->winy;
1062
1063                                         /* speed factor */
1064                                         y *= WALK_ROTATE_FAC;
1065
1066                                         /* user adjustement factor */
1067                                         y *= walk->mouse_speed;
1068
1069                                         /* clamp the angle limits */
1070                                         /* it ranges from 90.0f to -90.0f */
1071                                         angle = -asinf(rv3d->viewmat[2][2]);
1072
1073                                         if (angle > WALK_TOP_LIMIT && y > 0.0f)
1074                                                 y = 0.0f;
1075
1076                                         else if (angle < WALK_BOTTOM_LIMIT && y < 0.0f)
1077                                                 y = 0.0f;
1078
1079                                         copy_v3_fl3(upvec, 1.0f, 0.0f, 0.0f);
1080                                         mul_m3_v3(mat, upvec);
1081                                         /* Rotate about the relative up vec */
1082                                         axis_angle_to_quat(tmp_quat, upvec, -y);
1083                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
1084                                 }
1085
1086                                 /* rotate about the Y axis- look left/right */
1087                                 if (moffset[0]) {
1088                                         float upvec[3];
1089                                         float x;
1090
1091                                         /* if we're upside down invert the moffset */
1092                                         copy_v3_fl3(upvec, 0.0f, 1.0f, 0.0f);
1093                                         mul_m3_v3(mat, upvec);
1094
1095                                         if (upvec[2] < 0.0f)
1096                                                 moffset[0] = -moffset[0];
1097
1098                                         /* relative offset */
1099                                         x = (float) moffset[0] / ar->winx;
1100
1101                                         /* speed factor */
1102                                         x *= WALK_ROTATE_FAC;
1103
1104                                         /* user adjustement factor */
1105                                         x *= walk->mouse_speed;
1106
1107                                         /* Rotate about the relative up vec */
1108                                         axis_angle_to_quat_single(tmp_quat, 'Z', x);
1109                                         mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
1110                                 }
1111                         }
1112
1113                         /* WASD - 'move' translation code */
1114                         if ((walk->active_directions) &&
1115                             (walk->gravity_state == WALK_GRAVITY_STATE_OFF))
1116                         {
1117
1118                                 short direction;
1119                                 zero_v3(dvec);
1120
1121                                 if ((walk->active_directions & WALK_BIT_FORWARD) ||
1122                                     (walk->active_directions & WALK_BIT_BACKWARD))
1123                                 {
1124
1125                                         direction = 0;
1126
1127                                         if ((walk->active_directions & WALK_BIT_FORWARD))
1128                                                 direction += 1;
1129
1130                                         if ((walk->active_directions & WALK_BIT_BACKWARD))
1131                                                 direction -= 1;
1132
1133                                         copy_v3_fl3(dvec_tmp, 0.0f, 0.0f, direction);
1134                                         mul_m3_v3(mat, dvec_tmp);
1135
1136                                         if (walk->navigation_mode == WALK_MODE_GRAVITY) {
1137                                                 dvec_tmp[2] = 0.0f;
1138                                         }
1139
1140                                         normalize_v3(dvec_tmp);
1141                                         add_v3_v3(dvec, dvec_tmp);
1142
1143                                 }
1144
1145                                 if ((walk->active_directions & WALK_BIT_LEFT) ||
1146                                     (walk->active_directions & WALK_BIT_RIGHT))
1147                                 {
1148
1149                                         direction = 0;
1150
1151                                         if ((walk->active_directions & WALK_BIT_LEFT))
1152                                                 direction += 1;
1153
1154                                         if ((walk->active_directions & WALK_BIT_RIGHT))
1155                                                 direction -= 1;
1156
1157                                         dvec_tmp[0] = direction * rv3d->viewinv[0][0];
1158                                         dvec_tmp[1] = direction * rv3d->viewinv[0][1];
1159                                         dvec_tmp[2] = 0.0f;
1160
1161                                         normalize_v3(dvec_tmp);
1162                                         add_v3_v3(dvec, dvec_tmp);
1163
1164                                 }
1165
1166                                 if ((walk->active_directions & WALK_BIT_UP) ||
1167                                     (walk->active_directions & WALK_BIT_DOWN))
1168                                 {
1169
1170                                         if (walk->navigation_mode == WALK_MODE_FREE) {
1171
1172                                                 direction = 0;
1173
1174                                                 if ((walk->active_directions & WALK_BIT_UP))
1175                                                         direction -= 1;
1176
1177                                                 if ((walk->active_directions & WALK_BIT_DOWN))
1178                                                         direction = 1;
1179
1180                                                 copy_v3_fl3(dvec_tmp, 0.0f, 0.0f, direction);
1181                                                 add_v3_v3(dvec, dvec_tmp);
1182                                         }
1183                                 }
1184
1185                                 /* apply movement */
1186                                 mul_v3_fl(dvec, walk->speed * time_redraw);
1187                         }
1188
1189                         /* stick to the floor */
1190                         if (walk->navigation_mode == WALK_MODE_GRAVITY &&
1191                             ELEM(walk->gravity_state,
1192                                  WALK_GRAVITY_STATE_OFF,
1193                                  WALK_GRAVITY_STATE_START))
1194                         {
1195
1196                                 bool ret;
1197                                 float ray_distance;
1198                                 float difference = -100.0f;
1199                                 float fall_distance;
1200
1201                                 ret = walk_floor_distance_get(rv3d, walk, dvec, &ray_distance);
1202
1203                                 if (ret) {
1204                                         difference = walk->view_height - ray_distance;
1205                                 }
1206
1207                                 /* the distance we would fall naturally smoothly enough that we
1208                                  * can manually drop the object without activating gravity */
1209                                 fall_distance = time_redraw * walk->speed * WALK_BOOST_FACTOR;
1210
1211                                 if (fabsf(difference) < fall_distance) {
1212                                         /* slope/stairs */
1213                                         dvec[2] -= difference;
1214
1215                                         /* in case we switched from FREE to GRAVITY too close to the ground */
1216                                         if (walk->gravity_state == WALK_GRAVITY_STATE_START)
1217                                                 walk->gravity_state = WALK_GRAVITY_STATE_OFF;
1218                                 }
1219                                 else {
1220                                         /* hijack the teleport variables */
1221                                         walk->teleport.initial_time = PIL_check_seconds_timer();
1222                                         walk->gravity_state = WALK_GRAVITY_STATE_ON;
1223                                         walk->teleport.duration = 0.0f;
1224
1225                                         copy_v3_v3(walk->teleport.origin, walk->rv3d->viewinv[3]);
1226                                         copy_v2_v2(walk->teleport.direction, dvec);
1227
1228                                 }
1229                         }
1230
1231                         /* Falling or jumping) */
1232                         if (ELEM(walk->gravity_state, WALK_GRAVITY_STATE_ON, WALK_GRAVITY_STATE_JUMP)) {
1233                                 float t;
1234                                 float z_cur, z_new;
1235                                 bool ret;
1236                                 float ray_distance, difference = -100.0f;
1237
1238                                 /* delta time */
1239                                 t = (float)(PIL_check_seconds_timer() - walk->teleport.initial_time);
1240
1241                                 /* keep moving if we were moving */
1242                                 copy_v2_v2(dvec, walk->teleport.direction);
1243
1244                                 z_cur = walk->rv3d->viewinv[3][2];
1245                                 z_new = walk->teleport.origin[2] - getFreeFallDistance(walk->gravity, t) * walk->grid;
1246
1247                                 /* jump */
1248                                 z_new += t * walk->speed_jump * walk->grid;
1249
1250                                 /* duration is the jump duration */
1251                                 if (t > walk->teleport.duration) {
1252
1253                                         /* check to see if we are landing */
1254                                         ret = walk_floor_distance_get(rv3d, walk, dvec, &ray_distance);
1255
1256                                         if (ret) {
1257                                                 difference = walk->view_height - ray_distance;
1258                                         }
1259
1260                                         if (difference > 0.0f) {
1261                                                 /* quit falling, lands at "view_height" from the floor */
1262                                                 dvec[2] -= difference;
1263                                                 walk->gravity_state = WALK_GRAVITY_STATE_OFF;
1264                                                 walk->speed_jump = 0.0f;
1265                                         }
1266                                         else {
1267                                                 /* keep falling */
1268                                                 dvec[2] = z_cur - z_new;
1269                                         }
1270                                 }
1271                                 else {
1272                                         /* keep going up (jump) */
1273                                         dvec[2] = z_cur - z_new;
1274                                 }
1275                         }
1276
1277                         /* Teleport */
1278                         else if (walk->teleport.state == WALK_TELEPORT_STATE_ON) {
1279                                 float t; /* factor */
1280                                 float new_loc[3];
1281                                 float cur_loc[3];
1282
1283                                 /* linear interpolation */
1284                                 t = (float)(PIL_check_seconds_timer() - walk->teleport.initial_time);
1285                                 t /= walk->teleport.duration;
1286
1287                                 /* clamp so we don't go past our limit */
1288                                 if (t >= 1.0f) {
1289                                         t = 1.0f;
1290                                         walk->teleport.state = WALK_TELEPORT_STATE_OFF;
1291                                         walk_navigation_mode_set(C, op, walk, walk->teleport.navigation_mode);
1292                                 }
1293
1294                                 mul_v3_v3fl(new_loc, walk->teleport.direction, t);
1295                                 add_v3_v3(new_loc, walk->teleport.origin);
1296
1297                                 copy_v3_v3(cur_loc, walk->rv3d->viewinv[3]);
1298                                 sub_v3_v3v3(dvec, cur_loc, new_loc);
1299                         }
1300
1301                         if (rv3d->persp == RV3D_CAMOB) {
1302                                 Object *lock_ob = ED_view3d_cameracontrol_object_get(walk->v3d_camera_control);
1303                                 if (lock_ob->protectflag & OB_LOCK_LOCX) dvec[0] = 0.0f;
1304                                 if (lock_ob->protectflag & OB_LOCK_LOCY) dvec[1] = 0.0f;
1305                                 if (lock_ob->protectflag & OB_LOCK_LOCZ) dvec[2] = 0.0f;
1306                         }
1307
1308                         /* scale the movement to the scene size */
1309                         mul_v3_v3fl(dvec_tmp, dvec, walk->grid);
1310                         add_v3_v3(rv3d->ofs, dvec_tmp);
1311
1312                         if (rv3d->persp == RV3D_CAMOB) {
1313                                 const bool do_rotate = (moffset[0] || moffset[1]);
1314                                 const bool do_translate = (walk->speed != 0.0f);
1315                                 walkMoveCamera(C, walk, do_rotate, do_translate);
1316                         }
1317                 }
1318                 else {
1319                         /* we're not redrawing but we need to update the time else the view will jump */
1320                         walk->time_lastdraw = PIL_check_seconds_timer();
1321                 }
1322                 /* end drawing */
1323                 copy_v3_v3(walk->dvec_prev, dvec);
1324         }
1325
1326         return OPERATOR_FINISHED;
1327 #undef WALK_ROTATE_FAC
1328 #undef WALK_ZUP_CORRECT_FAC
1329 #undef WALK_ZUP_CORRECT_ACCEL
1330 #undef WALK_SMOOTH_FAC
1331 #undef WALK_TOP_LIMIT
1332 #undef WALK_BOTTOM_LIMIT
1333 #undef WALK_MOVE_SPEED
1334 #undef WALK_BOOST_FACTOR
1335 }
1336
1337 #ifdef WITH_INPUT_NDOF
1338 static void walkApply_ndof(bContext *C, WalkInfo *walk)
1339 {
1340         Object *lock_ob = ED_view3d_cameracontrol_object_get(walk->v3d_camera_control);
1341         bool has_translate, has_rotate;
1342
1343         view3d_ndof_fly(walk->ndof,
1344                         walk->v3d, walk->rv3d,
1345                         walk->is_slow, lock_ob ? lock_ob->protectflag : 0,
1346                         &has_translate, &has_rotate);
1347
1348         if (has_translate || has_rotate) {
1349                 walk->redraw = true;
1350
1351                 if (walk->rv3d->persp == RV3D_CAMOB) {
1352                         walkMoveCamera(C, walk, has_rotate, has_translate);
1353                 }
1354         }
1355 }
1356 #endif /* WITH_INPUT_NDOF */
1357
1358 /****** walk operator ******/
1359 static int walk_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1360 {
1361         RegionView3D *rv3d = CTX_wm_region_view3d(C);
1362         WalkInfo *walk;
1363
1364         if (rv3d->viewlock & RV3D_LOCKED)
1365                 return OPERATOR_CANCELLED;
1366
1367         walk = MEM_callocN(sizeof(WalkInfo), "NavigationWalkOperation");
1368
1369         op->customdata = walk;
1370
1371         if (initWalkInfo(C, walk, op) == false) {
1372                 MEM_freeN(op->customdata);
1373                 return OPERATOR_CANCELLED;
1374         }
1375
1376         walkEvent(C, op, walk, event);
1377
1378         WM_event_add_modal_handler(C, op);
1379
1380         return OPERATOR_RUNNING_MODAL;
1381 }
1382
1383 static void walk_cancel(bContext *C, wmOperator *op)
1384 {
1385         WalkInfo *walk = op->customdata;
1386
1387         walk->state = WALK_CANCEL;
1388         walkEnd(C, walk);
1389         op->customdata = NULL;
1390 }
1391
1392 static int walk_modal(bContext *C, wmOperator *op, const wmEvent *event)
1393 {
1394         int exit_code;
1395         bool do_draw = false;
1396         WalkInfo *walk = op->customdata;
1397         RegionView3D *rv3d = walk->rv3d;
1398         Object *walk_object = ED_view3d_cameracontrol_object_get(walk->v3d_camera_control);
1399
1400         walk->redraw = false;
1401
1402         walkEvent(C, op, walk, event);
1403
1404 #ifdef WITH_INPUT_NDOF
1405         if (walk->ndof) { /* 3D mouse overrules [2D mouse + timer] */
1406                 if (event->type == NDOF_MOTION) {
1407                         walkApply_ndof(C, walk);
1408                 }
1409         }
1410         else
1411 #endif /* WITH_INPUT_NDOF */
1412         if (event->type == TIMER && event->customdata == walk->timer) {
1413                 walkApply(C, op, walk);
1414         }
1415
1416         do_draw |= walk->redraw;
1417
1418         exit_code = walkEnd(C, walk);
1419
1420         if (exit_code != OPERATOR_RUNNING_MODAL)
1421                 do_draw = true;
1422
1423         if (do_draw) {
1424                 if (rv3d->persp == RV3D_CAMOB) {
1425                         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, walk_object);
1426                 }
1427
1428                 // puts("redraw!"); // too frequent, commented with NDOF_WALK_DRAW_TOOMUCH for now
1429                 ED_region_tag_redraw(CTX_wm_region(C));
1430         }
1431
1432         if (ELEM(exit_code, OPERATOR_FINISHED, OPERATOR_CANCELLED))
1433                 ED_area_headerprint(CTX_wm_area(C), NULL);
1434
1435         return exit_code;
1436 }
1437
1438 void VIEW3D_OT_walk(wmOperatorType *ot)
1439 {
1440         /* identifiers */
1441         ot->name = "Walk Navigation";
1442         ot->description = "Interactively walk around the scene";
1443         ot->idname = "VIEW3D_OT_walk";
1444
1445         /* api callbacks */
1446         ot->invoke = walk_invoke;
1447         ot->cancel = walk_cancel;
1448         ot->modal = walk_modal;
1449         ot->poll = ED_operator_region_view3d_active;
1450
1451         /* flags */
1452         ot->flag = OPTYPE_BLOCKING;
1453 }