Grease Pencil: Bugfixes
[blender.git] / source / blender / editors / gpencil / gpencil_paint.c
1 /* Grease Pencil  - version 2
2  * By Joshua Leung
3  */
4
5 #include <stdio.h>
6 #include <stddef.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <math.h>
10
11 #include "MEM_guardedalloc.h"
12
13 #include "BLI_blenlib.h"
14 #include "BLI_arithb.h"
15
16 #include "BKE_gpencil.h"
17 #include "BKE_context.h"
18 #include "BKE_global.h"
19 #include "BKE_report.h"
20 #include "BKE_utildefines.h"
21
22 #include "DNA_gpencil_types.h"
23 #include "DNA_action_types.h"
24 #include "DNA_object_types.h"
25 #include "DNA_scene_types.h"
26 #include "DNA_screen_types.h"
27 #include "DNA_space_types.h"
28 #include "DNA_view2d_types.h"
29 #include "DNA_view3d_types.h"
30 #include "DNA_windowmanager_types.h"
31
32 #include "UI_view2d.h"
33
34 #include "ED_armature.h"
35 #include "ED_gpencil.h"
36 #include "ED_sequencer.h"
37 #include "ED_screen.h"
38 #include "ED_view3d.h"
39
40 #include "RNA_access.h"
41 #include "RNA_define.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45
46 #include "gpencil_intern.h"
47
48 /* ******************************************* */
49 /* Context Wrangling... */
50
51 /* Get the active Grease Pencil datablock */
52 // TODO: move this to a gpencil_utils.c? 
53 bGPdata *gpencil_data_getactive (bContext *C)
54 {
55         Scene *scene= CTX_data_scene(C);
56         ScrArea *sa= CTX_wm_area(C);
57         
58         /* if there's an active area, check if the particular editor may
59          * have defined any special Grease Pencil context for editing...
60          */
61         if (sa) {
62                 switch (sa->spacetype) {
63                         case SPACE_VIEW3D: /* 3D-View */
64                         {
65                                 Object *ob= CTX_data_active_object(C);
66                                 
67                                 /* just in case... */
68                                 if (ob) {
69                                         /* depending on the mode of the object, we may be able to get some GP data 
70                                          * from different elements - i.e. bones...
71                                          */
72                                         if (ob->mode & OB_MODE_POSE) {
73                                                 //bPoseChannel *pchan= CTX_data_active_pchan(C);
74                                                 
75                                                 /* if posechannel has GP data, use that... */
76                                                 //if (pchan && pchan->gpd)
77                                                 //      return pchan->gpd;
78                                         }
79                                         
80                                         /* still here, so check if active Object has GP data */
81                                         //if (ob->gpd)
82                                         //      return ob->gpd;
83                                 }
84                         }
85                                 break;
86                         
87                         case SPACE_NODE: /* Nodes Editor */
88                         {
89                                 //SpaceNode *snode= (SpaceNode *)CTX_wm_space_data(C);
90                                 
91                                 /* return the GP data for the active node block/node */
92                         }
93                                 break;
94                                 
95                         case SPACE_SEQ: /* Sequencer */
96                         {
97                                 //SpaceSeq *sseq= (SpaceSeq *)CTX_wm_space_data(C);
98                                 
99                                 /* return the GP data for the active strips/image/etc. */
100                         }
101                                 break;
102                 }
103         }
104         
105         /* just fall back on the scene's GP data */
106         return (scene) ? scene->gpd : NULL;
107 }
108
109
110 /* check if context is suitable for drawing */
111 static int gpencil_draw_poll (bContext *C)
112 {
113         // TODO: must check context for Grease Pencil data...
114         return 1;
115 }
116
117 /* ******************************************* */
118 /* 'Globals' and Defines */
119
120 /* Temporary 'Stroke' Operation data */
121 typedef struct tGPsdata {
122         Scene *scene;       /* current scene from context */
123         ScrArea *sa;            /* area where painting originated */
124         ARegion *ar;        /* region where painting originated */
125         View2D *v2d;            /* needed for GP_STROKE_2DSPACE */
126         
127 #if 0 // XXX review this 2d image stuff...
128         ImBuf *ibuf;            /* needed for GP_STROKE_2DIMAGE */
129         struct IBufViewSettings {
130                 int offsx, offsy;                       /* offsets */
131                 int sizex, sizey;                       /* dimensions to use as scale-factor */
132         } im2d_settings;        /* needed for GP_STROKE_2DIMAGE */
133 #endif
134         
135         bGPdata *gpd;           /* gp-datablock layer comes from */
136         bGPDlayer *gpl;         /* layer we're working on */
137         bGPDframe *gpf;         /* frame we're working on */
138         
139         short status;           /* current status of painting */
140         short paintmode;        /* mode for painting */
141         
142         int mval[2];            /* current mouse-position */
143         int mvalo[2];           /* previous recorded mouse-position */
144         
145         float pressure;         /* current stylus pressure */
146         float opressure;        /* previous stylus pressure */
147         
148         short radius;           /* radius of influence for eraser */
149         short flags;            /* flags that can get set during runtime */
150 } tGPsdata;
151
152 /* values for tGPsdata->status */
153 enum {
154         GP_STATUS_NORMAL = 0,   /* running normally */
155         GP_STATUS_ERROR,                /* something wasn't correctly set up */
156         GP_STATUS_DONE                  /* painting done */
157 };
158
159 /* values for tGPsdata->paintmode */
160 enum {
161         GP_PAINTMODE_DRAW = 0,
162         GP_PAINTMODE_ERASER
163 };
164
165 /* Return flags for adding points to stroke buffer */
166 enum {
167         GP_STROKEADD_INVALID    = -2,           /* error occurred - insufficient info to do so */
168         GP_STROKEADD_OVERFLOW   = -1,           /* error occurred - cannot fit any more points */
169         GP_STROKEADD_NORMAL,                            /* point was successfully added */
170         GP_STROKEADD_FULL                                       /* cannot add any more points to buffer */
171 };
172
173 /* Runtime flags */
174 enum {
175         GP_PAINTFLAG_FIRSTRUN           = (1<<0),       /* operator just started */
176         GP_PAINTFLAG_STRAIGHTLINES      = (1<<1),       /* only take the endpoints of a stroke */
177 };
178
179 /* ------ */
180
181 /* maximum sizes of gp-session buffer */
182 #define GP_STROKE_BUFFER_MAX    5000
183
184 /* Macros for accessing sensitivity thresholds... */
185         /* minimum number of pixels mouse should move before new point created */
186 #define MIN_MANHATTEN_PX        (U.gp_manhattendist)
187         /* minimum length of new segment before new point can be added */
188 #define MIN_EUCLIDEAN_PX        (U.gp_euclideandist)
189
190 /* macro to test if only converting endpoints - only for use when converting!  */       
191 // XXX for now, don't test for editpaint too...
192 //#define GP_BUFFER2STROKE_ENDPOINTS ((gpd->flag & GP_DATA_EDITPAINT) && (p->flags & GP_PAINTFLAG_STRAIGHTLINES))
193 #define GP_BUFFER2STROKE_ENDPOINTS ((p->flags & GP_PAINTFLAG_STRAIGHTLINES))
194
195 /* ------ */
196 /* Forward defines for some functions... */
197
198 static void gp_session_validatebuffer(tGPsdata *p);
199
200 /* ******************************************* */
201 /* Calculations/Conversions */
202
203 /* Stroke Editing ---------------------------- */
204
205 /* check if the current mouse position is suitable for adding a new point */
206 static short gp_stroke_filtermval (tGPsdata *p, int mval[2], int pmval[2])
207 {
208         int dx= abs(mval[0] - pmval[0]);
209         int dy= abs(mval[1] - pmval[1]);
210         
211         /* check if mouse moved at least certain distance on both axes (best case) */
212         if ((dx > MIN_MANHATTEN_PX) && (dy > MIN_MANHATTEN_PX))
213                 return 1;
214         
215         /* check if the distance since the last point is significant enough */
216         // future optimisation: sqrt here may be too slow?
217         else if (sqrt(dx*dx + dy*dy) > MIN_EUCLIDEAN_PX)
218                 return 1;
219         
220         /* mouse 'didn't move' */
221         else
222                 return 0;
223 }
224
225 /* convert screen-coordinates to buffer-coordinates */
226 // XXX this method needs a total overhaul!
227 static void gp_stroke_convertcoords (tGPsdata *p, short mval[], float out[])
228 {
229         bGPdata *gpd= p->gpd;
230         
231         /* in 3d-space - pt->x/y/z are 3 side-by-side floats */
232         if (gpd->sbuffer_sflag & GP_STROKE_3DSPACE) {
233                 View3D *v3d= p->sa->spacedata.first;
234                 const short mx=mval[0], my=mval[1];
235                 float *fp= give_cursor(p->scene, v3d); // XXX NULL could be v3d
236                 float dvec[3];
237                 
238                 /* Current method just converts each point in screen-coordinates to 
239                  * 3D-coordinates using the 3D-cursor as reference. In general, this 
240                  * works OK, but it could of course be improved.
241                  *
242                  * TODO:
243                  *      - investigate using nearest point(s) on a previous stroke as
244                  *        reference point instead or as offset, for easier stroke matching
245                  *      - investigate projection onto geometry (ala retopo)
246                  */
247                 
248                 /* method taken from editview.c - mouse_cursor() */
249                 project_short_noclip(p->ar, fp, mval);
250                 window_to_3d_delta(p->ar, dvec, mval[0]-mx, mval[1]-my);
251                 VecSubf(out, fp, dvec);
252         }
253         
254         /* 2d - on 'canvas' (assume that p->v2d is set) */
255         else if ((gpd->sbuffer_sflag & GP_STROKE_2DSPACE) && (p->v2d)) {
256                 float x, y;
257                 
258                 UI_view2d_region_to_view(p->v2d, mval[0], mval[1], &x, &y);
259                 
260                 out[0]= x;
261                 out[1]= y;
262         }
263         
264 #if 0
265         /* 2d - on image 'canvas' (assume that p->v2d is set) */
266         else if (gpd->sbuffer_sflag & GP_STROKE_2DIMAGE) {
267                 int sizex, sizey, offsx, offsy;
268                 
269                 /* get stored settings 
270                  *      - assume that these have been set already (there are checks that set sane 'defaults' just in case)
271                  */
272                 sizex= p->im2d_settings.sizex;
273                 sizey= p->im2d_settings.sizey;
274                 offsx= p->im2d_settings.offsx;
275                 offsy= p->im2d_settings.offsy;
276                 
277                 /* calculate new points */
278                 out[0]= (float)(mval[0] - offsx) / (float)sizex;
279                 out[1]= (float)(mval[1] - offsy) / (float)sizey;
280         }
281 #endif
282         
283         /* 2d - relative to screen (viewport area) */
284         else {
285                 out[0] = (float)(mval[0]) / (float)(p->sa->winx) * 1000;
286                 out[1] = (float)(mval[1]) / (float)(p->sa->winy) * 1000;
287         }
288 }
289
290 /* add current stroke-point to buffer (returns whether point was successfully added) */
291 static short gp_stroke_addpoint (tGPsdata *p, int mval[2], float pressure)
292 {
293         bGPdata *gpd= p->gpd;
294         tGPspoint *pt;
295         
296         /* check if still room in buffer */
297         if (gpd->sbuffer_size >= GP_STROKE_BUFFER_MAX)
298                 return GP_STROKEADD_OVERFLOW;
299         
300         /* get pointer to destination point */
301         pt= ((tGPspoint *)(gpd->sbuffer) + gpd->sbuffer_size);
302         
303         /* store settings */
304         pt->x= mval[0];
305         pt->y= mval[1];
306         pt->pressure= pressure;
307         
308         /* increment counters */
309         gpd->sbuffer_size++;
310         
311         /* check if another operation can still occur */
312         if (gpd->sbuffer_size == GP_STROKE_BUFFER_MAX)
313                 return GP_STROKEADD_FULL;
314         else
315                 return GP_STROKEADD_NORMAL;
316 }
317
318 /* smooth a stroke (in buffer) before storing it */
319 static void gp_stroke_smooth (tGPsdata *p)
320 {
321         bGPdata *gpd= p->gpd;
322         int i=0, cmx=gpd->sbuffer_size;
323         
324         /* only smooth if smoothing is enabled, and we're not doing a straight line */
325         if (!(U.gp_settings & GP_PAINT_DOSMOOTH) || GP_BUFFER2STROKE_ENDPOINTS)
326                 return;
327         
328         /* don't try if less than 2 points in buffer */
329         if ((cmx <= 2) || (gpd->sbuffer == NULL))
330                 return;
331         
332         /* apply weighting-average (note doing this along path sequentially does introduce slight error) */
333         for (i=0; i < gpd->sbuffer_size; i++) {
334                 tGPspoint *pc= (((tGPspoint *)gpd->sbuffer) + i);
335                 tGPspoint *pb= (i-1 > 0)?(pc-1):(pc);
336                 tGPspoint *pa= (i-2 > 0)?(pc-2):(pb);
337                 tGPspoint *pd= (i+1 < cmx)?(pc+1):(pc);
338                 tGPspoint *pe= (i+2 < cmx)?(pc+2):(pd);
339                 
340                 pc->x= (short)(0.1*pa->x + 0.2*pb->x + 0.4*pc->x + 0.2*pd->x + 0.1*pe->x);
341                 pc->y= (short)(0.1*pa->y + 0.2*pb->y + 0.4*pc->y + 0.2*pd->y + 0.1*pe->y);
342         }
343 }
344
345 /* simplify a stroke (in buffer) before storing it 
346  *      - applies a reverse Chaikin filter
347  *      - code adapted from etch-a-ton branch (editarmature_sketch.c)
348  */
349 static void gp_stroke_simplify (tGPsdata *p)
350 {
351         bGPdata *gpd= p->gpd;
352         tGPspoint *old_points= (tGPspoint *)gpd->sbuffer;
353         short num_points= gpd->sbuffer_size;
354         short flag= gpd->sbuffer_sflag;
355         short i, j;
356         
357         /* only simplify if simlification is enabled, and we're not doing a straight line */
358         if (!(U.gp_settings & GP_PAINT_DOSIMPLIFY) || GP_BUFFER2STROKE_ENDPOINTS)
359                 return;
360         
361         /* don't simplify if less than 4 points in buffer */
362         if ((num_points <= 2) || (old_points == NULL))
363                 return;
364                 
365         /* clear buffer (but don't free mem yet) so that we can write to it 
366          *      - firstly set sbuffer to NULL, so a new one is allocated
367          *      - secondly, reset flag after, as it gets cleared auto
368          */
369         gpd->sbuffer= NULL;
370         gp_session_validatebuffer(p);
371         gpd->sbuffer_sflag = flag;
372         
373 /* macro used in loop to get position of new point
374  *      - used due to the mixture of datatypes in use here
375  */
376 #define GP_SIMPLIFY_AVPOINT(offs, sfac) \
377         { \
378                 co[0] += (float)(old_points[offs].x * sfac); \
379                 co[1] += (float)(old_points[offs].y * sfac); \
380                 pressure += old_points[offs].pressure * sfac; \
381         }
382         
383         for (i = 0, j = 0; i < num_points; i++)
384         {
385                 if (i - j == 3)
386                 {
387                         float co[2], pressure;
388                         int mco[2];
389                         
390                         /* initialise values */
391                         co[0]= 0;
392                         co[1]= 0;
393                         pressure = 0;
394                         
395                         /* using macro, calculate new point */
396                         GP_SIMPLIFY_AVPOINT(j, -0.25f);
397                         GP_SIMPLIFY_AVPOINT(j+1, 0.75f);
398                         GP_SIMPLIFY_AVPOINT(j+2, 0.75f);
399                         GP_SIMPLIFY_AVPOINT(j+3, -0.25f);
400                         
401                         /* set values for adding */
402                         mco[0]= (int)co[0];
403                         mco[1]= (int)co[1];
404                         
405                         /* ignore return values on this... assume to be ok for now */
406                         gp_stroke_addpoint(p, mco, pressure);
407                         
408                         j += 2;
409                 }
410         } 
411         
412         /* free old buffer */
413         MEM_freeN(old_points);
414 }
415
416
417 /* make a new stroke from the buffer data */
418 static void gp_stroke_newfrombuffer (tGPsdata *p)
419 {
420         bGPdata *gpd= p->gpd;
421         bGPDstroke *gps;
422         bGPDspoint *pt;
423         tGPspoint *ptc;
424         int i, totelem;
425         
426         /* get total number of points to allocate space for:
427          *      - in 'Draw Mode', holding the Ctrl-Modifier will only take endpoints
428          *      - otherwise, do whole stroke
429          */
430         if (GP_BUFFER2STROKE_ENDPOINTS)
431                 totelem = (gpd->sbuffer_size >= 2) ? 2: gpd->sbuffer_size;
432         else
433                 totelem = gpd->sbuffer_size;
434         
435         /* exit with error if no valid points from this stroke */
436         if (totelem == 0) {
437                 if (G.f & G_DEBUG) 
438                         printf("Error: No valid points in stroke buffer to convert (tot=%d) \n", gpd->sbuffer_size);
439                 return;
440         }
441         
442         /* allocate memory for a new stroke */
443         gps= MEM_callocN(sizeof(bGPDstroke), "gp_stroke");
444         
445         /* allocate enough memory for a continuous array for storage points */
446         pt= gps->points= MEM_callocN(sizeof(bGPDspoint)*totelem, "gp_stroke_points");
447         
448         /* copy appropriate settings for stroke */
449         gps->totpoints= totelem;
450         gps->thickness= p->gpl->thickness;
451         gps->flag= gpd->sbuffer_sflag;
452         
453         /* copy points from the buffer to the stroke */
454         if (GP_BUFFER2STROKE_ENDPOINTS) {
455                 /* 'Draw Mode' + Ctrl-Modifier - only endpoints */
456                 {
457                         /* first point */
458                         ptc= gpd->sbuffer;
459                         
460                         /* convert screen-coordinates to appropriate coordinates (and store them) */
461                         gp_stroke_convertcoords(p, &ptc->x, &pt->x);
462                         
463                         /* copy pressure */
464                         pt->pressure= ptc->pressure;
465                         
466                         pt++;
467                 }
468                         
469                 if (totelem == 2) {
470                         /* last point if applicable */
471                         ptc= ((tGPspoint *)gpd->sbuffer) + (gpd->sbuffer_size - 1);
472                         
473                         /* convert screen-coordinates to appropriate coordinates (and store them) */
474                         gp_stroke_convertcoords(p, &ptc->x, &pt->x);
475                         
476                         /* copy pressure */
477                         pt->pressure= ptc->pressure;
478                 }
479         }
480         else {
481                 /* convert all points (normal behaviour) */
482                 for (i=0, ptc=gpd->sbuffer; i < gpd->sbuffer_size && ptc; i++, ptc++) {
483                         /* convert screen-coordinates to appropriate coordinates (and store them) */
484                         gp_stroke_convertcoords(p, &ptc->x, &pt->x);
485                         
486                         /* copy pressure */
487                         pt->pressure= ptc->pressure;
488                         
489                         pt++;
490                 }
491         }
492         
493         /* add stroke to frame */
494         BLI_addtail(&p->gpf->strokes, gps);
495 }
496
497 /* --- 'Eraser' for 'Paint' Tool ------ */
498
499 /* eraser tool - remove segment from stroke/split stroke (after lasso inside) */
500 static short gp_stroke_eraser_splitdel (bGPDframe *gpf, bGPDstroke *gps, int i)
501 {
502         bGPDspoint *pt_tmp= gps->points;
503         bGPDstroke *gsn = NULL;
504
505         /* if stroke only had two points, get rid of stroke */
506         if (gps->totpoints == 2) {
507                 /* free stroke points, then stroke */
508                 MEM_freeN(pt_tmp);
509                 BLI_freelinkN(&gpf->strokes, gps);
510                 
511                 /* nothing left in stroke, so stop */
512                 return 1;
513         }
514
515         /* if last segment, just remove segment from the stroke */
516         else if (i == gps->totpoints - 2) {
517                 /* allocate new points array, and assign most of the old stroke there */
518                 gps->totpoints--;
519                 gps->points= MEM_callocN(sizeof(bGPDspoint)*gps->totpoints, "gp_stroke_points");
520                 memcpy(gps->points, pt_tmp, sizeof(bGPDspoint)*gps->totpoints);
521                 
522                 /* free temp buffer */
523                 MEM_freeN(pt_tmp);
524                 
525                 /* nothing left in stroke, so stop */
526                 return 1;
527         }
528
529         /* if first segment, just remove segment from the stroke */
530         else if (i == 0) {
531                 /* allocate new points array, and assign most of the old stroke there */
532                 gps->totpoints--;
533                 gps->points= MEM_callocN(sizeof(bGPDspoint)*gps->totpoints, "gp_stroke_points");
534                 memcpy(gps->points, pt_tmp + 1, sizeof(bGPDspoint)*gps->totpoints);
535                 
536                 /* free temp buffer */
537                 MEM_freeN(pt_tmp);
538                 
539                 /* no break here, as there might still be stuff to remove in this stroke */
540                 return 0;
541         }
542
543         /* segment occurs in 'middle' of stroke, so split */
544         else {
545                 /* duplicate stroke, and assign 'later' data to that stroke */
546                 gsn= MEM_dupallocN(gps);
547                 gsn->prev= gsn->next= NULL;
548                 BLI_insertlinkafter(&gpf->strokes, gps, gsn);
549                 
550                 gsn->totpoints= gps->totpoints - i;
551                 gsn->points= MEM_callocN(sizeof(bGPDspoint)*gsn->totpoints, "gp_stroke_points");
552                 memcpy(gsn->points, pt_tmp + i, sizeof(bGPDspoint)*gsn->totpoints);
553                 
554                 /* adjust existing stroke  */
555                 gps->totpoints= i;
556                 gps->points= MEM_callocN(sizeof(bGPDspoint)*gps->totpoints, "gp_stroke_points");
557                 memcpy(gps->points, pt_tmp, sizeof(bGPDspoint)*i);
558                 
559                 /* free temp buffer */
560                 MEM_freeN(pt_tmp);
561                 
562                 /* nothing left in stroke, so stop */
563                 return 1;
564         }
565 }
566
567 /* eraser tool - check if part of stroke occurs within last segment drawn by eraser */
568 static short gp_stroke_eraser_strokeinside (int mval[], int mvalo[], short rad, short x0, short y0, short x1, short y1)
569 {
570         /* simple within-radius check for now */
571         if (edge_inside_circle(mval[0], mval[1], rad, x0, y0, x1, y1))
572                 return 1;
573         
574         /* not inside */
575         return 0;
576
577
578 /* eraser tool - evaluation per stroke */
579 // TODO: this could really do with some optimisation (KD-Tree/BVH?)
580 static void gp_stroke_eraser_dostroke (tGPsdata *p, int mval[], int mvalo[], short rad, rcti *rect, bGPDframe *gpf, bGPDstroke *gps)
581 {
582         bGPDspoint *pt1, *pt2;
583         int x0=0, y0=0, x1=0, y1=0;
584         short xyval[2];
585         int i;
586         
587         if (gps->totpoints == 0) {
588                 /* just free stroke */
589                 if (gps->points) 
590                         MEM_freeN(gps->points);
591                 BLI_freelinkN(&gpf->strokes, gps);
592         }
593         else if (gps->totpoints == 1) {
594                 /* get coordinates */
595                 if (gps->flag & GP_STROKE_3DSPACE) {
596                         project_short(p->ar, &gps->points->x, xyval);
597                         x0= xyval[0];
598                         y0= xyval[1];
599                 }
600                 else if (gps->flag & GP_STROKE_2DSPACE) {                       
601                         UI_view2d_view_to_region(p->v2d, gps->points->x, gps->points->y, &x0, &y0);
602                 }
603 #if 0
604                 else if (gps->flag & GP_STROKE_2DIMAGE) {                       
605                         int offsx, offsy, sizex, sizey;
606                         
607                         /* get stored settings */
608                         sizex= p->im2d_settings.sizex;
609                         sizey= p->im2d_settings.sizey;
610                         offsx= p->im2d_settings.offsx;
611                         offsy= p->im2d_settings.offsy;
612                         
613                         /* calculate new points */
614                         x0= (int)((gps->points->x * sizex) + offsx);
615                         y0= (int)((gps->points->y * sizey) + offsy);
616                 }
617 #endif
618                 else {
619                         x0= (int)(gps->points->x / 1000 * p->sa->winx);
620                         y0= (int)(gps->points->y / 1000 * p->sa->winy);
621                 }
622                 
623                 /* do boundbox check first */
624                 if (BLI_in_rcti(rect, x0, y0)) {
625                         /* only check if point is inside */
626                         if ( ((x0-mval[0])*(x0-mval[0]) + (y0-mval[1])*(y0-mval[1])) <= rad*rad ) {
627                                 /* free stroke */
628                                 MEM_freeN(gps->points);
629                                 BLI_freelinkN(&gpf->strokes, gps);
630                         }
631                 }
632         }
633         else {  
634                 /* loop over the points in the stroke, checking for intersections 
635                  *      - an intersection will require the stroke to be split
636                  */
637                 for (i=0; (i+1) < gps->totpoints; i++) {
638                         /* get points to work with */
639                         pt1= gps->points + i;
640                         pt2= gps->points + i + 1;
641                         
642                         /* get coordinates */
643                         if (gps->flag & GP_STROKE_3DSPACE) {
644                                 project_short(p->ar, &pt1->x, xyval);
645                                 x0= xyval[0];
646                                 y0= xyval[1];
647                                 
648                                 project_short(p->ar, &pt2->x, xyval);
649                                 x1= xyval[0];
650                                 y1= xyval[1];
651                         }
652                         else if (gps->flag & GP_STROKE_2DSPACE) {
653                                 UI_view2d_view_to_region(p->v2d, pt1->x, pt1->y, &x0, &y0);
654                                 
655                                 UI_view2d_view_to_region(p->v2d, pt2->x, pt2->y, &x1, &y1);
656                         }
657 #if 0
658                         else if (gps->flag & GP_STROKE_2DIMAGE) {
659                                 int offsx, offsy, sizex, sizey;
660                                 
661                                 /* get stored settings */
662                                 sizex= p->im2d_settings.sizex;
663                                 sizey= p->im2d_settings.sizey;
664                                 offsx= p->im2d_settings.offsx;
665                                 offsy= p->im2d_settings.offsy;
666                                 
667                                 /* calculate new points */
668                                 x0= (int)((pt1->x * sizex) + offsx);
669                                 y0= (int)((pt1->y * sizey) + offsy);
670                                 
671                                 x1= (int)((pt2->x * sizex) + offsx);
672                                 y1= (int)((pt2->y * sizey) + offsy);
673                         }
674 #endif
675                         else {
676                                 x0= (int)(pt1->x / 1000 * p->sa->winx);
677                                 y0= (int)(pt1->y / 1000 * p->sa->winy);
678                                 x1= (int)(pt2->x / 1000 * p->sa->winx);
679                                 y1= (int)(pt2->y / 1000 * p->sa->winy);
680                         }
681                         
682                         /* check that point segment of the boundbox of the eraser stroke */
683                         if (BLI_in_rcti(rect, x0, y0) || BLI_in_rcti(rect, x1, y1)) {
684                                 /* check if point segment of stroke had anything to do with
685                                  * eraser region  (either within stroke painted, or on its lines)
686                                  *      - this assumes that linewidth is irrelevant
687                                  */
688                                 if (gp_stroke_eraser_strokeinside(mval, mvalo, rad, x0, y0, x1, y1)) {
689                                         /* if function returns true, break this loop (as no more point to check) */
690                                         if (gp_stroke_eraser_splitdel(gpf, gps, i))
691                                                 break;
692                                 }
693                         }
694                 }
695         }
696 }
697
698 /* erase strokes which fall under the eraser strokes */
699 static void gp_stroke_doeraser (tGPsdata *p)
700 {
701         bGPDframe *gpf= p->gpf;
702         bGPDstroke *gps, *gpn;
703         rcti rect;
704         
705         /* rect is rectangle of eraser */
706         rect.xmin= p->mval[0] - p->radius;
707         rect.ymin= p->mval[1] - p->radius;
708         rect.xmax= p->mval[0] + p->radius;
709         rect.ymax= p->mval[1] + p->radius;
710         
711         /* loop over strokes, checking segments for intersections */
712         for (gps= gpf->strokes.first; gps; gps= gpn) {
713                 gpn= gps->next;
714                 gp_stroke_eraser_dostroke(p, p->mval, p->mvalo, p->radius, &rect, gpf, gps);
715         }
716 }
717
718 /* ******************************************* */
719 /* Sketching Operator */
720
721 /* clear the session buffers (call this before AND after a paint operation) */
722 static void gp_session_validatebuffer (tGPsdata *p)
723 {
724         bGPdata *gpd= p->gpd;
725         
726         /* clear memory of buffer (or allocate it if starting a new session) */
727         if (gpd->sbuffer)
728                 memset(gpd->sbuffer, 0, sizeof(tGPspoint)*GP_STROKE_BUFFER_MAX);
729         else
730                 gpd->sbuffer= MEM_callocN(sizeof(tGPspoint)*GP_STROKE_BUFFER_MAX, "gp_session_strokebuffer");
731         
732         /* reset indices */
733         gpd->sbuffer_size = 0;
734         
735         /* reset flags */
736         gpd->sbuffer_sflag= 0;
737 }
738
739 /* init new painting session */
740 static tGPsdata *gp_session_initpaint (bContext *C)
741 {
742         tGPsdata *p = NULL;
743         ScrArea *curarea= CTX_wm_area(C);
744         ARegion *ar= CTX_wm_region(C);
745         
746         /* make sure the active view (at the starting time) is a 3d-view */
747         if (curarea == NULL) {
748                 if (G.f & G_DEBUG) 
749                         printf("Error: No active view for painting \n");
750                 return NULL;
751         }
752         
753         /* create new context data */
754         p= MEM_callocN(sizeof(tGPsdata), "GPencil Drawing Data");
755         
756         /* pass on current scene */
757         p->scene= CTX_data_scene(C);
758         
759         switch (curarea->spacetype) {
760                 /* supported views first */
761                 case SPACE_VIEW3D:
762                 {
763                         //View3D *v3d= curarea->spacedata.first;
764                         
765                         /* set current area */
766                         p->sa= curarea;
767                         p->ar= ar;
768                         
769 #if 0 // XXX will this sort of antiquated stuff be restored?
770                         /* check that gpencil data is allowed to be drawn */
771                         if ((v3d->flag2 & V3D_DISPGP)==0) {
772                                 p->status= GP_STATUS_ERROR;
773                                 if (G.f & G_DEBUG) 
774                                         printf("Error: In active view, Grease Pencil not shown \n");
775                                 return p;
776                         }
777 #endif
778                 }
779                         break;
780 #if 0 // XXX these other spaces will come over time...
781                 case SPACE_NODE:
782                 {
783                         SpaceNode *snode= curarea->spacedata.first;
784                         
785                         /* set current area */
786                         p->sa= curarea;
787                         p->ar= ar;
788                         p->v2d= &ar->v2d;
789                         
790                         /* check that gpencil data is allowed to be drawn */
791                         if ((snode->flag & SNODE_DISPGP)==0) {
792                                 p->status= GP_STATUS_ERROR;
793                                 if (G.f & G_DEBUG) 
794                                         printf("Error: In active view, Grease Pencil not shown \n");
795                                 return;
796                         }
797                 }
798                         break;
799                 case SPACE_SEQ:
800                 {
801                         SpaceSeq *sseq= curarea->spacedata.first;
802                         
803                         /* set current area */
804                         p->sa= curarea;
805                         p->ar= ar;
806                         p->v2d= &ar->v2d;
807                         
808                         /* check that gpencil data is allowed to be drawn */
809                         if (sseq->mainb == SEQ_DRAW_SEQUENCE) {
810                                 p->status= GP_STATUS_ERROR;
811                                 if (G.f & G_DEBUG) 
812                                         printf("Error: In active view (sequencer), active mode doesn't support Grease Pencil \n");
813                                 return;
814                         }
815                         if ((sseq->flag & SEQ_DRAW_GPENCIL)==0) {
816                                 p->status= GP_STATUS_ERROR;
817                                 if (G.f & G_DEBUG) 
818                                         printf("Error: In active view, Grease Pencil not shown \n");
819                                 return;
820                         }
821                 }
822                         break;  
823                 case SPACE_IMAGE:
824                 {
825                         SpaceImage *sima= curarea->spacedata.first;
826                         
827                         /* set the current area */
828                         p->sa= curarea;
829                         p->ar= ar;
830                         p->v2d= &ar->v2d;
831                         p->ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
832                         
833                         /* check that gpencil data is allowed to be drawn */
834                         if ((sima->flag & SI_DISPGP)==0) {
835                                 p->status= GP_STATUS_ERROR;
836                                 if (G.f & G_DEBUG)
837                                         printf("Error: In active view, Grease Pencil not shown \n");
838                                 return;
839                         }
840                 }
841                         break;
842 #endif
843                 /* unsupported views */
844                 default:
845                 {
846                         p->status= GP_STATUS_ERROR;
847                         if (G.f & G_DEBUG) 
848                                 printf("Error: Active view not appropriate for Grease Pencil drawing \n");
849                         return p;
850                 }
851                         break;
852         }
853         
854         /* get gp-data */
855         p->gpd= gpencil_data_getactive(C);
856         if (p->gpd == NULL) {
857                 /* add new GPencil block for the active scene for now... */
858                 p->gpd= gpencil_data_addnew("GPencil");
859                 p->scene->gpd= p->gpd;
860         }
861         
862         /* set edit flags - so that buffer will get drawn */
863         G.f |= G_GREASEPENCIL;
864         
865         /* set initial run flag */
866         p->flags |= GP_PAINTFLAG_FIRSTRUN;
867         
868         /* clear out buffer (stored in gp-data), in case something contaminated it */
869         gp_session_validatebuffer(p);
870         
871 #if 0
872         /* set 'default' im2d_settings just in case something that uses this doesn't set it */
873         p->im2d_settings.sizex= 1;
874         p->im2d_settings.sizey= 1;
875 #endif
876         
877         /* return context data for running paint operator */
878         return p;
879 }
880
881 /* cleanup after a painting session */
882 static void gp_session_cleanup (tGPsdata *p)
883 {
884         bGPdata *gpd= (p) ? p->gpd : NULL;
885         
886         /* error checking */
887         if (gpd == NULL)
888                 return;
889         
890         /* free stroke buffer */
891         if (gpd->sbuffer) {
892                 MEM_freeN(gpd->sbuffer);
893                 gpd->sbuffer= NULL;
894         }
895         
896         /* clear flags */
897         gpd->sbuffer_size= 0;
898         gpd->sbuffer_sflag= 0;
899 }
900
901 /* init new stroke */
902 static void gp_paint_initstroke (tGPsdata *p, short paintmode)
903 {       
904         /* get active layer (or add a new one if non-existent) */
905         p->gpl= gpencil_layer_getactive(p->gpd);
906         if (p->gpl == NULL)
907                 p->gpl= gpencil_layer_addnew(p->gpd);
908         if (p->gpl->flag & GP_LAYER_LOCKED) {
909                 p->status= GP_STATUS_ERROR;
910                 if (G.f & G_DEBUG)
911                         printf("Error: Cannot paint on locked layer \n");
912                 return;
913         }
914                 
915         /* get active frame (add a new one if not matching frame) */
916         p->gpf= gpencil_layer_getframe(p->gpl, p->scene->r.cfra, 1);
917         if (p->gpf == NULL) {
918                 p->status= GP_STATUS_ERROR;
919                 if (G.f & G_DEBUG) 
920                         printf("Error: No frame created (gpencil_paint_init) \n");
921                 return;
922         }
923         else
924                 p->gpf->flag |= GP_FRAME_PAINT;
925         
926         /* set 'eraser' for this stroke if using eraser */
927         p->paintmode= paintmode;
928         if (p->paintmode == GP_PAINTMODE_ERASER)
929                 p->gpd->sbuffer_sflag |= GP_STROKE_ERASER;
930         
931         /* check if points will need to be made in view-aligned space */
932         // XXX this should be the default? this is something that needs review
933         /*if (p->gpd->flag & GP_DATA_VIEWALIGN)*/ {
934                 switch (p->sa->spacetype) {
935                         case SPACE_VIEW3D:
936                         {
937                                 View3D *v3d= (View3D *)p->sa->spacedata.first;
938                                 RegionView3D *rv3d= p->ar->regiondata;
939                                 
940                                 // TODO: this should only happen for scene... otherwise use object center!
941                                 float *fp= give_cursor(p->scene, v3d);
942                                 initgrabz(rv3d, fp[0], fp[1], fp[2]);
943                                 
944                                 p->gpd->sbuffer_sflag |= GP_STROKE_3DSPACE;
945                         }
946                                 break;
947 #if 0 // XXX other spacetypes to be restored in due course
948                         case SPACE_NODE:
949                         {
950                                 p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
951                         }
952                                 break;
953                         case SPACE_SEQ:
954                         {
955                                 SpaceSeq *sseq= (SpaceSeq *)p->sa->spacedata.first;
956                                 int rectx, recty;
957                                 float zoom, zoomx, zoomy;
958                                 
959                                 /* set draw 2d-stroke flag */
960                                 p->gpd->sbuffer_sflag |= GP_STROKE_2DIMAGE;
961                                 
962                                 /* calculate zoom factor */
963                                 zoom= (float)(SEQ_ZOOM_FAC(sseq->zoom));
964                                 if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
965                                         zoomx = zoom * ((float)p->scene->r.xasp / (float)p->scene->r.yasp);
966                                         zoomy = zoom;
967                                 } 
968                                 else
969                                         zoomx = zoomy = zoom;
970                                 
971                                 /* calculate rect size to use to calculate the size of the drawing area
972                                  *      - We use the size of the output image not the size of the ibuf being shown
973                                  *        as it is too messy getting the ibuf (and could be too slow). This should be
974                                  *        a reasonable for most cases anyway.
975                                  */
976                                 rectx= (p->scene->r.size * p->scene->r.xsch) / 100;
977                                 recty= (p->scene->r.size * p->scene->r.ysch) / 100; 
978                                 
979                                 /* set offset and scale values for opertations to use */
980                                 p->im2d_settings.sizex= (int)(zoomx * rectx);
981                                 p->im2d_settings.sizey= (int)(zoomy * recty);
982                                 p->im2d_settings.offsx= (int)((p->sa->winx-p->im2d_settings.sizex)/2 + sseq->xof);
983                                 p->im2d_settings.offsy= (int)((p->sa->winy-p->im2d_settings.sizey)/2 + sseq->yof);
984                         }
985                                 break;
986                         case SPACE_IMAGE:
987                         {
988                                 /* check if any ibuf available */
989                                 if (p->ibuf)
990                                         p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
991                         }
992                                 break;
993 #endif
994                 }
995         }
996 }
997
998 /* finish off a stroke (clears buffer, but doesn't finish the paint operation) */
999 static void gp_paint_strokeend (tGPsdata *p)
1000 {
1001         /* check if doing eraser or not */
1002         if ((p->gpd->sbuffer_sflag & GP_STROKE_ERASER) == 0) {
1003                 /* smooth stroke before transferring? */
1004                 gp_stroke_smooth(p);
1005                 
1006                 /* simplify stroke before transferring? */
1007                 gp_stroke_simplify(p);
1008                 
1009                 /* transfer stroke to frame */
1010                 gp_stroke_newfrombuffer(p);
1011         }
1012         
1013         /* clean up buffer now */
1014         gp_session_validatebuffer(p);
1015 }
1016
1017 /* finish off stroke painting operation */
1018 static void gp_paint_cleanup (tGPsdata *p)
1019 {
1020         /* finish off a stroke */
1021         gp_paint_strokeend(p);
1022         
1023         /* "unlock" frame */
1024         p->gpf->flag &= ~GP_FRAME_PAINT;
1025 }
1026
1027 /* ------------------------------- */
1028
1029
1030 static int gpencil_draw_init (bContext *C, wmOperator *op)
1031 {
1032         tGPsdata *p;
1033         int paintmode= RNA_enum_get(op->ptr, "mode");
1034         
1035         /* check context */
1036         p= op->customdata= gp_session_initpaint(C);
1037         if ((p == NULL) || (p->status == GP_STATUS_ERROR)) {
1038                 /* something wasn't set correctly in context */
1039                 gp_session_cleanup(p);
1040                 return 0;
1041         }
1042         
1043         /* init painting data */
1044         gp_paint_initstroke(p, paintmode);
1045         if (p->status == GP_STATUS_ERROR) {
1046                 gp_session_cleanup(p);
1047                 return 0;
1048         }
1049         
1050         /* radius for eraser circle is defined in userprefs now */
1051         // TODO: make this more easily tweaked... 
1052         p->radius= U.gp_eraser;
1053         
1054         /* everything is now setup ok */
1055         return 1;
1056 }
1057
1058 /* ------------------------------- */
1059
1060 static void gpencil_draw_exit (bContext *C, wmOperator *op)
1061 {
1062         tGPsdata *p= op->customdata;
1063         
1064         /* clear edit flags */
1065         G.f &= ~G_GREASEPENCIL;
1066         
1067         /* restore cursor to indicate end of drawing */
1068         // XXX  (cursor callbacks in regiontype) setcursor_space(p.sa->spacetype, CURSOR_STD);
1069         
1070         /* check size of buffer before cleanup, to determine if anything happened here */
1071         if (p->paintmode == GP_PAINTMODE_ERASER) {
1072                 // TODO clear radial cursor thing
1073                 // XXX draw_sel_circle(NULL, p.mvalo, 0, p.radius, 0);
1074         }
1075         
1076         /* cleanup */
1077         gp_paint_cleanup(p);
1078         gp_session_cleanup(p);
1079         
1080         /* finally, free the temp data */
1081         MEM_freeN(p);
1082         op->customdata= NULL;
1083 }
1084
1085 static int gpencil_draw_cancel (bContext *C, wmOperator *op)
1086 {
1087         /* this is just a wrapper around exit() */
1088         gpencil_draw_exit(C, op);
1089         return OPERATOR_CANCELLED;
1090 }
1091
1092 /* ------------------------------- */
1093
1094 static void gpencil_draw_apply_event (bContext *C, wmOperator *op, wmEvent *event)
1095 {
1096         tGPsdata *p= op->customdata;
1097         ARegion *ar= p->ar;
1098         int tablet=0;
1099
1100         /* convert from window-space to area-space mouse coordintes */
1101         // NOTE: float to ints conversions, +1 factor is probably used to ensure a bit more accurate rounding...
1102         p->mval[0]= event->x - ar->winrct.xmin + 1;
1103         p->mval[1]= event->y - ar->winrct.ymin + 1;
1104         
1105         /* handle pressure sensitivity (which is supplied by tablets) */
1106         if (event->custom == EVT_DATA_TABLET) {
1107                 wmTabletData *wmtab= event->customdata;
1108                 
1109                 tablet= (wmtab->Active != EVT_TABLET_NONE);
1110                 p->pressure= wmtab->Pressure;
1111                 //if (wmtab->Active == EVT_TABLET_ERASER)
1112                         // TODO... this should get caught by the keymaps which call drawing in the first place
1113         }
1114         else
1115                 p->pressure= 1.0f;
1116         
1117         /* special exception for start of strokes (i.e. maybe for just a dot) */
1118         if (p->flags & GP_PAINTFLAG_FIRSTRUN) {
1119                 p->flags &= ~GP_PAINTFLAG_FIRSTRUN;
1120                 
1121                 p->mvalo[0]= p->mval[0];
1122                 p->mvalo[1]= p->mval[1];
1123                 p->opressure= p->pressure;
1124                 
1125                 /* special exception here for too high pressure values on first touch in
1126                  *  windows for some tablets, then we just skip first touch ..  
1127                  */
1128                 if (tablet && (p->pressure >= 0.99f))
1129                         return;
1130         }
1131         
1132         
1133         /* handle drawing/erasing -> test for erasing first */
1134         if (p->paintmode == GP_PAINTMODE_ERASER) {
1135                 /* do 'live' erasing now */
1136                 gp_stroke_doeraser(p);
1137                 
1138                 /* store used values */
1139                 p->mvalo[0]= p->mval[0];
1140                 p->mvalo[1]= p->mval[1];
1141                 p->opressure= p->pressure;
1142         }
1143         /* only add current point to buffer if mouse moved (even though we got an event, it might be just noise) */
1144         else if (gp_stroke_filtermval(p, p->mval, p->mvalo)) {
1145                 /* try to add point */
1146                 short ok= gp_stroke_addpoint(p, p->mval, p->pressure);
1147                 
1148                 /* handle errors while adding point */
1149                 if ((ok == GP_STROKEADD_FULL) || (ok == GP_STROKEADD_OVERFLOW)) {
1150                         /* finish off old stroke */
1151                         gp_paint_strokeend(p);
1152                         
1153                         /* start a new stroke, starting from previous point */
1154                         gp_stroke_addpoint(p, p->mvalo, p->opressure);
1155                         ok= gp_stroke_addpoint(p, p->mval, p->pressure);
1156                 }
1157                 else if (ok == GP_STROKEADD_INVALID) {
1158                         /* the painting operation cannot continue... */
1159                         BKE_report(op->reports, RPT_ERROR, "Cannot paint stroke");
1160                         p->status = GP_STATUS_ERROR;
1161                         
1162                         if (G.f & G_DEBUG) 
1163                                 printf("Error: Grease-Pencil Paint - Add Point Invalid \n");
1164                         // XXX break!
1165                 }
1166                 
1167                 /* store used values */
1168                 p->mvalo[0]= p->mval[0];
1169                 p->mvalo[1]= p->mval[1];
1170                 p->opressure= p->pressure;
1171         }
1172         
1173         /* force refresh */
1174         WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL); // XXX please work!
1175 }
1176
1177 /* ------------------------------- */
1178
1179 static int gpencil_draw_invoke (bContext *C, wmOperator *op, wmEvent *event)
1180 {
1181         tGPsdata *p = NULL;
1182         
1183         printf("GPencil - Starting Drawing \n");
1184         
1185         /* try to initialise context data needed while drawing */
1186         if (!gpencil_draw_init(C, op)) {
1187                 if (op->customdata) MEM_freeN(op->customdata);
1188                 printf("\tGP - no valid data \n");
1189                 return OPERATOR_CANCELLED;
1190         }
1191         else
1192                 p= op->customdata;
1193         
1194         // TODO: set any additional settings that we can take from the events?
1195         // TODO? if tablet is erasing, force eraser to be on?
1196         
1197         /* if eraser is on, draw radial aid */
1198         if (p->paintmode == GP_PAINTMODE_ERASER) {
1199                 // TODO: this involves mucking around with radial control, so we leave this for now..
1200         }
1201         
1202         printf("\tGP - set first spot\n");
1203         
1204         /* handle the initial drawing - i.e. for just doing a simple dot */
1205         gpencil_draw_apply_event(C, op, event);
1206         
1207         /* add a modal handler for this operator, so that we can then draw continuous strokes */
1208         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1209         return OPERATOR_RUNNING_MODAL;
1210 }
1211
1212 static int gpencil_draw_modal (bContext *C, wmOperator *op, wmEvent *event)
1213 {
1214         tGPsdata *p= op->customdata;
1215         
1216         printf("\tGP - handle modal event...\n");
1217         
1218         switch (event->type) {
1219                 /* end of stroke -> ONLY when a mouse-button release occurs 
1220                  * otherwise, carry on to mouse-move...
1221                  */
1222                 case LEFTMOUSE:
1223                 case MIDDLEMOUSE:
1224                 case RIGHTMOUSE: 
1225                         if (event->val != KM_PRESS) {
1226                                 printf("\t\tGP - end of stroke \n");
1227                                 gpencil_draw_exit(C, op);
1228                                 return OPERATOR_FINISHED;
1229                         }
1230                 
1231                 /* moving mouse - assumed that mouse button is down */
1232                 case MOUSEMOVE:
1233                         /* handle drawing event */
1234                         printf("\t\tGP - add point\n");
1235                         gpencil_draw_apply_event(C, op, event);
1236                         
1237                         /* finish painting operation if anything went wrong just now */
1238                         if (p->status == GP_STATUS_ERROR) {
1239                                 printf("\t\t\tGP - error done! \n");
1240                                 gpencil_draw_exit(C, op);
1241                                 return OPERATOR_CANCELLED;
1242                         }
1243                         break;
1244                 
1245                 /* scrolling mouse-wheel increases radius of eraser 
1246                  *      - though this is quite a difficult action to perform
1247                  */
1248                 case WHEELUPMOUSE:
1249                         p->radius += 1.5f;
1250                         break;
1251                 case WHEELDOWNMOUSE:
1252                         p->radius -= 1.5f;
1253                         break;
1254                         
1255                 /* handle ctrl key - used to toggle straight-lines only (for drawing) */
1256                 // XXX hardcoded keymap stuff
1257                 case LEFTCTRLKEY:
1258                 case RIGHTCTRLKEY:
1259                         if (event->val == KM_PRESS)
1260                                 p->flags |= GP_PAINTFLAG_STRAIGHTLINES;
1261                         else if (event->val == KM_RELEASE)
1262                                 p->flags &= ~GP_PAINTFLAG_STRAIGHTLINES;
1263                         break;
1264                         
1265                 default:
1266                         printf("\t\tGP unknown event - %d \n", event->type);
1267                         break;
1268         }
1269         
1270         return OPERATOR_RUNNING_MODAL;
1271 }
1272
1273 /* ------------------------------- */
1274
1275 static EnumPropertyItem prop_gpencil_drawmodes[] = {
1276         {GP_PAINTMODE_DRAW, "DRAW", 0, "Draw", ""},
1277         {GP_PAINTMODE_ERASER, "ERASER", 0, "Eraser", ""},
1278         {0, NULL, 0, NULL, NULL}
1279 };
1280
1281 void GPENCIL_OT_draw (wmOperatorType *ot)
1282 {
1283         /* identifiers */
1284         ot->name= "Grease Pencil Draw";
1285         ot->idname= "GPENCIL_OT_draw";
1286         ot->description= "Make annotations on the active data.";
1287         
1288         /* api callbacks */
1289         //ot->exec= gpencil_draw_exec;
1290         ot->invoke= gpencil_draw_invoke;
1291         ot->modal= gpencil_draw_modal;
1292         ot->cancel= gpencil_draw_cancel;
1293         ot->poll= gpencil_draw_poll;
1294         
1295         /* flags */
1296         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1297         
1298         /* settings for drawing */
1299         RNA_def_enum(ot->srna, "mode", prop_gpencil_drawmodes, 0, "Mode", "Way to intepret mouse movements.");
1300 }