Recreating my GSoC branch.
[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
43 /* Brush operators */
44 static int brush_add_exec(bContext *C, wmOperator *op)
45 {
46         /*int type = RNA_enum_get(op->ptr, "type");*/
47         Brush *br = NULL;
48
49         br = add_brush("Brush");
50
51         if(br)
52                 paint_brush_set(paint_get_active(CTX_data_scene(C)), br);
53
54         return OPERATOR_FINISHED;
55 }
56
57 static EnumPropertyItem brush_type_items[] = {
58         {OB_MODE_SCULPT, "SCULPT", ICON_SCULPTMODE_HLT, "Sculpt", ""},
59         {OB_MODE_VERTEX_PAINT, "VERTEX_PAINT", ICON_VPAINT_HLT, "Vertex Paint", ""},
60         {OB_MODE_WEIGHT_PAINT, "WEIGHT_PAINT", ICON_WPAINT_HLT, "Weight Paint", ""},
61         {OB_MODE_TEXTURE_PAINT, "TEXTURE_PAINT", ICON_TPAINT_HLT, "Texture Paint", ""},
62         {0, NULL, 0, NULL, NULL}};
63
64 void BRUSH_OT_add(wmOperatorType *ot)
65 {
66         /* identifiers */
67         ot->name= "Add Brush";
68         ot->description= "Add brush by mode type";
69         ot->idname= "BRUSH_OT_add";
70         
71         /* api callbacks */
72         ot->exec= brush_add_exec;
73         
74         /* flags */
75         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
76
77         RNA_def_enum(ot->srna, "type", brush_type_items, OB_MODE_VERTEX_PAINT, "Type", "Which paint mode to create the brush for.");
78 }
79
80 static int vertex_color_set_exec(bContext *C, wmOperator *op)
81 {
82         Scene *scene = CTX_data_scene(C);
83         Object *obact = CTX_data_active_object(C);
84         unsigned int paintcol = vpaint_get_current_col(scene->toolsettings->vpaint);
85         vpaint_fill(obact, paintcol);
86         
87         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
88         return OPERATOR_FINISHED;
89 }
90
91 void PAINT_OT_vertex_color_set(wmOperatorType *ot)
92 {
93         /* identifiers */
94         ot->name= "Set Vertex Colors";
95         ot->idname= "PAINT_OT_vertex_color_set";
96         
97         /* api callbacks */
98         ot->exec= vertex_color_set_exec;
99         ot->poll= vertex_paint_mode_poll;
100         
101         /* flags */
102         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
103 }
104
105 /**************************** registration **********************************/
106
107 void ED_operatortypes_paint(void)
108 {
109         /* brush */
110         WM_operatortype_append(BRUSH_OT_add);
111         WM_operatortype_append(BRUSH_OT_curve_preset);
112
113         /* image */
114         WM_operatortype_append(PAINT_OT_texture_paint_toggle);
115         WM_operatortype_append(PAINT_OT_texture_paint_radial_control);
116         WM_operatortype_append(PAINT_OT_image_paint);
117         WM_operatortype_append(PAINT_OT_image_paint_radial_control);
118         WM_operatortype_append(PAINT_OT_sample_color);
119         WM_operatortype_append(PAINT_OT_grab_clone);
120         WM_operatortype_append(PAINT_OT_clone_cursor_set);
121         WM_operatortype_append(PAINT_OT_project_image);
122         WM_operatortype_append(PAINT_OT_image_from_view);
123
124
125         /* weight */
126         WM_operatortype_append(PAINT_OT_weight_paint_toggle);
127         WM_operatortype_append(PAINT_OT_weight_paint_radial_control);
128         WM_operatortype_append(PAINT_OT_weight_paint);
129         WM_operatortype_append(PAINT_OT_weight_set);
130         WM_operatortype_append(PAINT_OT_weight_from_bones);
131
132         /* vertex */
133         WM_operatortype_append(PAINT_OT_vertex_paint_radial_control);
134         WM_operatortype_append(PAINT_OT_vertex_paint_toggle);
135         WM_operatortype_append(PAINT_OT_vertex_paint);
136         WM_operatortype_append(PAINT_OT_vertex_color_set);
137
138         /* face-select */
139         WM_operatortype_append(PAINT_OT_face_select_linked);
140         WM_operatortype_append(PAINT_OT_face_select_linked_pick);
141         WM_operatortype_append(PAINT_OT_face_select_all);
142 }
143
144 static void ed_keymap_paint_brush_switch(wmKeyMap *keymap, const char *path)
145 {
146         wmKeyMapItem *kmi;
147
148         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", ONEKEY, KM_PRESS, 0, 0);
149         RNA_string_set(kmi->ptr, "data_path", path);
150         RNA_int_set(kmi->ptr, "value", 0);
151         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", TWOKEY, KM_PRESS, 0, 0);
152         RNA_string_set(kmi->ptr, "data_path", path);
153         RNA_int_set(kmi->ptr, "value", 1);
154         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", THREEKEY, KM_PRESS, 0, 0);
155         RNA_string_set(kmi->ptr, "data_path", path);
156         RNA_int_set(kmi->ptr, "value", 2);
157         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", FOURKEY, KM_PRESS, 0, 0);
158         RNA_string_set(kmi->ptr, "data_path", path);
159         RNA_int_set(kmi->ptr, "value", 3);
160         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", FIVEKEY, KM_PRESS, 0, 0);
161         RNA_string_set(kmi->ptr, "data_path", path);
162         RNA_int_set(kmi->ptr, "value", 4);
163         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", SIXKEY, KM_PRESS, 0, 0);
164         RNA_string_set(kmi->ptr, "data_path", path);
165         RNA_int_set(kmi->ptr, "value", 5);
166         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", SEVENKEY, KM_PRESS, 0, 0);
167         RNA_string_set(kmi->ptr, "data_path", path);
168         RNA_int_set(kmi->ptr, "value", 6);
169         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", EIGHTKEY, KM_PRESS, 0, 0);
170         RNA_string_set(kmi->ptr, "data_path", path);
171         RNA_int_set(kmi->ptr, "value", 7);
172         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", NINEKEY, KM_PRESS, 0, 0);
173         RNA_string_set(kmi->ptr, "data_path", path);
174         RNA_int_set(kmi->ptr, "value", 8);
175         kmi= WM_keymap_add_item(keymap, "WM_OT_context_set_int", ZEROKEY, KM_PRESS, 0, 0);
176         RNA_string_set(kmi->ptr, "data_path", path);
177         RNA_int_set(kmi->ptr, "value", 9);
178 }
179
180 static void ed_keymap_paint_brush_size(wmKeyMap *keymap, const char *path)
181 {
182         wmKeyMapItem *kmi;
183         
184         kmi= WM_keymap_add_item(keymap, "WM_OT_context_scale_int", LEFTBRACKETKEY, KM_PRESS, 0, 0);
185         RNA_string_set(kmi->ptr, "data_path", path);
186         RNA_float_set(kmi->ptr, "value", 0.9);
187         
188         kmi= WM_keymap_add_item(keymap, "WM_OT_context_scale_int", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
189         RNA_string_set(kmi->ptr, "data_path", path);
190         RNA_float_set(kmi->ptr, "value", 10.0/9.0); // 1.1111....
191 }       
192
193 void ED_keymap_paint(wmKeyConfig *keyconf)
194 {
195         wmKeyMap *keymap;
196         wmKeyMapItem *kmi;
197         int i;
198         
199         /* Sculpt mode */
200         keymap= WM_keymap_find(keyconf, "Sculpt", 0, 0);
201         keymap->poll= sculpt_poll;
202
203         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_radial_control", FKEY, KM_PRESS, 0, 0)->ptr, "mode", WM_RADIALCONTROL_SIZE);
204         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_radial_control", FKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", WM_RADIALCONTROL_STRENGTH);
205         RNA_enum_set(WM_keymap_add_item(keymap, "SCULPT_OT_radial_control", FKEY, KM_PRESS, KM_CTRL, 0)->ptr, "mode", WM_RADIALCONTROL_ANGLE);
206
207         WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, 0, 0);
208         WM_keymap_add_item(keymap, "SCULPT_OT_brush_stroke", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
209
210         for(i=0; i<=5; i++)
211                 RNA_int_set(WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", ZEROKEY+i, KM_PRESS, KM_CTRL, 0)->ptr, "level", i);
212
213         /* multires switch */
214         kmi= WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEUPKEY, KM_PRESS, 0, 0);
215         RNA_int_set(kmi->ptr, "level", 1);
216         RNA_boolean_set(kmi->ptr, "relative", 1);
217
218         kmi= WM_keymap_add_item(keymap, "OBJECT_OT_subdivision_set", PAGEDOWNKEY, KM_PRESS, 0, 0);
219         RNA_int_set(kmi->ptr, "level", -1);
220         RNA_boolean_set(kmi->ptr, "relative", 1);
221
222         ed_keymap_paint_brush_switch(keymap, "tool_settings.sculpt.active_brush_index");
223         ed_keymap_paint_brush_size(keymap, "tool_settings.sculpt.brush.size");
224         
225         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", AKEY, KM_PRESS, 0, 0);
226         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_anchor");
227
228         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_SHIFT, 0);
229         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_smooth_stroke");
230
231         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", RKEY, KM_PRESS, 0, 0);
232         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_rake");
233
234         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", AKEY, KM_PRESS, KM_SHIFT, 0);
235         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.brush.use_airbrush");
236
237         /* brush switching */
238         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", DKEY, KM_PRESS, 0, 0);
239         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
240         RNA_string_set(kmi->ptr, "value", "Draw");
241         
242         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", SKEY, KM_PRESS, 0, 0);
243         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
244         RNA_string_set(kmi->ptr, "value", "Smooth");
245         
246         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PKEY, KM_PRESS, 0, 0);
247         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
248         RNA_string_set(kmi->ptr, "value", "Pinch");
249         
250         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", GKEY, KM_PRESS, 0, 0);
251         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
252         RNA_string_set(kmi->ptr, "value", "Grab");
253         
254         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", LKEY, KM_PRESS, 0, 0);
255         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
256         RNA_string_set(kmi->ptr, "value", "Layer");
257         
258         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", TKEY, KM_PRESS, KM_SHIFT, 0); // was just T in 2.4x
259         RNA_string_set(kmi->ptr, "data_path", "tool_settings.sculpt.active_brush_name");
260         RNA_string_set(kmi->ptr, "value", "Flatten");
261         
262
263         /* Vertex Paint mode */
264         keymap= WM_keymap_find(keyconf, "Vertex Paint", 0, 0);
265         keymap->poll= vertex_paint_mode_poll;
266
267         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_vertex_paint_radial_control", FKEY, KM_PRESS, 0, 0)->ptr, "mode", WM_RADIALCONTROL_SIZE);
268         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);
269         WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0);
270         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", RIGHTMOUSE, KM_PRESS, 0, 0);
271
272         WM_keymap_add_item(keymap,
273                         "PAINT_OT_vertex_color_set",KKEY, KM_PRESS, KM_SHIFT, 0);
274
275         ed_keymap_paint_brush_switch(keymap, "tool_settings.vertex_paint.active_brush_index");
276         ed_keymap_paint_brush_size(keymap, "tool_settings.vertex_paint.brush.size");
277
278         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
279         RNA_string_set(kmi->ptr, "data_path", "vertex_paint_object.data.use_paint_mask");
280
281         /* Weight Paint mode */
282         keymap= WM_keymap_find(keyconf, "Weight Paint", 0, 0);
283         keymap->poll= weight_paint_mode_poll;
284
285         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_weight_paint_radial_control", FKEY, KM_PRESS, 0, 0)->ptr, "mode", WM_RADIALCONTROL_SIZE);
286         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);
287
288         WM_keymap_verify_item(keymap, "PAINT_OT_weight_paint", LEFTMOUSE, KM_PRESS, 0, 0);
289
290         WM_keymap_add_item(keymap,
291                         "PAINT_OT_weight_set", KKEY, KM_PRESS, KM_SHIFT, 0);
292
293         ed_keymap_paint_brush_switch(keymap, "tool_settings.weight_paint.active_brush_index");
294         ed_keymap_paint_brush_size(keymap, "tool_settings.weight_paint.brush.size");
295
296         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
297         RNA_string_set(kmi->ptr, "data_path", "weight_paint_object.data.use_paint_mask");
298
299         WM_keymap_verify_item(keymap, "PAINT_OT_weight_from_bones", WKEY, KM_PRESS, 0, 0);
300
301         /* Image/Texture Paint mode */
302         keymap= WM_keymap_find(keyconf, "Image Paint", 0, 0);
303         keymap->poll= image_texture_paint_poll;
304
305         RNA_enum_set(WM_keymap_add_item(keymap, "PAINT_OT_texture_paint_radial_control", FKEY, KM_PRESS, 0, 0)->ptr, "mode", WM_RADIALCONTROL_SIZE);
306         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);
307
308         WM_keymap_add_item(keymap, "PAINT_OT_image_paint", LEFTMOUSE, KM_PRESS, 0, 0);
309         WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
310         WM_keymap_add_item(keymap, "PAINT_OT_sample_color", RIGHTMOUSE, KM_PRESS, 0, 0);
311         WM_keymap_add_item(keymap, "PAINT_OT_clone_cursor_set", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
312
313         ed_keymap_paint_brush_switch(keymap, "tool_settings.image_paint.active_brush_index");
314         ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
315
316         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0); /* mask toggle */
317         RNA_string_set(kmi->ptr, "data_path", "texture_paint_object.data.use_paint_mask");
318
319         /* face-mask mode */
320         keymap= WM_keymap_find(keyconf, "Face Mask", 0, 0);
321         keymap->poll= facemask_paint_poll;
322
323         WM_keymap_add_item(keymap, "PAINT_OT_face_select_all", AKEY, KM_PRESS, 0, 0);
324         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
325         WM_keymap_add_item(keymap, "PAINT_OT_face_select_linked_pick", LKEY, KM_PRESS, 0, 0);
326
327 }