* added keymap C for crease brush
[blender.git] / source / blender / editors / sculpt_paint / paint_ops.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * ***** END GPL LICENSE BLOCK *****
20  */
21
22 #include "DNA_object_types.h"
23 #include "DNA_scene_types.h"
24
25 #include "BKE_brush.h"
26 #include "BKE_context.h"
27 #include "BKE_paint.h"
28
29 #include "ED_screen.h"
30 #include "UI_resources.h"
31
32 #include "WM_api.h"
33 #include "WM_types.h"
34
35 #include "RNA_access.h"
36 #include "RNA_define.h"
37
38 #include "paint_intern.h"
39 #include "sculpt_intern.h"
40
41 #include <string.h>
42 //#include <stdio.h>
43
44 /* Brush operators */
45 static int brush_add_exec(bContext *C, wmOperator *op)
46 {
47         /*int type = RNA_enum_get(op->ptr, "type");*/
48         Paint *paint = paint_get_active(CTX_data_scene(C));
49         Brush *br = paint_brush(paint);
50
51         if (br)
52                 br = copy_brush(br);
53         else
54                 br = add_brush("Brush");
55
56         paint_brush_set(paint_get_active(CTX_data_scene(C)), br);
57
58         return OPERATOR_FINISHED;
59 }
60
61 void BRUSH_OT_add(wmOperatorType *ot)
62 {
63         /* identifiers */
64         ot->name= "Add Brush";
65         ot->description= "Add brush by mode type";
66         ot->idname= "BRUSH_OT_add";
67         
68         /* api callbacks */
69         ot->exec= brush_add_exec;
70         
71         /* flags */
72         ot->flag= OPTYPE_UNDO;
73 }
74
75
76 static int brush_scale_size_exec(bContext *C, wmOperator *op)
77 {
78         /*int type = RNA_enum_get(op->ptr, "type");*/
79         Paint *paint = paint_get_active(CTX_data_scene(C));
80         Brush *br = paint_brush(paint);
81         float factor = RNA_float_get(op->ptr, "scalar");
82
83         if (br) {
84                 if (U.sculpt_paint_settings & SCULPT_PAINT_USE_UNIFIED_SIZE) {
85                         U.sculpt_paint_unified_size *= factor;
86                 }
87                 else {
88                         br->size *= factor;
89                 }
90         }
91
92         return OPERATOR_FINISHED;
93 }
94
95 void BRUSH_OT_scale_size(wmOperatorType *ot)
96 {
97         /* identifiers */
98         ot->name= "Scale Sculpt/Paint Brush Size";
99         ot->description= "Change brush size by a scalar";
100         ot->idname= "BRUSH_OT_scale_size";
101         
102         /* api callbacks */
103         ot->exec= brush_scale_size_exec;
104         
105         /* flags */
106         ot->flag= OPTYPE_UNDO;
107
108         RNA_def_float(ot->srna, "scalar", 1, 0, 2, "Scalar", "Factor to scale brush size by", 0, 2);
109 }
110
111 static int vertex_color_set_exec(bContext *C, wmOperator *op)
112 {
113         Scene *scene = CTX_data_scene(C);
114         Object *obact = CTX_data_active_object(C);
115         unsigned int paintcol = vpaint_get_current_col(scene->toolsettings->vpaint);
116         vpaint_fill(obact, paintcol);
117         
118         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
119         return OPERATOR_FINISHED;
120 }
121
122 void PAINT_OT_vertex_color_set(wmOperatorType *ot)
123 {
124         /* identifiers */
125         ot->name= "Set Vertex Colors";
126         ot->idname= "PAINT_OT_vertex_color_set";
127         
128         /* api callbacks */
129         ot->exec= vertex_color_set_exec;
130         ot->poll= vertex_paint_mode_poll;
131         
132         /* flags */
133         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
134 }
135
136 /**************************** registration **********************************/
137
138 void ED_operatortypes_paint(void)
139 {
140         /* brush */
141         WM_operatortype_append(BRUSH_OT_add);
142         WM_operatortype_append(BRUSH_OT_scale_size);
143         WM_operatortype_append(BRUSH_OT_curve_preset);
144
145
146         /* image */
147         WM_operatortype_append(PAINT_OT_texture_paint_toggle);
148         WM_operatortype_append(PAINT_OT_texture_paint_radial_control);
149         WM_operatortype_append(PAINT_OT_image_paint);
150         WM_operatortype_append(PAINT_OT_image_paint_radial_control);
151         WM_operatortype_append(PAINT_OT_sample_color);
152         WM_operatortype_append(PAINT_OT_grab_clone);
153         WM_operatortype_append(PAINT_OT_clone_cursor_set);
154         WM_operatortype_append(PAINT_OT_project_image);
155         WM_operatortype_append(PAINT_OT_image_from_view);
156
157
158         /* weight */
159         WM_operatortype_append(PAINT_OT_weight_paint_toggle);
160         WM_operatortype_append(PAINT_OT_weight_paint_radial_control);
161         WM_operatortype_append(PAINT_OT_weight_paint);
162         WM_operatortype_append(PAINT_OT_weight_set);
163         WM_operatortype_append(PAINT_OT_weight_from_bones);
164
165         /* vertex */
166         WM_operatortype_append(PAINT_OT_vertex_paint_radial_control);
167         WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
168         WM_operatortype_append(PAINT_OT_vertex_paint);
169         WM_operatortype_append(PAINT_OT_vertex_color_set);
170
171         /* face-select */
172         WM_operatortype_append(PAINT_OT_face_select_linked);
173         WM_operatortype_append(PAINT_OT_face_select_linked_pick);
174         WM_operatortype_append(PAINT_OT_face_select_all);
175 }
176
177
178 static void ed_keymap_paint_brush_switch(wmKeyMap *keymap, const char *path)
179 {
180         wmKeyMapItem *kmi;
181
182         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY, KM_PRESS, 0, 0);
183         RNA_string_set(kmi->ptr, "data_path", path);
184         RNA_int_set(kmi->ptr, "value", 0);
185         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", TWOKEY, KM_PRESS, 0, 0);
186         RNA_string_set(kmi->ptr, "data_path", path);
187         RNA_int_set(kmi->ptr, "value", 1);
188         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", THREEKEY, KM_PRESS, 0, 0);
189         RNA_string_set(kmi->ptr, "data_path", path);
190         RNA_int_set(kmi->ptr, "value", 2);
191         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", FOURKEY, KM_PRESS, 0, 0);
192         RNA_string_set(kmi->ptr, "data_path", path);
193         RNA_int_set(kmi->ptr, "value", 3);
194         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", FIVEKEY, KM_PRESS, 0, 0);
195         RNA_string_set(kmi->ptr, "data_path", path);
196         RNA_int_set(kmi->ptr, "value", 4);
197         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", SIXKEY, KM_PRESS, 0, 0);
198         RNA_string_set(kmi->ptr, "data_path", path);
199         RNA_int_set(kmi->ptr, "value", 5);
200         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", SEVENKEY, KM_PRESS, 0, 0);
201         RNA_string_set(kmi->ptr, "data_path", path);
202         RNA_int_set(kmi->ptr, "value", 6);
203         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", EIGHTKEY, KM_PRESS, 0, 0);
204         RNA_string_set(kmi->ptr, "data_path", path);
205         RNA_int_set(kmi->ptr, "value", 7);
206         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", NINEKEY, KM_PRESS, 0, 0);
207         RNA_string_set(kmi->ptr, "data_path", path);
208         RNA_int_set(kmi->ptr, "value", 8);
209         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", ZEROKEY, KM_PRESS, 0, 0);
210         RNA_string_set(kmi->ptr, "data_path", path);
211         RNA_int_set(kmi->ptr, "value", 9);
212 }
213
214 static void ed_keymap_paint_brush_size(wmKeyMap *keymap, const char *path)
215 {
216         wmKeyMapItem *kmi;
217
218         kmi= WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", LEFTBRACKETKEY, KM_PRESS, 0, 0);
219         RNA_float_set(kmi->ptr, "scalar", 0.9);
220
221         kmi= WM_keymap_add_item(keymap, "BRUSH_OT_scale_size", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
222         RNA_float_set(kmi->ptr, "scalar", 10.0/9.0); // 1.1111....
223 }
224
225 void ED_keymap_paint(wmKeyConfig *keyconf)
226 {
227         wmKeyMap *keymap;
228         wmKeyMapItem *kmi;
229         int i;
230         
231         /* Sculpt mode */
232         keymap= WM_keymap_find(keyconf, "Sculpt", 0, 0);
233         keymap->poll= sculpt_poll;
234
235         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_radial_control", FKEY, KM_PRESS, 0, 0)->ptr,        "mode", WM_RADIALCONTROL_SIZE);
236         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", WM_RADIALCONTROL_STRENGTH);
237         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0)->ptr,  "mode", WM_RADIALCONTROL_ANGLE);
238
239         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0,        0)->ptr, "mode", WM_BRUSHSTROKE_NORMAL);
240         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_CTRL,  0)->ptr, "mode", WM_BRUSHSTROKE_INVERT);
241         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", WM_BRUSHSTROKE_SMOOTH);
242
243         //stroke_mode_modal_keymap(keyconf);
244
245         for(i=0; i<=5; i++)
246                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY+i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
247
248         /* multires switch */
249         kmi= WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
250         RNA_int_set(kmi->ptr, "level", 1);
251         RNA_boolean_set(kmi->ptr, "relative", 1);
252
253         kmi= WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
254         RNA_int_set(kmi->ptr, "level", -1);
255         RNA_boolean_set(kmi->ptr, "relative", 1);
256
257         ed_keymap_paint_brush_switch(keymap, "tool_settings.sculpt.active_brush_index");
258         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
259
260         /* */
261         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", AKEY, KM_PRESS, 0, 0);
262         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_anchor");
263
264         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
265         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
266
267         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", RKEY, KM_PRESS, 0, 0);
268         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_rake");
269
270         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", AKEY, KM_PRESS, KM_SHIFT, 0);
271         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_airbrush");
272
273         /* brush switching */
274         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", DKEY, KM_PRESS, 0, 0);
275         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
276         RNA_string_set(kmi->ptr, "value", "SculptDraw");
277         
278         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", SKEY, KM_PRESS, 0, 0);
279         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
280         RNA_string_set(kmi->ptr, "value", "Smooth");
281         
282         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PKEY, KM_PRESS, 0, 0);
283         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
284         RNA_string_set(kmi->ptr, "value", "Pinch/Magnify");
285         
286         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", GKEY, KM_PRESS, 0, 0);
287         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
288         RNA_string_set(kmi->ptr, "value", "Grab");
289         
290         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", LKEY, KM_PRESS, 0, 0);
291         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
292         RNA_string_set(kmi->ptr, "value", "Layer");
293
294         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", CKEY, KM_PRESS, 0, 0);
295         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
296         RNA_string_set(kmi->ptr, "value", "Crease");
297
298         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", TKEY, KM_PRESS, KM_SHIFT, 0); // was just T in 2.4x
299         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
300         RNA_string_set(kmi->ptr, "value", "Flatten/Contrast");
301
302         /* Vertex Paint mode */
303         keymap= WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
304         keymap->poll= vertex_paint_mode_poll;
305
306         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_vertex_paint_radial_control", FKEY, KM_PRESS, 0, 0)->ptr, "mode", WM_RADIALCONTROL_SIZE);
307         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_vertex_paint_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", WM_RADIALCONTROL_STRENGTH);
308         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
309         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", RIGHTMOUSE, KM_PRESS, 0, 0);
310
311         WM_keymap_add_item(keymap,
312                         "PAINT_OT_vertex_color_set",KKEY, KM_PRESS, KM_SHIFT, 0);
313
314         ed_keymap_paint_brush_switch(keymap, "tool_settings.vertex_paint.active_brush_index");
315         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
316
317         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
318         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
319
320         /* Weight Paint mode */
321         keymap= WM_keymap_find(keyconf, "Weight Paint", 0, 0);
322         keymap->poll= weight_paint_mode_poll;
323
324         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_paint_radial_control", FKEY, KM_PRESS, 0, 0)->ptr, "mode", WM_RADIALCONTROL_SIZE);
325         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_paint_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", WM_RADIALCONTROL_STRENGTH);
326
327         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
328
329         WM_keymap_add_item(keymap,
330                         "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
331
332         ed_keymap_paint_brush_switch(keymap, "tool_settings.weight_paint.active_brush_index");
333         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
334
335         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
336         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
337
338         WM_keymap_verify_item(keymap, "PAINT_OT_weight_from_bones", WKEY, KM_PRESS, 0, 0);
339
340         /* Image/Texture Paint mode */
341         keymap= WM_keymap_find(keyconf, "Image Paint", 0, 0);
342         keymap->poll= image_texture_paint_poll;
343
344         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_texture_paint_radial_control", FKEY, KM_PRESS, 0, 0)->ptr, "mode", WM_RADIALCONTROL_SIZE);
345         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_texture_paint_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", WM_RADIALCONTROL_STRENGTH);
346
347         WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0, 0);
348         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
349         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", RIGHTMOUSE, KM_PRESS, 0, 0);
350         WM_keymap_add_item(keymap, "PAINT_OT_clone_cursor_set", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
351
352         ed_keymap_paint_brush_switch(keymap, "tool_settings.image_paint.active_brush_index");
353         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
354
355         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
356         RNA_string_set(kmi->ptr, "data_path", "texture_paint_object.data.use_paint_mask");
357
358         /* face-mask mode */
359         keymap= WM_keymap_find(keyconf, "Face Mask", 0, 0);
360         keymap->poll= facemask_paint_poll;
361
362         WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
363         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
364         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
365
366 }