2.5/Sculpt:
[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 "BIF_gl.h"
47 #include "BIF_glutil.h"
48
49 #include "ED_screen.h"
50 #include "ED_view3d.h"
51
52 #include "paint_intern.h"
53
54 #include <float.h>
55 #include <math.h>
56
57 typedef struct PaintStroke {
58         void *mode_data;
59         void *smooth_stroke_cursor;
60
61         /* Cached values */
62         ViewContext vc;
63         bglMats mats;
64         Brush *brush;
65
66         float last_mouse_position[2];
67
68         /* Set whether any stroke step has yet occured
69            e.g. in sculpt mode, stroke doesn't start until cursor
70            passes over the mesh */
71         int stroke_started;
72
73         StrokeTestStart test_start;
74         StrokeUpdateStep update_step;
75         StrokeDone done;
76 } PaintStroke;
77
78 /*** Cursor ***/
79 static void paint_draw_smooth_stroke(bContext *C, int x, int y, void *customdata) 
80 {
81         Brush *brush = paint_brush(paint_get_active(CTX_data_scene(C)));
82         PaintStroke *stroke = customdata;
83
84         glColor4ubv(paint_get_active(CTX_data_scene(C))->paint_cursor_col);
85         glEnable(GL_LINE_SMOOTH);
86         glEnable(GL_BLEND);
87
88         if(stroke && brush && (brush->flag & BRUSH_SMOOTH_STROKE)) {
89                 ARegion *ar = CTX_wm_region(C);
90                 sdrawline(x, y, (int)stroke->last_mouse_position[0] - ar->winrct.xmin,
91                           (int)stroke->last_mouse_position[1] - ar->winrct.ymin);
92         }
93
94         glDisable(GL_BLEND);
95         glDisable(GL_LINE_SMOOTH);
96 }
97
98 static void paint_draw_cursor(bContext *C, int x, int y, void *customdata)
99 {
100         Brush *brush = paint_brush(paint_get_active(CTX_data_scene(C)));
101
102         glColor4ubv(paint_get_active(CTX_data_scene(C))->paint_cursor_col);
103         glEnable(GL_LINE_SMOOTH);
104         glEnable(GL_BLEND);
105
106         glTranslatef((float)x, (float)y, 0.0f);
107         glutil_draw_lined_arc(0.0, M_PI*2.0, brush->size, 40);
108         glTranslatef((float)-x, (float)-y, 0.0f);
109
110         glDisable(GL_BLEND);
111         glDisable(GL_LINE_SMOOTH);
112 }
113
114 /* Put the location of the next stroke dot into the stroke RNA and apply it to the mesh */
115 static void paint_brush_stroke_add_step(bContext *C, wmOperator *op, wmEvent *event, float mouse[2])
116 {
117         PointerRNA itemptr;
118         float cur_depth, pressure = 1;
119         float center[3];
120         PaintStroke *stroke = op->customdata;
121
122         cur_depth = read_cached_depth(&stroke->vc, mouse[0], mouse[1]);
123         view3d_unproject(&stroke->mats, center, mouse[0], mouse[1], cur_depth); 
124
125         /* Tablet */
126         if(event->custom == EVT_DATA_TABLET) {
127                 wmTabletData *wmtab= event->customdata;
128                 if(wmtab->Active != EVT_TABLET_NONE)
129                         pressure= wmtab->Pressure;
130         }
131                                 
132         /* Add to stroke */
133         RNA_collection_add(op->ptr, "stroke", &itemptr);
134         RNA_float_set_array(&itemptr, "location", center);
135         RNA_float_set_array(&itemptr, "mouse", mouse);
136         RNA_boolean_set(&itemptr, "flip", event->shift);
137         RNA_float_set(&itemptr, "pressure", pressure);
138
139         stroke->last_mouse_position[0] = mouse[0];
140         stroke->last_mouse_position[1] = mouse[1];
141
142         stroke->update_step(C, stroke, &itemptr);
143 }
144
145 /* Returns zero if no sculpt changes should be made, non-zero otherwise */
146 static int paint_smooth_stroke(PaintStroke *stroke, float output[2], wmEvent *event)
147 {
148         output[0] = event->x;
149         output[1] = event->y;
150
151         if(stroke->brush->flag & BRUSH_SMOOTH_STROKE && stroke->brush->sculpt_tool != SCULPT_TOOL_GRAB) {
152                 float u = stroke->brush->smooth_stroke_factor, v = 1.0 - u;
153                 float dx = stroke->last_mouse_position[0] - event->x, dy = stroke->last_mouse_position[1] - event->y;
154
155                 /* If the mouse is moving within the radius of the last move,
156                    don't update the mouse position. This allows sharp turns. */
157                 if(dx*dx + dy*dy < stroke->brush->smooth_stroke_radius * stroke->brush->smooth_stroke_radius)
158                         return 0;
159
160                 output[0] = event->x * v + stroke->last_mouse_position[0] * u;
161                 output[1] = event->y * v + stroke->last_mouse_position[1] * u;
162         }
163
164         return 1;
165 }
166
167 /* Returns zero if the stroke dots should not be spaced, non-zero otherwise */
168 static int paint_space_stroke_enabled(Brush *br)
169 {
170         return (br->flag & BRUSH_SPACE) && !(br->flag & BRUSH_ANCHORED) && (br->sculpt_tool != SCULPT_TOOL_GRAB);
171 }
172
173 /* For brushes with stroke spacing enabled, moves mouse in steps
174    towards the final mouse location. */
175 static int paint_space_stroke(bContext *C, wmOperator *op, wmEvent *event, const float final_mouse[2])
176 {
177         PaintStroke *stroke = op->customdata;
178         int cnt = 0;
179
180         if(paint_space_stroke_enabled(stroke->brush)) {
181                 float mouse[2] = {stroke->last_mouse_position[0], stroke->last_mouse_position[1]};
182                 float vec[2] = {final_mouse[0] - mouse[0], final_mouse[1] - mouse[1]};
183                 float length, scale;
184                 int steps = 0, i;
185
186                 /* Normalize the vector between the last stroke dot and the goal */
187                 length = sqrt(vec[0]*vec[0] + vec[1]*vec[1]);
188
189                 if(length > FLT_EPSILON) {
190                         scale = stroke->brush->spacing / length;
191                         vec[0] *= scale;
192                         vec[1] *= scale;
193
194                         steps = (int)(length / stroke->brush->spacing);
195                         for(i = 0; i < steps; ++i, ++cnt) {
196                                 mouse[0] += vec[0];
197                                 mouse[1] += vec[1];
198                                 paint_brush_stroke_add_step(C, op, event, mouse);
199                         }
200                 }
201         }
202
203         return cnt;
204 }
205
206 /**** Public API ****/
207
208 PaintStroke *paint_stroke_new(bContext *C, StrokeTestStart test_start,
209                               StrokeUpdateStep update_step, StrokeDone done)
210 {
211         PaintStroke *stroke = MEM_callocN(sizeof(PaintStroke), "PaintStroke");
212
213         stroke->brush = paint_brush(paint_get_active(CTX_data_scene(C)));
214         view3d_set_viewcontext(C, &stroke->vc);
215         view3d_get_transformation(&stroke->vc, stroke->vc.obact, &stroke->mats);
216
217         stroke->test_start = test_start;
218         stroke->update_step = update_step;
219         stroke->done = done;
220
221         return stroke;
222 }
223
224 int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event)
225 {
226         ARegion *ar = CTX_wm_region(C);
227         PaintStroke *stroke = op->customdata;
228         float mouse[2];
229
230         if(!stroke->stroke_started) {
231                 stroke->last_mouse_position[0] = event->x;
232                 stroke->last_mouse_position[1] = event->y;
233                 stroke->stroke_started = stroke->test_start(C, op, event);
234
235                 if(stroke->stroke_started)
236                         stroke->smooth_stroke_cursor =
237                                 WM_paint_cursor_activate(CTX_wm_manager(C), paint_poll, paint_draw_smooth_stroke, stroke);
238
239                 ED_region_tag_redraw(ar);
240         }
241
242         if(stroke->stroke_started) {
243                 if(paint_smooth_stroke(stroke, mouse, event)) {
244                         if(paint_space_stroke_enabled(stroke->brush)) {
245                                 if(!paint_space_stroke(C, op, event, mouse))
246                                         ED_region_tag_redraw(ar);
247                         }
248                         else
249                                 paint_brush_stroke_add_step(C, op, event, mouse);
250                 }
251                 else
252                         ED_region_tag_redraw(ar);
253         }
254
255         /* TODO: fix hardcoded event here */
256         if(event->type == LEFTMOUSE && event->val == 0) {
257                 if(stroke->smooth_stroke_cursor)
258                         WM_paint_cursor_end(CTX_wm_manager(C), stroke->smooth_stroke_cursor);
259
260                 stroke->done(C, stroke);
261                 MEM_freeN(stroke);
262                 return OPERATOR_FINISHED;
263         }
264         else
265                 return OPERATOR_RUNNING_MODAL;
266 }
267
268 int paint_stroke_exec(bContext *C, wmOperator *op)
269 {
270         PaintStroke *stroke = op->customdata;
271
272         RNA_BEGIN(op->ptr, itemptr, "stroke") {
273                 stroke->update_step(C, stroke, &itemptr);
274         }
275         RNA_END;
276
277         MEM_freeN(stroke);
278         op->customdata = NULL;
279
280         return OPERATOR_FINISHED;
281 }
282
283 ViewContext *paint_stroke_view_context(PaintStroke *stroke)
284 {
285         return &stroke->vc;
286 }
287
288 void *paint_stroke_mode_data(struct PaintStroke *stroke)
289 {
290         return stroke->mode_data;
291 }
292
293 void paint_stroke_set_mode_data(PaintStroke *stroke, void *mode_data)
294 {
295         stroke->mode_data = mode_data;
296 }
297
298 int paint_poll(bContext *C)
299 {
300         Paint *p = paint_get_active(CTX_data_scene(C));
301         Object *ob = CTX_data_active_object(C);
302
303         return p && ob && paint_brush(p) &&
304                 CTX_wm_area(C)->spacetype == SPACE_VIEW3D &&
305                 CTX_wm_region(C)->regiontype == RGN_TYPE_WINDOW;
306 }
307
308 void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
309 {
310         Paint *p = paint_get_active(CTX_data_scene(C));
311
312         if(p && !p->paint_cursor)
313                 p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
314 }
315