Cleanup: move brush query into utility function
authorCampbell Barton <ideasman42@gmail.com>
Sat, 3 Nov 2018 04:44:16 +0000 (15:44 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 3 Nov 2018 04:55:45 +0000 (15:55 +1100)
source/blender/blenkernel/BKE_paint.h
source/blender/blenkernel/intern/paint.c
source/blender/blenkernel/intern/paint_toolslots.c
source/blender/makesrna/intern/rna_sculpt_paint.c

index b8938c87275404db690add94e841c0c4b69c4869..c5fc60548128f5badf3370eda38297d282b164b7 100644 (file)
@@ -136,6 +136,9 @@ struct Paint *BKE_paint_get_active(struct Scene *sce, struct ViewLayer *view_lay
 struct Paint *BKE_paint_get_active_from_context(const struct bContext *C);
 ePaintMode BKE_paintmode_get_active_from_context(const struct bContext *C);
 struct Brush *BKE_paint_brush(struct Paint *paint);
+bool BKE_paint_brush_tool_info(
+        const struct Scene *scene, const struct Paint *paint,
+        uint *r_tool_offset, eObjectMode *r_ob_mode);
 void BKE_paint_brush_set(struct Paint *paint, struct Brush *br);
 struct Palette *BKE_paint_palette(struct Paint *paint);
 void BKE_paint_palette_set(struct Paint *p, struct Palette *palette);
index 93b54fcb132f6bdf57853d8c887a6254e8e777dd..7838fb69f1adbe2ef48382439eaba8e91bd67e99 100644 (file)
@@ -302,6 +302,58 @@ void BKE_paint_brush_set(Paint *p, Brush *br)
        }
 }
 
+bool BKE_paint_brush_tool_info(
+        const Scene *scene, const struct Paint *paint,
+        uint *r_tool_offset, eObjectMode *r_ob_mode)
+{
+       ToolSettings *ts = scene->toolsettings;
+       if (paint == &ts->imapaint.paint) {
+               if (r_tool_offset != NULL) {
+                       *r_tool_offset = offsetof(Brush, imagepaint_tool);
+               }
+               if (r_ob_mode != NULL) {
+                       *r_ob_mode = OB_MODE_TEXTURE_PAINT;
+               }
+       }
+       else if (paint == &ts->sculpt->paint) {
+               if (r_tool_offset != NULL) {
+                       *r_tool_offset = offsetof(Brush, sculpt_tool);
+               }
+               if (r_ob_mode != NULL) {
+                       *r_ob_mode = OB_MODE_SCULPT;
+               }
+       }
+       else if (paint == &ts->vpaint->paint) {
+               if (r_tool_offset != NULL) {
+                       *r_tool_offset = offsetof(Brush, vertexpaint_tool);
+               }
+               if (r_ob_mode != NULL) {
+                       *r_ob_mode = OB_MODE_VERTEX_PAINT;
+               }
+       }
+       else if (paint == &ts->wpaint->paint) {
+               if (r_tool_offset != NULL) {
+                       *r_tool_offset = offsetof(Brush, vertexpaint_tool);
+               }
+               if (r_ob_mode != NULL) {
+                       *r_ob_mode = OB_MODE_WEIGHT_PAINT;
+               }
+       }
+       else if (paint == &ts->gp_paint->paint) {
+               if (r_tool_offset != NULL) {
+                       *r_tool_offset = offsetof(Brush, gpencil_tool);
+               }
+               if (r_ob_mode != NULL) {
+                       *r_ob_mode = OB_MODE_GPENCIL_PAINT;
+               }
+       }
+       else {
+               return false;
+       }
+       return true;
+}
+
+
 /** Free (or release) any data used by this paint curve (does not free the pcurve itself). */
 void BKE_paint_curve_free(PaintCurve *pc)
 {
index bdafe2172762ac51ed060c829026a43bb9571c99..19c461d177728197c37afda4c551598bb2d153e1 100644 (file)
@@ -93,23 +93,10 @@ void BKE_paint_toolslots_init_from_main(struct Main *bmain)
 
 void BKE_paint_toolslots_brush_update_ex(Scene *scene, Paint *paint, Brush *brush)
 {
-       ToolSettings *ts = scene->toolsettings;
-       int slot_index;
-       if (paint == &ts->imapaint.paint) {
-               slot_index = brush->imagepaint_tool;
-       }
-       else if (paint == &ts->sculpt->paint) {
-               slot_index = brush->sculpt_tool;
-       }
-       else if (paint == &ts->vpaint->paint) {
-               slot_index = brush->vertexpaint_tool;
-       }
-       else if (paint == &ts->wpaint->paint) {
-               slot_index = brush->vertexpaint_tool;
-       }
-       else if (paint == &ts->gp_paint->paint) {
-               slot_index = brush->gpencil_tool;
-       }
+       uint tool_offset = 0;
+       bool ok = BKE_paint_brush_tool_info(scene, paint, &tool_offset, NULL);
+       BLI_assert(ok);
+       int slot_index = *(char *)POINTER_OFFSET(brush, tool_offset);
        BKE_paint_toolslots_len_ensure(paint, slot_index + 1);
        PaintToolSlot *tslot = &paint->tool_slots[slot_index];
        id_us_plus(&brush->id);
index dcddf77b963559a6f8459729ca6e544aa9e26b86..86c860ef6a5fcf487df459342118799cc94926a2 100644 (file)
@@ -274,36 +274,17 @@ static bool rna_Brush_mode_poll(PointerRNA *ptr, PointerRNA value)
 {
        Scene *scene = (Scene *)ptr->id.data;
        const Paint *paint = ptr->data;
-       ToolSettings *ts = scene->toolsettings;
        Brush *brush = value.id.data;
-       int mode = 0;
+       eObjectMode ob_mode = 0;
        uint brush_tool_offset = 0;
 
        /* check the origin of the Paint struct to see which paint
         * mode to select from */
 
-       if (paint == &ts->imapaint.paint) {
-               mode = OB_MODE_TEXTURE_PAINT;
-               brush_tool_offset = offsetof(Brush, imagepaint_tool);
-       }
-       else if (paint == &ts->sculpt->paint) {
-               mode = OB_MODE_SCULPT;
-               brush_tool_offset = offsetof(Brush, sculpt_tool);
-       }
-       else if (paint == &ts->vpaint->paint) {
-               mode = OB_MODE_VERTEX_PAINT;
-               brush_tool_offset = offsetof(Brush, vertexpaint_tool);
-       }
-       else if (paint == &ts->wpaint->paint) {
-               mode = OB_MODE_WEIGHT_PAINT;
-               brush_tool_offset = offsetof(Brush, vertexpaint_tool);
-       }
-       else if (paint == &ts->gp_paint->paint) {
-               mode = OB_MODE_GPENCIL_PAINT;
-               brush_tool_offset = offsetof(Brush, gpencil_tool);
-       }
+       bool ok = BKE_paint_brush_tool_info(scene, paint, &brush_tool_offset, &ob_mode);
+       BLI_assert(ok);
 
-       if (brush->ob_mode & mode) {
+       if (brush->ob_mode & ob_mode) {
                if (paint->brush) {
                        const char *tool_a = (const char *)POINTER_OFFSET(paint->brush, brush_tool_offset);
                        const char *tool_b = (const char *)POINTER_OFFSET(brush,        brush_tool_offset);