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