Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / gpencil / gpencil_paint.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) 2008, Blender Foundation, Joshua Leung
19  * This is a new part of Blender
20  *
21  * Contributor(s): Joshua Leung, Antonio Vazquez
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/gpencil/gpencil_paint.c
27  *  \ingroup edgpencil
28  */
29
30
31 #include <stdio.h>
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_rand.h"
43 #include "BLI_math_geom.h"
44
45 #include "BLT_translation.h"
46
47 #include "PIL_time.h"
48
49 #include "BKE_paint.h"
50 #include "BKE_gpencil.h"
51 #include "BKE_context.h"
52 #include "BKE_global.h"
53 #include "BKE_report.h"
54 #include "BKE_screen.h"
55 #include "BKE_tracking.h"
56 #include "BKE_colortools.h"
57
58 #include "DNA_object_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_gpencil_types.h"
61 #include "DNA_brush_types.h"
62 #include "DNA_windowmanager_types.h"
63
64 #include "UI_view2d.h"
65
66 #include "ED_gpencil.h"
67 #include "ED_screen.h"
68 #include "ED_view3d.h"
69 #include "ED_clip.h"
70
71 #include "BIF_glutil.h"
72
73 #include "GPU_immediate.h"
74 #include "GPU_immediate_util.h"
75
76 #include "RNA_access.h"
77 #include "RNA_define.h"
78
79 #include "WM_api.h"
80 #include "WM_types.h"
81
82 #include "DEG_depsgraph.h"
83
84 #include "gpencil_intern.h"
85
86 /* ******************************************* */
87 /* 'Globals' and Defines */
88
89 /* values for tGPsdata->status */
90 typedef enum eGPencil_PaintStatus {
91         GP_STATUS_IDLING = 0,   /* stroke isn't in progress yet */
92         GP_STATUS_PAINTING,     /* a stroke is in progress */
93         GP_STATUS_ERROR,        /* something wasn't correctly set up */
94         GP_STATUS_DONE          /* painting done */
95 } eGPencil_PaintStatus;
96
97 /* Return flags for adding points to stroke buffer */
98 typedef enum eGP_StrokeAdd_Result {
99         GP_STROKEADD_INVALID    = -2,       /* error occurred - insufficient info to do so */
100         GP_STROKEADD_OVERFLOW   = -1,       /* error occurred - cannot fit any more points */
101         GP_STROKEADD_NORMAL,                /* point was successfully added */
102         GP_STROKEADD_FULL                   /* cannot add any more points to buffer */
103 } eGP_StrokeAdd_Result;
104
105 /* Runtime flags */
106 typedef enum eGPencil_PaintFlags {
107         GP_PAINTFLAG_FIRSTRUN       = (1 << 0),    /* operator just started */
108         GP_PAINTFLAG_STROKEADDED    = (1 << 1),
109         GP_PAINTFLAG_V3D_ERASER_DEPTH = (1 << 2),
110         GP_PAINTFLAG_SELECTMASK     = (1 << 3),
111 } eGPencil_PaintFlags;
112
113
114 /* Temporary 'Stroke' Operation data
115  *   "p" = op->customdata
116  */
117 typedef struct tGPsdata {
118         Scene *scene;       /* current scene from context */
119         struct Depsgraph *depsgraph;
120
121         wmWindow *win;      /* window where painting originated */
122         ScrArea *sa;        /* area where painting originated */
123         ARegion *ar;        /* region where painting originated */
124         View2D *v2d;        /* needed for GP_STROKE_2DSPACE */
125         rctf *subrect;      /* for using the camera rect within the 3d view */
126         rctf subrect_data;
127
128         GP_SpaceConversion gsc; /* settings to pass to gp_points_to_xy() */
129
130         PointerRNA ownerPtr; /* pointer to owner of gp-datablock */
131         bGPdata *gpd;       /* gp-datablock layer comes from */
132         bGPDlayer *gpl;     /* layer we're working on */
133         bGPDframe *gpf;     /* frame we're working on */
134
135         char *align_flag;   /* projection-mode flags (toolsettings - eGPencil_Placement_Flags) */
136
137         eGPencil_PaintStatus status;     /* current status of painting */
138         eGPencil_PaintModes  paintmode;  /* mode for painting */
139         eGPencil_PaintFlags  flags;      /* flags that can get set during runtime (eGPencil_PaintFlags) */
140
141         short radius;       /* radius of influence for eraser */
142
143         int mval[2];        /* current mouse-position */
144         int mvalo[2];       /* previous recorded mouse-position */
145
146         float pressure;     /* current stylus pressure */
147         float opressure;    /* previous stylus pressure */
148
149         /* These need to be doubles, as (at least under unix) they are in seconds since epoch,
150          * float (and its 7 digits precision) is definitively not enough here!
151          * double, with its 15 digits precision, ensures us millisecond precision for a few centuries at least.
152          */
153         double inittime;    /* Used when converting to path */
154         double curtime;     /* Used when converting to path */
155         double ocurtime;    /* Used when converting to path */
156
157         float imat[4][4];   /* inverted transformation matrix applying when converting coords from screen-space
158                              * to region space */
159         float mat[4][4];
160
161         float custom_color[4]; /* custom color - hack for enforcing a particular color for track/mask editing */
162
163         void *erasercursor; /* radial cursor data for drawing eraser */
164
165         bGPDpalettecolor *palettecolor; /* current palette color */
166         bGPDbrush *brush; /* current drawing brush */
167         short straight[2];   /* 1: line horizontal, 2: line vertical, other: not defined, second element position */
168         int lock_axis;       /* lock drawing to one axis */
169
170         short keymodifier;   /* key used for invoking the operator */
171 } tGPsdata;
172
173 /* ------ */
174
175 /* Macros for accessing sensitivity thresholds... */
176 /* minimum number of pixels mouse should move before new point created */
177 #define MIN_MANHATTEN_PX    (U.gp_manhattendist)
178 /* minimum length of new segment before new point can be added */
179 #define MIN_EUCLIDEAN_PX    (U.gp_euclideandist)
180
181 static bool gp_stroke_added_check(tGPsdata *p)
182 {
183         return (p->gpf && p->gpf->strokes.last && p->flags & GP_PAINTFLAG_STROKEADDED);
184 }
185
186 static void gp_stroke_added_enable(tGPsdata *p)
187 {
188         BLI_assert(p->gpf->strokes.last != NULL);
189         p->flags |= GP_PAINTFLAG_STROKEADDED;
190 }
191
192 /* ------ */
193 /* Forward defines for some functions... */
194
195 static void gp_session_validatebuffer(tGPsdata *p);
196
197 /* ******************************************* */
198 /* Context Wrangling... */
199
200 /* check if context is suitable for drawing */
201 static int gpencil_draw_poll(bContext *C)
202 {
203         if (ED_operator_regionactive(C)) {
204                 /* check if current context can support GPencil data */
205                 if (ED_gpencil_data_get_pointers(C, NULL) != NULL) {
206                         /* check if Grease Pencil isn't already running */
207                         if (ED_gpencil_session_active() == 0)
208                                 return 1;
209                         else
210                                 CTX_wm_operator_poll_msg_set(C, "Grease Pencil operator is already active");
211                 }
212                 else {
213                         CTX_wm_operator_poll_msg_set(C, "Failed to find Grease Pencil data to draw into");
214                 }
215         }
216         else {
217                 CTX_wm_operator_poll_msg_set(C, "Active region not set");
218         }
219
220         return 0;
221 }
222
223 /* check if projecting strokes into 3d-geometry in the 3D-View */
224 static bool gpencil_project_check(tGPsdata *p)
225 {
226         bGPdata *gpd = p->gpd;
227         return ((gpd->sbuffer_sflag & GP_STROKE_3DSPACE) && (*p->align_flag & (GP_PROJECT_DEPTH_VIEW | GP_PROJECT_DEPTH_STROKE)));
228 }
229
230 /* ******************************************* */
231 /* Calculations/Conversions */
232
233 /* Utilities --------------------------------- */
234
235 /* get the reference point for stroke-point conversions */
236 static void gp_get_3d_reference(tGPsdata *p, float vec[3])
237 {
238         View3D *v3d = p->sa->spacedata.first;
239         const float *fp = ED_view3d_cursor3d_get(p->scene, v3d)->location;
240
241         /* the reference point used depends on the owner... */
242 #if 0 /* XXX: disabled for now, since we can't draw relative to the owner yet */
243         if (p->ownerPtr.type == &RNA_Object) {
244                 Object *ob = (Object *)p->ownerPtr.data;
245
246                 /* active Object
247                  *  - use relative distance of 3D-cursor from object center
248                  */
249                 sub_v3_v3v3(vec, fp, ob->loc);
250         }
251         else
252 #endif
253         {
254                 /* use 3D-cursor */
255                 copy_v3_v3(vec, fp);
256         }
257 }
258
259 /* Stroke Editing ---------------------------- */
260
261 /* check if the current mouse position is suitable for adding a new point */
262 static bool gp_stroke_filtermval(tGPsdata *p, const int mval[2], int pmval[2])
263 {
264         int dx = abs(mval[0] - pmval[0]);
265         int dy = abs(mval[1] - pmval[1]);
266
267         /* if buffer is empty, just let this go through (i.e. so that dots will work) */
268         if (p->gpd->sbuffer_size == 0)
269                 return true;
270
271         /* check if mouse moved at least certain distance on both axes (best case)
272          *      - aims to eliminate some jitter-noise from input when trying to draw straight lines freehand
273          */
274         else if ((dx > MIN_MANHATTEN_PX) && (dy > MIN_MANHATTEN_PX))
275                 return true;
276
277         /* check if the distance since the last point is significant enough
278          *      - prevents points being added too densely
279          *      - distance here doesn't use sqrt to prevent slowness... we should still be safe from overflows though
280          */
281         else if ((dx * dx + dy * dy) > MIN_EUCLIDEAN_PX * MIN_EUCLIDEAN_PX)
282                 return true;
283
284         /* mouse 'didn't move' */
285         else
286                 return false;
287 }
288
289 /* reproject the points of the stroke to a plane locked to axis to avoid stroke offset */
290 static void gp_project_points_to_plane(RegionView3D *rv3d, bGPDstroke *gps, const float origin[3], const int axis)
291 {
292         float plane_normal[3];
293         float vn[3];
294
295         float ray[3];
296         float rpoint[3];
297
298         /* normal vector for a plane locked to axis */
299         zero_v3(plane_normal);
300         plane_normal[axis] = 1.0f;
301
302         /* Reproject the points in the plane */
303         for (int i = 0; i < gps->totpoints; i++) {
304                 bGPDspoint *pt = &gps->points[i];
305
306                 /* get a vector from the point with the current view direction of the viewport */
307                 ED_view3d_global_to_vector(rv3d, &pt->x, vn);
308
309                 /* calculate line extrem point to create a ray that cross the plane */
310                 mul_v3_fl(vn, -50.0f);
311                 add_v3_v3v3(ray, &pt->x, vn);
312
313                 /* if the line never intersect, the point is not changed */
314                 if (isect_line_plane_v3(rpoint, &pt->x, ray, origin, plane_normal)) {
315                         copy_v3_v3(&pt->x, rpoint);
316                 }
317         }
318 }
319
320 /* reproject stroke to plane locked to axis in 3d cursor location */
321 static void gp_reproject_toplane(tGPsdata *p, bGPDstroke *gps)
322 {
323         bGPdata *gpd = p->gpd;
324         float origin[3];
325         float cursor[3];
326         RegionView3D *rv3d = p->ar->regiondata;
327
328         /* verify the stroke mode is CURSOR 3d space mode */
329         if ((gpd->sbuffer_sflag & GP_STROKE_3DSPACE) == 0) {
330                 return;
331         }
332         if ((*p->align_flag & GP_PROJECT_VIEWSPACE) == 0) {
333                 return;
334         }
335         if ((*p->align_flag & GP_PROJECT_DEPTH_VIEW) || (*p->align_flag & GP_PROJECT_DEPTH_STROKE)) {
336                 return;
337         }
338
339         /* get 3d cursor and set origin for locked axis only. Uses axis-1 because the enum for XYZ start with 1 */
340         gp_get_3d_reference(p, cursor);
341         zero_v3(origin);
342         origin[p->lock_axis - 1] = cursor[p->lock_axis - 1];
343
344         gp_project_points_to_plane(rv3d, gps, origin, p->lock_axis - 1);
345 }
346
347 /* convert screen-coordinates to buffer-coordinates */
348 /* XXX this method needs a total overhaul! */
349 static void gp_stroke_convertcoords(tGPsdata *p, const int mval[2], float out[3], float *depth)
350 {
351         bGPdata *gpd = p->gpd;
352
353         /* in 3d-space - pt->x/y/z are 3 side-by-side floats */
354         if (gpd->sbuffer_sflag & GP_STROKE_3DSPACE) {
355                 if (gpencil_project_check(p) && (ED_view3d_autodist_simple(p->ar, mval, out, 0, depth))) {
356                         /* projecting onto 3D-Geometry
357                          *      - nothing more needs to be done here, since view_autodist_simple() has already done it
358                          */
359                 }
360                 else {
361                         float mval_prj[2];
362                         float rvec[3], dvec[3];
363                         float mval_f[2] = {UNPACK2(mval)};
364                         float zfac;
365
366                         /* Current method just converts each point in screen-coordinates to
367                          * 3D-coordinates using the 3D-cursor as reference. In general, this
368                          * works OK, but it could of course be improved.
369                          *
370                          * TODO:
371                          *      - investigate using nearest point(s) on a previous stroke as
372                          *        reference point instead or as offset, for easier stroke matching
373                          */
374
375                         gp_get_3d_reference(p, rvec);
376                         zfac = ED_view3d_calc_zfac(p->ar->regiondata, rvec, NULL);
377
378                         if (ED_view3d_project_float_global(p->ar, rvec, mval_prj, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
379                                 sub_v2_v2v2(mval_f, mval_prj, mval_f);
380                                 ED_view3d_win_to_delta(p->ar, mval_f, dvec, zfac);
381                                 sub_v3_v3v3(out, rvec, dvec);
382                         }
383                         else {
384                                 zero_v3(out);
385                         }
386                 }
387         }
388
389         /* 2d - on 'canvas' (assume that p->v2d is set) */
390         else if ((gpd->sbuffer_sflag & GP_STROKE_2DSPACE) && (p->v2d)) {
391                 UI_view2d_region_to_view(p->v2d, mval[0], mval[1], &out[0], &out[1]);
392                 mul_v3_m4v3(out, p->imat, out);
393         }
394
395         /* 2d - relative to screen (viewport area) */
396         else {
397                 if (p->subrect == NULL) { /* normal 3D view */
398                         out[0] = (float)(mval[0]) / (float)(p->ar->winx) * 100;
399                         out[1] = (float)(mval[1]) / (float)(p->ar->winy) * 100;
400                 }
401                 else { /* camera view, use subrect */
402                         out[0] = ((mval[0] - p->subrect->xmin) / BLI_rctf_size_x(p->subrect)) * 100;
403                         out[1] = ((mval[1] - p->subrect->ymin) / BLI_rctf_size_y(p->subrect)) * 100;
404                 }
405         }
406 }
407
408 /* apply jitter to stroke */
409 static void gp_brush_jitter(bGPdata *gpd, bGPDbrush *brush, tGPspoint *pt, const int mval[2], int r_mval[2])
410 {
411         float pressure = pt->pressure;
412         float tmp_pressure = pt->pressure;
413         if (brush->draw_jitter > 0.0f) {
414                 float curvef = curvemapping_evaluateF(brush->cur_jitter, 0, pressure);
415                 tmp_pressure = curvef * brush->draw_sensitivity;
416         }
417         const float exfactor = (brush->draw_jitter + 2.0f) * (brush->draw_jitter + 2.0f); /* exponential value */
418         const float fac = BLI_frand() * exfactor * tmp_pressure;
419         /* Jitter is applied perpendicular to the mouse movement vector (2D space) */
420         float mvec[2], svec[2];
421         /* mouse movement in ints -> floats */
422         if (gpd->sbuffer_size > 1) {
423                 mvec[0] = (float)(mval[0] - (pt - 1)->x);
424                 mvec[1] = (float)(mval[1] - (pt - 1)->y);
425                 normalize_v2(mvec);
426         }
427         else {
428                 mvec[0] = 0.0f;
429                 mvec[1] = 0.0f;
430         }
431         /* rotate mvec by 90 degrees... */
432         svec[0] = -mvec[1];
433         svec[1] = mvec[0];
434         /* scale the displacement by the random, and apply */
435         if (BLI_frand() > 0.5f) {
436                 mul_v2_fl(svec, -fac);
437         }
438         else {
439                 mul_v2_fl(svec, fac);
440         }
441
442         r_mval[0] = mval[0] + svec[0];
443         r_mval[1] = mval[1] + svec[1];
444
445 }
446
447 /* apply pressure change depending of the angle of the stroke to simulate a pen with shape */
448 static void gp_brush_angle(bGPdata *gpd, bGPDbrush *brush, tGPspoint *pt, const int mval[2])
449 {
450         float mvec[2];
451         float sen = brush->draw_angle_factor; /* sensitivity */;
452         float fac;
453         float mpressure;
454
455         float angle = brush->draw_angle; /* default angle of brush in radians */;
456         float v0[2] = { cos(angle), sin(angle) }; /* angle vector of the brush with full thickness */
457
458         /* Apply to first point (only if there are 2 points because before no data to do it ) */
459         if (gpd->sbuffer_size == 1) {
460                 mvec[0] = (float)(mval[0] - (pt - 1)->x);
461                 mvec[1] = (float)(mval[1] - (pt - 1)->y);
462                 normalize_v2(mvec);
463
464                 /* uses > 1.0f to get a smooth transition in first point */
465                 fac = 1.4f - fabs(dot_v2v2(v0, mvec)); /* 0.0 to 1.0 */
466                 (pt - 1)->pressure = (pt - 1)->pressure - (sen * fac);
467
468                 CLAMP((pt - 1)->pressure, GPENCIL_ALPHA_OPACITY_THRESH, 1.0f);
469         }
470
471         /* apply from second point */
472         if (gpd->sbuffer_size >= 1) {
473                 mvec[0] = (float)(mval[0] - (pt - 1)->x);
474                 mvec[1] = (float)(mval[1] - (pt - 1)->y);
475                 normalize_v2(mvec);
476
477                 fac = 1.0f - fabs(dot_v2v2(v0, mvec)); /* 0.0 to 1.0 */
478                 /* interpolate with previous point for smoother transitions */
479                 mpressure = interpf(pt->pressure - (sen * fac), (pt - 1)->pressure, 0.3f);
480                 pt->pressure = mpressure;
481
482                 CLAMP(pt->pressure, GPENCIL_ALPHA_OPACITY_THRESH, 1.0f);
483         }
484
485 }
486
487 /* add current stroke-point to buffer (returns whether point was successfully added) */
488 static short gp_stroke_addpoint(
489         tGPsdata *p, const int mval[2], float pressure, double curtime)
490 {
491         bGPdata *gpd = p->gpd;
492         bGPDbrush *brush = p->brush;
493         tGPspoint *pt;
494         ToolSettings *ts = p->scene->toolsettings;
495
496         /* check painting mode */
497         if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) {
498                 /* straight lines only - i.e. only store start and end point in buffer */
499                 if (gpd->sbuffer_size == 0) {
500                         /* first point in buffer (start point) */
501                         pt = (tGPspoint *)(gpd->sbuffer);
502
503                         /* store settings */
504                         copy_v2_v2_int(&pt->x, mval);
505                         pt->pressure = 1.0f; /* T44932 - Pressure vals are unreliable, so ignore for now */
506                         pt->strength = 1.0f;
507                         pt->time = (float)(curtime - p->inittime);
508
509                         /* increment buffer size */
510                         gpd->sbuffer_size++;
511                 }
512                 else {
513                         /* just reset the endpoint to the latest value
514                          *      - assume that pointers for this are always valid...
515                          */
516                         pt = ((tGPspoint *)(gpd->sbuffer) + 1);
517
518                         /* store settings */
519                         copy_v2_v2_int(&pt->x, mval);
520                         pt->pressure = 1.0f; /* T44932 - Pressure vals are unreliable, so ignore for now */
521                         pt->strength = 1.0f;
522                         pt->time = (float)(curtime - p->inittime);
523
524                         /* now the buffer has 2 points (and shouldn't be allowed to get any larger) */
525                         gpd->sbuffer_size = 2;
526                 }
527
528                 /* can keep carrying on this way :) */
529                 return GP_STROKEADD_NORMAL;
530         }
531         else if (p->paintmode == GP_PAINTMODE_DRAW) { /* normal drawing */
532                 /* check if still room in buffer */
533                 if (gpd->sbuffer_size >= GP_STROKE_BUFFER_MAX)
534                         return GP_STROKEADD_OVERFLOW;
535
536                 /* get pointer to destination point */
537                 pt = ((tGPspoint *)(gpd->sbuffer) + gpd->sbuffer_size);
538
539                 /* store settings */
540                 /* pressure */
541                 if (brush->flag & GP_BRUSH_USE_PRESSURE) {
542                         float curvef = curvemapping_evaluateF(brush->cur_sensitivity, 0, pressure);
543                         pt->pressure = curvef * brush->draw_sensitivity;
544                 }
545                 else {
546                         pt->pressure = 1.0f;
547                 }
548                 /* Apply jitter to position */
549                 if (brush->draw_jitter > 0.0f) {
550                         int r_mval[2];
551                         gp_brush_jitter(gpd, brush, pt, mval, r_mval);
552                         copy_v2_v2_int(&pt->x, r_mval);
553                 }
554                 else {
555                         copy_v2_v2_int(&pt->x, mval);
556                 }
557                 /* apply randomness to pressure */
558                 if ((brush->draw_random_press > 0.0f) && (brush->flag & GP_BRUSH_USE_RANDOM_PRESSURE)) {
559                         float curvef = curvemapping_evaluateF(brush->cur_sensitivity, 0, pressure);
560                         float tmp_pressure = curvef * brush->draw_sensitivity;
561                         if (BLI_frand() > 0.5f) {
562                                 pt->pressure -= tmp_pressure * brush->draw_random_press * BLI_frand();
563                         }
564                         else {
565                                 pt->pressure += tmp_pressure * brush->draw_random_press * BLI_frand();
566                         }
567                         CLAMP(pt->pressure, GPENCIL_STRENGTH_MIN, 1.0f);
568                 }
569
570                 /* apply angle of stroke to brush size */
571                 if (brush->draw_angle_factor > 0.0f) {
572                         gp_brush_angle(gpd, brush, pt, mval);
573                 }
574
575                 /* color strength */
576                 if (brush->flag & GP_BRUSH_USE_STENGTH_PRESSURE) {
577                         float curvef = curvemapping_evaluateF(brush->cur_strength, 0, pressure);
578                         float tmp_pressure = curvef * brush->draw_sensitivity;
579
580                         pt->strength = tmp_pressure * brush->draw_strength;
581                 }
582                 else {
583                         pt->strength = brush->draw_strength;
584                 }
585                 CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
586
587                 /* apply randomness to color strength */
588                 if ((brush->draw_random_press > 0.0f) && (brush->flag & GP_BRUSH_USE_RANDOM_STRENGTH)) {
589                         if (BLI_frand() > 0.5f) {
590                                 pt->strength -= pt->strength * brush->draw_random_press * BLI_frand();
591                         }
592                         else {
593                                 pt->strength += pt->strength * brush->draw_random_press * BLI_frand();
594                         }
595                         CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
596                 }
597
598                 /* point time */
599                 pt->time = (float)(curtime - p->inittime);
600
601                 /* increment counters */
602                 gpd->sbuffer_size++;
603
604                 /* check if another operation can still occur */
605                 if (gpd->sbuffer_size == GP_STROKE_BUFFER_MAX)
606                         return GP_STROKEADD_FULL;
607                 else
608                         return GP_STROKEADD_NORMAL;
609         }
610         else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
611
612                 bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
613                 /* get pointer to destination point */
614                 pt = (tGPspoint *)(gpd->sbuffer);
615
616                 /* store settings */
617                 copy_v2_v2_int(&pt->x, mval);
618                 pt->pressure = 1.0f; /* T44932 - Pressure vals are unreliable, so ignore for now */
619                 pt->strength = 1.0f;
620                 pt->time = (float)(curtime - p->inittime);
621
622                 /* if there's stroke for this poly line session add (or replace last) point
623                  * to stroke. This allows to draw lines more interactively (see new segment
624                  * during mouse slide, e.g.)
625                  */
626                 if (gp_stroke_added_check(p)) {
627                         bGPDstroke *gps = p->gpf->strokes.last;
628                         bGPDspoint *pts;
629
630                         /* first time point is adding to temporary buffer -- need to allocate new point in stroke */
631                         if (gpd->sbuffer_size == 0) {
632                                 gps->points = MEM_reallocN(gps->points, sizeof(bGPDspoint) * (gps->totpoints + 1));
633                                 gps->totpoints++;
634                         }
635
636                         pts = &gps->points[gps->totpoints - 1];
637
638                         /* special case for poly lines: normally,
639                          * depth is needed only when creating new stroke from buffer,
640                          * but poly lines are converting to stroke instantly,
641                          * so initialize depth buffer before converting coordinates
642                          */
643                         if (gpencil_project_check(p)) {
644                                 View3D *v3d = p->sa->spacedata.first;
645
646                                 view3d_region_operator_needs_opengl(p->win, p->ar);
647                                 ED_view3d_autodist_init(
648                                         p->depsgraph, p->ar, v3d, (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE) ? 1 : 0);
649                         }
650
651                         /* convert screen-coordinates to appropriate coordinates (and store them) */
652                         gp_stroke_convertcoords(p, &pt->x, &pts->x, NULL);
653                         /* if axis locked, reproject to plane locked (only in 3d space) */
654                         if (p->lock_axis > GP_LOCKAXIS_NONE) {
655                                 gp_reproject_toplane(p, gps);
656                         }
657                         /* if parented change position relative to parent object */
658                         if (gpl->parent != NULL) {
659                                 gp_apply_parent_point(gpl, pts);
660                         }
661                         /* copy pressure and time */
662                         pts->pressure = pt->pressure;
663                         pts->strength = pt->strength;
664                         pts->time = pt->time;
665                         /* force fill recalc */
666                         gps->flag |= GP_STROKE_RECALC_CACHES;
667                 }
668
669                 /* increment counters */
670                 if (gpd->sbuffer_size == 0)
671                         gpd->sbuffer_size++;
672
673                 return GP_STROKEADD_NORMAL;
674         }
675
676         /* return invalid state for now... */
677         return GP_STROKEADD_INVALID;
678 }
679
680 /* simplify a stroke (in buffer) before storing it
681  *      - applies a reverse Chaikin filter
682  *      - code adapted from etch-a-ton branch
683  */
684 static void gp_stroke_simplify(tGPsdata *p)
685 {
686         bGPdata *gpd = p->gpd;
687         tGPspoint *old_points = (tGPspoint *)gpd->sbuffer;
688         short num_points = gpd->sbuffer_size;
689         short flag = gpd->sbuffer_sflag;
690         short i, j;
691
692         /* only simplify if simplification is enabled, and we're not doing a straight line */
693         if (!(U.gp_settings & GP_PAINT_DOSIMPLIFY) || (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT))
694                 return;
695
696         /* don't simplify if less than 4 points in buffer */
697         if ((num_points <= 4) || (old_points == NULL))
698                 return;
699
700         /* clear buffer (but don't free mem yet) so that we can write to it
701          *      - firstly set sbuffer to NULL, so a new one is allocated
702          *      - secondly, reset flag after, as it gets cleared auto
703          */
704         gpd->sbuffer = NULL;
705         gp_session_validatebuffer(p);
706         gpd->sbuffer_sflag = flag;
707
708 /* macro used in loop to get position of new point
709  *      - used due to the mixture of datatypes in use here
710  */
711 #define GP_SIMPLIFY_AVPOINT(offs, sfac) \
712         { \
713                 co[0] += (float)(old_points[offs].x * sfac); \
714                 co[1] += (float)(old_points[offs].y * sfac); \
715                 pressure += old_points[offs].pressure * sfac; \
716                 time += old_points[offs].time * sfac; \
717         } (void)0
718
719         /* XXX Here too, do not lose start and end points! */
720         gp_stroke_addpoint(p, &old_points->x, old_points->pressure, p->inittime + (double)old_points->time);
721         for (i = 0, j = 0; i < num_points; i++) {
722                 if (i - j == 3) {
723                         float co[2], pressure, time;
724                         int mco[2];
725
726                         /* initialize values */
727                         co[0] = 0.0f;
728                         co[1] = 0.0f;
729                         pressure = 0.0f;
730                         time = 0.0f;
731
732                         /* using macro, calculate new point */
733                         GP_SIMPLIFY_AVPOINT(j, -0.25f);
734                         GP_SIMPLIFY_AVPOINT(j + 1, 0.75f);
735                         GP_SIMPLIFY_AVPOINT(j + 2, 0.75f);
736                         GP_SIMPLIFY_AVPOINT(j + 3, -0.25f);
737
738                         /* set values for adding */
739                         mco[0] = (int)co[0];
740                         mco[1] = (int)co[1];
741
742                         /* ignore return values on this... assume to be ok for now */
743                         gp_stroke_addpoint(p, mco, pressure, p->inittime + (double)time);
744
745                         j += 2;
746                 }
747         }
748         gp_stroke_addpoint(p, &old_points[num_points - 1].x, old_points[num_points - 1].pressure,
749                            p->inittime + (double)old_points[num_points - 1].time);
750
751         /* free old buffer */
752         MEM_freeN(old_points);
753 }
754
755 /* make a new stroke from the buffer data */
756 static void gp_stroke_newfrombuffer(tGPsdata *p)
757 {
758         bGPdata *gpd = p->gpd;
759         bGPDlayer *gpl = p->gpl;
760         bGPDstroke *gps;
761         bGPDspoint *pt;
762         tGPspoint *ptc;
763         bGPDbrush *brush = p->brush;
764         ToolSettings *ts = p->scene->toolsettings;
765
766         int i, totelem;
767         /* since strokes are so fine, when using their depth we need a margin otherwise they might get missed */
768         int depth_margin = (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE) ? 4 : 0;
769
770         /* get total number of points to allocate space for
771          *      - drawing straight-lines only requires the endpoints
772          */
773         if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT)
774                 totelem = (gpd->sbuffer_size >= 2) ? 2 : gpd->sbuffer_size;
775         else
776                 totelem = gpd->sbuffer_size;
777
778         /* exit with error if no valid points from this stroke */
779         if (totelem == 0) {
780                 if (G.debug & G_DEBUG)
781                         printf("Error: No valid points in stroke buffer to convert (tot=%d)\n", gpd->sbuffer_size);
782                 return;
783         }
784
785         /* special case for poly line -- for already added stroke during session
786          * coordinates are getting added to stroke immediately to allow more
787          * interactive behavior
788          */
789         if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
790                 if (gp_stroke_added_check(p)) {
791                         return;
792                 }
793         }
794
795         /* allocate memory for a new stroke */
796         gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke");
797
798         /* copy appropriate settings for stroke */
799         gps->totpoints = totelem;
800         gps->thickness = brush->thickness;
801         gps->flag = gpd->sbuffer_sflag;
802         gps->inittime = p->inittime;
803
804         /* enable recalculation flag by default (only used if hq fill) */
805         gps->flag |= GP_STROKE_RECALC_CACHES;
806
807         /* allocate enough memory for a continuous array for storage points */
808         int sublevel = brush->sublevel;
809         int new_totpoints = gps->totpoints;
810
811         for (i = 0; i < sublevel; i++) {
812                 new_totpoints += new_totpoints - 1;
813         }
814         gps->points = MEM_callocN(sizeof(bGPDspoint) * new_totpoints, "gp_stroke_points");
815         /* initialize triangle memory to dummy data */
816         gps->triangles = MEM_callocN(sizeof(bGPDtriangle), "GP Stroke triangulation");
817         gps->flag |= GP_STROKE_RECALC_CACHES;
818         gps->tot_triangles = 0;
819         /* set pointer to first non-initialized point */
820         pt = gps->points + (gps->totpoints - totelem);
821
822         /* copy points from the buffer to the stroke */
823         if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) {
824                 /* straight lines only -> only endpoints */
825                 {
826                         /* first point */
827                         ptc = gpd->sbuffer;
828
829                         /* convert screen-coordinates to appropriate coordinates (and store them) */
830                         gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
831                         /* if axis locked, reproject to plane locked (only in 3d space) */
832                         if (p->lock_axis > GP_LOCKAXIS_NONE) {
833                                 gp_reproject_toplane(p, gps);
834                         }
835                         /* if parented change position relative to parent object */
836                         if (gpl->parent != NULL) {
837                                 gp_apply_parent_point(gpl, pt);
838                         }
839                         /* copy pressure and time */
840                         pt->pressure = ptc->pressure;
841                         pt->strength = ptc->strength;
842                         CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
843                         pt->time = ptc->time;
844
845                         pt++;
846                 }
847
848                 if (totelem == 2) {
849                         /* last point if applicable */
850                         ptc = ((tGPspoint *)gpd->sbuffer) + (gpd->sbuffer_size - 1);
851
852                         /* convert screen-coordinates to appropriate coordinates (and store them) */
853                         gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
854                         /* if axis locked, reproject to plane locked (only in 3d space) */
855                         if (p->lock_axis > GP_LOCKAXIS_NONE) {
856                                 gp_reproject_toplane(p, gps);
857                         }
858                         /* if parented change position relative to parent object */
859                         if (gpl->parent != NULL) {
860                                 gp_apply_parent_point(gpl, pt);
861                         }
862
863                         /* copy pressure and time */
864                         pt->pressure = ptc->pressure;
865                         pt->strength = ptc->strength;
866                         CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
867                         pt->time = ptc->time;
868                 }
869         }
870         else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
871                 /* first point */
872                 ptc = gpd->sbuffer;
873
874                 /* convert screen-coordinates to appropriate coordinates (and store them) */
875                 gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
876                 /* if axis locked, reproject to plane locked (only in 3d space) */
877                 if (p->lock_axis > GP_LOCKAXIS_NONE) {
878                         gp_reproject_toplane(p, gps);
879                 }
880                 /* if parented change position relative to parent object */
881                 if (gpl->parent != NULL) {
882                         gp_apply_parent_point(gpl, pt);
883                 }
884                 /* copy pressure and time */
885                 pt->pressure = ptc->pressure;
886                 pt->strength = ptc->strength;
887                 CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
888                 pt->time = ptc->time;
889         }
890         else {
891                 float *depth_arr = NULL;
892
893                 /* get an array of depths, far depths are blended */
894                 if (gpencil_project_check(p)) {
895                         int mval[2], mval_prev[2] = { 0 };
896                         int interp_depth = 0;
897                         int found_depth = 0;
898
899                         depth_arr = MEM_mallocN(sizeof(float) * gpd->sbuffer_size, "depth_points");
900
901                         for (i = 0, ptc = gpd->sbuffer; i < gpd->sbuffer_size; i++, ptc++, pt++) {
902                                 copy_v2_v2_int(mval, &ptc->x);
903
904                                 if ((ED_view3d_autodist_depth(p->ar, mval, depth_margin, depth_arr + i) == 0) &&
905                                     (i && (ED_view3d_autodist_depth_seg(p->ar, mval, mval_prev, depth_margin + 1, depth_arr + i) == 0)))
906                                 {
907                                         interp_depth = true;
908                                 }
909                                 else {
910                                         found_depth = true;
911                                 }
912
913                                 copy_v2_v2_int(mval_prev, mval);
914                         }
915
916                         if (found_depth == false) {
917                                 /* eeh... not much we can do.. :/, ignore depth in this case, use the 3D cursor */
918                                 for (i = gpd->sbuffer_size - 1; i >= 0; i--)
919                                         depth_arr[i] = 0.9999f;
920                         }
921                         else {
922                                 if (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE_ENDPOINTS) {
923                                         /* remove all info between the valid endpoints */
924                                         int first_valid = 0;
925                                         int last_valid = 0;
926
927                                         for (i = 0; i < gpd->sbuffer_size; i++) {
928                                                 if (depth_arr[i] != FLT_MAX)
929                                                         break;
930                                         }
931                                         first_valid = i;
932
933                                         for (i = gpd->sbuffer_size - 1; i >= 0; i--) {
934                                                 if (depth_arr[i] != FLT_MAX)
935                                                         break;
936                                         }
937                                         last_valid = i;
938
939                                         /* invalidate non-endpoints, so only blend between first and last */
940                                         for (i = first_valid + 1; i < last_valid; i++)
941                                                 depth_arr[i] = FLT_MAX;
942
943                                         interp_depth = true;
944                                 }
945
946                                 if (interp_depth) {
947                                         interp_sparse_array(depth_arr, gpd->sbuffer_size, FLT_MAX);
948                                 }
949                         }
950                 }
951
952
953                 pt = gps->points;
954
955                 /* convert all points (normal behavior) */
956                 for (i = 0, ptc = gpd->sbuffer; i < gpd->sbuffer_size && ptc; i++, ptc++, pt++) {
957                         /* convert screen-coordinates to appropriate coordinates (and store them) */
958                         gp_stroke_convertcoords(p, &ptc->x, &pt->x, depth_arr ? depth_arr + i : NULL);
959
960                         /* copy pressure and time */
961                         pt->pressure = ptc->pressure;
962                         pt->strength = ptc->strength;
963                         CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
964                         pt->time = ptc->time;
965                 }
966
967                 /* subdivide the stroke */
968                 if (sublevel > 0) {
969                         int totpoints = gps->totpoints;
970                         for (i = 0; i < sublevel; i++) {
971                                 /* we're adding one new point between each pair of verts on each step */
972                                 totpoints += totpoints - 1;
973
974                                 gp_subdivide_stroke(gps, totpoints);
975                         }
976                 }
977                 /* apply randomness to stroke */
978                 if (brush->draw_random_sub > 0.0f) {
979                         gp_randomize_stroke(gps, brush);
980                 }
981
982                 /* smooth stroke after subdiv - only if there's something to do
983                  * for each iteration, the factor is reduced to get a better smoothing without changing too much
984                  * the original stroke
985                  */
986                 if (brush->draw_smoothfac > 0.0f) {
987                         float reduce = 0.0f;
988                         for (int r = 0; r < brush->draw_smoothlvl; ++r) {
989                                 for (i = 0; i < gps->totpoints; i++) {
990                                         /* NOTE: No pressure smoothing, or else we get annoying thickness changes while drawing... */
991                                         gp_smooth_stroke(gps, i, brush->draw_smoothfac - reduce, false);
992                                 }
993                                 reduce += 0.25f;  // reduce the factor
994                         }
995                 }
996
997                 /* if axis locked, reproject to plane locked (only in 3d space) */
998                 if (p->lock_axis > GP_LOCKAXIS_NONE) {
999                         gp_reproject_toplane(p, gps);
1000                 }
1001                 /* if parented change position relative to parent object */
1002                 if (gpl->parent != NULL) {
1003                         gp_apply_parent(gpl, gps);
1004                 }
1005
1006                 if (depth_arr)
1007                         MEM_freeN(depth_arr);
1008         }
1009         /* Save palette color */
1010         bGPDpalette *palette = BKE_gpencil_palette_getactive(p->gpd);
1011         bGPDpalettecolor *palcolor = BKE_gpencil_palettecolor_getactive(palette);
1012         gps->palcolor = palcolor;
1013         BLI_strncpy(gps->colorname, palcolor->info, sizeof(gps->colorname));
1014
1015         /* add stroke to frame, usually on tail of the listbase, but if on back is enabled the stroke is added on listbase head
1016          * because the drawing order is inverse and the head stroke is the first to draw. This is very useful for artist
1017          * when drawing the background
1018          */
1019         if ((ts->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode != GP_PAINTMODE_DRAW_POLY)) {
1020                 BLI_addhead(&p->gpf->strokes, gps);
1021         }
1022         else {
1023                 BLI_addtail(&p->gpf->strokes, gps);
1024         }
1025         gp_stroke_added_enable(p);
1026 }
1027
1028 /* --- 'Eraser' for 'Paint' Tool ------ */
1029
1030 /* which which point is infront (result should only be used for comparison) */
1031 static float view3d_point_depth(const RegionView3D *rv3d, const float co[3])
1032 {
1033         if (rv3d->is_persp) {
1034                 return ED_view3d_calc_zfac(rv3d, co, NULL);
1035         }
1036         else {
1037                 return -dot_v3v3(rv3d->viewinv[2], co);
1038         }
1039 }
1040
1041 /* only erase stroke points that are visible */
1042 static bool gp_stroke_eraser_is_occluded(tGPsdata *p, const bGPDspoint *pt, const int x, const int y)
1043 {
1044         if ((p->sa->spacetype == SPACE_VIEW3D) &&
1045             (p->flags & GP_PAINTFLAG_V3D_ERASER_DEPTH))
1046         {
1047                 RegionView3D *rv3d = p->ar->regiondata;
1048                 bGPDlayer *gpl = p->gpl;
1049
1050                 const int mval[2] = {x, y};
1051                 float mval_3d[3];
1052                 float fpt[3];
1053
1054                 float diff_mat[4][4];
1055                 /* calculate difference matrix if parent object */
1056                 ED_gpencil_parent_location(gpl, diff_mat);
1057
1058                 if (ED_view3d_autodist_simple(p->ar, mval, mval_3d, 0, NULL)) {
1059                         const float depth_mval = view3d_point_depth(rv3d, mval_3d);
1060
1061                         mul_v3_m4v3(fpt, diff_mat, &pt->x);
1062                         const float depth_pt   = view3d_point_depth(rv3d, fpt);
1063
1064                         if (depth_pt > depth_mval) {
1065                                 return true;
1066                         }
1067                 }
1068         }
1069         return false;
1070 }
1071
1072 /* apply a falloff effect to brush strength, based on distance */
1073 static float gp_stroke_eraser_calc_influence(tGPsdata *p, const int mval[2], const int radius, const int co[2])
1074 {
1075         /* Linear Falloff... */
1076         float distance = (float)len_v2v2_int(mval, co);
1077         float fac;
1078
1079         CLAMP(distance, 0.0f, (float)radius);
1080         fac = 1.0f - (distance / (float)radius);
1081
1082         /* Control this further using pen pressure */
1083         fac *= p->pressure;
1084
1085         /* Return influence factor computed here */
1086         return fac;
1087 }
1088
1089 /* eraser tool - evaluation per stroke */
1090 /* TODO: this could really do with some optimization (KD-Tree/BVH?) */
1091 static void gp_stroke_eraser_dostroke(tGPsdata *p,
1092                                       bGPDlayer *gpl, bGPDframe *gpf, bGPDstroke *gps,
1093                                       const int mval[2], const int mvalo[2],
1094                                       const int radius, const rcti *rect)
1095 {
1096         bGPDspoint *pt1, *pt2;
1097         int pc1[2] = {0};
1098         int pc2[2] = {0};
1099         int i;
1100         float diff_mat[4][4];
1101
1102         /* calculate difference matrix if parent object */
1103         if (gpl->parent != NULL) {
1104                 ED_gpencil_parent_location(gpl, diff_mat);
1105         }
1106
1107         if (gps->totpoints == 0) {
1108                 /* just free stroke */
1109                 if (gps->points)
1110                         MEM_freeN(gps->points);
1111                 if (gps->triangles)
1112                         MEM_freeN(gps->triangles);
1113                 BLI_freelinkN(&gpf->strokes, gps);
1114         }
1115         else if (gps->totpoints == 1) {
1116                 /* only process if it hasn't been masked out... */
1117                 if (!(p->flags & GP_PAINTFLAG_SELECTMASK) || (gps->points->flag & GP_SPOINT_SELECT)) {
1118                         if (gpl->parent == NULL) {
1119                                 gp_point_to_xy(&p->gsc, gps, gps->points, &pc1[0], &pc1[1]);
1120                         }
1121                         else {
1122                                 bGPDspoint pt_temp;
1123                                 gp_point_to_parent_space(gps->points, diff_mat, &pt_temp);
1124                                 gp_point_to_xy(&p->gsc, gps, &pt_temp, &pc1[0], &pc1[1]);
1125                         }
1126                         /* do boundbox check first */
1127                         if ((!ELEM(V2D_IS_CLIPPED, pc1[0], pc1[1])) && BLI_rcti_isect_pt(rect, pc1[0], pc1[1])) {
1128                                 /* only check if point is inside */
1129                                 if (len_v2v2_int(mval, pc1) <= radius) {
1130                                         /* free stroke */
1131                                         // XXX: pressure sensitive eraser should apply here too?
1132                                         MEM_freeN(gps->points);
1133                                         if (gps->triangles)
1134                                                 MEM_freeN(gps->triangles);
1135                                         BLI_freelinkN(&gpf->strokes, gps);
1136                                 }
1137                         }
1138                 }
1139         }
1140         else {
1141                 /* Pressure threshold at which stroke should be culled: Calculated as pressure value
1142                  * below which we would have invisible strokes
1143                  */
1144                 const float cull_thresh = (gps->thickness) ?  1.0f / ((float)gps->thickness)  : 1.0f;
1145
1146                 /* Amount to decrease the pressure of each point with each stroke */
1147                 // TODO: Fetch from toolsettings, or compute based on thickness instead?
1148                 const float strength = 0.1f;
1149
1150                 /* Perform culling? */
1151                 bool do_cull = false;
1152
1153
1154                 /* Clear Tags
1155                  *
1156                  * Note: It's better this way, as we are sure that
1157                  * we don't miss anything, though things will be
1158                  * slightly slower as a result
1159                  */
1160                 for (i = 0; i < gps->totpoints; i++) {
1161                         bGPDspoint *pt = &gps->points[i];
1162                         pt->flag &= ~GP_SPOINT_TAG;
1163                 }
1164
1165                 /* First Pass: Loop over the points in the stroke
1166                  *   1) Thin out parts of the stroke under the brush
1167                  *   2) Tag "too thin" parts for removal (in second pass)
1168                  */
1169                 for (i = 0; (i + 1) < gps->totpoints; i++) {
1170                         /* get points to work with */
1171                         pt1 = gps->points + i;
1172                         pt2 = gps->points + i + 1;
1173
1174                         /* only process if it hasn't been masked out... */
1175                         if ((p->flags & GP_PAINTFLAG_SELECTMASK) && !(gps->points->flag & GP_SPOINT_SELECT))
1176                                 continue;
1177
1178                         if (gpl->parent == NULL) {
1179                                 gp_point_to_xy(&p->gsc, gps, pt1, &pc1[0], &pc1[1]);
1180                                 gp_point_to_xy(&p->gsc, gps, pt2, &pc2[0], &pc2[1]);
1181                         }
1182                         else {
1183                                 bGPDspoint npt;
1184                                 gp_point_to_parent_space(pt1, diff_mat, &npt);
1185                                 gp_point_to_xy(&p->gsc, gps, &npt, &pc1[0], &pc1[1]);
1186
1187                                 gp_point_to_parent_space(pt2, diff_mat, &npt);
1188                                 gp_point_to_xy(&p->gsc, gps, &npt, &pc2[0], &pc2[1]);
1189                         }
1190
1191                         /* Check that point segment of the boundbox of the eraser stroke */
1192                         if (((!ELEM(V2D_IS_CLIPPED, pc1[0], pc1[1])) && BLI_rcti_isect_pt(rect, pc1[0], pc1[1])) ||
1193                             ((!ELEM(V2D_IS_CLIPPED, pc2[0], pc2[1])) && BLI_rcti_isect_pt(rect, pc2[0], pc2[1])))
1194                         {
1195                                 /* Check if point segment of stroke had anything to do with
1196                                  * eraser region  (either within stroke painted, or on its lines)
1197                                  *  - this assumes that linewidth is irrelevant
1198                                  */
1199                                 if (gp_stroke_inside_circle(mval, mvalo, radius, pc1[0], pc1[1], pc2[0], pc2[1])) {
1200                                         if ((gp_stroke_eraser_is_occluded(p, pt1, pc1[0], pc1[1]) == false) ||
1201                                             (gp_stroke_eraser_is_occluded(p, pt2, pc2[0], pc2[1]) == false))
1202                                         {
1203                                                 /* Point is affected: */
1204                                                 /* 1) Adjust thickness
1205                                                  *  - Influence of eraser falls off with distance from the middle of the eraser
1206                                                  *  - Second point gets less influence, as it might get hit again in the next segment
1207                                                  */
1208                                                 pt1->pressure -= gp_stroke_eraser_calc_influence(p, mval, radius, pc1) * strength;
1209                                                 pt2->pressure -= gp_stroke_eraser_calc_influence(p, mval, radius, pc2) * strength / 2.0f;
1210
1211                                                 /* 2) Tag any point with overly low influence for removal in the next pass */
1212                                                 if (pt1->pressure < cull_thresh) {
1213                                                         pt1->flag |= GP_SPOINT_TAG;
1214                                                         do_cull = true;
1215                                                 }
1216                                                 if (pt2->pressure < cull_thresh) {
1217                                                         pt2->flag |= GP_SPOINT_TAG;
1218                                                         do_cull = true;
1219                                                 }
1220                                         }
1221                                 }
1222                         }
1223                 }
1224
1225                 /* Second Pass: Remove any points that are tagged */
1226                 if (do_cull) {
1227                         gp_stroke_delete_tagged_points(gpf, gps, gps->next, GP_SPOINT_TAG);
1228                 }
1229         }
1230 }
1231
1232 /* erase strokes which fall under the eraser strokes */
1233 static void gp_stroke_doeraser(tGPsdata *p)
1234 {
1235         bGPDlayer *gpl;
1236         bGPDstroke *gps, *gpn;
1237         rcti rect;
1238
1239         /* rect is rectangle of eraser */
1240         rect.xmin = p->mval[0] - p->radius;
1241         rect.ymin = p->mval[1] - p->radius;
1242         rect.xmax = p->mval[0] + p->radius;
1243         rect.ymax = p->mval[1] + p->radius;
1244
1245         if (p->sa->spacetype == SPACE_VIEW3D) {
1246                 if (p->flags & GP_PAINTFLAG_V3D_ERASER_DEPTH) {
1247                         View3D *v3d = p->sa->spacedata.first;
1248                         view3d_region_operator_needs_opengl(p->win, p->ar);
1249                         ED_view3d_autodist_init(p->depsgraph, p->ar, v3d, 0);
1250                 }
1251         }
1252
1253         /* loop over all layers too, since while it's easy to restrict editing to
1254          * only a subset of layers, it is harder to perform the same erase operation
1255          * on multiple layers...
1256          */
1257         for (gpl = p->gpd->layers.first; gpl; gpl = gpl->next) {
1258                 bGPDframe *gpf = gpl->actframe;
1259
1260                 /* only affect layer if it's editable (and visible) */
1261                 if (gpencil_layer_is_editable(gpl) == false) {
1262                         continue;
1263                 }
1264                 else if (gpf == NULL) {
1265                         continue;
1266                 }
1267
1268                 /* loop over strokes, checking segments for intersections */
1269                 for (gps = gpf->strokes.first; gps; gps = gpn) {
1270                         gpn = gps->next;
1271                         /* check if the color is editable */
1272                         if (ED_gpencil_stroke_color_use(gpl, gps) == false) {
1273                                 continue;
1274                         }
1275                         /* Not all strokes in the datablock may be valid in the current editor/context
1276                          * (e.g. 2D space strokes in the 3D view, if the same datablock is shared)
1277                          */
1278                         if (ED_gpencil_stroke_can_use_direct(p->sa, gps)) {
1279                                 gp_stroke_eraser_dostroke(p, gpl, gpf, gps, p->mval, p->mvalo, p->radius, &rect);
1280                         }
1281                 }
1282         }
1283 }
1284
1285 /* ******************************************* */
1286 /* Sketching Operator */
1287
1288 /* clear the session buffers (call this before AND after a paint operation) */
1289 static void gp_session_validatebuffer(tGPsdata *p)
1290 {
1291         bGPdata *gpd = p->gpd;
1292
1293         /* clear memory of buffer (or allocate it if starting a new session) */
1294         if (gpd->sbuffer) {
1295                 /* printf("\t\tGP - reset sbuffer\n"); */
1296                 memset(gpd->sbuffer, 0, sizeof(tGPspoint) * GP_STROKE_BUFFER_MAX);
1297         }
1298         else {
1299                 /* printf("\t\tGP - allocate sbuffer\n"); */
1300                 gpd->sbuffer = MEM_callocN(sizeof(tGPspoint) * GP_STROKE_BUFFER_MAX, "gp_session_strokebuffer");
1301         }
1302
1303         /* reset indices */
1304         gpd->sbuffer_size = 0;
1305
1306         /* reset flags */
1307         gpd->sbuffer_sflag = 0;
1308
1309         /* reset inittime */
1310         p->inittime = 0.0;
1311 }
1312
1313 /* create a new palette color */
1314 static bGPDpalettecolor *gp_create_new_color(bGPDpalette *palette)
1315 {
1316         bGPDpalettecolor *palcolor;
1317
1318         palcolor = BKE_gpencil_palettecolor_addnew(palette, DATA_("Color"), true);
1319
1320         return palcolor;
1321 }
1322
1323 /* initialize a drawing brush */
1324 static void gp_init_drawing_brush(ToolSettings *ts, tGPsdata *p)
1325 {
1326         bGPDbrush *brush;
1327
1328         /* if not exist, create a new one */
1329         if (BLI_listbase_is_empty(&ts->gp_brushes)) {
1330                 /* create new brushes */
1331                 BKE_gpencil_brush_init_presets(ts);
1332                 brush = BKE_gpencil_brush_getactive(ts);
1333         }
1334         else {
1335                 /* Use the current */
1336                 brush = BKE_gpencil_brush_getactive(ts);
1337         }
1338         /* be sure curves are initializated */
1339         curvemapping_initialize(brush->cur_sensitivity);
1340         curvemapping_initialize(brush->cur_strength);
1341         curvemapping_initialize(brush->cur_jitter);
1342
1343         /* asign to temp tGPsdata */
1344         p->brush = brush;
1345 }
1346
1347
1348 /* initialize a paint palette brush and a default color if not exist */
1349 static void gp_init_palette(tGPsdata *p)
1350 {
1351         bGPdata *gpd;
1352         bGPDpalette *palette;
1353         bGPDpalettecolor *palcolor;
1354
1355         gpd = p->gpd;
1356
1357         /* if not exist, create a new palette */
1358         if (BLI_listbase_is_empty(&gpd->palettes)) {
1359                 /* create new palette */
1360                 palette = BKE_gpencil_palette_addnew(gpd, DATA_("GP_Palette"), true);
1361                 /* now create a default color */
1362                 palcolor = gp_create_new_color(palette);
1363         }
1364         else {
1365                 /* Use the current palette and color */
1366                 palette = BKE_gpencil_palette_getactive(gpd);
1367                 /* the palette needs one color */
1368                 if (BLI_listbase_is_empty(&palette->colors)) {
1369                         palcolor = gp_create_new_color(palette);
1370                 }
1371                 else {
1372                         palcolor = BKE_gpencil_palettecolor_getactive(palette);
1373                 }
1374                 /* in some situations can be null, so use first */
1375                 if (palcolor == NULL) {
1376                         BKE_gpencil_palettecolor_setactive(palette, palette->colors.first);
1377                         palcolor = palette->colors.first;
1378                 }
1379         }
1380
1381         /* asign to temp tGPsdata */
1382         p->palettecolor = palcolor;
1383 }
1384
1385 /* (re)init new painting data */
1386 static bool gp_session_initdata(bContext *C, tGPsdata *p)
1387 {
1388         bGPdata **gpd_ptr = NULL;
1389         ScrArea *curarea = CTX_wm_area(C);
1390         ARegion *ar = CTX_wm_region(C);
1391         ToolSettings *ts = CTX_data_tool_settings(C);
1392
1393         /* make sure the active view (at the starting time) is a 3d-view */
1394         if (curarea == NULL) {
1395                 p->status = GP_STATUS_ERROR;
1396                 if (G.debug & G_DEBUG)
1397                         printf("Error: No active view for painting\n");
1398                 return 0;
1399         }
1400
1401         /* pass on current scene and window */
1402         p->scene = CTX_data_scene(C);
1403         p->depsgraph = CTX_data_depsgraph(C);
1404         p->win = CTX_wm_window(C);
1405
1406         unit_m4(p->imat);
1407         unit_m4(p->mat);
1408
1409         switch (curarea->spacetype) {
1410                 /* supported views first */
1411                 case SPACE_VIEW3D:
1412                 {
1413                         /* View3D *v3d = curarea->spacedata.first; */
1414                         /* RegionView3D *rv3d = ar->regiondata; */
1415
1416                         /* set current area
1417                          *      - must verify that region data is 3D-view (and not something else)
1418                          */
1419                         /* CAUTION: If this is the "toolbar", then this will change on the first stroke */
1420                         p->sa = curarea;
1421                         p->ar = ar;
1422                         p->align_flag = &ts->gpencil_v3d_align;
1423
1424                         if (ar->regiondata == NULL) {
1425                                 p->status = GP_STATUS_ERROR;
1426                                 if (G.debug & G_DEBUG)
1427                                         printf("Error: 3D-View active region doesn't have any region data, so cannot be drawable\n");
1428                                 return 0;
1429                         }
1430                         break;
1431                 }
1432                 case SPACE_NODE:
1433                 {
1434                         /* SpaceNode *snode = curarea->spacedata.first; */
1435
1436                         /* set current area */
1437                         p->sa = curarea;
1438                         p->ar = ar;
1439                         p->v2d = &ar->v2d;
1440                         p->align_flag = &ts->gpencil_v2d_align;
1441                         break;
1442                 }
1443                 case SPACE_SEQ:
1444                 {
1445                         SpaceSeq *sseq = curarea->spacedata.first;
1446
1447                         /* set current area */
1448                         p->sa = curarea;
1449                         p->ar = ar;
1450                         p->v2d = &ar->v2d;
1451                         p->align_flag = &ts->gpencil_seq_align;
1452
1453                         /* check that gpencil data is allowed to be drawn */
1454                         if (sseq->mainb == SEQ_DRAW_SEQUENCE) {
1455                                 p->status = GP_STATUS_ERROR;
1456                                 if (G.debug & G_DEBUG)
1457                                         printf("Error: In active view (sequencer), active mode doesn't support Grease Pencil\n");
1458                                 return 0;
1459                         }
1460                         break;
1461                 }
1462                 case SPACE_IMAGE:
1463                 {
1464                         /* SpaceImage *sima = curarea->spacedata.first; */
1465
1466                         /* set the current area */
1467                         p->sa = curarea;
1468                         p->ar = ar;
1469                         p->v2d = &ar->v2d;
1470                         p->align_flag = &ts->gpencil_ima_align;
1471                         break;
1472                 }
1473                 case SPACE_CLIP:
1474                 {
1475                         SpaceClip *sc = curarea->spacedata.first;
1476                         MovieClip *clip = ED_space_clip_get_clip(sc);
1477
1478                         if (clip == NULL) {
1479                                 p->status = GP_STATUS_ERROR;
1480                                 return false;
1481                         }
1482
1483                         /* set the current area */
1484                         p->sa = curarea;
1485                         p->ar = ar;
1486                         p->v2d = &ar->v2d;
1487                         p->align_flag = &ts->gpencil_v2d_align;
1488
1489                         invert_m4_m4(p->imat, sc->unistabmat);
1490
1491                         /* custom color for new layer */
1492                         p->custom_color[0] = 1.0f;
1493                         p->custom_color[1] = 0.0f;
1494                         p->custom_color[2] = 0.5f;
1495                         p->custom_color[3] = 0.9f;
1496
1497                         if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
1498                                 int framenr = ED_space_clip_get_clip_frame_number(sc);
1499                                 MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking);
1500                                 MovieTrackingMarker *marker = track ? BKE_tracking_marker_get(track, framenr) : NULL;
1501
1502                                 if (marker) {
1503                                         p->imat[3][0] -= marker->pos[0];
1504                                         p->imat[3][1] -= marker->pos[1];
1505                                 }
1506                                 else {
1507                                         p->status = GP_STATUS_ERROR;
1508                                         return false;
1509                                 }
1510                         }
1511
1512                         invert_m4_m4(p->mat, p->imat);
1513                         copy_m4_m4(p->gsc.mat, p->mat);
1514                         break;
1515                 }
1516                 /* unsupported views */
1517                 default:
1518                 {
1519                         p->status = GP_STATUS_ERROR;
1520                         if (G.debug & G_DEBUG)
1521                                 printf("Error: Active view not appropriate for Grease Pencil drawing\n");
1522                         return 0;
1523                 }
1524         }
1525
1526         /* get gp-data */
1527         gpd_ptr = ED_gpencil_data_get_pointers(C, &p->ownerPtr);
1528         if (gpd_ptr == NULL) {
1529                 p->status = GP_STATUS_ERROR;
1530                 if (G.debug & G_DEBUG)
1531                         printf("Error: Current context doesn't allow for any Grease Pencil data\n");
1532                 return 0;
1533         }
1534         else {
1535                 /* if no existing GPencil block exists, add one */
1536                 if (*gpd_ptr == NULL)
1537                         *gpd_ptr = BKE_gpencil_data_addnew("GPencil");
1538                 p->gpd = *gpd_ptr;
1539         }
1540
1541         if (ED_gpencil_session_active() == 0) {
1542                 /* initialize undo stack,
1543                  * also, existing undo stack would make buffer drawn
1544                  */
1545                 gpencil_undo_init(p->gpd);
1546         }
1547
1548         /* clear out buffer (stored in gp-data), in case something contaminated it */
1549         gp_session_validatebuffer(p);
1550         /* set brush and create a new one if null */
1551         gp_init_drawing_brush(ts, p);
1552         /* set palette info and create a new one if null */
1553         gp_init_palette(p);
1554         /* set palette colors */
1555         bGPDpalettecolor *palcolor = p->palettecolor;
1556         bGPdata *pdata = p->gpd;
1557         copy_v4_v4(pdata->scolor, palcolor->color);
1558         copy_v4_v4(pdata->sfill, palcolor->fill);
1559         pdata->sflag = palcolor->flag;
1560         /* lock axis */
1561         p->lock_axis = ts->gp_sculpt.lock_axis;
1562
1563         return 1;
1564 }
1565
1566 /* init new painting session */
1567 static tGPsdata *gp_session_initpaint(bContext *C)
1568 {
1569         tGPsdata *p = NULL;
1570
1571         /* create new context data */
1572         p = MEM_callocN(sizeof(tGPsdata), "GPencil Drawing Data");
1573
1574         gp_session_initdata(C, p);
1575
1576         /* radius for eraser circle is defined in userprefs now */
1577         /* NOTE: we do this here, so that if we exit immediately,
1578          *       erase size won't get lost
1579          */
1580         p->radius = U.gp_eraser;
1581
1582         /* return context data for running paint operator */
1583         return p;
1584 }
1585
1586 /* cleanup after a painting session */
1587 static void gp_session_cleanup(tGPsdata *p)
1588 {
1589         bGPdata *gpd = (p) ? p->gpd : NULL;
1590
1591         /* error checking */
1592         if (gpd == NULL)
1593                 return;
1594
1595         /* free stroke buffer */
1596         if (gpd->sbuffer) {
1597                 /* printf("\t\tGP - free sbuffer\n"); */
1598                 MEM_freeN(gpd->sbuffer);
1599                 gpd->sbuffer = NULL;
1600         }
1601
1602         /* clear flags */
1603         gpd->sbuffer_size = 0;
1604         gpd->sbuffer_sflag = 0;
1605         p->inittime = 0.0;
1606 }
1607
1608 /* init new stroke */
1609 static void gp_paint_initstroke(tGPsdata *p, eGPencil_PaintModes paintmode, Depsgraph *depsgraph)
1610 {
1611         Scene *scene = p->scene;
1612         ToolSettings *ts = scene->toolsettings;
1613
1614         /* get active layer (or add a new one if non-existent) */
1615         p->gpl = BKE_gpencil_layer_getactive(p->gpd);
1616         if (p->gpl == NULL) {
1617                 p->gpl = BKE_gpencil_layer_addnew(p->gpd, "GP_Layer", true);
1618
1619                 if (p->custom_color[3])
1620                         copy_v3_v3(p->gpl->color, p->custom_color);
1621         }
1622         if (p->gpl->flag & GP_LAYER_LOCKED) {
1623                 p->status = GP_STATUS_ERROR;
1624                 if (G.debug & G_DEBUG)
1625                         printf("Error: Cannot paint on locked layer\n");
1626                 return;
1627         }
1628
1629         /* get active frame (add a new one if not matching frame) */
1630         if (paintmode == GP_PAINTMODE_ERASER) {
1631                 /* Eraser mode:
1632                  * 1) Add new frames to all frames that we might touch,
1633                  * 2) Ensure that p->gpf refers to the frame used for the active layer
1634                  *    (to avoid problems with other tools which expect it to exist)
1635                  */
1636                 bool has_layer_to_erase = false;
1637
1638                 for (bGPDlayer *gpl = p->gpd->layers.first; gpl; gpl = gpl->next) {
1639                         /* Skip if layer not editable */
1640                         if (gpencil_layer_is_editable(gpl) == false)
1641                                 continue;
1642
1643                         /* Add a new frame if needed (and based off the active frame,
1644                          * as we need some existing strokes to erase)
1645                          *
1646                          * Note: We don't add a new frame if there's nothing there now, so
1647                          *       -> If there are no frames at all, don't add one
1648                          *       -> If there are no strokes in that frame, don't add a new empty frame
1649                          */
1650                         if (gpl->actframe && gpl->actframe->strokes.first) {
1651                                 gpl->actframe = BKE_gpencil_layer_getframe(gpl, CFRA, GP_GETFRAME_ADD_COPY);
1652                                 has_layer_to_erase = true;
1653                         }
1654
1655                         /* XXX: we omit GP_FRAME_PAINT here for now,
1656                          * as it is only really useful for doing
1657                          * paintbuffer drawing
1658                          */
1659                 }
1660
1661                 /* Ensure this gets set... */
1662                 p->gpf = p->gpl->actframe;
1663
1664                 /* Restrict eraser to only affecting selected strokes, if the "selection mask" is on
1665                  * (though this is only available in editmode)
1666                  */
1667                 if (p->gpd->flag & GP_DATA_STROKE_EDITMODE) {
1668                         if (ts->gp_sculpt.flag & GP_BRUSHEDIT_FLAG_SELECT_MASK) {
1669                                 p->flags |= GP_PAINTFLAG_SELECTMASK;
1670                         }
1671                 }
1672
1673                 if (has_layer_to_erase == false) {
1674                         p->status = GP_STATUS_ERROR;
1675                         //if (G.debug & G_DEBUG)
1676                                 printf("Error: Eraser will not be affecting anything (gpencil_paint_init)\n");
1677                         return;
1678                 }
1679         }
1680         else {
1681                 /* Drawing Modes - Add a new frame if needed on the active layer */
1682                 short add_frame_mode;
1683
1684                 if (ts->gpencil_flags & GP_TOOL_FLAG_RETAIN_LAST)
1685                         add_frame_mode = GP_GETFRAME_ADD_COPY;
1686                 else
1687                         add_frame_mode = GP_GETFRAME_ADD_NEW;
1688
1689                 p->gpf = BKE_gpencil_layer_getframe(p->gpl, CFRA, add_frame_mode);
1690
1691                 if (p->gpf == NULL) {
1692                         p->status = GP_STATUS_ERROR;
1693                         if (G.debug & G_DEBUG)
1694                                 printf("Error: No frame created (gpencil_paint_init)\n");
1695                         return;
1696                 }
1697                 else {
1698                         p->gpf->flag |= GP_FRAME_PAINT;
1699                 }
1700         }
1701
1702         /* set 'eraser' for this stroke if using eraser */
1703         p->paintmode = paintmode;
1704         if (p->paintmode == GP_PAINTMODE_ERASER) {
1705                 p->gpd->sbuffer_sflag |= GP_STROKE_ERASER;
1706
1707                 /* check if we should respect depth while erasing */
1708                 if (p->sa->spacetype == SPACE_VIEW3D) {
1709                         if (p->gpl->flag & GP_LAYER_NO_XRAY) {
1710                                 p->flags |= GP_PAINTFLAG_V3D_ERASER_DEPTH;
1711                         }
1712                 }
1713         }
1714         else {
1715                 /* disable eraser flags - so that we can switch modes during a session */
1716                 p->gpd->sbuffer_sflag &= ~GP_STROKE_ERASER;
1717
1718                 if (p->sa->spacetype == SPACE_VIEW3D) {
1719                         if (p->gpl->flag & GP_LAYER_NO_XRAY) {
1720                                 p->flags &= ~GP_PAINTFLAG_V3D_ERASER_DEPTH;
1721                         }
1722                 }
1723         }
1724
1725         /* set 'initial run' flag, which is only used to denote when a new stroke is starting */
1726         p->flags |= GP_PAINTFLAG_FIRSTRUN;
1727
1728
1729         /* when drawing in the camera view, in 2D space, set the subrect */
1730         p->subrect = NULL;
1731         if ((*p->align_flag & GP_PROJECT_VIEWSPACE) == 0) {
1732                 if (p->sa->spacetype == SPACE_VIEW3D) {
1733                         View3D *v3d = p->sa->spacedata.first;
1734                         RegionView3D *rv3d = p->ar->regiondata;
1735
1736                         /* for camera view set the subrect */
1737                         if (rv3d->persp == RV3D_CAMOB) {
1738                                 ED_view3d_calc_camera_border(p->scene, depsgraph, p->ar, v3d, rv3d, &p->subrect_data, true); /* no shift */
1739                                 p->subrect = &p->subrect_data;
1740                         }
1741                 }
1742         }
1743
1744         /* init stroke point space-conversion settings... */
1745         p->gsc.gpd = p->gpd;
1746         p->gsc.gpl = p->gpl;
1747
1748         p->gsc.sa = p->sa;
1749         p->gsc.ar = p->ar;
1750         p->gsc.v2d = p->v2d;
1751
1752         p->gsc.subrect_data = p->subrect_data;
1753         p->gsc.subrect = p->subrect;
1754
1755         copy_m4_m4(p->gsc.mat, p->mat);
1756
1757
1758         /* check if points will need to be made in view-aligned space */
1759         if (*p->align_flag & GP_PROJECT_VIEWSPACE) {
1760                 switch (p->sa->spacetype) {
1761                         case SPACE_VIEW3D:
1762                         {
1763                                 p->gpd->sbuffer_sflag |= GP_STROKE_3DSPACE;
1764                                 break;
1765                         }
1766                         case SPACE_NODE:
1767                         {
1768                                 p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1769                                 break;
1770                         }
1771                         case SPACE_SEQ:
1772                         {
1773                                 p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1774                                 break;
1775                         }
1776                         case SPACE_IMAGE:
1777                         {
1778                                 SpaceImage *sima = (SpaceImage *)p->sa->spacedata.first;
1779
1780                                 /* only set these flags if the image editor doesn't have an image active,
1781                                  * otherwise user will be confused by strokes not appearing after they're drawn
1782                                  *
1783                                  * Admittedly, this is a bit hacky, but it works much nicer from an ergonomic standpoint!
1784                                  */
1785                                 if (ELEM(NULL, sima, sima->image)) {
1786                                         /* make strokes be drawn in screen space */
1787                                         p->gpd->sbuffer_sflag &= ~GP_STROKE_2DSPACE;
1788                                         *(p->align_flag) &= ~GP_PROJECT_VIEWSPACE;
1789                                 }
1790                                 else {
1791                                         p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1792                                 }
1793                                 break;
1794                         }
1795                         case SPACE_CLIP:
1796                         {
1797                                 p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1798                                 break;
1799                         }
1800                 }
1801         }
1802 }
1803
1804 /* finish off a stroke (clears buffer, but doesn't finish the paint operation) */
1805 static void gp_paint_strokeend(tGPsdata *p)
1806 {
1807         ToolSettings *ts = p->scene->toolsettings;
1808         /* for surface sketching, need to set the right OpenGL context stuff so that
1809          * the conversions will project the values correctly...
1810          */
1811         if (gpencil_project_check(p)) {
1812                 View3D *v3d = p->sa->spacedata.first;
1813
1814                 /* need to restore the original projection settings before packing up */
1815                 view3d_region_operator_needs_opengl(p->win, p->ar);
1816                 ED_view3d_autodist_init(p->depsgraph, p->ar, v3d, (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE) ? 1 : 0);
1817         }
1818
1819         /* check if doing eraser or not */
1820         if ((p->gpd->sbuffer_sflag & GP_STROKE_ERASER) == 0) {
1821                 /* simplify stroke before transferring? */
1822                 gp_stroke_simplify(p);
1823
1824                 /* transfer stroke to frame */
1825                 gp_stroke_newfrombuffer(p);
1826         }
1827
1828         /* clean up buffer now */
1829         gp_session_validatebuffer(p);
1830 }
1831
1832 /* finish off stroke painting operation */
1833 static void gp_paint_cleanup(tGPsdata *p)
1834 {
1835         /* p->gpd==NULL happens when stroke failed to initialize,
1836          * for example when GP is hidden in current space (sergey)
1837          */
1838         if (p->gpd) {
1839                 /* finish off a stroke */
1840                 gp_paint_strokeend(p);
1841         }
1842
1843         /* "unlock" frame */
1844         if (p->gpf)
1845                 p->gpf->flag &= ~GP_FRAME_PAINT;
1846 }
1847
1848 /* ------------------------------- */
1849
1850 /* Helper callback for drawing the cursor itself */
1851 static void gpencil_draw_eraser(bContext *UNUSED(C), int x, int y, void *p_ptr)
1852 {
1853         tGPsdata *p = (tGPsdata *)p_ptr;
1854
1855         if (p->paintmode == GP_PAINTMODE_ERASER) {
1856                 Gwn_VertFormat *format = immVertexFormat();
1857                 const uint shdr_pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1858                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1859
1860                 glEnable(GL_LINE_SMOOTH);
1861                 glEnable(GL_BLEND);
1862                 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1863
1864                 immUniformColor4ub(255, 100, 100, 20);
1865                 imm_draw_circle_fill_2d(shdr_pos, x, y, p->radius, 40);
1866
1867                 immUnbindProgram();
1868
1869                 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
1870
1871                 float viewport_size[4];
1872                 glGetFloatv(GL_VIEWPORT, viewport_size);
1873                 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
1874
1875                 immUniformColor4f(1.0f, 0.39f, 0.39f, 0.78f);
1876                 immUniform1i("num_colors", 0);  /* "simple" mode */
1877                 immUniform1f("dash_width", 12.0f);
1878                 immUniform1f("dash_factor", 0.5f);
1879
1880                 imm_draw_circle_wire_2d(shdr_pos, x, y, p->radius,
1881                                      /* XXX Dashed shader gives bad results with sets of small segments currently,
1882                                       *     temp hack around the issue. :( */
1883                                      max_ii(8, p->radius / 2));  /* was fixed 40 */
1884
1885                 immUnbindProgram();
1886
1887                 glDisable(GL_BLEND);
1888                 glDisable(GL_LINE_SMOOTH);
1889         }
1890 }
1891
1892 /* Turn brush cursor in 3D view on/off */
1893 static void gpencil_draw_toggle_eraser_cursor(bContext *C, tGPsdata *p, short enable)
1894 {
1895         if (p->erasercursor && !enable) {
1896                 /* clear cursor */
1897                 WM_paint_cursor_end(CTX_wm_manager(C), p->erasercursor);
1898                 p->erasercursor = NULL;
1899         }
1900         else if (enable && !p->erasercursor) {
1901                 /* enable cursor */
1902                 p->erasercursor = WM_paint_cursor_activate(CTX_wm_manager(C),
1903                                                            NULL, /* XXX */
1904                                                            gpencil_draw_eraser, p);
1905         }
1906 }
1907
1908 /* Check if tablet eraser is being used (when processing events) */
1909 static bool gpencil_is_tablet_eraser_active(const wmEvent *event)
1910 {
1911         if (event->tablet_data) {
1912                 const wmTabletData *wmtab = event->tablet_data;
1913                 return (wmtab->Active == EVT_TABLET_ERASER);
1914         }
1915
1916         return false;
1917 }
1918
1919 /* ------------------------------- */
1920
1921 static void gpencil_draw_exit(bContext *C, wmOperator *op)
1922 {
1923         tGPsdata *p = op->customdata;
1924
1925         /* clear undo stack */
1926         gpencil_undo_finish();
1927
1928         /* restore cursor to indicate end of drawing */
1929         WM_cursor_modal_restore(CTX_wm_window(C));
1930
1931         /* don't assume that operator data exists at all */
1932         if (p) {
1933                 /* check size of buffer before cleanup, to determine if anything happened here */
1934                 if (p->paintmode == GP_PAINTMODE_ERASER) {
1935                         /* turn off radial brush cursor */
1936                         gpencil_draw_toggle_eraser_cursor(C, p, false);
1937                 }
1938
1939                 /* always store the new eraser size to be used again next time
1940                  * NOTE: Do this even when not in eraser mode, as eraser may
1941                  *       have been toggled at some point.
1942                  */
1943                 U.gp_eraser = p->radius;
1944
1945                 /* cleanup */
1946                 gp_paint_cleanup(p);
1947                 gp_session_cleanup(p);
1948
1949                 /* finally, free the temp data */
1950                 MEM_freeN(p);
1951         }
1952
1953         op->customdata = NULL;
1954 }
1955
1956 static void gpencil_draw_cancel(bContext *C, wmOperator *op)
1957 {
1958         /* this is just a wrapper around exit() */
1959         gpencil_draw_exit(C, op);
1960 }
1961
1962 /* ------------------------------- */
1963
1964
1965 static int gpencil_draw_init(bContext *C, wmOperator *op, const wmEvent *event)
1966 {
1967         tGPsdata *p;
1968         eGPencil_PaintModes paintmode = RNA_enum_get(op->ptr, "mode");
1969
1970         /* check context */
1971         p = op->customdata = gp_session_initpaint(C);
1972         if ((p == NULL) || (p->status == GP_STATUS_ERROR)) {
1973                 /* something wasn't set correctly in context */
1974                 gpencil_draw_exit(C, op);
1975                 return 0;
1976         }
1977
1978         /* init painting data */
1979         gp_paint_initstroke(p, paintmode, CTX_data_depsgraph(C));
1980         if (p->status == GP_STATUS_ERROR) {
1981                 gpencil_draw_exit(C, op);
1982                 return 0;
1983         }
1984
1985         if (event != NULL) {
1986                 p->keymodifier = event->keymodifier;
1987         }
1988         else {
1989                 p->keymodifier = -1;
1990         }
1991
1992         /* everything is now setup ok */
1993         return 1;
1994 }
1995
1996
1997 /* ------------------------------- */
1998
1999 /* ensure that the correct cursor icon is set */
2000 static void gpencil_draw_cursor_set(tGPsdata *p)
2001 {
2002         if (p->paintmode == GP_PAINTMODE_ERASER)
2003                 WM_cursor_modal_set(p->win, BC_CROSSCURSOR);  /* XXX need a better cursor */
2004         else
2005                 WM_cursor_modal_set(p->win, BC_PAINTBRUSHCURSOR);
2006 }
2007
2008 /* update UI indicators of status, including cursor and header prints */
2009 static void gpencil_draw_status_indicators(tGPsdata *p)
2010 {
2011         /* header prints */
2012         switch (p->status) {
2013                 case GP_STATUS_PAINTING:
2014                         /* only print this for paint-sessions, otherwise it gets annoying */
2015                         if (GPENCIL_SKETCH_SESSIONS_ON(p->scene))
2016                                 ED_area_headerprint(p->sa, IFACE_("Grease Pencil: Drawing/erasing stroke... Release to end stroke"));
2017                         break;
2018
2019                 case GP_STATUS_IDLING:
2020                         /* print status info */
2021                         switch (p->paintmode) {
2022                                 case GP_PAINTMODE_ERASER:
2023                                         ED_area_headerprint(p->sa, IFACE_("Grease Pencil Erase Session: Hold and drag LMB or RMB to erase | "
2024                                                                           "ESC/Enter to end  (or click outside this area)"));
2025                                         break;
2026                                 case GP_PAINTMODE_DRAW_STRAIGHT:
2027                                         ED_area_headerprint(p->sa, IFACE_("Grease Pencil Line Session: Hold and drag LMB to draw | "
2028                                                                           "ESC/Enter to end  (or click outside this area)"));
2029                                         break;
2030                                 case GP_PAINTMODE_DRAW:
2031                                         ED_area_headerprint(p->sa, IFACE_("Grease Pencil Freehand Session: Hold and drag LMB to draw | "
2032                                                                           "E/ESC/Enter to end  (or click outside this area)"));
2033                                         break;
2034                                 case GP_PAINTMODE_DRAW_POLY:
2035                                         ED_area_headerprint(p->sa, IFACE_("Grease Pencil Poly Session: LMB click to place next stroke vertex | "
2036                                                                           "ESC/Enter to end  (or click outside this area)"));
2037                                         break;
2038
2039                                 default: /* unhandled future cases */
2040                                         ED_area_headerprint(p->sa, IFACE_("Grease Pencil Session: ESC/Enter to end   (or click outside this area)"));
2041                                         break;
2042                         }
2043                         break;
2044
2045                 case GP_STATUS_ERROR:
2046                 case GP_STATUS_DONE:
2047                         /* clear status string */
2048                         ED_area_headerprint(p->sa, NULL);
2049                         break;
2050         }
2051 }
2052
2053 /* ------------------------------- */
2054
2055 /* create a new stroke point at the point indicated by the painting context */
2056 static void gpencil_draw_apply(wmOperator *op, tGPsdata *p, Depsgraph *depsgraph)
2057 {
2058         /* handle drawing/erasing -> test for erasing first */
2059         if (p->paintmode == GP_PAINTMODE_ERASER) {
2060                 /* do 'live' erasing now */
2061                 gp_stroke_doeraser(p);
2062
2063                 /* store used values */
2064                 p->mvalo[0] = p->mval[0];
2065                 p->mvalo[1] = p->mval[1];
2066                 p->opressure = p->pressure;
2067         }
2068         /* only add current point to buffer if mouse moved (even though we got an event, it might be just noise) */
2069         else if (gp_stroke_filtermval(p, p->mval, p->mvalo)) {
2070                 /* try to add point */
2071                 short ok = gp_stroke_addpoint(p, p->mval, p->pressure, p->curtime);
2072
2073                 /* handle errors while adding point */
2074                 if ((ok == GP_STROKEADD_FULL) || (ok == GP_STROKEADD_OVERFLOW)) {
2075                         /* finish off old stroke */
2076                         gp_paint_strokeend(p);
2077                         /* And start a new one!!! Else, projection errors! */
2078                         gp_paint_initstroke(p, p->paintmode, depsgraph);
2079
2080                         /* start a new stroke, starting from previous point */
2081                         /* XXX Must manually reset inittime... */
2082                         /* XXX We only need to reuse previous point if overflow! */
2083                         if (ok == GP_STROKEADD_OVERFLOW) {
2084                                 p->inittime = p->ocurtime;
2085                                 gp_stroke_addpoint(p, p->mvalo, p->opressure, p->ocurtime);
2086                         }
2087                         else {
2088                                 p->inittime = p->curtime;
2089                         }
2090                         gp_stroke_addpoint(p, p->mval, p->pressure, p->curtime);
2091                 }
2092                 else if (ok == GP_STROKEADD_INVALID) {
2093                         /* the painting operation cannot continue... */
2094                         BKE_report(op->reports, RPT_ERROR, "Cannot paint stroke");
2095                         p->status = GP_STATUS_ERROR;
2096
2097                         if (G.debug & G_DEBUG)
2098                                 printf("Error: Grease-Pencil Paint - Add Point Invalid\n");
2099                         return;
2100                 }
2101
2102                 /* store used values */
2103                 p->mvalo[0] = p->mval[0];
2104                 p->mvalo[1] = p->mval[1];
2105                 p->opressure = p->pressure;
2106                 p->ocurtime = p->curtime;
2107         }
2108 }
2109
2110 /* handle draw event */
2111 static void gpencil_draw_apply_event(wmOperator *op, const wmEvent *event, Depsgraph *depsgraph)
2112 {
2113         tGPsdata *p = op->customdata;
2114         PointerRNA itemptr;
2115         float mousef[2];
2116         int tablet = 0;
2117
2118         /* convert from window-space to area-space mouse coordinates
2119          * NOTE: float to ints conversions, +1 factor is probably used to ensure a bit more accurate rounding...
2120          */
2121         p->mval[0] = event->mval[0] + 1;
2122         p->mval[1] = event->mval[1] + 1;
2123
2124         /* verify key status for straight lines */
2125         if ((event->ctrl > 0) || (event->alt > 0)) {
2126                 if (p->straight[0] == 0) {
2127                         int dx = abs(p->mval[0] - p->mvalo[0]);
2128                         int dy = abs(p->mval[1] - p->mvalo[1]);
2129                         if ((dx > 0) || (dy > 0)) {
2130                                 /* check mouse direction to replace the other coordinate with previous values */
2131                                 if (dx >= dy) {
2132                                         /* horizontal */
2133                                         p->straight[0] = 1;
2134                                         p->straight[1] = p->mval[1]; /* save y */
2135                                 }
2136                                 else {
2137                                         /* vertical */
2138                                         p->straight[0] = 2;
2139                                         p->straight[1] = p->mval[0]; /* save x */
2140                                 }
2141                         }
2142                 }
2143         }
2144         else {
2145                 p->straight[0] = 0;
2146         }
2147
2148         p->curtime = PIL_check_seconds_timer();
2149
2150         /* handle pressure sensitivity (which is supplied by tablets) */
2151         if (event->tablet_data) {
2152                 const wmTabletData *wmtab = event->tablet_data;
2153
2154                 tablet = (wmtab->Active != EVT_TABLET_NONE);
2155                 p->pressure = wmtab->Pressure;
2156
2157                 /* Hack for pressure sensitive eraser on D+RMB when using a tablet:
2158                  *  The pen has to float over the tablet surface, resulting in
2159                  *  zero pressure (T47101). Ignore pressure values if floating
2160                  *  (i.e. "effectively zero" pressure), and only when the "active"
2161                  *  end is the stylus (i.e. the default when not eraser)
2162                  */
2163                 if (p->paintmode == GP_PAINTMODE_ERASER) {
2164                         if ((wmtab->Active != EVT_TABLET_ERASER) && (p->pressure < 0.001f)) {
2165                                 p->pressure = 1.0f;
2166                         }
2167                 }
2168         }
2169         else {
2170                 /* No tablet data -> No pressure info is available */
2171                 p->pressure = 1.0f;
2172         }
2173
2174         /* special exception for start of strokes (i.e. maybe for just a dot) */
2175         if (p->flags & GP_PAINTFLAG_FIRSTRUN) {
2176                 p->flags &= ~GP_PAINTFLAG_FIRSTRUN;
2177
2178                 p->mvalo[0] = p->mval[0];
2179                 p->mvalo[1] = p->mval[1];
2180                 p->opressure = p->pressure;
2181                 p->inittime = p->ocurtime = p->curtime;
2182                 p->straight[0] = 0;
2183                 p->straight[1] = 0;
2184
2185                 /* special exception here for too high pressure values on first touch in
2186                  *  windows for some tablets, then we just skip first touch...
2187                  */
2188                 if (tablet && (p->pressure >= 0.99f))
2189                         return;
2190         }
2191
2192         /* check if alt key is pressed and limit to straight lines */
2193         if (p->straight[0] != 0) {
2194                 if (p->straight[0] == 1) {
2195                         /* horizontal */
2196                         p->mval[1] = p->straight[1]; /* replace y */
2197                 }
2198                 else {
2199                         /* vertical */
2200                         p->mval[0] = p->straight[1]; /* replace x */
2201                 }
2202         }
2203
2204         /* fill in stroke data (not actually used directly by gpencil_draw_apply) */
2205         RNA_collection_add(op->ptr, "stroke", &itemptr);
2206
2207         mousef[0] = p->mval[0];
2208         mousef[1] = p->mval[1];
2209         RNA_float_set_array(&itemptr, "mouse", mousef);
2210         RNA_float_set(&itemptr, "pressure", p->pressure);
2211         RNA_boolean_set(&itemptr, "is_start", (p->flags & GP_PAINTFLAG_FIRSTRUN) != 0);
2212
2213         RNA_float_set(&itemptr, "time", p->curtime - p->inittime);
2214
2215         /* apply the current latest drawing point */
2216         gpencil_draw_apply(op, p, depsgraph);
2217
2218         /* force refresh */
2219         ED_region_tag_redraw(p->ar); /* just active area for now, since doing whole screen is too slow */
2220 }
2221
2222 /* ------------------------------- */
2223
2224 /* operator 'redo' (i.e. after changing some properties, but also for repeat last) */
2225 static int gpencil_draw_exec(bContext *C, wmOperator *op)
2226 {
2227         tGPsdata *p = NULL;
2228         Depsgraph *depsgraph = CTX_data_depsgraph(C);
2229
2230         /* printf("GPencil - Starting Re-Drawing\n"); */
2231
2232         /* try to initialize context data needed while drawing */
2233         if (!gpencil_draw_init(C, op, NULL)) {
2234                 if (op->customdata) MEM_freeN(op->customdata);
2235                 /* printf("\tGP - no valid data\n"); */
2236                 return OPERATOR_CANCELLED;
2237         }
2238         else
2239                 p = op->customdata;
2240
2241         /* printf("\tGP - Start redrawing stroke\n"); */
2242
2243         /* loop over the stroke RNA elements recorded (i.e. progress of mouse movement),
2244          * setting the relevant values in context at each step, then applying
2245          */
2246         RNA_BEGIN (op->ptr, itemptr, "stroke")
2247         {
2248                 float mousef[2];
2249
2250                 /* printf("\t\tGP - stroke elem\n"); */
2251
2252                 /* get relevant data for this point from stroke */
2253                 RNA_float_get_array(&itemptr, "mouse", mousef);
2254                 p->mval[0] = (int)mousef[0];
2255                 p->mval[1] = (int)mousef[1];
2256                 p->pressure = RNA_float_get(&itemptr, "pressure");
2257                 p->curtime = (double)RNA_float_get(&itemptr, "time") + p->inittime;
2258
2259                 if (RNA_boolean_get(&itemptr, "is_start")) {
2260                         /* if first-run flag isn't set already (i.e. not true first stroke),
2261                          * then we must terminate the previous one first before continuing
2262                          */
2263                         if ((p->flags & GP_PAINTFLAG_FIRSTRUN) == 0) {
2264                                 /* TODO: both of these ops can set error-status, but we probably don't need to worry */
2265                                 gp_paint_strokeend(p);
2266                                 gp_paint_initstroke(p, p->paintmode, depsgraph);
2267                         }
2268                 }
2269
2270                 /* if first run, set previous data too */
2271                 if (p->flags & GP_PAINTFLAG_FIRSTRUN) {
2272                         p->flags &= ~GP_PAINTFLAG_FIRSTRUN;
2273
2274                         p->mvalo[0] = p->mval[0];
2275                         p->mvalo[1] = p->mval[1];
2276                         p->opressure = p->pressure;
2277                         p->ocurtime = p->curtime;
2278                 }
2279
2280                 /* apply this data as necessary now (as per usual) */
2281                 gpencil_draw_apply(op, p, depsgraph);
2282         }
2283         RNA_END;
2284
2285         /* printf("\tGP - done\n"); */
2286
2287         /* cleanup */
2288         gpencil_draw_exit(C, op);
2289
2290         /* refreshes */
2291         WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
2292
2293         /* done */
2294         return OPERATOR_FINISHED;
2295 }
2296
2297 /* ------------------------------- */
2298
2299 /* start of interactive drawing part of operator */
2300 static int gpencil_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2301 {
2302         tGPsdata *p = NULL;
2303
2304         if (G.debug & G_DEBUG)
2305                 printf("GPencil - Starting Drawing\n");
2306
2307         /* try to initialize context data needed while drawing */
2308         if (!gpencil_draw_init(C, op, event)) {
2309                 if (op->customdata)
2310                         MEM_freeN(op->customdata);
2311                 if (G.debug & G_DEBUG)
2312                         printf("\tGP - no valid data\n");
2313                 return OPERATOR_CANCELLED;
2314         }
2315         else
2316                 p = op->customdata;
2317
2318         /* TODO: set any additional settings that we can take from the events?
2319          * TODO? if tablet is erasing, force eraser to be on? */
2320
2321         /* TODO: move cursor setting stuff to stroke-start so that paintmode can be changed midway... */
2322
2323         /* if eraser is on, draw radial aid */
2324         if (p->paintmode == GP_PAINTMODE_ERASER) {
2325                 gpencil_draw_toggle_eraser_cursor(C, p, true);
2326         }
2327         /* set cursor
2328          * NOTE: This may change later (i.e. intentionally via brush toggle,
2329          *       or unintentionally if the user scrolls outside the area)...
2330          */
2331         gpencil_draw_cursor_set(p);
2332
2333         /* only start drawing immediately if we're allowed to do so... */
2334         if (RNA_boolean_get(op->ptr, "wait_for_input") == false) {
2335                 /* hotkey invoked - start drawing */
2336                 /* printf("\tGP - set first spot\n"); */
2337                 p->status = GP_STATUS_PAINTING;
2338
2339                 /* handle the initial drawing - i.e. for just doing a simple dot */
2340                 gpencil_draw_apply_event(op, event, CTX_data_depsgraph(C));
2341                 op->flag |= OP_IS_MODAL_CURSOR_REGION;
2342         }
2343         else {
2344                 /* toolbar invoked - don't start drawing yet... */
2345                 /* printf("\tGP - hotkey invoked... waiting for click-drag\n"); */
2346                 op->flag |= OP_IS_MODAL_CURSOR_REGION;
2347         }
2348
2349         WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
2350         /* add a modal handler for this operator, so that we can then draw continuous strokes */
2351         WM_event_add_modal_handler(C, op);
2352         return OPERATOR_RUNNING_MODAL;
2353 }
2354
2355 /* gpencil modal operator stores area, which can be removed while using it (like fullscreen) */
2356 static bool gpencil_area_exists(bContext *C, ScrArea *sa_test)
2357 {
2358         bScreen *sc = CTX_wm_screen(C);
2359         return (BLI_findindex(&sc->areabase, sa_test) != -1);
2360 }
2361
2362 static tGPsdata *gpencil_stroke_begin(bContext *C, wmOperator *op)
2363 {
2364         tGPsdata *p = op->customdata;
2365
2366         /* we must check that we're still within the area that we're set up to work from
2367          * otherwise we could crash (see bug #20586)
2368          */
2369         if (CTX_wm_area(C) != p->sa) {
2370                 printf("\t\t\tGP - wrong area execution abort!\n");
2371                 p->status = GP_STATUS_ERROR;
2372         }
2373
2374         /* printf("\t\tGP - start stroke\n"); */
2375
2376         /* we may need to set up paint env again if we're resuming */
2377         /* XXX: watch it with the paintmode! in future,
2378          *      it'd be nice to allow changing paint-mode when in sketching-sessions */
2379
2380         if (gp_session_initdata(C, p))
2381                 gp_paint_initstroke(p, p->paintmode, CTX_data_depsgraph(C));
2382
2383         if (p->status != GP_STATUS_ERROR) {
2384                 p->status = GP_STATUS_PAINTING;
2385                 op->flag &= ~OP_IS_MODAL_CURSOR_REGION;
2386         }
2387
2388         return op->customdata;
2389 }
2390
2391 static void gpencil_stroke_end(wmOperator *op)
2392 {
2393         tGPsdata *p = op->customdata;
2394
2395         gp_paint_cleanup(p);
2396
2397         gpencil_undo_push(p->gpd);
2398
2399         gp_session_cleanup(p);
2400
2401         p->status = GP_STATUS_IDLING;
2402         op->flag |= OP_IS_MODAL_CURSOR_REGION;
2403
2404         p->gpd = NULL;
2405         p->gpl = NULL;
2406         p->gpf = NULL;
2407 }
2408
2409 /* Move last stroke in the listbase to the head to be drawn below all previous strokes in the layer */
2410 static void gpencil_move_last_stroke_to_back(bContext *C)
2411 {
2412         /* move last stroke (the polygon) to head of the listbase stroke to draw on back of all previous strokes */
2413         bGPdata *gpd = ED_gpencil_data_get_active(C);
2414         bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
2415
2416         /* sanity checks */
2417         if (ELEM(NULL, gpd, gpl, gpl->actframe)) {
2418                 return;
2419         }
2420
2421         bGPDframe *gpf = gpl->actframe;
2422         bGPDstroke *gps = gpf->strokes.last;
2423         if (ELEM(NULL, gps)) {
2424                 return;
2425         }
2426
2427         BLI_remlink(&gpf->strokes, gps);
2428         BLI_insertlinkbefore(&gpf->strokes, gpf->strokes.first, gps);
2429 }
2430
2431 /* events handling during interactive drawing part of operator */
2432 static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event)
2433 {
2434         tGPsdata *p = op->customdata;
2435         ToolSettings *ts = CTX_data_tool_settings(C);
2436         int estate = OPERATOR_PASS_THROUGH; /* default exit state - pass through to support MMB view nav, etc. */
2437
2438         /* if (event->type == NDOF_MOTION)
2439          *    return OPERATOR_PASS_THROUGH;
2440          * -------------------------------
2441          * [mce] Not quite what I was looking
2442          * for, but a good start! GP continues to
2443          * draw on the screen while the 3D mouse
2444          * moves the viewpoint. Problem is that
2445          * the stroke is converted to 3D only after
2446          * it is finished. This approach should work
2447          * better in tools that immediately apply
2448          * in 3D space.
2449          */
2450
2451         if (p->status == GP_STATUS_IDLING) {
2452                 ARegion *ar = CTX_wm_region(C);
2453                 p->ar = ar;
2454         }
2455
2456         /* we don't pass on key events, GP is used with key-modifiers - prevents Dkey to insert drivers */
2457         if (ISKEYBOARD(event->type)) {
2458                 if (ELEM(event->type, LEFTARROWKEY, DOWNARROWKEY, RIGHTARROWKEY, UPARROWKEY, ZKEY)) {
2459                         /* allow some keys:
2460                          *   - for frame changing [#33412]
2461                          *   - for undo (during sketching sessions)
2462                          */
2463                 }
2464                 else if (ELEM(event->type, PAD0, PAD1, PAD2, PAD3, PAD4, PAD5, PAD6, PAD7, PAD8, PAD9)) {
2465                         /* allow numpad keys so that camera/view manipulations can still take place
2466                          * - PAD0 in particular is really important for Grease Pencil drawing,
2467                          *   as animators may be working "to camera", so having this working
2468                          *   is essential for ensuring that they can quickly return to that view
2469                          */
2470                 }
2471                 else if ((ELEM(event->type, p->keymodifier)) && (event->val == KM_RELEASE)) {
2472                         /* enable continuous if release D key in mid drawing */
2473                         p->scene->toolsettings->gpencil_flags |= GP_TOOL_FLAG_PAINTSESSIONS_ON;
2474                 }
2475                 else if ((event->type == BKEY) && (event->val == KM_RELEASE)) {
2476                         /* Add Blank Frame
2477                          * - Since this operator is non-modal, we can just call it here, and keep going...
2478                  * - This operator is especially useful when animating
2479                          */
2480                         WM_operator_name_call(C, "GPENCIL_OT_blank_frame_add", WM_OP_EXEC_DEFAULT, NULL);
2481                         estate = OPERATOR_RUNNING_MODAL;
2482                 }
2483                 else {
2484                         estate = OPERATOR_RUNNING_MODAL;
2485                 }
2486         }
2487
2488         //printf("\tGP - handle modal event...\n");
2489
2490         /* exit painting mode (and/or end current stroke)
2491          * NOTE: cannot do RIGHTMOUSE (as is standard for canceling) as that would break polyline [#32647]
2492          */
2493         if (ELEM(event->type, RETKEY, PADENTER, ESCKEY, SPACEKEY, EKEY)) {
2494                 /* exit() ends the current stroke before cleaning up */
2495                 /* printf("\t\tGP - end of paint op + end of stroke\n"); */
2496                 /* if drawing polygon and enable on back, must move stroke */
2497                 if (ts) {
2498                         if ((ts->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode == GP_PAINTMODE_DRAW_POLY)) {
2499                                 if (p->flags & GP_PAINTFLAG_STROKEADDED) {
2500                                         gpencil_move_last_stroke_to_back(C);
2501                                 }
2502                         }
2503                 }
2504                 p->status = GP_STATUS_DONE;
2505                 estate = OPERATOR_FINISHED;
2506         }
2507
2508         /* toggle painting mode upon mouse-button movement
2509          *  - LEFTMOUSE  = standard drawing (all) / straight line drawing (all) / polyline (toolbox only)
2510          *  - RIGHTMOUSE = polyline (hotkey) / eraser (all)
2511          *    (Disabling RIGHTMOUSE case here results in bugs like [#32647])
2512          * also making sure we have a valid event value, to not exit too early
2513          */
2514         if (ELEM(event->type, LEFTMOUSE, RIGHTMOUSE) && (event->val != KM_NOTHING)) {
2515                 /* if painting, end stroke */
2516                 if (p->status == GP_STATUS_PAINTING) {
2517                         int sketch = 0;
2518
2519                         /* basically, this should be mouse-button up = end stroke
2520                          * BUT what happens next depends on whether we 'painting sessions' is enabled
2521                          */
2522                         sketch |= GPENCIL_SKETCH_SESSIONS_ON(p->scene);
2523                         /* polyline drawing is also 'sketching' -- all knots should be added during one session */
2524                         sketch |= (p->paintmode == GP_PAINTMODE_DRAW_POLY);
2525
2526                         if (sketch) {
2527                                 /* end stroke only, and then wait to resume painting soon */
2528                                 /* printf("\t\tGP - end stroke only\n"); */
2529                                 gpencil_stroke_end(op);
2530
2531                                 /* If eraser mode is on, turn it off after the stroke finishes
2532                                  * NOTE: This just makes it nicer to work with drawing sessions
2533                                  */
2534                                 if (p->paintmode == GP_PAINTMODE_ERASER) {
2535                                         p->paintmode = RNA_enum_get(op->ptr, "mode");
2536
2537                                         /* if the original mode was *still* eraser,
2538                                          * we'll let it say for now, since this gives
2539                                          * users an opportunity to have visual feedback
2540                                          * when adjusting eraser size
2541                                          */
2542                                         if (p->paintmode != GP_PAINTMODE_ERASER) {
2543                                                 /* turn off cursor...
2544                                                  * NOTE: this should be enough for now
2545                                                  *       Just hiding this makes it seem like
2546                                                  *       you can paint again...
2547                                                  */
2548                                                 gpencil_draw_toggle_eraser_cursor(C, p, false);
2549                                         }
2550                                 }
2551
2552                                 /* we've just entered idling state, so this event was processed (but no others yet) */
2553                                 estate = OPERATOR_RUNNING_MODAL;
2554
2555                                 /* stroke could be smoothed, send notifier to refresh screen */
2556                                 WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
2557                         }
2558                         else {
2559                                 /* printf("\t\tGP - end of stroke + op\n"); */
2560                                 /* if drawing polygon and enable on back, must move stroke */
2561                                 if (ts) {
2562                                         if ((ts->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode == GP_PAINTMODE_DRAW_POLY)) {
2563                                                 if (p->flags & GP_PAINTFLAG_STROKEADDED) {
2564                                                         gpencil_move_last_stroke_to_back(C);
2565                                                 }
2566                                         }
2567                                 }
2568                                 p->status = GP_STATUS_DONE;
2569                                 estate = OPERATOR_FINISHED;
2570                         }
2571                 }
2572                 else if (event->val == KM_PRESS) {
2573                         bool in_bounds = false;
2574
2575                         /* Check if we're outside the bounds of the active region
2576                          * NOTE: An exception here is that if launched from the toolbar,
2577                          *       whatever region we're now in should become the new region
2578                          */
2579                         if ((p->ar) && (p->ar->regiontype == RGN_TYPE_TOOLS)) {
2580                                 /* Change to whatever region is now under the mouse */
2581                                 ARegion *current_region = BKE_area_find_region_xy(p->sa, RGN_TYPE_ANY, event->x, event->y);
2582
2583                                 if (G.debug & G_DEBUG) {
2584                                         printf("found alternative region %p (old was %p) - at %d %d (sa: %d %d -> %d %d)\n",
2585                                                 current_region, p->ar, event->x, event->y,
2586                                                 p->sa->totrct.xmin, p->sa->totrct.ymin, p->sa->totrct.xmax, p->sa->totrct.ymax);
2587                                 }
2588
2589                                 if (current_region) {
2590                                         /* Assume that since we found the cursor in here, it is in bounds
2591                                          * and that this should be the region that we begin drawing in
2592                                          */
2593                                         p->ar = current_region;
2594                                         in_bounds = true;
2595                                 }
2596                                 else {
2597                                         /* Out of bounds, or invalid in some other way */
2598                                         p->status = GP_STATUS_ERROR;
2599                                         estate = OPERATOR_CANCELLED;
2600
2601                                         if (G.debug & G_DEBUG)
2602                                                 printf("%s: Region under cursor is out of bounds, so cannot be drawn on\n", __func__);
2603                                 }
2604                         }
2605                         else if (p->ar) {
2606                                 rcti region_rect;
2607
2608                                 /* Perform bounds check using  */
2609                                 ED_region_visible_rect(p->ar, &region_rect);
2610                                 in_bounds = BLI_rcti_isect_pt_v(&region_rect, event->mval);
2611                         }
2612                         else {
2613                                 /* No region */
2614                                 p->status = GP_STATUS_ERROR;
2615                                 estate = OPERATOR_CANCELLED;
2616
2617                                 if (G.debug & G_DEBUG)
2618                                         printf("%s: No active region found in GP Paint session data\n", __func__);
2619                         }
2620
2621                         if (in_bounds) {
2622                                 /* Switch paintmode (temporarily if need be) based on which button was used
2623                                  * NOTE: This is to make it more convenient to erase strokes when using drawing sessions
2624                                  */
2625                                 if ((event->type == RIGHTMOUSE) || gpencil_is_tablet_eraser_active(event)) {
2626                                         /* turn on eraser */
2627                                         p->paintmode = GP_PAINTMODE_ERASER;
2628                                 }
2629                                 else if (event->type == LEFTMOUSE) {
2630                                         /* restore drawmode to default */
2631                                         p->paintmode = RNA_enum_get(op->ptr, "mode");
2632                                 }
2633
2634                                 gpencil_draw_toggle_eraser_cursor(C, p, p->paintmode == GP_PAINTMODE_ERASER);
2635
2636                                 /* not painting, so start stroke (this should be mouse-button down) */
2637                                 p = gpencil_stroke_begin(C, op);
2638
2639                                 if (p->status == GP_STATUS_ERROR) {
2640                                         estate = OPERATOR_CANCELLED;
2641                                 }
2642                         }
2643                         else if (p->status != GP_STATUS_ERROR) {
2644                                 /* User clicked outside bounds of window while idling, so exit paintmode
2645                                  * NOTE: Don't enter this case if an error occurred while finding the
2646                                  *       region (as above)
2647                                  */
2648                                 /* if drawing polygon and enable on back, must move stroke */
2649                                 if (ts) {
2650                                         if ((ts->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode == GP_PAINTMODE_DRAW_POLY)) {
2651                                                 if (p->flags & GP_PAINTFLAG_STROKEADDED) {
2652                                                         gpencil_move_last_stroke_to_back(C);
2653                                                 }
2654                                         }
2655                                 }
2656                                 p->status = GP_STATUS_DONE;
2657                                 estate = OPERATOR_FINISHED;
2658                         }
2659                 }
2660                 else {
2661                         p->status = GP_STATUS_IDLING;
2662                         op->flag |= OP_IS_MODAL_CURSOR_REGION;
2663                 }
2664         }
2665
2666         /* handle mode-specific events */
2667         if (p->status == GP_STATUS_PAINTING) {
2668                 /* handle painting mouse-movements? */
2669                 if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE) || (p->flags & GP_PAINTFLAG_FIRSTRUN)) {
2670                         /* handle drawing event */
2671                         /* printf("\t\tGP - add point\n"); */
2672                         gpencil_draw_apply_event(op, event, CTX_data_depsgraph(C));
2673
2674                         /* finish painting operation if anything went wrong just now */
2675                         if (p->status == GP_STATUS_ERROR) {
2676                                 printf("\t\t\t\tGP - add error done!\n");
2677                                 estate = OPERATOR_CANCELLED;
2678                         }
2679                         else {
2680                                 /* event handled, so just tag as running modal */
2681                                 /* printf("\t\t\t\tGP - add point handled!\n"); */
2682                                 estate = OPERATOR_RUNNING_MODAL;
2683                         }
2684                 }
2685                 /* eraser size */
2686                 else if ((p->paintmode == GP_PAINTMODE_ERASER) &&
2687                          ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE, PADPLUSKEY, PADMINUS))
2688                 {
2689                         /* just resize the brush (local version)
2690                          * TODO: fix the hardcoded size jumps (set to make a visible difference) and hardcoded keys
2691                          */
2692                         /* printf("\t\tGP - resize eraser\n"); */
2693                         switch (event->type) {
2694                                 case WHEELDOWNMOUSE: /* larger */
2695                                 case PADPLUSKEY:
2696                                         p->radius += 5;
2697                                         break;
2698
2699                                 case WHEELUPMOUSE: /* smaller */
2700                                 case PADMINUS:
2701                                         p->radius -= 5;
2702
2703                                         if (p->radius <= 0)
2704                                                 p->radius = 1;
2705                                         break;
2706                         }
2707
2708                         /* force refresh */
2709                         ED_region_tag_redraw(p->ar); /* just active area for now, since doing whole screen is too slow */
2710
2711                         /* event handled, so just tag as running modal */
2712                         estate = OPERATOR_RUNNING_MODAL;
2713                 }
2714                 /* there shouldn't be any other events, but just in case there are, let's swallow them
2715                  * (i.e. to prevent problems with undo)
2716                  */
2717                 else {
2718                         /* swallow event to save ourselves trouble */
2719                         estate = OPERATOR_RUNNING_MODAL;
2720                 }
2721         }
2722
2723         /* gpencil modal operator stores area, which can be removed while using it (like fullscreen) */
2724         if (0 == gpencil_area_exists(C, p->sa))
2725                 estate = OPERATOR_CANCELLED;
2726         else {
2727                 /* update status indicators - cursor, header, etc. */
2728                 gpencil_draw_status_indicators(p);
2729                 gpencil_draw_cursor_set(p); /* cursor may have changed outside our control - T44084 */
2730         }
2731
2732         /* process last operations before exiting */
2733         switch (estate) {
2734                 case OPERATOR_FINISHED:
2735                         /* one last flush before we're done */
2736                         gpencil_draw_exit(C, op);
2737                         WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
2738                         break;
2739
2740                 case OPERATOR_CANCELLED:
2741                         gpencil_draw_exit(C, op);
2742                         break;
2743
2744                 case OPERATOR_RUNNING_MODAL | OPERATOR_PASS_THROUGH:
2745                         /* event doesn't need to be handled */
2746 #if 0
2747                         printf("unhandled event -> %d (mmb? = %d | mmv? = %d)\n",
2748                                event->type, event->type == MIDDLEMOUSE, event->type==MOUSEMOVE);
2749 #endif
2750                         break;
2751         }
2752
2753         /* return status code */
2754         return estate;
2755 }
2756
2757 /* ------------------------------- */
2758
2759 static const EnumPropertyItem prop_gpencil_drawmodes[] = {
2760         {GP_PAINTMODE_DRAW, "DRAW", 0, "Draw Freehand", "Draw freehand stroke(s)"},
2761         {GP_PAINTMODE_DRAW_STRAIGHT, "DRAW_STRAIGHT", 0, "Draw Straight Lines", "Draw straight line segment(s)"},
2762         {GP_PAINTMODE_DRAW_POLY, "DRAW_POLY", 0, "Draw Poly Line", "Click to place endpoints of straight line segments (connected)"},
2763         {GP_PAINTMODE_ERASER, "ERASER", 0, "Eraser", "Erase Grease Pencil strokes"},
2764         {0, NULL, 0, NULL, NULL}
2765 };
2766
2767 void GPENCIL_OT_draw(wmOperatorType *ot)
2768 {
2769         PropertyRNA *prop;
2770
2771         /* identifiers */
2772         ot->name = "Grease Pencil Draw";
2773         ot->idname = "GPENCIL_OT_draw";
2774         ot->description = "Make annotations on the active data";
2775
2776         /* api callbacks */
2777         ot->exec = gpencil_draw_exec;
2778         ot->invoke = gpencil_draw_invoke;
2779         ot->modal = gpencil_draw_modal;
2780         ot->cancel = gpencil_draw_cancel;
2781         ot->poll = gpencil_draw_poll;
2782
2783         /* flags */
2784         ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING;
2785
2786         /* settings for drawing */
2787         ot->prop = RNA_def_enum(ot->srna, "mode", prop_gpencil_drawmodes, 0, "Mode", "Way to interpret mouse movements");
2788
2789         prop = RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2790         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
2791
2792         /* NOTE: wait for input is enabled by default, so that all UI code can work properly without needing users to know about this */
2793         prop = RNA_def_boolean(ot->srna, "wait_for_input", true, "Wait for Input", "Wait for first click instead of painting immediately");
2794         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2795 }