GSOC 2013 paint
[blender-staging.git] / source / blender / editors / transform / transform_constraints.c
index 5515ca69bba3aeb409f5e89f619d8abc9e230546..d8f17315c0103c7f6409435740b215bbb861972f 100644 (file)
 #include <string.h>
 #include <math.h>
 
-#ifndef WIN32
-#  include <unistd.h>
-#else
-#  include <io.h>
-#endif
-
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
@@ -135,9 +129,9 @@ static void postConstraintChecks(TransInfo *t, float vec[3], float pvec[3])
 
        mul_m3_v3(t->con.imtx, vec);
 
-       snapGrid(t, vec);
+       snapGridIncrement(t, vec);
 
-       if (t->num.flag & T_NULL_ONE) {
+       if (t->flag & T_NULL_ONE) {
                if (!(t->con.mode & CON_AXIS0))
                        vec[0] = 1.0f;
 
@@ -148,8 +142,7 @@ static void postConstraintChecks(TransInfo *t, float vec[3], float pvec[3])
                        vec[2] = 1.0f;
        }
 
-       if (hasNumInput(&t->num)) {
-               applyNumInput(&t->num, vec);
+       if (applyNumInput(&t->num, vec)) {
                constraintNumInput(t, vec);
                removeAspectRatio(t, vec);
        }
@@ -393,7 +386,11 @@ static void applyObjectConstraintVec(TransInfo *t, TransData *td, const float in
                        if (t->con.mode & CON_AXIS2) {
                                out[2] = in[i++];
                        }
+
                        mul_m3_v3(td->axismtx, out);
+                       if (t->flag & T_EDIT) {
+                               mul_m3_v3(t->obedit_mat, out);
+                       }
                }
        }
 }
@@ -445,6 +442,9 @@ static void applyObjectConstraintSize(TransInfo *t, TransData *td, float smat[3]
                }
 
                mul_m3_m3m3(tmat, smat, imat);
+               if (t->flag & T_EDIT) {
+                       mul_m3_m3m3(smat, t->obedit_mat, smat);
+               }
                mul_m3_m3m3(smat, td->axismtx, tmat);
        }
 }
@@ -509,24 +509,34 @@ static void applyObjectConstraintRot(TransInfo *t, TransData *td, float vec[3],
 {
        if (t->con.mode & CON_APPLY) {
                int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
+               float tmp_axismtx[3][3];
+               float (*axismtx)[3];
 
                /* on setup call, use first object */
                if (td == NULL) {
                        td = t->data;
                }
 
+               if (t->flag & T_EDIT) {
+                       mul_m3_m3m3(tmp_axismtx, t->obedit_mat, td->axismtx);
+                       axismtx = tmp_axismtx;
+               }
+               else {
+                       axismtx = td->axismtx;
+               }
+
                switch (mode) {
                        case CON_AXIS0:
                        case (CON_AXIS1 | CON_AXIS2):
-                               copy_v3_v3(vec, td->axismtx[0]);
+                               copy_v3_v3(vec, axismtx[0]);
                                break;
                        case CON_AXIS1:
                        case (CON_AXIS0 | CON_AXIS2):
-                               copy_v3_v3(vec, td->axismtx[1]);
+                               copy_v3_v3(vec, axismtx[1]);
                                break;
                        case CON_AXIS2:
                        case (CON_AXIS0 | CON_AXIS1):
-                               copy_v3_v3(vec, td->axismtx[2]);
+                               copy_v3_v3(vec, axismtx[2]);
                                break;
                }
                if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
@@ -552,39 +562,47 @@ void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[])
        t->con.applyVec = applyAxisConstraintVec;
        t->con.applySize = applyAxisConstraintSize;
        t->con.applyRot = applyAxisConstraintRot;
-       t->redraw = 1;
+       t->redraw = TREDRAW_HARD;
 }
 
-void setLocalConstraint(TransInfo *t, int mode, const char text[])
+/* applies individual td->axismtx constraints */
+void setAxisMatrixConstraint(TransInfo *t, int mode, const char text[])
 {
-       /* edit-mode now allows local transforms too */
-#if 1
-       if ((t->flag & T_EDIT) && (t->around != V3D_LOCAL)) {
-               float obmat[3][3];
-               copy_m3_m4(obmat, t->scene->obedit->obmat);
-               normalize_m3(obmat);
-               setConstraint(t, obmat, mode, text);
-       }
-       else
-#endif
-       {
-               if (t->total == 1) {
-                       setConstraint(t, t->data->axismtx, mode, text);
+       if (t->total == 1) {
+               float axismtx[3][3];
+               if (t->flag & T_EDIT) {
+                       mul_m3_m3m3(axismtx, t->obedit_mat, t->data->axismtx);
                }
                else {
-                       BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
-                       copy_m3_m3(t->con.mtx, t->data->axismtx);
-                       t->con.mode = mode;
-                       getConstraintMatrix(t);
-
-                       startConstraint(t);
-
-                       t->con.drawExtra = drawObjectConstraint;
-                       t->con.applyVec = applyObjectConstraintVec;
-                       t->con.applySize = applyObjectConstraintSize;
-                       t->con.applyRot = applyObjectConstraintRot;
-                       t->redraw = 1;
+                       copy_m3_m3(axismtx, t->data->axismtx);
                }
+
+               setConstraint(t, axismtx, mode, text);
+       }
+       else {
+               BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
+               copy_m3_m3(t->con.mtx, t->data->axismtx);
+               t->con.mode = mode;
+               getConstraintMatrix(t);
+
+               startConstraint(t);
+
+               t->con.drawExtra = drawObjectConstraint;
+               t->con.applyVec = applyObjectConstraintVec;
+               t->con.applySize = applyObjectConstraintSize;
+               t->con.applyRot = applyObjectConstraintRot;
+               t->redraw = TREDRAW_HARD;
+       }
+}
+
+void setLocalConstraint(TransInfo *t, int mode, const char text[])
+{
+       /* edit-mode now allows local transforms too */
+       if (t->flag & T_EDIT) {
+               setConstraint(t, t->obedit_mat, mode, text);
+       }
+       else {
+               setAxisMatrixConstraint(t, mode, text);
        }
 }
 
@@ -601,18 +619,24 @@ void setUserConstraint(TransInfo *t, short orientation, int mode, const char fte
        switch (orientation) {
                case V3D_MANIP_GLOBAL:
                {
-                       float mtx[3][3] = MAT3_UNITY;
+                       float mtx[3][3];
                        BLI_snprintf(text, sizeof(text), ftext, IFACE_("global"));
+                       unit_m3(mtx);
                        setConstraint(t, mtx, mode, text);
+                       break;
                }
-               break;
                case V3D_MANIP_LOCAL:
                        BLI_snprintf(text, sizeof(text), ftext, IFACE_("local"));
                        setLocalConstraint(t, mode, text);
                        break;
                case V3D_MANIP_NORMAL:
                        BLI_snprintf(text, sizeof(text), ftext, IFACE_("normal"));
-                       setConstraint(t, t->spacemtx, mode, text);
+                       if (checkUseAxisMatrix(t)) {
+                               setAxisMatrixConstraint(t, mode, text);
+                       }
+                       else {
+                               setConstraint(t, t->spacemtx, mode, text);
+                       }
                        break;
                case V3D_MANIP_VIEW:
                        BLI_snprintf(text, sizeof(text), ftext, IFACE_("view"));
@@ -639,7 +663,7 @@ void drawConstraint(TransInfo *t)
 {
        TransCon *tc = &(t->con);
 
-       if (!ELEM3(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE))
+       if (!ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE))
                return;
        if (!(tc->mode & CON_APPLY))
                return;
@@ -705,6 +729,7 @@ void drawPropCircle(const struct bContext *C, TransInfo *t)
                RegionView3D *rv3d = CTX_wm_region_view3d(C);
                float tmat[4][4], imat[4][4];
                float center[3];
+               int depth_test_enabled;
 
                UI_ThemeColor(TH_GRID);
 
@@ -731,16 +756,26 @@ void drawPropCircle(const struct bContext *C, TransInfo *t)
                                /* untested - mask aspect is TODO */
                                ED_space_image_get_aspect(t->sa->spacedata.first, &aspx, &aspy);
                        }
+                       else if (t->options & CTX_PAINT_CURVE) {
+                               aspx = aspy = 1.0;
+                       }
                        else {
                                ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
                        }
                        glScalef(1.0f / aspx, 1.0f / aspy, 1.0);
                }
 
+               depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
+               if (depth_test_enabled)
+                       glDisable(GL_DEPTH_TEST);
+
                set_inverted_drawing(1);
                drawcircball(GL_LINE_LOOP, center, t->prop_size, imat);
                set_inverted_drawing(0);
 
+               if (depth_test_enabled)
+                       glEnable(GL_DEPTH_TEST);
+
                glPopMatrix();
        }
 }
@@ -756,31 +791,46 @@ static void drawObjectConstraint(TransInfo *t)
        short options = DRAWLIGHT;
        TransData *td = t->data;
        int i;
+       float tmp_axismtx[3][3];
 
        for (i = 0; i < t->total; i++, td++) {
                float co[3];
+               float (*axismtx)[3];
+
+               if (t->flag & T_PROP_EDIT) {
+                       /* we're sorted, so skip the rest */
+                       if (td->factor == 0.0f) {
+                               break;
+                       }
+               }
 
                if (t->flag & T_OBJECT) {
                        copy_v3_v3(co, td->ob->obmat[3]);
+                       axismtx = td->axismtx;
                }
                else if (t->flag & T_EDIT) {
                        mul_v3_m4v3(co, t->obedit->obmat, td->center);
+
+                       mul_m3_m3m3(tmp_axismtx, t->obedit_mat, td->axismtx);
+                       axismtx = tmp_axismtx;
                }
                else if (t->flag & T_POSE) {
                        mul_v3_m4v3(co, t->poseobj->obmat, td->center);
+                       axismtx = td->axismtx;
                }
                else {
                        copy_v3_v3(co, td->center);
+                       axismtx = td->axismtx;
                }
 
                if (t->con.mode & CON_AXIS0) {
-                       drawLine(t, td->center, td->axismtx[0], 'X', options);
+                       drawLine(t, co, axismtx[0], 'X', options);
                }
                if (t->con.mode & CON_AXIS1) {
-                       drawLine(t, td->center, td->axismtx[1], 'Y', options);
+                       drawLine(t, co, axismtx[1], 'Y', options);
                }
                if (t->con.mode & CON_AXIS2) {
-                       drawLine(t, td->center, td->axismtx[2], 'Z', options);
+                       drawLine(t, co, axismtx[2], 'Z', options);
                }
                options &= ~DRAWLIGHT;
        }
@@ -866,13 +916,13 @@ void postSelectConstraint(TransInfo *t)
        setNearestAxis(t);
 
        startConstraint(t);
-       t->redraw = 1;
+       t->redraw = TREDRAW_HARD;
 }
 
 static void setNearestAxis2d(TransInfo *t)
 {
        /* no correction needed... just use whichever one is lower */
-       if (abs(t->mval[0] - t->con.imval[0]) < abs(t->mval[1] - t->con.imval[1]) ) {
+       if (abs(t->mval[0] - t->con.imval[0]) < abs(t->mval[1] - t->con.imval[1])) {
                t->con.mode |= CON_AXIS1;
                BLI_strncpy(t->con.text, IFACE_(" along Y axis"), sizeof(t->con.text));
        }
@@ -885,9 +935,9 @@ static void setNearestAxis2d(TransInfo *t)
 static void setNearestAxis3d(TransInfo *t)
 {
        float zfac;
-       float mvec[3], axis[3], proj[3];
+       float mvec[3], proj[3];
        float len[3];
-       int i, icoord[2];
+       int i;
 
        /* calculate mouse movement */
        mvec[0] = (float)(t->mval[0] - t->con.imval[0]);
@@ -905,24 +955,25 @@ static void setNearestAxis3d(TransInfo *t)
        zfac = len_v3(t->persinv[0]) * 2.0f / t->ar->winx * zfac * 30.0f;
 
        for (i = 0; i < 3; i++) {
+               float axis[3], axis_2d[2];
+
                copy_v3_v3(axis, t->con.mtx[i]);
 
                mul_v3_fl(axis, zfac);
                /* now we can project to get window coordinate */
                add_v3_v3(axis, t->con.center);
-               projectIntView(t, axis, icoord);
+               projectFloatView(t, axis, axis_2d);
 
-               axis[0] = (float)(icoord[0] - t->center2d[0]);
-               axis[1] = (float)(icoord[1] - t->center2d[1]);
+               sub_v2_v2v2(axis, axis_2d, t->center2d);
                axis[2] = 0.0f;
 
-               if (normalize_v3(axis) != 0.0f) {
+               if (normalize_v3(axis) > 1e-3f) {
                        project_v3_v3v3(proj, mvec, axis);
                        sub_v3_v3v3(axis, mvec, proj);
                        len[i] = normalize_v3(axis);
                }
                else {
-                       len[i] = 10000000000.0f;
+                       len[i] = 1e10f;
                }
        }