Multitouch trackpad 2 fingers gestures implementation
[blender-staging.git] / source / blender / editors / space_view3d / view3d_edit.c
index c0d9d49d1883cc5a4cb128c2282bb40b6525ffba..288918c3c1e9f0a39c2c2afe4597a6b5b7b4c8f2 100644 (file)
@@ -445,7 +445,9 @@ enum {
 #define VIEW_MODAL_CONFIRM                             1 /* used for all view operations */
 #define VIEWROT_MODAL_AXIS_SNAP_ENABLE 2
 #define VIEWROT_MODAL_AXIS_SNAP_DISABLE        3
-
+#define VIEWROT_MODAL_SWITCH_ZOOM              4
+#define VIEWROT_MODAL_SWITCH_MOVE              5
+#define VIEWROT_MODAL_SWITCH_ROTATE            6
 
 /* called in transform_ops.c, on each regeneration of keymaps  */
 void viewrotate_modal_keymap(wmKeyConfig *keyconf)
@@ -454,7 +456,10 @@ void viewrotate_modal_keymap(wmKeyConfig *keyconf)
        {VIEW_MODAL_CONFIRM,    "CONFIRM", 0, "Cancel", ""},
 
        {VIEWROT_MODAL_AXIS_SNAP_ENABLE,        "AXIS_SNAP_ENABLE", 0, "Enable Axis Snap", ""},
-       {VIEWROT_MODAL_AXIS_SNAP_DISABLE,       "AXIS_SNAP_DISABLE", 0, "Enable Axis Snap", ""},
+       {VIEWROT_MODAL_AXIS_SNAP_DISABLE,       "AXIS_SNAP_DISABLE", 0, "Disable Axis Snap", ""},
+               
+       {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
+       {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
 
        {0, NULL, 0, NULL, NULL}};
 
@@ -469,9 +474,13 @@ void viewrotate_modal_keymap(wmKeyConfig *keyconf)
        WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
        WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
 
-       WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_ENABLE);
-       WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_DISABLE);
+       WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_ENABLE);
+       WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_DISABLE);
 
+       WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
+       WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
+       WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
+       
        /* assign map to operators */
        WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");
 
@@ -545,7 +554,7 @@ static void viewrotate_apply(ViewOpsData *vod, int x, int y)
                invert_m3_m3(m_inv,m);
 
                /* Determine the direction of the x vector (for rotating up and down) */
-               /* This can likely be compuated directly from the quaternion. */
+               /* This can likely be computed directly from the quaternion. */
                mul_m3_v3(m_inv,xvec);
 
                /* Perform the up/down rotation */
@@ -633,6 +642,14 @@ static int viewrotate_modal(bContext *C, wmOperator *op, wmEvent *event)
                                vod->axis_snap= FALSE;
                                event_code= VIEW_APPLY;
                                break;
+                       case VIEWROT_MODAL_SWITCH_ZOOM:
+                               WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
+                               event_code= VIEW_CONFIRM;
+                               break;
+                       case VIEWROT_MODAL_SWITCH_MOVE:
+                               WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
+                               event_code= VIEW_CONFIRM;
+                               break;
                }
        }
        else if(event->type==vod->origkey && event->val==KM_RELEASE) {
@@ -673,11 +690,30 @@ static int viewrotate_invoke(bContext *C, wmOperator *op, wmEvent *event)
                        vod->rv3d->persp= RV3D_PERSP;
                ED_region_tag_redraw(vod->ar);
        }
+       
+       if (event->type == MOUSEPAN) {
+               viewrotate_apply(vod, event->prevx, event->prevy);
+               request_depth_update(CTX_wm_region_view3d(C));
+               
+               viewops_data_free(C, op);
+               
+               return OPERATOR_FINISHED;
+       }
+       else if (event->type == MOUSEROTATE) {
+               /* MOUSEROTATE performs orbital rotation, so y axis delta is set to 0 */
+               viewrotate_apply(vod, event->prevx, event->y);
+               request_depth_update(CTX_wm_region_view3d(C));
+               
+               viewops_data_free(C, op);
+               
+               return OPERATOR_FINISHED;
+       }
+       else {          
+               /* add temp handler */
+               WM_event_add_modal_handler(C, op);
 
-       /* add temp handler */
-       WM_event_add_modal_handler(C, op);
-
-       return OPERATOR_RUNNING_MODAL;
+               return OPERATOR_RUNNING_MODAL;
+       }
 }
 
 static int ED_operator_view3d_rotate(bContext *C)
@@ -736,6 +772,10 @@ void viewmove_modal_keymap(wmKeyConfig *keyconf)
        WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
        WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
 
+       WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
+       WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
+       WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
+       
        /* assign map to operators */
        WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
 }
@@ -784,6 +824,14 @@ static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
                        case VIEW_MODAL_CONFIRM:
                                event_code= VIEW_CONFIRM;
                                break;
+                       case VIEWROT_MODAL_SWITCH_ZOOM:
+                               WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
+                               event_code= VIEW_CONFIRM;
+                               break;
+                       case VIEWROT_MODAL_SWITCH_ROTATE:
+                               WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
+                               event_code= VIEW_CONFIRM;
+                               break;
                }
        }
        else if(event->type==vod->origkey && event->val==KM_RELEASE) {
@@ -809,13 +857,23 @@ static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
        /* makes op->customdata */
        viewops_data_create(C, op, event);
 
-       /* add temp handler */
-       WM_event_add_modal_handler(C, op);
+       if (event->type == MOUSEPAN) {
+               ViewOpsData *vod= op->customdata;
+               viewmove_apply(vod, event->prevx, event->prevy);
+               request_depth_update(CTX_wm_region_view3d(C));
+               
+               viewops_data_free(C, op);               
+               
+               return OPERATOR_FINISHED;
+       }
+       else {
+               /* add temp handler */
+               WM_event_add_modal_handler(C, op);
 
-       return OPERATOR_RUNNING_MODAL;
+               return OPERATOR_RUNNING_MODAL;
+       }
 }
 
-
 void VIEW3D_OT_move(wmOperatorType *ot)
 {
 
@@ -854,6 +912,10 @@ void viewzoom_modal_keymap(wmKeyConfig *keyconf)
        WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
        WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
 
+       WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
+       WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
+       WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
+       
        /* assign map to operators */
        WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
 }
@@ -907,11 +969,11 @@ static void view_zoom_mouseloc(ARegion *ar, float dfac, int mx, int my)
 }
 
 
-static void viewzoom_apply(ViewOpsData *vod, int x, int y)
+static void viewzoom_apply(ViewOpsData *vod, int x, int y, short viewzoom)
 {
        float zfac=1.0;
 
-       if(U.viewzoom==USER_ZOOM_CONT) {
+       if(viewzoom==USER_ZOOM_CONT) {
                double time= PIL_check_seconds_timer();
                float time_step= (float)(time - vod->timer_lastdraw);
 
@@ -919,7 +981,7 @@ static void viewzoom_apply(ViewOpsData *vod, int x, int y)
                zfac = 1.0f + (((float)(vod->origx - x + vod->origy - y)/20.0) * time_step);
                vod->timer_lastdraw= time;
        }
-       else if(U.viewzoom==USER_ZOOM_SCALE) {
+       else if(viewzoom==USER_ZOOM_SCALE) {
                int ctr[2], len1, len2;
                // method which zooms based on how far you move the mouse
 
@@ -953,7 +1015,7 @@ static void viewzoom_apply(ViewOpsData *vod, int x, int y)
                view_zoom_mouseloc(vod->ar, zfac, vod->oldx, vod->oldy);
 
 
-       if ((U.uiflag & USER_ORBIT_ZBUF) && (U.viewzoom==USER_ZOOM_CONT) && (vod->rv3d->persp==RV3D_PERSP)) {
+       if ((U.uiflag & USER_ORBIT_ZBUF) && (viewzoom==USER_ZOOM_CONT) && (vod->rv3d->persp==RV3D_PERSP)) {
                float upvec[3], mat[3][3];
 
                /* Secret apricot feature, translate the view when in continues mode */
@@ -995,6 +1057,14 @@ static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
                        case VIEW_MODAL_CONFIRM:
                                event_code= VIEW_CONFIRM;
                                break;
+                       case VIEWROT_MODAL_SWITCH_MOVE:
+                               WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
+                               event_code= VIEW_CONFIRM;
+                               break;
+                       case VIEWROT_MODAL_SWITCH_ROTATE:
+                               WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
+                               event_code= VIEW_CONFIRM;
+                               break;
                }
        }
        else if(event->type==vod->origkey && event->val==KM_RELEASE) {
@@ -1002,7 +1072,7 @@ static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
        }
 
        if(event_code==VIEW_APPLY) {
-               viewzoom_apply(vod, event->x, event->y);
+               viewzoom_apply(vod, event->x, event->y, U.viewzoom);
        }
        else if (event_code==VIEW_CONFIRM) {
                request_depth_update(CTX_wm_region_view3d(C));
@@ -1073,13 +1143,34 @@ static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
 
                vod= op->customdata;
 
-               vod->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
-               vod->timer_lastdraw= PIL_check_seconds_timer();
+               if (event->type == MOUSEZOOM) {
+                       if (U.uiflag & USER_ZOOM_INVERT) /* Bypass Zoom invert flag */
+                               SWAP(int, event->x, event->prevx);
 
-               /* add temp handler */
-               WM_event_add_modal_handler(C, op);
+                       if (U.uiflag & USER_ZOOM_DOLLY_HORIZ) {
+                               vod->origx = vod->oldx = event->x;
+                               viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY);
+                       }
+                       else {
+                               
+                               /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
+                               vod->origy = vod->oldy = event->x;
+                               viewzoom_apply(vod, event->x, event->prevx, USER_ZOOM_DOLLY);
+                       }
+                       request_depth_update(CTX_wm_region_view3d(C));
+                       
+                       viewops_data_free(C, op);
+                       return OPERATOR_FINISHED;
+               }
+               else {
+                       vod->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
+                       vod->timer_lastdraw= PIL_check_seconds_timer();
 
-               return OPERATOR_RUNNING_MODAL;
+                       /* add temp handler */
+                       WM_event_add_modal_handler(C, op);
+
+                       return OPERATOR_RUNNING_MODAL;
+               }
        }
        return OPERATOR_FINISHED;
 }
@@ -2034,7 +2125,7 @@ static int view3d_clipping_exec(bContext *C, wmOperator *op)
        view3d_operator_needs_opengl(C);
 
        view3d_set_viewcontext(C, &vc);
-       view3d_get_transformation(vc.ar, vc.rv3d, vc.obact, &mats);
+       view3d_get_transformation(vc.ar, vc.rv3d, NULL, &mats); /* NULL because we don't want it in object space */
        view3d_calculate_clipping(rv3d->clipbb, rv3d->clip, &mats, &rect);
 
        return OPERATOR_FINISHED;