Merge branch 'master' into blender2.8
[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 int 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, ePaintTextureProjective, ePaintTexture2D, ePaintVertex)) {
209                 copy_v3_v3(color->rgb, BKE_brush_color_get(scene, brush));
210                 color->value = 0.0;
211         }
212         else if (mode == ePaintWeight) {
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(Main *bmain, Paint *paint, const int tool,
369                                   const size_t tool_offset, const int ob_mode,
370                                   const char *tool_name, const bool create_missing,
371                                   const bool toggle)
372 {
373         Brush *brush, *brush_orig = BKE_paint_brush(paint);
374
375         if (toggle)
376                 brush = brush_tool_toggle(bmain, brush_orig, tool, tool_offset, ob_mode);
377         else
378                 brush = brush_tool_cycle(bmain, brush_orig, tool, tool_offset, ob_mode);
379
380         if (!brush && brush_tool(brush_orig, tool_offset) != tool && create_missing) {
381                 brush = BKE_brush_add(bmain, tool_name, ob_mode);
382                 id_us_min(&brush->id);  /* fake user only */
383                 brush_tool_set(brush, tool_offset, tool);
384                 brush->toggle_brush = brush_orig;
385         }
386
387         if (brush) {
388                 BKE_paint_brush_set(paint, brush);
389                 BKE_paint_invalidate_overlay_all();
390
391                 WM_main_add_notifier(NC_BRUSH | NA_EDITED, brush);
392                 return OPERATOR_FINISHED;
393         }
394         else {
395                 return OPERATOR_CANCELLED;
396         }
397 }
398
399 /* used in the PAINT_OT_brush_select operator */
400 #define OB_MODE_ACTIVE 0
401
402 static int brush_select_exec(bContext *C, wmOperator *op)
403 {
404         Main *bmain = CTX_data_main(C);
405         ToolSettings *toolsettings = CTX_data_tool_settings(C);
406         Paint *paint = NULL;
407         int tool, paint_mode = RNA_enum_get(op->ptr, "paint_mode");
408         const bool create_missing = RNA_boolean_get(op->ptr, "create_missing");
409         const bool toggle = RNA_boolean_get(op->ptr, "toggle");
410         const char *tool_name = "Brush";
411         size_t tool_offset;
412
413         if (paint_mode == OB_MODE_ACTIVE) {
414                 Object *ob = CTX_data_active_object(C);
415                 if (ob) {
416                         /* select current paint mode */
417                         paint_mode = ob->mode & OB_MODE_ALL_PAINT;
418                 }
419                 else {
420                         return OPERATOR_CANCELLED;
421                 }
422         }
423
424         switch (paint_mode) {
425                 case OB_MODE_SCULPT:
426                         paint = &toolsettings->sculpt->paint;
427                         tool_offset = offsetof(Brush, sculpt_tool);
428                         tool = RNA_enum_get(op->ptr, "sculpt_tool");
429                         RNA_enum_name_from_value(rna_enum_brush_sculpt_tool_items, tool, &tool_name);
430                         break;
431                 case OB_MODE_VERTEX_PAINT:
432                         paint = &toolsettings->vpaint->paint;
433                         tool_offset = offsetof(Brush, vertexpaint_tool);
434                         tool = RNA_enum_get(op->ptr, "vertex_paint_tool");
435                         RNA_enum_name_from_value(rna_enum_brush_vertex_tool_items, tool, &tool_name);
436                         break;
437                 case OB_MODE_WEIGHT_PAINT:
438                         paint = &toolsettings->wpaint->paint;
439                         /* vertexpaint_tool is used for weight paint mode */
440                         tool_offset = offsetof(Brush, vertexpaint_tool);
441                         tool = RNA_enum_get(op->ptr, "weight_paint_tool");
442                         RNA_enum_name_from_value(rna_enum_brush_vertex_tool_items, tool, &tool_name);
443                         break;
444                 case OB_MODE_TEXTURE_PAINT:
445                         paint = &toolsettings->imapaint.paint;
446                         tool_offset = offsetof(Brush, imagepaint_tool);
447                         tool = RNA_enum_get(op->ptr, "texture_paint_tool");
448                         RNA_enum_name_from_value(rna_enum_brush_image_tool_items, tool, &tool_name);
449                         break;
450                 default:
451                         /* invalid paint mode */
452                         return OPERATOR_CANCELLED;
453         }
454
455         return brush_generic_tool_set(bmain, paint, tool, tool_offset,
456                                       paint_mode, tool_name, create_missing,
457                                       toggle);
458 }
459
460 static void PAINT_OT_brush_select(wmOperatorType *ot)
461 {
462         static const EnumPropertyItem paint_mode_items[] = {
463                 {OB_MODE_ACTIVE, "ACTIVE", 0, "Current", "Set brush for active paint mode"},
464                 {OB_MODE_SCULPT, "SCULPT", ICON_SCULPTMODE_HLT, "Sculpt", ""},
465                 {OB_MODE_VERTEX_PAINT, "VERTEX_PAINT", ICON_VPAINT_HLT, "Vertex Paint", ""},
466                 {OB_MODE_WEIGHT_PAINT, "WEIGHT_PAINT", ICON_WPAINT_HLT, "Weight Paint", ""},
467                 {OB_MODE_TEXTURE_PAINT, "TEXTURE_PAINT", ICON_TPAINT_HLT, "Texture Paint", ""},
468                 {0, NULL, 0, NULL, NULL}
469         };
470         PropertyRNA *prop;
471
472         /* identifiers */
473         ot->name = "Brush Select";
474         ot->description = "Select a paint mode's brush by tool type";
475         ot->idname = "PAINT_OT_brush_select";
476
477         /* api callbacks */
478         ot->exec = brush_select_exec;
479
480         /* flags */
481         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
482
483         /* props */
484         RNA_def_enum(ot->srna, "paint_mode", paint_mode_items, OB_MODE_ACTIVE, "Paint Mode", "");
485         RNA_def_enum(ot->srna, "sculpt_tool", rna_enum_brush_sculpt_tool_items, 0, "Sculpt Tool", "");
486         RNA_def_enum(ot->srna, "vertex_paint_tool", rna_enum_brush_vertex_tool_items, 0, "Vertex Paint Tool", "");
487         RNA_def_enum(ot->srna, "weight_paint_tool", rna_enum_brush_vertex_tool_items, 0, "Weight Paint Tool", "");
488         RNA_def_enum(ot->srna, "texture_paint_tool", rna_enum_brush_image_tool_items, 0, "Texture Paint Tool", "");
489
490         prop = RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "Toggle between two brushes rather than cycling");
491         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
492         prop = RNA_def_boolean(ot->srna, "create_missing", 0, "Create Missing", "If the requested brush type does not exist, create a new brush");
493         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
494 }
495
496 static wmKeyMapItem *keymap_brush_select(wmKeyMap *keymap, int paint_mode,
497                                          int tool, int keymap_type,
498                                          int keymap_modifier)
499 {
500         wmKeyMapItem *kmi;
501         kmi = WM_keymap_add_item(keymap, "PAINT_OT_brush_select",
502                                  keymap_type, KM_PRESS, keymap_modifier, 0);
503
504         RNA_enum_set(kmi->ptr, "paint_mode", paint_mode);
505         
506         switch (paint_mode) {
507                 case OB_MODE_SCULPT:
508                         RNA_enum_set(kmi->ptr, "sculpt_tool", tool);
509                         break;
510                 case OB_MODE_VERTEX_PAINT:
511                         RNA_enum_set(kmi->ptr, "vertex_paint_tool", tool);
512                         break;
513                 case OB_MODE_WEIGHT_PAINT:
514                         RNA_enum_set(kmi->ptr, "weight_paint_tool", tool);
515                         break;
516                 case OB_MODE_TEXTURE_PAINT:
517                         RNA_enum_set(kmi->ptr, "texture_paint_tool", tool);
518                         break;
519         }
520
521         return kmi;
522 }
523
524 static int brush_uv_sculpt_tool_set_exec(bContext *C, wmOperator *op)
525 {
526         Brush *brush;
527         Scene *scene = CTX_data_scene(C);
528         ToolSettings *ts = scene->toolsettings;
529         ts->uv_sculpt_tool = RNA_enum_get(op->ptr, "tool");
530         brush = ts->uvsculpt->paint.brush;
531         /* To update toolshelf */
532         WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush);
533
534         return OPERATOR_FINISHED;
535 }
536
537 static void BRUSH_OT_uv_sculpt_tool_set(wmOperatorType *ot)
538 {
539         /* identifiers */
540         ot->name = "UV Sculpt Tool Set";
541         ot->description = "Set the UV sculpt tool";
542         ot->idname = "BRUSH_OT_uv_sculpt_tool_set";
543
544         /* api callbacks */
545         ot->exec = brush_uv_sculpt_tool_set_exec;
546         ot->poll = uv_sculpt_poll;
547
548         /* flags */
549         ot->flag = 0;
550
551         /* props */
552         ot->prop = RNA_def_enum(ot->srna, "tool", rna_enum_uv_sculpt_tool_items, 0, "Tool", "");
553 }
554
555 /***** Stencil Control *****/
556
557 typedef enum {
558         STENCIL_TRANSLATE,
559         STENCIL_SCALE,
560         STENCIL_ROTATE
561 } StencilControlMode;
562
563 typedef enum {
564         STENCIL_PRIMARY = 0,
565         STENCIL_SECONDARY = 1
566 } StencilTextureMode;
567
568
569 typedef enum {
570         STENCIL_CONSTRAINT_X = 1,
571         STENCIL_CONSTRAINT_Y = 2
572 } StencilConstraint;
573
574 typedef struct {
575         float init_mouse[2];
576         float init_spos[2];
577         float init_sdim[2];
578         float init_rot;
579         float init_angle;
580         float lenorig;
581         float area_size[2];
582         StencilControlMode mode;
583         StencilConstraint constrain_mode;
584         int mask; /* we are twaking mask or colour stencil */
585         Brush *br;
586         float *dim_target;
587         float *rot_target;
588         float *pos_target;
589         short event_type;
590 } StencilControlData;
591
592 static void stencil_set_target(StencilControlData *scd)
593 {
594         Brush *br = scd->br;
595         float mdiff[2];
596         if (scd->mask) {
597                 copy_v2_v2(scd->init_sdim, br->mask_stencil_dimension);
598                 copy_v2_v2(scd->init_spos, br->mask_stencil_pos);
599                 scd->init_rot = br->mask_mtex.rot;
600
601                 scd->dim_target = br->mask_stencil_dimension;
602                 scd->rot_target = &br->mask_mtex.rot;
603                 scd->pos_target = br->mask_stencil_pos;
604
605                 sub_v2_v2v2(mdiff, scd->init_mouse, br->mask_stencil_pos);
606         }
607         else {
608                 copy_v2_v2(scd->init_sdim, br->stencil_dimension);
609                 copy_v2_v2(scd->init_spos, br->stencil_pos);
610                 scd->init_rot = br->mtex.rot;
611
612                 scd->dim_target = br->stencil_dimension;
613                 scd->rot_target = &br->mtex.rot;
614                 scd->pos_target = br->stencil_pos;
615
616                 sub_v2_v2v2(mdiff, scd->init_mouse, br->stencil_pos);
617         }
618
619         scd->lenorig = len_v2(mdiff);
620
621         scd->init_angle = atan2f(mdiff[1], mdiff[0]);
622 }
623
624 static int stencil_control_invoke(bContext *C, wmOperator *op, const wmEvent *event)
625 {
626         Paint *paint = BKE_paint_get_active_from_context(C);
627         Brush *br = BKE_paint_brush(paint);
628         float mvalf[2] = {event->mval[0], event->mval[1]};
629         ARegion *ar = CTX_wm_region(C);
630         StencilControlData *scd;
631         int mask = RNA_enum_get(op->ptr, "texmode");
632
633         if (mask) {
634                 if (br->mask_mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL)
635                         return OPERATOR_CANCELLED;
636         }
637         else {
638                 if (br->mtex.brush_map_mode != MTEX_MAP_MODE_STENCIL)
639                         return OPERATOR_CANCELLED;
640         }
641
642         scd = MEM_mallocN(sizeof(StencilControlData), "stencil_control");
643         scd->mask = mask;
644         scd->br = br;
645
646         copy_v2_v2(scd->init_mouse, mvalf);
647
648         stencil_set_target(scd);
649
650         scd->mode = RNA_enum_get(op->ptr, "mode");
651         scd->event_type = event->type;
652         scd->area_size[0] = ar->winx;
653         scd->area_size[1] = ar->winy;
654
655
656         op->customdata = scd;
657         WM_event_add_modal_handler(C, op);
658
659         return OPERATOR_RUNNING_MODAL;
660 }
661
662 static void stencil_restore(StencilControlData *scd)
663 {
664         copy_v2_v2(scd->dim_target, scd->init_sdim);
665         copy_v2_v2(scd->pos_target, scd->init_spos);
666         *scd->rot_target = scd->init_rot;
667 }
668
669 static void stencil_control_cancel(bContext *UNUSED(C), wmOperator *op)
670 {
671         StencilControlData *scd = op->customdata;
672
673         stencil_restore(scd);
674         MEM_freeN(op->customdata);
675 }
676
677 static void stencil_control_calculate(StencilControlData *scd, const int mval[2])
678 {
679 #define PIXEL_MARGIN 5
680
681         float mdiff[2];
682         float mvalf[2] = {mval[0], mval[1]};
683         switch (scd->mode) {
684                 case STENCIL_TRANSLATE:
685                         sub_v2_v2v2(mdiff, mvalf, scd->init_mouse);
686                         add_v2_v2v2(scd->pos_target, scd->init_spos,
687                                     mdiff);
688                         CLAMP(scd->pos_target[0],
689                               -scd->dim_target[0] + PIXEL_MARGIN,
690                               scd->area_size[0] + scd->dim_target[0] - PIXEL_MARGIN);
691
692                         CLAMP(scd->pos_target[1],
693                               -scd->dim_target[1] + PIXEL_MARGIN,
694                               scd->area_size[1] + scd->dim_target[1] - PIXEL_MARGIN);
695
696                         break;
697                 case STENCIL_SCALE:
698                 {
699                         float len, factor;
700                         sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
701                         len = len_v2(mdiff);
702                         factor = len / scd->lenorig;
703                         copy_v2_v2(mdiff, scd->init_sdim);
704                         if (scd->constrain_mode != STENCIL_CONSTRAINT_Y)
705                                 mdiff[0] = factor * scd->init_sdim[0];
706                         if (scd->constrain_mode != STENCIL_CONSTRAINT_X)
707                                 mdiff[1] = factor * scd->init_sdim[1];
708                         CLAMP(mdiff[0], 5.0f, 10000.0f);
709                         CLAMP(mdiff[1], 5.0f, 10000.0f);
710                         copy_v2_v2(scd->dim_target, mdiff);
711                         break;
712                 }
713                 case STENCIL_ROTATE:
714                 {
715                         float angle;
716                         sub_v2_v2v2(mdiff, mvalf, scd->pos_target);
717                         angle = atan2f(mdiff[1], mdiff[0]);
718                         angle = scd->init_rot + angle - scd->init_angle;
719                         if (angle < 0.0f)
720                                 angle += (float)(2 * M_PI);
721                         if (angle > (float)(2 * M_PI))
722                                 angle -= (float)(2 * M_PI);
723                         *scd->rot_target = angle;
724                         break;
725                 }
726         }
727 #undef PIXEL_MARGIN
728 }
729
730 static int stencil_control_modal(bContext *C, wmOperator *op, const wmEvent *event)
731 {
732         StencilControlData *scd = op->customdata;
733
734         if (event->type == scd->event_type && event->val == KM_RELEASE) {
735                 MEM_freeN(op->customdata);
736                 WM_event_add_notifier(C, NC_WINDOW, NULL);
737                 return OPERATOR_FINISHED;
738         }
739
740         switch (event->type) {
741                 case MOUSEMOVE:
742                         stencil_control_calculate(scd, event->mval);
743                         break;
744                 case ESCKEY:
745                         if (event->val == KM_PRESS) {
746                                 stencil_control_cancel(C, op);
747                                 WM_event_add_notifier(C, NC_WINDOW, NULL);
748                                 return OPERATOR_CANCELLED;
749                         }
750                         break;
751                 case XKEY:
752                         if (event->val == KM_PRESS) {
753
754                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_X)
755                                         scd->constrain_mode = 0;
756                                 else
757                                         scd->constrain_mode = STENCIL_CONSTRAINT_X;
758
759                                 stencil_control_calculate(scd, event->mval);
760                         }
761                         break;
762                 case YKEY:
763                         if (event->val == KM_PRESS) {
764                                 if (scd->constrain_mode == STENCIL_CONSTRAINT_Y)
765                                         scd->constrain_mode = 0;
766                                 else
767                                         scd->constrain_mode = STENCIL_CONSTRAINT_Y;
768
769                                 stencil_control_calculate(scd, event->mval);
770                         }
771                         break;
772                 default:
773                         break;
774         }
775
776         ED_region_tag_redraw(CTX_wm_region(C));
777
778         return OPERATOR_RUNNING_MODAL;
779 }
780
781 static int stencil_control_poll(bContext *C)
782 {
783         ePaintMode mode = BKE_paintmode_get_active_from_context(C);
784
785         Paint *paint;
786         Brush *br;
787
788         if (!paint_supports_texture(mode))
789                 return false;
790
791         paint = BKE_paint_get_active_from_context(C);
792         br = BKE_paint_brush(paint);
793         return (br &&
794                 (br->mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL ||
795                  br->mask_mtex.brush_map_mode == MTEX_MAP_MODE_STENCIL));
796 }
797
798 static void BRUSH_OT_stencil_control(wmOperatorType *ot)
799 {
800         static const EnumPropertyItem stencil_control_items[] = {
801                 {STENCIL_TRANSLATE, "TRANSLATION", 0, "Translation", ""},
802                 {STENCIL_SCALE, "SCALE", 0, "Scale", ""},
803                 {STENCIL_ROTATE, "ROTATION", 0, "Rotation", ""},
804                 {0, NULL, 0, NULL, NULL}
805         };
806
807         static const EnumPropertyItem stencil_texture_items[] = {
808                 {STENCIL_PRIMARY, "PRIMARY", 0, "Primary", ""},
809                 {STENCIL_SECONDARY, "SECONDARY", 0, "Secondary", ""},
810                 {0, NULL, 0, NULL, NULL}
811         };
812         /* identifiers */
813         ot->name = "Stencil Brush Control";
814         ot->description = "Control the stencil brush";
815         ot->idname = "BRUSH_OT_stencil_control";
816
817         /* api callbacks */
818         ot->invoke = stencil_control_invoke;
819         ot->modal = stencil_control_modal;
820         ot->cancel = stencil_control_cancel;
821         ot->poll = stencil_control_poll;
822
823         /* flags */
824         ot->flag = 0;
825
826         RNA_def_enum(ot->srna, "mode", stencil_control_items, STENCIL_TRANSLATE, "Tool", "");
827         RNA_def_enum(ot->srna, "texmode", stencil_texture_items, STENCIL_PRIMARY, "Tool", "");
828 }
829
830
831 static int stencil_fit_image_aspect_exec(bContext *C, wmOperator *op)
832 {
833         Paint *paint = BKE_paint_get_active_from_context(C);
834         Brush *br = BKE_paint_brush(paint);
835         bool use_scale = RNA_boolean_get(op->ptr, "use_scale");
836         bool use_repeat = RNA_boolean_get(op->ptr, "use_repeat");
837         bool do_mask = RNA_boolean_get(op->ptr, "mask");
838         Tex *tex = NULL;
839         MTex *mtex = NULL;
840         if (br) {
841                 mtex = do_mask ? &br->mask_mtex : &br->mtex;
842                 tex = mtex->tex;
843         }
844
845         if (tex && tex->type == TEX_IMAGE && tex->ima) {
846                 float aspx, aspy;
847                 Image *ima = tex->ima;
848                 float orig_area, stencil_area, factor;
849                 ED_image_get_uv_aspect(ima, NULL, &aspx, &aspy);
850
851                 if (use_scale) {
852                         aspx *= mtex->size[0];
853                         aspy *= mtex->size[1];
854                 }
855
856                 if (use_repeat && tex->extend == TEX_REPEAT) {
857                         aspx *= tex->xrepeat;
858                         aspy *= tex->yrepeat;
859                 }
860
861                 orig_area = aspx * aspy;
862
863                 if (do_mask) {
864                         stencil_area = br->mask_stencil_dimension[0] * br->mask_stencil_dimension[1];
865                 }
866                 else {
867                         stencil_area = br->stencil_dimension[0] * br->stencil_dimension[1];
868                 }
869
870                 factor = sqrtf(stencil_area / orig_area);
871
872                 if (do_mask) {
873                         br->mask_stencil_dimension[0] = factor * aspx;
874                         br->mask_stencil_dimension[1] = factor * aspy;
875
876                 }
877                 else {
878                         br->stencil_dimension[0] = factor * aspx;
879                         br->stencil_dimension[1] = factor * aspy;
880                 }
881         }
882
883         WM_event_add_notifier(C, NC_WINDOW, NULL);
884
885         return OPERATOR_FINISHED;
886 }
887
888
889 static void BRUSH_OT_stencil_fit_image_aspect(wmOperatorType *ot)
890 {
891         /* identifiers */
892         ot->name = "Image Aspect";
893         ot->description = "When using an image texture, adjust the stencil size to fit the image aspect ratio";
894         ot->idname = "BRUSH_OT_stencil_fit_image_aspect";
895
896         /* api callbacks */
897         ot->exec = stencil_fit_image_aspect_exec;
898         ot->poll = stencil_control_poll;
899
900         /* flags */
901         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
902
903         RNA_def_boolean(ot->srna, "use_repeat", 1, "Use Repeat", "Use repeat mapping values");
904         RNA_def_boolean(ot->srna, "use_scale", 1, "Use Scale", "Use texture scale values");
905         RNA_def_boolean(ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
906 }
907
908
909 static int stencil_reset_transform_exec(bContext *C, wmOperator *op)
910 {
911         Paint *paint = BKE_paint_get_active_from_context(C);
912         Brush *br = BKE_paint_brush(paint);
913         bool do_mask = RNA_boolean_get(op->ptr, "mask");
914
915         if (!br)
916                 return OPERATOR_CANCELLED;
917         
918         if (do_mask) {
919                 br->mask_stencil_pos[0] = 256;
920                 br->mask_stencil_pos[1] = 256;
921
922                 br->mask_stencil_dimension[0] = 256;
923                 br->mask_stencil_dimension[1] = 256;
924
925                 br->mask_mtex.rot = 0;
926         }
927         else {
928                 br->stencil_pos[0] = 256;
929                 br->stencil_pos[1] = 256;
930
931                 br->stencil_dimension[0] = 256;
932                 br->stencil_dimension[1] = 256;
933
934                 br->mtex.rot = 0;
935         }
936
937         WM_event_add_notifier(C, NC_WINDOW, NULL);
938
939         return OPERATOR_FINISHED;
940 }
941
942
943 static void BRUSH_OT_stencil_reset_transform(wmOperatorType *ot)
944 {
945         /* identifiers */
946         ot->name = "Reset Transform";
947         ot->description = "Reset the stencil transformation to the default";
948         ot->idname = "BRUSH_OT_stencil_reset_transform";
949
950         /* api callbacks */
951         ot->exec = stencil_reset_transform_exec;
952         ot->poll = stencil_control_poll;
953
954         /* flags */
955         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
956
957         RNA_def_boolean(ot->srna, "mask", 0, "Modify Mask Stencil", "Modify either the primary or mask stencil");
958 }
959
960
961 static void ed_keymap_stencil(wmKeyMap *keymap)
962 {
963         wmKeyMapItem *kmi;
964
965         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, 0, 0);
966         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
967         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT, 0);
968         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
969         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL, 0);
970         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
971
972         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_ALT, 0);
973         RNA_enum_set(kmi->ptr, "mode", STENCIL_TRANSLATE);
974         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
975         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_SHIFT | KM_ALT, 0);
976         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
977         RNA_enum_set(kmi->ptr, "mode", STENCIL_SCALE);
978         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_stencil_control", RIGHTMOUSE, KM_PRESS, KM_CTRL | KM_ALT, 0);
979         RNA_enum_set(kmi->ptr, "texmode", STENCIL_SECONDARY);
980         RNA_enum_set(kmi->ptr, "mode", STENCIL_ROTATE);
981 }
982
983 /**************************** registration **********************************/
984
985 void ED_operatormacros_paint(void)
986 {
987         wmOperatorType *ot;
988         wmOperatorTypeMacro *otmacro;
989
990         ot = WM_operatortype_append_macro("PAINTCURVE_OT_add_point_slide", "Add Curve Point and Slide",
991                                           "Add new curve point and slide it", OPTYPE_UNDO);
992         ot->description = "Add new curve point and slide it";
993         WM_operatortype_macro_define(ot, "PAINTCURVE_OT_add_point");
994         otmacro = WM_operatortype_macro_define(ot, "PAINTCURVE_OT_slide");
995         RNA_boolean_set(otmacro->ptr, "align", true);
996         RNA_boolean_set(otmacro->ptr, "select", false);
997 }
998
999
1000 void ED_operatortypes_paint(void)
1001 {
1002         /* palette */
1003         WM_operatortype_append(PALETTE_OT_new);
1004         WM_operatortype_append(PALETTE_OT_color_add);
1005         WM_operatortype_append(PALETTE_OT_color_delete);
1006
1007         /* paint curve */
1008         WM_operatortype_append(PAINTCURVE_OT_new);
1009         WM_operatortype_append(PAINTCURVE_OT_add_point);
1010         WM_operatortype_append(PAINTCURVE_OT_delete_point);
1011         WM_operatortype_append(PAINTCURVE_OT_select);
1012         WM_operatortype_append(PAINTCURVE_OT_slide);
1013         WM_operatortype_append(PAINTCURVE_OT_draw);
1014         WM_operatortype_append(PAINTCURVE_OT_cursor);
1015
1016         /* brush */
1017         WM_operatortype_append(BRUSH_OT_add);
1018         WM_operatortype_append(BRUSH_OT_scale_size);
1019         WM_operatortype_append(BRUSH_OT_curve_preset);
1020         WM_operatortype_append(BRUSH_OT_reset);
1021         WM_operatortype_append(BRUSH_OT_stencil_control);
1022         WM_operatortype_append(BRUSH_OT_stencil_fit_image_aspect);
1023         WM_operatortype_append(BRUSH_OT_stencil_reset_transform);
1024
1025         /* note, particle uses a different system, can be added with existing operators in wm.py */
1026         WM_operatortype_append(PAINT_OT_brush_select);
1027         WM_operatortype_append(BRUSH_OT_uv_sculpt_tool_set);
1028
1029         /* image */
1030         WM_operatortype_append(PAINT_OT_texture_paint_toggle);
1031         WM_operatortype_append(PAINT_OT_image_paint);
1032         WM_operatortype_append(PAINT_OT_sample_color);
1033         WM_operatortype_append(PAINT_OT_grab_clone);
1034         WM_operatortype_append(PAINT_OT_project_image);
1035         WM_operatortype_append(PAINT_OT_image_from_view);
1036         WM_operatortype_append(PAINT_OT_brush_colors_flip);
1037         WM_operatortype_append(PAINT_OT_add_texture_paint_slot);
1038         WM_operatortype_append(PAINT_OT_add_simple_uvs);
1039
1040         /* weight */
1041         WM_operatortype_append(PAINT_OT_weight_paint_toggle);
1042         WM_operatortype_append(PAINT_OT_weight_paint);
1043         WM_operatortype_append(PAINT_OT_weight_set);
1044         WM_operatortype_append(PAINT_OT_weight_from_bones);
1045         WM_operatortype_append(PAINT_OT_weight_gradient);
1046         WM_operatortype_append(PAINT_OT_weight_sample);
1047         WM_operatortype_append(PAINT_OT_weight_sample_group);
1048
1049         /* uv */
1050         WM_operatortype_append(SCULPT_OT_uv_sculpt_stroke);
1051
1052         /* vertex selection */
1053         WM_operatortype_append(PAINT_OT_vert_select_all);
1054         WM_operatortype_append(PAINT_OT_vert_select_ungrouped);
1055
1056         /* vertex */
1057         WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
1058         WM_operatortype_append(PAINT_OT_vertex_paint);
1059         WM_operatortype_append(PAINT_OT_vertex_color_set);
1060         WM_operatortype_append(PAINT_OT_vertex_color_smooth);
1061
1062         WM_operatortype_append(PAINT_OT_vertex_color_brightness_contrast);
1063         WM_operatortype_append(PAINT_OT_vertex_color_hsv);
1064         WM_operatortype_append(PAINT_OT_vertex_color_invert);
1065         WM_operatortype_append(PAINT_OT_vertex_color_levels);
1066         WM_operatortype_append(PAINT_OT_vertex_color_from_weight);
1067
1068         /* face-select */
1069         WM_operatortype_append(PAINT_OT_face_select_linked);
1070         WM_operatortype_append(PAINT_OT_face_select_linked_pick);
1071         WM_operatortype_append(PAINT_OT_face_select_all);
1072         WM_operatortype_append(PAINT_OT_face_select_hide);
1073         WM_operatortype_append(PAINT_OT_face_select_reveal);
1074
1075         /* partial visibility */
1076         WM_operatortype_append(PAINT_OT_hide_show);
1077
1078         /* paint masking */
1079         WM_operatortype_append(PAINT_OT_mask_flood_fill);
1080         WM_operatortype_append(PAINT_OT_mask_lasso_gesture);
1081 }
1082
1083
1084 static void ed_keymap_paint_brush_switch(wmKeyMap *keymap, const char *mode)
1085 {
1086         wmKeyMapItem *kmi;
1087         int i;
1088         /* index 0-9 (zero key is tenth), shift key for index 10-19 */
1089         for (i = 0; i < 20; i++) {
1090                 kmi = WM_keymap_add_item(keymap, "BRUSH_OT_active_index_set",
1091                                          ZEROKEY + ((i + 1) % 10), KM_PRESS, i < 10 ? 0 : KM_SHIFT, 0);
1092                 RNA_string_set(kmi->ptr, "mode", mode);
1093                 RNA_int_set(kmi->ptr, "index", i);
1094         }
1095 }
1096
1097 static void ed_keymap_paint_brush_size(wmKeyMap *keymap, const char *UNUSED(path))
1098 {
1099         wmKeyMapItem *kmi;
1100
1101         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", LEFTBRACKETKEY, KM_PRESS, 0, 0);
1102         RNA_float_set(kmi->ptr, "scalar", 0.9);
1103
1104         kmi = WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
1105         RNA_float_set(kmi->ptr, "scalar", 10.0 / 9.0); // 1.1111....
1106 }
1107
1108 static void set_brush_rc_path(PointerRNA *ptr, const char *brush_path,
1109                               const char *output_name, const char *input_name)
1110 {
1111         char *path;
1112
1113         path = BLI_sprintfN("%s.%s", brush_path, input_name);
1114         RNA_string_set(ptr, output_name, path);
1115         MEM_freeN(path);
1116 }
1117
1118 void set_brush_rc_props(PointerRNA *ptr, const char *paint,
1119                         const char *prop, const char *secondary_prop,
1120                         RCFlags flags)
1121 {
1122         const char *ups_path = "tool_settings.unified_paint_settings";
1123         char *brush_path;
1124
1125         brush_path = BLI_sprintfN("tool_settings.%s.brush", paint);
1126
1127         set_brush_rc_path(ptr, brush_path, "data_path_primary", prop);
1128         if (secondary_prop) {
1129                 set_brush_rc_path(ptr, ups_path, "use_secondary", secondary_prop);
1130                 set_brush_rc_path(ptr, ups_path, "data_path_secondary", prop);
1131         }
1132         else {
1133                 RNA_string_set(ptr, "use_secondary", "");
1134                 RNA_string_set(ptr, "data_path_secondary", "");
1135         }
1136         set_brush_rc_path(ptr, brush_path, "color_path", "cursor_color_add");
1137         if (flags & RC_SECONDARY_ROTATION)
1138                 set_brush_rc_path(ptr, brush_path, "rotation_path", "mask_texture_slot.angle");
1139         else
1140                 set_brush_rc_path(ptr, brush_path, "rotation_path", "texture_slot.angle");
1141         RNA_string_set(ptr, "image_id", brush_path);
1142
1143         if (flags & RC_COLOR) {
1144                 set_brush_rc_path(ptr, brush_path, "fill_color_path", "color");
1145         }
1146         else {
1147                 RNA_string_set(ptr, "fill_color_path", "");
1148         }
1149
1150         if (flags & RC_COLOR_OVERRIDE) {
1151                 RNA_string_set(ptr, "fill_color_override_path", "tool_settings.unified_paint_settings.color");
1152                 RNA_string_set(ptr, "fill_color_override_test_path", "tool_settings.unified_paint_settings.use_unified_color");
1153         }
1154         else {
1155                 RNA_string_set(ptr, "fill_color_override_path", "");
1156                 RNA_string_set(ptr, "fill_color_override_test_path", "");
1157         }
1158
1159         if (flags & RC_ZOOM)
1160                 RNA_string_set(ptr, "zoom_path", "space_data.zoom");
1161         else
1162                 RNA_string_set(ptr, "zoom_path", "");
1163
1164         RNA_boolean_set(ptr, "secondary_tex", (flags & RC_SECONDARY_ROTATION) != 0);
1165
1166         MEM_freeN(brush_path);
1167 }
1168
1169 static void ed_keymap_paint_brush_radial_control(wmKeyMap *keymap, const char *paint,
1170                                                  RCFlags flags)
1171 {
1172         wmKeyMapItem *kmi;
1173         /* only size needs to follow zoom, strength shows fixed size circle */
1174         int flags_nozoom = flags & (~RC_ZOOM);
1175         int flags_noradial_secondary = flags & (~(RC_SECONDARY_ROTATION | RC_ZOOM));
1176
1177         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, 0, 0);
1178         set_brush_rc_props(kmi->ptr, paint, "size", "use_unified_size", flags);
1179
1180         kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0);
1181         set_brush_rc_props(kmi->ptr, paint, "strength", "use_unified_strength", flags_nozoom);
1182
1183         if (flags & RC_WEIGHT) {
1184                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", WKEY, KM_PRESS, 0, 0);
1185                 set_brush_rc_props(kmi->ptr, paint, "weight", "use_unified_weight", flags_nozoom);
1186         }
1187
1188         if (flags & RC_ROTATION) {
1189                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0);
1190                 set_brush_rc_props(kmi->ptr, paint, "texture_slot.angle", NULL, flags_noradial_secondary);
1191         }
1192
1193         if (flags & RC_SECONDARY_ROTATION) {
1194                 kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, KM_CTRL | KM_ALT, 0);
1195                 set_brush_rc_props(kmi->ptr, paint, "mask_texture_slot.angle", NULL, flags_nozoom);
1196         }
1197 }
1198
1199 static void paint_partial_visibility_keys(wmKeyMap *keymap)
1200 {
1201         wmKeyMapItem *kmi;
1202         
1203         /* Partial visibility */
1204         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_SHIFT, 0);
1205         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
1206         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
1207         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, 0, 0);
1208         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_HIDE);
1209         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_INSIDE);
1210         kmi = WM_keymap_add_item(keymap, "PAINT_OT_hide_show", HKEY, KM_PRESS, KM_ALT, 0);
1211         RNA_enum_set(kmi->ptr, "action", PARTIALVIS_SHOW);
1212         RNA_enum_set(kmi->ptr, "area", PARTIALVIS_ALL);
1213 }
1214
1215
1216 static void paint_keymap_curve(wmKeyMap *keymap)
1217 {
1218         wmKeyMapItem *kmi;
1219
1220         WM_keymap_add_item(keymap, "PAINTCURVE_OT_add_point_slide", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
1221         WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
1222         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
1223         RNA_boolean_set(kmi->ptr, "extend", true);
1224         WM_keymap_add_item(keymap, "PAINTCURVE_OT_slide", ACTIONMOUSE, KM_PRESS, 0, 0);
1225         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_slide", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
1226         RNA_boolean_set(kmi->ptr, "align", true);
1227         kmi = WM_keymap_add_item(keymap, "PAINTCURVE_OT_select", AKEY, KM_PRESS, 0, 0);
1228         RNA_boolean_set(kmi->ptr, "toggle", true);
1229
1230         WM_keymap_add_item(keymap, "PAINTCURVE_OT_cursor", ACTIONMOUSE, KM_PRESS, 0, 0);
1231         WM_keymap_add_item(keymap, "PAINTCURVE_OT_delete_point", XKEY, KM_PRESS, 0, 0);
1232         WM_keymap_add_item(keymap, "PAINTCURVE_OT_delete_point", DELKEY, KM_PRESS, 0, 0);
1233
1234         WM_keymap_add_item(keymap, "PAINTCURVE_OT_draw", RETKEY, KM_PRESS, 0, 0);
1235         WM_keymap_add_item(keymap, "PAINTCURVE_OT_draw", PADENTER, KM_PRESS, 0, 0);
1236
1237         WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", GKEY, KM_PRESS, 0, 0);
1238         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", EVT_TWEAK_S, KM_ANY, 0, 0);
1239         WM_keymap_add_item(keymap, "TRANSFORM_OT_rotate", RKEY, KM_PRESS, 0, 0);
1240         WM_keymap_add_item(keymap, "TRANSFORM_OT_resize", SKEY, KM_PRESS, 0, 0);
1241 }
1242
1243 void ED_keymap_paint(wmKeyConfig *keyconf)
1244 {
1245         wmKeyMap *keymap;
1246         wmKeyMapItem *kmi;
1247         int i;
1248         
1249         keymap = WM_keymap_find(keyconf, "Paint Curve", 0, 0);
1250         keymap->poll = paint_curve_poll;
1251
1252         paint_keymap_curve(keymap);
1253
1254         /* Sculpt mode */
1255         keymap = WM_keymap_find(keyconf, "Sculpt", 0, 0);
1256         keymap->poll = sculpt_mode_poll;
1257
1258         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1259         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1260         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1261
1262         /* Partial visibility, sculpt-only for now */
1263         paint_partial_visibility_keys(keymap);
1264
1265         for (i = 0; i <= 5; i++)
1266                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY + i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
1267
1268         /* Clear mask */
1269         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", MKEY, KM_PRESS, KM_ALT, 0);
1270         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_FLOOD_VALUE);
1271         RNA_float_set(kmi->ptr, "value", 0);
1272
1273         /* Invert mask */
1274         kmi = WM_keymap_add_item(keymap, "PAINT_OT_mask_flood_fill", IKEY, KM_PRESS, KM_CTRL, 0);
1275         RNA_enum_set(kmi->ptr, "mode", PAINT_MASK_INVERT);
1276
1277         WM_keymap_add_item(keymap, "PAINT_OT_mask_lasso_gesture", LEFTMOUSE, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
1278
1279         /* Toggle mask visibility */
1280         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, KM_CTRL, 0);
1281         RNA_string_set(kmi->ptr, "data_path", "scene.tool_settings.sculpt.show_mask");
1282
1283         /* Toggle dynamic topology */
1284         WM_keymap_add_item(keymap, "SCULPT_OT_dynamic_topology_toggle", DKEY, KM_PRESS, KM_CTRL, 0);
1285
1286         /* Dynamic-topology detail size
1287          * 
1288          * This should be improved further, perhaps by showing a triangle
1289          * grid rather than brush alpha */
1290         kmi = WM_keymap_add_item(keymap, "SCULPT_OT_set_detail_size", DKEY, KM_PRESS, KM_SHIFT, 0);
1291
1292         /* multires switch */
1293         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
1294         RNA_int_set(kmi->ptr, "level", 1);
1295         RNA_boolean_set(kmi->ptr, "relative", true);
1296
1297         kmi = WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
1298         RNA_int_set(kmi->ptr, "level", -1);
1299         RNA_boolean_set(kmi->ptr, "relative", true);
1300
1301         ed_keymap_paint_brush_switch(keymap, "sculpt");
1302         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
1303         ed_keymap_paint_brush_radial_control(keymap, "sculpt", RC_ROTATION);
1304
1305         ed_keymap_stencil(keymap);
1306
1307         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_DRAW, XKEY, 0);
1308         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SMOOTH, SKEY, 0);
1309         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_PINCH, PKEY, 0);
1310         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_INFLATE, IKEY, 0);
1311         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_GRAB, GKEY, 0);
1312         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_LAYER, LKEY, 0);
1313         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_FLATTEN, TKEY, KM_SHIFT);
1314         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CLAY, CKEY, 0);
1315         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_CREASE, CKEY, KM_SHIFT);
1316         keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_SNAKE_HOOK, KKEY, 0);
1317         kmi = keymap_brush_select(keymap, OB_MODE_SCULPT, SCULPT_TOOL_MASK, MKEY, 0);
1318         RNA_boolean_set(kmi->ptr, "toggle", 1);
1319         RNA_boolean_set(kmi->ptr, "create_missing", 1);
1320
1321         /* */
1322         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1323         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.stroke_method");
1324
1325         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1326         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
1327
1328         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1329
1330         /* Vertex Paint mode */
1331         keymap = WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
1332         keymap->poll = vertex_paint_mode_poll;
1333
1334         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
1335         WM_keymap_add_item(keymap, "PAINT_OT_brush_colors_flip", XKEY, KM_PRESS, 0, 0);
1336         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1337
1338         WM_keymap_add_item(keymap,
1339                            "PAINT_OT_vertex_color_set", KKEY, KM_PRESS, KM_SHIFT, 0);
1340
1341         ed_keymap_paint_brush_switch(keymap, "vertex_paint");
1342         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
1343         ed_keymap_paint_brush_radial_control(keymap, "vertex_paint", RC_COLOR | RC_COLOR_OVERRIDE | RC_ROTATION);
1344
1345         ed_keymap_stencil(keymap);
1346
1347         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1348         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
1349
1350         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1351         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.use_smooth_stroke");
1352
1353         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1354
1355         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1356         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
1357
1358         /* Weight Paint mode */
1359         keymap = WM_keymap_find(keyconf, "Weight Paint", 0, 0);
1360         keymap->poll = weight_paint_mode_poll;
1361
1362         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
1363
1364         /* these keys are from 2.4x but could be changed */
1365         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
1366         WM_keymap_verify_item(keymap, "PAINT_OT_weight_sample_group", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
1367
1368         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_gradient", LEFTMOUSE, KM_PRESS, KM_ALT, 0)->ptr,           "type", WPAINT_GRADIENT_TYPE_LINEAR);
1369         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);
1370
1371         WM_keymap_add_item(keymap,
1372                            "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
1373
1374         ed_keymap_paint_brush_switch(keymap, "weight_paint");
1375         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
1376         ed_keymap_paint_brush_radial_control(keymap, "weight_paint", RC_WEIGHT);
1377
1378         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1379         RNA_string_set(kmi->ptr, "data_path", "tool_settings.vertex_paint.brush.stroke_method");
1380
1381         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* face mask toggle */
1382         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
1383
1384         /* note, conflicts with vertex paint, but this is more useful */
1385         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", VKEY, KM_PRESS, 0, 0); /* vert mask toggle */
1386         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask_vertex");
1387
1388         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1389         RNA_string_set(kmi->ptr, "data_path", "tool_settings.weight_paint.brush.use_smooth_stroke");
1390
1391         /*Weight paint's Vertex Selection Mode */
1392         keymap = WM_keymap_find(keyconf, "Weight Paint Vertex Selection", 0, 0);
1393         keymap->poll = vert_paint_poll;
1394         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", AKEY, KM_PRESS, 0, 0);
1395         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
1396         kmi = WM_keymap_add_item(keymap, "PAINT_OT_vert_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1397         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1398         WM_keymap_add_item(keymap, "VIEW3D_OT_select_border", BKEY, KM_PRESS, 0, 0);
1399         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
1400         RNA_boolean_set(kmi->ptr, "deselect", false);
1401         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0);
1402         RNA_boolean_set(kmi->ptr, "deselect", true);
1403         WM_keymap_add_item(keymap, "VIEW3D_OT_select_circle", CKEY, KM_PRESS, 0, 0);
1404
1405         /* Image/Texture Paint mode */
1406         keymap = WM_keymap_find(keyconf, "Image Paint", 0, 0);
1407         keymap->poll = image_texture_paint_poll;
1408
1409         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1410         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1411         WM_keymap_add_item(keymap, "PAINT_OT_brush_colors_flip", XKEY, KM_PRESS, 0, 0);
1412         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
1413         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
1414
1415         ed_keymap_paint_brush_switch(keymap, "image_paint");
1416         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
1417         ed_keymap_paint_brush_radial_control(
1418                 keymap, "image_paint",
1419                 RC_COLOR | RC_COLOR_OVERRIDE | RC_ZOOM | RC_ROTATION | RC_SECONDARY_ROTATION);
1420
1421         ed_keymap_stencil(keymap);
1422
1423         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
1424         RNA_string_set(kmi->ptr, "data_path", "image_paint_object.data.use_paint_mask");
1425
1426         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
1427         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.use_smooth_stroke");
1428
1429         WM_keymap_add_menu(keymap, "VIEW3D_MT_angle_control", RKEY, KM_PRESS, 0, 0);
1430
1431         kmi = WM_keymap_add_item(keymap, "WM_OT_context_menu_enum", EKEY, KM_PRESS, 0, 0);
1432         RNA_string_set(kmi->ptr, "data_path", "tool_settings.image_paint.brush.stroke_method");
1433
1434         /* face-mask mode */
1435         keymap = WM_keymap_find(keyconf, "Face Mask", 0, 0);
1436         keymap->poll = facemask_paint_poll;
1437
1438         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
1439         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
1440         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
1441         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
1442         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, 0, 0);
1443         RNA_boolean_set(kmi->ptr, "unselected", false);
1444         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
1445         RNA_boolean_set(kmi->ptr, "unselected", true);
1446         WM_keymap_add_item(keymap, "PAINT_OT_face_select_reveal", HKEY, KM_PRESS, KM_ALT, 0);
1447
1448         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
1449         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
1450         RNA_boolean_set(kmi->ptr, "deselect", false);
1451         kmi = WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
1452         RNA_boolean_set(kmi->ptr, "deselect", true);
1453
1454         keymap = WM_keymap_find(keyconf, "UV Sculpt", 0, 0);
1455         keymap->poll = uv_sculpt_keymap_poll;
1456
1457         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", QKEY, KM_PRESS, 0, 0);
1458         RNA_string_set(kmi->ptr, "data_path", "tool_settings.use_uv_sculpt");
1459
1460         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", BRUSH_STROKE_NORMAL);
1461         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", BRUSH_STROKE_INVERT);
1462         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_uv_sculpt_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", BRUSH_STROKE_SMOOTH);
1463
1464         ed_keymap_paint_brush_size(keymap, "tool_settings.uv_sculpt.brush.size");
1465         ed_keymap_paint_brush_radial_control(keymap, "uv_sculpt", 0);
1466
1467         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);
1468         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);
1469         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);
1470
1471         /* paint stroke */
1472         keymap = paint_stroke_modal_keymap(keyconf);
1473         WM_modalkeymap_assign(keymap, "SCULPT_OT_brush_stroke");
1474 }