1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup edsculpt
19  */
20 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLI_listbase.h"
24 #include "BLI_math_vector.h"
25 #include "BLI_string.h"
26 #include "BLI_utildefines.h"
27 #include <stdlib.h>
28 
29 #include "IMB_imbuf.h"
30 #include "IMB_imbuf_types.h"
31 
32 #include "DNA_brush_types.h"
33 #include "DNA_customdata_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
36 
37 #include "BKE_brush.h"
38 #include "BKE_context.h"
39 #include "BKE_image.h"
40 #include "BKE_lib_id.h"
41 #include "BKE_main.h"
42 #include "BKE_paint.h"
43 #include "BKE_report.h"
44 
45 #include "ED_image.h"
46 #include "ED_paint.h"
47 #include "ED_screen.h"
48 
49 #include "WM_api.h"
50 #include "WM_toolsystem.h"
51 #include "WM_types.h"
52 
53 #include "RNA_access.h"
54 #include "RNA_define.h"
55 
56 #include "paint_intern.h"
57 #include "sculpt_intern.h"
58 
59 #include <stddef.h>
60 #include <string.h>
61 
62 /* Brush operators */
brush_add_exec(bContext * C,wmOperator * UNUSED (op))63 static int brush_add_exec(bContext *C, wmOperator *UNUSED(op))
64 {
65   /*int type = RNA_enum_get(op->ptr, "type");*/
66   Paint *paint = BKE_paint_get_active_from_context(C);
67   Brush *br = BKE_paint_brush(paint);
68   Main *bmain = CTX_data_main(C);
69   ePaintMode mode = BKE_paintmode_get_active_from_context(C);
70 
71   if (br) {
72     br = (Brush *)BKE_id_copy(bmain, &br->id);
73   }
74   else {
75     br = BKE_brush_add(bmain, "Brush", BKE_paint_object_mode_from_paintmode(mode));
76   }
77   id_us_min(&br->id); /* fake user only */
78 
79   BKE_paint_brush_set(paint, br);
80 
81   return OPERATOR_FINISHED;
82 }
83 
BRUSH_OT_add(wmOperatorType * ot)84 static void BRUSH_OT_add(wmOperatorType *ot)
85 {
86   /* identifiers */
87   ot->name = "Add Brush";
88   ot->description = "Add brush by mode type";
89   ot->idname = "BRUSH_OT_add";
90 
91   /* api callbacks */
92   ot->exec = brush_add_exec;
93 
94   /* flags */
95   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
96 }
97 
brush_add_gpencil_exec(bContext * C,wmOperator * UNUSED (op))98 static int brush_add_gpencil_exec(bContext *C, wmOperator *UNUSED(op))
99 {
100   /*int type = RNA_enum_get(op->ptr, "type");*/
101   ToolSettings *ts = CTX_data_tool_settings(C);
102   Paint *paint = &ts->gp_paint->paint;
103   Brush *br = BKE_paint_brush(paint);
104   Main *bmain = CTX_data_main(C);
105 
106   if (br) {
107     br = (Brush *)BKE_id_copy(bmain, &br->id);
108   }
109   else {
110     br = BKE_brush_add(bmain, "Brush", OB_MODE_PAINT_GPENCIL);
111 
112     /* Init grease pencil specific data. */
113     BKE_brush_init_gpencil_settings(br);
114   }
115 
116   id_us_min(&br->id); /* fake user only */
117 
118   BKE_paint_brush_set(paint, br);
119 
120   return OPERATOR_FINISHED;
121 }
122 
BRUSH_OT_add_gpencil(wmOperatorType * ot)123 static void BRUSH_OT_add_gpencil(wmOperatorType *ot)
124 {
125   /* identifiers */
126   ot->name = "Add Drawing Brush";
127   ot->description = "Add brush for Grease Pencil";
128   ot->idname = "BRUSH_OT_add_gpencil";
129 
130   /* api callbacks */
131   ot->exec = brush_add_gpencil_exec;
132 
133   /* flags */
134   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
135 }
136 
brush_scale_size_exec(bContext * C,wmOperator * op)137 static int brush_scale_size_exec(bContext *C, wmOperator *op)
138 {
139   Scene *scene = CTX_data_scene(C);
140   Paint *paint = BKE_paint_get_active_from_context(C);
141   Brush *brush = BKE_paint_brush(paint);
142   // Object *ob = CTX_data_active_object(C);
143   float scalar = RNA_float_get(op->ptr, "scalar");
144 
145   if (brush) {
146     /* pixel radius */
147     {
148       const int old_size = BKE_brush_size_get(scene, brush);
149       int size = (int)(scalar * old_size);
150 
151       if (abs(old_size - size) < U.pixelsize) {
152         if (scalar > 1) {
153           size += U.pixelsize;
154         }
155         else if (scalar < 1) {
156           size -= U.pixelsize;
157         }
158       }
159 
160       BKE_brush_size_set(scene, brush, size);
161     }
162 
163     /* unprojected radius */
164     {
165       float unprojected_radius = scalar * BKE_brush_unprojected_radius_get(scene, brush);
166 
167       if (unprojected_radius < 0.001f) { /* XXX magic number */
168         unprojected_radius = 0.001f;
169       }
170 
171       BKE_brush_unprojected_radius_set(scene, brush, unprojected_radius);
172     }
173 
174     WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
175   }
176 
177   return OPERATOR_FINISHED;
178 }
179 
BRUSH_OT_scale_size(wmOperatorType * ot)180 static void BRUSH_OT_scale_size(wmOperatorType *ot)
181 {
182   /* identifiers */
183   ot->name = "Scale Sculpt/Paint Brush Size";
184   ot->description = "Change brush size by a scalar";
185   ot->idname = "BRUSH_OT_scale_size";
186 
187   /* api callbacks */
188   ot->exec = brush_scale_size_exec;
189 
190   /* flags */
191   ot->flag = 0;
192 
193   RNA_def_float(ot->srna, "scalar", 1, 0, 2, "Scalar", "Factor to scale brush size by", 0, 2);
194 }
195 
196 /* Palette operators */
197 
palette_new_exec(bContext * C,wmOperator * UNUSED (op))198 static int palette_new_exec(bContext *C, wmOperator *UNUSED(op))
199 {
200   Paint *paint = BKE_paint_get_active_from_context(C);
201   Main *bmain = CTX_data_main(C);
202   Palette *palette;
203 
204   palette = BKE_palette_add(bmain, "Palette");
205 
206   BKE_paint_palette_set(paint, palette);
207 
208   return OPERATOR_FINISHED;
209 }
210 
PALETTE_OT_new(wmOperatorType * ot)211 static void PALETTE_OT_new(wmOperatorType *ot)
212 {
213   /* identifiers */
214   ot->name = "Add New Palette";
215   ot->description = "Add new palette";
216   ot->idname = "PALETTE_OT_new";
217 
218   /* api callbacks */
219   ot->exec = palette_new_exec;
220 
221   /* flags */
222   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
223 }
224 
palette_poll(bContext * C)225 static bool palette_poll(bContext *C)
226 {
227   Paint *paint = BKE_paint_get_active_from_context(C);
228 
229   if (paint && paint->palette != NULL) {
230     return true;
231   }
232 
233   return false;
234 }
235 
palette_color_add_exec(bContext * C,wmOperator * UNUSED (op))236 static int palette_color_add_exec(bContext *C, wmOperator *UNUSED(op))
237 {
238   Scene *scene = CTX_data_scene(C);
239   Paint *paint = BKE_paint_get_active_from_context(C);
240   Brush *brush = paint->brush;
241   ePaintMode mode = BKE_paintmode_get_active_from_context(C);
242   Palette *palette = paint->palette;
243   PaletteColor *color;
244 
245   color = BKE_palette_color_add(palette);
246   palette->active_color = BLI_listbase_count(&palette->colors) - 1;
247 
248   if (ELEM(mode,
249            PAINT_MODE_TEXTURE_3D,
250            PAINT_MODE_TEXTURE_2D,
251            PAINT_MODE_VERTEX,
252            PAINT_MODE_SCULPT)) {
253     copy_v3_v3(color->rgb, BKE_brush_color_get(scene, brush));
254     color->value = 0.0;
255   }
256   else if (mode == PAINT_MODE_WEIGHT) {
257     zero_v3(color->rgb);
258     color->value = brush->weight;
259   }
260 
261   return OPERATOR_FINISHED;
262 }
263 
PALETTE_OT_color_add(wmOperatorType * ot)264 static void PALETTE_OT_color_add(wmOperatorType *ot)
265 {
266   /* identifiers */
267   ot->name = "New Palette Color";
268   ot->description = "Add new color to active palette";
269   ot->idname = "PALETTE_OT_color_add";
270 
271   /* api callbacks */
272   ot->exec = palette_color_add_exec;
273   ot->poll = palette_poll;
274   /* flags */
275   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
276 }
277 
palette_color_delete_exec(bContext * C,wmOperator * UNUSED (op))278 static int palette_color_delete_exec(bContext *C, wmOperator *UNUSED(op))
279 {
280   Paint *paint = BKE_paint_get_active_from_context(C);
281   Palette *palette = paint->palette;
282   PaletteColor *color = BLI_findlink(&palette->colors, palette->active_color);
283 
284   if (color) {
285     BKE_palette_color_remove(palette, color);
286   }
287 
288   return OPERATOR_FINISHED;
289 }
290 
PALETTE_OT_color_delete(wmOperatorType * ot)291 static void PALETTE_OT_color_delete(wmOperatorType *ot)
292 {
293   /* identifiers */
294   ot->name = "Delete Palette Color";
295   ot->description = "Remove active color from palette";
296   ot->idname = "PALETTE_OT_color_delete";
297 
298   /* api callbacks */
299   ot->exec = palette_color_delete_exec;
300   ot->poll = palette_poll;
301   /* flags */
302   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
303 }
304 
305 /* --- Extract Palette from Image. */
palette_extract_img_poll(bContext * C)306 static bool palette_extract_img_poll(bContext *C)
307 {
308   SpaceLink *sl = CTX_wm_space_data(C);
309   if ((sl != NULL) && (sl->spacetype == SPACE_IMAGE)) {
310     SpaceImage *sima = CTX_wm_space_image(C);
311     Image *image = sima->image;
312     ImageUser iuser = sima->iuser;
313     return BKE_image_has_ibuf(image, &iuser);
314   }
315 
316   return false;
317 }
318 
palette_extract_img_exec(bContext * C,wmOperator * op)319 static int palette_extract_img_exec(bContext *C, wmOperator *op)
320 {
321   const int threshold = RNA_int_get(op->ptr, "threshold");
322 
323   Main *bmain = CTX_data_main(C);
324   bool done = false;
325 
326   SpaceImage *sima = CTX_wm_space_image(C);
327   Image *image = sima->image;
328   ImageUser iuser = sima->iuser;
329   void *lock;
330   ImBuf *ibuf;
331   GHash *color_table = BLI_ghash_int_new(__func__);
332 
333   ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
334 
335   if (ibuf && ibuf->rect) {
336     /* Extract all colors. */
337     const int range = (int)pow(10.0f, threshold);
338     for (int row = 0; row < ibuf->y; row++) {
339       for (int col = 0; col < ibuf->x; col++) {
340         float color[4];
341         IMB_sampleImageAtLocation(ibuf, (float)col, (float)row, false, color);
342         for (int i = 0; i < 3; i++) {
343           color[i] = truncf(color[i] * range) / range;
344         }
345 
346         uint key = rgb_to_cpack(color[0], color[1], color[2]);
347         if (!BLI_ghash_haskey(color_table, POINTER_FROM_INT(key))) {
348           BLI_ghash_insert(color_table, POINTER_FROM_INT(key), POINTER_FROM_INT(key));
349         }
350       }
351     }
352 
353     done = BKE_palette_from_hash(bmain, color_table, image->id.name + 2, false);
354   }
355 
356   /* Free memory. */
357   BLI_ghash_free(color_table, NULL, NULL);
358   BKE_image_release_ibuf(image, ibuf, lock);
359 
360   if (done) {
361     BKE_reportf(op->reports, RPT_INFO, "Palette created");
362   }
363 
364   return OPERATOR_FINISHED;
365 }
366 
PALETTE_OT_extract_from_image(wmOperatorType * ot)367 static void PALETTE_OT_extract_from_image(wmOperatorType *ot)
368 {
369   PropertyRNA *prop;
370 
371   /* identifiers */
372   ot->name = "Extract Palette from Image";
373   ot->idname = "PALETTE_OT_extract_from_image";
374   ot->description = "Extract all colors used in Image and create a Palette";
375 
376   /* api callbacks */
377   ot->exec = palette_extract_img_exec;
378   ot->poll = palette_extract_img_poll;
379 
380   /* flags */
381   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
382 
383   /* properties */
384   prop = RNA_def_int(ot->srna, "threshold", 1, 1, 1, "Threshold", "", 1, 1);
385   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
386 }
387 
388 /* Sort Palette color by Hue and Saturation. */
palette_sort_exec(bContext * C,wmOperator * op)389 static int palette_sort_exec(bContext *C, wmOperator *op)
390 {
391   const int type = RNA_enum_get(op->ptr, "type");
392 
393   Paint *paint = BKE_paint_get_active_from_context(C);
394   Palette *palette = paint->palette;
395 
396   if (palette == NULL) {
397     return OPERATOR_CANCELLED;
398   }
399 
400   tPaletteColorHSV *color_array = NULL;
401   tPaletteColorHSV *col_elm = NULL;
402 
403   const int totcol = BLI_listbase_count(&palette->colors);
404 
405   if (totcol > 0) {
406     color_array = MEM_calloc_arrayN(totcol, sizeof(tPaletteColorHSV), __func__);
407     /* Put all colors in an array. */
408     int t = 0;
409     LISTBASE_FOREACH (PaletteColor *, color, &palette->colors) {
410       float h, s, v;
411       rgb_to_hsv(color->rgb[0], color->rgb[1], color->rgb[2], &h, &s, &v);
412       col_elm = &color_array[t];
413       copy_v3_v3(col_elm->rgb, color->rgb);
414       col_elm->value = color->value;
415       col_elm->h = h;
416       col_elm->s = s;
417       col_elm->v = v;
418       t++;
419     }
420     /* Sort */
421     if (type == 1) {
422       BKE_palette_sort_hsv(color_array, totcol);
423     }
424     else if (type == 2) {
425       BKE_palette_sort_svh(color_array, totcol);
426     }
427     else if (type == 3) {
428       BKE_palette_sort_vhs(color_array, totcol);
429     }
430     else {
431       BKE_palette_sort_luminance(color_array, totcol);
432     }
433 
434     /* Clear old color swatches. */
435     PaletteColor *color_next = NULL;
436     for (PaletteColor *color = palette->colors.first; color; color = color_next) {
437       color_next = color->next;
438       BKE_palette_color_remove(palette, color);
439     }
440 
441     /* Recreate swatches sorted. */
442     for (int i = 0; i < totcol; i++) {
443       col_elm = &color_array[i];
444       PaletteColor *palcol = BKE_palette_color_add(palette);
445       if (palcol) {
446         copy_v3_v3(palcol->rgb, col_elm->rgb);
447       }
448     }
449   }
450 
451   /* Free memory. */
452   if (totcol > 0) {
453     MEM_SAFE_FREE(color_array);
454   }
455 
456   WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, NULL);
457 
458   return OPERATOR_FINISHED;
459 }
460 
PALETTE_OT_sort(wmOperatorType * ot)461 static void PALETTE_OT_sort(wmOperatorType *ot)
462 {
463   static const EnumPropertyItem sort_type[] = {
464       {1, "HSV", 0, "Hue, Saturation, Value", ""},
465       {2, "SVH", 0, "Saturation, Value, Hue", ""},
466       {3, "VHS", 0, "Value, Hue, Saturation", ""},
467       {4, "LUMINANCE", 0, "Luminance", ""},
468       {0, NULL, 0, NULL, NULL},
469   };
470 
471   /* identifiers */
472   ot->name = "Sort Palette";
473   ot->idname = "PALETTE_OT_sort";
474   ot->description = "Sort Palette Colors";
475 
476   /* api callbacks */
477   ot->exec = palette_sort_exec;
478   ot->poll = palette_poll;
479 
480   /* flags */
481   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
482 
483   ot->prop = RNA_def_enum(ot->srna, "type", sort_type, 1, "Type", "");
484 }
485 
486 /* Move colors in palette. */
palette_color_move_exec(bContext * C,wmOperator * op)487 static int palette_color_move_exec(bContext *C, wmOperator *op)
488 {
489   Paint *paint = BKE_paint_get_active_from_context(C);
490   Palette *palette = paint->palette;
491   PaletteColor *palcolor = BLI_findlink(&palette->colors, palette->active_color);
492 
493   if (palcolor == NULL) {
494     return OPERATOR_CANCELLED;
495   }
496 
497   const int direction = RNA_enum_get(op->ptr, "type");
498 
499   BLI_assert(ELEM(direction, -1, 0, 1)); /* we use value below */
500   if (BLI_listbase_link_move(&palette->colors, palcolor, direction)) {
501     palette->active_color += direction;
502     WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, NULL);
503   }
504 
505   return OPERATOR_FINISHED;
506 }
507 
PALETTE_OT_color_move(wmOperatorType * ot)508 static void PALETTE_OT_color_move(wmOperatorType *ot)
509 {
510   static const EnumPropertyItem slot_move[] = {
511       {-1, "UP", 0, "Up", ""},
512       {1, "DOWN", 0, "Down", ""},
513       {0, NULL, 0, NULL, NULL},
514   };
515 
516   /* identifiers */
517   ot->name = "Move Palette Color";
518   ot->idname = "PALETTE_OT_color_move";
519   ot->description = "Move the active Color up/down in the list";
520 
521   /* api callbacks */
522   ot->exec = palette_color_move_exec;
523   ot->poll = palette_poll;
524 
525   /* flags */
526   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
527 
528   ot->prop = RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
529 }
530 
531 /* Join Palette swatches. */
palette_join_exec(bContext * C,wmOperator * op)532 static int palette_join_exec(bContext *C, wmOperator *op)
533 {
534   Main *bmain = CTX_data_main(C);
535   Paint *paint = BKE_paint_get_active_from_context(C);
536   Palette *palette = paint->palette;
537   Palette *palette_join = NULL;
538   bool done = false;
539 
540   char name[MAX_ID_NAME - 2];
541   RNA_string_get(op->ptr, "palette", name);
542 
543   if ((palette == NULL) || (name[0] == '\0')) {
544     return OPERATOR_CANCELLED;
545   }
546 
547   palette_join = (Palette *)BKE_libblock_find_name(bmain, ID_PAL, name);
548   if (palette_join == NULL) {
549     return OPERATOR_CANCELLED;
550   }
551 
552   const int totcol = BLI_listbase_count(&palette_join->colors);
553 
554   if (totcol > 0) {
555     LISTBASE_FOREACH (PaletteColor *, color, &palette_join->colors) {
556       PaletteColor *palcol = BKE_palette_color_add(palette);
557       if (palcol) {
558         copy_v3_v3(palcol->rgb, color->rgb);
559         palcol->value = color->value;
560         done = true;
561       }
562     }
563   }
564 
565   if (done) {
566     /* Clear old color swatches. */
567     PaletteColor *color_next = NULL;
568     for (PaletteColor *color = palette_join->colors.first; color; color = color_next) {
569       color_next = color->next;
570       BKE_palette_color_remove(palette_join, color);
571     }
572 
573     /* Notifier. */
574     WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, NULL);
575   }
576 
577   return OPERATOR_FINISHED;
578 }
579 
PALETTE_OT_join(wmOperatorType * ot)580 static void PALETTE_OT_join(wmOperatorType *ot)
581 {
582   /* identifiers */
583   ot->name = "Join Palette Swatches";
584   ot->idname = "PALETTE_OT_join";
585   ot->description = "Join Palette Swatches";
586 
587   /* api callbacks */
588   ot->exec = palette_join_exec;
589   ot->poll = palette_poll;
590 
591   /* flags */
592   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
593 
594   /* properties */
595   RNA_def_string(ot->srna, "palette", NULL, MAX_ID_NAME - 2, "Palette", "Name of the Palette");
596 }
597 
brush_reset_exec(bContext * C,wmOperator * UNUSED (op))598 static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op))
599 {
600   Paint *paint = BKE_paint_get_active_from_context(C);
601   Brush *brush = BKE_paint_brush(paint);
602   Object *ob = CTX_data_active_object(C);
603 
604   if (!ob || !brush) {
605     return OPERATOR_CANCELLED;
606   }
607 
608   /* TODO: other modes */
609   if (ob->mode & OB_MODE_SCULPT) {
610     BKE_brush_sculpt_reset(brush);
611   }
612   else {
613     return OPERATOR_CANCELLED;
614   }
615   WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
616 
617   return OPERATOR_FINISHED;
618 }
619 
BRUSH_OT_reset(wmOperatorType * ot)620 static void BRUSH_OT_reset(wmOperatorType *ot)
621 {
622   /* identifiers */
623   ot->name = "Reset Brush";
624   ot->description = "Return brush to defaults based on current tool";
625   ot->idname = "BRUSH_OT_reset";
626 
627   /* api callbacks */
628   ot->exec = brush_reset_exec;
629 
630   /* flags */
631   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
632 }
633 
brush_tool(const Brush * brush,size_t tool_offset)634 static int brush_tool(const Brush *brush, size_t tool_offset)
635 {
636   return *(((char *)brush) + tool_offset);
637 }
638 
brush_tool_set(const Brush * brush,size_t tool_offset,int tool)639 static void brush_tool_set(const Brush *brush, size_t tool_offset, int tool)
640 {
641   *(((char *)brush) + tool_offset) = tool;
642 }
643 
brush_tool_cycle(Main * bmain,Paint * paint,Brush * brush_orig,const int tool)644 static Brush *brush_tool_cycle(Main *bmain, Paint *paint, Brush *brush_orig, const int tool)
645 {
646   Brush *brush, *first_brush;
647 
648   if (!brush_orig && !(brush_orig = bmain->brushes.first)) {
649     return NULL;
650   }
651 
652   if (brush_tool(brush_orig, paint->runtime.tool_offset) != tool) {
653     /* If current brush's tool is different from what we need,
654      * start cycling from the beginning of the list.
655      * Such logic will activate the same exact brush not relating from
656      * which tool user requests other tool.
657      */
658 
659     /* Try to tool-slot first. */
660     first_brush = BKE_paint_toolslots_brush_get(paint, tool);
661     if (first_brush == NULL) {
662       first_brush = bmain->brushes.first;
663     }
664   }
665   else {
666     /* If user wants to switch to brush with the same  tool as
667      * currently active brush do a cycling via all possible
668      * brushes with requested tool.
669      */
670     first_brush = brush_orig->id.next ? brush_orig->id.next : bmain->brushes.first;
671   }
672 
673   /* get the next brush with the active tool */
674   brush = first_brush;
675   do {
676     if ((brush->ob_mode & paint->runtime.ob_mode) &&
677         (brush_tool(brush, paint->runtime.tool_offset) == tool)) {
678       return brush;
679     }
680 
681     brush = brush->id.next ? brush->id.next : bmain->brushes.first;
682   } while (brush != first_brush);
683 
684   return NULL;
685 }
686 
brush_tool_toggle(Main * bmain,Paint * paint,Brush * brush_orig,const int tool)687 static Brush *brush_tool_toggle(Main *bmain, Paint *paint, Brush *brush_orig, const int tool)
688 {
689   if (!brush_orig || brush_tool(brush_orig, paint->runtime.tool_offset) != tool) {
690     Brush *br;
691     /* if the current brush is not using the desired tool, look
692      * for one that is */
693     br = brush_tool_cycle(bmain, paint, brush_orig, tool);
694     /* store the previously-selected brush */
695     if (br) {
696       br->toggle_brush = brush_orig;
697     }
698 
699     return br;
700   }
701   if (brush_orig->toggle_brush) {
702     /* if current brush is using the desired tool, try to toggle
703      * back to the previously selected brush. */
704     return brush_orig->toggle_brush;
705   }
706   return NULL;
707 }
708 
brush_generic_tool_set(bContext * C,Main * bmain,Paint * paint,const int tool,const char * tool_name,const bool create_missing,const bool toggle)709 static bool brush_generic_tool_set(bContext *C,
710                                    Main *bmain,
711                                    Paint *paint,
712                                    const int tool,
713                                    const char *tool_name,
714                                    const bool create_missing,
715                                    const bool toggle)
716 {
717   Brush *brush, *brush_orig = BKE_paint_brush(paint);
718 
719   if (toggle) {
720     brush = brush_tool_toggle(bmain, paint, brush_orig, tool);
721   }
722   else {
723     brush = brush_tool_cycle(bmain, paint, brush_orig, tool);
724   }
725 
726   if (((brush == NULL) && create_missing) &&
727       ((brush_orig == NULL) || brush_tool(brush_orig, paint->runtime.tool_offset) != tool)) {
728     brush = BKE_brush_add(bmain, tool_name, paint->runtime.ob_mode);
729     id_us_min(&brush->id); /* fake user only */
730     brush_tool_set(brush, paint->runtime.tool_offset, tool);
731     brush->toggle_brush = brush_orig;
732   }
733 
734   if (brush) {
735     BKE_paint_brush_set(paint, brush);
736     BKE_paint_invalidate_overlay_all();
737 
738     WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
739 
740     /* Tool System
741      * This is needed for when there is a non-sculpt tool active (transform for e.g.).
742      * In case we are toggling (and the brush changed to the toggle_brush), we need to get the
743      * tool_name again. */
744     int tool_result = brush_tool(brush, paint->runtime.tool_offset);
745     ePaintMode paint_mode = BKE_paintmode_get_active_from_context(C);
746     const EnumPropertyItem *items = BKE_paint_get_tool_enum_from_paintmode(paint_mode);
747     RNA_enum_name_from_value(items, tool_result, &tool_name);
748 
749     char tool_id[MAX_NAME];
750     SNPRINTF(tool_id, "builtin_brush.%s", tool_name);
751     WM_toolsystem_ref_set_by_id(C, tool_id);
752 
753     return true;
754   }
755   return false;
756 }
757 
758 static const ePaintMode brush_select_paint_modes[] = {
759     PAINT_MODE_SCULPT,
760     PAINT_MODE_VERTEX,
761     PAINT_MODE_WEIGHT,
762     PAINT_MODE_TEXTURE_3D,
763     PAINT_MODE_GPENCIL,
764     PAINT_MODE_VERTEX_GPENCIL,
765     PAINT_MODE_SCULPT_GPENCIL,
766     PAINT_MODE_WEIGHT_GPENCIL,
767 };
768 
brush_select_exec(bContext * C,wmOperator * op)769 static int brush_select_exec(bContext *C, wmOperator *op)
770 {
771   Main *bmain = CTX_data_main(C);
772   Scene *scene = CTX_data_scene(C);
773   const bool create_missing = RNA_boolean_get(op->ptr, "create_missing");
774   const bool toggle = RNA_boolean_get(op->ptr, "toggle");
775   const char *tool_name = "Brush";
776   int tool = 0;
777 
778   ePaintMode paint_mode = PAINT_MODE_INVALID;
779   for (int i = 0; i < ARRAY_SIZE(brush_select_paint_modes); i++) {
780     paint_mode = brush_select_paint_modes[i];
781     const char *op_prop_id = BKE_paint_get_tool_prop_id_from_paintmode(paint_mode);
782     PropertyRNA *prop = RNA_struct_find_property(op->ptr, op_prop_id);
783     if (RNA_property_is_set(op->ptr, prop)) {
784       tool = RNA_property_enum_get(op->ptr, prop);
785       break;
786     }
787   }
788 
789   if (paint_mode == PAINT_MODE_INVALID) {
790     return OPERATOR_CANCELLED;
791   }
792 
793   Paint *paint = BKE_paint_get_active_from_paintmode(scene, paint_mode);
794   if (paint == NULL) {
795     return OPERATOR_CANCELLED;
796   }
797   const EnumPropertyItem *items = BKE_paint_get_tool_enum_from_paintmode(paint_mode);
798   RNA_enum_name_from_value(items, tool, &tool_name);
799 
800   if (brush_generic_tool_set(C, bmain, paint, tool, tool_name, create_missing, toggle)) {
801     return OPERATOR_FINISHED;
802   }
803   return OPERATOR_CANCELLED;
804 }
805 
PAINT_OT_brush_select(wmOperatorType * ot)806 static void PAINT_OT_brush_select(wmOperatorType *ot)
807 {
808   PropertyRNA *prop;
809 
810   /* identifiers */
811   ot->name = "Brush Select";
812   ot->description = "Select a paint mode's brush by tool type";
813   ot->idname = "PAINT_OT_brush_select";
814 
815   /* api callbacks */
816   ot->exec = brush_select_exec;
817 
818   /* flags */
819   ot->flag = 0;
820 
821   /* props */
822   /* All properties are hidden, so as not to show the redo panel. */
823   for (int i = 0; i < ARRAY_SIZE(brush_select_paint_modes); i++) {
824     const ePaintMode paint_mode = brush_select_paint_modes[i];
825     const char *prop_id = BKE_paint_get_tool_prop_id_from_paintmode(paint_mode);
826     prop = RNA_def_enum(
827         ot->srna, prop_id, BKE_paint_get_tool_enum_from_paintmode(paint_mode), 0, prop_id, "");
828     RNA_def_property_flag(prop, PROP_HIDDEN);
829   }
830 
831   prop = RNA_def_boolean(
832       ot->srna, "toggle", 0, "Toggle", "Toggle between two brushes rather than cycling");
833   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
834   prop = RNA_def_boolean(ot->srna,
835                          "create_missing",
836                          0,
837                          "Create Missing",
838                          "If the requested brush type does not exist, create a new brush");
839   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
840 }
841 
842 /***** Stencil Control *****/
843 
844 typedef enum {
845   STENCIL_TRANSLATE,
846   STENCIL_SCALE,
847   STENCIL_ROTATE,
848 } StencilControlMode;
849 
850 typedef enum {
851   STENCIL_PRIMARY = 0,
852   STENCIL_SECONDARY = 1,
853 } StencilTextureMode;
854 
855 typedef enum {
856   STENCIL_CONSTRAINT_X = 1,
857   STENCIL_CONSTRAINT_Y = 2,
858 } StencilConstraint;
859 
860 typedef struct {
861   float init_mouse[2];
862   float init_spos[2];
863   float init_sdim[2];
864   float init_rot;
865   float init_angle;
866   float lenorig;
867   float area_size[2];
868   StencilControlMode mode;
869   StencilConstraint constrain_mode;
870   /** We are tweaking mask or color stencil. */
871   int mask;
872   Brush *br;
873   float *dim_target;
874   float *rot_target;
875   float *pos_target;
876   short launch_event;
877 } StencilControlData;
878 
stencil_set_target(StencilControlData * scd)879 static void stencil_set_target(StencilControlData *scd)
880 {
881   Brush *br = scd->br;
882   float mdiff[2];
883   if (scd->mask) {
884     copy_v2_v2(scd->init_sdim, br->mask_stencil_dimension);
885     copy_v2_v2(scd->init_spos, br->mask_stencil_pos);
886     scd->init_rot = br->mask_mtex.rot;
887 
888     scd->dim_target = br->mask_stencil_dimension;
889     scd->rot_target = &br->mask_mtex.rot;
890     scd->pos_target = br->mask_stencil_pos;
891 
892     sub_v2_v2v2(mdiff, scd->init_mouse, br->mask_stencil_pos);
893   }
894   else {
895     copy_v2_v2(scd->init_sdim, br->stencil_dimension);
896     copy_v2_v2(scd->init_spos, br->stencil_pos);
897     scd->init_rot = br->mtex.rot;
898 
899     scd->dim_target = br->stencil_dimension;
900     scd->rot_target = &br->mtex.rot;
901     scd->pos_target = br->stencil_pos;
902 
903     sub_v2_v2v2(mdiff, scd->init_mouse, br->stencil_pos);
904   }
905 
906   scd->lenorig = len_v2(mdiff);
907 
908   scd->init_angle = atan2f(mdiff[1], mdiff[0]);
909 }
910 
stencil_control_invoke(bContext * C,wmOperator * op,const wmEvent * event)911 static int stencil_control_invoke(bContext *C, wmOperator *op, const wmEvent *event)
912 {
913   Paint *paint = BKE_paint_get_active_from_context(C);
914   Brush *br = BKE_paint_brush(paint);
915   const float mvalf[2] = {event->mval[0], event->mval[1]};
916   ARegion *region = CTX_wm_region(C);
917   StencilControlData *scd;
918   int mask = RNA_enum_get(op->ptr, "texmode");
919 
920   if (mask) {
921     if (br->mask_mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL) {
922       return OPERATOR_CANCELLED;
923     }
924   }
925   else {
926     if (br->mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL) {
927       return OPERATOR_CANCELLED;
928     }
929   }
930 
931   scd = MEM_mallocN(sizeof(StencilControlData), "stencil_control");
932   scd->mask = mask;
933   scd->br = br;
934 
935   copy_v2_v2(scd->init_mouse, mvalf);
936 
937   stencil_set_target(scd);
938 
939   scd->mode = RNA_enum_get(op->ptr, "mode");
940   scd->launch_event = WM_userdef_event_type_from_keymap_type(event->type);
941   scd->area_size[0] = region->winx;
942   scd->area_size[1] = region->winy;
943 
944   op->customdata = scd;
945   WM_event_add_modal_handler(C, op);
946 
947   return OPERATOR_RUNNING_MODAL;
948 }
949 
stencil_restore(StencilControlData * scd)950 static void stencil_restore(StencilControlData *scd)
951 {
952   copy_v2_v2(scd->dim_target, scd->init_sdim);
953   copy_v2_v2(scd->pos_target, scd->init_spos);
954   *scd->rot_target = scd->init_rot;
955 }
956 
stencil_control_cancel(bContext * UNUSED (C),wmOperator * op)957 static void stencil_control_cancel(bContext *UNUSED(C), wmOperator *op)
958 {
959   StencilControlData *scd = op->customdata;
960 
961   stencil_restore(scd);
962   MEM_freeN(op->customdata);
963 }
964 
stencil_control_calculate(StencilControlData * scd,const int mval[2])965 static void stencil_control_calculate(StencilControlData *scd, const int mval[2])
966 {
967 #define PIXEL_MARGIN 5
968 
969   float mdiff[2];
970   const float mvalf[2] = {mval[0], mval[1]};
971   switch (scd->mode) {
972     case STENCIL_TRANSLATE:
973       sub_v2_v2v2(mdiff, mvalf, scd->init_mouse);
974       add_v2_v2v2(scd->pos_target, scd->init_spos, mdiff);
975       CLAMP(scd->pos_target[0],
976             -scd->dim_target[0] + PIXEL_MARGIN,
977             scd->area_size[0] + scd->dim_target[0] - PIXEL_MARGIN);
978 
979       CLAMP(scd->pos_target[1],
980             -scd->dim_target[1] + PIXEL_MARGIN,
981             scd->area_size[1] + scd->dim_target[1] - PIXEL_MARGIN);
982 
983       break;
984     case STENCIL_SCALE: {
985       float len, factor;
986       sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
987       len = len_v2(mdiff);
988       factor = len / scd->lenorig;
989       copy_v2_v2(mdiff, scd->init_sdim);
990       if (scd->constrain_mode != STENCIL_CONSTRAINT_Y) {
991         mdiff[0] = factor * scd->init_sdim[0];
992       }
993       if (scd->constrain_mode != STENCIL_CONSTRAINT_X) {
994         mdiff[1] = factor * scd->init_sdim[1];
995       }
996       clamp_v2(mdiff, 5.0f, 10000.0f);
997       copy_v2_v2(scd->dim_target, mdiff);
998       break;
999     }
1000     case STENCIL_ROTATE: {
1001       float angle;
1002       sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
1003       angle = atan2f(mdiff[1], mdiff[0]);
1004       angle = scd->init_rot + angle - scd->init_angle;
1005       if (angle < 0.0f) {
1006         angle += (float)(2 * M_PI);
1007       }
1008       if (angle > (float)(2 * M_PI)) {
1009         angle -= (float)(2 * M_PI);
1010       }
1011       *scd->rot_target = angle;
1012       break;
1013     }
1014   }
1015 #undef PIXEL_MARGIN
1016 }
1017 
stencil_control_modal(bContext * C,wmOperator * op,const wmEvent * event)1018 static int stencil_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
1019 {
1020   StencilControlData *scd = op->customdata;
1021 
1022   if (event->type == scd->launch_event && event->val == KM_RELEASE) {
1023     MEM_freeN(op->customdata);
1024     WM_event_add_notifier(C, NC_WINDOW, NULL);
1025     return OPERATOR_FINISHED;
1026   }
1027 
1028   switch (event->type) {
1029     case MOUSEMOVE:
1030       stencil_control_calculate(scd, event->mval);
1031       break;
1032     case EVT_ESCKEY:
1033       if (event->val == KM_PRESS) {
1034         stencil_control_cancel(C, op);
1035         WM_event_add_notifier(C, NC_WINDOW, NULL);
1036         return OPERATOR_CANCELLED;
1037       }
1038       break;
1039     case EVT_XKEY:
1040       if (event->val == KM_PRESS) {
1041 
1042         if (scd->constrain_mode == STENCIL_CONSTRAINT_X) {
1043           scd->constrain_mode = 0;
1044         }
1045         else {
1046           scd->constrain_mode = STENCIL_CONSTRAINT_X;
1047         }
1048 
1049         stencil_control_calculate(scd, event->mval);
1050       }
1051       break;
1052     case EVT_YKEY:
1053       if (event->val == KM_PRESS) {
1054         if (scd->constrain_mode == STENCIL_CONSTRAINT_Y) {
1055           scd->constrain_mode = 0;
1056         }
1057         else {
1058           scd->constrain_mode = STENCIL_CONSTRAINT_Y;
1059         }
1060 
1061         stencil_control_calculate(scd, event->mval);
1062       }
1063       break;
1064     default:
1065       break;
1066   }
1067 
1068   ED_region_tag_redraw(CTX_wm_region(C));
1069 
1070   return OPERATOR_RUNNING_MODAL;
1071 }
1072 
stencil_control_poll(bContext * C)1073 static bool stencil_control_poll(bContext *C)
1074 {
1075   ePaintMode mode = BKE_paintmode_get_active_from_context(C);
1076 
1077   Paint *paint;
1078   Brush *br;
1079 
1080   if (!paint_supports_texture(mode)) {
1081     return false;
1082   }
1083 
1084   paint = BKE_paint_get_active_from_context(C);
1085   br = BKE_paint_brush(paint);
1086   return (br && (br->mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL ||
1087                  br->mask_mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL));
1088 }
1089 
BRUSH_OT_stencil_control(wmOperatorType * ot)1090 static void BRUSH_OT_stencil_control(wmOperatorType *ot)
1091 {
1092   static const EnumPropertyItem stencil_control_items[] = {
1093       {STENCIL_TRANSLATE, "TRANSLATION", 0, "Translation", ""},
1094       {STENCIL_SCALE, "SCALE", 0, "Scale", ""},
1095       {STENCIL_ROTATE, "ROTATION", 0, "Rotation", ""},
1096       {0, NULL, 0, NULL, NULL},
1097   };
1098 
1099   static const EnumPropertyItem stencil_texture_items[] = {
1100       {STENCIL_PRIMARY, "PRIMARY", 0, "Primary", ""},
1101       {STENCIL_SECONDARY, "SECONDARY", 0, "Secondary", ""},
1102       {0, NULL, 0, NULL, NULL},
1103   };
1104   /* identifiers */
1105   ot->name = "Stencil Brush Control";
1106   ot->description = "Control the stencil brush";
1107   ot->idname = "BRUSH_OT_stencil_control";
1108 
1109   /* api callbacks */
1110   ot->invoke = stencil_control_invoke;
1111   ot->modal = stencil_control_modal;
1112   ot->cancel = stencil_control_cancel;
1113   ot->poll = stencil_control_poll;
1114 
1115   /* flags */
1116   ot->flag = 0;
1117 
1118   PropertyRNA *prop;
1119   prop = RNA_def_enum(ot->srna, "mode", stencil_control_items, STENCIL_TRANSLATE, "Tool", "");
1120   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1121   prop = RNA_def_enum(ot->srna, "texmode", stencil_texture_items, STENCIL_PRIMARY, "Tool", "");
1122   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
1123 }
1124 
stencil_fit_image_aspect_exec(bContext * C,wmOperator * op)1125 static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *op)
1126 {
1127   Paint *paint = BKE_paint_get_active_from_context(C);
1128   Brush *br = BKE_paint_brush(paint);
1129   bool use_scale = RNA_boolean_get(op->ptr, "use_scale");
1130   bool use_repeat = RNA_boolean_get(op->ptr, "use_repeat");
1131   bool do_mask = RNA_boolean_get(op->ptr, "mask");
1132   Tex *tex = NULL;
1133   MTex *mtex = NULL;
1134   if (br) {
1135     mtex = do_mask ? &br->mask_mtex : &br->mtex;
1136     tex = mtex->tex;
1137   }
1138 
1139   if (tex && tex->type == TEX_IMAGE && tex->ima) {
1140     float aspx, aspy;
1141     Image *ima = tex->ima;
1142     float orig_area, stencil_area, factor;
1143     ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
1144 
1145     if (use_scale) {
1146       aspx *= mtex->size[0];
1147       aspy *= mtex->size[1];
1148     }
1149 
1150     if (use_repeat && tex->extend == TEX_REPEAT) {
1151       aspx *= tex->xrepeat;
1152       aspy *= tex->yrepeat;
1153     }
1154 
1155     orig_area = fabsf(aspx * aspy);
1156 
1157     if (do_mask) {
1158       stencil_area = fabsf(br->mask_stencil_dimension[0] * br->mask_stencil_dimension[1]);
1159     }
1160     else {
1161       stencil_area = fabsf(br->stencil_dimension[0] * br->stencil_dimension[1]);
1162     }
1163 
1164     factor = sqrtf(stencil_area / orig_area);
1165 
1166     if (do_mask) {
1167       br->mask_stencil_dimension[0] = fabsf(factor * aspx);
1168       br->mask_stencil_dimension[1] = fabsf(factor * aspy);
1169     }
1170     else {
1171       br->stencil_dimension[0] = fabsf(factor * aspx);
1172       br->stencil_dimension[1] = fabsf(factor * aspy);
1173     }
1174   }
1175 
1176   WM_event_add_notifier(C, NC_WINDOW, NULL);
1177 
1178   return OPERATOR_FINISHED;
1179 }
1180 
BRUSH_OT_stencil_fit_image_aspect(wmOperatorType * ot)1181 static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
1182 {
1183   /* identifiers */
1184   ot->name = "Image Aspect";
1185   ot->description =
1186       "When using an image texture, adjust the stencil size to fit the image aspect ratio";
1187   ot->idname = "BRUSH_OT_stencil_fit_image_aspect";
1188 
1189   /* api callbacks */
1190   ot->exec = stencil_fit_image_aspect_exec;
1191   ot->poll = stencil_control_poll;
1192 
1193   /* flags */
1194   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1195 
1196   RNA_def_boolean(ot->srna, "use_repeat", 1, "Use Repeat", "Use repeat mapping values");
1197   RNA_def_boolean(ot->srna, "use_scale", 1, "Use Scale", "Use texture scale values");
1198   RNA_def_boolean(
1199       ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
1200 }
1201 
stencil_reset_transform_exec(bContext * C,wmOperator * op)1202 static int stencil_reset_transform_exec(bContext *C, wmOperator *op)
1203 {
1204   Paint *paint = BKE_paint_get_active_from_context(C);
1205   Brush *br = BKE_paint_brush(paint);
1206   bool do_mask = RNA_boolean_get(op->ptr, "mask");
1207 
1208   if (!br) {
1209     return OPERATOR_CANCELLED;
1210   }
1211 
1212   if (do_mask) {
1213     br->mask_stencil_pos[0] = 256;
1214     br->mask_stencil_pos[1] = 256;
1215 
1216     br->mask_stencil_dimension[0] = 256;
1217     br->mask_stencil_dimension[1] = 256;
1218 
1219     br->mask_mtex.rot = 0;
1220   }
1221   else {
1222     br->stencil_pos[0] = 256;
1223     br->stencil_pos[1] = 256;
1224 
1225     br->stencil_dimension[0] = 256;
1226     br->stencil_dimension[1] = 256;
1227 
1228     br->mtex.rot = 0;
1229   }
1230 
1231   WM_event_add_notifier(C, NC_WINDOW, NULL);
1232 
1233   return OPERATOR_FINISHED;
1234 }
1235 
BRUSH_OT_stencil_reset_transform(wmOperatorType * ot)1236 static void BRUSH_OT_stencil_reset_transform(wmOperatorType *ot)
1237 {
1238   /* identifiers */
1239   ot->name = "Reset Transform";
1240   ot->description = "Reset the stencil transformation to the default";
1241   ot->idname = "BRUSH_OT_stencil_reset_transform";
1242 
1243   /* api callbacks */
1244   ot->exec = stencil_reset_transform_exec;
1245   ot->poll = stencil_control_poll;
1246 
1247   /* flags */
1248   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1249 
1250   RNA_def_boolean(
1251       ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
1252 }
1253 
1254 /**************************** registration **********************************/
1255 
ED_operatormacros_paint(void)1256 void ED_operatormacros_paint(void)
1257 {
1258   wmOperatorType *ot;
1259   wmOperatorTypeMacro *otmacro;
1260 
1261   ot = WM_operatortype_append_macro("PAINTCURVE_OT_add_point_slide",
1262                                     "Add Curve Point and Slide",
1263                                     "Add new curve point and slide it",
1264                                     OPTYPE_UNDO);
1265   ot->description = "Add new curve point and slide it";
1266   WM_operatortype_macro_define(ot, "PAINTCURVE_OT_add_point");
1267   otmacro = WM_operatortype_macro_define(ot, "PAINTCURVE_OT_slide");
1268   RNA_boolean_set(otmacro->ptr, "align", true);
1269   RNA_boolean_set(otmacro->ptr, "select", false);
1270 }
1271 
ED_operatortypes_paint(void)1272 void ED_operatortypes_paint(void)
1273 {
1274   /* palette */
1275   WM_operatortype_append(PALETTE_OT_new);
1276   WM_operatortype_append(PALETTE_OT_color_add);
1277   WM_operatortype_append(PALETTE_OT_color_delete);
1278 
1279   WM_operatortype_append(PALETTE_OT_extract_from_image);
1280   WM_operatortype_append(PALETTE_OT_sort);
1281   WM_operatortype_append(PALETTE_OT_color_move);
1282   WM_operatortype_append(PALETTE_OT_join);
1283 
1284   /* paint curve */
1285   WM_operatortype_append(PAINTCURVE_OT_new);
1286   WM_operatortype_append(PAINTCURVE_OT_add_point);
1287   WM_operatortype_append(PAINTCURVE_OT_delete_point);
1288   WM_operatortype_append(PAINTCURVE_OT_select);
1289   WM_operatortype_append(PAINTCURVE_OT_slide);
1290   WM_operatortype_append(PAINTCURVE_OT_draw);
1291   WM_operatortype_append(PAINTCURVE_OT_cursor);
1292 
1293   /* brush */
1294   WM_operatortype_append(BRUSH_OT_add);
1295   WM_operatortype_append(BRUSH_OT_add_gpencil);
1296   WM_operatortype_append(BRUSH_OT_scale_size);
1297   WM_operatortype_append(BRUSH_OT_curve_preset);
1298   WM_operatortype_append(BRUSH_OT_reset);
1299   WM_operatortype_append(BRUSH_OT_stencil_control);
1300   WM_operatortype_append(BRUSH_OT_stencil_fit_image_aspect);
1301   WM_operatortype_append(BRUSH_OT_stencil_reset_transform);
1302 
1303   /* note, particle uses a different system, can be added with existing operators in wm.py */
1304   WM_operatortype_append(PAINT_OT_brush_select);
1305 
1306   /* image */
1307   WM_operatortype_append(PAINT_OT_texture_paint_toggle);
1308   WM_operatortype_append(PAINT_OT_image_paint);
1309   WM_operatortype_append(PAINT_OT_sample_color);
1310   WM_operatortype_append(PAINT_OT_grab_clone);
1311   WM_operatortype_append(PAINT_OT_project_image);
1312   WM_operatortype_append(PAINT_OT_image_from_view);
1313   WM_operatortype_append(PAINT_OT_brush_colors_flip);
1314   WM_operatortype_append(PAINT_OT_add_texture_paint_slot);
1315   WM_operatortype_append(PAINT_OT_add_simple_uvs);
1316 
1317   /* weight */
1318   WM_operatortype_append(PAINT_OT_weight_paint_toggle);
1319   WM_operatortype_append(PAINT_OT_weight_paint);
1320   WM_operatortype_append(PAINT_OT_weight_set);
1321   WM_operatortype_append(PAINT_OT_weight_from_bones);
1322   WM_operatortype_append(PAINT_OT_weight_gradient);
1323   WM_operatortype_append(PAINT_OT_weight_sample);
1324   WM_operatortype_append(PAINT_OT_weight_sample_group);
1325 
1326   /* uv */
1327   WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
1328 
1329   /* vertex selection */
1330   WM_operatortype_append(PAINT_OT_vert_select_all);
1331   WM_operatortype_append(PAINT_OT_vert_select_ungrouped);
1332 
1333   /* vertex */
1334   WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
1335   WM_operatortype_append(PAINT_OT_vertex_paint);
1336   WM_operatortype_append(PAINT_OT_vertex_color_set);
1337   WM_operatortype_append(PAINT_OT_vertex_color_smooth);
1338 
1339   WM_operatortype_append(PAINT_OT_vertex_color_brightness_contrast);
1340   WM_operatortype_append(PAINT_OT_vertex_color_hsv);
1341   WM_operatortype_append(PAINT_OT_vertex_color_invert);
1342   WM_operatortype_append(PAINT_OT_vertex_color_levels);
1343   WM_operatortype_append(PAINT_OT_vertex_color_from_weight);
1344 
1345   /* face-select */
1346   WM_operatortype_append(PAINT_OT_face_select_linked);
1347   WM_operatortype_append(PAINT_OT_face_select_linked_pick);
1348   WM_operatortype_append(PAINT_OT_face_select_all);
1349   WM_operatortype_append(PAINT_OT_face_select_hide);
1350   WM_operatortype_append(PAINT_OT_face_select_reveal);
1351 
1352   /* partial visibility */
1353   WM_operatortype_append(PAINT_OT_hide_show);
1354 
1355   /* paint masking */
1356   WM_operatortype_append(PAINT_OT_mask_flood_fill);
1357   WM_operatortype_append(PAINT_OT_mask_lasso_gesture);
1358   WM_operatortype_append(PAINT_OT_mask_box_gesture);
1359   WM_operatortype_append(PAINT_OT_mask_line_gesture);
1360 }
1361 
ED_keymap_paint(wmKeyConfig * keyconf)1362 void ED_keymap_paint(wmKeyConfig *keyconf)
1363 {
1364   wmKeyMap *keymap;
1365 
1366   keymap = WM_keymap_ensure(keyconf, "Paint Curve", 0, 0);
1367   keymap->poll = paint_curve_poll;
1368 
1369   /* Sculpt mode */
1370   keymap = WM_keymap_ensure(keyconf, "Sculpt", 0, 0);
1371   keymap->poll = SCULPT_mode_poll;
1372 
1373   /* Vertex Paint mode */
1374   keymap = WM_keymap_ensure(keyconf, "Vertex Paint", 0, 0);
1375   keymap->poll = vertex_paint_mode_poll;
1376 
1377   /* Weight Paint mode */
1378   keymap = WM_keymap_ensure(keyconf, "Weight Paint", 0, 0);
1379   keymap->poll = weight_paint_mode_poll;
1380 
1381   /*Weight paint's Vertex Selection Mode */
1382   keymap = WM_keymap_ensure(keyconf, "Paint Vertex Selection (Weight, Vertex)", 0, 0);
1383   keymap->poll = vert_paint_poll;
1384 
1385   /* Image/Texture Paint mode */
1386   keymap = WM_keymap_ensure(keyconf, "Image Paint", 0, 0);
1387   keymap->poll = image_texture_paint_poll;
1388 
1389   /* face-mask mode */
1390   keymap = WM_keymap_ensure(keyconf, "Paint Face Mask (Weight, Vertex, Texture)", 0, 0);
1391   keymap->poll = facemask_paint_poll;
1392 
1393   /* paint stroke */
1394   keymap = paint_stroke_modal_keymap(keyconf);
1395   WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
1396 }
1397