Commit: 888a0735eee5fbe05fc60d8755d83974a2f74680
Author: Campbell Barton
Date:   Sat Nov 3 16:52:06 2018 +1100
Branches: blender2.8
https://developer.blender.org/rB888a0735eee5fbe05fc60d8755d83974a2f74680

Tool System: validate tool slots when setting modes

Needed for entering paint modes on new scenes.

===================================================================

M       source/blender/blenkernel/BKE_paint.h
M       source/blender/blenkernel/intern/paint_toolslots.c
M       source/blender/editors/gpencil/gpencil_edit.c
M       source/blender/editors/sculpt_paint/paint_image.c
M       source/blender/editors/sculpt_paint/paint_vertex.c
M       source/blender/editors/sculpt_paint/sculpt.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_paint.h 
b/source/blender/blenkernel/BKE_paint.h
index c5fc6054812..652b794f937 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -177,6 +177,7 @@ void BKE_paint_toolslots_init_from_main(struct Main *bmain);
 void BKE_paint_toolslots_len_ensure(struct Paint *paint, int len);
 void BKE_paint_toolslots_brush_update_ex(struct Scene *scene, struct Paint 
*paint, struct Brush *brush);
 void BKE_paint_toolslots_brush_update(struct Scene *scene, struct Paint 
*paint);
+void BKE_paint_toolslots_brush_validate(struct Main *bmain, struct Scene 
*scene, struct Paint *paint);
 
 /* Used for both vertex color and weight paint */
 struct SculptVertexPaintGeomMap {
diff --git a/source/blender/blenkernel/intern/paint_toolslots.c 
b/source/blender/blenkernel/intern/paint_toolslots.c
index 19c461d1777..e7039d3160f 100644
--- a/source/blender/blenkernel/intern/paint_toolslots.c
+++ b/source/blender/blenkernel/intern/paint_toolslots.c
@@ -42,17 +42,15 @@ void BKE_paint_toolslots_len_ensure(Paint *paint, int len)
        }
 }
 
-typedef bool (*BrushCompatFn)(const Brush *brush);
-typedef char (*BrushToolFn)(const Brush *brush);
-
-static void paint_toolslots_init_paint(
-        Main *bmain,
-        Paint *paint,
-        BrushCompatFn brush_compat_fn, BrushToolFn brush_tool_fn)
+static void paint_toolslots_init(Main *bmain, Scene *scene, Paint *paint)
 {
+       uint tool_offset = 0;
+       eObjectMode ob_mode = 0;
+       bool ok = BKE_paint_brush_tool_info(scene, paint, &tool_offset, 
&ob_mode);
+       BLI_assert(ok);
        for (Brush *brush = bmain->brush.first; brush; brush = brush->id.next) {
-               if (brush_compat_fn(brush)) {
-                       uint slot_index = brush_tool_fn(brush);
+               if (brush->ob_mode & ob_mode) {
+                       const int slot_index = *(char *)POINTER_OFFSET(brush, 
tool_offset);
                        BKE_paint_toolslots_len_ensure(paint, slot_index + 1);
                        if (paint->tool_slots[slot_index].brush == NULL) {
                                paint->tool_slots[slot_index].brush = brush;
@@ -62,31 +60,15 @@ static void paint_toolslots_init_paint(
        }
 }
 
-/* Image paint. */
-static bool brush_compat_from_imagepaint(const Brush *brush) { return 
brush->ob_mode & OB_MODE_TEXTURE_PAINT; }
-static char brush_tool_from_imagepaint(const Brush *brush) { return 
brush->imagepaint_tool; }
-/* Sculpt. */
-static bool brush_compat_from_sculpt(const Brush *brush) { return 
brush->ob_mode & OB_MODE_SCULPT; }
-static char brush_tool_from_sculpt(const Brush *brush) { return 
brush->sculpt_tool; }
-/* Vertex Paint. */
-static bool brush_compat_from_vertexpaint(const Brush *brush) { return 
brush->ob_mode & OB_MODE_VERTEX_PAINT; }
-static char brush_tool_from_vertexpaint(const Brush *brush) { return 
brush->vertexpaint_tool; }
-/* Weight Paint. */
-static bool brush_compat_from_weightpaint(const Brush *brush) { return 
brush->ob_mode & OB_MODE_WEIGHT_PAINT; }
-static char brush_tool_from_weightpaint(const Brush *brush) { return 
brush->vertexpaint_tool; }
-/* Grease Pencil. */
-static bool brush_compat_from_gpencil(const Brush *brush) { return 
brush->ob_mode & OB_MODE_GPENCIL_PAINT; }
-static char brush_tool_from_gpencil(const Brush *brush) { return 
brush->gpencil_tool; }
-
 void BKE_paint_toolslots_init_from_main(struct Main *bmain)
 {
        for (Scene *scene = bmain->scene.first; scene; scene = scene->id.next) {
                ToolSettings *ts = scene->toolsettings;
-               paint_toolslots_init_paint(bmain, &ts->imapaint.paint, 
brush_compat_from_imagepaint, brush_tool_from_imagepaint);
-               paint_toolslots_init_paint(bmain, &ts->sculpt->paint, 
brush_compat_from_sculpt, brush_tool_from_sculpt);
-               paint_toolslots_init_paint(bmain, &ts->vpaint->paint, 
brush_compat_from_vertexpaint, brush_tool_from_vertexpaint);
-               paint_toolslots_init_paint(bmain, &ts->wpaint->paint, 
brush_compat_from_weightpaint, brush_tool_from_weightpaint);
-               paint_toolslots_init_paint(bmain, &ts->gp_paint->paint, 
brush_compat_from_gpencil, brush_tool_from_gpencil);
+               paint_toolslots_init(bmain, scene, &ts->imapaint.paint);
+               paint_toolslots_init(bmain, scene, &ts->sculpt->paint);
+               paint_toolslots_init(bmain, scene, &ts->vpaint->paint);
+               paint_toolslots_init(bmain, scene, &ts->wpaint->paint);
+               paint_toolslots_init(bmain, scene, &ts->gp_paint->paint);
        }
 }
 
@@ -111,3 +93,32 @@ void BKE_paint_toolslots_brush_update(Scene *scene, Paint 
*paint)
        }
        BKE_paint_toolslots_brush_update_ex(scene, paint, paint->brush);
 }
+
+/**
+ * Run this to ensure brush types are set for each slot on entering modes
+ * (for new scenes for example).
+ */
+void BKE_paint_toolslots_brush_validate(Main *bmain, Scene *scene, Paint 
*paint)
+{
+       /* Clear slots with invalid slots or mode (unlikely but possible). */
+       uint tool_offset = 0;
+       eObjectMode ob_mode = 0;
+       bool ok = BKE_paint_brush_tool_info(scene, paint, &tool_offset, 
&ob_mode);
+       BLI_assert(ok);
+       for (int i = 0; i < paint->tool_slots_len; i++) {
+               PaintToolSlot *tslot = &paint->tool_slots[i];
+               if (tslot->brush) {
+                       int slot_index = *(char *)POINTER_OFFSET(tslot->brush, 
tool_offset);
+                       if ((slot_index != i) || (tslot->brush->ob_mode & 
ob_mode) == 0) {
+                               id_us_min(&tslot->brush->id);
+                               tslot->brush = NULL;
+                       }
+               }
+       }
+
+       /* Unlikely but possible the active brush is not currently using a 
slot. */
+       BKE_paint_toolslots_brush_update(scene, paint);
+
+       /* Fill slots from brushes. */
+       paint_toolslots_init(bmain, scene, paint);
+}
diff --git a/source/blender/editors/gpencil/gpencil_edit.c 
b/source/blender/editors/gpencil/gpencil_edit.c
index 7b2eb7d6ecd..d8580f872f4 100644
--- a/source/blender/editors/gpencil/gpencil_edit.c
+++ b/source/blender/editors/gpencil/gpencil_edit.c
@@ -281,6 +281,8 @@ static int gpencil_paintmode_toggle_exec(bContext *C, 
wmOperator *op)
        const bool back = RNA_boolean_get(op->ptr, "back");
 
        struct wmMsgBus *mbus = CTX_wm_message_bus(C);
+       Main *bmain = CTX_data_main(C);
+       Scene *scene = CTX_data_scene(C);
        bGPdata *gpd = ED_gpencil_data_get_active(C);
        ToolSettings *ts = CTX_data_tool_settings(C);
 
@@ -315,11 +317,14 @@ static int gpencil_paintmode_toggle_exec(bContext *C, 
wmOperator *op)
                ob->mode = mode;
        }
 
-       /* be sure we have brushes */
-       Paint *paint = BKE_brush_get_gpencil_paint(ts);
-       /* if not exist, create a new one */
-       if (paint->brush == NULL) {
-               BKE_brush_gpencil_presets(C);
+       if (mode == OB_MODE_GPENCIL_PAINT) {
+               /* be sure we have brushes */
+               Paint *paint = BKE_brush_get_gpencil_paint(ts);
+               /* if not exist, create a new one */
+               if (paint->brush == NULL) {
+                       BKE_brush_gpencil_presets(C);
+               }
+               BKE_paint_toolslots_brush_validate(bmain, scene, 
&ts->gp_paint->paint);
        }
 
        /* setup other modes */
diff --git a/source/blender/editors/sculpt_paint/paint_image.c 
b/source/blender/editors/sculpt_paint/paint_image.c
index aaf82d8ef2b..be51c8071c2 100644
--- a/source/blender/editors/sculpt_paint/paint_image.c
+++ b/source/blender/editors/sculpt_paint/paint_image.c
@@ -1132,6 +1132,8 @@ static int texture_paint_toggle_exec(bContext *C, 
wmOperator *op)
 
                BKE_paint_init(bmain, scene, ePaintTextureProjective, 
PAINT_CURSOR_TEXTURE_PAINT);
 
+               BKE_paint_toolslots_brush_validate(bmain, scene, 
&imapaint->paint);
+
                if (U.glreslimit != 0)
                        GPU_free_images(bmain);
                GPU_paint_set_mipmap(bmain, 0);
diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c 
b/source/blender/editors/sculpt_paint/paint_vertex.c
index 7b3cfe184f5..94aeeb5d158 100644
--- a/source/blender/editors/sculpt_paint/paint_vertex.c
+++ b/source/blender/editors/sculpt_paint/paint_vertex.c
@@ -1247,6 +1247,7 @@ static int wpaint_mode_toggle_exec(bContext *C, 
wmOperator *op)
        const int mode_flag = OB_MODE_WEIGHT_PAINT;
        const bool is_mode_set = (ob->mode & mode_flag) != 0;
        Scene *scene = CTX_data_scene(C);
+       ToolSettings *ts = scene->toolsettings;
 
        if (!is_mode_set) {
                if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
@@ -1263,6 +1264,7 @@ static int wpaint_mode_toggle_exec(bContext *C, 
wmOperator *op)
                Depsgraph *depsgraph = CTX_data_depsgraph_on_load(C);
                wmWindowManager *wm = CTX_wm_manager(C);
                ED_object_wpaintmode_enter_ex(bmain, depsgraph, wm, scene, ob);
+               BKE_paint_toolslots_brush_validate(bmain, scene, 
&ts->wpaint->paint);
        }
 
        /* Weightpaint works by overriding colors in mesh,
@@ -2390,6 +2392,7 @@ static int vpaint_mode_toggle_exec(bContext *C, 
wmOperator *op)
        const int mode_flag = OB_MODE_VERTEX_PAINT;
        const bool is_mode_set = (ob->mode & mode_flag) != 0;
        Scene *scene = CTX_data_scene(C);
+       ToolSettings *ts = scene->toolsettings;
 
        if (!is_mode_set) {
                if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
@@ -2407,6 +2410,7 @@ static int vpaint_mode_toggle_exec(bContext *C, 
wmOperator *op)
                Depsgraph *depsgraph = CTX_data_depsgraph_on_load(C);
                wmWindowManager *wm = CTX_wm_manager(C);
                ED_object_vpaintmode_enter_ex(bmain, depsgraph, wm, scene, ob);
+               BKE_paint_toolslots_brush_validate(bmain, scene, 
&ts->wpaint->paint);
        }
 
        BKE_mesh_batch_cache_dirty_tag(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
diff --git a/source/blender/editors/sculpt_paint/sculpt.c 
b/source/blender/editors/sculpt_paint/sculpt.c
index 7365101b9f2..d5e629f205d 100644
--- a/source/blender/editors/sculpt_paint/sculpt.c
+++ b/source/blender/editors/sculpt_paint/sculpt.c
@@ -5819,6 +5819,7 @@ static int sculpt_mode_toggle_exec(bContext *C, 
wmOperator *op)
        Main *bmain = CTX_data_main(C);
        Depsgraph *depsgraph = CTX_data_depsgraph_on_load(C);
        Scene *scene = CTX_data_scene(C);
+       ToolSettings *ts = scene->toolsettings;
        Object *ob = CTX_data_active_object(C);
        const int mode_flag = OB_MODE_SCULPT;
        const bool is_mode_set = (ob->mode & mode_flag) != 0;
@@ -5834,6 +5835,7 @@ static int sculpt_mode_toggle_exec(bContext *C, 
wmOperator *op)
        }
        else {
                ED_object_sculptmode_enter_ex(bmain, depsgraph, scene, ob, 
op->reports);
+               BKE_paint_toolslots_brush_validate(bmain, scene, 
&ts->sculpt->paint);
        }
 
        WM_event_add_notifier(C, NC_SCENE | ND_MODE, scene);

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to