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