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