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