Cleanup: style, use braces for editors
[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 static int brush_uv_sculpt_tool_set_exec(bContext *C, wmOperator *op)
528 {
529   Brush *brush;
530   Scene *scene = CTX_data_scene(C);
531   ToolSettings *ts = scene->toolsettings;
532   ts->uv_sculpt_tool = RNA_enum_get(op->ptr, "tool");
533   brush = ts->uvsculpt->paint.brush;
534   /* To update toolshelf */
535   WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
536
537   return OPERATOR_FINISHED;
538 }
539
540 static void BRUSH_OT_uv_sculpt_tool_set(wmOperatorType *ot)
541 {
542   /* identifiers */
543   ot->name = "UV Sculpt Tool Set";
544   ot->description = "Set the UV sculpt tool";
545   ot->idname = "BRUSH_OT_uv_sculpt_tool_set";
546
547   /* api callbacks */
548   ot->exec = brush_uv_sculpt_tool_set_exec;
549   ot->poll = uv_sculpt_poll;
550
551   /* flags */
552   ot->flag = 0;
553
554   /* props */
555   ot->prop = RNA_def_enum(ot->srna, "tool", rna_enum_uv_sculpt_tool_items, 0, "Tool", "");
556 }
557
558 /***** Stencil Control *****/
559
560 typedef enum {
561   STENCIL_TRANSLATE,
562   STENCIL_SCALE,
563   STENCIL_ROTATE,
564 } StencilControlMode;
565
566 typedef enum {
567   STENCIL_PRIMARY = 0,
568   STENCIL_SECONDARY = 1,
569 } StencilTextureMode;
570
571 typedef enum {
572   STENCIL_CONSTRAINT_X = 1,
573   STENCIL_CONSTRAINT_Y = 2,
574 } StencilConstraint;
575
576 typedef struct {
577   float init_mouse[2];
578   float init_spos[2];
579   float init_sdim[2];
580   float init_rot;
581   float init_angle;
582   float lenorig;
583   float area_size[2];
584   StencilControlMode mode;
585   StencilConstraint constrain_mode;
586   int mask; /* we are twaking mask or colour stencil */
587   Brush *br;
588   float *dim_target;
589   float *rot_target;
590   float *pos_target;
591   short event_type;
592 } StencilControlData;
593
594 static void stencil_set_target(StencilControlData *scd)
595 {
596   Brush *br = scd->br;
597   float mdiff[2];
598   if (scd->mask) {
599     copy_v2_v2(scd->init_sdim, br->mask_stencil_dimension);
600     copy_v2_v2(scd->init_spos, br->mask_stencil_pos);
601     scd->init_rot = br->mask_mtex.rot;
602
603     scd->dim_target = br->mask_stencil_dimension;
604     scd->rot_target = &br->mask_mtex.rot;
605     scd->pos_target = br->mask_stencil_pos;
606
607     sub_v2_v2v2(mdiff, scd->init_mouse, br->mask_stencil_pos);
608   }
609   else {
610     copy_v2_v2(scd->init_sdim, br->stencil_dimension);
611     copy_v2_v2(scd->init_spos, br->stencil_pos);
612     scd->init_rot = br->mtex.rot;
613
614     scd->dim_target = br->stencil_dimension;
615     scd->rot_target = &br->mtex.rot;
616     scd->pos_target = br->stencil_pos;
617
618     sub_v2_v2v2(mdiff, scd->init_mouse, br->stencil_pos);
619   }
620
621   scd->lenorig = len_v2(mdiff);
622
623   scd->init_angle = atan2f(mdiff[1], mdiff[0]);
624 }
625
626 static int stencil_control_invoke(bContext *C, wmOperator *op, const wmEvent *event)
627 {
628   Paint *paint = BKE_paint_get_active_from_context(C);
629   Brush *br = BKE_paint_brush(paint);
630   float mvalf[2] = {event->mval[0], event->mval[1]};
631   ARegion *ar = CTX_wm_region(C);
632   StencilControlData *scd;
633   int mask = RNA_enum_get(op->ptr, "texmode");
634
635   if (mask) {
636     if (br->mask_mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL) {
637       return OPERATOR_CANCELLED;
638     }
639   }
640   else {
641     if (br->mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL) {
642       return OPERATOR_CANCELLED;
643     }
644   }
645
646   scd = MEM_mallocN(sizeof(StencilControlData), "stencil_control");
647   scd->mask = mask;
648   scd->br = br;
649
650   copy_v2_v2(scd->init_mouse, mvalf);
651
652   stencil_set_target(scd);
653
654   scd->mode = RNA_enum_get(op->ptr, "mode");
655   scd->event_type = event->type;
656   scd->area_size[0] = ar->winx;
657   scd->area_size[1] = ar->winy;
658
659   op->customdata = scd;
660   WM_event_add_modal_handler(C, op);
661
662   return OPERATOR_RUNNING_MODAL;
663 }
664
665 static void stencil_restore(StencilControlData *scd)
666 {
667   copy_v2_v2(scd->dim_target, scd->init_sdim);
668   copy_v2_v2(scd->pos_target, scd->init_spos);
669   *scd->rot_target = scd->init_rot;
670 }
671
672 static void stencil_control_cancel(bContext *UNUSED(C), wmOperator *op)
673 {
674   StencilControlData *scd = op->customdata;
675
676   stencil_restore(scd);
677   MEM_freeN(op->customdata);
678 }
679
680 static void stencil_control_calculate(StencilControlData *scd, const int mval[2])
681 {
682 #define PIXEL_MARGIN 5
683
684   float mdiff[2];
685   float mvalf[2] = {mval[0], mval[1]};
686   switch (scd->mode) {
687     case STENCIL_TRANSLATE:
688       sub_v2_v2v2(mdiff, mvalf, scd->init_mouse);
689       add_v2_v2v2(scd->pos_target, scd->init_spos, mdiff);
690       CLAMP(scd->pos_target[0],
691             -scd->dim_target[0] + PIXEL_MARGIN,
692             scd->area_size[0] + scd->dim_target[0] - PIXEL_MARGIN);
693
694       CLAMP(scd->pos_target[1],
695             -scd->dim_target[1] + PIXEL_MARGIN,
696             scd->area_size[1] + scd->dim_target[1] - PIXEL_MARGIN);
697
698       break;
699     case STENCIL_SCALE: {
700       float len, factor;
701       sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
702       len = len_v2(mdiff);
703       factor = len / scd->lenorig;
704       copy_v2_v2(mdiff, scd->init_sdim);
705       if (scd->constrain_mode != STENCIL_CONSTRAINT_Y) {
706         mdiff[0] = factor * scd->init_sdim[0];
707       }
708       if (scd->constrain_mode != STENCIL_CONSTRAINT_X) {
709         mdiff[1] = factor * scd->init_sdim[1];
710       }
711       CLAMP(mdiff[0], 5.0f, 10000.0f);
712       CLAMP(mdiff[1], 5.0f, 10000.0f);
713       copy_v2_v2(scd->dim_target, mdiff);
714       break;
715     }
716     case STENCIL_ROTATE: {
717       float angle;
718       sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
719       angle = atan2f(mdiff[1], mdiff[0]);
720       angle = scd->init_rot + angle - scd->init_angle;
721       if (angle < 0.0f) {
722         angle += (float)(2 * M_PI);
723       }
724       if (angle > (float)(2 * M_PI)) {
725         angle -= (float)(2 * M_PI);
726       }
727       *scd->rot_target = angle;
728       break;
729     }
730   }
731 #undef PIXEL_MARGIN
732 }
733
734 static int stencil_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
735 {
736   StencilControlData *scd = op->customdata;
737
738   if (event->type == scd->event_type && event->val == KM_RELEASE) {
739     MEM_freeN(op->customdata);
740     WM_event_add_notifier(C, NC_WINDOW, NULL);
741     return OPERATOR_FINISHED;
742   }
743
744   switch (event->type) {
745     case MOUSEMOVE:
746       stencil_control_calculate(scd, event->mval);
747       break;
748     case ESCKEY:
749       if (event->val == KM_PRESS) {
750         stencil_control_cancel(C, op);
751         WM_event_add_notifier(C, NC_WINDOW, NULL);
752         return OPERATOR_CANCELLED;
753       }
754       break;
755     case XKEY:
756       if (event->val == KM_PRESS) {
757
758         if (scd->constrain_mode == STENCIL_CONSTRAINT_X) {
759           scd->constrain_mode = 0;
760         }
761         else {
762           scd->constrain_mode = STENCIL_CONSTRAINT_X;
763         }
764
765         stencil_control_calculate(scd, event->mval);
766       }
767       break;
768     case YKEY:
769       if (event->val == KM_PRESS) {
770         if (scd->constrain_mode == STENCIL_CONSTRAINT_Y) {
771           scd->constrain_mode = 0;
772         }
773         else {
774           scd->constrain_mode = STENCIL_CONSTRAINT_Y;
775         }
776
777         stencil_control_calculate(scd, event->mval);
778       }
779       break;
780     default:
781       break;
782   }
783
784   ED_region_tag_redraw(CTX_wm_region(C));
785
786   return OPERATOR_RUNNING_MODAL;
787 }
788
789 static bool stencil_control_poll(bContext *C)
790 {
791   ePaintMode mode = BKE_paintmode_get_active_from_context(C);
792
793   Paint *paint;
794   Brush *br;
795
796   if (!paint_supports_texture(mode)) {
797     return false;
798   }
799
800   paint = BKE_paint_get_active_from_context(C);
801   br = BKE_paint_brush(paint);
802   return (br && (br->mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL ||
803                  br->mask_mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL));
804 }
805
806 static void BRUSH_OT_stencil_control(wmOperatorType *ot)
807 {
808   static const EnumPropertyItem stencil_control_items[] = {
809       {STENCIL_TRANSLATE, "TRANSLATION", 0, "Translation", ""},
810       {STENCIL_SCALE, "SCALE", 0, "Scale", ""},
811       {STENCIL_ROTATE, "ROTATION", 0, "Rotation", ""},
812       {0, NULL, 0, NULL, NULL},
813   };
814
815   static const EnumPropertyItem stencil_texture_items[] = {
816       {STENCIL_PRIMARY, "PRIMARY", 0, "Primary", ""},
817       {STENCIL_SECONDARY, "SECONDARY", 0, "Secondary", ""},
818       {0, NULL, 0, NULL, NULL},
819   };
820   /* identifiers */
821   ot->name = "Stencil Brush Control";
822   ot->description = "Control the stencil brush";
823   ot->idname = "BRUSH_OT_stencil_control";
824
825   /* api callbacks */
826   ot->invoke = stencil_control_invoke;
827   ot->modal = stencil_control_modal;
828   ot->cancel = stencil_control_cancel;
829   ot->poll = stencil_control_poll;
830
831   /* flags */
832   ot->flag = 0;
833
834   PropertyRNA *prop;
835   prop = RNA_def_enum(ot->srna, "mode", stencil_control_items, STENCIL_TRANSLATE, "Tool", "");
836   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
837   prop = RNA_def_enum(ot->srna, "texmode", stencil_texture_items, STENCIL_PRIMARY, "Tool", "");
838   RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
839 }
840
841 static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *op)
842 {
843   Paint *paint = BKE_paint_get_active_from_context(C);
844   Brush *br = BKE_paint_brush(paint);
845   bool use_scale = RNA_boolean_get(op->ptr, "use_scale");
846   bool use_repeat = RNA_boolean_get(op->ptr, "use_repeat");
847   bool do_mask = RNA_boolean_get(op->ptr, "mask");
848   Tex *tex = NULL;
849   MTex *mtex = NULL;
850   if (br) {
851     mtex = do_mask ? &br->mask_mtex : &br->mtex;
852     tex = mtex->tex;
853   }
854
855   if (tex && tex->type == TEX_IMAGE && tex->ima) {
856     float aspx, aspy;
857     Image *ima = tex->ima;
858     float orig_area, stencil_area, factor;
859     ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
860
861     if (use_scale) {
862       aspx *= mtex->size[0];
863       aspy *= mtex->size[1];
864     }
865
866     if (use_repeat && tex->extend == TEX_REPEAT) {
867       aspx *= tex->xrepeat;
868       aspy *= tex->yrepeat;
869     }
870
871     orig_area = aspx * aspy;
872
873     if (do_mask) {
874       stencil_area = br->mask_stencil_dimension[0] * br->mask_stencil_dimension[1];
875     }
876     else {
877       stencil_area = br->stencil_dimension[0] * br->stencil_dimension[1];
878     }
879
880     factor = sqrtf(stencil_area / orig_area);
881
882     if (do_mask) {
883       br->mask_stencil_dimension[0] = factor * aspx;
884       br->mask_stencil_dimension[1] = factor * aspy;
885     }
886     else {
887       br->stencil_dimension[0] = factor * aspx;
888       br->stencil_dimension[1] = factor * aspy;
889     }
890   }
891
892   WM_event_add_notifier(C, NC_WINDOW, NULL);
893
894   return OPERATOR_FINISHED;
895 }
896
897 static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
898 {
899   /* identifiers */
900   ot->name = "Image Aspect";
901   ot->description =
902       "When using an image texture, adjust the stencil size to fit the image aspect ratio";
903   ot->idname = "BRUSH_OT_stencil_fit_image_aspect";
904
905   /* api callbacks */
906   ot->exec = stencil_fit_image_aspect_exec;
907   ot->poll = stencil_control_poll;
908
909   /* flags */
910   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
911
912   RNA_def_boolean(ot->srna, "use_repeat", 1, "Use Repeat", "Use repeat mapping values");
913   RNA_def_boolean(ot->srna, "use_scale", 1, "Use Scale", "Use texture scale values");
914   RNA_def_boolean(
915       ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
916 }
917
918 static int stencil_reset_transform_exec(bContext *C, wmOperator *op)
919 {
920   Paint *paint = BKE_paint_get_active_from_context(C);
921   Brush *br = BKE_paint_brush(paint);
922   bool do_mask = RNA_boolean_get(op->ptr, "mask");
923
924   if (!br) {
925     return OPERATOR_CANCELLED;
926   }
927
928   if (do_mask) {
929     br->mask_stencil_pos[0] = 256;
930     br->mask_stencil_pos[1] = 256;
931
932     br->mask_stencil_dimension[0] = 256;
933     br->mask_stencil_dimension[1] = 256;
934
935     br->mask_mtex.rot = 0;
936   }
937   else {
938     br->stencil_pos[0] = 256;
939     br->stencil_pos[1] = 256;
940
941     br->stencil_dimension[0] = 256;
942     br->stencil_dimension[1] = 256;
943
944     br->mtex.rot = 0;
945   }
946
947   WM_event_add_notifier(C, NC_WINDOW, NULL);
948
949   return OPERATOR_FINISHED;
950 }
951
952 static void BRUSH_OT_stencil_reset_transform(wmOperatorType *ot)
953 {
954   /* identifiers */
955   ot->name = "Reset Transform";
956   ot->description = "Reset the stencil transformation to the default";
957   ot->idname = "BRUSH_OT_stencil_reset_transform";
958
959   /* api callbacks */
960   ot->exec = stencil_reset_transform_exec;
961   ot->poll = stencil_control_poll;
962
963   /* flags */
964   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
965
966   RNA_def_boolean(
967       ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
968 }
969
970 /**************************** registration **********************************/
971
972 void ED_operatormacros_paint(void)
973 {
974   wmOperatorType *ot;
975   wmOperatorTypeMacro *otmacro;
976
977   ot = WM_operatortype_append_macro("PAINTCURVE_OT_add_point_slide",
978                                     "Add Curve Point and Slide",
979                                     "Add new curve point and slide it",
980                                     OPTYPE_UNDO);
981   ot->description = "Add new curve point and slide it";
982   WM_operatortype_macro_define(ot, "PAINTCURVE_OT_add_point");
983   otmacro = WM_operatortype_macro_define(ot, "PAINTCURVE_OT_slide");
984   RNA_boolean_set(otmacro->ptr, "align", true);
985   RNA_boolean_set(otmacro->ptr, "select", false);
986 }
987
988 void ED_operatortypes_paint(void)
989 {
990   /* palette */
991   WM_operatortype_append(PALETTE_OT_new);
992   WM_operatortype_append(PALETTE_OT_color_add);
993   WM_operatortype_append(PALETTE_OT_color_delete);
994
995   /* paint curve */
996   WM_operatortype_append(PAINTCURVE_OT_new);
997   WM_operatortype_append(PAINTCURVE_OT_add_point);
998   WM_operatortype_append(PAINTCURVE_OT_delete_point);
999   WM_operatortype_append(PAINTCURVE_OT_select);
1000   WM_operatortype_append(PAINTCURVE_OT_slide);
1001   WM_operatortype_append(PAINTCURVE_OT_draw);
1002   WM_operatortype_append(PAINTCURVE_OT_cursor);
1003
1004   /* brush */
1005   WM_operatortype_append(BRUSH_OT_add);
1006   WM_operatortype_append(BRUSH_OT_add_gpencil);
1007   WM_operatortype_append(BRUSH_OT_scale_size);
1008   WM_operatortype_append(BRUSH_OT_curve_preset);
1009   WM_operatortype_append(BRUSH_OT_reset);
1010   WM_operatortype_append(BRUSH_OT_stencil_control);
1011   WM_operatortype_append(BRUSH_OT_stencil_fit_image_aspect);
1012   WM_operatortype_append(BRUSH_OT_stencil_reset_transform);
1013
1014   /* note, particle uses a different system, can be added with existing operators in wm.py */
1015   WM_operatortype_append(PAINT_OT_brush_select);
1016   WM_operatortype_append(BRUSH_OT_uv_sculpt_tool_set);
1017
1018   /* image */
1019   WM_operatortype_append(PAINT_OT_texture_paint_toggle);
1020   WM_operatortype_append(PAINT_OT_image_paint);
1021   WM_operatortype_append(PAINT_OT_sample_color);
1022   WM_operatortype_append(PAINT_OT_grab_clone);
1023   WM_operatortype_append(PAINT_OT_project_image);
1024   WM_operatortype_append(PAINT_OT_image_from_view);
1025   WM_operatortype_append(PAINT_OT_brush_colors_flip);
1026   WM_operatortype_append(PAINT_OT_add_texture_paint_slot);
1027   WM_operatortype_append(PAINT_OT_add_simple_uvs);
1028
1029   /* weight */
1030   WM_operatortype_append(PAINT_OT_weight_paint_toggle);
1031   WM_operatortype_append(PAINT_OT_weight_paint);
1032   WM_operatortype_append(PAINT_OT_weight_set);
1033   WM_operatortype_append(PAINT_OT_weight_from_bones);
1034   WM_operatortype_append(PAINT_OT_weight_gradient);
1035   WM_operatortype_append(PAINT_OT_weight_sample);
1036   WM_operatortype_append(PAINT_OT_weight_sample_group);
1037
1038   /* uv */
1039   WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
1040
1041   /* vertex selection */
1042   WM_operatortype_append(PAINT_OT_vert_select_all);
1043   WM_operatortype_append(PAINT_OT_vert_select_ungrouped);
1044
1045   /* vertex */
1046   WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
1047   WM_operatortype_append(PAINT_OT_vertex_paint);
1048   WM_operatortype_append(PAINT_OT_vertex_color_set);
1049   WM_operatortype_append(PAINT_OT_vertex_color_smooth);
1050
1051   WM_operatortype_append(PAINT_OT_vertex_color_brightness_contrast);
1052   WM_operatortype_append(PAINT_OT_vertex_color_hsv);
1053   WM_operatortype_append(PAINT_OT_vertex_color_invert);
1054   WM_operatortype_append(PAINT_OT_vertex_color_levels);
1055   WM_operatortype_append(PAINT_OT_vertex_color_from_weight);
1056
1057   /* face-select */
1058   WM_operatortype_append(PAINT_OT_face_select_linked);
1059   WM_operatortype_append(PAINT_OT_face_select_linked_pick);
1060   WM_operatortype_append(PAINT_OT_face_select_all);
1061   WM_operatortype_append(PAINT_OT_face_select_hide);
1062   WM_operatortype_append(PAINT_OT_face_select_reveal);
1063
1064   /* partial visibility */
1065   WM_operatortype_append(PAINT_OT_hide_show);
1066
1067   /* paint masking */
1068   WM_operatortype_append(PAINT_OT_mask_flood_fill);
1069   WM_operatortype_append(PAINT_OT_mask_lasso_gesture);
1070 }
1071
1072 void ED_keymap_paint(wmKeyConfig *keyconf)
1073 {
1074   wmKeyMap *keymap;
1075
1076   keymap = WM_keymap_ensure(keyconf, "Paint Curve", 0, 0);
1077   keymap->poll = paint_curve_poll;
1078
1079   /* Sculpt mode */
1080   keymap = WM_keymap_ensure(keyconf, "Sculpt", 0, 0);
1081   keymap->poll = sculpt_mode_poll;
1082
1083   /* Vertex Paint mode */
1084   keymap = WM_keymap_ensure(keyconf, "Vertex Paint", 0, 0);
1085   keymap->poll = vertex_paint_mode_poll;
1086
1087   /* Weight Paint mode */
1088   keymap = WM_keymap_ensure(keyconf, "Weight Paint", 0, 0);
1089   keymap->poll = weight_paint_mode_poll;
1090
1091   /*Weight paint's Vertex Selection Mode */
1092   keymap = WM_keymap_ensure(keyconf, "Weight Paint Vertex Selection", 0, 0);
1093   keymap->poll = vert_paint_poll;
1094
1095   /* Image/Texture Paint mode */
1096   keymap = WM_keymap_ensure(keyconf, "Image Paint", 0, 0);
1097   keymap->poll = image_texture_paint_poll;
1098
1099   /* face-mask mode */
1100   keymap = WM_keymap_ensure(keyconf, "Face Mask", 0, 0);
1101   keymap->poll = facemask_paint_poll;
1102
1103   keymap = WM_keymap_ensure(keyconf, "UV Sculpt", 0, 0);
1104   keymap->poll = uv_sculpt_keymap_poll;
1105
1106   /* paint stroke */
1107   keymap = paint_stroke_modal_keymap(keyconf);
1108   WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
1109 }