Code cleanup - Removing/commenting out various bits of legacy cruft related to
[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
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
43 #include "BKE_gpencil.h"
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_report.h"
47 #include "BKE_tracking.h"
48
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_gpencil_types.h"
52 #include "DNA_windowmanager_types.h"
53
54 #include "UI_view2d.h"
55
56 #include "ED_gpencil.h"
57 #include "ED_screen.h"
58 #include "ED_view3d.h"
59
60 #include "RNA_access.h"
61
62 #include "RNA_define.h"
63 #include "WM_api.h"
64 #include "WM_types.h"
65
66 #include "gpencil_intern.h"
67
68 /* ******************************************* */
69 /* 'Globals' and Defines */
70
71 /* Temporary 'Stroke' Operation data */
72 typedef struct tGPsdata {
73         Scene *scene;       /* current scene from context */
74         
75         wmWindow *win;      /* window where painting originated */
76         ScrArea *sa;        /* area where painting originated */
77         ARegion *ar;        /* region where painting originated */
78         View2D *v2d;        /* needed for GP_STROKE_2DSPACE */
79         rctf *subrect;      /* for using the camera rect within the 3d view */
80         rctf subrect_data;
81         
82         PointerRNA ownerPtr; /* pointer to owner of gp-datablock */
83         bGPdata *gpd;       /* gp-datablock layer comes from */
84         bGPDlayer *gpl;     /* layer we're working on */
85         bGPDframe *gpf;     /* frame we're working on */
86
87         short status;       /* current status of painting */
88         short paintmode;    /* mode for painting */
89         
90         int mval[2];        /* current mouse-position */
91         int mvalo[2];       /* previous recorded mouse-position */
92         
93         float pressure;     /* current stylus pressure */
94         float opressure;    /* previous stylus pressure */
95         
96         short radius;       /* radius of influence for eraser */
97         short flags;        /* flags that can get set during runtime */
98
99         float imat[4][4];   /* inverted transformation matrix applying when converting coords from screen-space
100                              * to region space */
101
102         float custom_color[4]; /* custom color for (?) */
103 } tGPsdata;
104
105 /* values for tGPsdata->status */
106 enum {
107         GP_STATUS_IDLING = 0,   /* stroke isn't in progress yet */
108         GP_STATUS_PAINTING,     /* a stroke is in progress */
109         GP_STATUS_ERROR,        /* something wasn't correctly set up */
110         GP_STATUS_DONE          /* painting done */
111 };
112
113 /* Return flags for adding points to stroke buffer */
114 enum {
115         GP_STROKEADD_INVALID    = -2,       /* error occurred - insufficient info to do so */
116         GP_STROKEADD_OVERFLOW   = -1,       /* error occurred - cannot fit any more points */
117         GP_STROKEADD_NORMAL,                /* point was successfully added */
118         GP_STROKEADD_FULL                   /* cannot add any more points to buffer */
119 };
120
121 /* Runtime flags */
122 enum {
123         GP_PAINTFLAG_FIRSTRUN       = (1 << 0),    /* operator just started */
124         GP_PAINTFLAG_STROKEADDED    = (1 << 1)
125 };
126
127 /* ------ */
128
129 /* maximum sizes of gp-session buffer */
130 #define GP_STROKE_BUFFER_MAX    5000
131
132 /* Macros for accessing sensitivity thresholds... */
133 /* minimum number of pixels mouse should move before new point created */
134 #define MIN_MANHATTEN_PX    (U.gp_manhattendist)
135 /* minimum length of new segment before new point can be added */
136 #define MIN_EUCLIDEAN_PX    (U.gp_euclideandist)
137
138 static int gp_stroke_added_check(tGPsdata *p)
139 {
140         return (p->gpf && p->gpf->strokes.last && p->flags & GP_PAINTFLAG_STROKEADDED);
141 }
142
143 static void gp_stroke_added_enable(tGPsdata *p)
144 {
145         BLI_assert(p->gpf->strokes.last != NULL);
146         p->flags |= GP_PAINTFLAG_STROKEADDED;
147 }
148
149 /* ------ */
150 /* Forward defines for some functions... */
151
152 static void gp_session_validatebuffer(tGPsdata *p);
153
154 /* ******************************************* */
155 /* Context Wrangling... */
156
157 /* check if context is suitable for drawing */
158 static int gpencil_draw_poll(bContext *C)
159 {
160         if (ED_operator_regionactive(C)) {
161                 /* check if current context can support GPencil data */
162                 if (gpencil_data_get_pointers(C, NULL) != NULL) {
163                         /* check if Grease Pencil isn't already running */
164                         if (ED_gpencil_session_active() == 0)
165                                 return 1;
166                         else
167                                 CTX_wm_operator_poll_msg_set(C, "Grease Pencil operator is already active");
168                 }
169                 else {
170                         CTX_wm_operator_poll_msg_set(C, "Failed to find Grease Pencil data to draw into");
171                 }
172         }
173         else {
174                 CTX_wm_operator_poll_msg_set(C, "Active region not set");
175         }
176         
177         return 0;
178 }
179
180 /* check if projecting strokes into 3d-geometry in the 3D-View */
181 static int gpencil_project_check(tGPsdata *p)
182 {
183         bGPdata *gpd = p->gpd;
184         return ((gpd->sbuffer_sflag & GP_STROKE_3DSPACE) && (p->gpd->flag & (GP_DATA_DEPTH_VIEW | GP_DATA_DEPTH_STROKE)));
185 }
186
187 /* ******************************************* */
188 /* Calculations/Conversions */
189
190 /* Utilities --------------------------------- */
191
192 /* get the reference point for stroke-point conversions */
193 static void gp_get_3d_reference(tGPsdata *p, float vec[3])
194 {
195         View3D *v3d = p->sa->spacedata.first;
196         float *fp = give_cursor(p->scene, v3d);
197         
198         /* the reference point used depends on the owner... */
199 #if 0 // XXX: disabled for now, since we can't draw relative to the owner yet
200         if (p->ownerPtr.type == &RNA_Object) {
201                 Object *ob = (Object *)p->ownerPtr.data;
202                 
203                 /* active Object 
204                  *  - use relative distance of 3D-cursor from object center
205                  */
206                 sub_v3_v3v3(vec, fp, ob->loc);
207         }
208         else
209 #endif
210         {
211                 /* use 3D-cursor */
212                 copy_v3_v3(vec, fp);
213         }
214 }
215
216 /* Stroke Editing ---------------------------- */
217
218 /* check if the current mouse position is suitable for adding a new point */
219 static short gp_stroke_filtermval(tGPsdata *p, const int mval[2], int pmval[2])
220 {
221         int dx = abs(mval[0] - pmval[0]);
222         int dy = abs(mval[1] - pmval[1]);
223         
224         /* if buffer is empty, just let this go through (i.e. so that dots will work) */
225         if (p->gpd->sbuffer_size == 0)
226                 return 1;
227         
228         /* check if mouse moved at least certain distance on both axes (best case) 
229          *      - aims to eliminate some jitter-noise from input when trying to draw straight lines freehand
230          */
231         else if ((dx > MIN_MANHATTEN_PX) && (dy > MIN_MANHATTEN_PX))
232                 return 1;
233         
234         /* check if the distance since the last point is significant enough 
235          *      - prevents points being added too densely
236          *      - distance here doesn't use sqrt to prevent slowness... we should still be safe from overflows though
237          */
238         else if ((dx * dx + dy * dy) > MIN_EUCLIDEAN_PX * MIN_EUCLIDEAN_PX)
239                 return 1;
240         
241         /* mouse 'didn't move' */
242         else
243                 return 0;
244 }
245
246 /* convert screen-coordinates to buffer-coordinates */
247 // XXX this method needs a total overhaul!
248 static void gp_stroke_convertcoords(tGPsdata *p, const int mval[2], float out[3], float *depth)
249 {
250         bGPdata *gpd = p->gpd;
251         
252         /* in 3d-space - pt->x/y/z are 3 side-by-side floats */
253         if (gpd->sbuffer_sflag & GP_STROKE_3DSPACE) {
254                 if (gpencil_project_check(p) && (ED_view3d_autodist_simple(p->ar, mval, out, 0, depth))) {
255                         /* projecting onto 3D-Geometry
256                          *      - nothing more needs to be done here, since view_autodist_simple() has already done it
257                          */
258                 }
259                 else {
260                         int mval_prj[2];
261                         float rvec[3], dvec[3];
262                         float mval_f[2];
263                         
264                         /* Current method just converts each point in screen-coordinates to
265                          * 3D-coordinates using the 3D-cursor as reference. In general, this
266                          * works OK, but it could of course be improved.
267                          *
268                          * TODO:
269                          *      - investigate using nearest point(s) on a previous stroke as
270                          *        reference point instead or as offset, for easier stroke matching
271                          */
272                         
273                         gp_get_3d_reference(p, rvec);
274                         
275                         /* method taken from editview.c - mouse_cursor() */
276                         project_int_noclip(p->ar, rvec, mval_prj);
277                         
278                         VECSUB2D(mval_f, mval_prj, mval);
279                         ED_view3d_win_to_delta(p->ar, mval_f, dvec);
280                         sub_v3_v3v3(out, rvec, dvec);
281                 }
282         }
283         
284         /* 2d - on 'canvas' (assume that p->v2d is set) */
285         else if ((gpd->sbuffer_sflag & GP_STROKE_2DSPACE) && (p->v2d)) {
286                 UI_view2d_region_to_view(p->v2d, mval[0], mval[1], &out[0], &out[1]);
287                 mul_v3_m4v3(out, p->imat, out);
288         }
289         
290         /* 2d - relative to screen (viewport area) */
291         else {
292                 if (p->subrect == NULL) { /* normal 3D view */
293                         out[0] = (float)(mval[0]) / (float)(p->ar->winx) * 100;
294                         out[1] = (float)(mval[1]) / (float)(p->ar->winy) * 100;
295                 }
296                 else { /* camera view, use subrect */
297                         out[0] = ((mval[0] - p->subrect->xmin) / ((p->subrect->xmax - p->subrect->xmin))) * 100;
298                         out[1] = ((mval[1] - p->subrect->ymin) / ((p->subrect->ymax - p->subrect->ymin))) * 100;
299                 }
300         }
301 }
302
303 /* add current stroke-point to buffer (returns whether point was successfully added) */
304 static short gp_stroke_addpoint(tGPsdata *p, const int mval[2], float pressure)
305 {
306         bGPdata *gpd = p->gpd;
307         tGPspoint *pt;
308
309         /* check painting mode */
310         if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) {
311                 /* straight lines only - i.e. only store start and end point in buffer */
312                 if (gpd->sbuffer_size == 0) {
313                         /* first point in buffer (start point) */
314                         pt = (tGPspoint *)(gpd->sbuffer);
315                         
316                         /* store settings */
317                         copy_v2_v2_int(&pt->x, mval);
318                         pt->pressure = pressure;
319                         
320                         /* increment buffer size */
321                         gpd->sbuffer_size++;
322                 }
323                 else {
324                         /* normally, we just reset the endpoint to the latest value 
325                          *      - assume that pointers for this are always valid...
326                          */
327                         pt = ((tGPspoint *)(gpd->sbuffer) + 1);
328                         
329                         /* store settings */
330                         copy_v2_v2_int(&pt->x, mval);
331                         pt->pressure = pressure;
332                         
333                         /* if this is just the second point we've added, increment the buffer size
334                          * so that it will be drawn properly...
335                          * otherwise, just leave it alone, otherwise we get problems
336                          */
337                         if (gpd->sbuffer_size != 2)
338                                 gpd->sbuffer_size = 2;
339                 }
340                 
341                 /* can keep carrying on this way :) */
342                 return GP_STROKEADD_NORMAL;
343         }
344         else if (p->paintmode == GP_PAINTMODE_DRAW) { /* normal drawing */
345                 /* check if still room in buffer */
346                 if (gpd->sbuffer_size >= GP_STROKE_BUFFER_MAX)
347                         return GP_STROKEADD_OVERFLOW;
348                 
349                 /* get pointer to destination point */
350                 pt = ((tGPspoint *)(gpd->sbuffer) + gpd->sbuffer_size);
351                 
352                 /* store settings */
353                 copy_v2_v2_int(&pt->x, mval);
354                 pt->pressure = pressure;
355                 
356                 /* increment counters */
357                 gpd->sbuffer_size++;
358                 
359                 /* check if another operation can still occur */
360                 if (gpd->sbuffer_size == GP_STROKE_BUFFER_MAX)
361                         return GP_STROKEADD_FULL;
362                 else
363                         return GP_STROKEADD_NORMAL;
364         }
365         else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
366                 /* get pointer to destination point */
367                 pt = (tGPspoint *)(gpd->sbuffer);
368                 
369                 /* store settings */
370                 copy_v2_v2_int(&pt->x, mval);
371                 pt->pressure = pressure;
372                 
373                 /* if there's stroke for this poly line session add (or replace last) point
374                  * to stroke. This allows to draw lines more interactively (see new segment
375                  * during mouse slide, i.e.) 
376                  */
377                 if (gp_stroke_added_check(p)) {
378                         bGPDstroke *gps = p->gpf->strokes.last;
379                         bGPDspoint *pts;
380                         
381                         /* first time point is adding to temporary buffer -- need to allocate new point in stroke */
382                         if (gpd->sbuffer_size == 0) {
383                                 gps->points = MEM_reallocN(gps->points, sizeof(bGPDspoint) * (gps->totpoints + 1));
384                                 gps->totpoints++;
385                         }
386                         
387                         pts = &gps->points[gps->totpoints - 1];
388                         
389                         /* special case for poly lines: normally, depth is needed only when creating new stroke from buffer,
390                          * but poly lines are converting to stroke instantly, so initialize depth buffer before converting coordinates 
391                          */
392                         if (gpencil_project_check(p)) {
393                                 View3D *v3d = p->sa->spacedata.first;
394                                 
395                                 view3d_region_operator_needs_opengl(p->win, p->ar);
396                                 ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1 : 0);
397                         }
398                         
399                         /* convert screen-coordinates to appropriate coordinates (and store them) */
400                         gp_stroke_convertcoords(p, &pt->x, &pts->x, NULL);
401                         
402                         /* copy pressure */
403                         pts->pressure = pt->pressure;
404                 }
405                 
406                 /* increment counters */
407                 if (gpd->sbuffer_size == 0)
408                         gpd->sbuffer_size++;
409                 
410                 return GP_STROKEADD_NORMAL;
411         }
412         
413         /* return invalid state for now... */
414         return GP_STROKEADD_INVALID;
415 }
416
417
418 /* temp struct for gp_stroke_smooth() */
419 typedef struct tGpSmoothCo {
420         int x;
421         int y;
422 } tGpSmoothCo;
423
424 /* smooth a stroke (in buffer) before storing it */
425 static void gp_stroke_smooth(tGPsdata *p)
426 {
427         bGPdata *gpd = p->gpd;
428         tGpSmoothCo *smoothArray, *spc;
429         int i = 0, cmx = gpd->sbuffer_size;
430         
431         /* only smooth if smoothing is enabled, and we're not doing a straight line */
432         if (!(U.gp_settings & GP_PAINT_DOSMOOTH) || ELEM(p->paintmode, GP_PAINTMODE_DRAW_STRAIGHT, GP_PAINTMODE_DRAW_POLY))
433                 return;
434         
435         /* don't try if less than 2 points in buffer */
436         if ((cmx <= 2) || (gpd->sbuffer == NULL))
437                 return;
438         
439         /* create a temporary smoothing coordinates buffer, use to store calculated values to prevent sequential error */
440         smoothArray = MEM_callocN(sizeof(tGpSmoothCo) * cmx, "gp_stroke_smooth smoothArray");
441         
442         /* first pass: calculate smoothing coordinates using weighted-averages */
443         for (i = 0, spc = smoothArray; i < gpd->sbuffer_size; i++, spc++) {
444                 const tGPspoint *pc = (((tGPspoint *)gpd->sbuffer) + i);
445                 const tGPspoint *pb = (i - 1 > 0) ? (pc - 1) : (pc);
446                 const tGPspoint *pa = (i - 2 > 0) ? (pc - 2) : (pb);
447                 const tGPspoint *pd = (i + 1 < cmx) ? (pc + 1) : (pc);
448                 const tGPspoint *pe = (i + 2 < cmx) ? (pc + 2) : (pd);
449                 
450                 spc->x = (int)(0.1 * pa->x + 0.2 * pb->x + 0.4 * pc->x + 0.2 * pd->x + 0.1 * pe->x);
451                 spc->y = (int)(0.1 * pa->y + 0.2 * pb->y + 0.4 * pc->y + 0.2 * pd->y + 0.1 * pe->y);
452         }
453         
454         /* second pass: apply smoothed coordinates */
455         for (i = 0, spc = smoothArray; i < gpd->sbuffer_size; i++, spc++) {
456                 tGPspoint *pc = (((tGPspoint *)gpd->sbuffer) + i);
457                 
458                 copy_v2_v2_int(&pc->x, &spc->x);
459         }
460         
461         /* free temp array */
462         MEM_freeN(smoothArray);
463 }
464
465 /* simplify a stroke (in buffer) before storing it 
466  *      - applies a reverse Chaikin filter
467  *      - code adapted from etch-a-ton branch (editarmature_sketch.c)
468  */
469 static void gp_stroke_simplify(tGPsdata *p)
470 {
471         bGPdata *gpd = p->gpd;
472         tGPspoint *old_points = (tGPspoint *)gpd->sbuffer;
473         short num_points = gpd->sbuffer_size;
474         short flag = gpd->sbuffer_sflag;
475         short i, j;
476         
477         /* only simplify if simplification is enabled, and we're not doing a straight line */
478         if (!(U.gp_settings & GP_PAINT_DOSIMPLIFY) || (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT))
479                 return;
480         
481         /* don't simplify if less than 4 points in buffer */
482         if ((num_points <= 4) || (old_points == NULL))
483                 return;
484                 
485         /* clear buffer (but don't free mem yet) so that we can write to it 
486          *      - firstly set sbuffer to NULL, so a new one is allocated
487          *      - secondly, reset flag after, as it gets cleared auto
488          */
489         gpd->sbuffer = NULL;
490         gp_session_validatebuffer(p);
491         gpd->sbuffer_sflag = flag;
492         
493 /* macro used in loop to get position of new point
494  *      - used due to the mixture of datatypes in use here
495  */
496 #define GP_SIMPLIFY_AVPOINT(offs, sfac) \
497         { \
498                 co[0] += (float)(old_points[offs].x * sfac); \
499                 co[1] += (float)(old_points[offs].y * sfac); \
500                 pressure += old_points[offs].pressure * sfac; \
501         } (void)0
502         
503         for (i = 0, j = 0; i < num_points; i++) {
504                 if (i - j == 3) {
505                         float co[2], pressure;
506                         int mco[2];
507                         
508                         /* initialize values */
509                         co[0] = 0;
510                         co[1] = 0;
511                         pressure = 0;
512                         
513                         /* using macro, calculate new point */
514                         GP_SIMPLIFY_AVPOINT(j, -0.25f);
515                         GP_SIMPLIFY_AVPOINT(j + 1, 0.75f);
516                         GP_SIMPLIFY_AVPOINT(j + 2, 0.75f);
517                         GP_SIMPLIFY_AVPOINT(j + 3, -0.25f);
518                         
519                         /* set values for adding */
520                         mco[0] = (int)co[0];
521                         mco[1] = (int)co[1];
522                         
523                         /* ignore return values on this... assume to be ok for now */
524                         gp_stroke_addpoint(p, mco, pressure);
525                         
526                         j += 2;
527                 }
528         } 
529         
530         /* free old buffer */
531         MEM_freeN(old_points);
532 }
533
534
535 /* make a new stroke from the buffer data */
536 static void gp_stroke_newfrombuffer(tGPsdata *p)
537 {
538         bGPdata *gpd = p->gpd;
539         bGPDstroke *gps;
540         bGPDspoint *pt;
541         tGPspoint *ptc;
542         int i, totelem;
543         /* since strokes are so fine, when using their depth we need a margin otherwise they might get missed */
544         int depth_margin = (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 4 : 0;
545         
546         /* get total number of points to allocate space for 
547          *      - drawing straight-lines only requires the endpoints
548          */
549         if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT)
550                 totelem = (gpd->sbuffer_size >= 2) ? 2 : gpd->sbuffer_size;
551         else
552                 totelem = gpd->sbuffer_size;
553         
554         /* exit with error if no valid points from this stroke */
555         if (totelem == 0) {
556                 if (G.debug & G_DEBUG)
557                         printf("Error: No valid points in stroke buffer to convert (tot=%d)\n", gpd->sbuffer_size);
558                 return;
559         }
560         
561         /* special case for poly line -- for already added stroke during session
562          * coordinates are getting added to stroke immediately to allow more
563          * interactive behavior */
564         if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
565                 if (gp_stroke_added_check(p)) {
566                         return;
567                 }
568         }
569
570         /* allocate memory for a new stroke */
571         gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke");
572         
573         /* copy appropriate settings for stroke */
574         gps->totpoints = totelem;
575         gps->thickness = p->gpl->thickness;
576         gps->flag = gpd->sbuffer_sflag;
577         
578         /* allocate enough memory for a continuous array for storage points */
579         gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
580
581         /* set pointer to first non-initialized point */
582         pt = gps->points + (gps->totpoints - totelem);
583
584         /* copy points from the buffer to the stroke */
585         if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) {
586                 /* straight lines only -> only endpoints */
587                 {
588                         /* first point */
589                         ptc = gpd->sbuffer;
590                         
591                         /* convert screen-coordinates to appropriate coordinates (and store them) */
592                         gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
593                         
594                         /* copy pressure */
595                         pt->pressure = ptc->pressure;
596                         
597                         pt++;
598                 }
599                         
600                 if (totelem == 2) {
601                         /* last point if applicable */
602                         ptc = ((tGPspoint *)gpd->sbuffer) + (gpd->sbuffer_size - 1);
603                         
604                         /* convert screen-coordinates to appropriate coordinates (and store them) */
605                         gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
606                         
607                         /* copy pressure */
608                         pt->pressure = ptc->pressure;
609                 }
610         }
611         else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
612                 /* first point */
613                 ptc = gpd->sbuffer;
614                 
615                 /* convert screen-coordinates to appropriate coordinates (and store them) */
616                 gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
617                 
618                 /* copy pressure */
619                 pt->pressure = ptc->pressure;
620         }
621         else {
622                 float *depth_arr = NULL;
623                 
624                 /* get an array of depths, far depths are blended */
625                 if (gpencil_project_check(p)) {
626                         int mval[2], mval_prev[2] = {0};
627                         int interp_depth = 0;
628                         int found_depth = 0;
629                         
630                         depth_arr = MEM_mallocN(sizeof(float) * gpd->sbuffer_size, "depth_points");
631                         
632                         for (i = 0, ptc = gpd->sbuffer; i < gpd->sbuffer_size; i++, ptc++, pt++) {
633                                 copy_v2_v2_int(mval, &ptc->x);
634                                 
635                                 if ((ED_view3d_autodist_depth(p->ar, mval, depth_margin, depth_arr + i) == 0) &&
636                                     (i && (ED_view3d_autodist_depth_seg(p->ar, mval, mval_prev, depth_margin + 1, depth_arr + i) == 0)))
637                                 {
638                                         interp_depth = TRUE;
639                                 }
640                                 else {
641                                         found_depth = TRUE;
642                                 }
643                                 
644                                 copy_v2_v2_int(mval_prev, mval);
645                         }
646                         
647                         if (found_depth == FALSE) {
648                                 /* eeh... not much we can do.. :/, ignore depth in this case, use the 3D cursor */
649                                 for (i = gpd->sbuffer_size - 1; i >= 0; i--)
650                                         depth_arr[i] = 0.9999f;
651                         }
652                         else {
653                                 if (p->gpd->flag & GP_DATA_DEPTH_STROKE_ENDPOINTS) {
654                                         /* remove all info between the valid endpoints */
655                                         int first_valid = 0;
656                                         int last_valid = 0;
657                                         
658                                         for (i = 0; i < gpd->sbuffer_size; i++) {
659                                                 if (depth_arr[i] != FLT_MAX)
660                                                         break;
661                                         }
662                                         first_valid = i;
663                                         
664                                         for (i = gpd->sbuffer_size - 1; i >= 0; i--) {
665                                                 if (depth_arr[i] != FLT_MAX)
666                                                         break;
667                                         }
668                                         last_valid = i;
669                                         
670                                         /* invalidate non-endpoints, so only blend between first and last */
671                                         for (i = first_valid + 1; i < last_valid; i++)
672                                                 depth_arr[i] = FLT_MAX;
673                                         
674                                         interp_depth = TRUE;
675                                 }
676                                 
677                                 if (interp_depth) {
678                                         interp_sparse_array(depth_arr, gpd->sbuffer_size, FLT_MAX);
679                                 }
680                         }
681                 }
682                 
683                 
684                 pt = gps->points;
685                 
686                 /* convert all points (normal behavior) */
687                 for (i = 0, ptc = gpd->sbuffer; i < gpd->sbuffer_size && ptc; i++, ptc++, pt++) {
688                         /* convert screen-coordinates to appropriate coordinates (and store them) */
689                         gp_stroke_convertcoords(p, &ptc->x, &pt->x, depth_arr ? depth_arr + i : NULL);
690                         
691                         /* copy pressure */
692                         pt->pressure = ptc->pressure;
693                 }
694                 
695                 if (depth_arr)
696                         MEM_freeN(depth_arr);
697         }
698
699         /* add stroke to frame */
700         BLI_addtail(&p->gpf->strokes, gps);
701         gp_stroke_added_enable(p);
702 }
703
704 /* --- 'Eraser' for 'Paint' Tool ------ */
705
706 /* eraser tool - remove segment from stroke/split stroke (after lasso inside) */
707 static short gp_stroke_eraser_splitdel(bGPDframe *gpf, bGPDstroke *gps, int i)
708 {
709         bGPDspoint *pt_tmp = gps->points;
710         bGPDstroke *gsn = NULL;
711
712         /* if stroke only had two points, get rid of stroke */
713         if (gps->totpoints == 2) {
714                 /* free stroke points, then stroke */
715                 MEM_freeN(pt_tmp);
716                 BLI_freelinkN(&gpf->strokes, gps);
717                 
718                 /* nothing left in stroke, so stop */
719                 return 1;
720         }
721
722         /* if last segment, just remove segment from the stroke */
723         else if (i == gps->totpoints - 2) {
724                 /* allocate new points array, and assign most of the old stroke there */
725                 gps->totpoints--;
726                 gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
727                 memcpy(gps->points, pt_tmp, sizeof(bGPDspoint) * gps->totpoints);
728                 
729                 /* free temp buffer */
730                 MEM_freeN(pt_tmp);
731                 
732                 /* nothing left in stroke, so stop */
733                 return 1;
734         }
735
736         /* if first segment, just remove segment from the stroke */
737         else if (i == 0) {
738                 /* allocate new points array, and assign most of the old stroke there */
739                 gps->totpoints--;
740                 gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
741                 memcpy(gps->points, pt_tmp + 1, sizeof(bGPDspoint) * gps->totpoints);
742                 
743                 /* free temp buffer */
744                 MEM_freeN(pt_tmp);
745                 
746                 /* no break here, as there might still be stuff to remove in this stroke */
747                 return 0;
748         }
749
750         /* segment occurs in 'middle' of stroke, so split */
751         else {
752                 /* duplicate stroke, and assign 'later' data to that stroke */
753                 gsn = MEM_dupallocN(gps);
754                 gsn->prev = gsn->next = NULL;
755                 BLI_insertlinkafter(&gpf->strokes, gps, gsn);
756                 
757                 gsn->totpoints = gps->totpoints - i;
758                 gsn->points = MEM_callocN(sizeof(bGPDspoint) * gsn->totpoints, "gp_stroke_points");
759                 memcpy(gsn->points, pt_tmp + i, sizeof(bGPDspoint) * gsn->totpoints);
760                 
761                 /* adjust existing stroke  */
762                 gps->totpoints = i;
763                 gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
764                 memcpy(gps->points, pt_tmp, sizeof(bGPDspoint) * i);
765                 
766                 /* free temp buffer */
767                 MEM_freeN(pt_tmp);
768                 
769                 /* nothing left in stroke, so stop */
770                 return 1;
771         }
772 }
773
774 /* eraser tool - check if part of stroke occurs within last segment drawn by eraser */
775 static short gp_stroke_eraser_strokeinside(int mval[], int UNUSED(mvalo[]), short rad, short x0, short y0, short x1, short y1)
776 {
777         /* simple within-radius check for now */
778         if (edge_inside_circle(mval[0], mval[1], rad, x0, y0, x1, y1))
779                 return 1;
780         
781         /* not inside */
782         return 0;
783
784
785 /* eraser tool - evaluation per stroke */
786 // TODO: this could really do with some optimization (KD-Tree/BVH?)
787 static void gp_stroke_eraser_dostroke(tGPsdata *p, int mval[], int mvalo[], short rad, rcti *rect, bGPDframe *gpf, bGPDstroke *gps)
788 {
789         bGPDspoint *pt1, *pt2;
790         int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
791         int xyval[2];
792         int i;
793         
794         if (gps->totpoints == 0) {
795                 /* just free stroke */
796                 if (gps->points) 
797                         MEM_freeN(gps->points);
798                 BLI_freelinkN(&gpf->strokes, gps);
799         }
800         else if (gps->totpoints == 1) {
801                 /* get coordinates */
802                 if (gps->flag & GP_STROKE_3DSPACE) {
803                         project_int(p->ar, &gps->points->x, xyval);
804                         x0 = xyval[0];
805                         y0 = xyval[1];
806                 }
807                 else if (gps->flag & GP_STROKE_2DSPACE) {                       
808                         UI_view2d_view_to_region(p->v2d, gps->points->x, gps->points->y, &x0, &y0);
809                 }
810                 else {
811                         if (p->subrect == NULL) { /* normal 3D view */
812                                 x0 = (int)(gps->points->x / 100 * p->ar->winx);
813                                 y0 = (int)(gps->points->y / 100 * p->ar->winy);
814                         }
815                         else { /* camera view, use subrect */
816                                 x0 = (int)((gps->points->x / 100) * (p->subrect->xmax - p->subrect->xmin)) + p->subrect->xmin;
817                                 y0 = (int)((gps->points->y / 100) * (p->subrect->ymax - p->subrect->ymin)) + p->subrect->ymin;
818                         }
819                 }
820                 
821                 /* do boundbox check first */
822                 if (BLI_in_rcti(rect, x0, y0)) {
823                         /* only check if point is inside */
824                         if ( ((x0 - mval[0]) * (x0 - mval[0]) + (y0 - mval[1]) * (y0 - mval[1])) <= rad * rad) {
825                                 /* free stroke */
826                                 MEM_freeN(gps->points);
827                                 BLI_freelinkN(&gpf->strokes, gps);
828                         }
829                 }
830         }
831         else {  
832                 /* loop over the points in the stroke, checking for intersections 
833                  *  - an intersection will require the stroke to be split
834                  */
835                 for (i = 0; (i + 1) < gps->totpoints; i++) {
836                         /* get points to work with */
837                         pt1 = gps->points + i;
838                         pt2 = gps->points + i + 1;
839                         
840                         /* get coordinates */
841                         if (gps->flag & GP_STROKE_3DSPACE) {
842                                 project_int(p->ar, &pt1->x, xyval);
843                                 x0 = xyval[0];
844                                 y0 = xyval[1];
845                                 
846                                 project_int(p->ar, &pt2->x, xyval);
847                                 x1 = xyval[0];
848                                 y1 = xyval[1];
849                         }
850                         else if (gps->flag & GP_STROKE_2DSPACE) {
851                                 UI_view2d_view_to_region(p->v2d, pt1->x, pt1->y, &x0, &y0);
852                                 
853                                 UI_view2d_view_to_region(p->v2d, pt2->x, pt2->y, &x1, &y1);
854                         }
855                         else {
856                                 if (p->subrect == NULL) { /* normal 3D view */
857                                         x0 = (int)(pt1->x / 100 * p->ar->winx);
858                                         y0 = (int)(pt1->y / 100 * p->ar->winy);
859                                         x1 = (int)(pt2->x / 100 * p->ar->winx);
860                                         y1 = (int)(pt2->y / 100 * p->ar->winy);
861                                 }
862                                 else { /* camera view, use subrect */ 
863                                         x0 = (int)((pt1->x / 100) * (p->subrect->xmax - p->subrect->xmin)) + p->subrect->xmin;
864                                         y0 = (int)((pt1->y / 100) * (p->subrect->ymax - p->subrect->ymin)) + p->subrect->ymin;
865                                         x1 = (int)((pt2->x / 100) * (p->subrect->xmax - p->subrect->xmin)) + p->subrect->xmin;
866                                         y1 = (int)((pt2->y / 100) * (p->subrect->ymax - p->subrect->ymin)) + p->subrect->ymin;
867                                 }
868                         }
869                         
870                         /* check that point segment of the boundbox of the eraser stroke */
871                         if (BLI_in_rcti(rect, x0, y0) || BLI_in_rcti(rect, x1, y1)) {
872                                 /* check if point segment of stroke had anything to do with
873                                  * eraser region  (either within stroke painted, or on its lines)
874                                  *  - this assumes that linewidth is irrelevant
875                                  */
876                                 if (gp_stroke_eraser_strokeinside(mval, mvalo, rad, x0, y0, x1, y1)) {
877                                         /* if function returns true, break this loop (as no more point to check) */
878                                         if (gp_stroke_eraser_splitdel(gpf, gps, i))
879                                                 break;
880                                 }
881                         }
882                 }
883         }
884 }
885
886 /* erase strokes which fall under the eraser strokes */
887 static void gp_stroke_doeraser(tGPsdata *p)
888 {
889         bGPDframe *gpf = p->gpf;
890         bGPDstroke *gps, *gpn;
891         rcti rect;
892         
893         /* rect is rectangle of eraser */
894         rect.xmin = p->mval[0] - p->radius;
895         rect.ymin = p->mval[1] - p->radius;
896         rect.xmax = p->mval[0] + p->radius;
897         rect.ymax = p->mval[1] + p->radius;
898         
899         /* loop over strokes, checking segments for intersections */
900         for (gps = gpf->strokes.first; gps; gps = gpn) {
901                 gpn = gps->next;
902                 gp_stroke_eraser_dostroke(p, p->mval, p->mvalo, p->radius, &rect, gpf, gps);
903         }
904 }
905
906 /* ******************************************* */
907 /* Sketching Operator */
908
909 /* clear the session buffers (call this before AND after a paint operation) */
910 static void gp_session_validatebuffer(tGPsdata *p)
911 {
912         bGPdata *gpd = p->gpd;
913         
914         /* clear memory of buffer (or allocate it if starting a new session) */
915         if (gpd->sbuffer) {
916                 //printf("\t\tGP - reset sbuffer\n");
917                 memset(gpd->sbuffer, 0, sizeof(tGPspoint) * GP_STROKE_BUFFER_MAX);
918         }
919         else {
920                 //printf("\t\tGP - allocate sbuffer\n");
921                 gpd->sbuffer = MEM_callocN(sizeof(tGPspoint) * GP_STROKE_BUFFER_MAX, "gp_session_strokebuffer");
922         }
923         
924         /* reset indices */
925         gpd->sbuffer_size = 0;
926         
927         /* reset flags */
928         gpd->sbuffer_sflag = 0;
929 }
930
931 /* (re)init new painting data */
932 static int gp_session_initdata(bContext *C, tGPsdata *p)
933 {
934         bGPdata **gpd_ptr = NULL;
935         ScrArea *curarea = CTX_wm_area(C);
936         ARegion *ar = CTX_wm_region(C);
937         
938         /* make sure the active view (at the starting time) is a 3d-view */
939         if (curarea == NULL) {
940                 p->status = GP_STATUS_ERROR;
941                 if (G.debug & G_DEBUG)
942                         printf("Error: No active view for painting\n");
943                 return 0;
944         }
945         
946         /* pass on current scene and window */
947         p->scene = CTX_data_scene(C);
948         p->win = CTX_wm_window(C);
949
950         unit_m4(p->imat);
951         
952         switch (curarea->spacetype) {
953                 /* supported views first */
954                 case SPACE_VIEW3D:
955                 {
956                         // View3D *v3d= curarea->spacedata.first;
957                         // RegionView3D *rv3d= ar->regiondata;
958                         
959                         /* set current area 
960                          *      - must verify that region data is 3D-view (and not something else)
961                          */
962                         p->sa = curarea;
963                         p->ar = ar;
964                         
965                         if (ar->regiondata == NULL) {
966                                 p->status = GP_STATUS_ERROR;
967                                 if (G.debug & G_DEBUG)
968                                         printf("Error: 3D-View active region doesn't have any region data, so cannot be drawable\n");
969                                 return 0;
970                         }
971                 }
972                 break;
973
974                 case SPACE_NODE:
975                 {
976                         //SpaceNode *snode= curarea->spacedata.first;
977                         
978                         /* set current area */
979                         p->sa = curarea;
980                         p->ar = ar;
981                         p->v2d = &ar->v2d;
982                 }
983                 break;
984                 case SPACE_SEQ:
985                 {
986                         SpaceSeq *sseq = curarea->spacedata.first;
987                         
988                         /* set current area */
989                         p->sa = curarea;
990                         p->ar = ar;
991                         p->v2d = &ar->v2d;
992                         
993                         /* check that gpencil data is allowed to be drawn */
994                         if (sseq->mainb == SEQ_DRAW_SEQUENCE) {
995                                 p->status = GP_STATUS_ERROR;
996                                 if (G.debug & G_DEBUG)
997                                         printf("Error: In active view (sequencer), active mode doesn't support Grease Pencil\n");
998                                 return 0;
999                         }
1000                 }
1001                 break;
1002                 case SPACE_IMAGE:
1003                 {
1004                         //SpaceImage *sima= curarea->spacedata.first;
1005                         
1006                         /* set the current area */
1007                         p->sa = curarea;
1008                         p->ar = ar;
1009                         p->v2d = &ar->v2d;
1010                 }
1011                 break;
1012                 case SPACE_CLIP:
1013                 {
1014                         SpaceClip *sc = curarea->spacedata.first;
1015                         
1016                         /* set the current area */
1017                         p->sa = curarea;
1018                         p->ar = ar;
1019                         p->v2d = &ar->v2d;
1020                         
1021                         invert_m4_m4(p->imat, sc->unistabmat);
1022                         
1023                         /* custom color for new layer */
1024                         p->custom_color[0] = 1.0f;
1025                         p->custom_color[1] = 0.0f;
1026                         p->custom_color[2] = 0.5f;
1027                         p->custom_color[3] = 0.9f;
1028                         
1029                         if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
1030                                 int framenr = sc->user.framenr;
1031                                 MovieTrackingTrack *track = BKE_tracking_active_track(&sc->clip->tracking);
1032                                 MovieTrackingMarker *marker = BKE_tracking_exact_marker(track, framenr);
1033                                 
1034                                 p->imat[3][0] -= marker->pos[0];
1035                                 p->imat[3][1] -= marker->pos[1];
1036                         }
1037                 }
1038                 break;
1039                 
1040                 /* unsupported views */
1041                 default:
1042                 {
1043                         p->status = GP_STATUS_ERROR;
1044                         if (G.debug & G_DEBUG)
1045                                 printf("Error: Active view not appropriate for Grease Pencil drawing\n");
1046                         return 0;
1047                 }
1048                 break;
1049         }
1050         
1051         /* get gp-data */
1052         gpd_ptr = gpencil_data_get_pointers(C, &p->ownerPtr);
1053         if (gpd_ptr == NULL) {
1054                 p->status = GP_STATUS_ERROR;
1055                 if (G.debug & G_DEBUG)
1056                         printf("Error: Current context doesn't allow for any Grease Pencil data\n");
1057                 return 0;
1058         }
1059         else {
1060                 /* if no existing GPencil block exists, add one */
1061                 if (*gpd_ptr == NULL)
1062                         *gpd_ptr = gpencil_data_addnew("GPencil");
1063                 p->gpd = *gpd_ptr;
1064         }
1065         
1066         if (ED_gpencil_session_active() == 0) {
1067                 /* initialize undo stack,
1068                  * also, existing undo stack would make buffer drawn */
1069                 gpencil_undo_init(p->gpd);
1070         }
1071         
1072         /* clear out buffer (stored in gp-data), in case something contaminated it */
1073         gp_session_validatebuffer(p);
1074         
1075         return 1;
1076 }
1077
1078 /* init new painting session */
1079 static tGPsdata *gp_session_initpaint(bContext *C)
1080 {
1081         tGPsdata *p = NULL;
1082
1083         /* create new context data */
1084         p = MEM_callocN(sizeof(tGPsdata), "GPencil Drawing Data");
1085
1086         gp_session_initdata(C, p);
1087         
1088         /* return context data for running paint operator */
1089         return p;
1090 }
1091
1092 /* cleanup after a painting session */
1093 static void gp_session_cleanup(tGPsdata *p)
1094 {
1095         bGPdata *gpd = (p) ? p->gpd : NULL;
1096         
1097         /* error checking */
1098         if (gpd == NULL)
1099                 return;
1100         
1101         /* free stroke buffer */
1102         if (gpd->sbuffer) {
1103                 //printf("\t\tGP - free sbuffer\n");
1104                 MEM_freeN(gpd->sbuffer);
1105                 gpd->sbuffer = NULL;
1106         }
1107         
1108         /* clear flags */
1109         gpd->sbuffer_size = 0;
1110         gpd->sbuffer_sflag = 0;
1111 }
1112
1113 /* init new stroke */
1114 static void gp_paint_initstroke(tGPsdata *p, short paintmode)
1115 {       
1116         /* get active layer (or add a new one if non-existent) */
1117         p->gpl = gpencil_layer_getactive(p->gpd);
1118         if (p->gpl == NULL) {
1119                 p->gpl = gpencil_layer_addnew(p->gpd);
1120                 
1121                 if (p->custom_color[3])
1122                         copy_v3_v3(p->gpl->color, p->custom_color);
1123         }
1124         if (p->gpl->flag & GP_LAYER_LOCKED) {
1125                 p->status = GP_STATUS_ERROR;
1126                 if (G.debug & G_DEBUG)
1127                         printf("Error: Cannot paint on locked layer\n");
1128                 return;
1129         }
1130                 
1131         /* get active frame (add a new one if not matching frame) */
1132         p->gpf = gpencil_layer_getframe(p->gpl, p->scene->r.cfra, 1);
1133         if (p->gpf == NULL) {
1134                 p->status = GP_STATUS_ERROR;
1135                 if (G.debug & G_DEBUG)
1136                         printf("Error: No frame created (gpencil_paint_init)\n");
1137                 return;
1138         }
1139         else
1140                 p->gpf->flag |= GP_FRAME_PAINT;
1141         
1142         /* set 'eraser' for this stroke if using eraser */
1143         p->paintmode = paintmode;
1144         if (p->paintmode == GP_PAINTMODE_ERASER)
1145                 p->gpd->sbuffer_sflag |= GP_STROKE_ERASER;
1146                 
1147         /* set 'initial run' flag, which is only used to denote when a new stroke is starting */
1148         p->flags |= GP_PAINTFLAG_FIRSTRUN;
1149         
1150
1151         /* when drawing in the camera view, in 2D space, set the subrect */
1152         if (!(p->gpd->flag & GP_DATA_VIEWALIGN)) {
1153                 if (p->sa->spacetype == SPACE_VIEW3D) {
1154                         View3D *v3d = p->sa->spacedata.first;
1155                         RegionView3D *rv3d = p->ar->regiondata;
1156                         
1157                         /* for camera view set the subrect */
1158                         if (rv3d->persp == RV3D_CAMOB) {
1159                                 ED_view3d_calc_camera_border(p->scene, p->ar, v3d, rv3d, &p->subrect_data, TRUE); /* no shift */
1160                                 p->subrect = &p->subrect_data;
1161                         }
1162                 }
1163         }
1164
1165         /* check if points will need to be made in view-aligned space */
1166         if (p->gpd->flag & GP_DATA_VIEWALIGN) {
1167                 switch (p->sa->spacetype) {
1168                         case SPACE_VIEW3D:
1169                         {
1170                                 RegionView3D *rv3d = p->ar->regiondata;
1171                                 float rvec[3];
1172                                 
1173                                 /* get reference point for 3d space placement */
1174                                 gp_get_3d_reference(p, rvec);
1175                                 initgrabz(rv3d, rvec[0], rvec[1], rvec[2]);
1176                                 
1177                                 p->gpd->sbuffer_sflag |= GP_STROKE_3DSPACE;
1178                         }
1179                         break;
1180                         
1181                         case SPACE_NODE:
1182                         {
1183                                 p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1184                         }
1185                         break;
1186                         
1187                         case SPACE_SEQ:
1188                         {
1189                                 p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1190                         }
1191                         break;
1192                         
1193                         case SPACE_IMAGE:
1194                         {
1195                                 SpaceImage *sima = (SpaceImage *)p->sa->spacedata.first;
1196                                 
1197                                 /* only set these flags if the image editor doesn't have an image active,
1198                                  * otherwise user will be confused by strokes not appearing after they're drawn
1199                                  *
1200                                  * Admittedly, this is a bit hacky, but it works much nicer from an ergonomic standpoint!
1201                                  */
1202                                 if (ELEM(NULL, sima, sima->image)) {
1203                                         /* make strokes be drawn in screen space */
1204                                         p->gpd->sbuffer_sflag &= ~GP_STROKE_2DSPACE;
1205                                         p->gpd->flag &= ~GP_DATA_VIEWALIGN;
1206                                 }       
1207                                 else
1208                                         p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1209                         }
1210                         break;
1211                                 
1212                         case SPACE_CLIP:
1213                         {
1214                                 p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1215                         }
1216                         break;
1217                 }
1218         }
1219 }
1220
1221 /* finish off a stroke (clears buffer, but doesn't finish the paint operation) */
1222 static void gp_paint_strokeend(tGPsdata *p)
1223 {
1224         /* for surface sketching, need to set the right OpenGL context stuff so that 
1225          * the conversions will project the values correctly...
1226          */
1227         if (gpencil_project_check(p)) {
1228                 View3D *v3d = p->sa->spacedata.first;
1229                 
1230                 /* need to restore the original projection settings before packing up */
1231                 view3d_region_operator_needs_opengl(p->win, p->ar);
1232                 ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1 : 0);
1233         }
1234         
1235         /* check if doing eraser or not */
1236         if ((p->gpd->sbuffer_sflag & GP_STROKE_ERASER) == 0) {
1237                 /* smooth stroke before transferring? */
1238                 gp_stroke_smooth(p);
1239                 
1240                 /* simplify stroke before transferring? */
1241                 gp_stroke_simplify(p);
1242                 
1243                 /* transfer stroke to frame */
1244                 gp_stroke_newfrombuffer(p);
1245         }
1246         
1247         /* clean up buffer now */
1248         gp_session_validatebuffer(p);
1249 }
1250
1251 /* finish off stroke painting operation */
1252 static void gp_paint_cleanup(tGPsdata *p)
1253 {
1254         /* p->gpd==NULL happens when stroke failed to initialize,
1255          * for example. when GP is hidden in current space (sergey) */
1256         if (p->gpd) {
1257                 /* finish off a stroke */
1258                 gp_paint_strokeend(p);
1259         }
1260         
1261         /* "unlock" frame */
1262         if (p->gpf)
1263                 p->gpf->flag &= ~GP_FRAME_PAINT;
1264 }
1265
1266 /* ------------------------------- */
1267
1268 static void gpencil_draw_exit(bContext *C, wmOperator *op)
1269 {
1270         tGPsdata *p = op->customdata;
1271         
1272         /* clear undo stack */
1273         gpencil_undo_finish();
1274         
1275         /* restore cursor to indicate end of drawing */
1276         WM_cursor_restore(CTX_wm_window(C));
1277         
1278         /* don't assume that operator data exists at all */
1279         if (p) {
1280                 /* check size of buffer before cleanup, to determine if anything happened here */
1281                 if (p->paintmode == GP_PAINTMODE_ERASER) {
1282                         // TODO clear radial cursor thing
1283                         // XXX draw_sel_circle(NULL, p.mvalo, 0, p.radius, 0);
1284                 }
1285                 
1286                 /* cleanup */
1287                 gp_paint_cleanup(p);
1288                 gp_session_cleanup(p);
1289                 
1290                 /* finally, free the temp data */
1291                 MEM_freeN(p);   
1292         }
1293         
1294         op->customdata = NULL;
1295 }
1296
1297 static int gpencil_draw_cancel(bContext *C, wmOperator *op)
1298 {
1299         /* this is just a wrapper around exit() */
1300         gpencil_draw_exit(C, op);
1301         return OPERATOR_CANCELLED;
1302 }
1303
1304 /* ------------------------------- */
1305
1306
1307 static int gpencil_draw_init(bContext *C, wmOperator *op)
1308 {
1309         tGPsdata *p;
1310         int paintmode = RNA_enum_get(op->ptr, "mode");
1311         
1312         /* check context */
1313         p = op->customdata = gp_session_initpaint(C);
1314         if ((p == NULL) || (p->status == GP_STATUS_ERROR)) {
1315                 /* something wasn't set correctly in context */
1316                 gpencil_draw_exit(C, op);
1317                 return 0;
1318         }
1319         
1320         /* init painting data */
1321         gp_paint_initstroke(p, paintmode);
1322         if (p->status == GP_STATUS_ERROR) {
1323                 gpencil_draw_exit(C, op);
1324                 return 0;
1325         }
1326         
1327         /* radius for eraser circle is defined in userprefs now */
1328         p->radius = U.gp_eraser;
1329         
1330         /* everything is now setup ok */
1331         return 1;
1332 }
1333
1334 /* ------------------------------- */
1335
1336 /* update UI indicators of status, including cursor and header prints */
1337 static void gpencil_draw_status_indicators(tGPsdata *p)
1338 {
1339         /* header prints */
1340         switch (p->status) {
1341                 case GP_STATUS_PAINTING:
1342                         /* only print this for paint-sessions, otherwise it gets annoying */
1343                         if (GPENCIL_SKETCH_SESSIONS_ON(p->scene))
1344                                 ED_area_headerprint(p->sa, "Grease Pencil: Drawing/erasing stroke... Release to end stroke");
1345                         break;
1346                 
1347                 case GP_STATUS_IDLING:
1348                         /* print status info */
1349                         switch (p->paintmode) {
1350                                 case GP_PAINTMODE_ERASER:
1351                                         ED_area_headerprint(p->sa, "Grease Pencil Erase Session: Hold and drag LMB or RMB to erase | ESC/Enter to end");
1352                                         break;
1353                                 case GP_PAINTMODE_DRAW_STRAIGHT:
1354                                         ED_area_headerprint(p->sa, "Grease Pencil Line Session: Hold and drag LMB to draw | ESC/Enter to end");
1355                                         break;
1356                                 case GP_PAINTMODE_DRAW:
1357                                         ED_area_headerprint(p->sa, "Grease Pencil Freehand Session: Hold and drag LMB to draw | ESC/Enter to end");
1358                                         break;
1359                                         
1360                                 default: /* unhandled future cases */
1361                                         ED_area_headerprint(p->sa, "Grease Pencil Session: ESC/Enter to end");
1362                                         break;
1363                         }
1364                         break;
1365                         
1366                 case GP_STATUS_ERROR:
1367                 case GP_STATUS_DONE:
1368                         /* clear status string */
1369                         ED_area_headerprint(p->sa, NULL);
1370                         break;
1371         }
1372 }
1373
1374 /* ------------------------------- */
1375
1376 /* create a new stroke point at the point indicated by the painting context */
1377 static void gpencil_draw_apply(wmOperator *op, tGPsdata *p)
1378 {
1379         /* handle drawing/erasing -> test for erasing first */
1380         if (p->paintmode == GP_PAINTMODE_ERASER) {
1381                 /* do 'live' erasing now */
1382                 gp_stroke_doeraser(p);
1383                 
1384                 /* store used values */
1385                 p->mvalo[0] = p->mval[0];
1386                 p->mvalo[1] = p->mval[1];
1387                 p->opressure = p->pressure;
1388         }
1389         /* only add current point to buffer if mouse moved (even though we got an event, it might be just noise) */
1390         else if (gp_stroke_filtermval(p, p->mval, p->mvalo)) {
1391                 /* try to add point */
1392                 short ok = gp_stroke_addpoint(p, p->mval, p->pressure);
1393                 
1394                 /* handle errors while adding point */
1395                 if ((ok == GP_STROKEADD_FULL) || (ok == GP_STROKEADD_OVERFLOW)) {
1396                         /* finish off old stroke */
1397                         gp_paint_strokeend(p);
1398                         
1399                         /* start a new stroke, starting from previous point */
1400                         gp_stroke_addpoint(p, p->mvalo, p->opressure);
1401                         gp_stroke_addpoint(p, p->mval, p->pressure);
1402                 }
1403                 else if (ok == GP_STROKEADD_INVALID) {
1404                         /* the painting operation cannot continue... */
1405                         BKE_report(op->reports, RPT_ERROR, "Cannot paint stroke");
1406                         p->status = GP_STATUS_ERROR;
1407                         
1408                         if (G.debug & G_DEBUG)
1409                                 printf("Error: Grease-Pencil Paint - Add Point Invalid\n");
1410                         return;
1411                 }
1412                 
1413                 /* store used values */
1414                 p->mvalo[0] = p->mval[0];
1415                 p->mvalo[1] = p->mval[1];
1416                 p->opressure = p->pressure;
1417         }
1418 }
1419
1420 /* handle draw event */
1421 static void gpencil_draw_apply_event(wmOperator *op, wmEvent *event)
1422 {
1423         tGPsdata *p = op->customdata;
1424         PointerRNA itemptr;
1425         float mousef[2];
1426         int tablet = 0;
1427
1428         /* convert from window-space to area-space mouse coordintes 
1429          * NOTE: float to ints conversions, +1 factor is probably used to ensure a bit more accurate rounding... 
1430          */
1431         p->mval[0] = event->mval[0] + 1;
1432         p->mval[1] = event->mval[1] + 1;
1433
1434         /* handle pressure sensitivity (which is supplied by tablets) */
1435         if (event->custom == EVT_DATA_TABLET) {
1436                 wmTabletData *wmtab = event->customdata;
1437                 
1438                 tablet = (wmtab->Active != EVT_TABLET_NONE);
1439                 p->pressure = wmtab->Pressure;
1440                 
1441                 //if (wmtab->Active == EVT_TABLET_ERASER)
1442                 // TODO... this should get caught by the keymaps which call drawing in the first place
1443         }
1444         else
1445                 p->pressure = 1.0f;
1446         
1447         /* fill in stroke data (not actually used directly by gpencil_draw_apply) */
1448         RNA_collection_add(op->ptr, "stroke", &itemptr);
1449         
1450         mousef[0] = p->mval[0];
1451         mousef[1] = p->mval[1];
1452         RNA_float_set_array(&itemptr, "mouse", mousef);
1453         RNA_float_set(&itemptr, "pressure", p->pressure);
1454         RNA_boolean_set(&itemptr, "is_start", (p->flags & GP_PAINTFLAG_FIRSTRUN));
1455         
1456         /* special exception for start of strokes (i.e. maybe for just a dot) */
1457         if (p->flags & GP_PAINTFLAG_FIRSTRUN) {
1458                 p->flags &= ~GP_PAINTFLAG_FIRSTRUN;
1459                 
1460                 p->mvalo[0] = p->mval[0];
1461                 p->mvalo[1] = p->mval[1];
1462                 p->opressure = p->pressure;
1463                 
1464                 /* special exception here for too high pressure values on first touch in
1465                  *  windows for some tablets, then we just skip first touch ..  
1466                  */
1467                 if (tablet && (p->pressure >= 0.99f))
1468                         return;
1469         }
1470         
1471         /* apply the current latest drawing point */
1472         gpencil_draw_apply(op, p);
1473         
1474         /* force refresh */
1475         ED_region_tag_redraw(p->ar); /* just active area for now, since doing whole screen is too slow */
1476 }
1477
1478 /* ------------------------------- */
1479
1480 /* operator 'redo' (i.e. after changing some properties, but also for repeat last) */
1481 static int gpencil_draw_exec(bContext *C, wmOperator *op)
1482 {
1483         tGPsdata *p = NULL;
1484         
1485         //printf("GPencil - Starting Re-Drawing\n");
1486         
1487         /* try to initialize context data needed while drawing */
1488         if (!gpencil_draw_init(C, op)) {
1489                 if (op->customdata) MEM_freeN(op->customdata);
1490                 //printf("\tGP - no valid data\n");
1491                 return OPERATOR_CANCELLED;
1492         }
1493         else
1494                 p = op->customdata;
1495         
1496         //printf("\tGP - Start redrawing stroke\n");
1497         
1498         /* loop over the stroke RNA elements recorded (i.e. progress of mouse movement),
1499          * setting the relevant values in context at each step, then applying
1500          */
1501         RNA_BEGIN(op->ptr, itemptr, "stroke")
1502         {
1503                 float mousef[2];
1504                 
1505                 //printf("\t\tGP - stroke elem\n");
1506                 
1507                 /* get relevant data for this point from stroke */
1508                 RNA_float_get_array(&itemptr, "mouse", mousef);
1509                 p->mval[0] = (int)mousef[0];
1510                 p->mval[1] = (int)mousef[1];
1511                 p->pressure = RNA_float_get(&itemptr, "pressure");
1512                 
1513                 if (RNA_boolean_get(&itemptr, "is_start")) {
1514                         /* if first-run flag isn't set already (i.e. not true first stroke),
1515                          * then we must terminate the previous one first before continuing
1516                          */
1517                         if ((p->flags & GP_PAINTFLAG_FIRSTRUN) == 0) {
1518                                 // TODO: both of these ops can set error-status, but we probably don't need to worry
1519                                 gp_paint_strokeend(p);
1520                                 gp_paint_initstroke(p, p->paintmode);
1521                         }
1522                 }
1523                 
1524                 /* if first run, set previous data too */
1525                 if (p->flags & GP_PAINTFLAG_FIRSTRUN) {
1526                         p->flags &= ~GP_PAINTFLAG_FIRSTRUN;
1527                         
1528                         p->mvalo[0] = p->mval[0];
1529                         p->mvalo[1] = p->mval[1];
1530                         p->opressure = p->pressure;
1531                 }
1532                 
1533                 /* apply this data as necessary now (as per usual) */
1534                 gpencil_draw_apply(op, p);
1535         }
1536         RNA_END;
1537         
1538         //printf("\tGP - done\n");
1539         
1540         /* cleanup */
1541         gpencil_draw_exit(C, op);
1542         
1543         /* refreshes */
1544         WM_event_add_notifier(C, NC_SCREEN | ND_GPENCIL | NA_EDITED, NULL); // XXX need a nicer one that will work
1545         
1546         /* done */
1547         return OPERATOR_FINISHED;
1548 }
1549
1550 /* ------------------------------- */
1551
1552 /* start of interactive drawing part of operator */
1553 static int gpencil_draw_invoke(bContext *C, wmOperator *op, wmEvent *event)
1554 {
1555         tGPsdata *p = NULL;
1556         wmWindow *win = CTX_wm_window(C);
1557         
1558         if (G.debug & G_DEBUG)
1559                 printf("GPencil - Starting Drawing\n");
1560         
1561         /* try to initialize context data needed while drawing */
1562         if (!gpencil_draw_init(C, op)) {
1563                 if (op->customdata) 
1564                         MEM_freeN(op->customdata);
1565                 if (G.debug & G_DEBUG)
1566                         printf("\tGP - no valid data\n");
1567                 return OPERATOR_CANCELLED;
1568         }
1569         else
1570                 p = op->customdata;
1571         
1572         // TODO: set any additional settings that we can take from the events?
1573         // TODO? if tablet is erasing, force eraser to be on?
1574         
1575         // TODO: move cursor setting stuff to stroke-start so that paintmode can be changed midway...
1576         
1577         /* if eraser is on, draw radial aid */
1578         if (p->paintmode == GP_PAINTMODE_ERASER) {
1579                 // TODO: this involves mucking around with radial control, so we leave this for now..
1580         }
1581         
1582         /* set cursor */
1583         if (p->paintmode == GP_PAINTMODE_ERASER)
1584                 WM_cursor_modal(win, BC_CROSSCURSOR);  // XXX need a better cursor
1585         else
1586                 WM_cursor_modal(win, BC_PAINTBRUSHCURSOR);
1587         
1588         /* special hack: if there was an initial event, then we were invoked via a hotkey, and 
1589          * painting should start immediately. Otherwise, this was called from a toolbar, in which
1590          * case we should wait for the mouse to be clicked.
1591          */
1592         if (event->type) {
1593                 /* hotkey invoked - start drawing */
1594                 //printf("\tGP - set first spot\n");
1595                 p->status = GP_STATUS_PAINTING;
1596                 
1597                 /* handle the initial drawing - i.e. for just doing a simple dot */
1598                 gpencil_draw_apply_event(op, event);
1599         }
1600         else {
1601                 /* toolbar invoked - don't start drawing yet... */
1602                 //printf("\tGP - hotkey invoked... waiting for click-drag\n");
1603         }
1604         
1605         WM_event_add_notifier(C, NC_SCREEN | ND_GPENCIL, NULL);
1606         /* add a modal handler for this operator, so that we can then draw continuous strokes */
1607         WM_event_add_modal_handler(C, op);
1608         return OPERATOR_RUNNING_MODAL;
1609 }
1610
1611 /* gpencil modal operator stores area, which can be removed while using it (like fullscreen) */
1612 static int gpencil_area_exists(bContext *C, ScrArea *sa_test)
1613 {
1614         bScreen *sc = CTX_wm_screen(C);
1615         ScrArea *sa;
1616         
1617         for (sa = sc->areabase.first; sa; sa = sa->next) {
1618                 if (sa == sa_test)
1619                         return 1;
1620         }
1621         
1622         return 0;
1623 }
1624
1625 static tGPsdata *gpencil_stroke_begin(bContext *C, wmOperator *op)
1626 {
1627         tGPsdata *p = op->customdata;
1628
1629         /* we must check that we're still within the area that we're set up to work from
1630          * otherwise we could crash (see bug #20586)
1631          */
1632         if (CTX_wm_area(C) != p->sa) {
1633                 printf("\t\t\tGP - wrong area execution abort!\n");
1634                 p->status = GP_STATUS_ERROR;
1635         }
1636
1637         //printf("\t\tGP - start stroke\n");
1638
1639         /* we may need to set up paint env again if we're resuming */
1640         // XXX: watch it with the paintmode! in future, it'd be nice to allow changing paint-mode when in sketching-sessions
1641         // XXX: with tablet events, we may event want to check for eraser here, for nicer tablet support
1642
1643         if (gp_session_initdata(C, p))
1644                 gp_paint_initstroke(p, p->paintmode);
1645
1646         if (p->status != GP_STATUS_ERROR)
1647                 p->status = GP_STATUS_PAINTING;
1648
1649         return op->customdata;
1650 }
1651
1652 static void gpencil_stroke_end(wmOperator *op)
1653 {
1654         tGPsdata *p = op->customdata;
1655
1656         gp_paint_cleanup(p);
1657
1658         gpencil_undo_push(p->gpd);
1659
1660         gp_session_cleanup(p);
1661
1662         p->status = GP_STATUS_IDLING;
1663
1664         p->gpd = NULL;
1665         p->gpl = NULL;
1666         p->gpf = NULL;
1667 }
1668
1669 /* events handling during interactive drawing part of operator */
1670 static int gpencil_draw_modal(bContext *C, wmOperator *op, wmEvent *event)
1671 {
1672         tGPsdata *p = op->customdata;
1673         int estate = OPERATOR_PASS_THROUGH; /* default exit state - not handled, so let others have a share of the pie */
1674         
1675         // if (event->type == NDOF_MOTION)
1676         //      return OPERATOR_PASS_THROUGH;
1677         // -------------------------------
1678         // [mce] Not quite what I was looking
1679         // for, but a good start! GP continues to
1680         // draw on the screen while the 3D mouse
1681         // moves the viewpoint. Problem is that
1682         // the stroke is converted to 3D only after
1683         // it is finished. This approach should work
1684         // better in tools that immediately apply
1685         // in 3D space.
1686
1687         //printf("\tGP - handle modal event...\n");
1688         
1689         /* exit painting mode (and/or end current stroke) */
1690         if (ELEM5(event->type, RETKEY, PADENTER, ESCKEY, SPACEKEY, RIGHTMOUSE)) {
1691                 /* exit() ends the current stroke before cleaning up */
1692                 //printf("\t\tGP - end of paint op + end of stroke\n");
1693                 p->status = GP_STATUS_DONE;
1694                 estate = OPERATOR_FINISHED;
1695         }
1696         
1697         /* toggle painting mode upon mouse-button movement */
1698         if (event->type == LEFTMOUSE) {
1699                 /* if painting, end stroke */
1700                 if (p->status == GP_STATUS_PAINTING) {
1701                         int sketch = 0;
1702                         /* basically, this should be mouse-button up = end stroke 
1703                          * BUT what happens next depends on whether we 'painting sessions' is enabled
1704                          */
1705                         sketch |= GPENCIL_SKETCH_SESSIONS_ON(p->scene);
1706                         /* polyline drawing is also 'sketching' -- all knots should be added during one session */
1707                         sketch |= p->paintmode == GP_PAINTMODE_DRAW_POLY;
1708                         
1709                         if (sketch) {
1710                                 /* end stroke only, and then wait to resume painting soon */
1711                                 //printf("\t\tGP - end stroke only\n");
1712                                 gpencil_stroke_end(op);
1713                                 
1714                                 /* we've just entered idling state, so this event was processed (but no others yet) */
1715                                 estate = OPERATOR_RUNNING_MODAL;
1716                                 
1717                                 /* stroke could be smoothed, send notifier to refresh screen */
1718                                 WM_event_add_notifier(C, NC_SCREEN | ND_GPENCIL | NA_EDITED, NULL);
1719                         }
1720                         else {
1721                                 //printf("\t\tGP - end of stroke + op\n");
1722                                 p->status = GP_STATUS_DONE;
1723                                 estate = OPERATOR_FINISHED;
1724                         }
1725                 }
1726                 else if (event->val == KM_PRESS) {
1727                         /* not painting, so start stroke (this should be mouse-button down) */
1728                         p = gpencil_stroke_begin(C, op);
1729                         
1730                         if (p->status == GP_STATUS_ERROR) {
1731                                 estate = OPERATOR_CANCELLED;
1732                         }
1733                 } 
1734                 else {
1735                         p->status = GP_STATUS_IDLING;
1736                 }
1737         }
1738         
1739         /* handle mode-specific events */
1740         if (p->status == GP_STATUS_PAINTING) {
1741                 /* handle painting mouse-movements? */
1742                 if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE) || (p->flags & GP_PAINTFLAG_FIRSTRUN)) {
1743                         /* handle drawing event */
1744                         //printf("\t\tGP - add point\n");
1745                         gpencil_draw_apply_event(op, event);
1746                         
1747                         /* finish painting operation if anything went wrong just now */
1748                         if (p->status == GP_STATUS_ERROR) {
1749                                 printf("\t\t\t\tGP - add error done!\n");
1750                                 estate = OPERATOR_CANCELLED;
1751                         }
1752                         else {
1753                                 /* event handled, so just tag as running modal */
1754                                 //printf("\t\t\t\tGP - add point handled!\n");
1755                                 estate = OPERATOR_RUNNING_MODAL;
1756                         }
1757                 }
1758                 /* there shouldn't be any other events, but just in case there are, let's swallow them 
1759                  * (i.e. to prevent problems with with undo)
1760                  */
1761                 else {
1762                         /* swallow event to save ourselves trouble */
1763                         estate = OPERATOR_RUNNING_MODAL;
1764                 }
1765         }
1766         
1767         /* gpencil modal operator stores area, which can be removed while using it (like fullscreen) */
1768         if (0 == gpencil_area_exists(C, p->sa))
1769                 estate = OPERATOR_CANCELLED;
1770         else
1771                 /* update status indicators - cursor, header, etc. */
1772                 gpencil_draw_status_indicators(p);
1773         
1774         /* process last operations before exiting */
1775         switch (estate) {
1776                 case OPERATOR_FINISHED:
1777                         /* one last flush before we're done */
1778                         gpencil_draw_exit(C, op);
1779                         WM_event_add_notifier(C, NC_SCREEN | ND_GPENCIL | NA_EDITED, NULL); // XXX need a nicer one that will work
1780                         break;
1781                         
1782                 case OPERATOR_CANCELLED:
1783                         gpencil_draw_exit(C, op);
1784                         break;
1785                 
1786                 case OPERATOR_RUNNING_MODAL | OPERATOR_PASS_THROUGH:
1787                         /* event doesn't need to be handled */
1788                         //printf("unhandled event -> %d (mmb? = %d | mmv? = %d)\n", event->type, event->type == MIDDLEMOUSE, event->type==MOUSEMOVE);
1789                         break;
1790         }
1791         
1792         /* return status code */
1793         return estate;
1794 }
1795
1796 /* ------------------------------- */
1797
1798 static EnumPropertyItem prop_gpencil_drawmodes[] = {
1799         {GP_PAINTMODE_DRAW, "DRAW", 0, "Draw Freehand", ""},
1800         {GP_PAINTMODE_DRAW_STRAIGHT, "DRAW_STRAIGHT", 0, "Draw Straight Lines", ""},
1801         {GP_PAINTMODE_DRAW_POLY, "DRAW_POLY", 0, "Draw Poly Line", ""},
1802         {GP_PAINTMODE_ERASER, "ERASER", 0, "Eraser", ""},
1803         {0, NULL, 0, NULL, NULL}
1804 };
1805
1806 void GPENCIL_OT_draw(wmOperatorType *ot)
1807 {
1808         /* identifiers */
1809         ot->name = "Grease Pencil Draw";
1810         ot->idname = "GPENCIL_OT_draw";
1811         ot->description = "Make annotations on the active data";
1812         
1813         /* api callbacks */
1814         ot->exec = gpencil_draw_exec;
1815         ot->invoke = gpencil_draw_invoke;
1816         ot->modal = gpencil_draw_modal;
1817         ot->cancel = gpencil_draw_cancel;
1818         ot->poll = gpencil_draw_poll;
1819         
1820         /* flags */
1821         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
1822         
1823         /* settings for drawing */
1824         RNA_def_enum(ot->srna, "mode", prop_gpencil_drawmodes, 0, "Mode", "Way to interpret mouse movements");
1825         
1826         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1827 }