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