Stencil dimension control:
[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_object_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_brush_types.h"
36
37 #include "BKE_brush.h"
38 #include "BKE_context.h"
39 #include "BKE_paint.h"
40 #include "BKE_main.h"
41 #include "BKE_image.h"
42
43 #include "ED_sculpt.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
51 #include "RNA_access.h"
52 #include "RNA_define.h"
53 #include "RNA_enum_types.h"
54
55 #include "paint_intern.h"
56 #include "sculpt_intern.h"
57
58 #include <string.h>
59 //#include <stdio.h>
60 #include <stddef.h>
61
62 /* Brush operators */
63 static int brush_add_exec(bContext *C, wmOperator *UNUSED(op))
64 {
65         /*int type = RNA_enum_get(op->ptr, "type");*/
66         Paint *paint = paint_get_active_from_context(C);
67         Brush *br = paint_brush(paint);
68         Main *bmain = CTX_data_main(C);
69
70         if (br)
71                 br = BKE_brush_copy(br);
72         else
73                 br = BKE_brush_add(bmain, "Brush");
74
75         paint_brush_set(paint, br);
76
77         return OPERATOR_FINISHED;
78 }
79
80 static void BRUSH_OT_add(wmOperatorType *ot)
81 {
82         /* identifiers */
83         ot->name = "Add Brush";
84         ot->description = "Add brush by mode type";
85         ot->idname = "BRUSH_OT_add";
86         
87         /* api callbacks */
88         ot->exec = brush_add_exec;
89         
90         /* flags */
91         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
92 }
93
94
95 static int brush_scale_size_exec(bContext *C, wmOperator *op)
96 {
97         Scene *scene = CTX_data_scene(C);
98         Paint  *paint =  paint_get_active_from_context(C);
99         Brush  *brush =  paint_brush(paint);
100         // Object *ob = CTX_data_active_object(C);
101         float scalar = RNA_float_get(op->ptr, "scalar");
102
103         if (brush) {
104                 // pixel radius
105                 {
106                         const int old_size = BKE_brush_size_get(scene, brush);
107                         int size = (int)(scalar * old_size);
108
109                         if (old_size == size) {
110                                 if (scalar > 1) {
111                                         size++;
112                                 }
113                                 else if (scalar < 1) {
114                                         size--;
115                                 }
116                         }
117                         CLAMP(size, 1, 2000); // XXX magic number
118
119                         BKE_brush_size_set(scene, brush, size);
120                 }
121
122                 // unprojected radius
123                 {
124                         float unprojected_radius = scalar * BKE_brush_unprojected_radius_get(scene, brush);
125
126                         if (unprojected_radius < 0.001f) // XXX magic number
127                                 unprojected_radius = 0.001f;
128
129                         BKE_brush_unprojected_radius_set(scene, brush, unprojected_radius);
130                 }
131         }
132
133         return OPERATOR_FINISHED;
134 }
135
136 static void BRUSH_OT_scale_size(wmOperatorType *ot)
137 {
138         /* identifiers */
139         ot->name = "Scale Sculpt/Paint Brush Size";
140         ot->description = "Change brush size by a scalar";
141         ot->idname = "BRUSH_OT_scale_size";
142         
143         /* api callbacks */
144         ot->exec = brush_scale_size_exec;
145         
146         /* flags */
147         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
148
149         RNA_def_float(ot->srna, "scalar", 1, 0, 2, "Scalar", "Factor to scale brush size by", 0, 2);
150 }
151
152 static int vertex_color_set_exec(bContext *C, wmOperator *UNUSED(op))
153 {
154         Scene *scene = CTX_data_scene(C);
155         Object *obact = CTX_data_active_object(C);
156         unsigned int paintcol = vpaint_get_current_col(scene->toolsettings->vpaint);
157         vpaint_fill(obact, paintcol);
158         
159         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
160         return OPERATOR_FINISHED;
161 }
162
163 static void PAINT_OT_vertex_color_set(wmOperatorType *ot)
164 {
165         /* identifiers */
166         ot->name = "Set Vertex Colors";
167         ot->idname = "PAINT_OT_vertex_color_set";
168         ot->description = "Fill the active vertex color layer with the current paint color";
169         
170         /* api callbacks */
171         ot->exec = vertex_color_set_exec;
172         ot->poll = vertex_paint_mode_poll;
173         
174         /* flags */
175         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
176 }
177
178 static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op))
179 {
180         Paint *paint = paint_get_active_from_context(C);
181         Brush *brush = paint_brush(paint);
182         Object *ob = CTX_data_active_object(C);
183
184         if (!ob || !brush) return OPERATOR_CANCELLED;
185
186         if (ob->mode & OB_MODE_SCULPT)
187                 BKE_brush_sculpt_reset(brush);
188         /* TODO: other modes */
189
190         return OPERATOR_FINISHED;
191 }
192
193 static void BRUSH_OT_reset(wmOperatorType *ot)
194 {
195         /* identifiers */
196         ot->name = "Reset Brush";
197         ot->description = "Return brush to defaults based on current tool";
198         ot->idname = "BRUSH_OT_reset";
199         
200         /* api callbacks */
201         ot->exec = brush_reset_exec;
202         
203         /* flags */
204         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
205 }
206
207 static int brush_tool(const Brush *brush, size_t tool_offset)
208 {
209         return *(((char *)brush) + tool_offset);
210 }
211
212 static void brush_tool_set(const Brush *brush, size_t tool_offset, int tool)
213 {
214         *(((char *)brush) + tool_offset) = tool;
215 }
216
217 /* generic functions for setting the active brush based on the tool */
218 static Brush *brush_tool_cycle(Main *bmain, Brush *brush_orig, const int tool, const size_t tool_offset, const int ob_mode)
219 {
220         Brush *brush;
221
222         if (!brush_orig && !(brush_orig = bmain->brush.first)) {
223                 return NULL;
224         }
225
226         /* get the next brush with the active tool */
227         for (brush = brush_orig->id.next ? brush_orig->id.next : bmain->brush.first;
228              brush != brush_orig;
229              brush = brush->id.next ? brush->id.next : bmain->brush.first)
230         {
231                 if ((brush->ob_mode & ob_mode) &&
232                     (brush_tool(brush, tool_offset) == tool))
233                 {
234                         return brush;
235                 }
236         }
237
238         return NULL;
239 }
240
241 static Brush *brush_tool_toggle(Main *bmain, Brush *brush_orig, const int tool, const size_t tool_offset, const int ob_mode)
242 {
243         if (!brush_orig || brush_tool(brush_orig, tool_offset) != tool) {
244                 Brush *br;
245                 /* if the current brush is not using the desired tool, look
246                  * for one that is */
247                 br = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
248                 /* store the previously-selected brush */
249                 if (br)
250                         br->toggle_brush = brush_orig;
251                 
252                 return br;
253         }
254         else if (brush_orig->toggle_brush &&
255                  BLI_findindex(bmain->brush.first, brush_orig->toggle_brush) != -1)
256         {
257                 /* if current brush is using the desired tool, try to toggle
258                  * back to the previously selected brush (if it was set, and
259                  * if it still exists) */
260                 return brush_orig->toggle_brush;
261         }
262         else
263                 return NULL;
264 }
265
266 static int brush_generic_tool_set(Main *bmain, Paint *paint, const int tool,
267                                   const size_t tool_offset, const int ob_mode,
268                                   const char *tool_name, int create_missing,
269                                   int toggle)
270 {
271         Brush *brush, *brush_orig = paint_brush(paint);
272
273         if (toggle)
274                 brush = brush_tool_toggle(bmain, brush_orig, tool, tool_offset, ob_mode);
275         else
276                 brush = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
277
278         if (!brush && brush_tool(brush_orig, tool_offset) != tool && create_missing) {
279                 brush = BKE_brush_add(bmain, tool_name);
280                 brush_tool_set(brush, tool_offset, tool);
281                 brush->ob_mode = ob_mode;
282                 brush->toggle_brush = brush_orig;
283         }
284
285         if (brush) {
286                 paint_brush_set(paint, brush);
287                 WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
288                 return OPERATOR_FINISHED;
289         }
290         else {
291                 return OPERATOR_CANCELLED;
292         }
293 }
294
295 /* used in the PAINT_OT_brush_select operator */
296 #define OB_MODE_ACTIVE 0
297
298 static int brush_select_exec(bContext *C, wmOperator *op)
299 {
300         Main *bmain = CTX_data_main(C);
301         ToolSettings *toolsettings = CTX_data_tool_settings(C);
302         Paint *paint = NULL;
303         int tool, paint_mode = RNA_enum_get(op->ptr, "paint_mode");
304         int create_missing = RNA_boolean_get(op->ptr, "create_missing");
305         int toggle = RNA_boolean_get(op->ptr, "toggle");
306         const char *tool_name = "Brush";
307         size_t tool_offset;
308
309         if (paint_mode == OB_MODE_ACTIVE) {
310                 Object *ob = CTX_data_active_object(C);
311                 if (ob) {
312                         /* select current paint mode */
313                         paint_mode = ob->mode & OB_MODE_ALL_PAINT;
314                 }
315                 else {
316                         return OPERATOR_CANCELLED;
317                 }
318         }
319
320         switch (paint_mode) {
321                 case OB_MODE_SCULPT:
322                         paint = &toolsettings->sculpt->paint;
323                         tool_offset = offsetof(Brush, sculpt_tool);
324                         tool = RNA_enum_get(op->ptr, "sculpt_tool");
325                         RNA_enum_name_from_value(brush_sculpt_tool_items, tool, &tool_name);
326                         break;
327                 case OB_MODE_VERTEX_PAINT:
328                         paint = &toolsettings->vpaint->paint;
329                         tool_offset = offsetof(Brush, vertexpaint_tool);
330                         tool = RNA_enum_get(op->ptr, "vertex_paint_tool");
331                         RNA_enum_name_from_value(brush_vertex_tool_items, tool, &tool_name);
332                         break;
333                 case OB_MODE_WEIGHT_PAINT:
334                         paint = &toolsettings->wpaint->paint;
335                         /* vertexpaint_tool is used for weight paint mode */
336                         tool_offset = offsetof(Brush, vertexpaint_tool);
337                         tool = RNA_enum_get(op->ptr, "weight_paint_tool");
338                         RNA_enum_name_from_value(brush_vertex_tool_items, tool, &tool_name);
339                         break;
340                 case OB_MODE_TEXTURE_PAINT:
341                         paint = &toolsettings->imapaint.paint;
342                         tool_offset = offsetof(Brush, imagepaint_tool);
343                         tool = RNA_enum_get(op->ptr, "texture_paint_tool");
344                         RNA_enum_name_from_value(brush_image_tool_items, tool, &tool_name);
345                         break;
346                 default:
347                         /* invalid paint mode */
348                         return OPERATOR_CANCELLED;
349         }
350
351         return brush_generic_tool_set(bmain, paint, tool, tool_offset,
352                                       paint_mode, tool_name, create_missing,
353                                       toggle);
354 }
355
356 static void PAINT_OT_brush_select(wmOperatorType *ot)
357 {
358         static EnumPropertyItem paint_mode_items[] = {
359                 {OB_MODE_ACTIVE, "ACTIVE", 0, "Current", "Set brush for active paint mode"},
360                 {OB_MODE_SCULPT, "SCULPT", ICON_SCULPTMODE_HLT, "Sculpt", ""},
361                 {OB_MODE_VERTEX_PAINT, "VERTEX_PAINT", ICON_VPAINT_HLT, "Vertex Paint", ""},
362                 {OB_MODE_WEIGHT_PAINT, "WEIGHT_PAINT", ICON_WPAINT_HLT, "Weight Paint", ""},
363                 {OB_MODE_TEXTURE_PAINT, "TEXTURE_PAINT", ICON_TPAINT_HLT, "Texture Paint", ""},
364                 {0, NULL, 0, NULL, NULL}
365         };
366
367         /* identifiers */
368         ot->name = "Brush Select";
369         ot->description = "Select a paint mode's brush by tool type";
370         ot->idname = "PAINT_OT_brush_select";
371
372         /* api callbacks */
373         ot->exec = brush_select_exec;
374
375         /* flags */
376         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
377
378         /* props */
379         RNA_def_enum(ot->srna, "paint_mode", paint_mode_items, OB_MODE_ACTIVE, "Paint Mode", "");
380         RNA_def_enum(ot->srna, "sculpt_tool", brush_sculpt_tool_items, 0, "Sculpt Tool", "");
381         RNA_def_enum(ot->srna, "vertex_paint_tool", brush_vertex_tool_items, 0, "Vertex Paint Tool", "");
382         RNA_def_enum(ot->srna, "weight_paint_tool", brush_vertex_tool_items, 0, "Weight Paint Tool", "");
383         RNA_def_enum(ot->srna, "texture_paint_tool", brush_image_tool_items, 0, "Texture Paint Tool", "");
384
385         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "Toggle between two brushes rather than cycling");
386         RNA_def_boolean(ot->srna, "create_missing", 0, "Create Missing", "If the requested brush type does not exist, create a new brush");
387 }
388
389 static wmKeyMapItem *keymap_brush_select(wmKeyMap *keymap, int paint_mode,
390                                          int tool, int keymap_type,
391                                          int keymap_modifier)
392 {
393         wmKeyMapItem *kmi;
394         kmi = WM_keymap_add_item(keymap, "PAINT_OT_brush_select",
395                                  keymap_type, KM_PRESS, keymap_modifier, 0);
396
397         RNA_enum_set(kmi->ptr, "paint_mode", paint_mode);
398         
399         switch (paint_mode) {
400                 case OB_MODE_SCULPT:
401                         RNA_enum_set(kmi->ptr, "sculpt_tool", tool);
402                         break;
403                 case OB_MODE_VERTEX_PAINT:
404                         RNA_enum_set(kmi->ptr, "vertex_paint_tool", tool);
405                         break;
406                 case OB_MODE_WEIGHT_PAINT:
407                         RNA_enum_set(kmi->ptr, "weight_paint_tool", tool);
408                         break;
409                 case OB_MODE_TEXTURE_PAINT:
410                         RNA_enum_set(kmi->ptr, "texture_paint_tool", tool);
411                         break;
412         }
413
414         return kmi;
415 }
416
417 static int brush_uv_sculpt_tool_set_exec(bContext *C, wmOperator *op)
418 {
419         Brush *brush;
420         Scene *scene = CTX_data_scene(C);
421         ToolSettings *ts = scene->toolsettings;
422         ts->uv_sculpt_tool = RNA_enum_get(op->ptr, "tool");
423         brush = ts->uvsculpt->paint.brush;
424         /* To update toolshelf */
425         WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
426
427         return OPERATOR_FINISHED;
428 }
429
430 static void BRUSH_OT_uv_sculpt_tool_set(wmOperatorType *ot)
431 {
432         /* from rna_scene.c */
433         extern EnumPropertyItem uv_sculpt_tool_items[];
434         /* identifiers */
435         ot->name = "UV Sculpt Tool Set";
436         ot->description = "Set the UV sculpt tool";
437         ot->idname = "BRUSH_OT_uv_sculpt_tool_set";
438
439         /* api callbacks */
440         ot->exec = brush_uv_sculpt_tool_set_exec;
441         ot->poll = uv_sculpt_poll;
442
443         /* flags */
444         ot->flag = 0;
445
446         /* props */
447         ot->prop = RNA_def_enum(ot->srna, "tool", uv_sculpt_tool_items, 0, "Tool", "");
448 }
449
450 /***** Stencil Control *****/
451
452 typedef enum {
453 STENCIL_TRANSLATE,
454 STENCIL_SCALE,
455 STENCIL_ROTATE
456 } StencilControlMode;
457
458 typedef enum {
459 STENCIL_CONSTRAINT_X = 1,
460 STENCIL_CONSTRAINT_Y = 2
461 } StencilConstraint;
462
463 typedef struct {
464         float init_mouse[2];
465         float init_spos[2];
466         float init_sdim[2];
467         float init_rot;
468         float init_angle;
469         float lenorig;
470         StencilControlMode mode;
471         StencilConstraint constrain_mode;
472         Brush *br;
473 } StencilControlData;
474
475 static int stencil_control_invoke(bContext *C, wmOperator *op, const wmEvent *event)
476 {
477         Paint *paint = paint_get_active_from_context(C);
478         Brush *br = paint_brush(paint);
479         float mdiff[2];
480         float mvalf[2] = {event->mval[0], event->mval[1]};
481
482         StencilControlData *scd = MEM_mallocN(sizeof(StencilControlData), "stencil_control");
483
484         copy_v2_v2(scd->init_mouse, mvalf);
485         copy_v2_v2(scd->init_sdim, br->stencil_dimension);
486         copy_v2_v2(scd->init_spos, br->stencil_pos);
487         sub_v2_v2v2(mdiff, mvalf, br->stencil_pos);
488         scd->lenorig = len_v2(mdiff);
489         scd->br = br;
490         scd->init_rot = br->mtex.rot;
491         scd->init_angle = atan2(mdiff[1], mdiff[0]);
492         scd->mode = RNA_enum_get(op->ptr, "mode");
493
494         op->customdata = scd;
495         WM_event_add_modal_handler(C, op);
496
497         return OPERATOR_RUNNING_MODAL;
498 }
499
500
501 static int stencil_control_cancel(bContext *UNUSED(C), wmOperator *op)
502 {
503         StencilControlData *scd = op->customdata;
504         Brush *br = scd->br;
505
506         copy_v2_v2(br->stencil_dimension, scd->init_sdim);
507         copy_v2_v2(br->stencil_pos, scd->init_spos);
508         br->mtex.rot = scd->init_rot;
509         MEM_freeN(op->customdata);
510         return OPERATOR_CANCELLED;
511 }
512
513 static int stencil_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
514 {
515         StencilControlData *scd = op->customdata;
516
517         switch (event->type) {
518                 case MOUSEMOVE:
519                         {
520                                 float mdiff[2];
521                                 float mvalf[2] = {event->mval[0], event->mval[1]};
522                                 switch (scd->mode) {
523                                         case STENCIL_TRANSLATE:
524                                                 sub_v2_v2v2(mdiff, mvalf, scd->init_mouse);
525                                                 add_v2_v2v2(scd->br->stencil_pos, scd->init_spos,
526                                                             mdiff);
527                                                 break;
528                                         case STENCIL_SCALE:
529                                         {
530                                                 float len, factor;
531                                                 sub_v2_v2v2(mdiff, mvalf, scd->br->stencil_pos);
532                                                 len = len_v2(mdiff);
533                                                 factor = len / scd->lenorig;
534                                                 copy_v2_v2(mdiff, scd->init_sdim);
535                                                 if (scd->constrain_mode != STENCIL_CONSTRAINT_Y)
536                                                         mdiff[0] = factor * scd->init_sdim[0];
537                                                 if (scd->constrain_mode != STENCIL_CONSTRAINT_X)
538                                                         mdiff[1] = factor * scd->init_sdim[1];
539                                                 copy_v2_v2(scd->br->stencil_dimension, mdiff);
540                                                 break;
541                                         }
542                                         case STENCIL_ROTATE:
543                                         {
544                                                 float angle;
545                                                 sub_v2_v2v2(mdiff, mvalf, scd->br->stencil_pos);
546                                                 angle = atan2(mdiff[1], mdiff[0]);
547                                                 angle = scd->init_rot + angle - scd->init_angle;
548                                                 if (angle < 0.0f)
549                                                         angle += (float)(2 * M_PI);
550                                                 if (angle > (float)(2 * M_PI))
551                                                         angle -= (float)(2 * M_PI);
552                                                 scd->br->mtex.rot = angle;
553                                                 break;
554                                         }
555                                 }
556                         }
557                         break;
558                 /* XXX hardcoded! */
559                 case RIGHTMOUSE:
560                         if (event->val == KM_RELEASE) {
561                                 MEM_freeN(op->customdata);
562                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
563                                 return OPERATOR_FINISHED;
564                         }
565                 case ESCKEY:
566                         if (event->val == KM_PRESS) {
567                                 stencil_control_cancel(C, op);
568                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
569                                 return OPERATOR_CANCELLED;
570                         }
571                 case XKEY:
572                         if (event->val == KM_PRESS) {
573
574                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_X)
575                                         scd->constrain_mode = 0;
576                                 else
577                                         scd->constrain_mode = STENCIL_CONSTRAINT_X;
578                         }
579                         break;
580                 case YKEY:
581                         if (event->val == KM_PRESS) {
582                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_Y)
583                                         scd->constrain_mode = 0;
584                                 else
585                                         scd->constrain_mode = STENCIL_CONSTRAINT_Y;
586                         }
587                         break;
588                 default:
589                         break;
590         }
591
592         ED_region_tag_redraw(CTX_wm_region(C));
593
594         return OPERATOR_RUNNING_MODAL;
595 }
596
597 static int stencil_control_poll(bContext *C)
598 {
599         Paint *paint = paint_get_active_from_context(C);
600         Brush *br = paint_brush(paint);
601
602         return (br && br->mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL);
603 }
604
605 static void BRUSH_OT_stencil_control(wmOperatorType *ot)
606 {
607         static EnumPropertyItem stencil_control_items[] = {
608                 {STENCIL_TRANSLATE, "TRANSLATION", 0, "Translation", ""},
609                 {STENCIL_SCALE, "SCALE", 0, "Scale", ""},
610                 {STENCIL_ROTATE, "ROTATION", 0, "Rotation", ""},
611                 {0, NULL, 0, NULL, NULL}
612         };
613         /* identifiers */
614         ot->name = "Stencil Brush Control";
615         ot->description = "Control the stencil brush";
616         ot->idname = "BRUSH_OT_stencil_control";
617
618         /* api callbacks */
619         ot->invoke = stencil_control_invoke;
620         ot->modal = stencil_control_modal;
621         ot->cancel = stencil_control_cancel;
622         ot->poll = stencil_control_poll;
623
624         /* flags */
625         ot->flag = 0;
626
627         RNA_def_enum(ot->srna, "mode", stencil_control_items, 0, "Tool", "");
628 }
629
630
631 static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *UNUSED(op))
632 {
633         Paint *paint = paint_get_active_from_context(C);
634         Brush *br = paint_brush(paint);
635         Tex *tex = (br)? br->mtex.tex : NULL;
636
637         if (tex && tex->type == TEX_IMAGE && tex->ima) {
638                 float aspx, aspy;
639                 Image *ima = tex->ima;
640                 float orig_area, stencil_area, factor;
641                 ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
642
643                 orig_area = aspx*aspy;
644                 stencil_area = br->stencil_dimension[0]*br->stencil_dimension[1];
645
646                 factor = sqrt(stencil_area/orig_area);
647
648                 br->stencil_dimension[0] = factor*aspx;
649                 br->stencil_dimension[1] = factor*aspy;
650         }
651
652         return OPERATOR_FINISHED;
653 }
654
655
656 static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
657 {
658         /* identifiers */
659         ot->name = "Image Aspect";
660         ot->description = "Adjust the stencil size to fit image aspect ratio";
661         ot->idname = "BRUSH_OT_stencil_fit_image_aspect";
662
663         /* api callbacks */
664         ot->exec = stencil_fit_image_aspect_exec;
665         ot->poll = stencil_control_poll;
666
667         /* flags */
668         ot->flag = 0;
669 }
670
671
672 static void ed_keymap_stencil(wmKeyMap *keymap)
673 {
674         wmKeyMapItem *kmi;
675
676         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, 0, 0);
677         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
678         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT, 0);
679         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
680         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL, 0);
681         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
682
683 }
684
685 /**************************** registration **********************************/
686
687 void ED_operatortypes_paint(void)
688 {
689         /* brush */
690         WM_operatortype_append(BRUSH_OT_add);
691         WM_operatortype_append(BRUSH_OT_scale_size);
692         WM_operatortype_append(BRUSH_OT_curve_preset);
693         WM_operatortype_append(BRUSH_OT_reset);
694         WM_operatortype_append(BRUSH_OT_stencil_control);
695         WM_operatortype_append(BRUSH_OT_stencil_fit_image_aspect);
696
697         /* note, particle uses a different system, can be added with existing operators in wm.py */
698         WM_operatortype_append(PAINT_OT_brush_select);
699         WM_operatortype_append(BRUSH_OT_uv_sculpt_tool_set);
700
701         /* image */
702         WM_operatortype_append(PAINT_OT_texture_paint_toggle);
703         WM_operatortype_append(PAINT_OT_image_paint);
704         WM_operatortype_append(PAINT_OT_sample_color);
705         WM_operatortype_append(PAINT_OT_grab_clone);
706         WM_operatortype_append(PAINT_OT_project_image);
707         WM_operatortype_append(PAINT_OT_image_from_view);
708
709         /* weight */
710         WM_operatortype_append(PAINT_OT_weight_paint_toggle);
711         WM_operatortype_append(PAINT_OT_weight_paint);
712         WM_operatortype_append(PAINT_OT_weight_set);
713         WM_operatortype_append(PAINT_OT_weight_from_bones);
714         WM_operatortype_append(PAINT_OT_weight_gradient);
715         WM_operatortype_append(PAINT_OT_weight_sample);
716         WM_operatortype_append(PAINT_OT_weight_sample_group);
717
718         /* uv */
719         WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
720
721         /* vertex selection */
722         WM_operatortype_append(PAINT_OT_vert_select_all);
723         WM_operatortype_append(PAINT_OT_vert_select_ungrouped);
724
725         /* vertex */
726         WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
727         WM_operatortype_append(PAINT_OT_vertex_paint);
728         WM_operatortype_append(PAINT_OT_vertex_color_set);
729
730         /* face-select */
731         WM_operatortype_append(PAINT_OT_face_select_linked);
732         WM_operatortype_append(PAINT_OT_face_select_linked_pick);
733         WM_operatortype_append(PAINT_OT_face_select_all);
734         WM_operatortype_append(PAINT_OT_face_select_hide);
735         WM_operatortype_append(PAINT_OT_face_select_reveal);
736
737         /* partial visibility */
738         WM_operatortype_append(PAINT_OT_hide_show);
739
740         /* paint masking */
741         WM_operatortype_append(PAINT_OT_mask_flood_fill);
742 }
743
744
745 static void ed_keymap_paint_brush_switch(wmKeyMap *keymap, const char *mode)
746 {
747         wmKeyMapItem *kmi;
748         int i;
749         /* index 0-9 (zero key is tenth), shift key for index 10-19 */
750         for (i = 0; i < 20; i++) {
751                 kmi = WM_keymap_add_item(keymap, "BRUSH_OT_active_index_set",
752                                          ZEROKEY + ((i + 1) % 10), KM_PRESS, i < 10 ? 0 : KM_SHIFT, 0);
753                 RNA_string_set(kmi->ptr, "mode", mode);
754                 RNA_int_set(kmi->ptr, "index", i);
755         }
756 }
757
758 static void ed_keymap_paint_brush_size(wmKeyMap *keymap, const char *UNUSED(path))
759 {
760         wmKeyMapItem *kmi;
761
762         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", LEFTBRACKETKEY, KM_PRESS, 0, 0);
763         RNA_float_set(kmi->ptr, "scalar", 0.9);
764
765         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
766         RNA_float_set(kmi->ptr, "scalar", 10.0 / 9.0); // 1.1111....
767 }
768
769 typedef enum {
770         RC_COLOR = 1,
771         RC_ROTATION = 2,
772         RC_ZOOM = 4
773 } RCFlags;
774
775 static void set_brush_rc_path(PointerRNA *ptr, const char *brush_path,
776                               const char *output_name, const char *input_name)
777 {
778         char *path;
779
780         path = BLI_sprintfN("%s.%s", brush_path, input_name);
781         RNA_string_set(ptr, output_name, path);
782         MEM_freeN(path);
783 }
784
785 static void set_brush_rc_props(PointerRNA *ptr, const char *paint,
786                                const char *prop, const char *secondary_prop,
787                                RCFlags flags)
788 {
789         const char *ups_path = "tool_settings.unified_paint_settings";
790         char *brush_path;
791
792         brush_path = BLI_sprintfN("tool_settings.%s.brush", paint);
793
794         set_brush_rc_path(ptr, brush_path, "data_path_primary", prop);
795         if (secondary_prop) {
796                 set_brush_rc_path(ptr, ups_path, "use_secondary", secondary_prop);
797                 set_brush_rc_path(ptr, ups_path, "data_path_secondary", prop);
798         }
799         else {
800                 RNA_string_set(ptr, "use_secondary", "");
801                 RNA_string_set(ptr, "data_path_secondary", "");
802         }
803         set_brush_rc_path(ptr, brush_path, "color_path", "cursor_color_add");
804         set_brush_rc_path(ptr, brush_path, "rotation_path", "texture_slot.angle");
805         RNA_string_set(ptr, "image_id", brush_path);
806
807         if (flags & RC_COLOR)
808                 set_brush_rc_path(ptr, brush_path, "fill_color_path", "color");
809         else
810                 RNA_string_set(ptr, "fill_color_path", "");
811         if (flags & RC_ZOOM)
812                 RNA_string_set(ptr, "zoom_path", "space_data.zoom");
813         else
814                 RNA_string_set(ptr, "zoom_path", "");
815
816         MEM_freeN(brush_path);
817 }
818
819 static void ed_keymap_paint_brush_radial_control(wmKeyMap *keymap, const char *paint,
820                                                  RCFlags flags)
821 {
822         wmKeyMapItem *kmi;
823         /* only size needs to follow zoom, strength shows fixed size circle */
824         int flags_nozoom = flags & (~RC_ZOOM);
825
826         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, 0, 0);
827         set_brush_rc_props(kmi->ptr, paint, "size", "use_unified_size", flags);
828
829         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0);
830         set_brush_rc_props(kmi->ptr, paint, "strength", "use_unified_strength", flags_nozoom);
831
832         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", WKEY, KM_PRESS, 0, 0);
833         set_brush_rc_props(kmi->ptr, paint, "weight", "use_unified_weight", flags_nozoom);
834
835         if (flags & RC_ROTATION) {
836                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0);
837                 set_brush_rc_props(kmi->ptr, paint, "texture_slot.angle", NULL, flags_nozoom);
838         }
839 }
840
841 static void paint_partial_visibility_keys(wmKeyMap *keymap)
842 {
843         wmKeyMapItem *kmi;
844         
845         /* Partial visibility */
846         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_SHIFT, 0);
847         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
848         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
849         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, 0, 0);
850         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_HIDE);
851         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
852         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_ALT, 0);
853         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
854         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_ALL);
855 }
856
857 void ED_keymap_paint(wmKeyConfig *keyconf)
858 {
859         wmKeyMap *keymap;
860         wmKeyMapItem *kmi;
861         int i;
862         
863         /* Sculpt mode */
864         keymap = WM_keymap_find(keyconf, "Sculpt", 0, 0);
865         keymap->poll = sculpt_mode_poll;
866
867         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
868         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
869         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
870
871         /* Partial visibility, sculpt-only for now */
872         paint_partial_visibility_keys(keymap);
873
874         for (i = 0; i <= 5; i++)
875                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY + i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
876
877         /* Clear mask */
878         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", MKEY, KM_PRESS, KM_ALT, 0);
879         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_FLOOD_VALUE);
880         RNA_float_set(kmi->ptr, "value", 0);
881
882         /* Invert mask */
883         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", IKEY, KM_PRESS, KM_CTRL, 0);
884         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_INVERT);
885
886         /* Toggle dynamic topology */
887         WM_keymap_add_item(keymap, "SCULPT_OT_dynamic_topology_toggle", DKEY, KM_PRESS, KM_CTRL, 0);
888
889         /* Dynamic-topology detail size
890          * 
891          * This should be improved further, perhaps by showing a triangle
892          * grid rather than brush alpha */
893         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", DKEY, KM_PRESS, KM_SHIFT, 0);
894         set_brush_rc_props(kmi->ptr, "sculpt", "detail_size", NULL, 0);
895         RNA_string_set(kmi->ptr, "data_path_primary", "tool_settings.sculpt.detail_size");
896
897         /* multires switch */
898         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
899         RNA_int_set(kmi->ptr, "level", 1);
900         RNA_boolean_set(kmi->ptr, "relative", TRUE);
901
902         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
903         RNA_int_set(kmi->ptr, "level", -1);
904         RNA_boolean_set(kmi->ptr, "relative", TRUE);
905
906         ed_keymap_paint_brush_switch(keymap, "sculpt");
907         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
908         ed_keymap_paint_brush_radial_control(keymap, "sculpt", RC_ROTATION);
909
910         ed_keymap_stencil(keymap);
911
912         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_DRAW, DKEY, 0);
913         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SMOOTH, SKEY, 0);
914         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_PINCH, PKEY, 0);
915         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_INFLATE, IKEY, 0);
916         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_GRAB, GKEY, 0);
917         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_LAYER, LKEY, 0);
918         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_FLATTEN, TKEY, KM_SHIFT);
919         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CLAY, CKEY, 0);
920         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CREASE, CKEY, KM_SHIFT);
921         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SNAKE_HOOK, KKEY, 0);
922         kmi = keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_MASK, MKEY, 0);
923         RNA_boolean_set(kmi->ptr, "toggle", 1);
924         RNA_boolean_set(kmi->ptr, "create_missing", 1);
925
926         /* */
927         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
928         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.sculpt_stroke_method");
929
930         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
931         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
932
933         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", RKEY, KM_PRESS, 0, 0);
934         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.texture_angle_source_random");
935
936         /* Vertex Paint mode */
937         keymap = WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
938         keymap->poll = vertex_paint_mode_poll;
939
940         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
941         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
942
943         WM_keymap_add_item(keymap,
944                            "PAINT_OT_vertex_color_set", KKEY, KM_PRESS, KM_SHIFT, 0);
945
946         ed_keymap_paint_brush_switch(keymap, "vertex_paint");
947         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
948         ed_keymap_paint_brush_radial_control(keymap, "vertex_paint", RC_COLOR | RC_ROTATION);
949
950         ed_keymap_stencil(keymap);
951
952         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
953         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
954
955         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
956         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.use_smooth_stroke");
957
958         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", RKEY, KM_PRESS, 0, 0);
959         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.texture_angle_source_random");
960
961         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
962         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
963
964         /* Weight Paint mode */
965         keymap = WM_keymap_find(keyconf, "Weight Paint", 0, 0);
966         keymap->poll = weight_paint_mode_poll;
967
968         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
969
970         /* these keys are from 2.4x but could be changed */
971         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
972         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample_group", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
973
974         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_gradient", LEFTMOUSE, KM_PRESS, KM_ALT, 0)->ptr,           "type", WPAINT_GRADIENT_TYPE_LINEAR);
975         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);
976
977         WM_keymap_add_item(keymap,
978                            "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
979
980         ed_keymap_paint_brush_switch(keymap, "weight_paint");
981         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
982         ed_keymap_paint_brush_radial_control(keymap, "weight_paint", 0);
983
984         ed_keymap_stencil(keymap);
985
986         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
987         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
988
989         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* face mask toggle */
990         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
991
992         /* note, conflicts with vertex paint, but this is more useful */
993         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", VKEY, KM_PRESS, 0, 0); /* vert mask toggle */
994         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask_vertex");
995
996         WM_keymap_verify_item(keymap, "PAINT_OT_weight_from_bones", WKEY, KM_PRESS, 0, 0);
997
998         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
999         RNA_string_set(kmi->ptr, "data_path", "tool_settings.weight_paint.brush.use_smooth_stroke");
1000
1001         /*Weight paint's Vertex Selection Mode */
1002         keymap = WM_keymap_find(keyconf, "Weight Paint Vertex Selection", 0, 0);
1003         keymap->poll = vert_paint_poll;
1004         WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", AKEY, KM_PRESS, 0, 0);
1005         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1006         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1007         WM_keymap_add_item(keymap, "VIEW3D_OT_select_border", BKEY, KM_PRESS, 0, 0);
1008         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
1009         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
1010         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0);
1011         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
1012         WM_keymap_add_item(keymap, "VIEW3D_OT_select_circle", CKEY, KM_PRESS, 0, 0);
1013
1014         /* Image/Texture Paint mode */
1015         keymap = WM_keymap_find(keyconf, "Image Paint", 0, 0);
1016         keymap->poll = image_texture_paint_poll;
1017
1018         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1019         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1020         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
1021         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1022
1023         ed_keymap_paint_brush_switch(keymap, "image_paint");
1024         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
1025         ed_keymap_paint_brush_radial_control(keymap, "image_paint", RC_COLOR | RC_ZOOM | RC_ROTATION);
1026
1027         ed_keymap_stencil(keymap);
1028
1029         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1030         RNA_string_set(kmi->ptr, "data_path", "image_paint_object.data.use_paint_mask");
1031
1032         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1033         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.use_smooth_stroke");
1034
1035         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", RKEY, KM_PRESS, 0, 0);
1036         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.texture_angle_source_random");
1037
1038         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
1039         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.stroke_method");
1040
1041         /* face-mask mode */
1042         keymap = WM_keymap_find(keyconf, "Face Mask", 0, 0);
1043         keymap->poll = facemask_paint_poll;
1044
1045         WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
1046         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1047         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1048         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, 0, 0);
1049         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
1050         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
1051         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
1052         WM_keymap_add_item(keymap, "PAINT_OT_face_select_reveal", HKEY, KM_PRESS, KM_ALT, 0);
1053
1054         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
1055         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
1056
1057         keymap = WM_keymap_find(keyconf, "UV Sculpt", 0, 0);
1058         keymap->poll = uv_sculpt_poll;
1059
1060         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", QKEY, KM_PRESS, 0, 0);
1061         RNA_string_set(kmi->ptr, "data_path", "tool_settings.use_uv_sculpt");
1062
1063         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1064         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1065         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1066
1067         ed_keymap_paint_brush_size(keymap, "tool_settings.uv_sculpt.brush.size");
1068         ed_keymap_paint_brush_radial_control(keymap, "uv_sculpt", 0);
1069
1070         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);
1071         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);
1072         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);
1073
1074         /* paint stroke */
1075         keymap = paint_stroke_modal_keymap(keyconf);
1076         WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
1077 }