Merge branch 'master' into blender2.8
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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): Jason Wilkins, Tom Musgrove.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29 /** \file blender/editors/sculpt_paint/paint_stroke.c
30  *  \ingroup edsculpt
31  */
32
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_math.h"
37 #include "BLI_utildefines.h"
38 #include "BLI_rand.h"
39 #include "BLI_listbase.h"
40
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_brush_types.h"
44 #include "DNA_curve_types.h"
45
46 #include "RNA_access.h"
47
48 #include "BKE_context.h"
49 #include "BKE_paint.h"
50 #include "BKE_brush.h"
51 #include "BKE_curve.h"
52 #include "BKE_colortools.h"
53 #include "BKE_image.h"
54 #include "BKE_mesh.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61
62 #include "GPU_immediate.h"
63
64 #include "ED_screen.h"
65 #include "ED_view3d.h"
66
67 #include "IMB_imbuf_types.h"
68
69 #include "paint_intern.h"
70
71 #include <float.h>
72 #include <math.h>
73
74 //#define DEBUG_TIME
75
76 #ifdef DEBUG_TIME
77 #  include "PIL_time_utildefines.h"
78 #endif
79
80 typedef struct PaintSample {
81         float mouse[2];
82         float pressure;
83 } PaintSample;
84
85 typedef struct PaintStroke {
86         void *mode_data;
87         void *stroke_cursor;
88         wmTimer *timer;
89
90         /* Cached values */
91         ViewContext vc;
92         Brush *brush;
93         UnifiedPaintSettings *ups;
94
95         /* used for lines and curves */
96         ListBase line;
97
98         /* Paint stroke can use up to PAINT_MAX_INPUT_SAMPLES prior inputs
99          * to smooth the stroke */
100         PaintSample samples[PAINT_MAX_INPUT_SAMPLES];
101         int num_samples;
102         int cur_sample;
103
104         float last_mouse_position[2];
105         /* space distance covered so far */
106         float stroke_distance;
107
108         /* Set whether any stroke step has yet occurred
109          * e.g. in sculpt mode, stroke doesn't start until cursor
110          * passes over the mesh */
111         bool stroke_started;
112         /* Set when enough motion was found for rake rotation */
113         bool rake_started;
114         /* event that started stroke, for modal() return */
115         int event_type;
116         /* check if stroke variables have been initialized */
117         bool stroke_init;
118         /* check if various brush mapping variables have been initialized */
119         bool brush_init;
120         float initial_mouse[2];
121         /* cached_pressure stores initial pressure for size pressure influence mainly */
122         float cached_size_pressure;
123         /* last pressure will store last pressure value for use in interpolation for space strokes */
124         float last_pressure;
125         int stroke_mode;
126
127         float zoom_2d;
128         int pen_flip;
129         
130         /* line constraint */
131         bool constrain_line;
132         float constrained_pos[2];
133
134         StrokeGetLocation get_location;
135         StrokeTestStart test_start;
136         StrokeUpdateStep update_step;
137         StrokeRedraw redraw;
138         StrokeDone done;
139 } PaintStroke;
140
141 /*** Cursors ***/
142 static void paint_draw_smooth_cursor(bContext *C, int x, int y, void *customdata)
143 {
144         Paint *paint = BKE_paint_get_active_from_context(C);
145         Brush *brush = BKE_paint_brush(paint);
146         PaintStroke *stroke = customdata;
147
148         if (stroke && brush) {
149                 glEnable(GL_LINE_SMOOTH);
150                 glEnable(GL_BLEND);
151
152                 ARegion *ar = stroke->vc.ar;
153
154                 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
155                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
156                 immUniformColor4ubv(paint->paint_cursor_col);
157
158                 immBegin(GWN_PRIM_LINES, 2);
159                 immVertex2f(pos, x, y);
160                 immVertex2f(pos,
161                             stroke->last_mouse_position[0] + ar->winrct.xmin,
162                             stroke->last_mouse_position[1] + ar->winrct.ymin);
163
164                 immEnd();
165
166                 immUnbindProgram();
167
168                 glDisable(GL_BLEND);
169                 glDisable(GL_LINE_SMOOTH);
170         }
171 }
172
173 static void paint_draw_line_cursor(bContext *C, int x, int y, void *customdata)
174 {
175         Paint *paint = BKE_paint_get_active_from_context(C);
176         PaintStroke *stroke = customdata;
177
178         glEnable(GL_LINE_SMOOTH);
179
180         uint shdr_pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
181
182         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
183
184         float viewport_size[4];
185         glGetFloatv(GL_VIEWPORT, viewport_size);
186         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
187
188         immUniform1i("num_colors", 2);  /* "advanced" mode */
189         const float alpha = (float)paint->paint_cursor_col[3] / 255.0f;
190         immUniformArray4fv("colors", (float *)(float[][4]){{0.0f, 0.0f, 0.0f, alpha}, {1.0f, 1.0f, 1.0f, alpha}}, 2);
191         immUniform1f("dash_width", 6.0f);
192
193         immBegin(GWN_PRIM_LINES, 2);
194
195         ARegion *ar = stroke->vc.ar;
196
197         if (stroke->constrain_line) {
198                 immVertex2f(shdr_pos,
199                             stroke->last_mouse_position[0] + ar->winrct.xmin,
200                             stroke->last_mouse_position[1] + ar->winrct.ymin);
201
202                 immVertex2f(shdr_pos,
203                             stroke->constrained_pos[0] + ar->winrct.xmin,
204                             stroke->constrained_pos[1] + ar->winrct.ymin);
205         }
206         else {
207                 immVertex2f(shdr_pos,
208                             stroke->last_mouse_position[0] + ar->winrct.xmin,
209                             stroke->last_mouse_position[1] + ar->winrct.ymin);
210
211                 immVertex2f(shdr_pos, x, y);
212         }
213
214         immEnd();
215
216         immUnbindProgram();
217
218         glDisable(GL_LINE_SMOOTH);
219 }
220
221 static bool paint_tool_require_location(Brush *brush, ePaintMode mode)
222 {
223         switch (mode) {
224                 case ePaintSculpt:
225                         if (ELEM(brush->sculpt_tool, SCULPT_TOOL_GRAB, SCULPT_TOOL_ROTATE,
226                                                      SCULPT_TOOL_SNAKE_HOOK, SCULPT_TOOL_THUMB))
227                         {
228                                 return false;
229                         }
230                         else {
231                                 return true;
232                         }
233                 default:
234                         break;
235         }
236
237         return true;
238 }
239
240 /* Initialize the stroke cache variants from operator properties */
241 static bool paint_brush_update(bContext *C,
242                                Brush *brush,
243                                ePaintMode mode,
244                                struct PaintStroke *stroke,
245                                const float mouse_init[2],
246                                float mouse[2], float pressure,
247                                float location[3])
248 {
249         Scene *scene = CTX_data_scene(C);
250         UnifiedPaintSettings *ups = stroke->ups;
251         bool location_sampled = false;
252         bool location_success = false;
253         /* Use to perform all operations except applying the stroke,
254          * needed for operations that require cursor motion (rake). */
255         bool is_dry_run = false;
256         bool do_random = false;
257         bool do_random_mask = false;
258         /* XXX: Use pressure value from first brush step for brushes which don't
259          *      support strokes (grab, thumb). They depends on initial state and
260          *      brush coord/pressure/etc.
261          *      It's more an events design issue, which doesn't split coordinate/pressure/angle
262          *      changing events. We should avoid this after events system re-design */
263         if (!stroke->brush_init) {
264                 copy_v2_v2(stroke->initial_mouse, mouse);
265                 copy_v2_v2(ups->last_rake, mouse);
266                 copy_v2_v2(ups->tex_mouse, mouse);
267                 copy_v2_v2(ups->mask_tex_mouse, mouse);
268                 stroke->cached_size_pressure = pressure;
269
270                 ups->do_linear_conversion = false;
271                 ups->colorspace = NULL;
272
273                 /* check here if color sampling the main brush should do color conversion. This is done here
274                  * to avoid locking up to get the image buffer during sampling */
275                 if (brush->mtex.tex && brush->mtex.tex->type == TEX_IMAGE && brush->mtex.tex->ima) {
276                         ImBuf *tex_ibuf = BKE_image_pool_acquire_ibuf(brush->mtex.tex->ima, &brush->mtex.tex->iuser, NULL);
277                         if (tex_ibuf && tex_ibuf->rect_float == NULL) {
278                                 ups->do_linear_conversion = true;
279                                 ups->colorspace = tex_ibuf->rect_colorspace;
280                         }
281                         BKE_image_pool_release_ibuf(brush->mtex.tex->ima, tex_ibuf, NULL);
282                 }
283
284                 stroke->brush_init = true;
285         }
286
287         if (paint_supports_dynamic_size(brush, mode)) {
288                 copy_v2_v2(ups->tex_mouse, mouse);
289                 copy_v2_v2(ups->mask_tex_mouse, mouse);
290                 stroke->cached_size_pressure = pressure;
291         }
292
293         /* Truly temporary data that isn't stored in properties */
294
295         ups->stroke_active = true;
296         ups->size_pressure_value = stroke->cached_size_pressure;
297
298         ups->pixel_radius = BKE_brush_size_get(scene, brush);
299
300         if (BKE_brush_use_size_pressure(scene, brush) && paint_supports_dynamic_size(brush, mode)) {
301                 ups->pixel_radius *= stroke->cached_size_pressure;
302         }
303
304         if (paint_supports_dynamic_tex_coords(brush, mode)) {
305
306                 if (ELEM(brush->mtex.brush_map_mode,
307                          MTEX_MAP_MODE_VIEW,
308                          MTEX_MAP_MODE_AREA,
309                          MTEX_MAP_MODE_RANDOM))
310                 {
311                         do_random = true;
312                 }
313
314                 if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_RANDOM)
315                         BKE_brush_randomize_texture_coords(ups, false);
316                 else {
317                         copy_v2_v2(ups->tex_mouse, mouse);
318                 }
319
320                 /* take care of mask texture, if any */
321                 if (brush->mask_mtex.tex) {
322
323                         if (ELEM(brush->mask_mtex.brush_map_mode,
324                                  MTEX_MAP_MODE_VIEW,
325                                  MTEX_MAP_MODE_AREA,
326                                  MTEX_MAP_MODE_RANDOM))
327                         {
328                                 do_random_mask = true;
329                         }
330
331                         if (brush->mask_mtex.brush_map_mode == MTEX_MAP_MODE_RANDOM)
332                                 BKE_brush_randomize_texture_coords(ups, true);
333                         else {
334                                 copy_v2_v2(ups->mask_tex_mouse, mouse);
335                         }
336                 }
337         }
338
339
340         if (brush->flag & BRUSH_ANCHORED) {
341                 bool hit = false;
342                 float halfway[2];
343
344                 const float dx = mouse[0] - stroke->initial_mouse[0];
345                 const float dy = mouse[1] - stroke->initial_mouse[1];
346
347                 ups->anchored_size = ups->pixel_radius = sqrtf(dx * dx + dy * dy);
348
349                 ups->brush_rotation = ups->brush_rotation_sec = atan2f(dx, dy) + (float)M_PI;
350
351                 if (brush->flag & BRUSH_EDGE_TO_EDGE) {
352                         halfway[0] = dx * 0.5f + stroke->initial_mouse[0];
353                         halfway[1] = dy * 0.5f + stroke->initial_mouse[1];
354
355                         if (stroke->get_location) {
356                                 if (stroke->get_location(C, location, halfway)) {
357                                         hit = true;
358                                         location_sampled = true;
359                                         location_success = true;
360                                 }
361                                 else if (!paint_tool_require_location(brush, mode)) {
362                                         hit = true;
363                                 }
364                         }
365                         else {
366                                 hit = true;
367                         }
368                 }
369                 if (hit) {
370                         copy_v2_v2(ups->anchored_initial_mouse, halfway);
371                         copy_v2_v2(ups->tex_mouse, halfway);
372                         copy_v2_v2(ups->mask_tex_mouse, halfway);
373                         copy_v2_v2(mouse, halfway);
374                         ups->anchored_size /= 2.0f;
375                         ups->pixel_radius  /= 2.0f;
376                         stroke->stroke_distance = ups->pixel_radius;
377                 }
378                 else {
379                         copy_v2_v2(ups->anchored_initial_mouse, stroke->initial_mouse);
380                         copy_v2_v2(mouse, stroke->initial_mouse);
381                         stroke->stroke_distance = ups->pixel_radius;
382                 }
383                 ups->pixel_radius /= stroke->zoom_2d;
384                 ups->draw_anchored = true;
385         }
386         else {
387                 /* here we are using the initial mouse coordinate because we do not want the rake
388                  * result to depend on jittering */
389                 if (!stroke->brush_init) {
390                         copy_v2_v2(ups->last_rake, mouse_init);
391                 }
392                 /* curve strokes do their own rake calculation */
393                 else if (!(brush->flag & BRUSH_CURVE)) {
394                         if (!paint_calculate_rake_rotation(ups, brush, mouse_init)) {
395                                 /* Not enough motion to define an angle. */
396                                 if (!stroke->rake_started) {
397                                         is_dry_run = true;
398                                 }
399                         }
400                         else {
401                                 stroke->rake_started = true;
402                         }
403                 }
404         }
405
406         if (do_random) {
407                 if (brush->mtex.brush_angle_mode & MTEX_ANGLE_RANDOM) {
408                         ups->brush_rotation += -brush->mtex.random_angle / 2.0f +
409                                                brush->mtex.random_angle * BLI_frand();
410                 }
411         }
412
413         if (do_random_mask) {
414                 if (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RANDOM) {
415                         ups->brush_rotation_sec += -brush->mask_mtex.random_angle / 2.0f +
416                                                    brush->mask_mtex.random_angle * BLI_frand();
417                 }
418         }
419
420         if (!location_sampled) {
421                 if (stroke->get_location) {
422                         if (stroke->get_location(C, location, mouse))
423                                 location_success = true;
424                         else if (!paint_tool_require_location(brush, mode))
425                                 location_success = true;
426                 }
427                 else {
428                         zero_v3(location);
429                         location_success = true;
430                 }
431         }
432
433         return location_success && (is_dry_run == false);
434 }
435
436 static bool paint_stroke_use_jitter(ePaintMode mode, Brush *brush, bool invert)
437 {
438         bool use_jitter = (brush->flag & BRUSH_ABSOLUTE_JITTER) ?
439                 (brush->jitter_absolute != 0) : (brush->jitter != 0);
440
441         /* jitter-ed brush gives weird and unpredictable result for this
442          * kinds of stroke, so manually disable jitter usage (sergey) */
443         use_jitter &= (brush->flag & (BRUSH_DRAG_DOT | BRUSH_ANCHORED)) == 0;
444         use_jitter &= (!ELEM(mode, ePaintTexture2D, ePaintTextureProjective) ||
445                        !(invert && brush->imagepaint_tool == PAINT_TOOL_CLONE));
446
447
448         return use_jitter;
449 }
450
451 /* Put the location of the next stroke dot into the stroke RNA and apply it to the mesh */
452 static void paint_brush_stroke_add_step(bContext *C, wmOperator *op, const float mouse_in[2], float pressure)
453 {
454         Scene *scene = CTX_data_scene(C);
455         Paint *paint = BKE_paint_get_active_from_context(C);
456         ePaintMode mode = BKE_paintmode_get_active_from_context(C);
457         Brush *brush = BKE_paint_brush(paint);
458         PaintStroke *stroke = op->customdata;
459         UnifiedPaintSettings *ups = stroke->ups;
460         float mouse_out[2];
461         PointerRNA itemptr;
462         float location[3];
463
464 /* the following code is adapted from texture paint. It may not be needed but leaving here
465  * just in case for reference (code in texpaint removed as part of refactoring).
466  * It's strange that only texpaint had these guards. */
467 #if 0
468         /* special exception here for too high pressure values on first touch in
469          * windows for some tablets, then we just skip first touch ..  */
470         if (tablet && (pressure >= 0.99f) && ((pop->s.brush->flag & BRUSH_SPACING_PRESSURE) || BKE_brush_use_alpha_pressure(scene, pop->s.brush) || BKE_brush_use_size_pressure(scene, pop->s.brush)))
471                 return;
472
473         /* This can be removed once fixed properly in
474          * BKE_brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
475          * at zero pressure we should do nothing 1/2^12 is 0.0002 which is the sensitivity of the most sensitive pen tablet available */
476         if (tablet && (pressure < 0.0002f) && ((pop->s.brush->flag & BRUSH_SPACING_PRESSURE) || BKE_brush_use_alpha_pressure(scene, pop->s.brush) || BKE_brush_use_size_pressure(scene, pop->s.brush)))
477                 return;
478 #endif
479
480         /* copy last position -before- jittering, or space fill code
481          * will create too many dabs */
482         copy_v2_v2(stroke->last_mouse_position, mouse_in);
483         stroke->last_pressure = pressure;
484
485         if (paint_stroke_use_jitter(mode, brush, stroke->stroke_mode == BRUSH_STROKE_INVERT)) {
486                 float delta[2];
487                 float factor = stroke->zoom_2d;
488
489                 if (brush->flag & BRUSH_JITTER_PRESSURE)
490                         factor *= pressure;
491
492                 BKE_brush_jitter_pos(scene, brush, mouse_in, mouse_out);
493
494                 /* XXX: meh, this is round about because
495                  * BKE_brush_jitter_pos isn't written in the best way to
496                  * be reused here */
497                 if (factor != 1.0f) {
498                         sub_v2_v2v2(delta, mouse_out, mouse_in);
499                         mul_v2_fl(delta, factor);
500                         add_v2_v2v2(mouse_out, mouse_in, delta);
501                 }
502         }
503         else {
504                 copy_v2_v2(mouse_out, mouse_in);
505         }
506
507
508         ups->last_hit = paint_brush_update(C, brush, mode, stroke, mouse_in, mouse_out, pressure, location);
509         copy_v3_v3(ups->last_location, location);
510         if (!ups->last_hit) {
511                 return;
512         }
513
514         /* Add to stroke */
515         RNA_collection_add(op->ptr, "stroke", &itemptr);
516         RNA_float_set(&itemptr, "size", ups->pixel_radius);
517         RNA_float_set_array(&itemptr, "location", location);
518         RNA_float_set_array(&itemptr, "mouse", mouse_out);
519         RNA_boolean_set(&itemptr, "pen_flip", stroke->pen_flip);
520         RNA_float_set(&itemptr, "pressure", pressure);
521
522         stroke->update_step(C, stroke, &itemptr);
523
524         /* don't record this for now, it takes up a lot of memory when doing long
525          * strokes with small brush size, and operators have register disabled */
526         RNA_collection_clear(op->ptr, "stroke");
527 }
528
529 /* Returns zero if no sculpt changes should be made, non-zero otherwise */
530 static bool paint_smooth_stroke(
531         PaintStroke *stroke, const PaintSample *sample, ePaintMode mode,
532         float r_mouse[2], float *r_pressure)
533 {
534         if (paint_supports_smooth_stroke(stroke->brush, mode)) {
535                 float radius = stroke->brush->smooth_stroke_radius * stroke->zoom_2d;
536                 float u = stroke->brush->smooth_stroke_factor;
537
538                 /* If the mouse is moving within the radius of the last move,
539                  * don't update the mouse position. This allows sharp turns. */
540                 if (len_squared_v2v2(stroke->last_mouse_position, sample->mouse) < SQUARE(radius)) {
541                         return false;
542                 }
543
544                 interp_v2_v2v2(r_mouse, sample->mouse, stroke->last_mouse_position, u);
545                 *r_pressure = interpf(sample->pressure, stroke->last_pressure, u);
546         }
547         else {
548                 r_mouse[0] = sample->mouse[0];
549                 r_mouse[1] = sample->mouse[1];
550                 *r_pressure = sample->pressure;
551         }
552
553         return true;
554 }
555
556 static float paint_space_stroke_spacing(const Scene *scene, PaintStroke *stroke, float size_pressure, float spacing_pressure)
557 {
558         /* brushes can have a minimum size of 1.0 but with pressure it can be smaller then a pixel
559          * causing very high step sizes, hanging blender [#32381] */
560         const float size_clamp = max_ff(1.0f, BKE_brush_size_get(scene, stroke->brush) * size_pressure);
561         float spacing = stroke->brush->spacing;
562
563         /* apply spacing pressure */
564         if (stroke->brush->flag & BRUSH_SPACING_PRESSURE)
565                 spacing = spacing * (1.5f - spacing_pressure);
566
567         /* stroke system is used for 2d paint too, so we need to account for
568          * the fact that brush can be scaled there. */
569         spacing *= stroke->zoom_2d;
570
571         return max_ff(1.0, size_clamp * spacing / 50.0f);
572 }
573
574
575
576 static float paint_stroke_overlapped_curve(Brush *br, float x, float spacing)
577 {
578         int i;
579         const int n = 100 / spacing;
580         const float h = spacing / 50.0f;
581         const float x0 = x - 1;
582
583         float sum;
584
585         sum = 0;
586         for (i = 0; i < n; i++) {
587                 float xx;
588
589                 xx = fabsf(x0 + i * h);
590
591                 if (xx < 1.0f)
592                         sum += BKE_brush_curve_strength(br, xx, 1);
593         }
594
595         return sum;
596 }
597
598 static float paint_stroke_integrate_overlap(Brush *br, float factor)
599 {
600         int i;
601         int m;
602         float g;
603         float max;
604
605         float spacing = br->spacing * factor;
606
607         if (!(br->flag & BRUSH_SPACE_ATTEN && (br->spacing < 100)))
608                 return 1.0;
609
610         m = 10;
611         g = 1.0f / m;
612         max = 0;
613         for (i = 0; i < m; i++) {
614                 float overlap = fabs(paint_stroke_overlapped_curve(br, i * g, spacing));
615
616                 if (overlap > max)
617                         max = overlap;
618         }
619
620         if (max == 0.0f)
621                 return 1.0f;
622         else
623                 return 1.0f / max;
624 }
625
626 static float paint_space_stroke_spacing_variable(const Scene *scene, PaintStroke *stroke, float pressure, float dpressure, float length)
627 {
628         if (BKE_brush_use_size_pressure(scene, stroke->brush)) {
629                 /* use pressure to modify size. set spacing so that at 100%, the circles
630                  * are aligned nicely with no overlap. for this the spacing needs to be
631                  * the average of the previous and next size. */
632                 float s = paint_space_stroke_spacing(scene, stroke, 1.0f, pressure);
633                 float q = s * dpressure / (2.0f * length);
634                 float pressure_fac = (1.0f + q) / (1.0f - q);
635
636                 float last_size_pressure = stroke->last_pressure;
637                 float new_size_pressure = stroke->last_pressure * pressure_fac;
638
639                 /* average spacing */
640                 float last_spacing = paint_space_stroke_spacing(scene, stroke, last_size_pressure, pressure);
641                 float new_spacing = paint_space_stroke_spacing(scene, stroke, new_size_pressure, pressure);
642
643                 return 0.5f * (last_spacing + new_spacing);
644         }
645         else {
646                 /* no size pressure */
647                 return paint_space_stroke_spacing(scene, stroke, 1.0f, pressure);
648         }
649 }
650
651 /* For brushes with stroke spacing enabled, moves mouse in steps
652  * towards the final mouse location. */
653 static int paint_space_stroke(bContext *C, wmOperator *op, const float final_mouse[2], float final_pressure)
654 {
655         const Scene *scene = CTX_data_scene(C);
656         PaintStroke *stroke = op->customdata;
657         UnifiedPaintSettings *ups = stroke->ups;
658         int cnt = 0;
659
660         float pressure, dpressure;
661         float mouse[2], dmouse[2];
662         float length;
663         float no_pressure_spacing = paint_space_stroke_spacing(scene, stroke, 1.0f, 1.0f);
664
665         sub_v2_v2v2(dmouse, final_mouse, stroke->last_mouse_position);
666
667         pressure = stroke->last_pressure;
668         dpressure = final_pressure - stroke->last_pressure;
669
670         length = normalize_v2(dmouse);
671
672         while (length > 0.0f) {
673                 float spacing = paint_space_stroke_spacing_variable(scene, stroke, pressure, dpressure, length);
674
675                 if (length >= spacing) {
676                         mouse[0] = stroke->last_mouse_position[0] + dmouse[0] * spacing;
677                         mouse[1] = stroke->last_mouse_position[1] + dmouse[1] * spacing;
678                         pressure = stroke->last_pressure + (spacing / length) * dpressure;
679
680                         ups->overlap_factor = paint_stroke_integrate_overlap(stroke->brush, spacing / no_pressure_spacing);
681
682                         stroke->stroke_distance += spacing / stroke->zoom_2d;
683                         paint_brush_stroke_add_step(C, op, mouse, pressure);
684
685                         length -= spacing;
686                         pressure = stroke->last_pressure;
687                         dpressure = final_pressure - stroke->last_pressure;
688
689                         cnt++;
690                 }
691                 else {
692                         break;
693                 }
694         }
695
696         return cnt;
697 }
698
699 /**** Public API ****/
700
701 PaintStroke *paint_stroke_new(bContext *C,
702                               wmOperator *op,
703                               StrokeGetLocation get_location,
704                               StrokeTestStart test_start,
705                               StrokeUpdateStep update_step,
706                               StrokeRedraw redraw,
707                               StrokeDone done, int event_type)
708 {
709         PaintStroke *stroke = MEM_callocN(sizeof(PaintStroke), "PaintStroke");
710         ToolSettings *toolsettings = CTX_data_tool_settings(C);
711         UnifiedPaintSettings *ups = &toolsettings->unified_paint_settings;
712         Paint *p = BKE_paint_get_active_from_context(C);
713         Brush *br = stroke->brush = BKE_paint_brush(p);
714         float zoomx, zoomy;
715
716         ED_view3d_viewcontext_init(C, &stroke->vc);
717
718         stroke->get_location = get_location;
719         stroke->test_start = test_start;
720         stroke->update_step = update_step;
721         stroke->redraw = redraw;
722         stroke->done = done;
723         stroke->event_type = event_type; /* for modal, return event */
724         stroke->ups = ups;
725         stroke->stroke_mode = RNA_enum_get(op->ptr, "mode");
726
727         get_imapaint_zoom(C, &zoomx, &zoomy);
728         stroke->zoom_2d = max_ff(zoomx, zoomy);
729
730         if (stroke->stroke_mode == BRUSH_STROKE_INVERT) {
731                 if (br->flag & (BRUSH_CURVE)) {
732                         RNA_enum_set(op->ptr, "mode", BRUSH_STROKE_NORMAL);
733                 }
734         }
735         /* initialize here */
736         ups->overlap_factor = 1.0;
737         ups->stroke_active = true;
738         
739         zero_v3(ups->average_stroke_accum);
740         ups->average_stroke_counter = 0;
741         
742         /* initialize here to avoid initialization conflict with threaded strokes */
743         curvemapping_initialize(br->curve);
744         if (p->flags & PAINT_USE_CAVITY_MASK)
745                 curvemapping_initialize(p->cavity_curve);
746
747         BKE_paint_set_overlay_override(br->overlay_flags);
748
749         return stroke;
750 }
751
752 void paint_stroke_data_free(struct wmOperator *op)
753 {
754         BKE_paint_set_overlay_override(0);
755         MEM_SAFE_FREE(op->customdata);
756 }
757
758 static void stroke_done(struct bContext *C, struct wmOperator *op)
759 {
760         struct PaintStroke *stroke = op->customdata;
761         UnifiedPaintSettings *ups = stroke->ups;
762
763         ups->draw_anchored = false;
764         ups->stroke_active = false;
765
766         /* reset rotation here to avoid doing so in cursor display */
767         if (!(stroke->brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE))
768                 ups->brush_rotation = 0.0f;
769
770         if (!(stroke->brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE))
771                 ups->brush_rotation_sec = 0.0f;
772
773         if (stroke->stroke_started) {
774                 if (stroke->redraw)
775                         stroke->redraw(C, stroke, true);
776
777                 if (stroke->done)
778                         stroke->done(C, stroke);
779         }
780
781         if (stroke->timer) {
782                 WM_event_remove_timer(
783                         CTX_wm_manager(C),
784                         CTX_wm_window(C),
785                         stroke->timer);
786         }
787
788         if (stroke->stroke_cursor)
789                 WM_paint_cursor_end(CTX_wm_manager(C), stroke->stroke_cursor);
790
791         BLI_freelistN(&stroke->line);
792
793         paint_stroke_data_free(op);
794 }
795
796 /* Returns zero if the stroke dots should not be spaced, non-zero otherwise */
797 bool paint_space_stroke_enabled(Brush *br, ePaintMode mode)
798 {
799         return (br->flag & BRUSH_SPACE) && paint_supports_dynamic_size(br, mode);
800 }
801
802 static bool sculpt_is_grab_tool(Brush *br)
803 {
804         return ELEM(br->sculpt_tool,
805                      SCULPT_TOOL_GRAB,
806                      SCULPT_TOOL_THUMB,
807                      SCULPT_TOOL_ROTATE,
808                      SCULPT_TOOL_SNAKE_HOOK);
809 }
810
811 /* return true if the brush size can change during paint (normally used for pressure) */
812 bool paint_supports_dynamic_size(Brush *br, ePaintMode mode)
813 {
814         if (br->flag & BRUSH_ANCHORED)
815                 return false;
816
817         switch (mode) {
818                 case ePaintSculpt:
819                         if (sculpt_is_grab_tool(br))
820                                 return false;
821                         break;
822
823                 case ePaintTexture2D: /* fall through */
824                 case ePaintTextureProjective:
825                         if ((br->imagepaint_tool == PAINT_TOOL_FILL) &&
826                             (br->flag & BRUSH_USE_GRADIENT))
827                         {
828                                 return false;
829                         }
830                         break;
831
832                 default:
833                         break;
834         }
835         return true;
836 }
837
838 bool paint_supports_smooth_stroke(Brush *br, ePaintMode mode)
839 {
840         if (!(br->flag & BRUSH_SMOOTH_STROKE) ||
841             (br->flag & (BRUSH_ANCHORED | BRUSH_DRAG_DOT | BRUSH_LINE)))
842         {
843                 return false;
844         }
845
846         switch (mode) {
847                 case ePaintSculpt:
848                         if (sculpt_is_grab_tool(br))
849                                 return false;
850                         break;
851                 default:
852                         break;
853         }
854         return true;
855 }
856
857 bool paint_supports_texture(ePaintMode mode)
858 {
859         /* omit: PAINT_WEIGHT, PAINT_SCULPT_UV, PAINT_INVALID */
860         return ELEM(mode, ePaintSculpt, ePaintVertex, ePaintTextureProjective, ePaintTexture2D);
861 }
862
863 /* return true if the brush size can change during paint (normally used for pressure) */
864 bool paint_supports_dynamic_tex_coords(Brush *br, ePaintMode mode)
865 {
866         if (br->flag & BRUSH_ANCHORED)
867                 return false;
868
869         switch (mode) {
870                 case ePaintSculpt:
871                         if (sculpt_is_grab_tool(br))
872                                 return false;
873                         break;
874                 default:
875                         break;
876         }
877         return true;
878 }
879
880 #define PAINT_STROKE_MODAL_CANCEL 1
881
882 /* called in paint_ops.c, on each regeneration of keymaps  */
883 struct wmKeyMap *paint_stroke_modal_keymap(struct wmKeyConfig *keyconf)
884 {
885         static struct EnumPropertyItem modal_items[] = {
886                 {PAINT_STROKE_MODAL_CANCEL, "CANCEL", 0,
887                 "Cancel",
888                 "Cancel and undo a stroke in progress"},
889
890                 { 0 }
891         };
892
893         static const char *name = "Paint Stroke Modal";
894
895         struct wmKeyMap *keymap = WM_modalkeymap_get(keyconf, name);
896
897         /* this function is called for each spacetype, only needs to add map once */
898         if (!keymap) {
899                 keymap = WM_modalkeymap_add(keyconf, name, modal_items);
900
901                 /* items for modal map */
902                 WM_modalkeymap_add_item(
903                         keymap, ESCKEY, KM_PRESS, KM_ANY, 0, PAINT_STROKE_MODAL_CANCEL);
904         }
905
906         return keymap;
907 }
908
909 static void paint_stroke_add_sample(const Paint *paint,
910                                     PaintStroke *stroke,
911                                     float x, float y, float pressure)
912 {
913         PaintSample *sample = &stroke->samples[stroke->cur_sample];
914         int max_samples = CLAMPIS(paint->num_input_samples, 1, PAINT_MAX_INPUT_SAMPLES);
915
916         sample->mouse[0] = x;
917         sample->mouse[1] = y;
918         sample->pressure = pressure;
919
920         stroke->cur_sample++;
921         if (stroke->cur_sample >= max_samples)
922                 stroke->cur_sample = 0;
923         if (stroke->num_samples < max_samples)
924                 stroke->num_samples++;
925 }
926
927 static void paint_stroke_sample_average(const PaintStroke *stroke,
928                                         PaintSample *average)
929 {
930         int i;
931         
932         memset(average, 0, sizeof(*average));
933
934         BLI_assert(stroke->num_samples > 0);
935         
936         for (i = 0; i < stroke->num_samples; i++) {
937                 add_v2_v2(average->mouse, stroke->samples[i].mouse);
938                 average->pressure += stroke->samples[i].pressure;
939         }
940
941         mul_v2_fl(average->mouse, 1.0f / stroke->num_samples);
942         average->pressure /= stroke->num_samples;
943
944         /*printf("avg=(%f, %f), num=%d\n", average->mouse[0], average->mouse[1], stroke->num_samples);*/
945 }
946
947 /**
948  * Slightly different version of spacing for line/curve strokes,
949  * makes sure the dabs stay on the line path.
950  */
951 static void paint_line_strokes_spacing(
952         bContext *C, wmOperator *op, PaintStroke *stroke, float spacing, float *length_residue,
953         const float old_pos[2], const float new_pos[2])
954 {
955         UnifiedPaintSettings *ups = stroke->ups;
956
957         float mouse[2], dmouse[2];
958         float length;
959
960         sub_v2_v2v2(dmouse, new_pos, old_pos);
961         copy_v2_v2(stroke->last_mouse_position, old_pos);
962
963         length = normalize_v2(dmouse);
964
965         BLI_assert(length >= 0.0f);
966
967         if (length == 0.0f)
968                 return;
969
970         while (length > 0.0f) {
971                 float spacing_final = spacing - *length_residue;
972                 length += *length_residue;
973                 *length_residue = 0.0;
974
975                 if (length >= spacing) {
976                         mouse[0] = stroke->last_mouse_position[0] + dmouse[0] * spacing_final;
977                         mouse[1] = stroke->last_mouse_position[1] + dmouse[1] * spacing_final;
978
979                         ups->overlap_factor = paint_stroke_integrate_overlap(stroke->brush, 1.0);
980
981                         stroke->stroke_distance += spacing / stroke->zoom_2d;
982                         paint_brush_stroke_add_step(C, op, mouse, 1.0);
983
984                         length -= spacing;
985                         spacing_final = spacing;
986                 }
987                 else {
988                         break;
989                 }
990         }
991
992         *length_residue = length;
993 }
994
995
996 static void paint_stroke_line_end(bContext *C, wmOperator *op, PaintStroke *stroke, float mouse[2])
997 {
998         Brush *br = stroke->brush;
999         if (stroke->stroke_started && (br->flag & BRUSH_LINE)) {
1000                 stroke->ups->overlap_factor = paint_stroke_integrate_overlap(br, 1.0);
1001
1002                 paint_brush_stroke_add_step(C, op, stroke->last_mouse_position, 1.0);
1003                 paint_space_stroke(C, op, mouse, 1.0);
1004         }
1005 }
1006
1007 static bool paint_stroke_curve_end(bContext *C, wmOperator *op, PaintStroke *stroke)
1008 {
1009         Brush *br = stroke->brush;
1010
1011         if (br->flag & BRUSH_CURVE) {
1012                 UnifiedPaintSettings *ups = &CTX_data_tool_settings(C)->unified_paint_settings;
1013                 const Scene *scene = CTX_data_scene(C);
1014                 const float spacing = paint_space_stroke_spacing(scene, stroke, 1.0f, 1.0f);
1015                 PaintCurve *pc = br->paint_curve;
1016                 PaintCurvePoint *pcp;
1017                 float length_residue = 0.0f;
1018                 int i;
1019
1020                 if (!pc)
1021                         return true;
1022
1023 #ifdef DEBUG_TIME
1024                 TIMEIT_START_AVERAGED(whole_stroke);
1025 #endif
1026
1027                 pcp = pc->points;
1028                 stroke->ups->overlap_factor = paint_stroke_integrate_overlap(br, 1.0);
1029
1030                 for (i = 0; i < pc->tot_points - 1; i++, pcp++) {
1031                         int j;
1032                         float data[(PAINT_CURVE_NUM_SEGMENTS + 1) * 2];
1033                         float tangents[(PAINT_CURVE_NUM_SEGMENTS + 1) * 2];
1034                         PaintCurvePoint *pcp_next = pcp + 1;
1035                         bool do_rake = false;
1036
1037                         for (j = 0; j < 2; j++) {
1038                                 BKE_curve_forward_diff_bezier(
1039                                         pcp->bez.vec[1][j],
1040                                         pcp->bez.vec[2][j],
1041                                         pcp_next->bez.vec[0][j],
1042                                         pcp_next->bez.vec[1][j],
1043                                         data + j, PAINT_CURVE_NUM_SEGMENTS, sizeof(float[2]));
1044                         }
1045
1046                         if ((br->mtex.brush_angle_mode & MTEX_ANGLE_RAKE) ||
1047                             (br->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE))
1048                         {
1049                                 do_rake = true;
1050                                 for (j = 0; j < 2; j++) {
1051                                         BKE_curve_forward_diff_tangent_bezier(
1052                                                 pcp->bez.vec[1][j],
1053                                                 pcp->bez.vec[2][j],
1054                                                 pcp_next->bez.vec[0][j],
1055                                                 pcp_next->bez.vec[1][j],
1056                                                 tangents + j, PAINT_CURVE_NUM_SEGMENTS, sizeof(float[2]));
1057                                 }
1058                         }
1059
1060                         for (j = 0; j < PAINT_CURVE_NUM_SEGMENTS; j++) {
1061                                 if (do_rake) {
1062                                         float rotation = atan2f(tangents[2 * j], tangents[2 * j + 1]);
1063                                         paint_update_brush_rake_rotation(ups, br, rotation);
1064                                 }
1065
1066                                 if (!stroke->stroke_started) {
1067                                         stroke->last_pressure = 1.0;
1068                                         copy_v2_v2(stroke->last_mouse_position, data + 2 * j);
1069                                         stroke->stroke_started = stroke->test_start(C, op, stroke->last_mouse_position);
1070
1071                                         if (stroke->stroke_started) {
1072                                                 paint_brush_stroke_add_step(C, op, data + 2 * j, 1.0);
1073                                                 paint_line_strokes_spacing(C, op, stroke, spacing, &length_residue, data + 2 * j, data + 2 * (j + 1));
1074                                         }
1075                                 }
1076                                 else {
1077                                         paint_line_strokes_spacing(C, op, stroke, spacing, &length_residue, data + 2 * j, data + 2 * (j + 1));
1078                                 }
1079                         }
1080                 }
1081
1082                 stroke_done(C, op);
1083
1084 #ifdef DEBUG_TIME
1085                 TIMEIT_END_AVERAGED(whole_stroke);
1086 #endif
1087
1088                 return true;
1089         }
1090
1091         return false;
1092 }
1093
1094 static void paint_stroke_line_constrain(PaintStroke *stroke, float mouse[2])
1095 {
1096         if (stroke->constrain_line) {
1097                 float line[2];
1098                 float angle, len, res;
1099                 
1100                 sub_v2_v2v2(line, mouse, stroke->last_mouse_position);
1101                 angle = atan2f(line[1], line[0]);
1102                 len = len_v2(line);
1103                 
1104                 /* divide angle by PI/4 */
1105                 angle = 4.0f * angle / (float)M_PI;
1106                 
1107                 /* now take residue */
1108                 res = angle - floorf(angle);
1109                 
1110                 /* residue decides how close we are at a certain angle */
1111                 if (res <= 0.5f) {
1112                         angle = floorf(angle) * (float)M_PI_4;
1113                 }
1114                 else {
1115                         angle = (floorf(angle) + 1.0f) * (float)M_PI_4;
1116                 }
1117                 
1118                 mouse[0] = stroke->constrained_pos[0] = len * cosf(angle) + stroke->last_mouse_position[0];
1119                 mouse[1] = stroke->constrained_pos[1] = len * sinf(angle) + stroke->last_mouse_position[1];
1120         }
1121 }
1122
1123 int paint_stroke_modal(bContext *C, wmOperator *op, const wmEvent *event)
1124 {
1125         Paint *p = BKE_paint_get_active_from_context(C);
1126         ePaintMode mode = BKE_paintmode_get_active_from_context(C);
1127         PaintStroke *stroke = op->customdata;
1128         Brush *br = stroke->brush;
1129         PaintSample sample_average;
1130         float mouse[2];
1131         bool first_dab = false;
1132         bool first_modal = false;
1133         bool redraw = false;
1134         float pressure;
1135
1136         /* see if tablet affects event. Line, anchored and drag dot strokes do not support pressure */
1137         pressure = (br->flag & (BRUSH_LINE | BRUSH_ANCHORED | BRUSH_DRAG_DOT)) ? 1.0f : WM_event_tablet_data(event, &stroke->pen_flip, NULL);
1138
1139         paint_stroke_add_sample(p, stroke, event->mval[0], event->mval[1], pressure);
1140         paint_stroke_sample_average(stroke, &sample_average);
1141
1142 #ifdef WITH_INPUT_NDOF
1143         /* let NDOF motion pass through to the 3D view so we can paint and rotate simultaneously!
1144          * this isn't perfect... even when an extra MOUSEMOVE is spoofed, the stroke discards it
1145          * since the 2D deltas are zero -- code in this file needs to be updated to use the
1146          * post-NDOF_MOTION MOUSEMOVE */
1147         if (event->type == NDOF_MOTION)
1148                 return OPERATOR_PASS_THROUGH;
1149 #endif
1150
1151         /* one time initialization */
1152         if (!stroke->stroke_init) {
1153                 if (paint_stroke_curve_end(C, op, stroke))
1154                         return OPERATOR_FINISHED;
1155
1156                 if (paint_supports_smooth_stroke(br, mode))
1157                         stroke->stroke_cursor =
1158                             WM_paint_cursor_activate(CTX_wm_manager(C), paint_poll, paint_draw_smooth_cursor, stroke);
1159
1160                 stroke->stroke_init = true;
1161                 first_modal = true;
1162         }
1163
1164         /* one time stroke initialization */
1165         if (!stroke->stroke_started) {
1166                 stroke->last_pressure = sample_average.pressure;
1167                 copy_v2_v2(stroke->last_mouse_position, sample_average.mouse);
1168                 stroke->stroke_started = stroke->test_start(C, op, sample_average.mouse);
1169                 BLI_assert((stroke->stroke_started & ~1) == 0);  /* 0/1 */
1170
1171                 if (stroke->stroke_started) {
1172                         if (br->flag & BRUSH_AIRBRUSH)
1173                                 stroke->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, stroke->brush->rate);
1174
1175                         if (br->flag & BRUSH_LINE) {
1176                                 stroke->stroke_cursor =
1177                                         WM_paint_cursor_activate(CTX_wm_manager(C), paint_poll, paint_draw_line_cursor, stroke);
1178                         }
1179
1180                         first_dab = true;
1181                 }
1182         }
1183
1184         /* Cancel */
1185         if (event->type == EVT_MODAL_MAP && event->val == PAINT_STROKE_MODAL_CANCEL) {
1186                 if (op->type->cancel) {
1187                         op->type->cancel(C, op);
1188                 }
1189                 else {
1190                         paint_stroke_cancel(C, op);
1191                 }
1192                 return OPERATOR_CANCELLED;
1193         }
1194
1195         if (event->type == stroke->event_type && !first_modal) {
1196                 if (event->val == KM_RELEASE) {
1197                         copy_v2_fl2(mouse, event->mval[0], event->mval[1]);
1198                         paint_stroke_line_constrain(stroke, mouse);
1199                         paint_stroke_line_end(C, op, stroke, mouse);
1200                         stroke_done(C, op);
1201                         return OPERATOR_FINISHED;
1202                 }
1203         }
1204         else if (ELEM(event->type, RETKEY, SPACEKEY)) {
1205                 paint_stroke_line_end(C, op, stroke, sample_average.mouse);
1206                 stroke_done(C, op);
1207                 return OPERATOR_FINISHED;
1208         }
1209         else if (br->flag & BRUSH_LINE) {
1210                 if (event->alt)
1211                         stroke->constrain_line = true;
1212                 else 
1213                         stroke->constrain_line = false;
1214
1215                 copy_v2_fl2(mouse, event->mval[0], event->mval[1]);
1216                 paint_stroke_line_constrain(stroke, mouse);
1217                 
1218                 if (stroke->stroke_started && (first_modal || (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)))) {
1219                         if ((br->mtex.brush_angle_mode & MTEX_ANGLE_RAKE) || (br->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)) {
1220                                 copy_v2_v2(stroke->ups->last_rake, stroke->last_mouse_position);
1221                         }
1222                         paint_calculate_rake_rotation(stroke->ups, br, mouse);
1223                 }
1224         }
1225         else if (first_modal ||
1226                  /* regular dabs */
1227                  (!(br->flag & (BRUSH_AIRBRUSH)) && (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE))) ||
1228                  /* airbrush */
1229                  ((br->flag & BRUSH_AIRBRUSH) && event->type == TIMER && event->customdata == stroke->timer))
1230         {
1231                 if (paint_smooth_stroke(stroke, &sample_average, mode, mouse, &pressure)) {
1232                         if (stroke->stroke_started) {
1233                                 if (paint_space_stroke_enabled(br, mode)) {
1234                                         if (paint_space_stroke(C, op, mouse, pressure))
1235                                                 redraw = true;
1236                                 }
1237                                 else {
1238                                         float dmouse[2];
1239                                         sub_v2_v2v2(dmouse, mouse, stroke->last_mouse_position);
1240                                         stroke->stroke_distance += len_v2(dmouse);
1241                                         paint_brush_stroke_add_step(C, op, mouse, pressure);
1242                                         redraw = true;
1243                                 }
1244                         }
1245                 }
1246         }
1247
1248         /* we want the stroke to have the first daub at the start location
1249          * instead of waiting till we have moved the space distance */
1250         if (first_dab &&
1251             paint_space_stroke_enabled(br, mode) &&
1252             !(br->flag & BRUSH_SMOOTH_STROKE))
1253         {
1254                 stroke->ups->overlap_factor = paint_stroke_integrate_overlap(br, 1.0);
1255                 paint_brush_stroke_add_step(C, op, sample_average.mouse, sample_average.pressure);
1256                 redraw = true;
1257         }
1258
1259         /* do updates for redraw. if event is inbetween mousemove there are more
1260          * coming, so postpone potentially slow redraw updates until all are done */
1261         if (event->type != INBETWEEN_MOUSEMOVE) {
1262                 wmWindow *window = CTX_wm_window(C);
1263                 ARegion *ar = CTX_wm_region(C);
1264
1265                 /* At the very least, invalidate the cursor */
1266                 if (ar && (p->flags & PAINT_SHOW_BRUSH))
1267                         WM_paint_cursor_tag_redraw(window, ar);
1268
1269                 if (redraw && stroke->redraw)
1270                         stroke->redraw(C, stroke, false);
1271         }
1272
1273         return OPERATOR_RUNNING_MODAL;
1274 }
1275
1276 int paint_stroke_exec(bContext *C, wmOperator *op)
1277 {
1278         PaintStroke *stroke = op->customdata;
1279
1280         /* only when executed for the first time */
1281         if (stroke->stroke_started == 0) {
1282                 PropertyRNA *strokeprop;
1283                 PointerRNA firstpoint;
1284                 float mouse[2];
1285
1286                 strokeprop = RNA_struct_find_property(op->ptr, "stroke");
1287
1288                 if (RNA_property_collection_lookup_int(op->ptr, strokeprop, 0, &firstpoint)) {
1289                         RNA_float_get_array(&firstpoint, "mouse", mouse);
1290                         stroke->stroke_started = stroke->test_start(C, op, mouse);
1291                 }
1292         }
1293
1294         if (stroke->stroke_started) {
1295                 RNA_BEGIN (op->ptr, itemptr, "stroke")
1296                 {
1297                         stroke->update_step(C, stroke, &itemptr);
1298                 }
1299                 RNA_END;
1300         }
1301
1302         bool ok = (stroke->stroke_started != 0);
1303
1304         stroke_done(C, op);
1305
1306         return ok ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1307 }
1308
1309 void paint_stroke_cancel(bContext *C, wmOperator *op)
1310 {
1311         stroke_done(C, op);
1312 }
1313
1314 ViewContext *paint_stroke_view_context(PaintStroke *stroke)
1315 {
1316         return &stroke->vc;
1317 }
1318
1319 void *paint_stroke_mode_data(struct PaintStroke *stroke)
1320 {
1321         return stroke->mode_data;
1322 }
1323
1324 bool paint_stroke_flipped(struct PaintStroke *stroke)
1325 {
1326         return stroke->pen_flip;
1327 }
1328
1329 float paint_stroke_distance_get(struct PaintStroke *stroke)
1330 {
1331         return stroke->stroke_distance;
1332 }
1333
1334 void paint_stroke_set_mode_data(PaintStroke *stroke, void *mode_data)
1335 {
1336         stroke->mode_data = mode_data;
1337 }
1338
1339 int paint_poll(bContext *C)
1340 {
1341         Paint *p = BKE_paint_get_active_from_context(C);
1342         Object *ob = CTX_data_active_object(C);
1343         ScrArea *sa = CTX_wm_area(C);
1344         ARegion *ar = CTX_wm_region(C);
1345
1346         return p && ob && BKE_paint_brush(p) &&
1347                (sa && ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_IMAGE)) &&
1348                (ar && ar->regiontype == RGN_TYPE_WINDOW);
1349 }