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 int 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(Main *bmain, Paint *paint, const int tool,
370                                   const size_t tool_offset, const int ob_mode,
371                                   const char *tool_name, const bool create_missing,
372                                   const bool toggle)
373 {
374         Brush *brush, *brush_orig = BKE_paint_brush(paint);
375
376         if (toggle)
377                 brush = brush_tool_toggle(bmain, brush_orig, tool, tool_offset, ob_mode);
378         else
379                 brush = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
380
381         if (!brush && brush_tool(brush_orig, tool_offset) != tool && create_missing) {
382                 brush = BKE_brush_add(bmain, tool_name, ob_mode);
383                 id_us_min(&brush->id);  /* fake user only */
384                 brush_tool_set(brush, tool_offset, tool);
385                 brush->toggle_brush = brush_orig;
386         }
387
388         if (brush) {
389                 BKE_paint_brush_set(paint, brush);
390                 BKE_paint_invalidate_overlay_all();
391
392                 WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
393                 return OPERATOR_FINISHED;
394         }
395         else {
396                 return OPERATOR_CANCELLED;
397         }
398 }
399
400 /* used in the PAINT_OT_brush_select operator */
401 #define OB_MODE_ACTIVE 0
402
403 static int brush_select_exec(bContext *C, wmOperator *op)
404 {
405         Main *bmain = CTX_data_main(C);
406         ToolSettings *toolsettings = CTX_data_tool_settings(C);
407         Paint *paint = NULL;
408         int tool, paint_mode = RNA_enum_get(op->ptr, "paint_mode");
409         const bool create_missing = RNA_boolean_get(op->ptr, "create_missing");
410         const bool toggle = RNA_boolean_get(op->ptr, "toggle");
411         const char *tool_name = "Brush";
412         size_t tool_offset;
413
414         if (paint_mode == OB_MODE_ACTIVE) {
415                 Object *ob = CTX_data_active_object(C);
416                 if (ob) {
417                         /* select current paint mode */
418                         paint_mode = ob->mode & OB_MODE_ALL_PAINT;
419                 }
420                 else {
421                         return OPERATOR_CANCELLED;
422                 }
423         }
424
425         switch (paint_mode) {
426                 case OB_MODE_SCULPT:
427                         paint = &toolsettings->sculpt->paint;
428                         tool_offset = offsetof(Brush, sculpt_tool);
429                         tool = RNA_enum_get(op->ptr, "sculpt_tool");
430                         RNA_enum_name_from_value(rna_enum_brush_sculpt_tool_items, tool, &tool_name);
431                         break;
432                 case OB_MODE_VERTEX_PAINT:
433                         paint = &toolsettings->vpaint->paint;
434                         tool_offset = offsetof(Brush, vertexpaint_tool);
435                         tool = RNA_enum_get(op->ptr, "vertex_paint_tool");
436                         RNA_enum_name_from_value(rna_enum_brush_vertex_tool_items, tool, &tool_name);
437                         break;
438                 case OB_MODE_WEIGHT_PAINT:
439                         paint = &toolsettings->wpaint->paint;
440                         /* vertexpaint_tool is used for weight paint mode */
441                         tool_offset = offsetof(Brush, vertexpaint_tool);
442                         tool = RNA_enum_get(op->ptr, "weight_paint_tool");
443                         RNA_enum_name_from_value(rna_enum_brush_vertex_tool_items, tool, &tool_name);
444                         break;
445                 case OB_MODE_TEXTURE_PAINT:
446                         paint = &toolsettings->imapaint.paint;
447                         tool_offset = offsetof(Brush, imagepaint_tool);
448                         tool = RNA_enum_get(op->ptr, "texture_paint_tool");
449                         RNA_enum_name_from_value(rna_enum_brush_image_tool_items, tool, &tool_name);
450                         break;
451                 default:
452                         /* invalid paint mode */
453                         return OPERATOR_CANCELLED;
454         }
455
456         /* TODO(campbell): Use the toolsystem for now, ideally the toolsystem will display brushes directly
457          * so we don't need to sync between tools and brushes. */
458         if (false) {
459                 return brush_generic_tool_set(
460                             bmain, paint, tool, tool_offset,
461                             paint_mode, tool_name, create_missing,
462                             toggle);
463         }
464         else {
465                 WorkSpace *workspace = CTX_wm_workspace(C);
466                 if (WM_toolsystem_ref_set_by_name(C, workspace, NULL, tool_name, true)) {
467                         return OPERATOR_FINISHED;
468                 }
469                 else {
470                         return OPERATOR_CANCELLED;
471                 }
472         }
473 }
474
475 static void PAINT_OT_brush_select(wmOperatorType *ot)
476 {
477         static const EnumPropertyItem paint_mode_items[] = {
478                 {OB_MODE_ACTIVE, "ACTIVE", 0, "Current", "Set brush for active paint mode"},
479                 {OB_MODE_SCULPT, "SCULPT", ICON_SCULPTMODE_HLT, "Sculpt", ""},
480                 {OB_MODE_VERTEX_PAINT, "VERTEX_PAINT", ICON_VPAINT_HLT, "Vertex Paint", ""},
481                 {OB_MODE_WEIGHT_PAINT, "WEIGHT_PAINT", ICON_WPAINT_HLT, "Weight Paint", ""},
482                 {OB_MODE_TEXTURE_PAINT, "TEXTURE_PAINT", ICON_TPAINT_HLT, "Texture Paint", ""},
483                 {0, NULL, 0, NULL, NULL}
484         };
485         PropertyRNA *prop;
486
487         /* identifiers */
488         ot->name = "Brush Select";
489         ot->description = "Select a paint mode's brush by tool type";
490         ot->idname = "PAINT_OT_brush_select";
491
492         /* api callbacks */
493         ot->exec = brush_select_exec;
494
495         /* flags */
496         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
497
498         /* props */
499         RNA_def_enum(ot->srna, "paint_mode", paint_mode_items, OB_MODE_ACTIVE, "Paint Mode", "");
500         RNA_def_enum(ot->srna, "sculpt_tool", rna_enum_brush_sculpt_tool_items, 0, "Sculpt Tool", "");
501         RNA_def_enum(ot->srna, "vertex_paint_tool", rna_enum_brush_vertex_tool_items, 0, "Vertex Paint Tool", "");
502         RNA_def_enum(ot->srna, "weight_paint_tool", rna_enum_brush_vertex_tool_items, 0, "Weight Paint Tool", "");
503         RNA_def_enum(ot->srna, "texture_paint_tool", rna_enum_brush_image_tool_items, 0, "Texture Paint Tool", "");
504
505         prop = RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "Toggle between two brushes rather than cycling");
506         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
507         prop = RNA_def_boolean(ot->srna, "create_missing", 0, "Create Missing", "If the requested brush type does not exist, create a new brush");
508         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
509 }
510
511 static wmKeyMapItem *keymap_brush_select(
512         wmKeyMap *keymap, int paint_mode,
513         int tool, int keymap_type,
514         int keymap_modifier)
515 {
516         wmKeyMapItem *kmi;
517         kmi = WM_keymap_add_item(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,
703                                     mdiff);
704                         CLAMP(scd->pos_target[0],
705                               -scd->dim_target[0] + PIXEL_MARGIN,
706                               scd->area_size[0] + scd->dim_target[0] - PIXEL_MARGIN);
707
708                         CLAMP(scd->pos_target[1],
709                               -scd->dim_target[1] + PIXEL_MARGIN,
710                               scd->area_size[1] + scd->dim_target[1] - PIXEL_MARGIN);
711
712                         break;
713                 case STENCIL_SCALE:
714                 {
715                         float len, factor;
716                         sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
717                         len = len_v2(mdiff);
718                         factor = len / scd->lenorig;
719                         copy_v2_v2(mdiff, scd->init_sdim);
720                         if (scd->constrain_mode != STENCIL_CONSTRAINT_Y)
721                                 mdiff[0] = factor * scd->init_sdim[0];
722                         if (scd->constrain_mode != STENCIL_CONSTRAINT_X)
723                                 mdiff[1] = factor * scd->init_sdim[1];
724                         CLAMP(mdiff[0], 5.0f, 10000.0f);
725                         CLAMP(mdiff[1], 5.0f, 10000.0f);
726                         copy_v2_v2(scd->dim_target, mdiff);
727                         break;
728                 }
729                 case STENCIL_ROTATE:
730                 {
731                         float angle;
732                         sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
733                         angle = atan2f(mdiff[1], mdiff[0]);
734                         angle = scd->init_rot + angle - scd->init_angle;
735                         if (angle < 0.0f)
736                                 angle += (float)(2 * M_PI);
737                         if (angle > (float)(2 * M_PI))
738                                 angle -= (float)(2 * M_PI);
739                         *scd->rot_target = angle;
740                         break;
741                 }
742         }
743 #undef PIXEL_MARGIN
744 }
745
746 static int stencil_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
747 {
748         StencilControlData *scd = op->customdata;
749
750         if (event->type == scd->event_type && event->val == KM_RELEASE) {
751                 MEM_freeN(op->customdata);
752                 WM_event_add_notifier(C, NC_WINDOW, NULL);
753                 return OPERATOR_FINISHED;
754         }
755
756         switch (event->type) {
757                 case MOUSEMOVE:
758                         stencil_control_calculate(scd, event->mval);
759                         break;
760                 case ESCKEY:
761                         if (event->val == KM_PRESS) {
762                                 stencil_control_cancel(C, op);
763                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
764                                 return OPERATOR_CANCELLED;
765                         }
766                         break;
767                 case XKEY:
768                         if (event->val == KM_PRESS) {
769
770                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_X)
771                                         scd->constrain_mode = 0;
772                                 else
773                                         scd->constrain_mode = STENCIL_CONSTRAINT_X;
774
775                                 stencil_control_calculate(scd, event->mval);
776                         }
777                         break;
778                 case YKEY:
779                         if (event->val == KM_PRESS) {
780                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_Y)
781                                         scd->constrain_mode = 0;
782                                 else
783                                         scd->constrain_mode = STENCIL_CONSTRAINT_Y;
784
785                                 stencil_control_calculate(scd, event->mval);
786                         }
787                         break;
788                 default:
789                         break;
790         }
791
792         ED_region_tag_redraw(CTX_wm_region(C));
793
794         return OPERATOR_RUNNING_MODAL;
795 }
796
797 static int stencil_control_poll(bContext *C)
798 {
799         ePaintMode mode = BKE_paintmode_get_active_from_context(C);
800
801         Paint *paint;
802         Brush *br;
803
804         if (!paint_supports_texture(mode))
805                 return false;
806
807         paint = BKE_paint_get_active_from_context(C);
808         br = BKE_paint_brush(paint);
809         return (br &&
810                 (br->mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL ||
811                  br->mask_mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL));
812 }
813
814 static void BRUSH_OT_stencil_control(wmOperatorType *ot)
815 {
816         static const EnumPropertyItem stencil_control_items[] = {
817                 {STENCIL_TRANSLATE, "TRANSLATION", 0, "Translation", ""},
818                 {STENCIL_SCALE, "SCALE", 0, "Scale", ""},
819                 {STENCIL_ROTATE, "ROTATION", 0, "Rotation", ""},
820                 {0, NULL, 0, NULL, NULL}
821         };
822
823         static const EnumPropertyItem stencil_texture_items[] = {
824                 {STENCIL_PRIMARY, "PRIMARY", 0, "Primary", ""},
825                 {STENCIL_SECONDARY, "SECONDARY", 0, "Secondary", ""},
826                 {0, NULL, 0, NULL, NULL}
827         };
828         /* identifiers */
829         ot->name = "Stencil Brush Control";
830         ot->description = "Control the stencil brush";
831         ot->idname = "BRUSH_OT_stencil_control";
832
833         /* api callbacks */
834         ot->invoke = stencil_control_invoke;
835         ot->modal = stencil_control_modal;
836         ot->cancel = stencil_control_cancel;
837         ot->poll = stencil_control_poll;
838
839         /* flags */
840         ot->flag = 0;
841
842         RNA_def_enum(ot->srna, "mode", stencil_control_items, STENCIL_TRANSLATE, "Tool", "");
843         RNA_def_enum(ot->srna, "texmode", stencil_texture_items, STENCIL_PRIMARY, "Tool", "");
844 }
845
846
847 static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *op)
848 {
849         Paint *paint = BKE_paint_get_active_from_context(C);
850         Brush *br = BKE_paint_brush(paint);
851         bool use_scale = RNA_boolean_get(op->ptr, "use_scale");
852         bool use_repeat = RNA_boolean_get(op->ptr, "use_repeat");
853         bool do_mask = RNA_boolean_get(op->ptr, "mask");
854         Tex *tex = NULL;
855         MTex *mtex = NULL;
856         if (br) {
857                 mtex = do_mask ? &br->mask_mtex : &br->mtex;
858                 tex = mtex->tex;
859         }
860
861         if (tex && tex->type == TEX_IMAGE && tex->ima) {
862                 float aspx, aspy;
863                 Image *ima = tex->ima;
864                 float orig_area, stencil_area, factor;
865                 ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
866
867                 if (use_scale) {
868                         aspx *= mtex->size[0];
869                         aspy *= mtex->size[1];
870                 }
871
872                 if (use_repeat && tex->extend == TEX_REPEAT) {
873                         aspx *= tex->xrepeat;
874                         aspy *= tex->yrepeat;
875                 }
876
877                 orig_area = aspx * aspy;
878
879                 if (do_mask) {
880                         stencil_area = br->mask_stencil_dimension[0] * br->mask_stencil_dimension[1];
881                 }
882                 else {
883                         stencil_area = br->stencil_dimension[0] * br->stencil_dimension[1];
884                 }
885
886                 factor = sqrtf(stencil_area / orig_area);
887
888                 if (do_mask) {
889                         br->mask_stencil_dimension[0] = factor * aspx;
890                         br->mask_stencil_dimension[1] = factor * aspy;
891
892                 }
893                 else {
894                         br->stencil_dimension[0] = factor * aspx;
895                         br->stencil_dimension[1] = factor * aspy;
896                 }
897         }
898
899         WM_event_add_notifier(C, NC_WINDOW, NULL);
900
901         return OPERATOR_FINISHED;
902 }
903
904
905 static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
906 {
907         /* identifiers */
908         ot->name = "Image Aspect";
909         ot->description = "When using an image texture, adjust the stencil size to fit the image aspect ratio";
910         ot->idname = "BRUSH_OT_stencil_fit_image_aspect";
911
912         /* api callbacks */
913         ot->exec = stencil_fit_image_aspect_exec;
914         ot->poll = stencil_control_poll;
915
916         /* flags */
917         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
918
919         RNA_def_boolean(ot->srna, "use_repeat", 1, "Use Repeat", "Use repeat mapping values");
920         RNA_def_boolean(ot->srna, "use_scale", 1, "Use Scale", "Use texture scale values");
921         RNA_def_boolean(ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
922 }
923
924
925 static int stencil_reset_transform_exec(bContext *C, wmOperator *op)
926 {
927         Paint *paint = BKE_paint_get_active_from_context(C);
928         Brush *br = BKE_paint_brush(paint);
929         bool do_mask = RNA_boolean_get(op->ptr, "mask");
930
931         if (!br)
932                 return OPERATOR_CANCELLED;
933
934         if (do_mask) {
935                 br->mask_stencil_pos[0] = 256;
936                 br->mask_stencil_pos[1] = 256;
937
938                 br->mask_stencil_dimension[0] = 256;
939                 br->mask_stencil_dimension[1] = 256;
940
941                 br->mask_mtex.rot = 0;
942         }
943         else {
944                 br->stencil_pos[0] = 256;
945                 br->stencil_pos[1] = 256;
946
947                 br->stencil_dimension[0] = 256;
948                 br->stencil_dimension[1] = 256;
949
950                 br->mtex.rot = 0;
951         }
952
953         WM_event_add_notifier(C, NC_WINDOW, NULL);
954
955         return OPERATOR_FINISHED;
956 }
957
958
959 static void BRUSH_OT_stencil_reset_transform(wmOperatorType *ot)
960 {
961         /* identifiers */
962         ot->name = "Reset Transform";
963         ot->description = "Reset the stencil transformation to the default";
964         ot->idname = "BRUSH_OT_stencil_reset_transform";
965
966         /* api callbacks */
967         ot->exec = stencil_reset_transform_exec;
968         ot->poll = stencil_control_poll;
969
970         /* flags */
971         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
972
973         RNA_def_boolean(ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
974 }
975
976
977 static void ed_keymap_stencil(wmKeyMap *keymap)
978 {
979         wmKeyMapItem *kmi;
980
981         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, 0, 0);
982         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
983         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT, 0);
984         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
985         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL, 0);
986         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
987
988         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_ALT, 0);
989         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
990         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
991         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT | KM_ALT, 0);
992         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
993         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
994         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL | KM_ALT, 0);
995         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
996         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
997 }
998
999 /**************************** registration **********************************/
1000
1001 void ED_operatormacros_paint(void)
1002 {
1003         wmOperatorType *ot;
1004         wmOperatorTypeMacro *otmacro;
1005
1006         ot = WM_operatortype_append_macro("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(PointerRNA *ptr, const char *brush_path,
1111                               const char *output_name, const char *input_name)
1112 {
1113         char *path;
1114
1115         path = BLI_sprintfN("%s.%s", brush_path, input_name);
1116         RNA_string_set(ptr, output_name, path);
1117         MEM_freeN(path);
1118 }
1119
1120 void set_brush_rc_props(PointerRNA *ptr, const char *paint,
1121                         const char *prop, const char *secondary_prop,
1122                         RCFlags flags)
1123 {
1124         const char *ups_path = "tool_settings.unified_paint_settings";
1125         char *brush_path;
1126
1127         brush_path = BLI_sprintfN("tool_settings.%s.brush", paint);
1128
1129         set_brush_rc_path(ptr, brush_path, "data_path_primary", prop);
1130         if (secondary_prop) {
1131                 set_brush_rc_path(ptr, ups_path, "use_secondary", secondary_prop);
1132                 set_brush_rc_path(ptr, ups_path, "data_path_secondary", prop);
1133         }
1134         else {
1135                 RNA_string_set(ptr, "use_secondary", "");
1136                 RNA_string_set(ptr, "data_path_secondary", "");
1137         }
1138         set_brush_rc_path(ptr, brush_path, "color_path", "cursor_color_add");
1139         if (flags & RC_SECONDARY_ROTATION)
1140                 set_brush_rc_path(ptr, brush_path, "rotation_path", "mask_texture_slot.angle");
1141         else
1142                 set_brush_rc_path(ptr, brush_path, "rotation_path", "texture_slot.angle");
1143         RNA_string_set(ptr, "image_id", brush_path);
1144
1145         if (flags & RC_COLOR) {
1146                 set_brush_rc_path(ptr, brush_path, "fill_color_path", "color");
1147         }
1148         else {
1149                 RNA_string_set(ptr, "fill_color_path", "");
1150         }
1151
1152         if (flags & RC_COLOR_OVERRIDE) {
1153                 RNA_string_set(ptr, "fill_color_override_path", "tool_settings.unified_paint_settings.color");
1154                 RNA_string_set(ptr, "fill_color_override_test_path", "tool_settings.unified_paint_settings.use_unified_color");
1155         }
1156         else {
1157                 RNA_string_set(ptr, "fill_color_override_path", "");
1158                 RNA_string_set(ptr, "fill_color_override_test_path", "");
1159         }
1160
1161         if (flags & RC_ZOOM)
1162                 RNA_string_set(ptr, "zoom_path", "space_data.zoom");
1163         else
1164                 RNA_string_set(ptr, "zoom_path", "");
1165
1166         RNA_boolean_set(ptr, "secondary_tex", (flags & RC_SECONDARY_ROTATION) != 0);
1167
1168         MEM_freeN(brush_path);
1169 }
1170
1171 static void ed_keymap_paint_brush_radial_control(wmKeyMap *keymap, const char *paint,
1172                                                  RCFlags flags)
1173 {
1174         wmKeyMapItem *kmi;
1175         /* only size needs to follow zoom, strength shows fixed size circle */
1176         int flags_nozoom = flags & (~RC_ZOOM);
1177         int flags_noradial_secondary = flags & (~(RC_SECONDARY_ROTATION | RC_ZOOM));
1178
1179         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, 0, 0);
1180         set_brush_rc_props(kmi->ptr, paint, "size", "use_unified_size", flags);
1181
1182         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0);
1183         set_brush_rc_props(kmi->ptr, paint, "strength", "use_unified_strength", flags_nozoom);
1184
1185         if (flags & RC_WEIGHT) {
1186                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", WKEY, KM_PRESS, 0, 0);
1187                 set_brush_rc_props(kmi->ptr, paint, "weight", "use_unified_weight", flags_nozoom);
1188         }
1189
1190         if (flags & RC_ROTATION) {
1191                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0);
1192                 set_brush_rc_props(kmi->ptr, paint, "texture_slot.angle", NULL, flags_noradial_secondary);
1193         }
1194
1195         if (flags & RC_SECONDARY_ROTATION) {
1196                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL | KM_ALT, 0);
1197                 set_brush_rc_props(kmi->ptr, paint, "mask_texture_slot.angle", NULL, flags_nozoom);
1198         }
1199 }
1200
1201 static void paint_partial_visibility_keys(wmKeyMap *keymap)
1202 {
1203         wmKeyMapItem *kmi;
1204
1205         /* Partial visibility */
1206         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_SHIFT, 0);
1207         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
1208         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
1209         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, 0, 0);
1210         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_HIDE);
1211         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
1212         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_ALT, 0);
1213         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
1214         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_ALL);
1215 }
1216
1217
1218 static void paint_keymap_curve(wmKeyMap *keymap)
1219 {
1220         wmKeyMapItem *kmi;
1221
1222         WM_keymap_add_item(keymap, "PAINTCURVE_OT_add_point_slide", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
1223         WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
1224         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
1225         RNA_boolean_set(kmi->ptr, "extend", true);
1226         WM_keymap_add_item(keymap, "PAINTCURVE_OT_slide", ACTIONMOUSE, KM_PRESS, 0, 0);
1227         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_slide", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
1228         RNA_boolean_set(kmi->ptr, "align", true);
1229         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", AKEY, KM_PRESS, 0, 0);
1230         RNA_boolean_set(kmi->ptr, "toggle", true);
1231
1232         WM_keymap_add_item(keymap, "PAINTCURVE_OT_cursor", ACTIONMOUSE, KM_PRESS, 0, 0);
1233         WM_keymap_add_item(keymap, "PAINTCURVE_OT_delete_point", XKEY, KM_PRESS, 0, 0);
1234         WM_keymap_add_item(keymap, "PAINTCURVE_OT_delete_point", DELKEY, KM_PRESS, 0, 0);
1235
1236         WM_keymap_add_item(keymap, "PAINTCURVE_OT_draw", RETKEY, KM_PRESS, 0, 0);
1237         WM_keymap_add_item(keymap, "PAINTCURVE_OT_draw", PADENTER, KM_PRESS, 0, 0);
1238
1239         WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", GKEY, KM_PRESS, 0, 0);
1240         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", EVT_TWEAK_S, KM_ANY, 0, 0);
1241         WM_keymap_add_item(keymap, "TRANSFORM_OT_rotate", RKEY, KM_PRESS, 0, 0);
1242         WM_keymap_add_item(keymap, "TRANSFORM_OT_resize", SKEY, KM_PRESS, 0, 0);
1243 }
1244
1245 void ED_keymap_paint(wmKeyConfig *keyconf)
1246 {
1247         wmKeyMap *keymap;
1248         wmKeyMapItem *kmi;
1249         int i;
1250
1251         keymap = WM_keymap_find(keyconf, "Paint Curve", 0, 0);
1252         keymap->poll = paint_curve_poll;
1253
1254         paint_keymap_curve(keymap);
1255
1256         /* Sculpt mode */
1257         keymap = WM_keymap_find(keyconf, "Sculpt", 0, 0);
1258         keymap->poll = sculpt_mode_poll;
1259
1260         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1261         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1262         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1263
1264         /* Partial visibility, sculpt-only for now */
1265         paint_partial_visibility_keys(keymap);
1266
1267         for (i = 0; i <= 5; i++)
1268                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY + i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
1269
1270         /* Clear mask */
1271         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", MKEY, KM_PRESS, KM_ALT, 0);
1272         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_FLOOD_VALUE);
1273         RNA_float_set(kmi->ptr, "value", 0);
1274
1275         /* Invert mask */
1276         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", IKEY, KM_PRESS, KM_CTRL, 0);
1277         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_INVERT);
1278
1279         WM_keymap_add_item(keymap, "PAINT_OT_mask_lasso_gesture", LEFTMOUSE, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
1280
1281         /* Toggle mask visibility */
1282         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, KM_CTRL, 0);
1283         RNA_string_set(kmi->ptr, "data_path", "scene.tool_settings.sculpt.show_mask");
1284
1285         /* Toggle dynamic topology */
1286         WM_keymap_add_item(keymap, "SCULPT_OT_dynamic_topology_toggle", DKEY, KM_PRESS, KM_CTRL, 0);
1287
1288         /* Dynamic-topology detail size
1289          *
1290          * This should be improved further, perhaps by showing a triangle
1291          * grid rather than brush alpha */
1292         kmi = WM_keymap_add_item(keymap, "SCULPT_OT_set_detail_size", DKEY, KM_PRESS, KM_SHIFT, 0);
1293
1294         /* multires switch */
1295         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
1296         RNA_int_set(kmi->ptr, "level", 1);
1297         RNA_boolean_set(kmi->ptr, "relative", true);
1298
1299         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
1300         RNA_int_set(kmi->ptr, "level", -1);
1301         RNA_boolean_set(kmi->ptr, "relative", true);
1302
1303         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
1304         ed_keymap_paint_brush_radial_control(keymap, "sculpt", RC_ROTATION);
1305
1306         ed_keymap_stencil(keymap);
1307
1308         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_DRAW, XKEY, 0);
1309         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SMOOTH, SKEY, 0);
1310         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_PINCH, PKEY, 0);
1311         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_INFLATE, IKEY, 0);
1312         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_GRAB, GKEY, 0);
1313         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_LAYER, LKEY, 0);
1314         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_FLATTEN, TKEY, KM_SHIFT);
1315         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CLAY, CKEY, 0);
1316         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CREASE, CKEY, KM_SHIFT);
1317         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SNAKE_HOOK, KKEY, 0);
1318         kmi = keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_MASK, MKEY, 0);
1319         RNA_boolean_set(kmi->ptr, "toggle", 1);
1320         RNA_boolean_set(kmi->ptr, "create_missing", 1);
1321
1322         /* */
1323         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1324         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.stroke_method");
1325
1326         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1327         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
1328
1329         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1330
1331         /* Vertex Paint mode */
1332         keymap = WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
1333         keymap->poll = vertex_paint_mode_poll;
1334
1335         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
1336         WM_keymap_add_item(keymap, "PAINT_OT_brush_colors_flip", XKEY, KM_PRESS, 0, 0);
1337         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1338
1339         WM_keymap_add_item(keymap,
1340                            "PAINT_OT_vertex_color_set", KKEY, KM_PRESS, KM_SHIFT, 0);
1341
1342         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
1343         ed_keymap_paint_brush_radial_control(keymap, "vertex_paint", RC_COLOR | RC_COLOR_OVERRIDE | RC_ROTATION);
1344
1345         ed_keymap_stencil(keymap);
1346
1347         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1348         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
1349
1350         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1351         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.use_smooth_stroke");
1352
1353         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1354
1355         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1356         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
1357
1358         /* Weight Paint mode */
1359         keymap = WM_keymap_find(keyconf, "Weight Paint", 0, 0);
1360         keymap->poll = weight_paint_mode_poll;
1361
1362         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
1363
1364         /* these keys are from 2.4x but could be changed */
1365         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
1366         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample_group", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
1367
1368         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_gradient", LEFTMOUSE, KM_PRESS, KM_ALT, 0)->ptr,           "type", WPAINT_GRADIENT_TYPE_LINEAR);
1369         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);
1370
1371         WM_keymap_add_item(keymap,
1372                            "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
1373
1374         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
1375         ed_keymap_paint_brush_radial_control(keymap, "weight_paint", RC_WEIGHT);
1376
1377         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1378         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
1379
1380         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* face mask toggle */
1381         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
1382
1383         /* note, conflicts with vertex paint, but this is more useful */
1384         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", VKEY, KM_PRESS, 0, 0); /* vert mask toggle */
1385         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask_vertex");
1386
1387         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1388         RNA_string_set(kmi->ptr, "data_path", "tool_settings.weight_paint.brush.use_smooth_stroke");
1389
1390         /*Weight paint's Vertex Selection Mode */
1391         keymap = WM_keymap_find(keyconf, "Weight Paint Vertex Selection", 0, 0);
1392         keymap->poll = vert_paint_poll;
1393         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", AKEY, KM_PRESS, 0, 0);
1394         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
1395         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1396         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1397         WM_keymap_add_item(keymap, "VIEW3D_OT_select_border", BKEY, KM_PRESS, 0, 0);
1398         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
1399         RNA_boolean_set(kmi->ptr, "deselect", false);
1400         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0);
1401         RNA_boolean_set(kmi->ptr, "deselect", true);
1402         WM_keymap_add_item(keymap, "VIEW3D_OT_select_circle", CKEY, KM_PRESS, 0, 0);
1403
1404         /* Image/Texture Paint mode */
1405         keymap = WM_keymap_find(keyconf, "Image Paint", 0, 0);
1406         keymap->poll = image_texture_paint_poll;
1407
1408         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1409         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1410         WM_keymap_add_item(keymap, "PAINT_OT_brush_colors_flip", XKEY, KM_PRESS, 0, 0);
1411         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
1412         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1413
1414         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
1415         ed_keymap_paint_brush_radial_control(
1416                 keymap, "image_paint",
1417                 RC_COLOR | RC_COLOR_OVERRIDE | RC_ZOOM | RC_ROTATION | RC_SECONDARY_ROTATION);
1418
1419         ed_keymap_stencil(keymap);
1420
1421         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1422         RNA_string_set(kmi->ptr, "data_path", "image_paint_object.data.use_paint_mask");
1423
1424         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1425         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.use_smooth_stroke");
1426
1427         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1428
1429         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1430         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.stroke_method");
1431
1432         /* face-mask mode */
1433         keymap = WM_keymap_find(keyconf, "Face Mask", 0, 0);
1434         keymap->poll = facemask_paint_poll;
1435
1436         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
1437         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
1438         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1439         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1440         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, 0, 0);
1441         RNA_boolean_set(kmi->ptr, "unselected", false);
1442         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
1443         RNA_boolean_set(kmi->ptr, "unselected", true);
1444         WM_keymap_add_item(keymap, "PAINT_OT_face_select_reveal", HKEY, KM_PRESS, KM_ALT, 0);
1445
1446         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
1447         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
1448         RNA_boolean_set(kmi->ptr, "deselect", false);
1449         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
1450         RNA_boolean_set(kmi->ptr, "deselect", true);
1451
1452         keymap = WM_keymap_find(keyconf, "UV Sculpt", 0, 0);
1453         keymap->poll = uv_sculpt_keymap_poll;
1454
1455         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", QKEY, KM_PRESS, 0, 0);
1456         RNA_string_set(kmi->ptr, "data_path", "tool_settings.use_uv_sculpt");
1457
1458         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1459         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1460         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1461
1462         ed_keymap_paint_brush_size(keymap, "tool_settings.uv_sculpt.brush.size");
1463         ed_keymap_paint_brush_radial_control(keymap, "uv_sculpt", 0);
1464
1465         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);
1466         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);
1467         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);
1468
1469         /* paint stroke */
1470         keymap = paint_stroke_modal_keymap(keyconf);
1471         WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
1472 }