Sculpt: fix bug where mouse release event was not caught in some cases.
[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_arithb.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         PaintStroke *stroke = op->customdata;
129
130         /* XXX: can remove the if statement once all modes have this */
131         if(stroke->get_location)
132                 stroke->get_location(C, stroke, center, mouse);
133
134         /* Tablet */
135         if(event->custom == EVT_DATA_TABLET) {
136                 wmTabletData *wmtab= event->customdata;
137                 if(wmtab->Active != EVT_TABLET_NONE)
138                         pressure= wmtab->Pressure;
139         }
140                                 
141         /* Add to stroke */
142         RNA_collection_add(op->ptr, "stroke", &itemptr);
143         RNA_float_set_array(&itemptr, "location", center);
144         RNA_float_set_array(&itemptr, "mouse", mouse);
145         RNA_boolean_set(&itemptr, "flip", event->shift);
146         RNA_float_set(&itemptr, "pressure", pressure);
147
148         stroke->last_mouse_position[0] = mouse[0];
149         stroke->last_mouse_position[1] = mouse[1];
150
151         stroke->update_step(C, stroke, &itemptr);
152 }
153
154 /* Returns zero if no sculpt changes should be made, non-zero otherwise */
155 static int paint_smooth_stroke(PaintStroke *stroke, float output[2], wmEvent *event)
156 {
157         output[0] = event->x;
158         output[1] = event->y;
159
160         if(stroke->brush->flag & BRUSH_SMOOTH_STROKE && stroke->brush->sculpt_tool != SCULPT_TOOL_GRAB) {
161                 float u = stroke->brush->smooth_stroke_factor, v = 1.0 - u;
162                 float dx = stroke->last_mouse_position[0] - event->x, dy = stroke->last_mouse_position[1] - event->y;
163
164                 /* If the mouse is moving within the radius of the last move,
165                    don't update the mouse position. This allows sharp turns. */
166                 if(dx*dx + dy*dy < stroke->brush->smooth_stroke_radius * stroke->brush->smooth_stroke_radius)
167                         return 0;
168
169                 output[0] = event->x * v + stroke->last_mouse_position[0] * u;
170                 output[1] = event->y * v + stroke->last_mouse_position[1] * u;
171         }
172
173         return 1;
174 }
175
176 /* Returns zero if the stroke dots should not be spaced, non-zero otherwise */
177 static int paint_space_stroke_enabled(Brush *br)
178 {
179         return (br->flag & BRUSH_SPACE) && !(br->flag & BRUSH_ANCHORED) && (br->sculpt_tool != SCULPT_TOOL_GRAB);
180 }
181
182 /* For brushes with stroke spacing enabled, moves mouse in steps
183    towards the final mouse location. */
184 static int paint_space_stroke(bContext *C, wmOperator *op, wmEvent *event, const float final_mouse[2])
185 {
186         PaintStroke *stroke = op->customdata;
187         int cnt = 0;
188
189         if(paint_space_stroke_enabled(stroke->brush)) {
190                 float mouse[2] = {stroke->last_mouse_position[0], stroke->last_mouse_position[1]};
191                 float vec[2] = {final_mouse[0] - mouse[0], final_mouse[1] - mouse[1]};
192                 float length, scale;
193                 int steps = 0, i;
194
195                 /* Normalize the vector between the last stroke dot and the goal */
196                 length = sqrt(vec[0]*vec[0] + vec[1]*vec[1]);
197
198                 if(length > FLT_EPSILON) {
199                         scale = stroke->brush->spacing / length;
200                         vec[0] *= scale;
201                         vec[1] *= scale;
202
203                         steps = (int)(length / stroke->brush->spacing);
204                         for(i = 0; i < steps; ++i, ++cnt) {
205                                 mouse[0] += vec[0];
206                                 mouse[1] += vec[1];
207                                 paint_brush_stroke_add_step(C, op, event, mouse);
208                         }
209                 }
210         }
211
212         return cnt;
213 }
214
215 /**** Public API ****/
216
217 PaintStroke *paint_stroke_new(bContext *C,
218                               StrokeGetLocation get_location,
219                               StrokeTestStart test_start,
220                               StrokeUpdateStep update_step,
221                               StrokeDone done)
222 {
223         PaintStroke *stroke = MEM_callocN(sizeof(PaintStroke), "PaintStroke");
224
225         stroke->brush = paint_brush(paint_get_active(CTX_data_scene(C)));
226         view3d_set_viewcontext(C, &stroke->vc);
227         view3d_get_transformation(stroke->vc.ar, stroke->vc.rv3d, stroke->vc.obact, &stroke->mats);
228
229         stroke->get_location = get_location;
230         stroke->test_start = test_start;
231         stroke->update_step = update_step;
232         stroke->done = done;
233
234         return stroke;
235 }
236
237 int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event)
238 {
239         PaintStroke *stroke = op->customdata;
240         float mouse[2];
241         int first= 0;
242
243         if(!stroke->stroke_started) {
244                 stroke->last_mouse_position[0] = event->x;
245                 stroke->last_mouse_position[1] = event->y;
246                 stroke->stroke_started = stroke->test_start(C, op, event);
247
248                 if(stroke->stroke_started) {
249                         stroke->smooth_stroke_cursor =
250                                 WM_paint_cursor_activate(CTX_wm_manager(C), paint_poll, paint_draw_smooth_stroke, stroke);
251
252                         if(stroke->brush->flag & BRUSH_AIRBRUSH)
253                                 stroke->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, stroke->brush->rate);
254                 }
255
256                 first= 1;
257                 //ED_region_tag_redraw(ar);
258         }
259
260         /* TODO: fix hardcoded events here */
261         if(event->type == LEFTMOUSE && event->val == KM_RELEASE) {
262                 /* exit stroke, free data */
263                 if(stroke->smooth_stroke_cursor)
264                         WM_paint_cursor_end(CTX_wm_manager(C), stroke->smooth_stroke_cursor);
265
266                 if(stroke->timer)
267                         WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), stroke->timer);
268
269                 stroke->done(C, stroke);
270                 MEM_freeN(stroke);
271                 return OPERATOR_FINISHED;
272         }
273         else if(first || event->type == MOUSEMOVE || (event->type == TIMER && (event->customdata == stroke->timer))) {
274                 if(stroke->stroke_started) {
275                         if(paint_smooth_stroke(stroke, mouse, event)) {
276                                 if(paint_space_stroke_enabled(stroke->brush)) {
277                                         if(!paint_space_stroke(C, op, event, mouse))
278                                                 ;//ED_region_tag_redraw(ar);
279                                 }
280                                 else
281                                         paint_brush_stroke_add_step(C, op, event, mouse);
282                         }
283                         else
284                                 ;//ED_region_tag_redraw(ar);
285                 }
286         }
287
288         return OPERATOR_RUNNING_MODAL;
289 }
290
291 int paint_stroke_exec(bContext *C, wmOperator *op)
292 {
293         PaintStroke *stroke = op->customdata;
294
295         RNA_BEGIN(op->ptr, itemptr, "stroke") {
296                 stroke->update_step(C, stroke, &itemptr);
297         }
298         RNA_END;
299
300         MEM_freeN(stroke);
301         op->customdata = NULL;
302
303         return OPERATOR_FINISHED;
304 }
305
306 ViewContext *paint_stroke_view_context(PaintStroke *stroke)
307 {
308         return &stroke->vc;
309 }
310
311 void *paint_stroke_mode_data(struct PaintStroke *stroke)
312 {
313         return stroke->mode_data;
314 }
315
316 void paint_stroke_set_mode_data(PaintStroke *stroke, void *mode_data)
317 {
318         stroke->mode_data = mode_data;
319 }
320
321 int paint_poll(bContext *C)
322 {
323         Paint *p = paint_get_active(CTX_data_scene(C));
324         Object *ob = CTX_data_active_object(C);
325
326         return p && ob && paint_brush(p) &&
327                 CTX_wm_area(C)->spacetype == SPACE_VIEW3D &&
328                 CTX_wm_region(C)->regiontype == RGN_TYPE_WINDOW;
329 }
330
331 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
332 {
333         Paint *p = paint_get_active(CTX_data_scene(C));
334
335         if(p && !p->paint_cursor)
336                 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
337 }
338