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
33 #include "DNA_customdata_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_brush_types.h"
37
38 #include "BKE_brush.h"
39 #include "BKE_context.h"
40 #include "BKE_paint.h"
41 #include "BKE_main.h"
42
43 #include "ED_paint.h"
44 #include "ED_screen.h"
45 #include "ED_image.h"
46 #include "UI_resources.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50 #include "WM_toolsystem.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54 #include "RNA_enum_types.h"
55
56 #include "paint_intern.h"
57 #include "sculpt_intern.h"
58
59 #include <string.h>
60 //#include <stdio.h>
61 #include <stddef.h>
62
63 /* Brush operators */
64 static int brush_add_exec(bContext *C, wmOperator *UNUSED(op))
65 {
66         /*int type = RNA_enum_get(op->ptr, "type");*/
67         Paint *paint = BKE_paint_get_active_from_context(C);
68         Brush *br = BKE_paint_brush(paint);
69         Main *bmain = CTX_data_main(C);
70         ePaintMode mode = BKE_paintmode_get_active_from_context(C);
71
72         if (br) {
73                 br = BKE_brush_copy(bmain, br);
74         }
75         else {
76                 br = BKE_brush_add(bmain, "Brush", BKE_paint_object_mode_from_paint_mode(mode));
77                 id_us_min(&br->id);  /* fake user only */
78         }
79
80         BKE_paint_brush_set(paint, br);
81
82         return OPERATOR_FINISHED;
83 }
84
85 static void BRUSH_OT_add(wmOperatorType *ot)
86 {
87         /* identifiers */
88         ot->name = "Add Brush";
89         ot->description = "Add brush by mode type";
90         ot->idname = "BRUSH_OT_add";
91
92         /* api callbacks */
93         ot->exec = brush_add_exec;
94
95         /* flags */
96         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
97 }
98
99
100 static int brush_scale_size_exec(bContext *C, wmOperator *op)
101 {
102         Scene *scene = CTX_data_scene(C);
103         Paint  *paint =  BKE_paint_get_active_from_context(C);
104         Brush  *brush =  BKE_paint_brush(paint);
105         // Object *ob = CTX_data_active_object(C);
106         float scalar = RNA_float_get(op->ptr, "scalar");
107
108         if (brush) {
109                 // pixel radius
110                 {
111                         const int old_size = BKE_brush_size_get(scene, brush);
112                         int size = (int)(scalar * old_size);
113
114                         if (abs(old_size - size) < U.pixelsize) {
115                                 if (scalar > 1) {
116                                         size += U.pixelsize;
117                                 }
118                                 else if (scalar < 1) {
119                                         size -= U.pixelsize;
120                                 }
121                         }
122
123                         BKE_brush_size_set(scene, brush, size);
124                 }
125
126                 // unprojected radius
127                 {
128                         float unprojected_radius = scalar * BKE_brush_unprojected_radius_get(scene, brush);
129
130                         if (unprojected_radius < 0.001f) // XXX magic number
131                                 unprojected_radius = 0.001f;
132
133                         BKE_brush_unprojected_radius_set(scene, brush, unprojected_radius);
134                 }
135
136                 WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
137         }
138
139         return OPERATOR_FINISHED;
140 }
141
142 static void BRUSH_OT_scale_size(wmOperatorType *ot)
143 {
144         /* identifiers */
145         ot->name = "Scale Sculpt/Paint Brush Size";
146         ot->description = "Change brush size by a scalar";
147         ot->idname = "BRUSH_OT_scale_size";
148
149         /* api callbacks */
150         ot->exec = brush_scale_size_exec;
151
152         /* flags */
153         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
154
155         RNA_def_float(ot->srna, "scalar", 1, 0, 2, "Scalar", "Factor to scale brush size by", 0, 2);
156 }
157
158 /* Palette operators */
159
160 static int palette_new_exec(bContext *C, wmOperator *UNUSED(op))
161 {
162         Paint *paint = BKE_paint_get_active_from_context(C);
163         Main *bmain = CTX_data_main(C);
164         Palette *palette;
165
166         palette = BKE_palette_add(bmain, "Palette");
167
168         BKE_paint_palette_set(paint, palette);
169
170         return OPERATOR_FINISHED;
171 }
172
173 static void PALETTE_OT_new(wmOperatorType *ot)
174 {
175         /* identifiers */
176         ot->name = "Add New Palette";
177         ot->description = "Add new palette";
178         ot->idname = "PALETTE_OT_new";
179
180         /* api callbacks */
181         ot->exec = palette_new_exec;
182
183         /* flags */
184         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
185 }
186
187 static bool palette_poll(bContext *C)
188 {
189         Paint *paint = BKE_paint_get_active_from_context(C);
190
191         if (paint && paint->palette != NULL)
192                 return true;
193
194         return false;
195 }
196
197 static int palette_color_add_exec(bContext *C, wmOperator *UNUSED(op))
198 {
199         Scene *scene = CTX_data_scene(C);
200         Paint *paint = BKE_paint_get_active_from_context(C);
201         Brush *brush = paint->brush;
202         ePaintMode mode = BKE_paintmode_get_active_from_context(C);
203         Palette *palette = paint->palette;
204         PaletteColor *color;
205
206         color = BKE_palette_color_add(palette);
207         palette->active_color = BLI_listbase_count(&palette->colors) - 1;
208
209         if (ELEM(mode, ePaintTextureProjective, ePaintTexture2D, ePaintVertex)) {
210                 copy_v3_v3(color->rgb, BKE_brush_color_get(scene, brush));
211                 color->value = 0.0;
212         }
213         else if (mode == ePaintWeight) {
214                 zero_v3(color->rgb);
215                 color->value = brush->weight;
216         }
217
218         return OPERATOR_FINISHED;
219 }
220
221 static void PALETTE_OT_color_add(wmOperatorType *ot)
222 {
223         /* identifiers */
224         ot->name = "New Palette Color";
225         ot->description = "Add new color to active palette";
226         ot->idname = "PALETTE_OT_color_add";
227
228         /* api callbacks */
229         ot->exec = palette_color_add_exec;
230         ot->poll = palette_poll;
231         /* flags */
232         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
233 }
234
235
236 static int palette_color_delete_exec(bContext *C, wmOperator *UNUSED(op))
237 {
238         Paint *paint = BKE_paint_get_active_from_context(C);
239         Palette *palette = paint->palette;
240         PaletteColor *color = BLI_findlink(&palette->colors, palette->active_color);
241
242         if (color) {
243                 BKE_palette_color_remove(palette, color);
244         }
245
246         return OPERATOR_FINISHED;
247 }
248
249 static void PALETTE_OT_color_delete(wmOperatorType *ot)
250 {
251         /* identifiers */
252         ot->name = "Delete Palette Color";
253         ot->description = "Remove active color from palette";
254         ot->idname = "PALETTE_OT_color_delete";
255
256         /* api callbacks */
257         ot->exec = palette_color_delete_exec;
258         ot->poll = palette_poll;
259         /* flags */
260         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
261 }
262
263 static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op))
264 {
265         Paint *paint = BKE_paint_get_active_from_context(C);
266         Brush *brush = BKE_paint_brush(paint);
267         Object *ob = CTX_data_active_object(C);
268
269         if (!ob || !brush) return OPERATOR_CANCELLED;
270
271         /* TODO: other modes */
272         if (ob->mode & OB_MODE_SCULPT) {
273                 BKE_brush_sculpt_reset(brush);
274         }
275         else {
276                 return OPERATOR_CANCELLED;
277         }
278         WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
279
280         return OPERATOR_FINISHED;
281 }
282
283 static void BRUSH_OT_reset(wmOperatorType *ot)
284 {
285         /* identifiers */
286         ot->name = "Reset Brush";
287         ot->description = "Return brush to defaults based on current tool";
288         ot->idname = "BRUSH_OT_reset";
289
290         /* api callbacks */
291         ot->exec = brush_reset_exec;
292
293         /* flags */
294         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
295 }
296
297 static int brush_tool(const Brush *brush, size_t tool_offset)
298 {
299         return *(((char *)brush) + tool_offset);
300 }
301
302 static void brush_tool_set(const Brush *brush, size_t tool_offset, int tool)
303 {
304         *(((char *)brush) + tool_offset) = tool;
305 }
306
307 /* generic functions for setting the active brush based on the tool */
308 static Brush *brush_tool_cycle(Main *bmain, Brush *brush_orig, const int tool, const size_t tool_offset, const int ob_mode)
309 {
310         Brush *brush, *first_brush;
311
312         if (!brush_orig && !(brush_orig = bmain->brush.first)) {
313                 return NULL;
314         }
315
316         if (brush_tool(brush_orig, tool_offset) != tool) {
317                 /* If current brush's tool is different from what we need,
318                  * start cycling from the beginning of the list.
319                  * Such logic will activate the same exact brush not relating from
320                  * which tool user requests other tool.
321                  */
322                 first_brush = bmain->brush.first;
323         }
324         else {
325                 /* If user wants to switch to brush with the same  tool as
326                  * currently active brush do a cycling via all possible
327                  * brushes with requested tool.
328                  */
329                 first_brush = brush_orig->id.next ? brush_orig->id.next : bmain->brush.first;
330         }
331
332         /* get the next brush with the active tool */
333         brush = first_brush;
334         do {
335                 if ((brush->ob_mode & ob_mode) &&
336                     (brush_tool(brush, tool_offset) == tool))
337                 {
338                         return brush;
339                 }
340
341                 brush = brush->id.next ? brush->id.next : bmain->brush.first;
342         } while (brush != first_brush);
343
344         return NULL;
345 }
346
347 static Brush *brush_tool_toggle(Main *bmain, Brush *brush_orig, const int tool, const size_t tool_offset, const int ob_mode)
348 {
349         if (!brush_orig || brush_tool(brush_orig, tool_offset) != tool) {
350                 Brush *br;
351                 /* if the current brush is not using the desired tool, look
352                  * for one that is */
353                 br = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
354                 /* store the previously-selected brush */
355                 if (br)
356                         br->toggle_brush = brush_orig;
357
358                 return br;
359         }
360         else if (brush_orig->toggle_brush) {
361                 /* if current brush is using the desired tool, try to toggle
362                  * back to the previously selected brush. */
363                 return brush_orig->toggle_brush;
364         }
365         else
366                 return NULL;
367 }
368
369 static int brush_generic_tool_set(
370         Main *bmain, Paint *paint, const int tool,
371         const size_t tool_offset, const int ob_mode,
372         const char *tool_name, const bool create_missing,
373         const bool toggle)
374 {
375         Brush *brush, *brush_orig = BKE_paint_brush(paint);
376
377         if (toggle)
378                 brush = brush_tool_toggle(bmain, brush_orig, tool, tool_offset, ob_mode);
379         else
380                 brush = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
381
382         if (!brush && brush_tool(brush_orig, tool_offset) != tool && create_missing) {
383                 brush = BKE_brush_add(bmain, tool_name, ob_mode);
384                 id_us_min(&brush->id);  /* fake user only */
385                 brush_tool_set(brush, tool_offset, tool);
386                 brush->toggle_brush = brush_orig;
387         }
388
389         if (brush) {
390                 BKE_paint_brush_set(paint, brush);
391                 BKE_paint_invalidate_overlay_all();
392
393                 WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
394                 return OPERATOR_FINISHED;
395         }
396         else {
397                 return OPERATOR_CANCELLED;
398         }
399 }
400
401 /* used in the PAINT_OT_brush_select operator */
402 #define OB_MODE_ACTIVE 0
403
404 static int brush_select_exec(bContext *C, wmOperator *op)
405 {
406         Main *bmain = CTX_data_main(C);
407         ToolSettings *toolsettings = CTX_data_tool_settings(C);
408         Paint *paint = NULL;
409         int tool, paint_mode = RNA_enum_get(op->ptr, "paint_mode");
410         const bool create_missing = RNA_boolean_get(op->ptr, "create_missing");
411         const bool toggle = RNA_boolean_get(op->ptr, "toggle");
412         const char *tool_name = "Brush";
413         size_t tool_offset;
414
415         if (paint_mode == OB_MODE_ACTIVE) {
416                 Object *ob = CTX_data_active_object(C);
417                 if (ob) {
418                         /* select current paint mode */
419                         paint_mode = ob->mode & OB_MODE_ALL_PAINT;
420                 }
421                 else {
422                         return OPERATOR_CANCELLED;
423                 }
424         }
425
426         switch (paint_mode) {
427                 case OB_MODE_SCULPT:
428                         paint = &toolsettings->sculpt->paint;
429                         tool_offset = offsetof(Brush, sculpt_tool);
430                         tool = RNA_enum_get(op->ptr, "sculpt_tool");
431                         RNA_enum_name_from_value(rna_enum_brush_sculpt_tool_items, tool, &tool_name);
432                         break;
433                 case OB_MODE_VERTEX_PAINT:
434                         paint = &toolsettings->vpaint->paint;
435                         tool_offset = offsetof(Brush, vertexpaint_tool);
436                         tool = RNA_enum_get(op->ptr, "vertex_paint_tool");
437                         RNA_enum_name_from_value(rna_enum_brush_vertex_tool_items, tool, &tool_name);
438                         break;
439                 case OB_MODE_WEIGHT_PAINT:
440                         paint = &toolsettings->wpaint->paint;
441                         /* vertexpaint_tool is used for weight paint mode */
442                         tool_offset = offsetof(Brush, vertexpaint_tool);
443                         tool = RNA_enum_get(op->ptr, "weight_paint_tool");
444                         RNA_enum_name_from_value(rna_enum_brush_vertex_tool_items, tool, &tool_name);
445                         break;
446                 case OB_MODE_TEXTURE_PAINT:
447                         paint = &toolsettings->imapaint.paint;
448                         tool_offset = offsetof(Brush, imagepaint_tool);
449                         tool = RNA_enum_get(op->ptr, "texture_paint_tool");
450                         RNA_enum_name_from_value(rna_enum_brush_image_tool_items, tool, &tool_name);
451                         break;
452                 default:
453                         /* invalid paint mode */
454                         return OPERATOR_CANCELLED;
455         }
456
457         /* TODO(campbell): Use the toolsystem for now, ideally the toolsystem will display brushes directly
458          * so we don't need to sync between tools and brushes. */
459         int ret = brush_generic_tool_set(
460                 bmain, paint, tool, tool_offset,
461                 paint_mode, tool_name, create_missing,
462                 toggle);
463
464         if ((ret == OPERATOR_FINISHED) && (paint->brush != NULL)) {
465                 Brush *brush = paint->brush;
466                 WorkSpace *workspace = CTX_wm_workspace(C);
467                 if (WM_toolsystem_ref_set_by_name(C, workspace, NULL, brush->id.name + 2, true)) {
468                         /* ok */
469                 }
470         }
471         return ret;
472 }
473
474 static void PAINT_OT_brush_select(wmOperatorType *ot)
475 {
476         static const EnumPropertyItem paint_mode_items[] = {
477                 {OB_MODE_ACTIVE, "ACTIVE", 0, "Current", "Set brush for active paint mode"},
478                 {OB_MODE_SCULPT, "SCULPT", ICON_SCULPTMODE_HLT, "Sculpt", ""},
479                 {OB_MODE_VERTEX_PAINT, "VERTEX_PAINT", ICON_VPAINT_HLT, "Vertex Paint", ""},
480                 {OB_MODE_WEIGHT_PAINT, "WEIGHT_PAINT", ICON_WPAINT_HLT, "Weight Paint", ""},
481                 {OB_MODE_TEXTURE_PAINT, "TEXTURE_PAINT", ICON_TPAINT_HLT, "Texture Paint", ""},
482                 {0, NULL, 0, NULL, NULL}
483         };
484         PropertyRNA *prop;
485
486         /* identifiers */
487         ot->name = "Brush Select";
488         ot->description = "Select a paint mode's brush by tool type";
489         ot->idname = "PAINT_OT_brush_select";
490
491         /* api callbacks */
492         ot->exec = brush_select_exec;
493
494         /* flags */
495         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
496
497         /* props */
498         RNA_def_enum(ot->srna, "paint_mode", paint_mode_items, OB_MODE_ACTIVE, "Paint Mode", "");
499         RNA_def_enum(ot->srna, "sculpt_tool", rna_enum_brush_sculpt_tool_items, 0, "Sculpt Tool", "");
500         RNA_def_enum(ot->srna, "vertex_paint_tool", rna_enum_brush_vertex_tool_items, 0, "Vertex Paint Tool", "");
501         RNA_def_enum(ot->srna, "weight_paint_tool", rna_enum_brush_vertex_tool_items, 0, "Weight Paint Tool", "");
502         RNA_def_enum(ot->srna, "texture_paint_tool", rna_enum_brush_image_tool_items, 0, "Texture Paint Tool", "");
503
504         prop = RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "Toggle between two brushes rather than cycling");
505         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
506         prop = RNA_def_boolean(ot->srna, "create_missing", 0, "Create Missing", "If the requested brush type does not exist, create a new brush");
507         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
508 }
509
510 static wmKeyMapItem *keymap_brush_select(
511         wmKeyMap *keymap, int paint_mode,
512         int tool, int keymap_type,
513         int keymap_modifier)
514 {
515         wmKeyMapItem *kmi;
516         kmi = WM_keymap_add_item(
517                 keymap, "PAINT_OT_brush_select",
518                 keymap_type, KM_PRESS, keymap_modifier, 0);
519
520         RNA_enum_set(kmi->ptr, "paint_mode", paint_mode);
521
522         switch (paint_mode) {
523                 case OB_MODE_SCULPT:
524                         RNA_enum_set(kmi->ptr, "sculpt_tool", tool);
525                         break;
526                 case OB_MODE_VERTEX_PAINT:
527                         RNA_enum_set(kmi->ptr, "vertex_paint_tool", tool);
528                         break;
529                 case OB_MODE_WEIGHT_PAINT:
530                         RNA_enum_set(kmi->ptr, "weight_paint_tool", tool);
531                         break;
532                 case OB_MODE_TEXTURE_PAINT:
533                         RNA_enum_set(kmi->ptr, "texture_paint_tool", tool);
534                         break;
535         }
536
537         return kmi;
538 }
539
540 static int brush_uv_sculpt_tool_set_exec(bContext *C, wmOperator *op)
541 {
542         Brush *brush;
543         Scene *scene = CTX_data_scene(C);
544         ToolSettings *ts = scene->toolsettings;
545         ts->uv_sculpt_tool = RNA_enum_get(op->ptr, "tool");
546         brush = ts->uvsculpt->paint.brush;
547         /* To update toolshelf */
548         WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
549
550         return OPERATOR_FINISHED;
551 }
552
553 static void BRUSH_OT_uv_sculpt_tool_set(wmOperatorType *ot)
554 {
555         /* identifiers */
556         ot->name = "UV Sculpt Tool Set";
557         ot->description = "Set the UV sculpt tool";
558         ot->idname = "BRUSH_OT_uv_sculpt_tool_set";
559
560         /* api callbacks */
561         ot->exec = brush_uv_sculpt_tool_set_exec;
562         ot->poll = uv_sculpt_poll;
563
564         /* flags */
565         ot->flag = 0;
566
567         /* props */
568         ot->prop = RNA_def_enum(ot->srna, "tool", rna_enum_uv_sculpt_tool_items, 0, "Tool", "");
569 }
570
571 /***** Stencil Control *****/
572
573 typedef enum {
574         STENCIL_TRANSLATE,
575         STENCIL_SCALE,
576         STENCIL_ROTATE
577 } StencilControlMode;
578
579 typedef enum {
580         STENCIL_PRIMARY = 0,
581         STENCIL_SECONDARY = 1
582 } StencilTextureMode;
583
584
585 typedef enum {
586         STENCIL_CONSTRAINT_X = 1,
587         STENCIL_CONSTRAINT_Y = 2
588 } StencilConstraint;
589
590 typedef struct {
591         float init_mouse[2];
592         float init_spos[2];
593         float init_sdim[2];
594         float init_rot;
595         float init_angle;
596         float lenorig;
597         float area_size[2];
598         StencilControlMode mode;
599         StencilConstraint constrain_mode;
600         int mask; /* we are twaking mask or colour stencil */
601         Brush *br;
602         float *dim_target;
603         float *rot_target;
604         float *pos_target;
605         short event_type;
606 } StencilControlData;
607
608 static void stencil_set_target(StencilControlData *scd)
609 {
610         Brush *br = scd->br;
611         float mdiff[2];
612         if (scd->mask) {
613                 copy_v2_v2(scd->init_sdim, br->mask_stencil_dimension);
614                 copy_v2_v2(scd->init_spos, br->mask_stencil_pos);
615                 scd->init_rot = br->mask_mtex.rot;
616
617                 scd->dim_target = br->mask_stencil_dimension;
618                 scd->rot_target = &br->mask_mtex.rot;
619                 scd->pos_target = br->mask_stencil_pos;
620
621                 sub_v2_v2v2(mdiff, scd->init_mouse, br->mask_stencil_pos);
622         }
623         else {
624                 copy_v2_v2(scd->init_sdim, br->stencil_dimension);
625                 copy_v2_v2(scd->init_spos, br->stencil_pos);
626                 scd->init_rot = br->mtex.rot;
627
628                 scd->dim_target = br->stencil_dimension;
629                 scd->rot_target = &br->mtex.rot;
630                 scd->pos_target = br->stencil_pos;
631
632                 sub_v2_v2v2(mdiff, scd->init_mouse, br->stencil_pos);
633         }
634
635         scd->lenorig = len_v2(mdiff);
636
637         scd->init_angle = atan2f(mdiff[1], mdiff[0]);
638 }
639
640 static int stencil_control_invoke(bContext *C, wmOperator *op, const wmEvent *event)
641 {
642         Paint *paint = BKE_paint_get_active_from_context(C);
643         Brush *br = BKE_paint_brush(paint);
644         float mvalf[2] = {event->mval[0], event->mval[1]};
645         ARegion *ar = CTX_wm_region(C);
646         StencilControlData *scd;
647         int mask = RNA_enum_get(op->ptr, "texmode");
648
649         if (mask) {
650                 if (br->mask_mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL)
651                         return OPERATOR_CANCELLED;
652         }
653         else {
654                 if (br->mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL)
655                         return OPERATOR_CANCELLED;
656         }
657
658         scd = MEM_mallocN(sizeof(StencilControlData), "stencil_control");
659         scd->mask = mask;
660         scd->br = br;
661
662         copy_v2_v2(scd->init_mouse, mvalf);
663
664         stencil_set_target(scd);
665
666         scd->mode = RNA_enum_get(op->ptr, "mode");
667         scd->event_type = event->type;
668         scd->area_size[0] = ar->winx;
669         scd->area_size[1] = ar->winy;
670
671
672         op->customdata = scd;
673         WM_event_add_modal_handler(C, op);
674
675         return OPERATOR_RUNNING_MODAL;
676 }
677
678 static void stencil_restore(StencilControlData *scd)
679 {
680         copy_v2_v2(scd->dim_target, scd->init_sdim);
681         copy_v2_v2(scd->pos_target, scd->init_spos);
682         *scd->rot_target = scd->init_rot;
683 }
684
685 static void stencil_control_cancel(bContext *UNUSED(C), wmOperator *op)
686 {
687         StencilControlData *scd = op->customdata;
688
689         stencil_restore(scd);
690         MEM_freeN(op->customdata);
691 }
692
693 static void stencil_control_calculate(StencilControlData *scd, const int mval[2])
694 {
695 #define PIXEL_MARGIN 5
696
697         float mdiff[2];
698         float mvalf[2] = {mval[0], mval[1]};
699         switch (scd->mode) {
700                 case STENCIL_TRANSLATE:
701                         sub_v2_v2v2(mdiff, mvalf, scd->init_mouse);
702                         add_v2_v2v2(scd->pos_target, scd->init_spos, mdiff);
703                         CLAMP(scd->pos_target[0],
704                               -scd->dim_target[0] + PIXEL_MARGIN,
705                               scd->area_size[0] + scd->dim_target[0] - PIXEL_MARGIN);
706
707                         CLAMP(scd->pos_target[1],
708                               -scd->dim_target[1] + PIXEL_MARGIN,
709                               scd->area_size[1] + scd->dim_target[1] - PIXEL_MARGIN);
710
711                         break;
712                 case STENCIL_SCALE:
713                 {
714                         float len, factor;
715                         sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
716                         len = len_v2(mdiff);
717                         factor = len / scd->lenorig;
718                         copy_v2_v2(mdiff, scd->init_sdim);
719                         if (scd->constrain_mode != STENCIL_CONSTRAINT_Y)
720                                 mdiff[0] = factor * scd->init_sdim[0];
721                         if (scd->constrain_mode != STENCIL_CONSTRAINT_X)
722                                 mdiff[1] = factor * scd->init_sdim[1];
723                         CLAMP(mdiff[0], 5.0f, 10000.0f);
724                         CLAMP(mdiff[1], 5.0f, 10000.0f);
725                         copy_v2_v2(scd->dim_target, mdiff);
726                         break;
727                 }
728                 case STENCIL_ROTATE:
729                 {
730                         float angle;
731                         sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
732                         angle = atan2f(mdiff[1], mdiff[0]);
733                         angle = scd->init_rot + angle - scd->init_angle;
734                         if (angle < 0.0f)
735                                 angle += (float)(2 * M_PI);
736                         if (angle > (float)(2 * M_PI))
737                                 angle -= (float)(2 * M_PI);
738                         *scd->rot_target = angle;
739                         break;
740                 }
741         }
742 #undef PIXEL_MARGIN
743 }
744
745 static int stencil_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
746 {
747         StencilControlData *scd = op->customdata;
748
749         if (event->type == scd->event_type && event->val == KM_RELEASE) {
750                 MEM_freeN(op->customdata);
751                 WM_event_add_notifier(C, NC_WINDOW, NULL);
752                 return OPERATOR_FINISHED;
753         }
754
755         switch (event->type) {
756                 case MOUSEMOVE:
757                         stencil_control_calculate(scd, event->mval);
758                         break;
759                 case ESCKEY:
760                         if (event->val == KM_PRESS) {
761                                 stencil_control_cancel(C, op);
762                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
763                                 return OPERATOR_CANCELLED;
764                         }
765                         break;
766                 case XKEY:
767                         if (event->val == KM_PRESS) {
768
769                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_X)
770                                         scd->constrain_mode = 0;
771                                 else
772                                         scd->constrain_mode = STENCIL_CONSTRAINT_X;
773
774                                 stencil_control_calculate(scd, event->mval);
775                         }
776                         break;
777                 case YKEY:
778                         if (event->val == KM_PRESS) {
779                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_Y)
780                                         scd->constrain_mode = 0;
781                                 else
782                                         scd->constrain_mode = STENCIL_CONSTRAINT_Y;
783
784                                 stencil_control_calculate(scd, event->mval);
785                         }
786                         break;
787                 default:
788                         break;
789         }
790
791         ED_region_tag_redraw(CTX_wm_region(C));
792
793         return OPERATOR_RUNNING_MODAL;
794 }
795
796 static bool stencil_control_poll(bContext *C)
797 {
798         ePaintMode mode = BKE_paintmode_get_active_from_context(C);
799
800         Paint *paint;
801         Brush *br;
802
803         if (!paint_supports_texture(mode))
804                 return false;
805
806         paint = BKE_paint_get_active_from_context(C);
807         br = BKE_paint_brush(paint);
808         return (br &&
809                 (br->mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL ||
810                  br->mask_mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL));
811 }
812
813 static void BRUSH_OT_stencil_control(wmOperatorType *ot)
814 {
815         static const EnumPropertyItem stencil_control_items[] = {
816                 {STENCIL_TRANSLATE, "TRANSLATION", 0, "Translation", ""},
817                 {STENCIL_SCALE, "SCALE", 0, "Scale", ""},
818                 {STENCIL_ROTATE, "ROTATION", 0, "Rotation", ""},
819                 {0, NULL, 0, NULL, NULL}
820         };
821
822         static const EnumPropertyItem stencil_texture_items[] = {
823                 {STENCIL_PRIMARY, "PRIMARY", 0, "Primary", ""},
824                 {STENCIL_SECONDARY, "SECONDARY", 0, "Secondary", ""},
825                 {0, NULL, 0, NULL, NULL}
826         };
827         /* identifiers */
828         ot->name = "Stencil Brush Control";
829         ot->description = "Control the stencil brush";
830         ot->idname = "BRUSH_OT_stencil_control";
831
832         /* api callbacks */
833         ot->invoke = stencil_control_invoke;
834         ot->modal = stencil_control_modal;
835         ot->cancel = stencil_control_cancel;
836         ot->poll = stencil_control_poll;
837
838         /* flags */
839         ot->flag = 0;
840
841         RNA_def_enum(ot->srna, "mode", stencil_control_items, STENCIL_TRANSLATE, "Tool", "");
842         RNA_def_enum(ot->srna, "texmode", stencil_texture_items, STENCIL_PRIMARY, "Tool", "");
843 }
844
845
846 static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *op)
847 {
848         Paint *paint = BKE_paint_get_active_from_context(C);
849         Brush *br = BKE_paint_brush(paint);
850         bool use_scale = RNA_boolean_get(op->ptr, "use_scale");
851         bool use_repeat = RNA_boolean_get(op->ptr, "use_repeat");
852         bool do_mask = RNA_boolean_get(op->ptr, "mask");
853         Tex *tex = NULL;
854         MTex *mtex = NULL;
855         if (br) {
856                 mtex = do_mask ? &br->mask_mtex : &br->mtex;
857                 tex = mtex->tex;
858         }
859
860         if (tex && tex->type == TEX_IMAGE && tex->ima) {
861                 float aspx, aspy;
862                 Image *ima = tex->ima;
863                 float orig_area, stencil_area, factor;
864                 ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
865
866                 if (use_scale) {
867                         aspx *= mtex->size[0];
868                         aspy *= mtex->size[1];
869                 }
870
871                 if (use_repeat && tex->extend == TEX_REPEAT) {
872                         aspx *= tex->xrepeat;
873                         aspy *= tex->yrepeat;
874                 }
875
876                 orig_area = aspx * aspy;
877
878                 if (do_mask) {
879                         stencil_area = br->mask_stencil_dimension[0] * br->mask_stencil_dimension[1];
880                 }
881                 else {
882                         stencil_area = br->stencil_dimension[0] * br->stencil_dimension[1];
883                 }
884
885                 factor = sqrtf(stencil_area / orig_area);
886
887                 if (do_mask) {
888                         br->mask_stencil_dimension[0] = factor * aspx;
889                         br->mask_stencil_dimension[1] = factor * aspy;
890
891                 }
892                 else {
893                         br->stencil_dimension[0] = factor * aspx;
894                         br->stencil_dimension[1] = factor * aspy;
895                 }
896         }
897
898         WM_event_add_notifier(C, NC_WINDOW, NULL);
899
900         return OPERATOR_FINISHED;
901 }
902
903
904 static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
905 {
906         /* identifiers */
907         ot->name = "Image Aspect";
908         ot->description = "When using an image texture, adjust the stencil size to fit the image aspect ratio";
909         ot->idname = "BRUSH_OT_stencil_fit_image_aspect";
910
911         /* api callbacks */
912         ot->exec = stencil_fit_image_aspect_exec;
913         ot->poll = stencil_control_poll;
914
915         /* flags */
916         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
917
918         RNA_def_boolean(ot->srna, "use_repeat", 1, "Use Repeat", "Use repeat mapping values");
919         RNA_def_boolean(ot->srna, "use_scale", 1, "Use Scale", "Use texture scale values");
920         RNA_def_boolean(ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
921 }
922
923
924 static int stencil_reset_transform_exec(bContext *C, wmOperator *op)
925 {
926         Paint *paint = BKE_paint_get_active_from_context(C);
927         Brush *br = BKE_paint_brush(paint);
928         bool do_mask = RNA_boolean_get(op->ptr, "mask");
929
930         if (!br)
931                 return OPERATOR_CANCELLED;
932
933         if (do_mask) {
934                 br->mask_stencil_pos[0] = 256;
935                 br->mask_stencil_pos[1] = 256;
936
937                 br->mask_stencil_dimension[0] = 256;
938                 br->mask_stencil_dimension[1] = 256;
939
940                 br->mask_mtex.rot = 0;
941         }
942         else {
943                 br->stencil_pos[0] = 256;
944                 br->stencil_pos[1] = 256;
945
946                 br->stencil_dimension[0] = 256;
947                 br->stencil_dimension[1] = 256;
948
949                 br->mtex.rot = 0;
950         }
951
952         WM_event_add_notifier(C, NC_WINDOW, NULL);
953
954         return OPERATOR_FINISHED;
955 }
956
957
958 static void BRUSH_OT_stencil_reset_transform(wmOperatorType *ot)
959 {
960         /* identifiers */
961         ot->name = "Reset Transform";
962         ot->description = "Reset the stencil transformation to the default";
963         ot->idname = "BRUSH_OT_stencil_reset_transform";
964
965         /* api callbacks */
966         ot->exec = stencil_reset_transform_exec;
967         ot->poll = stencil_control_poll;
968
969         /* flags */
970         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
971
972         RNA_def_boolean(ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
973 }
974
975
976 static void ed_keymap_stencil(wmKeyMap *keymap)
977 {
978         wmKeyMapItem *kmi;
979
980         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, 0, 0);
981         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
982         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT, 0);
983         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
984         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL, 0);
985         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
986
987         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_ALT, 0);
988         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
989         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
990         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT | KM_ALT, 0);
991         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
992         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
993         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL | KM_ALT, 0);
994         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
995         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
996 }
997
998 /**************************** registration **********************************/
999
1000 void ED_operatormacros_paint(void)
1001 {
1002         wmOperatorType *ot;
1003         wmOperatorTypeMacro *otmacro;
1004
1005         ot = WM_operatortype_append_macro(
1006                 "PAINTCURVE_OT_add_point_slide", "Add Curve Point and Slide",
1007                 "Add new curve point and slide it", OPTYPE_UNDO);
1008         ot->description = "Add new curve point and slide it";
1009         WM_operatortype_macro_define(ot, "PAINTCURVE_OT_add_point");
1010         otmacro = WM_operatortype_macro_define(ot, "PAINTCURVE_OT_slide");
1011         RNA_boolean_set(otmacro->ptr, "align", true);
1012         RNA_boolean_set(otmacro->ptr, "select", false);
1013 }
1014
1015
1016 void ED_operatortypes_paint(void)
1017 {
1018         /* palette */
1019         WM_operatortype_append(PALETTE_OT_new);
1020         WM_operatortype_append(PALETTE_OT_color_add);
1021         WM_operatortype_append(PALETTE_OT_color_delete);
1022
1023         /* paint curve */
1024         WM_operatortype_append(PAINTCURVE_OT_new);
1025         WM_operatortype_append(PAINTCURVE_OT_add_point);
1026         WM_operatortype_append(PAINTCURVE_OT_delete_point);
1027         WM_operatortype_append(PAINTCURVE_OT_select);
1028         WM_operatortype_append(PAINTCURVE_OT_slide);
1029         WM_operatortype_append(PAINTCURVE_OT_draw);
1030         WM_operatortype_append(PAINTCURVE_OT_cursor);
1031
1032         /* brush */
1033         WM_operatortype_append(BRUSH_OT_add);
1034         WM_operatortype_append(BRUSH_OT_scale_size);
1035         WM_operatortype_append(BRUSH_OT_curve_preset);
1036         WM_operatortype_append(BRUSH_OT_reset);
1037         WM_operatortype_append(BRUSH_OT_stencil_control);
1038         WM_operatortype_append(BRUSH_OT_stencil_fit_image_aspect);
1039         WM_operatortype_append(BRUSH_OT_stencil_reset_transform);
1040
1041         /* note, particle uses a different system, can be added with existing operators in wm.py */
1042         WM_operatortype_append(PAINT_OT_brush_select);
1043         WM_operatortype_append(BRUSH_OT_uv_sculpt_tool_set);
1044
1045         /* image */
1046         WM_operatortype_append(PAINT_OT_texture_paint_toggle);
1047         WM_operatortype_append(PAINT_OT_image_paint);
1048         WM_operatortype_append(PAINT_OT_sample_color);
1049         WM_operatortype_append(PAINT_OT_grab_clone);
1050         WM_operatortype_append(PAINT_OT_project_image);
1051         WM_operatortype_append(PAINT_OT_image_from_view);
1052         WM_operatortype_append(PAINT_OT_brush_colors_flip);
1053         WM_operatortype_append(PAINT_OT_add_texture_paint_slot);
1054         WM_operatortype_append(PAINT_OT_add_simple_uvs);
1055
1056         /* weight */
1057         WM_operatortype_append(PAINT_OT_weight_paint_toggle);
1058         WM_operatortype_append(PAINT_OT_weight_paint);
1059         WM_operatortype_append(PAINT_OT_weight_set);
1060         WM_operatortype_append(PAINT_OT_weight_from_bones);
1061         WM_operatortype_append(PAINT_OT_weight_gradient);
1062         WM_operatortype_append(PAINT_OT_weight_sample);
1063         WM_operatortype_append(PAINT_OT_weight_sample_group);
1064
1065         /* uv */
1066         WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
1067
1068         /* vertex selection */
1069         WM_operatortype_append(PAINT_OT_vert_select_all);
1070         WM_operatortype_append(PAINT_OT_vert_select_ungrouped);
1071
1072         /* vertex */
1073         WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
1074         WM_operatortype_append(PAINT_OT_vertex_paint);
1075         WM_operatortype_append(PAINT_OT_vertex_color_set);
1076         WM_operatortype_append(PAINT_OT_vertex_color_smooth);
1077
1078         WM_operatortype_append(PAINT_OT_vertex_color_brightness_contrast);
1079         WM_operatortype_append(PAINT_OT_vertex_color_hsv);
1080         WM_operatortype_append(PAINT_OT_vertex_color_invert);
1081         WM_operatortype_append(PAINT_OT_vertex_color_levels);
1082         WM_operatortype_append(PAINT_OT_vertex_color_from_weight);
1083
1084         /* face-select */
1085         WM_operatortype_append(PAINT_OT_face_select_linked);
1086         WM_operatortype_append(PAINT_OT_face_select_linked_pick);
1087         WM_operatortype_append(PAINT_OT_face_select_all);
1088         WM_operatortype_append(PAINT_OT_face_select_hide);
1089         WM_operatortype_append(PAINT_OT_face_select_reveal);
1090
1091         /* partial visibility */
1092         WM_operatortype_append(PAINT_OT_hide_show);
1093
1094         /* paint masking */
1095         WM_operatortype_append(PAINT_OT_mask_flood_fill);
1096         WM_operatortype_append(PAINT_OT_mask_lasso_gesture);
1097 }
1098
1099 static void ed_keymap_paint_brush_size(wmKeyMap *keymap, const char *UNUSED(path))
1100 {
1101         wmKeyMapItem *kmi;
1102
1103         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", LEFTBRACKETKEY, KM_PRESS, 0, 0);
1104         RNA_float_set(kmi->ptr, "scalar", 0.9);
1105
1106         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
1107         RNA_float_set(kmi->ptr, "scalar", 10.0 / 9.0); // 1.1111....
1108 }
1109
1110 static void set_brush_rc_path(
1111         PointerRNA *ptr, const char *brush_path,
1112         const char *output_name, const char *input_name)
1113 {
1114         char *path;
1115
1116         path = BLI_sprintfN("%s.%s", brush_path, input_name);
1117         RNA_string_set(ptr, output_name, path);
1118         MEM_freeN(path);
1119 }
1120
1121 void set_brush_rc_props(
1122         PointerRNA *ptr, const char *paint,
1123         const char *prop, const char *secondary_prop,
1124         RCFlags flags)
1125 {
1126         const char *ups_path = "tool_settings.unified_paint_settings";
1127         char *brush_path;
1128
1129         brush_path = BLI_sprintfN("tool_settings.%s.brush", paint);
1130
1131         set_brush_rc_path(ptr, brush_path, "data_path_primary", prop);
1132         if (secondary_prop) {
1133                 set_brush_rc_path(ptr, ups_path, "use_secondary", secondary_prop);
1134                 set_brush_rc_path(ptr, ups_path, "data_path_secondary", prop);
1135         }
1136         else {
1137                 RNA_string_set(ptr, "use_secondary", "");
1138                 RNA_string_set(ptr, "data_path_secondary", "");
1139         }
1140         set_brush_rc_path(ptr, brush_path, "color_path", "cursor_color_add");
1141         if (flags & RC_SECONDARY_ROTATION)
1142                 set_brush_rc_path(ptr, brush_path, "rotation_path", "mask_texture_slot.angle");
1143         else
1144                 set_brush_rc_path(ptr, brush_path, "rotation_path", "texture_slot.angle");
1145         RNA_string_set(ptr, "image_id", brush_path);
1146
1147         if (flags & RC_COLOR) {
1148                 set_brush_rc_path(ptr, brush_path, "fill_color_path", "color");
1149         }
1150         else {
1151                 RNA_string_set(ptr, "fill_color_path", "");
1152         }
1153
1154         if (flags & RC_COLOR_OVERRIDE) {
1155                 RNA_string_set(ptr, "fill_color_override_path", "tool_settings.unified_paint_settings.color");
1156                 RNA_string_set(ptr, "fill_color_override_test_path", "tool_settings.unified_paint_settings.use_unified_color");
1157         }
1158         else {
1159                 RNA_string_set(ptr, "fill_color_override_path", "");
1160                 RNA_string_set(ptr, "fill_color_override_test_path", "");
1161         }
1162
1163         if (flags & RC_ZOOM)
1164                 RNA_string_set(ptr, "zoom_path", "space_data.zoom");
1165         else
1166                 RNA_string_set(ptr, "zoom_path", "");
1167
1168         RNA_boolean_set(ptr, "secondary_tex", (flags & RC_SECONDARY_ROTATION) != 0);
1169
1170         MEM_freeN(brush_path);
1171 }
1172
1173 static void ed_keymap_paint_brush_radial_control(
1174         wmKeyMap *keymap, const char *paint,
1175         RCFlags flags)
1176 {
1177         wmKeyMapItem *kmi;
1178         /* only size needs to follow zoom, strength shows fixed size circle */
1179         int flags_nozoom = flags & (~RC_ZOOM);
1180         int flags_noradial_secondary = flags & (~(RC_SECONDARY_ROTATION | RC_ZOOM));
1181
1182         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, 0, 0);
1183         set_brush_rc_props(kmi->ptr, paint, "size", "use_unified_size", flags);
1184
1185         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0);
1186         set_brush_rc_props(kmi->ptr, paint, "strength", "use_unified_strength", flags_nozoom);
1187
1188         if (flags & RC_WEIGHT) {
1189                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", WKEY, KM_PRESS, 0, 0);
1190                 set_brush_rc_props(kmi->ptr, paint, "weight", "use_unified_weight", flags_nozoom);
1191         }
1192
1193         if (flags & RC_ROTATION) {
1194                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0);
1195                 set_brush_rc_props(kmi->ptr, paint, "texture_slot.angle", NULL, flags_noradial_secondary);
1196         }
1197
1198         if (flags & RC_SECONDARY_ROTATION) {
1199                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL | KM_ALT, 0);
1200                 set_brush_rc_props(kmi->ptr, paint, "mask_texture_slot.angle", NULL, flags_nozoom);
1201         }
1202 }
1203
1204 static void paint_partial_visibility_keys(wmKeyMap *keymap)
1205 {
1206         wmKeyMapItem *kmi;
1207
1208         /* Partial visibility */
1209         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_SHIFT, 0);
1210         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
1211         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
1212         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, 0, 0);
1213         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_HIDE);
1214         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
1215         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_ALT, 0);
1216         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
1217         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_ALL);
1218 }
1219
1220
1221 static void paint_keymap_curve(wmKeyMap *keymap)
1222 {
1223         wmKeyMapItem *kmi;
1224
1225         WM_keymap_add_item(keymap, "PAINTCURVE_OT_add_point_slide", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
1226         WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
1227         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
1228         RNA_boolean_set(kmi->ptr, "extend", true);
1229         WM_keymap_add_item(keymap, "PAINTCURVE_OT_slide", ACTIONMOUSE, KM_PRESS, 0, 0);
1230         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_slide", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
1231         RNA_boolean_set(kmi->ptr, "align", true);
1232         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", AKEY, KM_PRESS, 0, 0);
1233         RNA_boolean_set(kmi->ptr, "toggle", true);
1234
1235         WM_keymap_add_item(keymap, "PAINTCURVE_OT_cursor", ACTIONMOUSE, KM_PRESS, 0, 0);
1236
1237         WM_keymap_add_item(keymap, "PAINTCURVE_OT_delete_point", XKEY, KM_PRESS, 0, 0);
1238         WM_keymap_add_item(keymap, "PAINTCURVE_OT_delete_point", DELKEY, KM_PRESS, 0, 0);
1239
1240         WM_keymap_add_item(keymap, "PAINTCURVE_OT_draw", RETKEY, KM_PRESS, 0, 0);
1241         WM_keymap_add_item(keymap, "PAINTCURVE_OT_draw", PADENTER, KM_PRESS, 0, 0);
1242
1243         WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", GKEY, KM_PRESS, 0, 0);
1244         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", EVT_TWEAK_S, KM_ANY, 0, 0);
1245         WM_keymap_add_item(keymap, "TRANSFORM_OT_rotate", RKEY, KM_PRESS, 0, 0);
1246         WM_keymap_add_item(keymap, "TRANSFORM_OT_resize", SKEY, KM_PRESS, 0, 0);
1247 }
1248
1249 void ED_keymap_paint(wmKeyConfig *keyconf)
1250 {
1251         wmKeyMap *keymap;
1252         wmKeyMapItem *kmi;
1253         int i;
1254
1255         keymap = WM_keymap_find(keyconf, "Paint Curve", 0, 0);
1256         keymap->poll = paint_curve_poll;
1257
1258         paint_keymap_curve(keymap);
1259
1260         /* Sculpt mode */
1261         keymap = WM_keymap_find(keyconf, "Sculpt", 0, 0);
1262         keymap->poll = sculpt_mode_poll;
1263
1264         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1265         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1266         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1267
1268         /* Partial visibility, sculpt-only for now */
1269         paint_partial_visibility_keys(keymap);
1270
1271         for (i = 0; i <= 5; i++)
1272                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY + i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
1273
1274         /* Clear mask */
1275         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", MKEY, KM_PRESS, KM_ALT, 0);
1276         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_FLOOD_VALUE);
1277         RNA_float_set(kmi->ptr, "value", 0);
1278
1279         /* Invert mask */
1280         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", IKEY, KM_PRESS, KM_CTRL, 0);
1281         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_INVERT);
1282
1283         WM_keymap_add_item(keymap, "PAINT_OT_mask_lasso_gesture", LEFTMOUSE, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
1284
1285         /* Toggle mask visibility */
1286         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, KM_CTRL, 0);
1287         RNA_string_set(kmi->ptr, "data_path", "scene.tool_settings.sculpt.show_mask");
1288
1289         /* Toggle dynamic topology */
1290         WM_keymap_add_item(keymap, "SCULPT_OT_dynamic_topology_toggle", DKEY, KM_PRESS, KM_CTRL, 0);
1291
1292         /* Dynamic-topology detail size
1293          *
1294          * This should be improved further, perhaps by showing a triangle
1295          * grid rather than brush alpha */
1296         kmi = WM_keymap_add_item(keymap, "SCULPT_OT_set_detail_size", DKEY, KM_PRESS, KM_SHIFT, 0);
1297
1298         /* multires switch */
1299         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
1300         RNA_int_set(kmi->ptr, "level", 1);
1301         RNA_boolean_set(kmi->ptr, "relative", true);
1302
1303         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
1304         RNA_int_set(kmi->ptr, "level", -1);
1305         RNA_boolean_set(kmi->ptr, "relative", true);
1306
1307         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
1308         ed_keymap_paint_brush_radial_control(keymap, "sculpt", RC_ROTATION);
1309
1310         ed_keymap_stencil(keymap);
1311
1312         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_DRAW, XKEY, 0);
1313         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SMOOTH, SKEY, 0);
1314         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_PINCH, PKEY, 0);
1315         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_INFLATE, IKEY, 0);
1316         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_GRAB, GKEY, 0);
1317         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_LAYER, LKEY, 0);
1318         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_FLATTEN, TKEY, KM_SHIFT);
1319         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CLAY, CKEY, 0);
1320         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CREASE, CKEY, KM_SHIFT);
1321         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SNAKE_HOOK, KKEY, 0);
1322         kmi = keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_MASK, MKEY, 0);
1323         RNA_boolean_set(kmi->ptr, "toggle", 1);
1324         RNA_boolean_set(kmi->ptr, "create_missing", 1);
1325
1326         /* */
1327         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1328         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.stroke_method");
1329
1330         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1331         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
1332
1333         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1334
1335         /* Vertex Paint mode */
1336         keymap = WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
1337         keymap->poll = vertex_paint_mode_poll;
1338
1339         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
1340         WM_keymap_add_item(keymap, "PAINT_OT_brush_colors_flip", XKEY, KM_PRESS, 0, 0);
1341         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1342
1343         WM_keymap_add_item(keymap, "PAINT_OT_vertex_color_set", KKEY, KM_PRESS, KM_SHIFT, 0);
1344
1345         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
1346         ed_keymap_paint_brush_radial_control(keymap, "vertex_paint", RC_COLOR | RC_COLOR_OVERRIDE | RC_ROTATION);
1347
1348         ed_keymap_stencil(keymap);
1349
1350         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1351         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
1352
1353         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1354         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.use_smooth_stroke");
1355
1356         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1357
1358         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1359         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
1360
1361         /* Weight Paint mode */
1362         keymap = WM_keymap_find(keyconf, "Weight Paint", 0, 0);
1363         keymap->poll = weight_paint_mode_poll;
1364
1365         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
1366
1367         /* these keys are from 2.4x but could be changed */
1368         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
1369         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample_group", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
1370
1371         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_gradient", LEFTMOUSE, KM_PRESS, KM_ALT, 0)->ptr,           "type", WPAINT_GRADIENT_TYPE_LINEAR);
1372         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_gradient", LEFTMOUSE, KM_PRESS, KM_ALT | KM_CTRL, 0)->ptr, "type", WPAINT_GRADIENT_TYPE_RADIAL);
1373
1374         WM_keymap_add_item(keymap, "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
1375
1376         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
1377         ed_keymap_paint_brush_radial_control(keymap, "weight_paint", RC_WEIGHT);
1378
1379         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1380         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
1381
1382         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* face mask toggle */
1383         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
1384
1385         /* note, conflicts with vertex paint, but this is more useful */
1386         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", VKEY, KM_PRESS, 0, 0); /* vert mask toggle */
1387         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask_vertex");
1388
1389         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1390         RNA_string_set(kmi->ptr, "data_path", "tool_settings.weight_paint.brush.use_smooth_stroke");
1391
1392         /*Weight paint's Vertex Selection Mode */
1393         keymap = WM_keymap_find(keyconf, "Weight Paint Vertex Selection", 0, 0);
1394         keymap->poll = vert_paint_poll;
1395         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", AKEY, KM_PRESS, 0, 0);
1396         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
1397         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1398         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1399         WM_keymap_add_item(keymap, "VIEW3D_OT_select_border", BKEY, KM_PRESS, 0, 0);
1400         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
1401         RNA_boolean_set(kmi->ptr, "deselect", false);
1402         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0);
1403         RNA_boolean_set(kmi->ptr, "deselect", true);
1404         WM_keymap_add_item(keymap, "VIEW3D_OT_select_circle", CKEY, KM_PRESS, 0, 0);
1405
1406         /* Image/Texture Paint mode */
1407         keymap = WM_keymap_find(keyconf, "Image Paint", 0, 0);
1408         keymap->poll = image_texture_paint_poll;
1409
1410         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1411         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1412         WM_keymap_add_item(keymap, "PAINT_OT_brush_colors_flip", XKEY, KM_PRESS, 0, 0);
1413         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
1414         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1415
1416         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
1417         ed_keymap_paint_brush_radial_control(
1418                 keymap, "image_paint",
1419                 RC_COLOR | RC_COLOR_OVERRIDE | RC_ZOOM | RC_ROTATION | RC_SECONDARY_ROTATION);
1420
1421         ed_keymap_stencil(keymap);
1422
1423         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1424         RNA_string_set(kmi->ptr, "data_path", "image_paint_object.data.use_paint_mask");
1425
1426         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1427         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.use_smooth_stroke");
1428
1429         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1430
1431         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1432         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.stroke_method");
1433
1434         /* face-mask mode */
1435         keymap = WM_keymap_find(keyconf, "Face Mask", 0, 0);
1436         keymap->poll = facemask_paint_poll;
1437
1438         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
1439         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
1440         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1441         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1442         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, 0, 0);
1443         RNA_boolean_set(kmi->ptr, "unselected", false);
1444         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
1445         RNA_boolean_set(kmi->ptr, "unselected", true);
1446         WM_keymap_add_item(keymap, "PAINT_OT_face_select_reveal", HKEY, KM_PRESS, KM_ALT, 0);
1447
1448         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
1449         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
1450         RNA_boolean_set(kmi->ptr, "deselect", false);
1451         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
1452         RNA_boolean_set(kmi->ptr, "deselect", true);
1453
1454         keymap = WM_keymap_find(keyconf, "UV Sculpt", 0, 0);
1455         keymap->poll = uv_sculpt_keymap_poll;
1456
1457         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", QKEY, KM_PRESS, 0, 0);
1458         RNA_string_set(kmi->ptr, "data_path", "tool_settings.use_uv_sculpt");
1459
1460         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1461         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1462         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1463
1464         ed_keymap_paint_brush_size(keymap, "tool_settings.uv_sculpt.brush.size");
1465         ed_keymap_paint_brush_radial_control(keymap, "uv_sculpt", 0);
1466
1467         RNA_enum_set(WM_keymap_add_item(keymap, "BRUSH_OT_uv_sculpt_tool_set", SKEY, KM_PRESS, 0, 0)->ptr, "tool", UV_SCULPT_TOOL_RELAX);
1468         RNA_enum_set(WM_keymap_add_item(keymap, "BRUSH_OT_uv_sculpt_tool_set", PKEY, KM_PRESS, 0, 0)->ptr, "tool", UV_SCULPT_TOOL_PINCH);
1469         RNA_enum_set(WM_keymap_add_item(keymap, "BRUSH_OT_uv_sculpt_tool_set", GKEY, KM_PRESS, 0, 0)->ptr, "tool", UV_SCULPT_TOOL_GRAB);
1470
1471         /* paint stroke */
1472         keymap = paint_stroke_modal_keymap(keyconf);
1473         WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
1474 }