Blender V5.3
eyedropper_grease_pencil_color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_listbase.hh"
18
19#include "BLT_translation.hh"
20
21#include "DNA_brush_types.h"
22#include "DNA_material_types.h"
23
24#include "BKE_brush.hh"
25#include "BKE_context.hh"
26#include "BKE_grease_pencil.hh"
27#include "BKE_lib_id.hh"
28#include "BKE_material.hh"
29#include "BKE_paint.hh"
30
32
33#include "WM_api.hh"
34#include "WM_types.hh"
35
36#include "RNA_access.hh"
37#include "RNA_define.hh"
38
39#include "ED_screen.hh"
40#include "ED_undo.hh"
41
43
44#include "eyedropper_intern.hh"
45#include "interface_intern.hh"
46
47namespace blender::ui {
48namespace greasepencil {
49
50enum class EyeMode : int8_t {
53 Brush = 2,
54};
55
56enum class MaterialMode : int8_t {
57 Stroke = 0,
58 Fill = 1,
59 Both = 2,
60};
61
75
76/* Helper: Draw status message while the user is running the operator */
78 wmOperator *op,
79 const wmEvent *event)
80{
81 std::string header;
82 header += IFACE_("Current: ");
83
84 const bool is_ctrl = (event->modifier & KM_CTRL) != 0;
85 const bool is_shift = (event->modifier & KM_SHIFT) != 0;
86
88
89 MaterialMode mat_mode = eye->mat_mode;
90 if (is_ctrl && !is_shift) {
91 mat_mode = MaterialMode::Stroke;
92 }
93 if (is_shift && !is_ctrl) {
94 mat_mode = MaterialMode::Fill;
95 }
96 if (is_ctrl && is_shift) {
97 mat_mode = MaterialMode::Both;
98 }
99
100 switch (mat_mode) {
102 header += IFACE_("Stroke");
103 break;
104 }
105 case MaterialMode::Fill: {
106 header += IFACE_("Fill");
107 break;
108 }
109 case MaterialMode::Both: {
110 header += CTX_IFACE_(BLT_I18NCONTEXT_ID_GPENCIL, "Both");
111 break;
112 }
113 }
114
115 header += IFACE_(", Ctrl: Stroke, Shift: Fill, Shift+Ctrl: Both");
116
117 ED_workspace_status_text(C, header.c_str());
118}
119
121{
122 EyedropperGreasePencil *eye = MEM_new<EyedropperGreasePencil>(__func__);
123
124 op->customdata = eye;
125 Scene *scene = CTX_data_scene(C);
126
127 const char *display_device;
128 display_device = scene->display_settings.display_device;
129 eye->display = IMB_colormanagement_display_get_named(display_device);
130
131 eye->accum_start = true;
132 eye->mode = EyeMode(RNA_enum_get(op->ptr, "mode"));
133 eye->mat_mode = MaterialMode(RNA_enum_get(op->ptr, "material_mode"));
134 return true;
135}
136
138{
139 /* Clear status message area. */
140 ED_workspace_status_text(C, nullptr);
141
143
144 MEM_delete<EyedropperGreasePencil>(eye);
145 /* Clear pointer. */
146 op->customdata = nullptr;
147}
148
149static void eyedropper_add_material(bContext *C, const float3 color, const MaterialMode mat_mode)
150{
151 Main *bmain = CTX_data_main(C);
153 Material *ma = nullptr;
154
155 bool found = false;
156
157 /* Look for a similar material in grease pencil slots. */
158 short *totcol = BKE_object_material_len_p(ob);
159 for (short i = 0; i < *totcol; i++) {
160 ma = BKE_object_material_get(ob, i + 1);
161 if (ma == nullptr) {
162 continue;
163 }
164
165 MaterialGPencilStyle *gp_style = ma->gp_style;
166 if (gp_style != nullptr) {
167 /* Check stroke color. */
168 bool found_stroke = compare_v3v3(gp_style->stroke_rgba, color, 0.01f);
169 /* Check fill color. */
170 bool found_fill = compare_v3v3(gp_style->fill_rgba, color, 0.01f);
171
172 if ((mat_mode == MaterialMode::Stroke) && (found_stroke)) {
173 found = true;
174 }
175 else if ((mat_mode == MaterialMode::Fill) && found_fill) {
176 found = true;
177 }
178 else if ((mat_mode == MaterialMode::Both) && found_stroke && found_fill) {
179 found = true;
180 }
181
182 /* Found existing material. */
183 if (found) {
184 ob->actcol = i + 1;
187 return;
188 }
189 }
190 }
191
192 /* If material was not found add a new material with stroke and/or fill color
193 * depending of the secondary key (LMB: Stroke, Shift: Fill, Shift+Ctrl: Stroke/Fill)
194 */
195 int idx;
196 Material *ma_new = BKE_grease_pencil_object_material_new(bmain, ob, "Material", &idx);
200
201 BLI_assert(ma_new != nullptr);
202
203 MaterialGPencilStyle *gp_style_new = ma_new->gp_style;
204 BLI_assert(gp_style_new != nullptr);
205
206 /* Only create Stroke (default option). */
207 if (mat_mode == MaterialMode::Stroke) {
208 /* Stroke color. */
209 copy_v3_v3(gp_style_new->stroke_rgba, color);
210 zero_v4(gp_style_new->fill_rgba);
211 }
212 /* Fill Only. */
213 else if (mat_mode == MaterialMode::Fill) {
214 /* Fill color. */
215 zero_v4(gp_style_new->stroke_rgba);
216 copy_v3_v3(gp_style_new->fill_rgba, color);
217 }
218 /* Stroke and Fill. */
219 else if (mat_mode == MaterialMode::Both) {
220 copy_v3_v3(gp_style_new->stroke_rgba, color);
221 copy_v3_v3(gp_style_new->fill_rgba, color);
222 }
223 /* Push undo for new created material. */
224 ED_undo_push(C, "Add Grease Pencil Material");
225}
226
227/* Create a new palette color and palette if needed. */
229{
230 Main *bmain = CTX_data_main(C);
231 Scene *scene = CTX_data_scene(C);
232 ToolSettings *ts = scene->toolsettings;
233 GpPaint *gp_paint = ts->gp_paint;
234 GpVertexPaint *gp_vertexpaint = ts->gp_vertexpaint;
235 Paint *paint = &gp_paint->paint;
236 Paint *vertexpaint = &gp_vertexpaint->paint;
237
238 /* Check for Palette in Draw and Vertex Paint Mode. */
239 if (paint->palette == nullptr) {
240 Palette *palette = BKE_palette_add(bmain, "Grease Pencil");
241 id_us_min(&palette->id);
242
244
245 if (vertexpaint->palette == nullptr) {
246 BKE_paint_palette_set(vertexpaint, palette);
247 }
248 }
249
250 /* Check if the color exist already. */
251 Palette *palette = paint->palette;
252
253 for (const auto [i, palcolor] : palette->colors.enumerate()) {
254 if (compare_v3v3(palcolor.color, color, 0.01f)) {
255 palette->active_color = i;
256 return;
257 }
258 }
259
260 /* Create Colors. */
261 PaletteColor *palcol = BKE_palette_color_add(palette);
262 if (palcol) {
263 palette->active_color = palette->colors.count() - 1;
265 }
266}
267
268/* Set the active brush's color. */
270{
271 Scene *scene = CTX_data_scene(C);
272 ToolSettings *ts = scene->toolsettings;
273 Paint *paint = &ts->gp_paint->paint;
275 if (brush == nullptr) {
276 return;
277 }
278
282}
283
284/* Set the material or the palette color. */
286 const wmEvent *event,
288{
289 const bool is_ctrl = (event->modifier & KM_CTRL) != 0;
290 const bool is_shift = (event->modifier & KM_SHIFT) != 0;
291
292 MaterialMode mat_mode = eye->mat_mode;
293 if (is_ctrl && !is_shift) {
294 mat_mode = MaterialMode::Stroke;
295 }
296 if (is_shift && !is_ctrl) {
297 mat_mode = MaterialMode::Fill;
298 }
299 if (is_ctrl && is_shift) {
300 mat_mode = MaterialMode::Both;
301 }
302
303 switch (eye->mode) {
305 eyedropper_add_material(C, eye->color, mat_mode);
306 break;
307 case EyeMode::Palette:
309 break;
310 case EyeMode::Brush:
312 break;
313 }
314}
315
316/* Sample the color below cursor. */
319 const int m_xy[2])
320{
321 /* Accumulate color. */
322 float3 col;
323 eyedropper_color_sample_fl(C, nullptr, m_xy, col);
324
325 eye->accum_col += col;
326 eye->accum_tot++;
327
328 eye->color = eye->accum_col;
329 if (eye->accum_tot > 1) {
330 eye->color = eye->accum_col / float(eye->accum_tot);
331 }
332}
333
338
339/* Main modal status check. */
341 wmOperator *op,
342 const wmEvent *event)
343{
346
347 /* Handle modal keymap */
348 switch (event->type) {
349 case EVT_MODAL_MAP: {
350 switch (event->val) {
352 /* enable accum and make first sample */
353 eye->accum_start = true;
355 break;
357 eye->accum_tot = 0;
358 eye->accum_col = float3(0.0f, 0.0f, 0.0f);
360 break;
361 case EYE_MODAL_CANCEL: {
363 return OPERATOR_CANCELLED;
364 }
367
368 /* Create material. */
371
373 return OPERATOR_FINISHED;
374 }
375 default: {
376 break;
377 }
378 }
379 break;
380 }
381 case MOUSEMOVE:
382 case INBETWEEN_MOUSEMOVE: {
383 if (eye->accum_start) {
384 /* button is pressed so keep sampling */
386 }
387 break;
388 }
389 default: {
390 break;
391 }
392 }
393
395}
396
398 wmOperator *op,
399 const wmEvent *event)
400{
402 /* Add modal temp handler. */
404 /* Status message. */
406
408 }
410}
411
412/* Repeat operator */
414{
416
417 /* cleanup */
419
420 return OPERATOR_FINISHED;
421 }
423}
424
426{
427 /* Only valid if the current active object is grease pencil. */
429 if ((obact == nullptr) || (obact->type != OB_GREASE_PENCIL)) {
430 return false;
431 }
432
433 /* Test we have a window below. */
434 return (CTX_wm_window(C) != nullptr);
435}
436} // namespace greasepencil
437
439{
440 static const EnumPropertyItem items_mode[] = {
441 {int(greasepencil::EyeMode::Material), "MATERIAL", 0, "Material", ""},
442 {int(greasepencil::EyeMode::Palette), "PALETTE", 0, "Palette", ""},
443 {int(greasepencil::EyeMode::Brush), "BRUSH", 0, "Brush", ""},
444 {0, nullptr, 0, nullptr, nullptr},
445 };
446
447 static const EnumPropertyItem items_material_mode[] = {
448 {int(greasepencil::MaterialMode::Stroke), "STROKE", 0, "Stroke", ""},
449 {int(greasepencil::MaterialMode::Fill), "FILL", 0, "Fill", ""},
450 {int(greasepencil::MaterialMode::Both), "BOTH", 0, "Both", ""},
451 {0, nullptr, 0, nullptr, nullptr},
452 };
453
454 /* Identifiers. */
455 ot->name = "Grease Pencil Eyedropper";
456 ot->idname = "UI_OT_eyedropper_grease_pencil_color";
457 ot->description = "Sample a color from the Blender Window and create Grease Pencil material";
458
459 /* API callbacks. */
465
466 /* Flags. */
468
469 /* Properties. */
470 ot->prop = RNA_def_enum(
471 ot->srna, "mode", items_mode, int(greasepencil::EyeMode::Material), "Mode", "");
472 ot->prop = RNA_def_enum(ot->srna,
473 "material_mode",
474 items_material_mode,
476 "Material Mode",
477 "");
479}
480
481} // namespace blender::ui
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define BLT_I18NCONTEXT_ID_GPENCIL
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_DATA
Definition WM_types.hh:552
#define NA_EDITED
Definition WM_types.hh:634
#define NC_MATERIAL
Definition WM_types.hh:393
#define NC_GPENCIL
Definition WM_types.hh:412
#define ND_OB_SHADING
Definition WM_types.hh:476
#define ND_SPACE_VIEW3D
Definition WM_types.hh:571
#define NC_OBJECT
Definition WM_types.hh:392
#define ND_SHADING_LINKS
Definition WM_types.hh:522
#define NC_SPACE
Definition WM_types.hh:405
static void eyedropper_add_palette_color(bContext *C, const float3 color)
static bool eyedropper_grease_pencil_poll(bContext *C)
static wmOperatorStatus eyedropper_grease_pencil_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void eyedropper_grease_pencil_color_sample(bContext *C, EyedropperGreasePencil *eye, const int m_xy[2])
static void eyedropper_grease_pencil_exit(bContext *C, wmOperator *op)
static bool eyedropper_grease_pencil_init(bContext *C, wmOperator *op)
static void eyedropper_grease_pencil_status_indicators(bContext *C, wmOperator *op, const wmEvent *event)
static void eyedropper_grease_pencil_cancel(bContext *C, wmOperator *op)
static void eyedropper_set_brush_color(bContext *C, const float3 &color)
static wmOperatorStatus eyedropper_grease_pencil_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void eyedropper_add_material(bContext *C, const float3 color, const MaterialMode mat_mode)
static wmOperatorStatus eyedropper_grease_pencil_exec(bContext *C, wmOperator *op)
static void eyedropper_grease_pencil_color_set(bContext *C, const wmEvent *event, EyedropperGreasePencil *eye)
bool eyedropper_color_sample_fl(bContext *C, Eyedropper *eye, const int event_xy[2], float r_col[3])
get the color from the screen.
void UI_OT_eyedropper_grease_pencil_color(wmOperatorType *ot)
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_palette_color_set(PaletteColor *color, const float rgb[3])
int RNA_enum_get(PointerRNA *ptr, const char *name)
MINLINE void copy_v3_v3(float r[3], const float a[3])
short * BKE_object_material_len_p(Object *ob)
Material * BKE_object_material_get(Object *ob, short act)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
wmWindow * CTX_wm_window(const bContext *C)
@ INBETWEEN_MOUSEMOVE
void BKE_brush_tag_unsaved_changes(Brush *brush)
Definition brush.cc:843
MINLINE void zero_v4(float r[4])
Material * BKE_grease_pencil_object_material_new(Main *bmain, Object *ob, const char *name, int *r_index)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
void BKE_brush_color_set(Paint *paint, Brush *brush, const float3 &color)
Definition brush.cc:1305
void BKE_brush_color_sync_legacy(Brush *brush)
Definition brush.cc:1319
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
PaletteColor * BKE_palette_color_add(Palette *palette)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
Main * CTX_data_main(const bContext *C)
Brush * BKE_paint_brush(Paint *paint)
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
const ColorManagedDisplay * IMB_colormanagement_display_get_named(const char *name)
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
VecBase< float, 3 > float3
Object * CTX_data_active_object(const bContext *C)
void id_us_min(ID *id)
Definition lib_id.cc:441
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
void BKE_paint_palette_set(Paint *paint, Palette *palette)
signed char int8_t
Definition BKE_text.h:76
ocio::Display ColorManagedDisplay
Palette * BKE_palette_add(Main *bmain, const char *name)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct Palette * palette
ListBaseT< PaletteColor > colors
ColorManagedDisplaySettings display_settings
struct ToolSettings * toolsettings
GpVertexPaint * gp_vertexpaint
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157