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