Fix #34040: Moving Normal Node with enabled Cycles Material Preview crashes
[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
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         Main *bmain = CTX_data_main(C);
66
67         if (br)
68                 br = BKE_brush_copy(br);
69         else
70                 br = BKE_brush_add(bmain, "Brush");
71
72         paint_brush_set(paint, br);
73
74         return OPERATOR_FINISHED;
75 }
76
77 static void BRUSH_OT_add(wmOperatorType *ot)
78 {
79         /* identifiers */
80         ot->name = "Add Brush";
81         ot->description = "Add brush by mode type";
82         ot->idname = "BRUSH_OT_add";
83         
84         /* api callbacks */
85         ot->exec = brush_add_exec;
86         
87         /* flags */
88         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
89 }
90
91
92 static int brush_scale_size_exec(bContext *C, wmOperator *op)
93 {
94         Scene *scene = CTX_data_scene(C);
95         Paint  *paint =  paint_get_active_from_context(C);
96         struct Brush  *brush =  paint_brush(paint);
97         // Object *ob = CTX_data_active_object(C);
98         float scalar = RNA_float_get(op->ptr, "scalar");
99
100         if (brush) {
101                 // pixel radius
102                 {
103                         const int old_size = BKE_brush_size_get(scene, brush);
104                         int size = (int)(scalar * old_size);
105
106                         if (old_size == size) {
107                                 if (scalar > 1) {
108                                         size++;
109                                 }
110                                 else if (scalar < 1) {
111                                         size--;
112                                 }
113                         }
114                         CLAMP(size, 1, 2000); // XXX magic number
115
116                         BKE_brush_size_set(scene, brush, size);
117                 }
118
119                 // unprojected radius
120                 {
121                         float unprojected_radius = scalar * BKE_brush_unprojected_radius_get(scene, brush);
122
123                         if (unprojected_radius < 0.001f) // XXX magic number
124                                 unprojected_radius = 0.001f;
125
126                         BKE_brush_unprojected_radius_set(scene, brush, unprojected_radius);
127                 }
128         }
129
130         return OPERATOR_FINISHED;
131 }
132
133 static void BRUSH_OT_scale_size(wmOperatorType *ot)
134 {
135         /* identifiers */
136         ot->name = "Scale Sculpt/Paint Brush Size";
137         ot->description = "Change brush size by a scalar";
138         ot->idname = "BRUSH_OT_scale_size";
139         
140         /* api callbacks */
141         ot->exec = brush_scale_size_exec;
142         
143         /* flags */
144         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
145
146         RNA_def_float(ot->srna, "scalar", 1, 0, 2, "Scalar", "Factor to scale brush size by", 0, 2);
147 }
148
149 static int vertex_color_set_exec(bContext *C, wmOperator *UNUSED(op))
150 {
151         Scene *scene = CTX_data_scene(C);
152         Object *obact = CTX_data_active_object(C);
153         unsigned int paintcol = vpaint_get_current_col(scene->toolsettings->vpaint);
154         vpaint_fill(obact, paintcol);
155         
156         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
157         return OPERATOR_FINISHED;
158 }
159
160 static void PAINT_OT_vertex_color_set(wmOperatorType *ot)
161 {
162         /* identifiers */
163         ot->name = "Set Vertex Colors";
164         ot->idname = "PAINT_OT_vertex_color_set";
165         ot->description = "Fill the active vertex color layer with the current paint color";
166         
167         /* api callbacks */
168         ot->exec = vertex_color_set_exec;
169         ot->poll = vertex_paint_mode_poll;
170         
171         /* flags */
172         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
173 }
174
175 static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op))
176 {
177         Paint *paint = paint_get_active_from_context(C);
178         struct Brush *brush = paint_brush(paint);
179         Object *ob = CTX_data_active_object(C);
180
181         if (!ob) return OPERATOR_CANCELLED;
182
183         if (ob->mode & OB_MODE_SCULPT)
184                 BKE_brush_sculpt_reset(brush);
185         /* TODO: other modes */
186
187         return OPERATOR_FINISHED;
188 }
189
190 static void BRUSH_OT_reset(wmOperatorType *ot)
191 {
192         /* identifiers */
193         ot->name = "Reset Brush";
194         ot->description = "Return brush to defaults based on current tool";
195         ot->idname = "BRUSH_OT_reset";
196         
197         /* api callbacks */
198         ot->exec = brush_reset_exec;
199         
200         /* flags */
201         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
202 }
203
204 static int brush_tool(const Brush *brush, size_t tool_offset)
205 {
206         return *(((char *)brush) + tool_offset);
207 }
208
209 static void brush_tool_set(const Brush *brush, size_t tool_offset, int tool)
210 {
211         *(((char *)brush) + tool_offset) = tool;
212 }
213
214 /* generic functions for setting the active brush based on the tool */
215 static Brush *brush_tool_cycle(Main *bmain, Brush *brush_orig, const int tool, const size_t tool_offset, const int ob_mode)
216 {
217         struct Brush *brush;
218
219         if (!brush_orig && !(brush_orig = bmain->brush.first)) {
220                 return NULL;
221         }
222
223         /* get the next brush with the active tool */
224         for (brush = brush_orig->id.next ? brush_orig->id.next : bmain->brush.first;
225              brush != brush_orig;
226              brush = brush->id.next ? brush->id.next : bmain->brush.first)
227         {
228                 if ((brush->ob_mode & ob_mode) &&
229                     (brush_tool(brush, tool_offset) == tool))
230                 {
231                         return brush;
232                 }
233         }
234
235         return NULL;
236 }
237
238 static Brush *brush_tool_toggle(Main *bmain, Brush *brush_orig, const int tool, const size_t tool_offset, const int ob_mode)
239 {
240         if (!brush_orig || brush_tool(brush_orig, tool_offset) != tool) {
241                 Brush *br;
242                 /* if the current brush is not using the desired tool, look
243                  * for one that is */
244                 br = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
245                 /* store the previously-selected brush */
246                 if (br)
247                         br->toggle_brush = brush_orig;
248                 
249                 return br;
250         }
251         else if (brush_orig->toggle_brush &&
252                  BLI_findindex(bmain->brush.first, brush_orig->toggle_brush) != -1)
253         {
254                 /* if current brush is using the desired tool, try to toggle
255                  * back to the previously selected brush (if it was set, and
256                  * if it still exists) */
257                 return brush_orig->toggle_brush;
258         }
259         else
260                 return NULL;
261 }
262
263 static int brush_generic_tool_set(Main *bmain, Paint *paint, const int tool,
264                                   const size_t tool_offset, const int ob_mode,
265                                   const char *tool_name, int create_missing,
266                                   int toggle)
267 {
268         struct Brush *brush, *brush_orig = paint_brush(paint);
269
270         if (toggle)
271                 brush = brush_tool_toggle(bmain, brush_orig, tool, tool_offset, ob_mode);
272         else
273                 brush = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
274
275         if (!brush && brush_tool(brush_orig, tool_offset) != tool && create_missing) {
276                 brush = BKE_brush_add(bmain, tool_name);
277                 brush_tool_set(brush, tool_offset, tool);
278                 brush->ob_mode = ob_mode;
279                 brush->toggle_brush = brush_orig;
280         }
281
282         if (brush) {
283                 paint_brush_set(paint, brush);
284                 WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
285                 return OPERATOR_FINISHED;
286         }
287         else {
288                 return OPERATOR_CANCELLED;
289         }
290 }
291
292 /* used in the PAINT_OT_brush_select operator */
293 #define OB_MODE_ACTIVE 0
294
295 static int brush_select_exec(bContext *C, wmOperator *op)
296 {
297         Main *bmain = CTX_data_main(C);
298         ToolSettings *toolsettings = CTX_data_tool_settings(C);
299         Paint *paint = NULL;
300         int tool, paint_mode = RNA_enum_get(op->ptr, "paint_mode");
301         int create_missing = RNA_boolean_get(op->ptr, "create_missing");
302         int toggle = RNA_boolean_get(op->ptr, "toggle");
303         const char *tool_name = "Brush";
304         size_t tool_offset;
305
306         if (paint_mode == OB_MODE_ACTIVE) {
307                 Object *ob = CTX_data_active_object(C);
308                 if (ob) {
309                         /* select current paint mode */
310                         paint_mode = ob->mode &
311                                      (OB_MODE_SCULPT |
312                                       OB_MODE_VERTEX_PAINT |
313                                       OB_MODE_WEIGHT_PAINT |
314                                       OB_MODE_TEXTURE_PAINT);
315                 }
316                 else {
317                         return OPERATOR_CANCELLED;
318                 }
319         }
320
321         switch (paint_mode) {
322                 case OB_MODE_SCULPT:
323                         paint = &toolsettings->sculpt->paint;
324                         tool_offset = offsetof(Brush, sculpt_tool);
325                         tool = RNA_enum_get(op->ptr, "sculpt_tool");
326                         RNA_enum_name_from_value(brush_sculpt_tool_items, tool, &tool_name);
327                         break;
328                 case OB_MODE_VERTEX_PAINT:
329                         paint = &toolsettings->vpaint->paint;
330                         tool_offset = offsetof(Brush, vertexpaint_tool);
331                         tool = RNA_enum_get(op->ptr, "vertex_paint_tool");
332                         RNA_enum_name_from_value(brush_vertex_tool_items, tool, &tool_name);
333                         break;
334                 case OB_MODE_WEIGHT_PAINT:
335                         paint = &toolsettings->wpaint->paint;
336                         /* vertexpaint_tool is used for weight paint mode */
337                         tool_offset = offsetof(Brush, vertexpaint_tool);
338                         tool = RNA_enum_get(op->ptr, "weight_paint_tool");
339                         RNA_enum_name_from_value(brush_vertex_tool_items, tool, &tool_name);
340                         break;
341                 case OB_MODE_TEXTURE_PAINT:
342                         paint = &toolsettings->imapaint.paint;
343                         tool_offset = offsetof(Brush, imagepaint_tool);
344                         tool = RNA_enum_get(op->ptr, "texture_paint_tool");
345                         RNA_enum_name_from_value(brush_image_tool_items, tool, &tool_name);
346                         break;
347                 default:
348                         /* invalid paint mode */
349                         return OPERATOR_CANCELLED;
350         }
351
352         return brush_generic_tool_set(bmain, paint, tool, tool_offset,
353                                       paint_mode, tool_name, create_missing,
354                                       toggle);
355 }
356
357 static void PAINT_OT_brush_select(wmOperatorType *ot)
358 {
359         static EnumPropertyItem paint_mode_items[] = {
360                 {OB_MODE_ACTIVE, "ACTIVE", 0, "Current", "Set brush for active paint mode"},
361                 {OB_MODE_SCULPT, "SCULPT", ICON_SCULPTMODE_HLT, "Sculpt", ""},
362                 {OB_MODE_VERTEX_PAINT, "VERTEX_PAINT", ICON_VPAINT_HLT, "Vertex Paint", ""},
363                 {OB_MODE_WEIGHT_PAINT, "WEIGHT_PAINT", ICON_WPAINT_HLT, "Weight Paint", ""},
364                 {OB_MODE_TEXTURE_PAINT, "TEXTURE_PAINT", ICON_TPAINT_HLT, "Texture Paint", ""},
365                 {0, NULL, 0, NULL, NULL}
366         };
367
368         /* identifiers */
369         ot->name = "Brush Select";
370         ot->description = "Select a paint mode's brush by tool type";
371         ot->idname = "PAINT_OT_brush_select";
372
373         /* api callbacks */
374         ot->exec = brush_select_exec;
375
376         /* flags */
377         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
378
379         /* props */
380         RNA_def_enum(ot->srna, "paint_mode", paint_mode_items, OB_MODE_ACTIVE, "Paint Mode", "");
381         RNA_def_enum(ot->srna, "sculpt_tool", brush_sculpt_tool_items, 0, "Sculpt Tool", "");
382         RNA_def_enum(ot->srna, "vertex_paint_tool", brush_vertex_tool_items, 0, "Vertex Paint Tool", "");
383         RNA_def_enum(ot->srna, "weight_paint_tool", brush_vertex_tool_items, 0, "Weight Paint Tool", "");
384         RNA_def_enum(ot->srna, "texture_paint_tool", brush_image_tool_items, 0, "Texture Paint Tool", "");
385
386         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "Toggle between two brushes rather than cycling");
387         RNA_def_boolean(ot->srna, "create_missing", 0, "Create Missing", "If the requested brush type does not exist, create a new brush");
388 }
389
390 static wmKeyMapItem *keymap_brush_select(wmKeyMap *keymap, int paint_mode,
391                                          int tool, int keymap_type,
392                                          int keymap_modifier)
393 {
394         wmKeyMapItem *kmi;
395         kmi = WM_keymap_add_item(keymap, "PAINT_OT_brush_select",
396                                  keymap_type, KM_PRESS, keymap_modifier, 0);
397
398         RNA_enum_set(kmi->ptr, "paint_mode", paint_mode);
399         
400         switch (paint_mode) {
401                 case OB_MODE_SCULPT:
402                         RNA_enum_set(kmi->ptr, "sculpt_tool", tool);
403                         break;
404                 case OB_MODE_VERTEX_PAINT:
405                         RNA_enum_set(kmi->ptr, "vertex_paint_tool", tool);
406                         break;
407                 case OB_MODE_WEIGHT_PAINT:
408                         RNA_enum_set(kmi->ptr, "weight_paint_tool", tool);
409                         break;
410                 case OB_MODE_TEXTURE_PAINT:
411                         RNA_enum_set(kmi->ptr, "texture_paint_tool", tool);
412                         break;
413         }
414
415         return kmi;
416 }
417
418 static int brush_uv_sculpt_tool_set_exec(bContext *C, wmOperator *op)
419 {
420         Brush *brush;
421         Scene *scene = CTX_data_scene(C);
422         ToolSettings *ts = scene->toolsettings;
423         ts->uv_sculpt_tool = RNA_enum_get(op->ptr, "tool");
424         brush = ts->uvsculpt->paint.brush;
425         /* To update toolshelf */
426         WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
427
428         return OPERATOR_FINISHED;
429 }
430
431 static void BRUSH_OT_uv_sculpt_tool_set(wmOperatorType *ot)
432 {
433         /* from rna_scene.c */
434         extern EnumPropertyItem uv_sculpt_tool_items[];
435         /* identifiers */
436         ot->name = "UV Sculpt Tool Set";
437         ot->description = "Set the UV sculpt tool";
438         ot->idname = "BRUSH_OT_uv_sculpt_tool_set";
439
440         /* api callbacks */
441         ot->exec = brush_uv_sculpt_tool_set_exec;
442         ot->poll = uv_sculpt_poll;
443
444         /* flags */
445         ot->flag = 0;
446
447         /* props */
448         ot->prop = RNA_def_enum(ot->srna, "tool", uv_sculpt_tool_items, 0, "Tool", "");
449 }
450
451 /**************************** registration **********************************/
452
453 void ED_operatortypes_paint(void)
454 {
455         /* brush */
456         WM_operatortype_append(BRUSH_OT_add);
457         WM_operatortype_append(BRUSH_OT_scale_size);
458         WM_operatortype_append(BRUSH_OT_curve_preset);
459         WM_operatortype_append(BRUSH_OT_reset);
460
461         /* note, particle uses a different system, can be added with existing operators in wm.py */
462         WM_operatortype_append(PAINT_OT_brush_select);
463         WM_operatortype_append(BRUSH_OT_uv_sculpt_tool_set);
464
465         /* image */
466         WM_operatortype_append(PAINT_OT_texture_paint_toggle);
467         WM_operatortype_append(PAINT_OT_image_paint);
468         WM_operatortype_append(PAINT_OT_sample_color);
469         WM_operatortype_append(PAINT_OT_grab_clone);
470         WM_operatortype_append(PAINT_OT_clone_cursor_set);
471         WM_operatortype_append(PAINT_OT_project_image);
472         WM_operatortype_append(PAINT_OT_image_from_view);
473
474         /* weight */
475         WM_operatortype_append(PAINT_OT_weight_paint_toggle);
476         WM_operatortype_append(PAINT_OT_weight_paint);
477         WM_operatortype_append(PAINT_OT_weight_set);
478         WM_operatortype_append(PAINT_OT_weight_from_bones);
479         WM_operatortype_append(PAINT_OT_weight_gradient);
480         WM_operatortype_append(PAINT_OT_weight_sample);
481         WM_operatortype_append(PAINT_OT_weight_sample_group);
482
483         /* uv */
484         WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
485
486         /* vertex selection */
487         WM_operatortype_append(PAINT_OT_vert_select_all);
488         WM_operatortype_append(PAINT_OT_vert_select_inverse);
489
490         /* vertex */
491         WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
492         WM_operatortype_append(PAINT_OT_vertex_paint);
493         WM_operatortype_append(PAINT_OT_vertex_color_set);
494
495         /* face-select */
496         WM_operatortype_append(PAINT_OT_face_select_linked);
497         WM_operatortype_append(PAINT_OT_face_select_linked_pick);
498         WM_operatortype_append(PAINT_OT_face_select_all);
499         WM_operatortype_append(PAINT_OT_face_select_inverse);
500         WM_operatortype_append(PAINT_OT_face_select_hide);
501         WM_operatortype_append(PAINT_OT_face_select_reveal);
502
503         /* partial visibility */
504         WM_operatortype_append(PAINT_OT_hide_show);
505
506         /* paint masking */
507         WM_operatortype_append(PAINT_OT_mask_flood_fill);
508 }
509
510
511 static void ed_keymap_paint_brush_switch(wmKeyMap *keymap, const char *mode)
512 {
513         wmKeyMapItem *kmi;
514         int i;
515         /* index 0-9 (zero key is tenth), shift key for index 10-19 */
516         for (i = 0; i < 20; i++) {
517                 kmi = WM_keymap_add_item(keymap, "BRUSH_OT_active_index_set",
518                                          ZEROKEY + ((i + 1) % 10), KM_PRESS, i < 10 ? 0 : KM_SHIFT, 0);
519                 RNA_string_set(kmi->ptr, "mode", mode);
520                 RNA_int_set(kmi->ptr, "index", i);
521         }
522 }
523
524 static void ed_keymap_paint_brush_size(wmKeyMap *keymap, const char *UNUSED(path))
525 {
526         wmKeyMapItem *kmi;
527
528         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", LEFTBRACKETKEY, KM_PRESS, 0, 0);
529         RNA_float_set(kmi->ptr, "scalar", 0.9);
530
531         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
532         RNA_float_set(kmi->ptr, "scalar", 10.0 / 9.0); // 1.1111....
533 }
534
535 typedef enum {
536         RC_COLOR = 1,
537         RC_ROTATION = 2,
538         RC_ZOOM = 4,
539 } RCFlags;
540
541 static void set_brush_rc_path(PointerRNA *ptr, const char *brush_path,
542                               const char *output_name, const char *input_name)
543 {
544         char *path;
545
546         path = BLI_sprintfN("%s.%s", brush_path, input_name);
547         RNA_string_set(ptr, output_name, path);
548         MEM_freeN(path);
549 }
550
551 static void set_brush_rc_props(PointerRNA *ptr, const char *paint,
552                                const char *prop, const char *secondary_prop,
553                                RCFlags flags)
554 {
555         const char *ups_path = "tool_settings.unified_paint_settings";
556         char *brush_path;
557
558         brush_path = BLI_sprintfN("tool_settings.%s.brush", paint);
559
560         set_brush_rc_path(ptr, brush_path, "data_path_primary", prop);
561         if (secondary_prop) {
562                 set_brush_rc_path(ptr, ups_path, "use_secondary", secondary_prop);
563                 set_brush_rc_path(ptr, ups_path, "data_path_secondary", prop);
564         }
565         else {
566                 RNA_string_set(ptr, "use_secondary", "");
567                 RNA_string_set(ptr, "data_path_secondary", "");
568         }
569         set_brush_rc_path(ptr, brush_path, "color_path", "cursor_color_add");
570         set_brush_rc_path(ptr, brush_path, "rotation_path", "texture_slot.angle");
571         RNA_string_set(ptr, "image_id", brush_path);
572
573         if (flags & RC_COLOR)
574                 set_brush_rc_path(ptr, brush_path, "fill_color_path", "color");
575         else
576                 RNA_string_set(ptr, "fill_color_path", "");
577         if (flags & RC_ZOOM)
578                 RNA_string_set(ptr, "zoom_path", "space_data.zoom");
579         else
580                 RNA_string_set(ptr, "zoom_path", "");
581
582         MEM_freeN(brush_path);
583 }
584
585 static void ed_keymap_paint_brush_radial_control(wmKeyMap *keymap, const char *paint,
586                                                  RCFlags flags)
587 {
588         wmKeyMapItem *kmi;
589         /* only size needs to follow zoom, strength shows fixed size circle */
590         int flags_nozoom = flags & (~RC_ZOOM);
591
592         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, 0, 0);
593         set_brush_rc_props(kmi->ptr, paint, "size", "use_unified_size", flags);
594
595         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0);
596         set_brush_rc_props(kmi->ptr, paint, "strength", "use_unified_strength", flags_nozoom);
597
598         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", WKEY, KM_PRESS, 0, 0);
599         set_brush_rc_props(kmi->ptr, paint, "weight", "use_unified_weight", flags_nozoom);
600
601         if (flags & RC_ROTATION) {
602                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0);
603                 set_brush_rc_props(kmi->ptr, paint, "texture_slot.angle", NULL, flags_nozoom);
604         }
605 }
606
607 static void paint_partial_visibility_keys(wmKeyMap *keymap)
608 {
609         wmKeyMapItem *kmi;
610         
611         /* Partial visibility */
612         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_SHIFT, 0);
613         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
614         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
615         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, 0, 0);
616         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_HIDE);
617         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
618         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_ALT, 0);
619         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
620         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_ALL);
621 }
622
623 void ED_keymap_paint(wmKeyConfig *keyconf)
624 {
625         wmKeyMap *keymap;
626         wmKeyMapItem *kmi;
627         int i;
628         
629         /* Sculpt mode */
630         keymap = WM_keymap_find(keyconf, "Sculpt", 0, 0);
631         keymap->poll = sculpt_mode_poll;
632
633         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
634         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
635         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
636
637         /* Partial visibility, sculpt-only for now */
638         paint_partial_visibility_keys(keymap);
639
640         for (i = 0; i <= 5; i++)
641                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY + i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
642
643         /* Clear mask */
644         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", MKEY, KM_PRESS, KM_ALT, 0);
645         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_FLOOD_VALUE);
646         RNA_float_set(kmi->ptr, "value", 0);
647
648         /* Invert mask */
649         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", IKEY, KM_PRESS, KM_CTRL, 0);
650         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_INVERT);
651
652         /* Toggle dynamic topology */
653         WM_keymap_add_item(keymap, "SCULPT_OT_dynamic_topology_toggle", DKEY, KM_PRESS, KM_CTRL, 0);
654
655         /* Dynamic-topology detail size
656          * 
657          * This should be improved further, perhaps by showing a triangle
658          * grid rather than brush alpha */
659         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", DKEY, KM_PRESS, KM_SHIFT, 0);
660         set_brush_rc_props(kmi->ptr, "sculpt", "detail_size", NULL, 0);
661         RNA_string_set(kmi->ptr, "data_path_primary", "tool_settings.sculpt.detail_size");
662
663         /* multires switch */
664         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
665         RNA_int_set(kmi->ptr, "level", 1);
666         RNA_boolean_set(kmi->ptr, "relative", TRUE);
667
668         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
669         RNA_int_set(kmi->ptr, "level", -1);
670         RNA_boolean_set(kmi->ptr, "relative", TRUE);
671
672         ed_keymap_paint_brush_switch(keymap, "sculpt");
673         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
674         ed_keymap_paint_brush_radial_control(keymap, "sculpt", RC_ROTATION);
675
676         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_DRAW, DKEY, 0);
677         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SMOOTH, SKEY, 0);
678         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_PINCH, PKEY, 0);
679         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_INFLATE, IKEY, 0);
680         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_GRAB, GKEY, 0);
681         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_LAYER, LKEY, 0);
682         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_FLATTEN, TKEY, KM_SHIFT);
683         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CLAY, CKEY, 0);
684         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CREASE, CKEY, KM_SHIFT);
685         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SNAKE_HOOK, KKEY, 0);
686         kmi = keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_MASK, MKEY, 0);
687         RNA_boolean_set(kmi->ptr, "toggle", 1);
688         RNA_boolean_set(kmi->ptr, "create_missing", 1);
689
690         /* */
691         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", AKEY, KM_PRESS, 0, 0);
692         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.stroke_method");
693
694         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
695         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
696
697         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", RKEY, KM_PRESS, 0, 0);
698         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.texture_angle_source_random");
699
700         /* Vertex Paint mode */
701         keymap = WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
702         keymap->poll = vertex_paint_mode_poll;
703
704         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
705         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", RIGHTMOUSE, KM_PRESS, 0, 0);
706
707         WM_keymap_add_item(keymap,
708                            "PAINT_OT_vertex_color_set", KKEY, KM_PRESS, KM_SHIFT, 0);
709
710         ed_keymap_paint_brush_switch(keymap, "vertex_paint");
711         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
712         ed_keymap_paint_brush_radial_control(keymap, "vertex_paint", RC_COLOR);
713
714         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
715         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
716
717         /* Weight Paint mode */
718         keymap = WM_keymap_find(keyconf, "Weight Paint", 0, 0);
719         keymap->poll = weight_paint_mode_poll;
720
721         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
722
723         /* these keys are from 2.4x but could be changed */
724         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
725         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample_group", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
726
727         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_gradient", LEFTMOUSE, KM_PRESS, KM_ALT, 0)->ptr,           "type", WPAINT_GRADIENT_TYPE_LINEAR);
728         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);
729
730         WM_keymap_add_item(keymap,
731                            "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
732
733         ed_keymap_paint_brush_switch(keymap, "weight_paint");
734         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
735         ed_keymap_paint_brush_radial_control(keymap, "weight_paint", 0);
736
737         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* face mask toggle */
738         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
739
740         /* note, conflicts with vertex paint, but this is more useful */
741         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", VKEY, KM_PRESS, 0, 0); /* vert mask toggle */
742         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask_vertex");
743
744         WM_keymap_verify_item(keymap, "PAINT_OT_weight_from_bones", WKEY, KM_PRESS, 0, 0);
745
746         
747         /*Weight paint's Vertex Selection Mode */
748         keymap = WM_keymap_find(keyconf, "Weight Paint Vertex Selection", 0, 0);
749         keymap->poll = vert_paint_poll;
750         WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", AKEY, KM_PRESS, 0, 0);
751         WM_keymap_add_item(keymap, "PAINT_OT_vert_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
752         WM_keymap_add_item(keymap, "VIEW3D_OT_select_border", BKEY, KM_PRESS, 0, 0);
753         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
754         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
755         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0);
756         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
757         WM_keymap_add_item(keymap, "VIEW3D_OT_select_circle", CKEY, KM_PRESS, 0, 0);
758
759         /* Image/Texture Paint mode */
760         keymap = WM_keymap_find(keyconf, "Image Paint", 0, 0);
761         keymap->poll = image_texture_paint_poll;
762
763         WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0, 0);
764         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
765         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", RIGHTMOUSE, KM_PRESS, 0, 0);
766         WM_keymap_add_item(keymap, "PAINT_OT_clone_cursor_set", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
767
768         ed_keymap_paint_brush_switch(keymap, "image_paint");
769         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
770         ed_keymap_paint_brush_radial_control(keymap, "image_paint", RC_COLOR | RC_ZOOM);
771
772         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
773         RNA_string_set(kmi->ptr, "data_path", "image_paint_object.data.use_paint_mask");
774
775         /* face-mask mode */
776         keymap = WM_keymap_find(keyconf, "Face Mask", 0, 0);
777         keymap->poll = facemask_paint_poll;
778
779         WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
780         WM_keymap_add_item(keymap, "PAINT_OT_face_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
781         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, 0, 0);
782         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
783         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
784         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
785         WM_keymap_add_item(keymap, "PAINT_OT_face_select_reveal", HKEY, KM_PRESS, KM_ALT, 0);
786
787         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
788         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
789
790         keymap = WM_keymap_find(keyconf, "UV Sculpt", 0, 0);
791         keymap->poll = uv_sculpt_poll;
792
793         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", QKEY, KM_PRESS, 0, 0);
794         RNA_string_set(kmi->ptr, "data_path", "tool_settings.use_uv_sculpt");
795
796         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
797         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
798         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
799
800         ed_keymap_paint_brush_size(keymap, "tool_settings.uv_sculpt.brush.size");
801         ed_keymap_paint_brush_radial_control(keymap, "uv_sculpt", 0);
802
803         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);
804         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);
805         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);
806
807         /* paint stroke */
808         keymap = paint_stroke_modal_keymap(keyconf);
809         WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
810 }