Fix new Text ID usercount handling in add/load cases.
[blender.git] / source / blender / editors / sculpt_paint / paint_ops.c
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 <stdlib.h>
24 #include "BLI_listbase.h"
25 #include "BLI_utildefines.h"
26 #include "BLI_math_vector.h"
27
28 #include "DNA_customdata_types.h"
29 #include "DNA_object_types.h"
30 #include "DNA_scene_types.h"
31 #include "DNA_brush_types.h"
32 #include "DNA_gpencil_types.h"
33
34 #include "BKE_brush.h"
35 #include "BKE_context.h"
36 #include "BKE_gpencil.h"
37 #include "BKE_library.h"
38 #include "BKE_main.h"
39 #include "BKE_paint.h"
40 #include "BKE_report.h"
41
42 #include "DEG_depsgraph.h"
43
44 #include "ED_paint.h"
45 #include "ED_screen.h"
46 #include "ED_select_utils.h"
47 #include "ED_image.h"
48 #include "ED_gpencil.h"
49 #include "UI_resources.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53 #include "WM_toolsystem.h"
54
55 #include "RNA_access.h"
56 #include "RNA_define.h"
57 #include "RNA_enum_types.h"
58
59 #include "paint_intern.h"
60 #include "sculpt_intern.h"
61
62 #include <string.h>
63 //#include <stdio.h>
64 #include <stddef.h>
65
66 /* Brush operators */
67 static int brush_add_exec(bContext *C, wmOperator *UNUSED(op))
68 {
69   /*int type = RNA_enum_get(op->ptr, "type");*/
70   Paint *paint = BKE_paint_get_active_from_context(C);
71   Brush *br = BKE_paint_brush(paint);
72   Main *bmain = CTX_data_main(C);
73   ePaintMode mode = BKE_paintmode_get_active_from_context(C);
74
75   if (br) {
76     br = BKE_brush_copy(bmain, br);
77   }
78   else {
79     br = BKE_brush_add(bmain, "Brush", BKE_paint_object_mode_from_paintmode(mode));
80     id_us_min(&br->id); /* fake user only */
81   }
82
83   BKE_paint_brush_set(paint, br);
84
85   return OPERATOR_FINISHED;
86 }
87
88 static void BRUSH_OT_add(wmOperatorType *ot)
89 {
90   /* identifiers */
91   ot->name = "Add Brush";
92   ot->description = "Add brush by mode type";
93   ot->idname = "BRUSH_OT_add";
94
95   /* api callbacks */
96   ot->exec = brush_add_exec;
97
98   /* flags */
99   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
100 }
101
102 static int brush_add_gpencil_exec(bContext *C, wmOperator *UNUSED(op))
103 {
104   /*int type = RNA_enum_get(op->ptr, "type");*/
105   ToolSettings *ts = CTX_data_tool_settings(C);
106   Paint *paint = &ts->gp_paint->paint;
107   Brush *br = BKE_paint_brush(paint);
108   Main *bmain = CTX_data_main(C);
109   // ePaintMode mode = PAINT_MODE_GPENCIL;
110
111   if (br) {
112     br = BKE_brush_copy(bmain, br);
113   }
114   else {
115     br = BKE_brush_add(bmain, "Brush", OB_MODE_PAINT_GPENCIL);
116     id_us_min(&br->id); /* fake user only */
117   }
118
119   BKE_paint_brush_set(paint, br);
120
121   /* init grease pencil specific data */
122   BKE_brush_init_gpencil_settings(br);
123
124   return OPERATOR_FINISHED;
125 }
126
127 static void BRUSH_OT_add_gpencil(wmOperatorType *ot)
128 {
129   /* identifiers */
130   ot->name = "Add Drawing Brush";
131   ot->description = "Add brush for Grease Pencil";
132   ot->idname = "BRUSH_OT_add_gpencil";
133
134   /* api callbacks */
135   ot->exec = brush_add_gpencil_exec;
136
137   /* flags */
138   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
139 }
140
141 static int brush_scale_size_exec(bContext *C, wmOperator *op)
142 {
143   Scene *scene = CTX_data_scene(C);
144   Paint *paint = BKE_paint_get_active_from_context(C);
145   Brush *brush = BKE_paint_brush(paint);
146   // Object *ob = CTX_data_active_object(C);
147   float scalar = RNA_float_get(op->ptr, "scalar");
148
149   if (brush) {
150     // pixel radius
151     {
152       const int old_size = BKE_brush_size_get(scene, brush);
153       int size = (int)(scalar * old_size);
154
155       if (abs(old_size - size) < U.pixelsize) {
156         if (scalar > 1) {
157           size += U.pixelsize;
158         }
159         else if (scalar < 1) {
160           size -= U.pixelsize;
161         }
162       }
163
164       BKE_brush_size_set(scene, brush, size);
165     }
166
167     // unprojected radius
168     {
169       float unprojected_radius = scalar * BKE_brush_unprojected_radius_get(scene, brush);
170
171       if (unprojected_radius < 0.001f) {  // XXX magic number
172         unprojected_radius = 0.001f;
173       }
174
175       BKE_brush_unprojected_radius_set(scene, brush, unprojected_radius);
176     }
177
178     WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
179   }
180
181   return OPERATOR_FINISHED;
182 }
183
184 static void BRUSH_OT_scale_size(wmOperatorType *ot)
185 {
186   /* identifiers */
187   ot->name = "Scale Sculpt/Paint Brush Size";
188   ot->description = "Change brush size by a scalar";
189   ot->idname = "BRUSH_OT_scale_size";
190
191   /* api callbacks */
192   ot->exec = brush_scale_size_exec;
193
194   /* flags */
195   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
196
197   RNA_def_float(ot->srna, "scalar", 1, 0, 2, "Scalar", "Factor to scale brush size by", 0, 2);
198 }
199
200 /* Palette operators */
201
202 static int palette_new_exec(bContext *C, wmOperator *UNUSED(op))
203 {
204   Paint *paint = BKE_paint_get_active_from_context(C);
205   Main *bmain = CTX_data_main(C);
206   Palette *palette;
207
208   palette = BKE_palette_add(bmain, "Palette");
209
210   BKE_paint_palette_set(paint, palette);
211
212   return OPERATOR_FINISHED;
213 }
214
215 static void PALETTE_OT_new(wmOperatorType *ot)
216 {
217   /* identifiers */
218   ot->name = "Add New Palette";
219   ot->description = "Add new palette";
220   ot->idname = "PALETTE_OT_new";
221
222   /* api callbacks */
223   ot->exec = palette_new_exec;
224
225   /* flags */
226   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
227 }
228
229 static bool palette_poll(bContext *C)
230 {
231   Paint *paint = BKE_paint_get_active_from_context(C);
232
233   if (paint && paint->palette != NULL) {
234     return true;
235   }
236
237   return false;
238 }
239
240 static int palette_color_add_exec(bContext *C, wmOperator *UNUSED(op))
241 {
242   Scene *scene = CTX_data_scene(C);
243   Paint *paint = BKE_paint_get_active_from_context(C);
244   Brush *brush = paint->brush;
245   ePaintMode mode = BKE_paintmode_get_active_from_context(C);
246   Palette *palette = paint->palette;
247   PaletteColor *color;
248
249   color = BKE_palette_color_add(palette);
250   palette->active_color = BLI_listbase_count(&palette->colors) - 1;
251
252   if (ELEM(mode, PAINT_MODE_TEXTURE_3D, PAINT_MODE_TEXTURE_2D, PAINT_MODE_VERTEX)) {
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
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
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
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 static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op))
306 {
307   Paint *paint = BKE_paint_get_active_from_context(C);
308   Brush *brush = BKE_paint_brush(paint);
309   Object *ob = CTX_data_active_object(C);
310
311   if (!ob || !brush) {
312     return OPERATOR_CANCELLED;
313   }
314
315   /* TODO: other modes */
316   if (ob->mode & OB_MODE_SCULPT) {
317     BKE_brush_sculpt_reset(brush);
318   }
319   else {
320     return OPERATOR_CANCELLED;
321   }
322   WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
323
324   return OPERATOR_FINISHED;
325 }
326
327 static void BRUSH_OT_reset(wmOperatorType *ot)
328 {
329   /* identifiers */
330   ot->name = "Reset Brush";
331   ot->description = "Return brush to defaults based on current tool";
332   ot->idname = "BRUSH_OT_reset";
333
334   /* api callbacks */
335   ot->exec = brush_reset_exec;
336
337   /* flags */
338   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
339 }
340
341 static int brush_tool(const Brush *brush, size_t tool_offset)
342 {
343   return *(((char *)brush) + tool_offset);
344 }
345
346 static void brush_tool_set(const Brush *brush, size_t tool_offset, int tool)
347 {
348   *(((char *)brush) + tool_offset) = tool;
349 }
350
351 static Brush *brush_tool_cycle(Main *bmain, Paint *paint, Brush *brush_orig, const int tool)
352 {
353   Brush *brush, *first_brush;
354
355   if (!brush_orig && !(brush_orig = bmain->brushes.first)) {
356     return NULL;
357   }
358
359   if (brush_tool(brush_orig, paint->runtime.tool_offset) != tool) {
360     /* If current brush's tool is different from what we need,
361      * start cycling from the beginning of the list.
362      * Such logic will activate the same exact brush not relating from
363      * which tool user requests other tool.
364      */
365
366     /* Try to tool-slot first. */
367     first_brush = BKE_paint_toolslots_brush_get(paint, tool);
368     if (first_brush == NULL) {
369       first_brush = bmain->brushes.first;
370     }
371   }
372   else {
373     /* If user wants to switch to brush with the same  tool as
374      * currently active brush do a cycling via all possible
375      * brushes with requested tool.
376      */
377     first_brush = brush_orig->id.next ? brush_orig->id.next : bmain->brushes.first;
378   }
379
380   /* get the next brush with the active tool */
381   brush = first_brush;
382   do {
383     if ((brush->ob_mode & paint->runtime.ob_mode) &&
384         (brush_tool(brush, paint->runtime.tool_offset) == tool)) {
385       return brush;
386     }
387
388     brush = brush->id.next ? brush->id.next : bmain->brushes.first;
389   } while (brush != first_brush);
390
391   return NULL;
392 }
393
394 static Brush *brush_tool_toggle(Main *bmain, Paint *paint, Brush *brush_orig, const int tool)
395 {
396   if (!brush_orig || brush_tool(brush_orig, paint->runtime.tool_offset) != tool) {
397     Brush *br;
398     /* if the current brush is not using the desired tool, look
399      * for one that is */
400     br = brush_tool_cycle(bmain, paint, brush_orig, tool);
401     /* store the previously-selected brush */
402     if (br) {
403       br->toggle_brush = brush_orig;
404     }
405
406     return br;
407   }
408   else if (brush_orig->toggle_brush) {
409     /* if current brush is using the desired tool, try to toggle
410      * back to the previously selected brush. */
411     return brush_orig->toggle_brush;
412   }
413   else {
414     return NULL;
415   }
416 }
417
418 static int brush_generic_tool_set(Main *bmain,
419                                   Paint *paint,
420                                   const int tool,
421                                   const char *tool_name,
422                                   const bool create_missing,
423                                   const bool toggle)
424 {
425   Brush *brush, *brush_orig = BKE_paint_brush(paint);
426
427   if (toggle) {
428     brush = brush_tool_toggle(bmain, paint, brush_orig, tool);
429   }
430   else {
431     brush = brush_tool_cycle(bmain, paint, brush_orig, tool);
432   }
433
434   if (!brush && brush_tool(brush_orig, paint->runtime.tool_offset) != tool && create_missing) {
435     brush = BKE_brush_add(bmain, tool_name, paint->runtime.ob_mode);
436     id_us_min(&brush->id); /* fake user only */
437     brush_tool_set(brush, paint->runtime.tool_offset, tool);
438     brush->toggle_brush = brush_orig;
439   }
440
441   if (brush) {
442     BKE_paint_brush_set(paint, brush);
443     BKE_paint_invalidate_overlay_all();
444
445     WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
446     return OPERATOR_FINISHED;
447   }
448   else {
449     return OPERATOR_CANCELLED;
450   }
451 }
452
453 static const ePaintMode brush_select_paint_modes[] = {
454     PAINT_MODE_SCULPT,
455     PAINT_MODE_VERTEX,
456     PAINT_MODE_WEIGHT,
457     PAINT_MODE_TEXTURE_3D,
458     PAINT_MODE_GPENCIL,
459 };
460
461 static int brush_select_exec(bContext *C, wmOperator *op)
462 {
463   Main *bmain = CTX_data_main(C);
464   Scene *scene = CTX_data_scene(C);
465   const bool create_missing = RNA_boolean_get(op->ptr, "create_missing");
466   const bool toggle = RNA_boolean_get(op->ptr, "toggle");
467   const char *tool_name = "Brush";
468   int tool = 0;
469
470   ePaintMode paint_mode = PAINT_MODE_INVALID;
471   for (int i = 0; i < ARRAY_SIZE(brush_select_paint_modes); i++) {
472     paint_mode = brush_select_paint_modes[i];
473     const char *op_prop_id = BKE_paint_get_tool_prop_id_from_paintmode(paint_mode);
474     PropertyRNA *prop = RNA_struct_find_property(op->ptr, op_prop_id);
475     if (RNA_property_is_set(op->ptr, prop)) {
476       tool = RNA_property_enum_get(op->ptr, prop);
477       break;
478     }
479   }
480
481   if (paint_mode == PAINT_MODE_INVALID) {
482     return OPERATOR_CANCELLED;
483   }
484
485   Paint *paint = BKE_paint_get_active_from_paintmode(scene, paint_mode);
486   const EnumPropertyItem *items = BKE_paint_get_tool_enum_from_paintmode(paint_mode);
487   RNA_enum_name_from_value(items, tool, &tool_name);
488   return brush_generic_tool_set(bmain, paint, tool, tool_name, create_missing, toggle);
489 }
490
491 static void PAINT_OT_brush_select(wmOperatorType *ot)
492 {
493   PropertyRNA *prop;
494
495   /* identifiers */
496   ot->name = "Brush Select";
497   ot->description = "Select a paint mode's brush by tool type";
498   ot->idname = "PAINT_OT_brush_select";
499
500   /* api callbacks */
501   ot->exec = brush_select_exec;
502
503   /* flags */
504   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
505
506   /* props */
507   /* All properties are hidden, so as not to show the redo panel. */
508   for (int i = 0; i < ARRAY_SIZE(brush_select_paint_modes); i++) {
509     const ePaintMode paint_mode = brush_select_paint_modes[i];
510     const char *prop_id = BKE_paint_get_tool_prop_id_from_paintmode(paint_mode);
511     prop = RNA_def_enum(
512         ot->srna, prop_id, BKE_paint_get_tool_enum_from_paintmode(paint_mode), 0, prop_id, "");
513     RNA_def_property_flag(prop, PROP_HIDDEN);
514   }
515
516   prop = RNA_def_boolean(
517       ot->srna, "toggle", 0, "Toggle", "Toggle between two brushes rather than cycling");
518   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
519   prop = RNA_def_boolean(ot->srna,
520                          "create_missing",
521                          0,
522                          "Create Missing",
523                          "If the requested brush type does not exist, create a new brush");
524   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
525 }
526
527 /***** Stencil Control *****/
528
529 typedef enum {
530   STENCIL_TRANSLATE,
531   STENCIL_SCALE,
532   STENCIL_ROTATE,
533 } StencilControlMode;
534
535 typedef enum {
536   STENCIL_PRIMARY = 0,
537   STENCIL_SECONDARY = 1,
538 } StencilTextureMode;
539
540 typedef enum {
541   STENCIL_CONSTRAINT_X = 1,
542   STENCIL_CONSTRAINT_Y = 2,
543 } StencilConstraint;
544
545 typedef struct {
546   float init_mouse[2];
547   float init_spos[2];
548   float init_sdim[2];
549   float init_rot;
550   float init_angle;
551   float lenorig;
552   float area_size[2];
553   StencilControlMode mode;
554   StencilConstraint constrain_mode;
555   /** We are tweaking mask or color stencil. */
556   int mask;
557   Brush *br;
558   float *dim_target;
559   float *rot_target;
560   float *pos_target;
561   short event_type;
562 } StencilControlData;
563
564 static void stencil_set_target(StencilControlData *scd)
565 {
566   Brush *br = scd->br;
567   float mdiff[2];
568   if (scd->mask) {
569     copy_v2_v2(scd->init_sdim, br->mask_stencil_dimension);
570     copy_v2_v2(scd->init_spos, br->mask_stencil_pos);
571     scd->init_rot = br->mask_mtex.rot;
572
573     scd->dim_target = br->mask_stencil_dimension;
574     scd->rot_target = &br->mask_mtex.rot;
575     scd->pos_target = br->mask_stencil_pos;
576
577     sub_v2_v2v2(mdiff, scd->init_mouse, br->mask_stencil_pos);
578   }
579   else {
580     copy_v2_v2(scd->init_sdim, br->stencil_dimension);
581     copy_v2_v2(scd->init_spos, br->stencil_pos);
582     scd->init_rot = br->mtex.rot;
583
584     scd->dim_target = br->stencil_dimension;
585     scd->rot_target = &br->mtex.rot;
586     scd->pos_target = br->stencil_pos;
587
588     sub_v2_v2v2(mdiff, scd->init_mouse, br->stencil_pos);
589   }
590
591   scd->lenorig = len_v2(mdiff);
592
593   scd->init_angle = atan2f(mdiff[1], mdiff[0]);
594 }
595
596 static int stencil_control_invoke(bContext *C, wmOperator *op, const wmEvent *event)
597 {
598   Paint *paint = BKE_paint_get_active_from_context(C);
599   Brush *br = BKE_paint_brush(paint);
600   float mvalf[2] = {event->mval[0], event->mval[1]};
601   ARegion *ar = CTX_wm_region(C);
602   StencilControlData *scd;
603   int mask = RNA_enum_get(op->ptr, "texmode");
604
605   if (mask) {
606     if (br->mask_mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL) {
607       return OPERATOR_CANCELLED;
608     }
609   }
610   else {
611     if (br->mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL) {
612       return OPERATOR_CANCELLED;
613     }
614   }
615
616   scd = MEM_mallocN(sizeof(StencilControlData), "stencil_control");
617   scd->mask = mask;
618   scd->br = br;
619
620   copy_v2_v2(scd->init_mouse, mvalf);
621
622   stencil_set_target(scd);
623
624   scd->mode = RNA_enum_get(op->ptr, "mode");
625   scd->event_type = event->type;
626   scd->area_size[0] = ar->winx;
627   scd->area_size[1] = ar->winy;
628
629   op->customdata = scd;
630   WM_event_add_modal_handler(C, op);
631
632   return OPERATOR_RUNNING_MODAL;
633 }
634
635 static void stencil_restore(StencilControlData *scd)
636 {
637   copy_v2_v2(scd->dim_target, scd->init_sdim);
638   copy_v2_v2(scd->pos_target, scd->init_spos);
639   *scd->rot_target = scd->init_rot;
640 }
641
642 static void stencil_control_cancel(bContext *UNUSED(C), wmOperator *op)
643 {
644   StencilControlData *scd = op->customdata;
645
646   stencil_restore(scd);
647   MEM_freeN(op->customdata);
648 }
649
650 static void stencil_control_calculate(StencilControlData *scd, const int mval[2])
651 {
652 #define PIXEL_MARGIN 5
653
654   float mdiff[2];
655   float mvalf[2] = {mval[0], mval[1]};
656   switch (scd->mode) {
657     case STENCIL_TRANSLATE:
658       sub_v2_v2v2(mdiff, mvalf, scd->init_mouse);
659       add_v2_v2v2(scd->pos_target, scd->init_spos, mdiff);
660       CLAMP(scd->pos_target[0],
661             -scd->dim_target[0] + PIXEL_MARGIN,
662             scd->area_size[0] + scd->dim_target[0] - PIXEL_MARGIN);
663
664       CLAMP(scd->pos_target[1],
665             -scd->dim_target[1] + PIXEL_MARGIN,
666             scd->area_size[1] + scd->dim_target[1] - PIXEL_MARGIN);
667
668       break;
669     case STENCIL_SCALE: {
670       float len, factor;
671       sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
672       len = len_v2(mdiff);
673       factor = len / scd->lenorig;
674       copy_v2_v2(mdiff, scd->init_sdim);
675       if (scd->constrain_mode != STENCIL_CONSTRAINT_Y) {
676         mdiff[0] = factor * scd->init_sdim[0];
677       }
678       if (scd->constrain_mode != STENCIL_CONSTRAINT_X) {
679         mdiff[1] = factor * scd->init_sdim[1];
680       }
681       CLAMP(mdiff[0], 5.0f, 10000.0f);
682       CLAMP(mdiff[1], 5.0f, 10000.0f);
683       copy_v2_v2(scd->dim_target, mdiff);
684       break;
685     }
686     case STENCIL_ROTATE: {
687       float angle;
688       sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
689       angle = atan2f(mdiff[1], mdiff[0]);
690       angle = scd->init_rot + angle - scd->init_angle;
691       if (angle < 0.0f) {
692         angle += (float)(2 * M_PI);
693       }
694       if (angle > (float)(2 * M_PI)) {
695         angle -= (float)(2 * M_PI);
696       }
697       *scd->rot_target = angle;
698       break;
699     }
700   }
701 #undef PIXEL_MARGIN
702 }
703
704 static int stencil_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
705 {
706   StencilControlData *scd = op->customdata;
707
708   if (event->type == scd->event_type && event->val == KM_RELEASE) {
709     MEM_freeN(op->customdata);
710     WM_event_add_notifier(C, NC_WINDOW, NULL);
711     return OPERATOR_FINISHED;
712   }
713
714   switch (event->type) {
715     case MOUSEMOVE:
716       stencil_control_calculate(scd, event->mval);
717       break;
718     case ESCKEY:
719       if (event->val == KM_PRESS) {
720         stencil_control_cancel(C, op);
721         WM_event_add_notifier(C, NC_WINDOW, NULL);
722         return OPERATOR_CANCELLED;
723       }
724       break;
725     case XKEY:
726       if (event->val == KM_PRESS) {
727
728         if (scd->constrain_mode == STENCIL_CONSTRAINT_X) {
729           scd->constrain_mode = 0;
730         }
731         else {
732           scd->constrain_mode = STENCIL_CONSTRAINT_X;
733         }
734
735         stencil_control_calculate(scd, event->mval);
736       }
737       break;
738     case YKEY:
739       if (event->val == KM_PRESS) {
740         if (scd->constrain_mode == STENCIL_CONSTRAINT_Y) {
741           scd->constrain_mode = 0;
742         }
743         else {
744           scd->constrain_mode = STENCIL_CONSTRAINT_Y;
745         }
746
747         stencil_control_calculate(scd, event->mval);
748       }
749       break;
750     default:
751       break;
752   }
753
754   ED_region_tag_redraw(CTX_wm_region(C));
755
756   return OPERATOR_RUNNING_MODAL;
757 }
758
759 static bool stencil_control_poll(bContext *C)
760 {
761   ePaintMode mode = BKE_paintmode_get_active_from_context(C);
762
763   Paint *paint;
764   Brush *br;
765
766   if (!paint_supports_texture(mode)) {
767     return false;
768   }
769
770   paint = BKE_paint_get_active_from_context(C);
771   br = BKE_paint_brush(paint);
772   return (br && (br->mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL ||
773                  br->mask_mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL));
774 }
775
776 static void BRUSH_OT_stencil_control(wmOperatorType *ot)
777 {
778   static const EnumPropertyItem stencil_control_items[] = {
779       {STENCIL_TRANSLATE, "TRANSLATION", 0, "Translation", ""},
780       {STENCIL_SCALE, "SCALE", 0, "Scale", ""},
781       {STENCIL_ROTATE, "ROTATION", 0, "Rotation", ""},
782       {0, NULL, 0, NULL, NULL},
783   };
784
785   static const EnumPropertyItem stencil_texture_items[] = {
786       {STENCIL_PRIMARY, "PRIMARY", 0, "Primary", ""},
787       {STENCIL_SECONDARY, "SECONDARY", 0, "Secondary", ""},
788       {0, NULL, 0, NULL, NULL},
789   };
790   /* identifiers */
791   ot->name = "Stencil Brush Control";
792   ot->description = "Control the stencil brush";
793   ot->idname = "BRUSH_OT_stencil_control";
794
795   /* api callbacks */
796   ot->invoke = stencil_control_invoke;
797   ot->modal = stencil_control_modal;
798   ot->cancel = stencil_control_cancel;
799   ot->poll = stencil_control_poll;
800
801   /* flags */
802   ot->flag = 0;
803
804   PropertyRNA *prop;
805   prop = RNA_def_enum(ot->srna, "mode", stencil_control_items, STENCIL_TRANSLATE, "Tool", "");
806   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
807   prop = RNA_def_enum(ot->srna, "texmode", stencil_texture_items, STENCIL_PRIMARY, "Tool", "");
808   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
809 }
810
811 static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *op)
812 {
813   Paint *paint = BKE_paint_get_active_from_context(C);
814   Brush *br = BKE_paint_brush(paint);
815   bool use_scale = RNA_boolean_get(op->ptr, "use_scale");
816   bool use_repeat = RNA_boolean_get(op->ptr, "use_repeat");
817   bool do_mask = RNA_boolean_get(op->ptr, "mask");
818   Tex *tex = NULL;
819   MTex *mtex = NULL;
820   if (br) {
821     mtex = do_mask ? &br->mask_mtex : &br->mtex;
822     tex = mtex->tex;
823   }
824
825   if (tex && tex->type == TEX_IMAGE && tex->ima) {
826     float aspx, aspy;
827     Image *ima = tex->ima;
828     float orig_area, stencil_area, factor;
829     ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
830
831     if (use_scale) {
832       aspx *= mtex->size[0];
833       aspy *= mtex->size[1];
834     }
835
836     if (use_repeat && tex->extend == TEX_REPEAT) {
837       aspx *= tex->xrepeat;
838       aspy *= tex->yrepeat;
839     }
840
841     orig_area = aspx * aspy;
842
843     if (do_mask) {
844       stencil_area = br->mask_stencil_dimension[0] * br->mask_stencil_dimension[1];
845     }
846     else {
847       stencil_area = br->stencil_dimension[0] * br->stencil_dimension[1];
848     }
849
850     factor = sqrtf(stencil_area / orig_area);
851
852     if (do_mask) {
853       br->mask_stencil_dimension[0] = factor * aspx;
854       br->mask_stencil_dimension[1] = factor * aspy;
855     }
856     else {
857       br->stencil_dimension[0] = factor * aspx;
858       br->stencil_dimension[1] = factor * aspy;
859     }
860   }
861
862   WM_event_add_notifier(C, NC_WINDOW, NULL);
863
864   return OPERATOR_FINISHED;
865 }
866
867 static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
868 {
869   /* identifiers */
870   ot->name = "Image Aspect";
871   ot->description =
872       "When using an image texture, adjust the stencil size to fit the image aspect ratio";
873   ot->idname = "BRUSH_OT_stencil_fit_image_aspect";
874
875   /* api callbacks */
876   ot->exec = stencil_fit_image_aspect_exec;
877   ot->poll = stencil_control_poll;
878
879   /* flags */
880   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
881
882   RNA_def_boolean(ot->srna, "use_repeat", 1, "Use Repeat", "Use repeat mapping values");
883   RNA_def_boolean(ot->srna, "use_scale", 1, "Use Scale", "Use texture scale values");
884   RNA_def_boolean(
885       ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
886 }
887
888 static int stencil_reset_transform_exec(bContext *C, wmOperator *op)
889 {
890   Paint *paint = BKE_paint_get_active_from_context(C);
891   Brush *br = BKE_paint_brush(paint);
892   bool do_mask = RNA_boolean_get(op->ptr, "mask");
893
894   if (!br) {
895     return OPERATOR_CANCELLED;
896   }
897
898   if (do_mask) {
899     br->mask_stencil_pos[0] = 256;
900     br->mask_stencil_pos[1] = 256;
901
902     br->mask_stencil_dimension[0] = 256;
903     br->mask_stencil_dimension[1] = 256;
904
905     br->mask_mtex.rot = 0;
906   }
907   else {
908     br->stencil_pos[0] = 256;
909     br->stencil_pos[1] = 256;
910
911     br->stencil_dimension[0] = 256;
912     br->stencil_dimension[1] = 256;
913
914     br->mtex.rot = 0;
915   }
916
917   WM_event_add_notifier(C, NC_WINDOW, NULL);
918
919   return OPERATOR_FINISHED;
920 }
921
922 static void BRUSH_OT_stencil_reset_transform(wmOperatorType *ot)
923 {
924   /* identifiers */
925   ot->name = "Reset Transform";
926   ot->description = "Reset the stencil transformation to the default";
927   ot->idname = "BRUSH_OT_stencil_reset_transform";
928
929   /* api callbacks */
930   ot->exec = stencil_reset_transform_exec;
931   ot->poll = stencil_control_poll;
932
933   /* flags */
934   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
935
936   RNA_def_boolean(
937       ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
938 }
939
940 /**************************** registration **********************************/
941
942 void ED_operatormacros_paint(void)
943 {
944   wmOperatorType *ot;
945   wmOperatorTypeMacro *otmacro;
946
947   ot = WM_operatortype_append_macro("PAINTCURVE_OT_add_point_slide",
948                                     "Add Curve Point and Slide",
949                                     "Add new curve point and slide it",
950                                     OPTYPE_UNDO);
951   ot->description = "Add new curve point and slide it";
952   WM_operatortype_macro_define(ot, "PAINTCURVE_OT_add_point");
953   otmacro = WM_operatortype_macro_define(ot, "PAINTCURVE_OT_slide");
954   RNA_boolean_set(otmacro->ptr, "align", true);
955   RNA_boolean_set(otmacro->ptr, "select", false);
956 }
957
958 void ED_operatortypes_paint(void)
959 {
960   /* palette */
961   WM_operatortype_append(PALETTE_OT_new);
962   WM_operatortype_append(PALETTE_OT_color_add);
963   WM_operatortype_append(PALETTE_OT_color_delete);
964
965   /* paint curve */
966   WM_operatortype_append(PAINTCURVE_OT_new);
967   WM_operatortype_append(PAINTCURVE_OT_add_point);
968   WM_operatortype_append(PAINTCURVE_OT_delete_point);
969   WM_operatortype_append(PAINTCURVE_OT_select);
970   WM_operatortype_append(PAINTCURVE_OT_slide);
971   WM_operatortype_append(PAINTCURVE_OT_draw);
972   WM_operatortype_append(PAINTCURVE_OT_cursor);
973
974   /* brush */
975   WM_operatortype_append(BRUSH_OT_add);
976   WM_operatortype_append(BRUSH_OT_add_gpencil);
977   WM_operatortype_append(BRUSH_OT_scale_size);
978   WM_operatortype_append(BRUSH_OT_curve_preset);
979   WM_operatortype_append(BRUSH_OT_reset);
980   WM_operatortype_append(BRUSH_OT_stencil_control);
981   WM_operatortype_append(BRUSH_OT_stencil_fit_image_aspect);
982   WM_operatortype_append(BRUSH_OT_stencil_reset_transform);
983
984   /* note, particle uses a different system, can be added with existing operators in wm.py */
985   WM_operatortype_append(PAINT_OT_brush_select);
986
987   /* image */
988   WM_operatortype_append(PAINT_OT_texture_paint_toggle);
989   WM_operatortype_append(PAINT_OT_image_paint);
990   WM_operatortype_append(PAINT_OT_sample_color);
991   WM_operatortype_append(PAINT_OT_grab_clone);
992   WM_operatortype_append(PAINT_OT_project_image);
993   WM_operatortype_append(PAINT_OT_image_from_view);
994   WM_operatortype_append(PAINT_OT_brush_colors_flip);
995   WM_operatortype_append(PAINT_OT_add_texture_paint_slot);
996   WM_operatortype_append(PAINT_OT_add_simple_uvs);
997
998   /* weight */
999   WM_operatortype_append(PAINT_OT_weight_paint_toggle);
1000   WM_operatortype_append(PAINT_OT_weight_paint);
1001   WM_operatortype_append(PAINT_OT_weight_set);
1002   WM_operatortype_append(PAINT_OT_weight_from_bones);
1003   WM_operatortype_append(PAINT_OT_weight_gradient);
1004   WM_operatortype_append(PAINT_OT_weight_sample);
1005   WM_operatortype_append(PAINT_OT_weight_sample_group);
1006
1007   /* uv */
1008   WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
1009
1010   /* vertex selection */
1011   WM_operatortype_append(PAINT_OT_vert_select_all);
1012   WM_operatortype_append(PAINT_OT_vert_select_ungrouped);
1013
1014   /* vertex */
1015   WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
1016   WM_operatortype_append(PAINT_OT_vertex_paint);
1017   WM_operatortype_append(PAINT_OT_vertex_color_set);
1018   WM_operatortype_append(PAINT_OT_vertex_color_smooth);
1019
1020   WM_operatortype_append(PAINT_OT_vertex_color_brightness_contrast);
1021   WM_operatortype_append(PAINT_OT_vertex_color_hsv);
1022   WM_operatortype_append(PAINT_OT_vertex_color_invert);
1023   WM_operatortype_append(PAINT_OT_vertex_color_levels);
1024   WM_operatortype_append(PAINT_OT_vertex_color_from_weight);
1025
1026   /* face-select */
1027   WM_operatortype_append(PAINT_OT_face_select_linked);
1028   WM_operatortype_append(PAINT_OT_face_select_linked_pick);
1029   WM_operatortype_append(PAINT_OT_face_select_all);
1030   WM_operatortype_append(PAINT_OT_face_select_hide);
1031   WM_operatortype_append(PAINT_OT_face_select_reveal);
1032
1033   /* partial visibility */
1034   WM_operatortype_append(PAINT_OT_hide_show);
1035
1036   /* paint masking */
1037   WM_operatortype_append(PAINT_OT_mask_flood_fill);
1038   WM_operatortype_append(PAINT_OT_mask_lasso_gesture);
1039 }
1040
1041 void ED_keymap_paint(wmKeyConfig *keyconf)
1042 {
1043   wmKeyMap *keymap;
1044
1045   keymap = WM_keymap_ensure(keyconf, "Paint Curve", 0, 0);
1046   keymap->poll = paint_curve_poll;
1047
1048   /* Sculpt mode */
1049   keymap = WM_keymap_ensure(keyconf, "Sculpt", 0, 0);
1050   keymap->poll = sculpt_mode_poll;
1051
1052   /* Vertex Paint mode */
1053   keymap = WM_keymap_ensure(keyconf, "Vertex Paint", 0, 0);
1054   keymap->poll = vertex_paint_mode_poll;
1055
1056   /* Weight Paint mode */
1057   keymap = WM_keymap_ensure(keyconf, "Weight Paint", 0, 0);
1058   keymap->poll = weight_paint_mode_poll;
1059
1060   /*Weight paint's Vertex Selection Mode */
1061   keymap = WM_keymap_ensure(keyconf, "Weight Paint Vertex Selection", 0, 0);
1062   keymap->poll = vert_paint_poll;
1063
1064   /* Image/Texture Paint mode */
1065   keymap = WM_keymap_ensure(keyconf, "Image Paint", 0, 0);
1066   keymap->poll = image_texture_paint_poll;
1067
1068   /* face-mask mode */
1069   keymap = WM_keymap_ensure(keyconf, "Face Mask", 0, 0);
1070   keymap->poll = facemask_paint_poll;
1071
1072   /* paint stroke */
1073   keymap = paint_stroke_modal_keymap(keyconf);
1074   WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
1075 }