Fix #31093: Brush's "Paint curve presets" in Image paint in UV\Image Editor doesn...
authorSergey Sharybin <sergey.vfx@gmail.com>
Mon, 4 Jun 2012 07:29:45 +0000 (07:29 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Mon, 4 Jun 2012 07:29:45 +0000 (07:29 +0000)
Some operators like curve presets, color sample and some more were using object's
mode to distinguish in which mode user is currently painting. Such approach fails
in cases when there's paint mode active in 3D viewport and Image Editor.

Changed logic here to use some context's state like active space which helps
distinguishing current paint mode more accurate.

Ported all areas which uses paint_get_active() to new paint_get_active_from_context().
There're still some calls to paint_get_active(), but that shouldn't be harmful due
to that places indeed have object's mode as priority when getting paint mode.

source/blender/blenkernel/BKE_paint.h
source/blender/blenkernel/intern/paint.c
source/blender/editors/sculpt_paint/paint_cursor.c
source/blender/editors/sculpt_paint/paint_image.c
source/blender/editors/sculpt_paint/paint_intern.h
source/blender/editors/sculpt_paint/paint_ops.c
source/blender/editors/sculpt_paint/paint_stroke.c
source/blender/editors/sculpt_paint/paint_utils.c
source/blender/editors/space_buttons/buttons_texture.c
source/blender/editors/space_view3d/view3d_edit.c

index b32b7145ff4f43b4393c20d08f2ef219f528b596..419fb4cedaeb43bddcac87fb0beed4f140f7e8f7 100644 (file)
@@ -32,6 +32,7 @@
  *  \ingroup bke
  */
 
+struct bContext;
 struct Brush;
 struct MDisps;
 struct MeshElemMap;
@@ -55,6 +56,7 @@ void free_paint(struct Paint *p);
 void copy_paint(struct Paint *src, struct Paint *tar);
 
 struct Paint *paint_get_active(struct Scene *sce);
+struct Paint *paint_get_active_from_context(const struct bContext *C);
 struct Brush *paint_brush(struct Paint *paint);
 void paint_brush_set(struct Paint *paint, struct Brush *br);
 
index c7f904755d9233741b0af778634dfce07bdf19db..25553448306f49f015662daef3dc2af0bc335071 100644 (file)
@@ -41,6 +41,7 @@
 #include "BLI_utildefines.h"
 
 #include "BKE_brush.h"
+#include "BKE_context.h"
 #include "BKE_library.h"
 #include "BKE_paint.h"
 #include "BKE_subsurf.h"
@@ -83,6 +84,53 @@ Paint *paint_get_active(Scene *sce)
        return NULL;
 }
 
+Paint *paint_get_active_from_context(const bContext *C)
+{
+       Scene *sce = CTX_data_scene(C);
+
+       if (sce) {
+               ToolSettings *ts = sce->toolsettings;
+               Object *obact = NULL;
+
+               if (sce->basact && sce->basact->object)
+                       obact = sce->basact->object;
+
+               if (CTX_wm_space_image(C) != NULL) {
+                       if (obact->mode == OB_MODE_EDIT) {
+                               if (ts->use_uv_sculpt)
+                                       return &ts->uvsculpt->paint;
+                               else
+                                       return &ts->imapaint.paint;
+                       }
+                       else {
+                               return &ts->imapaint.paint;
+                       }
+               }
+               else {
+                       switch (obact->mode) {
+                               case OB_MODE_SCULPT:
+                                       return &ts->sculpt->paint;
+                               case OB_MODE_VERTEX_PAINT:
+                                       return &ts->vpaint->paint;
+                               case OB_MODE_WEIGHT_PAINT:
+                                       return &ts->wpaint->paint;
+                               case OB_MODE_TEXTURE_PAINT:
+                                       return &ts->imapaint.paint;
+                               case OB_MODE_EDIT:
+                                       if (ts->use_uv_sculpt)
+                                               return &ts->uvsculpt->paint;
+                                       else
+                                               return &ts->imapaint.paint;
+                       }
+
+                       /* default to image paint */
+                       return &ts->imapaint.paint;
+               }
+       }
+
+       return NULL;
+}
+
 Brush *paint_brush(Paint *p)
 {
        return p ? p->brush : NULL;
index b5d44676cf681ed48b39cbbf2d693a38a5464b3c..5ca436b07b28730e2b4904900091e16d132285f8 100644 (file)
@@ -333,7 +333,7 @@ static int sculpt_get_brush_geometry(bContext *C, ViewContext *vc,
                                      float location[3])
 {
        Scene *scene = CTX_data_scene(C);
-       Paint *paint = paint_get_active(scene);
+       Paint *paint = paint_get_active_from_context(C);
        Brush *brush = paint_brush(paint);
        float window[2];
        int hit;
@@ -503,7 +503,7 @@ static void paint_cursor_on_hit(Sculpt *sd, Brush *brush, ViewContext *vc,
 static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
 {
        Scene *scene = CTX_data_scene(C);
-       Paint *paint = paint_get_active(scene);
+       Paint *paint = paint_get_active_from_context(C);
        Brush *brush = paint_brush(paint);
        ViewContext vc;
        float final_radius;
@@ -605,7 +605,7 @@ static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
 
 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
 {
-       Paint *p = paint_get_active(CTX_data_scene(C));
+       Paint *p = paint_get_active_from_context(C);
 
        if (p && !p->paint_cursor)
                p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
index ed86cb67687321f57e1f54fb8fc1498e9b47f005..4dee83dbb8243277f03f69c9a7996226c33cdc46 100644 (file)
@@ -5215,7 +5215,7 @@ static void brush_drawcursor(bContext *C, int x, int y, void *UNUSED(customdata)
 
        Scene *scene = CTX_data_scene(C);
        //Brush *brush= image_paint_brush(C);
-       Paint *paint = paint_get_active(scene);
+       Paint *paint = paint_get_active_from_context(C);
        Brush *brush = paint_brush(paint);
 
        if (paint && brush && paint->flags & PAINT_SHOW_BRUSH) {
@@ -5420,13 +5420,12 @@ void PAINT_OT_grab_clone(wmOperatorType *ot)
 
 static int sample_color_exec(bContext *C, wmOperator *op)
 {
-       Scene *scene = CTX_data_scene(C);
        Brush *brush = image_paint_brush(C);
        ARegion *ar = CTX_wm_region(C);
        int location[2];
 
        RNA_int_get_array(op->ptr, "location", location);
-       paint_sample_color(scene, ar, location[0], location[1]);
+       paint_sample_color(C, ar, location[0], location[1]);
 
        WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
        
index de149bf2806edc8b3ea566aac48769c4a3f0f99e..794e7755636b5730291b74caddd3d0584de02da3 100644 (file)
@@ -137,7 +137,7 @@ float paint_get_tex_pixel(struct Brush *br, float u, float v);
 int imapaint_pick_face(struct ViewContext *vc, const int mval[2], unsigned int *index, unsigned int totface);
 void imapaint_pick_uv(struct Scene *scene, struct Object *ob, unsigned int faceindex, const int xy[2], float uv[2]);
 
-void paint_sample_color(struct Scene *scene, struct ARegion *ar, int x, int y);
+void paint_sample_color(const struct bContext *C, struct ARegion *ar, int x, int y);
 void BRUSH_OT_curve_preset(struct wmOperatorType *ot);
 
 void PAINT_OT_face_select_linked(struct wmOperatorType *ot);
index 23d1c0090a0d1ece30589f9cf34ed053e6a6f039..7df6a893b5c2d483b903e796abc94321a7aea7d7 100644 (file)
@@ -60,7 +60,7 @@
 static int brush_add_exec(bContext *C, wmOperator *UNUSED(op))
 {
        /*int type = RNA_enum_get(op->ptr, "type");*/
-       Paint *paint = paint_get_active(CTX_data_scene(C));
+       Paint *paint = paint_get_active_from_context(C);
        struct Brush *br = paint_brush(paint);
 
        if (br)
@@ -68,7 +68,7 @@ static int brush_add_exec(bContext *C, wmOperator *UNUSED(op))
        else
                br = BKE_brush_add("Brush");
 
-       paint_brush_set(paint_get_active(CTX_data_scene(C)), br);
+       paint_brush_set(paint, br);
 
        return OPERATOR_FINISHED;
 }
@@ -91,7 +91,7 @@ static void BRUSH_OT_add(wmOperatorType *ot)
 static int brush_scale_size_exec(bContext *C, wmOperator *op)
 {
        Scene *scene = CTX_data_scene(C);
-       Paint  *paint =  paint_get_active(scene);
+       Paint  *paint =  paint_get_active_from_context(C);
        struct Brush  *brush =  paint_brush(paint);
        // Object *ob=     CTX_data_active_object(C);
        float scalar = RNA_float_get(op->ptr, "scalar");
@@ -173,7 +173,7 @@ static void PAINT_OT_vertex_color_set(wmOperatorType *ot)
 
 static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       Paint *paint = paint_get_active(CTX_data_scene(C));
+       Paint *paint = paint_get_active_from_context(C);
        struct Brush *brush = paint_brush(paint);
        Object *ob = CTX_data_active_object(C);
 
index 987ab932fd6e4f798bd4cc16299c5504eebd6ab8..e36056e0fd9af4506bc93f42029306a9bf197580 100644 (file)
@@ -100,10 +100,11 @@ typedef struct PaintStroke {
 /*** Cursor ***/
 static void paint_draw_smooth_stroke(bContext *C, int x, int y, void *customdata) 
 {
-       Brush *brush = paint_brush(paint_get_active(CTX_data_scene(C)));
+       Paint *paint = paint_get_active_from_context(C);
+       Brush *brush = paint_brush(paint);
        PaintStroke *stroke = customdata;
 
-       glColor4ubv(paint_get_active(CTX_data_scene(C))->paint_cursor_col);
+       glColor4ubv(paint->paint_cursor_col);
        glEnable(GL_LINE_SMOOTH);
        glEnable(GL_BLEND);
 
@@ -141,7 +142,7 @@ static float event_tablet_data(wmEvent *event, int *pen_flip)
 static void paint_brush_stroke_add_step(bContext *C, wmOperator *op, wmEvent *event, float mouse_in[2])
 {
        Scene *scene = CTX_data_scene(C);
-       Paint *paint = paint_get_active(scene);
+       Paint *paint = paint_get_active_from_context(C);
        Brush *brush = paint_brush(paint);
        PaintStroke *stroke = op->customdata;
        float mouse[3];
@@ -281,7 +282,7 @@ PaintStroke *paint_stroke_new(bContext *C,
 {
        PaintStroke *stroke = MEM_callocN(sizeof(PaintStroke), "PaintStroke");
 
-       stroke->brush = paint_brush(paint_get_active(CTX_data_scene(C)));
+       stroke->brush = paint_brush(paint_get_active_from_context(C));
        view3d_set_viewcontext(C, &stroke->vc);
        view3d_get_transformation(stroke->vc.ar, stroke->vc.rv3d, stroke->vc.obact, &stroke->mats);
 
@@ -394,7 +395,7 @@ static void paint_stroke_sample_average(const PaintStroke *stroke,
 
 int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
-       Paint *p = paint_get_active(CTX_data_scene(C));
+       Paint *p = paint_get_active_from_context(C);
        PaintStroke *stroke = op->customdata;
        PaintSample sample_average;
        float mouse[2];
@@ -518,7 +519,7 @@ void paint_stroke_set_mode_data(PaintStroke *stroke, void *mode_data)
 
 int paint_poll(bContext *C)
 {
-       Paint *p = paint_get_active(CTX_data_scene(C));
+       Paint *p = paint_get_active_from_context(C);
        Object *ob = CTX_data_active_object(C);
 
        return p && ob && paint_brush(p) &&
index 4c374674c9a98271ed764ce82bed00bf4e0bca46..082e40f8e4c2ce7adc8cb2eca4051c952d0f71f6 100644 (file)
@@ -333,9 +333,9 @@ int imapaint_pick_face(ViewContext *vc, const int mval[2], unsigned int *index,
 }
 
 /* used for both 3d view and image window */
-void paint_sample_color(Scene *scene, ARegion *ar, int x, int y)    /* frontbuf */
+void paint_sample_color(const bContext *C, ARegion *ar, int x, int y)    /* frontbuf */
 {
-       Brush *br = paint_brush(paint_get_active(scene));
+       Brush *br = paint_brush(paint_get_active_from_context(C));
        unsigned int col;
        char *cp;
 
@@ -357,7 +357,7 @@ void paint_sample_color(Scene *scene, ARegion *ar, int x, int y)    /* frontbuf
 
 static int brush_curve_preset_exec(bContext *C, wmOperator *op)
 {
-       Brush *br = paint_brush(paint_get_active(CTX_data_scene(C)));
+       Brush *br = paint_brush(paint_get_active_from_context(C));
        BKE_brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
 
        return OPERATOR_FINISHED;
@@ -365,7 +365,7 @@ static int brush_curve_preset_exec(bContext *C, wmOperator *op)
 
 static int brush_curve_preset_poll(bContext *C)
 {
-       Brush *br = paint_brush(paint_get_active(CTX_data_scene(C)));
+       Brush *br = paint_brush(paint_get_active_from_context(C));
 
        return br && br->curve;
 }
index 5c5c24f7bc1df07fd8fe9b36c13044e1e1e1b886..cdecda6343288251b57a845fe0731c4b0b287c45 100644 (file)
@@ -175,7 +175,7 @@ static void buttons_texture_users_from_context(ListBase *users, const bContext *
        if (!(pinid || pinid == &scene->id)) {
                ob = (scene->basact) ? scene->basact->object : NULL;
                wrld = scene->world;
-               brush = paint_brush(paint_get_active(scene));
+               brush = paint_brush(paint_get_active_from_context(C));
        }
 
        if (ob && ob->type == OB_LAMP && !la)
index 8ba0d75c7869a8755abb998809318e8cb0a32523..d632314f3cac6f13ba44cb4970a6512fa46f599b 100644 (file)
@@ -498,7 +498,7 @@ static void viewops_data_create(bContext *C, wmOperator *op, wmEvent *event)
 static void viewops_data_free(bContext *C, wmOperator *op)
 {
        ARegion *ar;
-       Paint *p = paint_get_active(CTX_data_scene(C));
+       Paint *p = paint_get_active_from_context(C);
 
        if (op->customdata) {
                ViewOpsData *vod = op->customdata;