2.5/Posemode:
[blender-staging.git] / source / blender / editors / sculpt_paint / paint_vertex.c
index 1623c7feef1619147a732b0d819e05d1964f89d5..bbc68a6bd0a856417ed11295b6b4b554c2d99bc1 100644 (file)
@@ -64,6 +64,7 @@
 #include "RNA_access.h"
 
 #include "BKE_armature.h"
+#include "BKE_brush.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_cloth.h"
 #include "BKE_context.h"
@@ -74,7 +75,6 @@
 #include "BKE_global.h"
 #include "BKE_mesh.h"
 #include "BKE_modifier.h"
-#include "BKE_multires.h"
 #include "BKE_object.h"
 #include "BKE_utildefines.h"
 
@@ -108,7 +108,9 @@ static void error() {}
 
 static int vp_poll(bContext *C)
 {
-       if(G.f & G_VERTEXPAINT) {
+       Object *ob = CTX_data_active_object(C);
+
+       if(ob && ob->mode & OB_MODE_VERTEX_PAINT) {
                ScrArea *sa= CTX_wm_area(C);
                if(sa->spacetype==SPACE_VIEW3D) {
                        ARegion *ar= CTX_wm_region(C);
@@ -121,7 +123,9 @@ static int vp_poll(bContext *C)
 
 static int wp_poll(bContext *C)
 {
-       if(G.f & G_WEIGHTPAINT) {
+       Object *ob = CTX_data_active_object(C);
+
+       if(ob && ob->mode & OB_MODE_WEIGHT_PAINT) {
                ScrArea *sa= CTX_wm_area(C);
                if(sa->spacetype==SPACE_VIEW3D) {
                        ARegion *ar= CTX_wm_region(C);
@@ -143,7 +147,7 @@ static void vp_drawcursor(bContext *C, int x, int y, void *customdata)
        glColor4ub(255, 255, 255, 128);
        glEnable( GL_LINE_SMOOTH );
        glEnable(GL_BLEND);
-       glutil_draw_lined_arc(0.0, M_PI*2.0, ts->vpaint->size, 40);
+       glutil_draw_lined_arc(0.0, M_PI*2.0, ts->vpaint->brush->size, 40);
        glDisable(GL_BLEND);
        glDisable( GL_LINE_SMOOTH );
        
@@ -159,7 +163,7 @@ static void wp_drawcursor(bContext *C, int x, int y, void *customdata)
        glColor4ub(200, 200, 255, 128);
        glEnable( GL_LINE_SMOOTH );
        glEnable(GL_BLEND);
-       glutil_draw_lined_arc(0.0, M_PI*2.0, ts->wpaint->size, 40);
+       glutil_draw_lined_arc(0.0, M_PI*2.0, ts->wpaint->brush->size, 40);
        glDisable(GL_BLEND);
        glDisable( GL_LINE_SMOOTH );
        
@@ -186,20 +190,13 @@ static VPaint *new_vpaint(int wpaint)
 {
        VPaint *vp= MEM_callocN(sizeof(VPaint), "VPaint");
        
-       vp->r= 1.0f;
-       vp->g= 1.0f;
-       vp->b= 1.0f;
-       vp->a= 0.2f;
-       vp->size= 25.0f;
        vp->gamma= vp->mul= 1.0f;
        
        vp->flag= VP_AREA+VP_SOFT+VP_SPRAY;
        
-       if(wpaint) {
-               vp->weight= 1.0f;
-               vp->a= 1.0f;
+       if(wpaint)
                vp->flag= VP_AREA+VP_SOFT;
-       }
+
        return vp;
 }
 
@@ -239,7 +236,7 @@ unsigned int rgba_to_mcol(float r, float g, float b, float a)
 
 static unsigned int vpaint_get_current_col(VPaint *vp)
 {
-       return rgba_to_mcol(vp->r, vp->g, vp->b, 1.0f);
+       return rgba_to_mcol(vp->brush->rgb[0], vp->brush->rgb[1], vp->brush->rgb[2], 1.0f);
 }
 
 void do_shared_vertexcol(Mesh *me)
@@ -334,8 +331,6 @@ void make_vertexcol(Scene *scene, int shade)        /* single ob */
        else
                memset(me->mcol, 255, 4*sizeof(MCol)*me->totface);
        
-// XXX if (me->mr) multires_load_cols(me);
-       
        DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
        
 }
@@ -378,12 +373,12 @@ void clear_vpaint(Scene *scene)
        unsigned int *to, paintcol;
        int a;
        
-       if((G.f & G_VERTEXPAINT)==0) return;
-
        ob= OBACT;
        me= get_mesh(ob);
        if(!ob || ob->id.lib) return;
 
+       if(!(ob->mode & OB_MODE_VERTEX_PAINT)) return;
+
        if(me==0 || me->mcol==0 || me->totface==0) return;
 
        paintcol= vpaint_get_current_col(scene->toolsettings->vpaint);
@@ -433,8 +428,9 @@ void clear_vpaint_selectedfaces(Scene *scene)
 /* fills in the selected faces with the current weight and vertex group */
 void clear_wpaint_selectedfaces(Scene *scene)
 {
-       VPaint *wp= scene->toolsettings->wpaint;
-       float paintweight= wp->weight;
+       ToolSettings *ts= scene->toolsettings;
+       VPaint *wp= ts->wpaint;
+       float paintweight= ts->vgroup_weight;
        Mesh *me;
        MFace *mface;
        Object *ob;
@@ -548,10 +544,10 @@ void vpaint_dogamma(Scene *scene)
        int a, temp;
        unsigned char *cp, gamtab[256];
 
-       if((G.f & G_VERTEXPAINT)==0) return;
-
        ob= OBACT;
        me= get_mesh(ob);
+
+       if(!(ob->mode & OB_MODE_VERTEX_PAINT)) return;
        if(me==0 || me->mcol==0 || me->totface==0) return;
 
        igam= 1.0/vp->gamma;
@@ -742,7 +738,7 @@ static void vpaint_blend(VPaint *vp, unsigned int *col, unsigned int *colorig, u
                unsigned int testcol=0, a;
                char *cp, *ct, *co;
                
-               alpha= (int)(255.0*vp->a);
+               alpha= (int)(255.0*vp->brush->alpha);
                
                if(vp->mode==VP_MIX || vp->mode==VP_BLUR) testcol= mcol_blend( *colorig, paintcol, alpha);
                else if(vp->mode==VP_ADD) testcol= mcol_add( *colorig, paintcol, alpha);
@@ -818,14 +814,14 @@ static int calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], floa
                dy= mval[1]-vertco[1];
                
                fac= sqrt(dx*dx + dy*dy);
-               if(fac > vp->size) return 0;
+               if(fac > vp->brush->size) return 0;
                if(vp->flag & VP_HARD)
                        alpha= 255;
                else
-                       alpha= 255.0*vp->a*(1.0-fac/vp->size);
+                       alpha= 255.0*vp->brush->alpha*(1.0-fac/vp->brush->size);
        }
        else {
-               alpha= 255.0*vp->a;
+               alpha= 255.0*vp->brush->alpha;
        }
 
        if(vp->flag & VP_NORMALS) {
@@ -872,7 +868,7 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
        if((wp->flag & VP_SPRAY)==0) {
                float testw=0.0f;
                
-               alpha= wp->a;
+               alpha= wp->brush->alpha;
                if(wp->mode==VP_MIX || wp->mode==VP_BLUR)
                        testw = paintval*alpha + uw->weight*(1.0-alpha);
                else if(wp->mode==VP_ADD)
@@ -918,7 +914,7 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
 void sample_wpaint(Scene *scene, ARegion *ar, View3D *v3d, int mode)
 {
        ViewContext vc;
-       VPaint *wp= scene->toolsettings->wpaint;
+       ToolSettings *ts= scene->toolsettings;
        Object *ob= OBACT;
        Mesh *me= get_mesh(ob);
        int index;
@@ -1028,20 +1024,20 @@ void sample_wpaint(Scene *scene, ARegion *ar, View3D *v3d, int mode)
                                fac= MIN4(w1, w2, w3, w4);
                                if(w1==fac) {
                                        dw= get_defweight(me->dvert+mface->v1, ob->actdef-1);
-                                       if(dw) wp->weight= dw->weight; else wp->weight= 0.0f;
+                                       if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
                                }
                                else if(w2==fac) {
                                        dw= get_defweight(me->dvert+mface->v2, ob->actdef-1);
-                                       if(dw) wp->weight= dw->weight; else wp->weight= 0.0f;
+                                       if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
                                }
                                else if(w3==fac) {
                                        dw= get_defweight(me->dvert+mface->v3, ob->actdef-1);
-                                       if(dw) wp->weight= dw->weight; else wp->weight= 0.0f;
+                                       if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
                                }
                                else if(w4==fac) {
                                        if(mface->v4) {
                                                dw= get_defweight(me->dvert+mface->v4, ob->actdef-1);
-                                               if(dw) wp->weight= dw->weight; else wp->weight= 0.0f;
+                                               if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
                                        }
                                }
                        }
@@ -1096,16 +1092,16 @@ static int set_wpaint(bContext *C, wmOperator *op)              /* toggle */
        Mesh *me;
        
        me= get_mesh(ob);
-       if(ob->id.lib || me==NULL) return OPERATOR_CANCELLED;
+       if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
        
        if(me && me->totface>=MAXINDEX) {
                error("Maximum number of faces: %d", MAXINDEX-1);
-               G.f &= ~G_WEIGHTPAINT;
+               ob->mode &= ~OB_MODE_WEIGHT_PAINT;
                return OPERATOR_CANCELLED;
        }
        
-       if(G.f & G_WEIGHTPAINT) G.f &= ~G_WEIGHTPAINT;
-       else G.f |= G_WEIGHTPAINT;
+       if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
+       else ob->mode |= OB_MODE_WEIGHT_PAINT;
        
        
        /* Weightpaint works by overriding colors in mesh,
@@ -1115,11 +1111,13 @@ static int set_wpaint(bContext *C, wmOperator *op)              /* toggle */
                */
        DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
        
-       if(G.f & G_WEIGHTPAINT) {
+       if(ob->mode & OB_MODE_WEIGHT_PAINT) {
                Object *par;
                
                if(wp==NULL)
                        wp= scene->toolsettings->wpaint= new_vpaint(1);
+
+               brush_check_exists(&wp->brush);
                
                toggle_paint_cursor(C, 1);
                
@@ -1127,7 +1125,7 @@ static int set_wpaint(bContext *C, wmOperator *op)                /* toggle */
                
                /* verify if active weight group is also active bone */
                par= modifiers_isDeformedByArmature(ob);
-               if(par && (par->flag & OB_POSEMODE)) {
+               if(par && (par->mode & OB_MODE_POSE)) {
                        bPoseChannel *pchan;
                        for(pchan= par->pose->chanbase.first; pchan; pchan= pchan->next)
                                if(pchan->bone->flag & BONE_ACTIVE)
@@ -1178,36 +1176,10 @@ void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
 
 /* ************ paint radial controls *************/
 
-void paint_radial_control_invoke(wmOperator *op, VPaint *vp)
-{
-       int mode = RNA_int_get(op->ptr, "mode");
-       float original_value;
-
-       if(mode == WM_RADIALCONTROL_SIZE)
-               original_value = vp->size;
-       else if(mode == WM_RADIALCONTROL_STRENGTH)
-               original_value = vp->a;
-
-       RNA_float_set(op->ptr, "initial_value", original_value);
-}
-
-static int paint_radial_control_exec(wmOperator *op, VPaint *vp)
-{
-       int mode = RNA_int_get(op->ptr, "mode");
-       float new_value = RNA_float_get(op->ptr, "new_value");
-
-       if(mode == WM_RADIALCONTROL_SIZE)
-               vp->size = new_value;
-       else if(mode == WM_RADIALCONTROL_STRENGTH)
-               vp->a = new_value;
-
-       return OPERATOR_FINISHED;
-}
-
 static int vpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        toggle_paint_cursor(C, 0);
-       paint_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->vpaint);
+       brush_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->vpaint->brush, 1);
        return WM_radial_control_invoke(C, op, event);
 }
 
@@ -1221,16 +1193,13 @@ static int vpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *eve
 
 static int vpaint_radial_control_exec(bContext *C, wmOperator *op)
 {
-       int ret = paint_radial_control_exec(op, CTX_data_scene(C)->toolsettings->vpaint);
-       char str[256];
-       WM_radial_control_string(op, str, 256);
-       return ret;
+       return brush_radial_control_exec(op, CTX_data_scene(C)->toolsettings->vpaint->brush, 1);
 }
 
 static int wpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        toggle_paint_cursor(C, 1);
-       paint_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->wpaint);
+       brush_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->wpaint->brush, 1);
        return WM_radial_control_invoke(C, op, event);
 }
 
@@ -1244,10 +1213,7 @@ static int wpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *eve
 
 static int wpaint_radial_control_exec(bContext *C, wmOperator *op)
 {
-       int ret = paint_radial_control_exec(op, CTX_data_scene(C)->toolsettings->wpaint);
-       char str[256];
-       WM_radial_control_string(op, str, 256);
-       return ret;
+       return brush_radial_control_exec(op, CTX_data_scene(C)->toolsettings->wpaint->brush, 1);
 }
 
 void PAINT_OT_weight_paint_radial_control(wmOperatorType *ot)
@@ -1263,7 +1229,7 @@ void PAINT_OT_weight_paint_radial_control(wmOperatorType *ot)
        ot->poll= wp_poll;
        
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
 }
 
 void PAINT_OT_vertex_paint_radial_control(wmOperatorType *ot)
@@ -1279,7 +1245,7 @@ void PAINT_OT_vertex_paint_radial_control(wmOperatorType *ot)
        ot->poll= vp_poll;
        
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
 }
 
 /* ************ weight paint operator ********** */
@@ -1313,7 +1279,7 @@ static void wpaint_exit(bContext *C, wmOperator *op)
                for(psys= ob->particlesystem.first; psys; psys= psys->next) {
                        for(i=0; i<PSYS_TOT_VG; i++) {
                                if(psys->vgroup[i]==ob->actdef) {
-                                       psys->recalc |= PSYS_RECALC_HAIR;
+                                       psys->recalc |= PSYS_RECALC_RESET;
                                        break;
                                }
                        }
@@ -1347,7 +1313,7 @@ static int wpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
                        Object *ob= vc->obact;
                        Mesh *me= ob->data;
                        float mat[4][4];
-                       float paintweight= wp->weight;
+                       float paintweight= ts->vgroup_weight;
                        int *indexar= wpd->indexar;
                        int totindex, index, alpha, totw;
                        short mval[2];
@@ -1366,7 +1332,7 @@ static int wpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
                        
                        /* which faces are involved */
                        if(wp->flag & VP_AREA) {
-                               totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], wp->size);
+                               totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], wp->brush->size);
                        }
                        else {
                                indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
@@ -1404,7 +1370,7 @@ static int wpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
                        if(wp->mode==VP_BLUR) 
                                paintweight= 0.0f;
                        else
-                               paintweight= wp->weight;
+                               paintweight= ts->vgroup_weight;
                        
                        for(index=0; index<totindex; index++) {
                                if(indexar[index] && indexar[index]<=me->totface) {
@@ -1503,7 +1469,7 @@ static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
        // XXX  if(multires_level1_test()) return;
        
        me= get_mesh(ob);
-       if(me==NULL || me->totface==0) return OPERATOR_CANCELLED;
+       if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
        
        /* if nothing was added yet, we make dverts and a vertex deform group */
        if (!me->dvert)
@@ -1605,7 +1571,7 @@ void PAINT_OT_weight_paint(wmOperatorType *ot)
        ot->poll= wp_poll;
        
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
        
 }
 
@@ -1622,22 +1588,22 @@ static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
        me= get_mesh(ob);
        
        if(me==NULL || object_data_is_libdata(ob)) {
-               G.f &= ~G_VERTEXPAINT;
-               return OPERATOR_FINISHED;
+               ob->mode &= ~OB_MODE_VERTEX_PAINT;
+               return OPERATOR_PASS_THROUGH;
        }
        
        if(me && me->totface>=MAXINDEX) {
                error("Maximum number of faces: %d", MAXINDEX-1);
-               G.f &= ~G_VERTEXPAINT;
+               ob->mode &= ~OB_MODE_VERTEX_PAINT;
                return OPERATOR_FINISHED;
        }
        
        if(me && me->mcol==NULL) make_vertexcol(scene, 0);
        
        /* toggle: end vpaint */
-       if(G.f & G_VERTEXPAINT) {
+       if(ob->mode & OB_MODE_VERTEX_PAINT) {
                
-               G.f &= ~G_VERTEXPAINT;
+               ob->mode &= ~OB_MODE_VERTEX_PAINT;
                
                if(vp) {
                        toggle_paint_cursor(C, 0);
@@ -1646,15 +1612,16 @@ static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
        }
        else {
                
-               G.f |= G_VERTEXPAINT;
+               ob->mode |= OB_MODE_VERTEX_PAINT;
                /* Turn off weight painting */
-               if (G.f & G_WEIGHTPAINT)
+               if (ob->mode & OB_MODE_WEIGHT_PAINT)
                        set_wpaint(C, op);
                
                if(vp==NULL)
                        vp= scene->toolsettings->vpaint= new_vpaint(0);
                
                toggle_paint_cursor(C, 0);
+               brush_check_exists(&scene->toolsettings->vpaint->brush);
        }
        
        if (me)
@@ -1765,7 +1732,7 @@ static int vpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
                                
                        /* which faces are involved */
                        if(vp->flag & VP_AREA) {
-                               totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], vp->size);
+                               totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], vp->brush->size);
                        }
                        else {
                                indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
@@ -1858,7 +1825,7 @@ static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
        
        /* context checks could be a poll() */
        me= get_mesh(ob);
-       if(me==NULL || me->totface==0) return OPERATOR_CANCELLED;
+       if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
        
        if(me->mcol==NULL) make_vertexcol(CTX_data_scene(C), 0);
        if(me->mcol==NULL) return OPERATOR_CANCELLED;
@@ -1890,7 +1857,6 @@ static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
 
 void PAINT_OT_vertex_paint(wmOperatorType *ot)
 {
-       
        /* identifiers */
        ot->name= "Vertex Paint";
        ot->idname= "PAINT_OT_vertex_paint";
@@ -1902,7 +1868,6 @@ void PAINT_OT_vertex_paint(wmOperatorType *ot)
        ot->poll= vp_poll;
        
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
-       
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
 }