Sculpt Branch:
[blender.git] / source / blender / editors / sculpt_paint / paint_stroke.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2009 by Nicholas Bishop
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29 #include "MEM_guardedalloc.h"
30
31 #include "DNA_brush_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
35
36 #include "RNA_access.h"
37
38 #include "BKE_context.h"
39 #include "BKE_paint.h"
40
41 #include "WM_api.h"
42 #include "WM_types.h"
43
44 #include "BLI_math.h"
45
46 #include "PIL_time.h"
47
48 #include "BIF_gl.h"
49 #include "BIF_glutil.h"
50
51 #include "ED_screen.h"
52 #include "ED_view3d.h"
53
54 #include "paint_intern.h"
55
56 #include <float.h>
57 #include <math.h>
58
59 typedef struct PaintStroke {
60         void *mode_data;
61         void *smooth_stroke_cursor;
62         wmTimer *timer;
63
64         /* Cached values */
65         ViewContext vc;
66         bglMats mats;
67         Brush *brush;
68
69         float last_mouse_position[2];
70
71         /* Set whether any stroke step has yet occured
72            e.g. in sculpt mode, stroke doesn't start until cursor
73            passes over the mesh */
74         int stroke_started;
75
76         StrokeGetLocation get_location;
77         StrokeTestStart test_start;
78         StrokeUpdateStep update_step;
79         StrokeDone done;
80 } PaintStroke;
81
82 /*** Cursor ***/
83 static void paint_draw_smooth_stroke(bContext *C, int x, int y, void *customdata) 
84 {
85         Brush *brush = paint_brush(paint_get_active(CTX_data_scene(C)));
86         PaintStroke *stroke = customdata;
87
88         glColor4ubv(paint_get_active(CTX_data_scene(C))->paint_cursor_col);
89         glEnable(GL_LINE_SMOOTH);
90         glEnable(GL_BLEND);
91
92         if(stroke && brush && (brush->flag & BRUSH_SMOOTH_STROKE)) {
93                 ARegion *ar = CTX_wm_region(C);
94                 sdrawline(x, y, (int)stroke->last_mouse_position[0] - ar->winrct.xmin,
95                           (int)stroke->last_mouse_position[1] - ar->winrct.ymin);
96         }
97
98         glDisable(GL_BLEND);
99         glDisable(GL_LINE_SMOOTH);
100 }
101
102 static void paint_draw_cursor(bContext *C, int x, int y, void *customdata)
103 {
104         Paint *paint = paint_get_active(CTX_data_scene(C));
105         Brush *brush = paint_brush(paint);
106
107         if(!(paint->flags & PAINT_SHOW_BRUSH))
108                 return;
109
110         glColor4ubv(paint_get_active(CTX_data_scene(C))->paint_cursor_col);
111         glEnable(GL_LINE_SMOOTH);
112         glEnable(GL_BLEND);
113
114         glTranslatef((float)x, (float)y, 0.0f);
115         glutil_draw_lined_arc(0.0, M_PI*2.0, brush->size, 40);
116         glTranslatef((float)-x, (float)-y, 0.0f);
117
118         glDisable(GL_BLEND);
119         glDisable(GL_LINE_SMOOTH);
120 }
121
122 /* Put the location of the next stroke dot into the stroke RNA and apply it to the mesh */
123 static void paint_brush_stroke_add_step(bContext *C, wmOperator *op, wmEvent *event, float mouse[2])
124 {
125         PointerRNA itemptr;
126         float pressure = 1;
127         float center[3] = {0, 0, 0};
128         int flip= event->shift?1:0;
129         PaintStroke *stroke = op->customdata;
130
131         /* XXX: can remove the if statement once all modes have this */
132         if(stroke->get_location)
133                 stroke->get_location(C, stroke, center, mouse);
134
135         /* Tablet */
136         if(event->custom == EVT_DATA_TABLET) {
137                 wmTabletData *wmtab= event->customdata;
138                 if(wmtab->Active != EVT_TABLET_NONE)
139                         pressure= wmtab->Pressure;
140                 if(wmtab->Active == EVT_TABLET_ERASER)
141                         flip = 1;
142         }
143                                 
144         /* Add to stroke */
145         RNA_collection_add(op->ptr, "stroke", &itemptr);
146         RNA_float_set_array(&itemptr, "location", center);
147         RNA_float_set_array(&itemptr, "mouse", mouse);
148         RNA_boolean_set(&itemptr, "flip", flip);
149         RNA_float_set(&itemptr, "pressure", pressure);
150
151         stroke->last_mouse_position[0] = mouse[0];
152         stroke->last_mouse_position[1] = mouse[1];
153
154         stroke->update_step(C, stroke, &itemptr);
155 }
156
157 /* Returns zero if no sculpt changes should be made, non-zero otherwise */
158 static int paint_smooth_stroke(PaintStroke *stroke, float output[2], wmEvent *event)
159 {
160         output[0] = event->x;
161         output[1] = event->y;
162
163         if(stroke->brush->flag & BRUSH_SMOOTH_STROKE && stroke->brush->sculpt_tool != SCULPT_TOOL_GRAB) {
164                 float u = stroke->brush->smooth_stroke_factor, v = 1.0 - u;
165                 float dx = stroke->last_mouse_position[0] - event->x, dy = stroke->last_mouse_position[1] - event->y;
166
167                 /* If the mouse is moving within the radius of the last move,
168                    don't update the mouse position. This allows sharp turns. */
169                 if(dx*dx + dy*dy < stroke->brush->smooth_stroke_radius * stroke->brush->smooth_stroke_radius)
170                         return 0;
171
172                 output[0] = event->x * v + stroke->last_mouse_position[0] * u;
173                 output[1] = event->y * v + stroke->last_mouse_position[1] * u;
174         }
175
176         return 1;
177 }
178
179 /* Returns zero if the stroke dots should not be spaced, non-zero otherwise */
180 static int paint_space_stroke_enabled(Brush *br)
181 {
182         return (br->flag & BRUSH_SPACE) && !(br->flag & BRUSH_ANCHORED) && (br->sculpt_tool != SCULPT_TOOL_GRAB);
183 }
184
185 /* For brushes with stroke spacing enabled, moves mouse in steps
186    towards the final mouse location. */
187 static int paint_space_stroke(bContext *C, wmOperator *op, wmEvent *event, const float final_mouse[2])
188 {
189         PaintStroke *stroke = op->customdata;
190         int cnt = 0;
191
192         if(paint_space_stroke_enabled(stroke->brush)) {
193                 float mouse[2] = {stroke->last_mouse_position[0], stroke->last_mouse_position[1]};
194                 float vec[2] = {final_mouse[0] - mouse[0], final_mouse[1] - mouse[1]};
195                 float length, scale;
196                 int steps = 0, i;
197
198                 /* Normalize the vector between the last stroke dot and the goal */
199                 length = sqrt(vec[0]*vec[0] + vec[1]*vec[1]);
200
201                 if(length > FLT_EPSILON) {
202                         scale = stroke->brush->spacing / length;
203                         vec[0] *= scale;
204                         vec[1] *= scale;
205
206                         steps = (int)(length / stroke->brush->spacing);
207                         for(i = 0; i < steps; ++i, ++cnt) {
208                                 mouse[0] += vec[0];
209                                 mouse[1] += vec[1];
210                                 paint_brush_stroke_add_step(C, op, event, mouse);
211                         }
212                 }
213         }
214
215         return cnt;
216 }
217
218 /**** Public API ****/
219
220 PaintStroke *paint_stroke_new(bContext *C,
221                               StrokeGetLocation get_location,
222                               StrokeTestStart test_start,
223                               StrokeUpdateStep update_step,
224                               StrokeDone done)
225 {
226         PaintStroke *stroke = MEM_callocN(sizeof(PaintStroke), "PaintStroke");
227
228         stroke->brush = paint_brush(paint_get_active(CTX_data_scene(C)));
229         view3d_set_viewcontext(C, &stroke->vc);
230         view3d_get_transformation(stroke->vc.ar, stroke->vc.rv3d, stroke->vc.obact, &stroke->mats);
231
232         stroke->get_location = get_location;
233         stroke->test_start = test_start;
234         stroke->update_step = update_step;
235         stroke->done = done;
236
237         return stroke;
238 }
239
240 int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event)
241 {
242         PaintStroke *stroke = op->customdata;
243         float mouse[2];
244         int first= 0;
245
246         if(!stroke->stroke_started) {
247                 stroke->last_mouse_position[0] = event->x;
248                 stroke->last_mouse_position[1] = event->y;
249                 stroke->stroke_started = stroke->test_start(C, op, event);
250
251                 if(stroke->stroke_started) {
252                         stroke->smooth_stroke_cursor =
253                                 WM_paint_cursor_activate(CTX_wm_manager(C), paint_poll, paint_draw_smooth_stroke, stroke);
254
255                         if(stroke->brush->flag & BRUSH_AIRBRUSH)
256                                 stroke->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, stroke->brush->rate);
257                 }
258
259                 first= 1;
260                 //ED_region_tag_redraw(ar);
261         }
262
263         /* TODO: fix hardcoded events here */
264         if(event->type == LEFTMOUSE && event->val == KM_RELEASE) {
265                 /* exit stroke, free data */
266                 if(stroke->smooth_stroke_cursor)
267                         WM_paint_cursor_end(CTX_wm_manager(C), stroke->smooth_stroke_cursor);
268
269                 if(stroke->timer)
270                         WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), stroke->timer);
271
272                 stroke->done(C, stroke);
273                 MEM_freeN(stroke);
274                 return OPERATOR_FINISHED;
275         }
276         else if(first || event->type == MOUSEMOVE || (event->type == TIMER && (event->customdata == stroke->timer))) {
277                 if(stroke->stroke_started) {
278                         if(paint_smooth_stroke(stroke, mouse, event)) {
279                                 if(paint_space_stroke_enabled(stroke->brush)) {
280                                         if(!paint_space_stroke(C, op, event, mouse))
281                                                 ;//ED_region_tag_redraw(ar);
282                                 }
283                                 else
284                                         paint_brush_stroke_add_step(C, op, event, mouse);
285                         }
286                         else
287                                 ;//ED_region_tag_redraw(ar);
288                 }
289         }
290
291         return OPERATOR_RUNNING_MODAL;
292 }
293
294 int paint_stroke_exec(bContext *C, wmOperator *op)
295 {
296         PaintStroke *stroke = op->customdata;
297
298         RNA_BEGIN(op->ptr, itemptr, "stroke") {
299                 stroke->update_step(C, stroke, &itemptr);
300         }
301         RNA_END;
302
303         MEM_freeN(stroke);
304         op->customdata = NULL;
305
306         return OPERATOR_FINISHED;
307 }
308
309 ViewContext *paint_stroke_view_context(PaintStroke *stroke)
310 {
311         return &stroke->vc;
312 }
313
314 void *paint_stroke_mode_data(struct PaintStroke *stroke)
315 {
316         return stroke->mode_data;
317 }
318
319 void paint_stroke_set_mode_data(PaintStroke *stroke, void *mode_data)
320 {
321         stroke->mode_data = mode_data;
322 }
323
324 int paint_poll(bContext *C)
325 {
326         Paint *p = paint_get_active(CTX_data_scene(C));
327         Object *ob = CTX_data_active_object(C);
328
329         return p && ob && paint_brush(p) &&
330                 CTX_wm_area(C)->spacetype == SPACE_VIEW3D &&
331                 CTX_wm_region(C)->regiontype == RGN_TYPE_WINDOW;
332 }
333
334 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
335 {
336         Paint *p = paint_get_active(CTX_data_scene(C));
337
338         if(p && !p->paint_cursor)
339                 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
340 }
341