Merging r50192 through r50223 from trunk into soc-2011-tomato
[blender-staging.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
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_brush_types.h"
35
36 #include "BKE_brush.h"
37 #include "BKE_context.h"
38 #include "BKE_paint.h"
39 #include "BKE_main.h"
40
41 #include "ED_sculpt.h"
42 #include "ED_screen.h"
43 #include "UI_resources.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50 #include "RNA_enum_types.h"
51
52 #include "paint_intern.h"
53 #include "sculpt_intern.h"
54
55 #include <string.h>
56 //#include <stdio.h>
57 #include <stddef.h>
58
59 /* Brush operators */
60 static int brush_add_exec(bContext *C, wmOperator *UNUSED(op))
61 {
62         /*int type = RNA_enum_get(op->ptr, "type");*/
63         Paint *paint = paint_get_active_from_context(C);
64         struct Brush *br = paint_brush(paint);
65
66         if (br)
67                 br = BKE_brush_copy(br);
68         else
69                 br = BKE_brush_add("Brush");
70
71         paint_brush_set(paint, br);
72
73         return OPERATOR_FINISHED;
74 }
75
76 static void BRUSH_OT_add(wmOperatorType *ot)
77 {
78         /* identifiers */
79         ot->name = "Add Brush";
80         ot->description = "Add brush by mode type";
81         ot->idname = "BRUSH_OT_add";
82         
83         /* api callbacks */
84         ot->exec = brush_add_exec;
85         
86         /* flags */
87         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
88 }
89
90
91 static int brush_scale_size_exec(bContext *C, wmOperator *op)
92 {
93         Scene *scene = CTX_data_scene(C);
94         Paint  *paint =  paint_get_active_from_context(C);
95         struct Brush  *brush =  paint_brush(paint);
96         // Object *ob=     CTX_data_active_object(C);
97         float scalar = RNA_float_get(op->ptr, "scalar");
98
99         if (brush) {
100                 // pixel radius
101                 {
102                         const int old_size = BKE_brush_size_get(scene, brush);
103                         int size = (int)(scalar * old_size);
104
105                         if (old_size == size) {
106                                 if (scalar > 1) {
107                                         size++;
108                                 }
109                                 else if (scalar < 1) {
110                                         size--;
111                                 }
112                         }
113                         CLAMP(size, 1, 2000); // XXX magic number
114
115                         BKE_brush_size_set(scene, brush, size);
116                 }
117
118                 // unprojected radius
119                 {
120                         float unprojected_radius = scalar * BKE_brush_unprojected_radius_get(scene, brush);
121
122                         if (unprojected_radius < 0.001f) // XXX magic number
123                                 unprojected_radius = 0.001f;
124
125                         BKE_brush_unprojected_radius_set(scene, brush, unprojected_radius);
126                 }
127         }
128
129         return OPERATOR_FINISHED;
130 }
131
132 static void BRUSH_OT_scale_size(wmOperatorType *ot)
133 {
134         /* identifiers */
135         ot->name = "Scale Sculpt/Paint Brush Size";
136         ot->description = "Change brush size by a scalar";
137         ot->idname = "BRUSH_OT_scale_size";
138         
139         /* api callbacks */
140         ot->exec = brush_scale_size_exec;
141         
142         /* flags */
143         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
144
145         RNA_def_float(ot->srna, "scalar", 1, 0, 2, "Scalar", "Factor to scale brush size by", 0, 2);
146 }
147
148 static int vertex_color_set_exec(bContext *C, wmOperator *UNUSED(op))
149 {
150         Scene *scene = CTX_data_scene(C);
151         Object *obact = CTX_data_active_object(C);
152         unsigned int paintcol = vpaint_get_current_col(scene->toolsettings->vpaint);
153         vpaint_fill(obact, paintcol);
154         
155         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
156         return OPERATOR_FINISHED;
157 }
158
159 static void PAINT_OT_vertex_color_set(wmOperatorType *ot)
160 {
161         /* identifiers */
162         ot->name = "Set Vertex Colors";
163         ot->idname = "PAINT_OT_vertex_color_set";
164         ot->description = "Fill the active vertex color layer with the current paint color";
165         
166         /* api callbacks */
167         ot->exec = vertex_color_set_exec;
168         ot->poll = vertex_paint_mode_poll;
169         
170         /* flags */
171         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
172 }
173
174 static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op))
175 {
176         Paint *paint = paint_get_active_from_context(C);
177         struct Brush *brush = paint_brush(paint);
178         Object *ob = CTX_data_active_object(C);
179
180         if (!ob) return OPERATOR_CANCELLED;
181
182         if (ob->mode & OB_MODE_SCULPT)
183                 BKE_brush_sculpt_reset(brush);
184         /* TODO: other modes */
185
186         return OPERATOR_FINISHED;
187 }
188
189 static void BRUSH_OT_reset(wmOperatorType *ot)
190 {
191         /* identifiers */
192         ot->name = "Reset Brush";
193         ot->description = "Return brush to defaults based on current tool";
194         ot->idname = "BRUSH_OT_reset";
195         
196         /* api callbacks */
197         ot->exec = brush_reset_exec;
198         
199         /* flags */
200         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
201 }
202
203 static int brush_tool(const Brush *brush, size_t tool_offset)
204 {
205         return *(((char *)brush) + tool_offset);
206 }
207
208 static void brush_tool_set(const Brush *brush, size_t tool_offset, int tool)
209 {
210         *(((char *)brush) + tool_offset) = tool;
211 }
212
213 /* generic functions for setting the active brush based on the tool */
214 static Brush *brush_tool_cycle(Main *bmain, Brush *brush_orig, const int tool, const size_t tool_offset, const int ob_mode)
215 {
216         struct Brush *brush;
217
218         if (!brush_orig && !(brush_orig = bmain->brush.first)) {
219                 return NULL;
220         }
221
222         /* get the next brush with the active tool */
223         for (brush = brush_orig->id.next ? brush_orig->id.next : bmain->brush.first;
224              brush != brush_orig;
225              brush = brush->id.next ? brush->id.next : bmain->brush.first)
226         {
227                 if ((brush->ob_mode & ob_mode) &&
228                     (brush_tool(brush, tool_offset) == tool))
229                 {
230                         return brush;
231                 }
232         }
233
234         return NULL;
235 }
236
237 static Brush *brush_tool_toggle(Main *bmain, Brush *brush_orig, const int tool, const size_t tool_offset, const int ob_mode)
238 {
239         if (!brush_orig || brush_tool(brush_orig, tool_offset) != tool) {
240                 Brush *br;
241                 /* if the current brush is not using the desired tool, look
242                  * for one that is */
243                 br = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
244                 /* store the previously-selected brush */
245                 if (br)
246                         br->toggle_brush = brush_orig;
247                 
248                 return br;
249         }
250         else if (brush_orig->toggle_brush &&
251                  BLI_findindex(bmain->brush.first, brush_orig->toggle_brush) != -1)
252         {
253                 /* if current brush is using the desired tool, try to toggle
254                  * back to the previously selected brush (if it was set, and
255                  * if it still exists) */
256                 return brush_orig->toggle_brush;
257         }
258         else
259                 return NULL;
260 }
261
262 static int brush_generic_tool_set(Main *bmain, Paint *paint, const int tool,
263                                   const size_t tool_offset, const int ob_mode,
264                                   const char *tool_name, int create_missing,
265                                   int toggle)
266 {
267         struct Brush *brush, *brush_orig = paint_brush(paint);
268
269         if (toggle)
270                 brush = brush_tool_toggle(bmain, brush_orig, tool, tool_offset, ob_mode);
271         else
272                 brush = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
273
274         if (!brush && brush_tool(brush_orig, tool_offset) != tool && create_missing) {
275                 brush = BKE_brush_add(tool_name);
276                 brush_tool_set(brush, tool_offset, tool);
277                 brush->ob_mode = ob_mode;
278                 brush->toggle_brush = brush_orig;
279         }
280
281         if (brush) {
282                 paint_brush_set(paint, brush);
283                 WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
284                 return OPERATOR_FINISHED;
285         }
286         else {
287                 return OPERATOR_CANCELLED;
288         }
289 }
290
291 /* used in the PAINT_OT_brush_select operator */
292 #define OB_MODE_ACTIVE 0
293
294 static int brush_select_exec(bContext *C, wmOperator *op)
295 {
296         Main *bmain = CTX_data_main(C);
297         ToolSettings *toolsettings = CTX_data_tool_settings(C);
298         Paint *paint = NULL;
299         int tool, paint_mode = RNA_enum_get(op->ptr, "paint_mode");
300         int create_missing = RNA_boolean_get(op->ptr, "create_missing");
301         int toggle = RNA_boolean_get(op->ptr, "toggle");
302         const char *tool_name = "Brush";
303         size_t tool_offset;
304
305         if (paint_mode == OB_MODE_ACTIVE) {
306                 Object *ob = CTX_data_active_object(C);
307                 if (ob) {
308                         /* select current paint mode */
309                         paint_mode = ob->mode &
310                                      (OB_MODE_SCULPT |
311                                       OB_MODE_VERTEX_PAINT |
312                                       OB_MODE_WEIGHT_PAINT |
313                                       OB_MODE_TEXTURE_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 /**************************** registration **********************************/
451
452 void ED_operatortypes_paint(void)
453 {
454         /* brush */
455         WM_operatortype_append(BRUSH_OT_add);
456         WM_operatortype_append(BRUSH_OT_scale_size);
457         WM_operatortype_append(BRUSH_OT_curve_preset);
458         WM_operatortype_append(BRUSH_OT_reset);
459
460         /* note, particle uses a different system, can be added with existing operators in wm.py */
461         WM_operatortype_append(PAINT_OT_brush_select);
462         WM_operatortype_append(BRUSH_OT_uv_sculpt_tool_set);
463
464         /* image */
465         WM_operatortype_append(PAINT_OT_texture_paint_toggle);
466         WM_operatortype_append(PAINT_OT_image_paint);
467         WM_operatortype_append(PAINT_OT_sample_color);
468         WM_operatortype_append(PAINT_OT_grab_clone);
469         WM_operatortype_append(PAINT_OT_clone_cursor_set);
470         WM_operatortype_append(PAINT_OT_project_image);
471         WM_operatortype_append(PAINT_OT_image_from_view);
472
473         /* weight */
474         WM_operatortype_append(PAINT_OT_weight_paint_toggle);
475         WM_operatortype_append(PAINT_OT_weight_paint);
476         WM_operatortype_append(PAINT_OT_weight_set);
477         WM_operatortype_append(PAINT_OT_weight_from_bones);
478         WM_operatortype_append(PAINT_OT_weight_sample);
479         WM_operatortype_append(PAINT_OT_weight_sample_group);
480
481         /* uv */
482         WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
483
484         /* vertex selection */
485         WM_operatortype_append(PAINT_OT_vert_select_all);
486         WM_operatortype_append(PAINT_OT_vert_select_inverse);
487
488         /* vertex */
489         WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
490         WM_operatortype_append(PAINT_OT_vertex_paint);
491         WM_operatortype_append(PAINT_OT_vertex_color_set);
492
493         /* face-select */
494         WM_operatortype_append(PAINT_OT_face_select_linked);
495         WM_operatortype_append(PAINT_OT_face_select_linked_pick);
496         WM_operatortype_append(PAINT_OT_face_select_all);
497         WM_operatortype_append(PAINT_OT_face_select_inverse);
498         WM_operatortype_append(PAINT_OT_face_select_hide);
499         WM_operatortype_append(PAINT_OT_face_select_reveal);
500
501         /* partial visibility */
502         WM_operatortype_append(PAINT_OT_hide_show);
503
504         /* paint masking */
505         WM_operatortype_append(PAINT_OT_mask_flood_fill);
506 }
507
508
509 static void ed_keymap_paint_brush_switch(wmKeyMap *keymap, const char *mode)
510 {
511         wmKeyMapItem *kmi;
512         int i;
513         /* index 0-9 (zero key is tenth), shift key for index 10-19 */
514         for (i = 0; i < 20; i++) {
515                 kmi = WM_keymap_add_item(keymap, "BRUSH_OT_active_index_set",
516                                          ZEROKEY + ((i + 1) % 10), KM_PRESS, i < 10 ? 0 : KM_SHIFT, 0);
517                 RNA_string_set(kmi->ptr, "mode", mode);
518                 RNA_int_set(kmi->ptr, "index", i);
519         }
520 }
521
522 static void ed_keymap_paint_brush_size(wmKeyMap *keymap, const char *UNUSED(path))
523 {
524         wmKeyMapItem *kmi;
525
526         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", LEFTBRACKETKEY, KM_PRESS, 0, 0);
527         RNA_float_set(kmi->ptr, "scalar", 0.9);
528
529         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
530         RNA_float_set(kmi->ptr, "scalar", 10.0 / 9.0); // 1.1111....
531 }
532
533 typedef enum {
534         RC_COLOR = 1,
535         RC_ROTATION = 2,
536         RC_ZOOM = 4,
537 } RCFlags;
538
539 static void set_brush_rc_path(PointerRNA *ptr, const char *brush_path,
540                               const char *output_name, const char *input_name)
541 {
542         char *path;
543
544         path = BLI_sprintfN("%s.%s", brush_path, input_name);
545         RNA_string_set(ptr, output_name, path);
546         MEM_freeN(path);
547 }
548
549 static void set_brush_rc_props(PointerRNA *ptr, const char *paint,
550                                const char *prop, const char *secondary_prop,
551                                RCFlags flags)
552 {
553         const char *ups_path = "tool_settings.unified_paint_settings";
554         char *brush_path;
555
556         brush_path = BLI_sprintfN("tool_settings.%s.brush", paint);
557
558         set_brush_rc_path(ptr, brush_path, "data_path_primary", prop);
559         if (secondary_prop) {
560                 set_brush_rc_path(ptr, ups_path, "use_secondary", secondary_prop);
561                 set_brush_rc_path(ptr, ups_path, "data_path_secondary", prop);
562         }
563         else {
564                 RNA_string_set(ptr, "use_secondary", "");
565                 RNA_string_set(ptr, "data_path_secondary", "");
566         }
567         set_brush_rc_path(ptr, brush_path, "color_path", "cursor_color_add");
568         set_brush_rc_path(ptr, brush_path, "rotation_path", "texture_slot.angle");
569         RNA_string_set(ptr, "image_id", brush_path);
570
571         if (flags & RC_COLOR)
572                 set_brush_rc_path(ptr, brush_path, "fill_color_path", "color");
573         else
574                 RNA_string_set(ptr, "fill_color_path", "");
575         if (flags & RC_ZOOM)
576                 RNA_string_set(ptr, "zoom_path", "space_data.zoom");
577         else
578                 RNA_string_set(ptr, "zoom_path", "");
579
580         MEM_freeN(brush_path);
581 }
582
583 static void ed_keymap_paint_brush_radial_control(wmKeyMap *keymap, const char *paint,
584                                                  RCFlags flags)
585 {
586         wmKeyMapItem *kmi;
587
588         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, 0, 0);
589         set_brush_rc_props(kmi->ptr, paint, "size", "use_unified_size", flags);
590
591         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0);
592         set_brush_rc_props(kmi->ptr, paint, "strength", "use_unified_strength", flags);
593
594         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", WKEY, KM_PRESS, 0, 0);
595         set_brush_rc_props(kmi->ptr, paint, "weight", "use_unified_weight", flags);
596
597         if (flags & RC_ROTATION) {
598                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0);
599                 set_brush_rc_props(kmi->ptr, paint, "texture_slot.angle", NULL, flags);
600         }
601 }
602
603 void paint_partial_visibility_keys(wmKeyMap *keymap)
604 {
605         wmKeyMapItem *kmi;
606         
607         /* Partial visibility */
608         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_SHIFT, 0);
609         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
610         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
611         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, 0, 0);
612         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_HIDE);
613         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
614         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_ALT, 0);
615         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
616         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_ALL);
617 }
618
619 void ED_keymap_paint(wmKeyConfig *keyconf)
620 {
621         wmKeyMap *keymap;
622         wmKeyMapItem *kmi;
623         int i;
624         
625         /* Sculpt mode */
626         keymap = WM_keymap_find(keyconf, "Sculpt", 0, 0);
627         keymap->poll = sculpt_mode_poll;
628
629         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
630         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
631         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
632
633         /* Partial visibility, sculpt-only for now */
634         paint_partial_visibility_keys(keymap);
635
636         for (i = 0; i <= 5; i++)
637                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY + i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
638
639         /* Clear mask */
640         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", MKEY, KM_PRESS, KM_ALT, 0);
641         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_FLOOD_VALUE);
642         RNA_float_set(kmi->ptr, "value", 0);
643
644         /* Invert mask */
645         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", IKEY, KM_PRESS, KM_CTRL, 0);
646         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_INVERT);
647
648         /* multires switch */
649         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
650         RNA_int_set(kmi->ptr, "level", 1);
651         RNA_boolean_set(kmi->ptr, "relative", TRUE);
652
653         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
654         RNA_int_set(kmi->ptr, "level", -1);
655         RNA_boolean_set(kmi->ptr, "relative", TRUE);
656
657         ed_keymap_paint_brush_switch(keymap, "sculpt");
658         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
659         ed_keymap_paint_brush_radial_control(keymap, "sculpt", RC_ROTATION);
660
661         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_DRAW, DKEY, 0);
662         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SMOOTH, SKEY, 0);
663         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_PINCH, PKEY, 0);
664         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_INFLATE, IKEY, 0);
665         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_GRAB, GKEY, 0);
666         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_LAYER, LKEY, 0);
667         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_FLATTEN, TKEY, KM_SHIFT);
668         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CLAY, CKEY, 0);
669         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CREASE, CKEY, KM_SHIFT);
670         kmi = keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_MASK, MKEY, 0);
671         RNA_boolean_set(kmi->ptr, "toggle", 1);
672         RNA_boolean_set(kmi->ptr, "create_missing", 1);
673
674         /* */
675         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
676         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.stroke_method");
677
678         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
679         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
680
681         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", RKEY, KM_PRESS, 0, 0);
682         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.texture_angle_source_random");
683
684         /* Vertex Paint mode */
685         keymap = WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
686         keymap->poll = vertex_paint_mode_poll;
687
688         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
689         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", RIGHTMOUSE, KM_PRESS, 0, 0);
690
691         WM_keymap_add_item(keymap,
692                            "PAINT_OT_vertex_color_set", KKEY, KM_PRESS, KM_SHIFT, 0);
693
694         ed_keymap_paint_brush_switch(keymap, "vertex_paint");
695         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
696         ed_keymap_paint_brush_radial_control(keymap, "vertex_paint", RC_COLOR);
697
698         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
699         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
700
701         /* Weight Paint mode */
702         keymap = WM_keymap_find(keyconf, "Weight Paint", 0, 0);
703         keymap->poll = weight_paint_mode_poll;
704
705         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
706
707         /* these keys are from 2.4x but could be changed */
708         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
709         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample_group", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
710
711         WM_keymap_add_item(keymap,
712                            "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
713
714         ed_keymap_paint_brush_switch(keymap, "weight_paint");
715         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
716         ed_keymap_paint_brush_radial_control(keymap, "weight_paint", 0);
717
718         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* face mask toggle */
719         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
720
721         /* note, conflicts with vertex paint, but this is more useful */
722         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", VKEY, KM_PRESS, 0, 0); /* vert mask toggle */
723         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask_vertex");
724
725         WM_keymap_verify_item(keymap, "PAINT_OT_weight_from_bones", WKEY, KM_PRESS, 0, 0);
726
727         
728         /*Weight paint's Vertex Selection Mode */
729         keymap = WM_keymap_find(keyconf, "Weight Paint Vertex Selection", 0, 0);
730         keymap->poll = vert_paint_poll;
731         WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", AKEY, KM_PRESS, 0, 0);
732         WM_keymap_add_item(keymap, "PAINT_OT_vert_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
733         WM_keymap_add_item(keymap, "VIEW3D_OT_select_border", BKEY, KM_PRESS, 0, 0);
734         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
735         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
736         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0);
737         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
738         WM_keymap_add_item(keymap, "VIEW3D_OT_select_circle", CKEY, KM_PRESS, 0, 0);
739
740         /* Image/Texture Paint mode */
741         keymap = WM_keymap_find(keyconf, "Image Paint", 0, 0);
742         keymap->poll = image_texture_paint_poll;
743
744         WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0, 0);
745         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
746         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", RIGHTMOUSE, KM_PRESS, 0, 0);
747         WM_keymap_add_item(keymap, "PAINT_OT_clone_cursor_set", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
748
749         ed_keymap_paint_brush_switch(keymap, "image_paint");
750         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
751         ed_keymap_paint_brush_radial_control(keymap, "image_paint", RC_COLOR | RC_ZOOM);
752
753         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
754         RNA_string_set(kmi->ptr, "data_path", "image_paint_object.data.use_paint_mask");
755
756         /* face-mask mode */
757         keymap = WM_keymap_find(keyconf, "Face Mask", 0, 0);
758         keymap->poll = facemask_paint_poll;
759
760         WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
761         WM_keymap_add_item(keymap, "PAINT_OT_face_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
762         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, 0, 0);
763         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
764         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
765         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
766         WM_keymap_add_item(keymap, "PAINT_OT_face_select_reveal", HKEY, KM_PRESS, KM_ALT, 0);
767
768         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
769         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
770
771         keymap = WM_keymap_find(keyconf, "UV Sculpt", 0, 0);
772         keymap->poll = uv_sculpt_poll;
773
774         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", QKEY, KM_PRESS, 0, 0);
775         RNA_string_set(kmi->ptr, "data_path", "tool_settings.use_uv_sculpt");
776
777         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
778         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
779         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
780
781         ed_keymap_paint_brush_size(keymap, "tool_settings.uv_sculpt.brush.size");
782         ed_keymap_paint_brush_radial_control(keymap, "uv_sculpt", 0);
783
784         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);
785         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);
786         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);
787
788         /* paint stroke */
789         keymap = paint_stroke_modal_keymap(keyconf);
790         WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
791 }