Ctr-Alt-F radial control operator for texture painting, controls the
[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, int create_missing,
317                                   int 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         int create_missing = RNA_boolean_get(op->ptr, "create_missing");
354         int 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 int 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         return OPERATOR_CANCELLED;
623 }
624
625 static void stencil_control_calculate(StencilControlData *scd, const int mval[2])
626 {
627 #define PIXEL_MARGIN 5
628
629         float mdiff[2];
630         float mvalf[2] = {mval[0], mval[1]};
631         switch (scd->mode) {
632                 case STENCIL_TRANSLATE:
633                         sub_v2_v2v2(mdiff, mvalf, scd->init_mouse);
634                         add_v2_v2v2(scd->pos_target, scd->init_spos,
635                                     mdiff);
636                         CLAMP(scd->pos_target[0],
637                               -scd->dim_target[0] + PIXEL_MARGIN,
638                               scd->area_size[0] + scd->dim_target[0] - PIXEL_MARGIN);
639
640                         CLAMP(scd->pos_target[1],
641                               -scd->dim_target[1] + PIXEL_MARGIN,
642                               scd->area_size[1] + scd->dim_target[1] - PIXEL_MARGIN);
643
644                         break;
645                 case STENCIL_SCALE:
646                 {
647                         float len, factor;
648                         sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
649                         len = len_v2(mdiff);
650                         factor = len / scd->lenorig;
651                         copy_v2_v2(mdiff, scd->init_sdim);
652                         if (scd->constrain_mode != STENCIL_CONSTRAINT_Y)
653                                 mdiff[0] = factor * scd->init_sdim[0];
654                         if (scd->constrain_mode != STENCIL_CONSTRAINT_X)
655                                 mdiff[1] = factor * scd->init_sdim[1];
656                         CLAMP(mdiff[0], 5.0f, 10000.0f);
657                         CLAMP(mdiff[1], 5.0f, 10000.0f);
658                         copy_v2_v2(scd->dim_target, mdiff);
659                         break;
660                 }
661                 case STENCIL_ROTATE:
662                 {
663                         float angle;
664                         sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
665                         angle = atan2(mdiff[1], mdiff[0]);
666                         angle = scd->init_rot + angle - scd->init_angle;
667                         if (angle < 0.0f)
668                                 angle += (float)(2 * M_PI);
669                         if (angle > (float)(2 * M_PI))
670                                 angle -= (float)(2 * M_PI);
671                         *scd->rot_target = angle;
672                         break;
673                 }
674         }
675 #undef PIXEL_MARGIN
676 }
677
678 static int stencil_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
679 {
680         StencilControlData *scd = op->customdata;
681
682         if (event->type == scd->event_type && event->val == KM_RELEASE) {
683                 MEM_freeN(op->customdata);
684                 WM_event_add_notifier(C, NC_WINDOW, NULL);
685                 return OPERATOR_FINISHED;
686         }
687
688         switch (event->type) {
689                 case MOUSEMOVE:
690                         stencil_control_calculate(scd, event->mval);
691                         break;
692                 case ESCKEY:
693                         if (event->val == KM_PRESS) {
694                                 stencil_control_cancel(C, op);
695                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
696                                 return OPERATOR_CANCELLED;
697                         }
698                         break;
699                 case XKEY:
700                         if (event->val == KM_PRESS) {
701
702                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_X)
703                                         scd->constrain_mode = 0;
704                                 else
705                                         scd->constrain_mode = STENCIL_CONSTRAINT_X;
706
707                                 stencil_control_calculate(scd, event->mval);
708                         }
709                         break;
710                 case YKEY:
711                         if (event->val == KM_PRESS) {
712                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_Y)
713                                         scd->constrain_mode = 0;
714                                 else
715                                         scd->constrain_mode = STENCIL_CONSTRAINT_Y;
716
717                                 stencil_control_calculate(scd, event->mval);
718                         }
719                         break;
720                 default:
721                         break;
722         }
723
724         ED_region_tag_redraw(CTX_wm_region(C));
725
726         return OPERATOR_RUNNING_MODAL;
727 }
728
729 static int stencil_control_poll(bContext *C)
730 {
731         Paint *paint = BKE_paint_get_active_from_context(C);
732         Brush *br = BKE_paint_brush(paint);
733
734         return (br &&
735                 (br->mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL ||
736                  br->mask_mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL));
737 }
738
739 static void BRUSH_OT_stencil_control(wmOperatorType *ot)
740 {
741         static EnumPropertyItem stencil_control_items[] = {
742                 {STENCIL_TRANSLATE, "TRANSLATION", 0, "Translation", ""},
743                 {STENCIL_SCALE, "SCALE", 0, "Scale", ""},
744                 {STENCIL_ROTATE, "ROTATION", 0, "Rotation", ""},
745                 {0, NULL, 0, NULL, NULL}
746         };
747
748         static EnumPropertyItem stencil_texture_items[] = {
749                 {STENCIL_PRIMARY, "PRIMARY", 0, "Primary", ""},
750                 {STENCIL_SECONDARY, "SECONDARY", 0, "Secondary", ""},
751                 {0, NULL, 0, NULL, NULL}
752         };
753         /* identifiers */
754         ot->name = "Stencil Brush Control";
755         ot->description = "Control the stencil brush";
756         ot->idname = "BRUSH_OT_stencil_control";
757
758         /* api callbacks */
759         ot->invoke = stencil_control_invoke;
760         ot->modal = stencil_control_modal;
761         ot->cancel = stencil_control_cancel;
762         ot->poll = stencil_control_poll;
763
764         /* flags */
765         ot->flag = 0;
766
767         RNA_def_enum(ot->srna, "mode", stencil_control_items, STENCIL_TRANSLATE, "Tool", "");
768         RNA_def_enum(ot->srna, "texmode", stencil_texture_items, STENCIL_PRIMARY, "Tool", "");
769 }
770
771
772 static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *op)
773 {
774         Paint *paint = BKE_paint_get_active_from_context(C);
775         Brush *br = BKE_paint_brush(paint);
776         bool use_scale = RNA_boolean_get(op->ptr, "use_scale");
777         bool use_repeat = RNA_boolean_get(op->ptr, "use_repeat");
778         bool do_mask = RNA_boolean_get(op->ptr, "mask");
779         Tex *tex = NULL;
780         MTex *mtex = NULL;
781         if (br) {
782                 mtex = do_mask ? &br->mask_mtex : &br->mtex;
783                 tex = mtex->tex;
784         }
785
786         if (tex && tex->type == TEX_IMAGE && tex->ima) {
787                 float aspx, aspy;
788                 Image *ima = tex->ima;
789                 float orig_area, stencil_area, factor;
790                 ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
791
792                 if (use_scale) {
793                         aspx *= mtex->size[0];
794                         aspy *= mtex->size[1];
795                 }
796
797                 if (use_repeat && tex->extend == TEX_REPEAT) {
798                         aspx *= tex->xrepeat;
799                         aspy *= tex->yrepeat;
800                 }
801
802                 orig_area = aspx * aspy;
803
804                 if (do_mask) {
805                         stencil_area = br->mask_stencil_dimension[0] * br->mask_stencil_dimension[1];
806                 }
807                 else {
808                         stencil_area = br->stencil_dimension[0] * br->stencil_dimension[1];
809                 }
810
811                 factor = sqrt(stencil_area / orig_area);
812
813                 if (do_mask) {
814                         br->mask_stencil_dimension[0] = factor * aspx;
815                         br->mask_stencil_dimension[1] = factor * aspy;
816
817                 }
818                 else {
819                         br->stencil_dimension[0] = factor * aspx;
820                         br->stencil_dimension[1] = factor * aspy;
821                 }
822         }
823
824         WM_event_add_notifier(C, NC_WINDOW, NULL);
825
826         return OPERATOR_FINISHED;
827 }
828
829
830 static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
831 {
832         /* identifiers */
833         ot->name = "Image Aspect";
834         ot->description = "When using an image texture, adjust the stencil size to fit the image aspect ratio";
835         ot->idname = "BRUSH_OT_stencil_fit_image_aspect";
836
837         /* api callbacks */
838         ot->exec = stencil_fit_image_aspect_exec;
839         ot->poll = stencil_control_poll;
840
841         /* flags */
842         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
843
844         RNA_def_boolean(ot->srna, "use_repeat", 1, "Use Repeat", "Use repeat mapping values");
845         RNA_def_boolean(ot->srna, "use_scale", 1, "Use Scale", "Use texture scale values");
846         RNA_def_boolean(ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
847 }
848
849
850 static int stencil_reset_transform_exec(bContext *C, wmOperator *op)
851 {
852         Paint *paint = BKE_paint_get_active_from_context(C);
853         Brush *br = BKE_paint_brush(paint);
854         bool do_mask = RNA_boolean_get(op->ptr, "mask");
855
856         if (!br)
857                 return OPERATOR_CANCELLED;
858         
859         if (do_mask) {
860                 br->mask_stencil_pos[0] = 256;
861                 br->mask_stencil_pos[1] = 256;
862
863                 br->mask_stencil_dimension[0] = 256;
864                 br->mask_stencil_dimension[1] = 256;
865
866                 br->mask_mtex.rot = 0;
867         }
868         else {
869                 br->stencil_pos[0] = 256;
870                 br->stencil_pos[1] = 256;
871
872                 br->stencil_dimension[0] = 256;
873                 br->stencil_dimension[1] = 256;
874
875                 br->mtex.rot = 0;
876         }
877
878         WM_event_add_notifier(C, NC_WINDOW, NULL);
879
880         return OPERATOR_FINISHED;
881 }
882
883
884 static void BRUSH_OT_stencil_reset_transform(wmOperatorType *ot)
885 {
886         /* identifiers */
887         ot->name = "Reset Transform";
888         ot->description = "Reset the stencil transformation to the default";
889         ot->idname = "BRUSH_OT_stencil_reset_transform";
890
891         /* api callbacks */
892         ot->exec = stencil_reset_transform_exec;
893         ot->poll = stencil_control_poll;
894
895         /* flags */
896         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
897
898         RNA_def_boolean(ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
899 }
900
901
902 static void ed_keymap_stencil(wmKeyMap *keymap)
903 {
904         wmKeyMapItem *kmi;
905
906         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, 0, 0);
907         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
908         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT, 0);
909         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
910         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL, 0);
911         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
912
913         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_ALT, 0);
914         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
915         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
916         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT | KM_ALT, 0);
917         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
918         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
919         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL | KM_ALT, 0);
920         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
921         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
922 }
923
924 /**************************** registration **********************************/
925
926 void ED_operatortypes_paint(void)
927 {
928         /* brush */
929         WM_operatortype_append(BRUSH_OT_add);
930         WM_operatortype_append(BRUSH_OT_scale_size);
931         WM_operatortype_append(BRUSH_OT_curve_preset);
932         WM_operatortype_append(BRUSH_OT_reset);
933         WM_operatortype_append(BRUSH_OT_stencil_control);
934         WM_operatortype_append(BRUSH_OT_stencil_fit_image_aspect);
935         WM_operatortype_append(BRUSH_OT_stencil_reset_transform);
936
937         /* note, particle uses a different system, can be added with existing operators in wm.py */
938         WM_operatortype_append(PAINT_OT_brush_select);
939         WM_operatortype_append(BRUSH_OT_uv_sculpt_tool_set);
940
941         /* image */
942         WM_operatortype_append(PAINT_OT_texture_paint_toggle);
943         WM_operatortype_append(PAINT_OT_image_paint);
944         WM_operatortype_append(PAINT_OT_sample_color);
945         WM_operatortype_append(PAINT_OT_grab_clone);
946         WM_operatortype_append(PAINT_OT_project_image);
947         WM_operatortype_append(PAINT_OT_image_from_view);
948
949         /* weight */
950         WM_operatortype_append(PAINT_OT_weight_paint_toggle);
951         WM_operatortype_append(PAINT_OT_weight_paint);
952         WM_operatortype_append(PAINT_OT_weight_set);
953         WM_operatortype_append(PAINT_OT_weight_from_bones);
954         WM_operatortype_append(PAINT_OT_weight_gradient);
955         WM_operatortype_append(PAINT_OT_weight_sample);
956         WM_operatortype_append(PAINT_OT_weight_sample_group);
957
958         /* uv */
959         WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
960
961         /* vertex selection */
962         WM_operatortype_append(PAINT_OT_vert_select_all);
963         WM_operatortype_append(PAINT_OT_vert_select_ungrouped);
964
965         /* vertex */
966         WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
967         WM_operatortype_append(PAINT_OT_vertex_paint);
968         WM_operatortype_append(PAINT_OT_vertex_color_set);
969         WM_operatortype_append(PAINT_OT_vertex_color_smooth);
970
971         /* face-select */
972         WM_operatortype_append(PAINT_OT_face_select_linked);
973         WM_operatortype_append(PAINT_OT_face_select_linked_pick);
974         WM_operatortype_append(PAINT_OT_face_select_all);
975         WM_operatortype_append(PAINT_OT_face_select_hide);
976         WM_operatortype_append(PAINT_OT_face_select_reveal);
977
978         /* partial visibility */
979         WM_operatortype_append(PAINT_OT_hide_show);
980
981         /* paint masking */
982         WM_operatortype_append(PAINT_OT_mask_flood_fill);
983 }
984
985
986 static void ed_keymap_paint_brush_switch(wmKeyMap *keymap, const char *mode)
987 {
988         wmKeyMapItem *kmi;
989         int i;
990         /* index 0-9 (zero key is tenth), shift key for index 10-19 */
991         for (i = 0; i < 20; i++) {
992                 kmi = WM_keymap_add_item(keymap, "BRUSH_OT_active_index_set",
993                                          ZEROKEY + ((i + 1) % 10), KM_PRESS, i < 10 ? 0 : KM_SHIFT, 0);
994                 RNA_string_set(kmi->ptr, "mode", mode);
995                 RNA_int_set(kmi->ptr, "index", i);
996         }
997 }
998
999 static void ed_keymap_paint_brush_size(wmKeyMap *keymap, const char *UNUSED(path))
1000 {
1001         wmKeyMapItem *kmi;
1002
1003         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", LEFTBRACKETKEY, KM_PRESS, 0, 0);
1004         RNA_float_set(kmi->ptr, "scalar", 0.9);
1005
1006         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
1007         RNA_float_set(kmi->ptr, "scalar", 10.0 / 9.0); // 1.1111....
1008 }
1009
1010 typedef enum {
1011         RC_COLOR    = 1,
1012         RC_ROTATION = 2,
1013         RC_ZOOM     = 4,
1014         RC_WEIGHT   = 8,
1015         RC_SECONDARY_ROTATION = 16
1016 } RCFlags;
1017
1018 static void set_brush_rc_path(PointerRNA *ptr, const char *brush_path,
1019                               const char *output_name, const char *input_name)
1020 {
1021         char *path;
1022
1023         path = BLI_sprintfN("%s.%s", brush_path, input_name);
1024         RNA_string_set(ptr, output_name, path);
1025         MEM_freeN(path);
1026 }
1027
1028 static void set_brush_rc_props(PointerRNA *ptr, const char *paint,
1029                                const char *prop, const char *secondary_prop,
1030                                RCFlags flags)
1031 {
1032         const char *ups_path = "tool_settings.unified_paint_settings";
1033         char *brush_path;
1034
1035         brush_path = BLI_sprintfN("tool_settings.%s.brush", paint);
1036
1037         set_brush_rc_path(ptr, brush_path, "data_path_primary", prop);
1038         if (secondary_prop) {
1039                 set_brush_rc_path(ptr, ups_path, "use_secondary", secondary_prop);
1040                 set_brush_rc_path(ptr, ups_path, "data_path_secondary", prop);
1041         }
1042         else {
1043                 RNA_string_set(ptr, "use_secondary", "");
1044                 RNA_string_set(ptr, "data_path_secondary", "");
1045         }
1046         set_brush_rc_path(ptr, brush_path, "color_path", "cursor_color_add");
1047         if (flags & RC_SECONDARY_ROTATION)
1048                 set_brush_rc_path(ptr, brush_path, "rotation_path", "mask_texture_slot.angle");
1049         else
1050                 set_brush_rc_path(ptr, brush_path, "rotation_path", "texture_slot.angle");
1051         RNA_string_set(ptr, "image_id", brush_path);
1052
1053         if (flags & RC_COLOR)
1054                 set_brush_rc_path(ptr, brush_path, "fill_color_path", "color");
1055         else
1056                 RNA_string_set(ptr, "fill_color_path", "");
1057         if (flags & RC_ZOOM)
1058                 RNA_string_set(ptr, "zoom_path", "space_data.zoom");
1059         else
1060                 RNA_string_set(ptr, "zoom_path", "");
1061
1062         if (flags & RC_SECONDARY_ROTATION)
1063                 RNA_boolean_set(ptr, "secondary_tex", true);
1064         else
1065                 RNA_boolean_set(ptr, "secondary_tex", false);
1066
1067         MEM_freeN(brush_path);
1068 }
1069
1070 static void ed_keymap_paint_brush_radial_control(wmKeyMap *keymap, const char *paint,
1071                                                  RCFlags flags)
1072 {
1073         wmKeyMapItem *kmi;
1074         /* only size needs to follow zoom, strength shows fixed size circle */
1075         int flags_nozoom = flags & (~RC_ZOOM);
1076         int flags_noradial_secondary = flags & (~(RC_SECONDARY_ROTATION | RC_ZOOM));
1077
1078         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, 0, 0);
1079         set_brush_rc_props(kmi->ptr, paint, "size", "use_unified_size", flags);
1080
1081         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0);
1082         set_brush_rc_props(kmi->ptr, paint, "strength", "use_unified_strength", flags_nozoom);
1083
1084         if (flags & RC_WEIGHT) {
1085                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", WKEY, KM_PRESS, 0, 0);
1086                 set_brush_rc_props(kmi->ptr, paint, "weight", "use_unified_weight", flags_nozoom);
1087         }
1088
1089         if (flags & RC_ROTATION) {
1090                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0);
1091                 set_brush_rc_props(kmi->ptr, paint, "texture_slot.angle", NULL, flags_noradial_secondary);
1092         }
1093
1094         if (flags & RC_SECONDARY_ROTATION) {
1095                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL | KM_ALT, 0);
1096                 set_brush_rc_props(kmi->ptr, paint, "mask_texture_slot.angle", NULL, flags_nozoom);
1097         }
1098 }
1099
1100 static void paint_partial_visibility_keys(wmKeyMap *keymap)
1101 {
1102         wmKeyMapItem *kmi;
1103         
1104         /* Partial visibility */
1105         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_SHIFT, 0);
1106         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
1107         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
1108         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, 0, 0);
1109         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_HIDE);
1110         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
1111         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_ALT, 0);
1112         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
1113         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_ALL);
1114 }
1115
1116 void ED_keymap_paint(wmKeyConfig *keyconf)
1117 {
1118         wmKeyMap *keymap;
1119         wmKeyMapItem *kmi;
1120         int i;
1121         
1122         /* Sculpt mode */
1123         keymap = WM_keymap_find(keyconf, "Sculpt", 0, 0);
1124         keymap->poll = sculpt_mode_poll;
1125
1126         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1127         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1128         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1129
1130         /* Partial visibility, sculpt-only for now */
1131         paint_partial_visibility_keys(keymap);
1132
1133         for (i = 0; i <= 5; i++)
1134                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY + i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
1135
1136         /* Clear mask */
1137         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", MKEY, KM_PRESS, KM_ALT, 0);
1138         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_FLOOD_VALUE);
1139         RNA_float_set(kmi->ptr, "value", 0);
1140
1141         /* Invert mask */
1142         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", IKEY, KM_PRESS, KM_CTRL, 0);
1143         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_INVERT);
1144
1145         /* Toggle dynamic topology */
1146         WM_keymap_add_item(keymap, "SCULPT_OT_dynamic_topology_toggle", DKEY, KM_PRESS, KM_CTRL, 0);
1147
1148         /* Dynamic-topology detail size
1149          * 
1150          * This should be improved further, perhaps by showing a triangle
1151          * grid rather than brush alpha */
1152         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", DKEY, KM_PRESS, KM_SHIFT, 0);
1153         set_brush_rc_props(kmi->ptr, "sculpt", "detail_size", NULL, 0);
1154         RNA_string_set(kmi->ptr, "data_path_primary", "tool_settings.sculpt.detail_size");
1155
1156         /* multires switch */
1157         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
1158         RNA_int_set(kmi->ptr, "level", 1);
1159         RNA_boolean_set(kmi->ptr, "relative", TRUE);
1160
1161         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
1162         RNA_int_set(kmi->ptr, "level", -1);
1163         RNA_boolean_set(kmi->ptr, "relative", TRUE);
1164
1165         ed_keymap_paint_brush_switch(keymap, "sculpt");
1166         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
1167         ed_keymap_paint_brush_radial_control(keymap, "sculpt", RC_ROTATION);
1168
1169         ed_keymap_stencil(keymap);
1170
1171         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_DRAW, DKEY, 0);
1172         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SMOOTH, SKEY, 0);
1173         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_PINCH, PKEY, 0);
1174         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_INFLATE, IKEY, 0);
1175         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_GRAB, GKEY, 0);
1176         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_LAYER, LKEY, 0);
1177         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_FLATTEN, TKEY, KM_SHIFT);
1178         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CLAY, CKEY, 0);
1179         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CREASE, CKEY, KM_SHIFT);
1180         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SNAKE_HOOK, KKEY, 0);
1181         kmi = keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_MASK, MKEY, 0);
1182         RNA_boolean_set(kmi->ptr, "toggle", 1);
1183         RNA_boolean_set(kmi->ptr, "create_missing", 1);
1184
1185         /* */
1186         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
1187         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.sculpt_stroke_method");
1188
1189         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1190         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
1191
1192         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", RKEY, KM_PRESS, 0, 0);
1193         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.texture_angle_source_random");
1194
1195         /* Vertex Paint mode */
1196         keymap = WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
1197         keymap->poll = vertex_paint_mode_poll;
1198
1199         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
1200         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1201
1202         WM_keymap_add_item(keymap,
1203                            "PAINT_OT_vertex_color_set", KKEY, KM_PRESS, KM_SHIFT, 0);
1204
1205         ed_keymap_paint_brush_switch(keymap, "vertex_paint");
1206         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
1207         ed_keymap_paint_brush_radial_control(keymap, "vertex_paint", RC_COLOR | RC_ROTATION);
1208
1209         ed_keymap_stencil(keymap);
1210
1211         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1212         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
1213
1214         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1215         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.use_smooth_stroke");
1216
1217         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", RKEY, KM_PRESS, 0, 0);
1218         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.texture_angle_source_random");
1219
1220         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
1221         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
1222
1223         /* Weight Paint mode */
1224         keymap = WM_keymap_find(keyconf, "Weight Paint", 0, 0);
1225         keymap->poll = weight_paint_mode_poll;
1226
1227         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
1228
1229         /* these keys are from 2.4x but could be changed */
1230         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
1231         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample_group", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
1232
1233         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_gradient", LEFTMOUSE, KM_PRESS, KM_ALT, 0)->ptr,           "type", WPAINT_GRADIENT_TYPE_LINEAR);
1234         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);
1235
1236         WM_keymap_add_item(keymap,
1237                            "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
1238
1239         ed_keymap_paint_brush_switch(keymap, "weight_paint");
1240         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
1241         ed_keymap_paint_brush_radial_control(keymap, "weight_paint", RC_WEIGHT);
1242
1243         ed_keymap_stencil(keymap);
1244
1245         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
1246         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
1247
1248         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* face mask toggle */
1249         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
1250
1251         /* note, conflicts with vertex paint, but this is more useful */
1252         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", VKEY, KM_PRESS, 0, 0); /* vert mask toggle */
1253         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask_vertex");
1254
1255         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1256         RNA_string_set(kmi->ptr, "data_path", "tool_settings.weight_paint.brush.use_smooth_stroke");
1257
1258         /*Weight paint's Vertex Selection Mode */
1259         keymap = WM_keymap_find(keyconf, "Weight Paint Vertex Selection", 0, 0);
1260         keymap->poll = vert_paint_poll;
1261         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", AKEY, KM_PRESS, 0, 0);
1262         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
1263         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1264         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1265         WM_keymap_add_item(keymap, "VIEW3D_OT_select_border", BKEY, KM_PRESS, 0, 0);
1266         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
1267         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
1268         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0);
1269         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
1270         WM_keymap_add_item(keymap, "VIEW3D_OT_select_circle", CKEY, KM_PRESS, 0, 0);
1271
1272         /* Image/Texture Paint mode */
1273         keymap = WM_keymap_find(keyconf, "Image Paint", 0, 0);
1274         keymap->poll = image_texture_paint_poll;
1275
1276         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1277         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1278         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
1279         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1280
1281         ed_keymap_paint_brush_switch(keymap, "image_paint");
1282         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
1283         ed_keymap_paint_brush_radial_control(keymap, "image_paint", RC_COLOR | RC_ZOOM | RC_ROTATION | RC_SECONDARY_ROTATION);
1284
1285         ed_keymap_stencil(keymap);
1286
1287         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1288         RNA_string_set(kmi->ptr, "data_path", "image_paint_object.data.use_paint_mask");
1289
1290         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1291         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.use_smooth_stroke");
1292
1293         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", RKEY, KM_PRESS, 0, 0);
1294         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.texture_angle_source_random");
1295
1296         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
1297         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.stroke_method");
1298
1299         /* face-mask mode */
1300         keymap = WM_keymap_find(keyconf, "Face Mask", 0, 0);
1301         keymap->poll = facemask_paint_poll;
1302
1303         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
1304         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
1305         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1306         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1307         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, 0, 0);
1308         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
1309         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
1310         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
1311         WM_keymap_add_item(keymap, "PAINT_OT_face_select_reveal", HKEY, KM_PRESS, KM_ALT, 0);
1312
1313         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
1314         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
1315         RNA_boolean_set(kmi->ptr, "deselect", false);
1316         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
1317         RNA_boolean_set(kmi->ptr, "deselect", true);
1318
1319         keymap = WM_keymap_find(keyconf, "UV Sculpt", 0, 0);
1320         keymap->poll = uv_sculpt_poll;
1321
1322         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", QKEY, KM_PRESS, 0, 0);
1323         RNA_string_set(kmi->ptr, "data_path", "tool_settings.use_uv_sculpt");
1324
1325         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1326         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1327         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1328
1329         ed_keymap_paint_brush_size(keymap, "tool_settings.uv_sculpt.brush.size");
1330         ed_keymap_paint_brush_radial_control(keymap, "uv_sculpt", 0);
1331
1332         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);
1333         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);
1334         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);
1335
1336         /* paint stroke */
1337         keymap = paint_stroke_modal_keymap(keyconf);
1338         WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
1339 }