remove unused args in draw*.c and some in view*.c, tag some as UNUSED().
[blender.git] / source / blender / editors / space_view3d / view3d_edit.c
index 2e26988877fb16b9cafe9702b6536968a29ff00b..9f1b55ee2ce511b434a545c58137cb3fcce5f2d1 100644 (file)
 #include "BLI_math.h"
 #include "BLI_rand.h"
 
-#include "BKE_action.h"
 #include "BKE_context.h"
-#include "BKE_depsgraph.h"
+#include "BKE_image.h"
+#include "BKE_library.h"
 #include "BKE_object.h"
-#include "BKE_global.h"
 #include "BKE_paint.h"
 #include "BKE_report.h"
 #include "BKE_scene.h"
-#include "BKE_screen.h"
-#include "BKE_utildefines.h"
 
 
 #include "BIF_gl.h"
@@ -786,7 +783,7 @@ static int view3d_camera_active_poll(bContext *C)
 
 static int view3d_rotate_poll(bContext *C)
 {
-       if (!ED_operator_view3d_active(C)) {
+       if (!ED_operator_region_view3d_active(C)) {
                return 0;
        } else {
                RegionView3D *rv3d= CTX_wm_region_view3d(C);
@@ -857,10 +854,11 @@ void viewmove_modal_keymap(wmKeyConfig *keyconf)
 static void viewmove_apply(ViewOpsData *vod, int x, int y)
 {
        if(vod->rv3d->persp==RV3D_CAMOB) {
-               float max= (float)MAX2(vod->ar->winx, vod->ar->winy);
+               float zoomfac= (M_SQRT2 + vod->rv3d->camzoom/50.0);
+               zoomfac= (zoomfac*zoomfac)*0.5;
 
-               vod->rv3d->camdx += (vod->oldx - x)/(max);
-               vod->rv3d->camdy += (vod->oldy - y)/(max);
+               vod->rv3d->camdx += (vod->oldx - x)/(vod->ar->winx * zoomfac);
+               vod->rv3d->camdy += (vod->oldy - y)/(vod->ar->winy * zoomfac);
                CLAMP(vod->rv3d->camdx, -1.0f, 1.0f);
                CLAMP(vod->rv3d->camdy, -1.0f, 1.0f);
 // XXX         preview3d_event= 0;
@@ -1168,7 +1166,7 @@ static int viewzoom_exec(bContext *C, wmOperator *op)
                /* this min and max is also in viewmove() */
                if(rv3d->persp==RV3D_CAMOB) {
                        rv3d->camzoom-= 10;
-                       if(rv3d->camzoom<-30) rv3d->camzoom= -30;
+                       if(rv3d->camzoom < RV3D_CAMZOOM_MIN) rv3d->camzoom= RV3D_CAMZOOM_MIN;
                }
                else if(rv3d->dist<10.0*v3d->far) {
                        view_zoom_mouseloc(CTX_wm_region(C), 1.2f, mx, my);
@@ -1177,7 +1175,7 @@ static int viewzoom_exec(bContext *C, wmOperator *op)
        else {
                if(rv3d->persp==RV3D_CAMOB) {
                        rv3d->camzoom+= 10;
-                       if(rv3d->camzoom>600) rv3d->camzoom= 600;
+                       if(rv3d->camzoom > RV3D_CAMZOOM_MAX) rv3d->camzoom= RV3D_CAMZOOM_MAX;
                }
                else if(rv3d->dist> 0.001*v3d->grid) {
                        view_zoom_mouseloc(CTX_wm_region(C), .83333f, mx, my);
@@ -1266,7 +1264,7 @@ void VIEW3D_OT_zoom(wmOperatorType *ot)
        ot->invoke= viewzoom_invoke;
        ot->exec= viewzoom_exec;
        ot->modal= viewzoom_modal;
-       ot->poll= ED_operator_view3d_active;
+       ot->poll= ED_operator_region_view3d_active;
 
        /* flags */
        ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
@@ -1276,7 +1274,7 @@ void VIEW3D_OT_zoom(wmOperatorType *ot)
        RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
 }
 
-static int viewhome_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
+static int view3d_all_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
 {
        ARegion *ar= CTX_wm_region(C);
        View3D *v3d = CTX_wm_view3d(C);
@@ -1291,12 +1289,11 @@ static int viewhome_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.
        int ok= 1, onedone=0;
 
        if(center) {
-               min[0]= min[1]= min[2]= 0.0f;
-               max[0]= max[1]= max[2]= 0.0f;
-
                /* in 2.4x this also move the cursor to (0, 0, 0) (with shift+c). */
                curs= give_cursor(scene, v3d);
-               curs[0]= curs[1]= curs[2]= 0.0;
+               zero_v3(min);
+               zero_v3(max);
+               zero_v3(curs);
        }
        else {
                INIT_MINMAX(min, max);
@@ -1320,9 +1317,7 @@ static int viewhome_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.
                return OPERATOR_FINISHED;
        }
 
-       afm[0]= (max[0]-min[0]);
-       afm[1]= (max[1]-min[1]);
-       afm[2]= (max[2]-min[2]);
+       sub_v3_v3v3(afm, max, min);
        size= 0.7f*MAX3(afm[0], afm[1], afm[2]);
        if(size==0.0) ok= 0;
 
@@ -1344,7 +1339,7 @@ static int viewhome_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.
 
                if (rv3d->persp==RV3D_CAMOB) {
                        rv3d->persp= RV3D_PERSP;
-                       smooth_view(C, NULL, v3d->camera, new_ofs, NULL, &new_dist, NULL);
+                       smooth_view(C, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
                }
                else {
                        smooth_view(C, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
@@ -1360,17 +1355,6 @@ static int viewhome_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.
        return OPERATOR_FINISHED;
 }
 
-static int viewhome_poll(bContext *C)
-{
-       if(ED_operator_view3d_active(C)) {
-               RegionView3D *rv3d= CTX_wm_region_view3d(C); //XXX, when accessed from a header menu this doesnt work!
-               if(rv3d && rv3d->persp!=RV3D_CAMOB) {
-                       return 1;
-               }
-       }
-
-       return 0;
-}
 
 void VIEW3D_OT_view_all(wmOperatorType *ot)
 {
@@ -1380,8 +1364,8 @@ void VIEW3D_OT_view_all(wmOperatorType *ot)
        ot->idname= "VIEW3D_OT_view_all";
 
        /* api callbacks */
-       ot->exec= viewhome_exec;
-       ot->poll= viewhome_poll;
+       ot->exec= view3d_all_exec;
+       ot->poll= ED_operator_view3d_active;
 
        /* flags */
        ot->flag= 0;
@@ -1524,7 +1508,7 @@ void VIEW3D_OT_view_selected(wmOperatorType *ot)
 
        /* api callbacks */
        ot->exec= viewselected_exec;
-       ot->poll= ED_operator_view3d_active;
+       ot->poll= ED_operator_region_view3d_active;
 
        /* flags */
        ot->flag= 0;
@@ -1609,7 +1593,7 @@ static int render_border_exec(bContext *C, wmOperator *op)
        rect.ymax= RNA_int_get(op->ptr, "ymax");
 
        /* calculate range */
-       calc_viewborder(scene, ar, rv3d, v3d, &vb);
+       view3d_calc_camera_border(scene, ar, rv3d, v3d, &vb);
 
        scene->r.border.xmin= ((float)rect.xmin-vb.xmin)/(vb.xmax-vb.xmin);
        scene->r.border.ymin= ((float)rect.ymin-vb.ymin)/(vb.ymax-vb.ymin);
@@ -1813,7 +1797,6 @@ static int view3d_zoom_border_invoke(bContext *C, wmOperator *op, wmEvent *event
 
 void VIEW3D_OT_zoom_border(wmOperatorType *ot)
 {
-
        /* identifiers */
        ot->name= "Border Zoom";
        ot->description = "Zoom in the view to the nearest object contained in the border";
@@ -1824,7 +1807,7 @@ void VIEW3D_OT_zoom_border(wmOperatorType *ot)
        ot->exec= view3d_zoom_border_exec;
        ot->modal= WM_border_select_modal;
 
-       ot->poll= ED_operator_view3d_active;
+       ot->poll= ED_operator_region_view3d_active;
 
        /* flags */
        ot->flag= 0;
@@ -1905,14 +1888,14 @@ static void axis_set_view(bContext *C, float q1, float q2, float q3, float q4, s
 
        if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
 
-               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= RV3D_ORTHO;
+               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
                else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
 
                smooth_view(C, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
        }
        else {
 
-               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= RV3D_ORTHO;
+               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
                else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
 
                smooth_view(C, NULL, NULL, NULL, new_quat, NULL, NULL);
@@ -2046,7 +2029,7 @@ void VIEW3D_OT_viewnumpad(wmOperatorType *ot)
 
        /* api callbacks */
        ot->exec= viewnumpad_exec;
-       ot->poll= ED_operator_view3d_active;
+       ot->poll= ED_operator_region_view3d_active;
 
        /* flags */
        ot->flag= 0;
@@ -2138,14 +2121,11 @@ static int viewpan_exec(bContext *C, wmOperator *op)
        pandir = RNA_enum_get(op->ptr, "type");
 
        initgrabz(rv3d, 0.0, 0.0, 0.0);
-
        if(pandir == V3D_VIEW_PANRIGHT) window_to_3d_delta(ar, vec, -32, 0);
        else if(pandir == V3D_VIEW_PANLEFT) window_to_3d_delta(ar, vec, 32, 0);
        else if(pandir == V3D_VIEW_PANUP) window_to_3d_delta(ar, vec, 0, -25);
        else if(pandir == V3D_VIEW_PANDOWN) window_to_3d_delta(ar, vec, 0, 25);
-       rv3d->ofs[0]+= vec[0];
-       rv3d->ofs[1]+= vec[1];
-       rv3d->ofs[2]+= vec[2];
+       add_v3_v3(rv3d->ofs, vec);
 
        if(rv3d->viewlock & RV3D_BOXVIEW)
                view3d_boxview_sync(CTX_wm_area(C), ar);
@@ -2164,7 +2144,7 @@ void VIEW3D_OT_view_pan(wmOperatorType *ot)
 
        /* api callbacks */
        ot->exec= viewpan_exec;
-       ot->poll= ED_operator_view3d_active;
+       ot->poll= ED_operator_region_view3d_active;
 
        /* flags */
        ot->flag= 0;
@@ -2196,7 +2176,7 @@ void VIEW3D_OT_view_persportho(wmOperatorType *ot)
 
        /* api callbacks */
        ot->exec= viewpersportho_exec;
-       ot->poll= ED_operator_view3d_active;
+       ot->poll= ED_operator_region_view3d_active;
 
        /* flags */
        ot->flag= 0;
@@ -2204,27 +2184,64 @@ void VIEW3D_OT_view_persportho(wmOperatorType *ot)
 
 /* ******************** add background image operator **************** */
 
-static int add_background_image_exec(bContext *C, wmOperator *op)
+static BGpic *add_background_image(bContext *C)
 {
        View3D *v3d= CTX_wm_view3d(C);
-
+       
        BGpic *bgpic= MEM_callocN(sizeof(BGpic), "Background Image");
        bgpic->size= 5.0;
        bgpic->blend= 0.5;
        bgpic->iuser.fie_ima= 2;
        bgpic->iuser.ok= 1;
        bgpic->view= 0; /* 0 for all */
-
+       
        BLI_addtail(&v3d->bgpicbase, bgpic);
+       
+       return bgpic;
+}
 
-       //ED_region_tag_redraw(v3d);
+static int add_background_image_exec(bContext *C, wmOperator *op)
+{
+       add_background_image(C);
 
        return OPERATOR_FINISHED;
 }
 
 static int add_background_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       return add_background_image_exec(C, op);
+       Scene *scene= CTX_data_scene(C);
+       View3D *v3d= CTX_wm_view3d(C);
+       Image *ima= NULL;
+       BGpic *bgpic;
+       char name[32];
+       
+       /* check input variables */
+       if(RNA_property_is_set(op->ptr, "filepath")) {
+               char path[FILE_MAX];
+               
+               RNA_string_get(op->ptr, "filepath", path);
+               ima= BKE_add_image_file(path, scene ? scene->r.cfra : 1);
+       }
+       else if(RNA_property_is_set(op->ptr, "name")) {
+               RNA_string_get(op->ptr, "name", name);
+               ima= (Image *)find_id("IM", name);
+       }
+       
+       bgpic = add_background_image(C);
+       
+       if (ima) {
+               bgpic->ima = ima;
+               
+               if(ima->id.us==0) id_us_plus(&ima->id);
+               else id_lib_extern(&ima->id);
+               
+               if (!(v3d->flag & V3D_DISPBGPICS))
+                       v3d->flag |= V3D_DISPBGPICS;
+       }
+       
+       WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
+       
+       return OPERATOR_FINISHED;
 }
 
 void VIEW3D_OT_add_background_image(wmOperatorType *ot)
@@ -2241,8 +2258,13 @@ void VIEW3D_OT_add_background_image(wmOperatorType *ot)
 
        /* flags */
        ot->flag   = 0;
+       
+       /* properties */
+       RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Image name to assign.");
+       RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
 }
 
+
 /* ***** remove image operator ******* */
 static int remove_background_image_exec(bContext *C, wmOperator *op)
 {
@@ -2278,6 +2300,7 @@ void VIEW3D_OT_remove_background_image(wmOperatorType *ot)
 
        RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Background image index to remove ", 0, INT_MAX);
 }
+
 /* ********************* set clipping operator ****************** */
 
 static void calc_clipping_plane(float clip[6][4], BoundBox *clipbb)
@@ -2372,7 +2395,7 @@ void VIEW3D_OT_clip_border(wmOperatorType *ot)
        ot->exec= view3d_clipping_exec;
        ot->modal= WM_border_select_modal;
 
-       ot->poll= ED_operator_view3d_active;
+       ot->poll= ED_operator_region_view3d_active;
 
        /* flags */
        ot->flag= 0;
@@ -2396,7 +2419,7 @@ static int set_3dcursor_invoke(bContext *C, wmOperator *op, wmEvent *event)
        float dx, dy, fz, *fp = NULL, dvec[3], oldcurs[3];
        short mx, my, mval[2];
 //     short ctrl= 0; // XXX
-
+       int flip;
        fp= give_cursor(scene, v3d);
 
 //     if(obedit && ctrl) lr_click= 1;
@@ -2404,9 +2427,18 @@ static int set_3dcursor_invoke(bContext *C, wmOperator *op, wmEvent *event)
 
        mx= event->x - ar->winrct.xmin;
        my= event->y - ar->winrct.ymin;
+
        project_short_noclip(ar, fp, mval);
+       flip= initgrabz(rv3d, fp[0], fp[1], fp[2]);
+       
+       /* reset the depth based on the view offset */
+       if(flip) {
+               negate_v3_v3(fp, rv3d->ofs);
 
-       initgrabz(rv3d, fp[0], fp[1], fp[2]);
+               /* re initialize */
+               project_short_noclip(ar, fp, mval);
+               flip= initgrabz(rv3d, fp[0], fp[1], fp[2]);
+       }
 
        if(mval[0]!=IS_CLIPPED) {
                short depth_used = 0;