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