diff options
Diffstat (limited to 'source/blender/editors/interface/interface_eyedropper_gpencil_color.c')
-rw-r--r-- | source/blender/editors/interface/interface_eyedropper_gpencil_color.c | 114 |
1 files changed, 87 insertions, 27 deletions
diff --git a/source/blender/editors/interface/interface_eyedropper_gpencil_color.c b/source/blender/editors/interface/interface_eyedropper_gpencil_color.c index 790609a17d8..f9f5c745e94 100644 --- a/source/blender/editors/interface/interface_eyedropper_gpencil_color.c +++ b/source/blender/editors/interface/interface_eyedropper_gpencil_color.c @@ -39,6 +39,7 @@ #include "BKE_gpencil.h" #include "BKE_main.h" #include "BKE_material.h" +#include "BKE_paint.h" #include "BKE_report.h" #include "UI_interface.h" @@ -65,6 +66,8 @@ typedef struct EyedropperGPencil { struct ColorManagedDisplay *display; /** color under cursor RGB */ float color[3]; + /** Mode */ + int mode; } EyedropperGPencil; /* Helper: Draw status message while the user is running the operator */ @@ -89,6 +92,7 @@ static bool eyedropper_gpencil_init(bContext *C, wmOperator *op) display_device = scene->display_settings.display_device; eye->display = IMB_colormanagement_display_get_named(display_device); + eye->mode = RNA_enum_get(op->ptr, "mode"); return true; } @@ -101,31 +105,15 @@ static void eyedropper_gpencil_exit(bContext *C, wmOperator *op) MEM_SAFE_FREE(op->customdata); } -/* Set the material. */ -static void eyedropper_gpencil_color_set(bContext *C, const wmEvent *event, EyedropperGPencil *eye) +static void eyedropper_add_material( + bContext *C, float col_conv[4], const bool only_stroke, const bool only_fill, const bool both) { Main *bmain = CTX_data_main(C); Object *ob = CTX_data_active_object(C); Material *ma = NULL; - const bool only_stroke = ((!event->ctrl) && (!event->shift)); - const bool only_fill = ((!event->ctrl) && (event->shift)); - const bool both = ((event->ctrl) && (event->shift)); - - float col_conv[4]; bool found = false; - /* Convert from linear rgb space to display space because grease pencil colors are in display - * space, and this conversion is needed to undo the conversion to linear performed by - * eyedropper_color_sample_fl. */ - if (eye->display) { - copy_v3_v3(col_conv, eye->color); - IMB_colormanagement_scene_linear_to_display_v3(col_conv, eye->display); - } - else { - copy_v3_v3(col_conv, eye->color); - } - /* Look for a similar material in grease pencil slots. */ short *totcol = BKE_object_material_len_p(ob); for (short i = 0; i < *totcol; i++) { @@ -138,15 +126,15 @@ static void eyedropper_gpencil_color_set(bContext *C, const wmEvent *event, Eyed if (gp_style != NULL) { /* Check stroke color. */ bool found_stroke = compare_v3v3(gp_style->stroke_rgba, col_conv, 0.01f) && - (gp_style->flag & GP_STYLE_STROKE_SHOW); + (gp_style->flag & GP_MATERIAL_STROKE_SHOW); /* Check fill color. */ bool found_fill = compare_v3v3(gp_style->fill_rgba, col_conv, 0.01f) && - (gp_style->flag & GP_STYLE_FILL_SHOW); + (gp_style->flag & GP_MATERIAL_FILL_SHOW); - if ((only_stroke) && (found_stroke) && ((gp_style->flag & GP_STYLE_FILL_SHOW) == 0)) { + if ((only_stroke) && (found_stroke) && ((gp_style->flag & GP_MATERIAL_FILL_SHOW) == 0)) { found = true; } - else if ((only_fill) && (found_fill) && ((gp_style->flag & GP_STYLE_STROKE_SHOW) == 0)) { + else if ((only_fill) && (found_fill) && ((gp_style->flag & GP_MATERIAL_STROKE_SHOW) == 0)) { found = true; } else if ((both) && (found_stroke) && (found_fill)) { @@ -180,22 +168,22 @@ static void eyedropper_gpencil_color_set(bContext *C, const wmEvent *event, Eyed /* Only create Stroke (default option). */ if (only_stroke) { /* Stroke color. */ - gp_style_new->flag |= GP_STYLE_STROKE_SHOW; - gp_style_new->flag &= ~GP_STYLE_FILL_SHOW; + gp_style_new->flag |= GP_MATERIAL_STROKE_SHOW; + gp_style_new->flag &= ~GP_MATERIAL_FILL_SHOW; copy_v3_v3(gp_style_new->stroke_rgba, col_conv); zero_v4(gp_style_new->fill_rgba); } /* Fill Only. */ else if (only_fill) { /* Fill color. */ - gp_style_new->flag &= ~GP_STYLE_STROKE_SHOW; - gp_style_new->flag |= GP_STYLE_FILL_SHOW; + gp_style_new->flag &= ~GP_MATERIAL_STROKE_SHOW; + gp_style_new->flag |= GP_MATERIAL_FILL_SHOW; zero_v4(gp_style_new->stroke_rgba); copy_v3_v3(gp_style_new->fill_rgba, col_conv); } /* Stroke and Fill. */ else if (both) { - gp_style_new->flag |= GP_STYLE_STROKE_SHOW | GP_STYLE_FILL_SHOW; + gp_style_new->flag |= GP_MATERIAL_STROKE_SHOW | GP_MATERIAL_FILL_SHOW; copy_v3_v3(gp_style_new->stroke_rgba, col_conv); copy_v3_v3(gp_style_new->fill_rgba, col_conv); } @@ -203,6 +191,69 @@ static void eyedropper_gpencil_color_set(bContext *C, const wmEvent *event, Eyed ED_undo_push(C, "Add Grease Pencil Material"); } +/* Create a new palette color and palette if needed. */ +static void eyedropper_add_palette_color(bContext *C, float col_conv[4]) +{ + Main *bmain = CTX_data_main(C); + Scene *scene = CTX_data_scene(C); + ToolSettings *ts = scene->toolsettings; + GpPaint *gp_paint = ts->gp_paint; + GpVertexPaint *gp_vertexpaint = ts->gp_vertexpaint; + Paint *paint = &gp_paint->paint; + Paint *vertexpaint = &gp_vertexpaint->paint; + + /* Check for Palette in Draw and Vertex Paint Mode. */ + if (paint->palette == NULL) { + paint->palette = BKE_palette_add(bmain, "Grease Pencil"); + if (vertexpaint->palette == NULL) { + vertexpaint->palette = paint->palette; + } + } + /* Check if the color exist already. */ + Palette *palette = paint->palette; + for (PaletteColor *palcolor = palette->colors.first; palcolor; palcolor = palcolor->next) { + if (compare_v3v3(palcolor->rgb, col_conv, 0.01f)) { + return; + } + } + + /* Create Colors. */ + PaletteColor *palcol = BKE_palette_color_add(palette); + if (palcol) { + copy_v3_v3(palcol->rgb, col_conv); + } +} + +/* Set the material or the palette color. */ +static void eyedropper_gpencil_color_set(bContext *C, const wmEvent *event, EyedropperGPencil *eye) +{ + + const bool only_stroke = ((!event->ctrl) && (!event->shift)); + const bool only_fill = ((!event->ctrl) && (event->shift)); + const bool both = ((event->ctrl) && (event->shift)); + + float col_conv[4]; + + /* Convert from linear rgb space to display space because grease pencil colors are in display + * space, and this conversion is needed to undo the conversion to linear performed by + * eyedropper_color_sample_fl. */ + if (eye->display) { + copy_v3_v3(col_conv, eye->color); + IMB_colormanagement_scene_linear_to_display_v3(col_conv, eye->display); + } + else { + copy_v3_v3(col_conv, eye->color); + } + + /* Add material or Palette color*/ + if (eye->mode == 0) { + eyedropper_add_material(C, col_conv, only_stroke, only_fill, both); + } + else { + eyedropper_add_palette_color(C, col_conv); + } +} + /* Sample the color below cursor. */ static void eyedropper_gpencil_color_sample(bContext *C, EyedropperGPencil *eye, int mx, int my) { @@ -307,6 +358,12 @@ static bool eyedropper_gpencil_poll(bContext *C) void UI_OT_eyedropper_gpencil_color(wmOperatorType *ot) { + static const EnumPropertyItem items_mode[] = { + {0, "MATERIAL", 0, "Material", ""}, + {1, "PALETTE", 0, "Palette", ""}, + {0, NULL, 0, NULL, NULL}, + }; + /* identifiers */ ot->name = "Grease Pencil Eyedropper"; ot->idname = "UI_OT_eyedropper_gpencil_color"; @@ -321,4 +378,7 @@ void UI_OT_eyedropper_gpencil_color(wmOperatorType *ot) /* flags */ ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING; + + /* properties */ + ot->prop = RNA_def_enum(ot->srna, "mode", items_mode, 0, "Mode", ""); } |