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