71
79
typedef struct tGPsdata {
72
80
Scene *scene; /* current scene from context */
74
wmWindow *win; /* window where painting originated */
75
ScrArea *sa; /* area where painting originated */
82
wmWindow *win; /* window where painting originated */
83
ScrArea *sa; /* area where painting originated */
76
84
ARegion *ar; /* region where painting originated */
77
View2D *v2d; /* needed for GP_STROKE_2DSPACE */
78
rctf *subrect; /* for using the camera rect within the 3d view */
85
View2D *v2d; /* needed for GP_STROKE_2DSPACE */
86
rctf *subrect; /* for using the camera rect within the 3d view */
82
#if 0 // XXX review this 2d image stuff...
83
ImBuf *ibuf; /* needed for GP_STROKE_2DIMAGE */
84
struct IBufViewSettings {
85
int offsx, offsy; /* offsets */
86
int sizex, sizey; /* dimensions to use as scale-factor */
87
} im2d_settings; /* needed for GP_STROKE_2DIMAGE */
90
PointerRNA ownerPtr;/* pointer to owner of gp-datablock */
91
bGPdata *gpd; /* gp-datablock layer comes from */
92
bGPDlayer *gpl; /* layer we're working on */
93
bGPDframe *gpf; /* frame we're working on */
95
short status; /* current status of painting */
96
short paintmode; /* mode for painting */
98
int mval[2]; /* current mouse-position */
99
int mvalo[2]; /* previous recorded mouse-position */
101
float pressure; /* current stylus pressure */
102
float opressure; /* previous stylus pressure */
104
short radius; /* radius of influence for eraser */
105
short flags; /* flags that can get set during runtime */
89
PointerRNA ownerPtr; /* pointer to owner of gp-datablock */
90
bGPdata *gpd; /* gp-datablock layer comes from */
91
bGPDlayer *gpl; /* layer we're working on */
92
bGPDframe *gpf; /* frame we're working on */
94
short status; /* current status of painting */
95
short paintmode; /* mode for painting */
97
int mval[2]; /* current mouse-position */
98
int mvalo[2]; /* previous recorded mouse-position */
100
float pressure; /* current stylus pressure */
101
float opressure; /* previous stylus pressure */
103
short radius; /* radius of influence for eraser */
104
short flags; /* flags that can get set during runtime */
106
/* These need to be doubles, as (at least under unix) they are in seconds since epoch,
107
* float (and its 7 digits precision) is definitively not enough here!
108
* double, with its 15 digits precision, ensures us millisecond precision for a few centuries at least.
110
double inittime; /* Used when converting to path */
111
double curtime; /* Used when converting to path */
112
double ocurtime; /* Used when converting to path */
107
114
float imat[4][4]; /* inverted transformation matrix applying when converting coords from screen-space
108
115
* to region space */
110
float custom_color[4]; /* custom color for (?) */
117
float custom_color[4]; /* custom color - hack for enforcing a particular color for track/mask editing */
119
void *erasercursor; /* radial cursor data for drawing eraser */
113
122
/* values for tGPsdata->status */
115
GP_STATUS_IDLING = 0, /* stroke isn't in progress yet */
116
GP_STATUS_PAINTING, /* a stroke is in progress */
117
GP_STATUS_ERROR, /* something wasn't correctly set up */
118
GP_STATUS_DONE /* painting done */
124
GP_STATUS_IDLING = 0, /* stroke isn't in progress yet */
125
GP_STATUS_PAINTING, /* a stroke is in progress */
126
GP_STATUS_ERROR, /* something wasn't correctly set up */
127
GP_STATUS_DONE /* painting done */
121
130
/* Return flags for adding points to stroke buffer */
123
GP_STROKEADD_INVALID = -2, /* error occurred - insufficient info to do so */
124
GP_STROKEADD_OVERFLOW = -1, /* error occurred - cannot fit any more points */
125
GP_STROKEADD_NORMAL, /* point was successfully added */
126
GP_STROKEADD_FULL /* cannot add any more points to buffer */
132
GP_STROKEADD_INVALID = -2, /* error occurred - insufficient info to do so */
133
GP_STROKEADD_OVERFLOW = -1, /* error occurred - cannot fit any more points */
134
GP_STROKEADD_NORMAL, /* point was successfully added */
135
GP_STROKEADD_FULL /* cannot add any more points to buffer */
129
138
/* Runtime flags */
131
GP_PAINTFLAG_FIRSTRUN = (1<<0), /* operator just started */
132
GP_PAINTFLAG_STROKEADDED = (1<<1) /* stroke was already added during draw session */
140
GP_PAINTFLAG_FIRSTRUN = (1 << 0), /* operator just started */
141
GP_PAINTFLAG_STROKEADDED = (1 << 1)
137
146
/* maximum sizes of gp-session buffer */
138
#define GP_STROKE_BUFFER_MAX 5000
147
#define GP_STROKE_BUFFER_MAX 5000
140
149
/* Macros for accessing sensitivity thresholds... */
141
/* minimum number of pixels mouse should move before new point created */
142
#define MIN_MANHATTEN_PX (U.gp_manhattendist)
143
/* minimum length of new segment before new point can be added */
144
#define MIN_EUCLIDEAN_PX (U.gp_euclideandist)
150
/* minimum number of pixels mouse should move before new point created */
151
#define MIN_MANHATTEN_PX (U.gp_manhattendist)
152
/* minimum length of new segment before new point can be added */
153
#define MIN_EUCLIDEAN_PX (U.gp_euclideandist)
155
static int gp_stroke_added_check(tGPsdata *p)
157
return (p->gpf && p->gpf->strokes.last && p->flags & GP_PAINTFLAG_STROKEADDED);
160
static void gp_stroke_added_enable(tGPsdata *p)
162
BLI_assert(p->gpf->strokes.last != NULL);
163
p->flags |= GP_PAINTFLAG_STROKEADDED;
147
167
/* Forward defines for some functions... */
187
207
/* Utilities --------------------------------- */
189
209
/* get the reference point for stroke-point conversions */
190
static void gp_get_3d_reference (tGPsdata *p, float vec[3])
210
static void gp_get_3d_reference(tGPsdata *p, float vec[3])
192
View3D *v3d= p->sa->spacedata.first;
193
float *fp= give_cursor(p->scene, v3d);
212
View3D *v3d = p->sa->spacedata.first;
213
const float *fp = give_cursor(p->scene, v3d);
195
215
/* the reference point used depends on the owner... */
196
#if 0 // XXX: disabled for now, since we can't draw relative to the owner yet
216
#if 0 /* XXX: disabled for now, since we can't draw relative to the owner yet */
197
217
if (p->ownerPtr.type == &RNA_Object) {
198
Object *ob= (Object *)p->ownerPtr.data;
218
Object *ob = (Object *)p->ownerPtr.data;
201
* - use relative distance of 3D-cursor from object center
221
* - use relative distance of 3D-cursor from object center
203
223
sub_v3_v3v3(vec, fp, ob->loc);
208
228
/* use 3D-cursor */
209
229
copy_v3_v3(vec, fp);
270
290
gp_get_3d_reference(p, rvec);
272
292
/* method taken from editview.c - mouse_cursor() */
273
project_int_noclip(p->ar, rvec, mval_prj);
275
VECSUB2D(mval_f, mval_prj, mval);
276
ED_view3d_win_to_delta(p->ar, mval_f, dvec);
277
sub_v3_v3v3(out, rvec, dvec);
293
/* TODO, use ED_view3d_project_float_global */
294
if (ED_view3d_project_int_global(p->ar, rvec, mval_prj, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
295
VECSUB2D(mval_f, mval_prj, mval);
296
ED_view3d_win_to_delta(p->ar, mval_f, dvec);
297
sub_v3_v3v3(out, rvec, dvec);
310
315
out[1] = (float)(mval[1]) / (float)(p->ar->winy) * 100;
312
317
else { /* camera view, use subrect */
313
out[0]= ((mval[0] - p->subrect->xmin) / ((p->subrect->xmax - p->subrect->xmin))) * 100;
314
out[1]= ((mval[1] - p->subrect->ymin) / ((p->subrect->ymax - p->subrect->ymin))) * 100;
318
out[0] = ((mval[0] - p->subrect->xmin) / BLI_rctf_size_x(p->subrect)) * 100;
319
out[1] = ((mval[1] - p->subrect->ymin) / BLI_rctf_size_y(p->subrect)) * 100;
319
324
/* add current stroke-point to buffer (returns whether point was successfully added) */
320
static short gp_stroke_addpoint (tGPsdata *p, const int mval[2], float pressure)
325
static short gp_stroke_addpoint(tGPsdata *p, const int mval[2], float pressure, double curtime)
322
bGPdata *gpd= p->gpd;
327
bGPdata *gpd = p->gpd;
325
330
/* check painting mode */
326
331
if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) {
327
332
/* straight lines only - i.e. only store start and end point in buffer */
328
333
if (gpd->sbuffer_size == 0) {
329
334
/* first point in buffer (start point) */
330
pt= (tGPspoint *)(gpd->sbuffer);
335
pt = (tGPspoint *)(gpd->sbuffer);
332
337
/* store settings */
333
338
copy_v2_v2_int(&pt->x, mval);
334
pt->pressure= pressure;
339
pt->pressure = pressure;
340
pt->time = (float)(curtime - p->inittime);
336
342
/* increment buffer size */
337
343
gpd->sbuffer_size++;
381
389
else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
382
390
/* get pointer to destination point */
383
pt= (tGPspoint *)(gpd->sbuffer);
391
pt = (tGPspoint *)(gpd->sbuffer);
385
393
/* store settings */
386
394
copy_v2_v2_int(&pt->x, mval);
387
pt->pressure= pressure;
395
pt->pressure = pressure;
396
pt->time = (float)(curtime - p->inittime);
389
398
/* if there's stroke for this poly line session add (or replace last) point
390
399
* to stroke. This allows to draw lines more interactively (see new segment
391
* during mouse slide, i.e.)
400
* during mouse slide, e.g.)
393
if (p->flags & GP_PAINTFLAG_STROKEADDED) {
394
bGPDstroke *gps= p->gpf->strokes.last;
402
if (gp_stroke_added_check(p)) {
403
bGPDstroke *gps = p->gpf->strokes.last;
397
406
/* first time point is adding to temporary buffer -- need to allocate new point in stroke */
398
407
if (gpd->sbuffer_size == 0) {
399
gps->points = MEM_reallocN(gps->points, sizeof(bGPDspoint)*(gps->totpoints+1));
408
gps->points = MEM_reallocN(gps->points, sizeof(bGPDspoint) * (gps->totpoints + 1));
400
409
gps->totpoints++;
403
pts = &gps->points[gps->totpoints-1];
405
/* special case for poly lines: normally, depth is needed only when creating new stroke from buffer,
406
* but poly lines are converting to stroke instantly, so initialize depth buffer before converting coordinates
412
pts = &gps->points[gps->totpoints - 1];
414
/* special case for poly lines: normally,
415
* depth is needed only when creating new stroke from buffer,
416
* but poly lines are converting to stroke instantly,
417
* so initialize depth buffer before converting coordinates
408
419
if (gpencil_project_check(p)) {
409
View3D *v3d= p->sa->spacedata.first;
420
View3D *v3d = p->sa->spacedata.first;
411
422
view3d_region_operator_needs_opengl(p->win, p->ar);
412
ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1:0);
423
ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1 : 0);
415
426
/* convert screen-coordinates to appropriate coordinates (and store them) */
416
427
gp_stroke_convertcoords(p, &pt->x, &pts->x, NULL);
419
pts->pressure= pt->pressure;
429
/* copy pressure and time */
430
pts->pressure = pt->pressure;
431
pts->time = pt->time;
422
434
/* increment counters */
423
435
if (gpd->sbuffer_size == 0)
424
436
gpd->sbuffer_size++;
426
438
return GP_STROKEADD_NORMAL;
452
457
if ((cmx <= 2) || (gpd->sbuffer == NULL))
455
/* create a temporary smoothing coordinates buffer, use to store calculated values to prevent sequential error */
456
smoothArray = MEM_callocN(sizeof(tGpSmoothCo)*cmx, "gp_stroke_smooth smoothArray");
458
/* first pass: calculate smoothing coordinates using weighted-averages */
459
for (i=0, spc=smoothArray; i < gpd->sbuffer_size; i++, spc++) {
460
const tGPspoint *pc= (((tGPspoint *)gpd->sbuffer) + i);
461
const tGPspoint *pb= (i-1 > 0)?(pc-1):(pc);
462
const tGPspoint *pa= (i-2 > 0)?(pc-2):(pb);
463
const tGPspoint *pd= (i+1 < cmx)?(pc+1):(pc);
464
const tGPspoint *pe= (i+2 < cmx)?(pc+2):(pd);
466
spc->x= (int)(0.1*pa->x + 0.2*pb->x + 0.4*pc->x + 0.2*pd->x + 0.1*pe->x);
467
spc->y= (int)(0.1*pa->y + 0.2*pb->y + 0.4*pc->y + 0.2*pd->y + 0.1*pe->y);
470
/* second pass: apply smoothed coordinates */
471
for (i=0, spc=smoothArray; i < gpd->sbuffer_size; i++, spc++) {
472
tGPspoint *pc= (((tGPspoint *)gpd->sbuffer) + i);
474
copy_v2_v2_int(&pc->x, &spc->x);
477
/* free temp array */
478
MEM_freeN(smoothArray);
460
/* Calculate smoothing coordinates using weighted-averages
461
* WARNING: we do NOT smooth first and last points (to avoid shrinkage)
463
spt = (tGPspoint *)gpd->sbuffer;
465
/* This (tmp_spt) small array stores the last two points' original coordinates,
466
* as we don't want to use already averaged ones! It is used as a cyclic buffer...
469
for (i = 1, spt++; i < cmx - 1; i++, spt++) {
470
const tGPspoint *pc = spt;
471
const tGPspoint *pb = &tmp_spt[(i - 1) % 3];
472
const tGPspoint *pa = (i - 1 > 0) ? (&tmp_spt[(i - 2) % 3]) : (pb);
473
const tGPspoint *pd = pc + 1;
474
const tGPspoint *pe = (i + 2 < cmx) ? (pc + 2) : (pd);
476
/* Store current point's original state for the two next points! */
477
tmp_spt[i % 3] = *spt;
479
spt->x = (int)(0.1 * pa->x + 0.2 * pb->x + 0.4 * pc->x + 0.2 * pd->x + 0.1 * pe->x);
480
spt->y = (int)(0.1 * pa->y + 0.2 * pb->y + 0.4 * pc->y + 0.2 * pd->y + 0.1 * pe->y);
481
484
/* simplify a stroke (in buffer) before storing it
482
485
* - applies a reverse Chaikin filter
483
486
* - code adapted from etch-a-ton branch (editarmature_sketch.c)
485
static void gp_stroke_simplify (tGPsdata *p)
488
static void gp_stroke_simplify(tGPsdata *p)
487
bGPdata *gpd= p->gpd;
488
tGPspoint *old_points= (tGPspoint *)gpd->sbuffer;
489
short num_points= gpd->sbuffer_size;
490
short flag= gpd->sbuffer_sflag;
490
bGPdata *gpd = p->gpd;
491
tGPspoint *old_points = (tGPspoint *)gpd->sbuffer;
492
short num_points = gpd->sbuffer_size;
493
short flag = gpd->sbuffer_sflag;
493
496
/* only simplify if simplification is enabled, and we're not doing a straight line */
514
517
co[0] += (float)(old_points[offs].x * sfac); \
515
518
co[1] += (float)(old_points[offs].y * sfac); \
516
519
pressure += old_points[offs].pressure * sfac; \
520
time += old_points[offs].time * sfac; \
523
/* XXX Here too, do not lose start and end points! */
524
gp_stroke_addpoint(p, &old_points->x, old_points->pressure, p->inittime + (double)old_points->time);
519
525
for (i = 0, j = 0; i < num_points; i++) {
520
526
if (i - j == 3) {
521
float co[2], pressure;
527
float co[2], pressure, time;
524
530
/* initialize values */
529
536
/* using macro, calculate new point */
530
537
GP_SIMPLIFY_AVPOINT(j, -0.25f);
531
GP_SIMPLIFY_AVPOINT(j+1, 0.75f);
532
GP_SIMPLIFY_AVPOINT(j+2, 0.75f);
533
GP_SIMPLIFY_AVPOINT(j+3, -0.25f);
538
GP_SIMPLIFY_AVPOINT(j + 1, 0.75f);
539
GP_SIMPLIFY_AVPOINT(j + 2, 0.75f);
540
GP_SIMPLIFY_AVPOINT(j + 3, -0.25f);
535
542
/* set values for adding */
539
546
/* ignore return values on this... assume to be ok for now */
540
gp_stroke_addpoint(p, mco, pressure);
547
gp_stroke_addpoint(p, mco, pressure, p->inittime + (double)time);
552
gp_stroke_addpoint(p, &old_points[num_points - 1].x, old_points[num_points - 1].pressure,
553
p->inittime + (double)old_points[num_points - 1].time);
546
555
/* free old buffer */
547
556
MEM_freeN(old_points);
577
586
/* special case for poly line -- for already added stroke during session
578
587
* coordinates are getting added to stroke immediately to allow more
579
* interactive behavior */
588
* interactive behavior
580
590
if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
581
if (p->flags & GP_PAINTFLAG_STROKEADDED)
591
if (gp_stroke_added_check(p)) {
585
596
/* allocate memory for a new stroke */
586
gps= MEM_callocN(sizeof(bGPDstroke), "gp_stroke");
597
gps = MEM_callocN(sizeof(bGPDstroke), "gp_stroke");
588
599
/* copy appropriate settings for stroke */
589
gps->totpoints= totelem;
590
gps->thickness= p->gpl->thickness;
591
gps->flag= gpd->sbuffer_sflag;
600
gps->totpoints = totelem;
601
gps->thickness = p->gpl->thickness;
602
gps->flag = gpd->sbuffer_sflag;
603
gps->inittime = p->inittime;
593
605
/* allocate enough memory for a continuous array for storage points */
594
gps->points= MEM_callocN(sizeof(bGPDspoint)*gps->totpoints, "gp_stroke_points");
606
gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
596
608
/* set pointer to first non-initialized point */
597
pt= gps->points + (gps->totpoints - totelem);
609
pt = gps->points + (gps->totpoints - totelem);
599
611
/* copy points from the buffer to the stroke */
600
612
if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) {
601
613
/* straight lines only -> only endpoints */
603
615
/* first point */
606
618
/* convert screen-coordinates to appropriate coordinates (and store them) */
607
619
gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
610
pt->pressure= ptc->pressure;
621
/* copy pressure and time */
622
pt->pressure = ptc->pressure;
623
pt->time = ptc->time;
615
628
if (totelem == 2) {
616
629
/* last point if applicable */
617
ptc= ((tGPspoint *)gpd->sbuffer) + (gpd->sbuffer_size - 1);
630
ptc = ((tGPspoint *)gpd->sbuffer) + (gpd->sbuffer_size - 1);
619
632
/* convert screen-coordinates to appropriate coordinates (and store them) */
620
633
gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
623
pt->pressure= ptc->pressure;
635
/* copy pressure and time */
636
pt->pressure = ptc->pressure;
637
pt->time = ptc->time;
626
640
else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
627
641
/* first point */
630
644
/* convert screen-coordinates to appropriate coordinates (and store them) */
631
645
gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL);
634
pt->pressure= ptc->pressure;
647
/* copy pressure and time */
648
pt->pressure = ptc->pressure;
649
pt->time = ptc->time;
637
float *depth_arr= NULL;
652
float *depth_arr = NULL;
639
654
/* get an array of depths, far depths are blended */
640
655
if (gpencil_project_check(p)) {
641
int mval[2], mval_prev[2]= {0};
656
int mval[2], mval_prev[2] = {0};
642
657
int interp_depth = 0;
643
658
int found_depth = 0;
645
depth_arr= MEM_mallocN(sizeof(float) * gpd->sbuffer_size, "depth_points");
647
for (i=0, ptc=gpd->sbuffer; i < gpd->sbuffer_size; i++, ptc++, pt++) {
660
depth_arr = MEM_mallocN(sizeof(float) * gpd->sbuffer_size, "depth_points");
662
for (i = 0, ptc = gpd->sbuffer; i < gpd->sbuffer_size; i++, ptc++, pt++) {
648
663
copy_v2_v2_int(mval, &ptc->x);
650
if ((ED_view3d_autodist_depth(p->ar, mval, depth_margin, depth_arr+i) == 0) &&
651
(i && (ED_view3d_autodist_depth_seg(p->ar, mval, mval_prev, depth_margin + 1, depth_arr+i) == 0))
665
if ((ED_view3d_autodist_depth(p->ar, mval, depth_margin, depth_arr + i) == 0) &&
666
(i && (ED_view3d_autodist_depth_seg(p->ar, mval, mval_prev, depth_margin + 1, depth_arr + i) == 0)))
659
674
copy_v2_v2_int(mval_prev, mval);
662
677
if (found_depth == FALSE) {
663
678
/* eeh... not much we can do.. :/, ignore depth in this case, use the 3D cursor */
664
for (i=gpd->sbuffer_size-1; i >= 0; i--)
679
for (i = gpd->sbuffer_size - 1; i >= 0; i--)
665
680
depth_arr[i] = 0.9999f;
701
716
/* convert all points (normal behavior) */
702
for (i=0, ptc=gpd->sbuffer; i < gpd->sbuffer_size && ptc; i++, ptc++, pt++) {
717
for (i = 0, ptc = gpd->sbuffer; i < gpd->sbuffer_size && ptc; i++, ptc++, pt++) {
703
718
/* convert screen-coordinates to appropriate coordinates (and store them) */
704
gp_stroke_convertcoords(p, &ptc->x, &pt->x, depth_arr ? depth_arr+i:NULL);
719
gp_stroke_convertcoords(p, &ptc->x, &pt->x, depth_arr ? depth_arr + i : NULL);
707
pt->pressure= ptc->pressure;
721
/* copy pressure and time */
722
pt->pressure = ptc->pressure;
723
pt->time = ptc->time;
711
727
MEM_freeN(depth_arr);
714
p->flags |= GP_PAINTFLAG_STROKEADDED;
716
730
/* add stroke to frame */
717
731
BLI_addtail(&p->gpf->strokes, gps);
732
gp_stroke_added_enable(p);
720
735
/* --- 'Eraser' for 'Paint' Tool ------ */
722
737
/* eraser tool - remove segment from stroke/split stroke (after lasso inside) */
723
static short gp_stroke_eraser_splitdel (bGPDframe *gpf, bGPDstroke *gps, int i)
738
static short gp_stroke_eraser_splitdel(bGPDframe *gpf, bGPDstroke *gps, int i)
725
bGPDspoint *pt_tmp= gps->points;
740
bGPDspoint *pt_tmp = gps->points;
726
741
bGPDstroke *gsn = NULL;
728
743
/* if stroke only had two points, get rid of stroke */
729
744
if (gps->totpoints == 2) {
730
745
/* free stroke points, then stroke */
748
763
/* nothing left in stroke, so stop */
752
767
/* if first segment, just remove segment from the stroke */
753
768
else if (i == 0) {
754
769
/* allocate new points array, and assign most of the old stroke there */
755
770
gps->totpoints--;
756
gps->points= MEM_callocN(sizeof(bGPDspoint)*gps->totpoints, "gp_stroke_points");
757
memcpy(gps->points, pt_tmp + 1, sizeof(bGPDspoint)*gps->totpoints);
771
gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
772
memcpy(gps->points, pt_tmp + 1, sizeof(bGPDspoint) * gps->totpoints);
774
/* We must adjust timings!
775
* Each point's timing data is a delta from stroke's inittime, so as we erase the first
776
* point of the stroke, we have to offset this inittime and all remaining points' delta values.
777
* This way we get a new stroke with exactly the same timing as if user had started drawing from
778
* the second point...
782
float delta = pt_tmp[1].time;
785
gps->inittime += (double)delta;
788
for (j = 0; j < gps->totpoints; j++, pts++) {
759
793
/* free temp buffer */
760
794
MEM_freeN(pt_tmp);
762
796
/* no break here, as there might still be stuff to remove in this stroke */
766
800
/* segment occurs in 'middle' of stroke, so split */
768
802
/* duplicate stroke, and assign 'later' data to that stroke */
769
gsn= MEM_dupallocN(gps);
770
gsn->prev= gsn->next= NULL;
803
gsn = MEM_dupallocN(gps);
804
gsn->prev = gsn->next = NULL;
771
805
BLI_insertlinkafter(&gpf->strokes, gps, gsn);
773
gsn->totpoints= gps->totpoints - i;
774
gsn->points= MEM_callocN(sizeof(bGPDspoint)*gsn->totpoints, "gp_stroke_points");
775
memcpy(gsn->points, pt_tmp + i, sizeof(bGPDspoint)*gsn->totpoints);
807
gsn->totpoints = gps->totpoints - i;
808
gsn->points = MEM_callocN(sizeof(bGPDspoint) * gsn->totpoints, "gp_stroke_points");
809
memcpy(gsn->points, pt_tmp + i, sizeof(bGPDspoint) * gsn->totpoints);
811
/* We must adjust timings of this new stroke!
812
* Each point's timing data is a delta from stroke's inittime, so as we erase the first
813
* point of the stroke, we have to offset this inittime and all remaing points' delta values.
814
* This way we get a new stroke with exactly the same timing as if user had started drawing from
815
* the second point...
819
float delta = pt_tmp[i].time;
822
gsn->inittime += (double)delta;
825
for (j = 0; j < gsn->totpoints; j++, pts++) {
777
830
/* adjust existing stroke */
779
gps->points= MEM_callocN(sizeof(bGPDspoint)*gps->totpoints, "gp_stroke_points");
780
memcpy(gps->points, pt_tmp, sizeof(bGPDspoint)*i);
832
gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points");
833
memcpy(gps->points, pt_tmp, sizeof(bGPDspoint) * i);
782
835
/* free temp buffer */
783
836
MEM_freeN(pt_tmp);
790
843
/* eraser tool - check if part of stroke occurs within last segment drawn by eraser */
791
static short gp_stroke_eraser_strokeinside (int mval[], int UNUSED(mvalo[]), short rad, short x0, short y0, short x1, short y1)
844
static short gp_stroke_eraser_strokeinside(const int mval[], const int UNUSED(mvalo[]),
845
int rad, int x0, int y0, int x1, int y1)
793
847
/* simple within-radius check for now */
794
if (edge_inside_circle(mval[0], mval[1], rad, x0, y0, x1, y1))
848
const float mval_fl[2] = {mval[0], mval[1]};
849
const float screen_co_a[2] = {x0, y0};
850
const float screen_co_b[2] = {x1, y1};
852
if (edge_inside_circle(mval_fl, rad, screen_co_a, screen_co_b)) {
860
static void gp_point_to_xy(ARegion *ar, View2D *v2d, rctf *subrect, bGPDstroke *gps, bGPDspoint *pt,
865
if (gps->flag & GP_STROKE_3DSPACE) {
866
if (ED_view3d_project_int_global(ar, &pt->x, xyval, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
871
*r_x = V2D_IS_CLIPPED;
872
*r_y = V2D_IS_CLIPPED;
875
else if (gps->flag & GP_STROKE_2DSPACE) {
876
UI_view2d_view_to_region(v2d, pt->x, pt->y, r_x, r_y);
879
if (subrect == NULL) { /* normal 3D view */
880
*r_x = (int)(pt->x / 100 * ar->winx);
881
*r_y = (int)(pt->y / 100 * ar->winy);
883
else { /* camera view, use subrect */
884
*r_x = (int)((pt->x / 100) * BLI_rctf_size_x(subrect)) + subrect->xmin;
885
*r_y = (int)((pt->y / 100) * BLI_rctf_size_y(subrect)) + subrect->ymin;
801
891
/* eraser tool - evaluation per stroke */
802
// TODO: this could really do with some optimization (KD-Tree/BVH?)
803
static void gp_stroke_eraser_dostroke (tGPsdata *p, int mval[], int mvalo[], short rad, rcti *rect, bGPDframe *gpf, bGPDstroke *gps)
892
/* TODO: this could really do with some optimization (KD-Tree/BVH?) */
893
static void gp_stroke_eraser_dostroke(tGPsdata *p,
894
const int mval[], const int mvalo[],
895
short rad, const rcti *rect, bGPDframe *gpf, bGPDstroke *gps)
805
897
bGPDspoint *pt1, *pt2;
806
int x0=0, y0=0, x1=0, y1=0;
898
int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
810
901
if (gps->totpoints == 0) {
814
905
BLI_freelinkN(&gpf->strokes, gps);
816
907
else if (gps->totpoints == 1) {
817
/* get coordinates */
818
if (gps->flag & GP_STROKE_3DSPACE) {
819
project_int(p->ar, &gps->points->x, xyval);
823
else if (gps->flag & GP_STROKE_2DSPACE) {
824
UI_view2d_view_to_region(p->v2d, gps->points->x, gps->points->y, &x0, &y0);
827
else if (gps->flag & GP_STROKE_2DIMAGE) {
828
int offsx, offsy, sizex, sizey;
830
/* get stored settings */
831
sizex= p->im2d_settings.sizex;
832
sizey= p->im2d_settings.sizey;
833
offsx= p->im2d_settings.offsx;
834
offsy= p->im2d_settings.offsy;
836
/* calculate new points */
837
x0= (int)((gps->points->x * sizex) + offsx);
838
y0= (int)((gps->points->y * sizey) + offsy);
842
if (p->subrect == NULL) { /* normal 3D view */
843
x0= (int)(gps->points->x / 100 * p->ar->winx);
844
y0= (int)(gps->points->y / 100 * p->ar->winy);
846
else { /* camera view, use subrect */
847
x0= (int)((gps->points->x / 100) * (p->subrect->xmax - p->subrect->xmin)) + p->subrect->xmin;
848
y0= (int)((gps->points->y / 100) * (p->subrect->ymax - p->subrect->ymin)) + p->subrect->ymin;
908
gp_point_to_xy(p->ar, p->v2d, p->subrect, gps, gps->points, &x0, &y0);
852
910
/* do boundbox check first */
853
if (BLI_in_rcti(rect, x0, y0)) {
911
if ((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(rect, x0, y0)) {
854
912
/* only check if point is inside */
855
if ( ((x0-mval[0])*(x0-mval[0]) + (y0-mval[1])*(y0-mval[1])) <= rad*rad ) {
913
if (((x0 - mval[0]) * (x0 - mval[0]) + (y0 - mval[1]) * (y0 - mval[1])) <= rad * rad) {
856
914
/* free stroke */
857
915
MEM_freeN(gps->points);
858
916
BLI_freelinkN(&gpf->strokes, gps);
863
921
/* loop over the points in the stroke, checking for intersections
864
* - an intersection will require the stroke to be split
922
* - an intersection will require the stroke to be split
866
for (i=0; (i+1) < gps->totpoints; i++) {
924
for (i = 0; (i + 1) < gps->totpoints; i++) {
867
925
/* get points to work with */
868
pt1= gps->points + i;
869
pt2= gps->points + i + 1;
926
pt1 = gps->points + i;
927
pt2 = gps->points + i + 1;
871
/* get coordinates */
872
if (gps->flag & GP_STROKE_3DSPACE) {
873
project_int(p->ar, &pt1->x, xyval);
877
project_int(p->ar, &pt2->x, xyval);
881
else if (gps->flag & GP_STROKE_2DSPACE) {
882
UI_view2d_view_to_region(p->v2d, pt1->x, pt1->y, &x0, &y0);
884
UI_view2d_view_to_region(p->v2d, pt2->x, pt2->y, &x1, &y1);
887
else if (gps->flag & GP_STROKE_2DIMAGE) {
888
int offsx, offsy, sizex, sizey;
890
/* get stored settings */
891
sizex= p->im2d_settings.sizex;
892
sizey= p->im2d_settings.sizey;
893
offsx= p->im2d_settings.offsx;
894
offsy= p->im2d_settings.offsy;
896
/* calculate new points */
897
x0= (int)((pt1->x * sizex) + offsx);
898
y0= (int)((pt1->y * sizey) + offsy);
900
x1= (int)((pt2->x * sizex) + offsx);
901
y1= (int)((pt2->y * sizey) + offsy);
905
if (p->subrect == NULL) { /* normal 3D view */
906
x0= (int)(pt1->x / 100 * p->ar->winx);
907
y0= (int)(pt1->y / 100 * p->ar->winy);
908
x1= (int)(pt2->x / 100 * p->ar->winx);
909
y1= (int)(pt2->y / 100 * p->ar->winy);
911
else { /* camera view, use subrect */
912
x0= (int)((pt1->x / 100) * (p->subrect->xmax - p->subrect->xmin)) + p->subrect->xmin;
913
y0= (int)((pt1->y / 100) * (p->subrect->ymax - p->subrect->ymin)) + p->subrect->ymin;
914
x1= (int)((pt2->x / 100) * (p->subrect->xmax - p->subrect->xmin)) + p->subrect->xmin;
915
y1= (int)((pt2->y / 100) * (p->subrect->ymax - p->subrect->ymin)) + p->subrect->ymin;
929
gp_point_to_xy(p->ar, p->v2d, p->subrect, gps, pt1, &x0, &y0);
930
gp_point_to_xy(p->ar, p->v2d, p->subrect, gps, pt2, &x1, &y1);
919
932
/* check that point segment of the boundbox of the eraser stroke */
920
if (BLI_in_rcti(rect, x0, y0) || BLI_in_rcti(rect, x1, y1)) {
933
if (((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(rect, x0, y0)) ||
934
((!ELEM(V2D_IS_CLIPPED, x1, y1)) && BLI_rcti_isect_pt(rect, x1, y1))) {
921
935
/* check if point segment of stroke had anything to do with
922
936
* eraser region (either within stroke painted, or on its lines)
923
* - this assumes that linewidth is irrelevant
937
* - this assumes that linewidth is irrelevant
925
939
if (gp_stroke_eraser_strokeinside(mval, mvalo, rad, x0, y0, x1, y1)) {
926
940
/* if function returns true, break this loop (as no more point to check) */
956
970
/* Sketching Operator */
958
972
/* clear the session buffers (call this before AND after a paint operation) */
959
static void gp_session_validatebuffer (tGPsdata *p)
973
static void gp_session_validatebuffer(tGPsdata *p)
961
bGPdata *gpd= p->gpd;
975
bGPdata *gpd = p->gpd;
963
977
/* clear memory of buffer (or allocate it if starting a new session) */
964
978
if (gpd->sbuffer) {
965
//printf("\t\tGP - reset sbuffer\n");
966
memset(gpd->sbuffer, 0, sizeof(tGPspoint)*GP_STROKE_BUFFER_MAX);
979
/* printf("\t\tGP - reset sbuffer\n"); */
980
memset(gpd->sbuffer, 0, sizeof(tGPspoint) * GP_STROKE_BUFFER_MAX);
969
//printf("\t\tGP - allocate sbuffer\n");
970
gpd->sbuffer= MEM_callocN(sizeof(tGPspoint)*GP_STROKE_BUFFER_MAX, "gp_session_strokebuffer");
983
/* printf("\t\tGP - allocate sbuffer\n"); */
984
gpd->sbuffer = MEM_callocN(sizeof(tGPspoint) * GP_STROKE_BUFFER_MAX, "gp_session_strokebuffer");
973
987
/* reset indices */
974
988
gpd->sbuffer_size = 0;
976
990
/* reset flags */
977
gpd->sbuffer_sflag= 0;
991
gpd->sbuffer_sflag = 0;
980
997
/* (re)init new painting data */
981
static int gp_session_initdata (bContext *C, tGPsdata *p)
998
static int gp_session_initdata(bContext *C, tGPsdata *p)
983
1000
bGPdata **gpd_ptr = NULL;
984
ScrArea *curarea= CTX_wm_area(C);
985
ARegion *ar= CTX_wm_region(C);
1001
ScrArea *curarea = CTX_wm_area(C);
1002
ARegion *ar = CTX_wm_region(C);
987
1004
/* make sure the active view (at the starting time) is a 3d-view */
988
1005
if (curarea == NULL) {
989
p->status= GP_STATUS_ERROR;
1006
p->status = GP_STATUS_ERROR;
990
1007
if (G.debug & G_DEBUG)
991
1008
printf("Error: No active view for painting\n");
995
1012
/* pass on current scene and window */
996
p->scene= CTX_data_scene(C);
997
p->win= CTX_wm_window(C);
1013
p->scene = CTX_data_scene(C);
1014
p->win = CTX_wm_window(C);
999
1016
unit_m4(p->imat);
1002
1019
/* supported views first */
1003
1020
case SPACE_VIEW3D:
1005
// View3D *v3d= curarea->spacedata.first;
1006
// RegionView3D *rv3d= ar->regiondata;
1022
/* View3D *v3d = curarea->spacedata.first; */
1023
/* RegionView3D *rv3d = ar->regiondata; */
1008
1025
/* set current area
1009
1026
* - must verify that region data is 3D-view (and not something else)
1014
1031
if (ar->regiondata == NULL) {
1015
p->status= GP_STATUS_ERROR;
1032
p->status = GP_STATUS_ERROR;
1016
1033
if (G.debug & G_DEBUG)
1017
1034
printf("Error: 3D-View active region doesn't have any region data, so cannot be drawable\n");
1021
#if 0 // XXX will this sort of antiquated stuff be restored?
1022
/* check that gpencil data is allowed to be drawn */
1023
if ((v3d->flag2 & V3D_DISPGP)==0) {
1024
p->status= GP_STATUS_ERROR;
1025
if (G.debug & G_DEBUG)
1026
printf("Error: In active view, Grease Pencil not shown\n");
1033
1040
case SPACE_NODE:
1035
//SpaceNode *snode= curarea->spacedata.first;
1042
/* SpaceNode *snode = curarea->spacedata.first; */
1037
1044
/* set current area */
1042
#if 0 // XXX will this sort of antiquated stuff be restored?
1043
/* check that gpencil data is allowed to be drawn */
1044
if ((snode->flag & SNODE_DISPGP)==0) {
1045
p->status= GP_STATUS_ERROR;
1046
if (G.debug & G_DEBUG)
1047
printf("Error: In active view, Grease Pencil not shown\n");
1053
#if 0 // XXX these other spaces will come over time...
1054
1050
case SPACE_SEQ:
1056
SpaceSeq *sseq= curarea->spacedata.first;
1052
SpaceSeq *sseq = curarea->spacedata.first;
1058
1054
/* set current area */
1063
1059
/* check that gpencil data is allowed to be drawn */
1064
1060
if (sseq->mainb == SEQ_DRAW_SEQUENCE) {
1065
p->status= GP_STATUS_ERROR;
1061
p->status = GP_STATUS_ERROR;
1066
1062
if (G.debug & G_DEBUG)
1067
1063
printf("Error: In active view (sequencer), active mode doesn't support Grease Pencil\n");
1070
if ((sseq->flag & SEQ_DRAW_GPENCIL)==0) {
1071
p->status= GP_STATUS_ERROR;
1072
if (G.debug & G_DEBUG)
1073
printf("Error: In active view, Grease Pencil not shown\n");
1079
1068
case SPACE_IMAGE:
1081
//SpaceImage *sima= curarea->spacedata.first;
1070
/* SpaceImage *sima = curarea->spacedata.first; */
1083
1072
/* set the current area */
1087
//p->ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1089
#if 0 // XXX disabled for now
1090
/* check that gpencil data is allowed to be drawn */
1091
if ((sima->flag & SI_DISPGP)==0) {
1092
p->status= GP_STATUS_ERROR;
1093
if (G.debug & G_DEBUG)
1094
printf("Error: In active view, Grease Pencil not shown\n");
1100
1078
case SPACE_CLIP:
1102
SpaceClip *sc= curarea->spacedata.first;
1080
SpaceClip *sc = curarea->spacedata.first;
1104
1082
/* set the current area */
1108
//p->ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
1110
1087
invert_m4_m4(p->imat, sc->unistabmat);
1112
1089
/* custom color for new layer */
1113
p->custom_color[0]= 1.0f;
1114
p->custom_color[1]= 0.0f;
1115
p->custom_color[2]= 0.5f;
1116
p->custom_color[3]= 0.9f;
1090
p->custom_color[0] = 1.0f;
1091
p->custom_color[1] = 0.0f;
1092
p->custom_color[2] = 0.5f;
1093
p->custom_color[3] = 0.9f;
1095
if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
1096
MovieClip *clip = ED_space_clip_get_clip(sc);
1097
int framenr = ED_space_clip_get_clip_frame_number(sc);
1098
MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking);
1099
MovieTrackingMarker *marker = BKE_tracking_marker_get_exact(track, framenr);
1101
p->imat[3][0] -= marker->pos[0];
1102
p->imat[3][1] -= marker->pos[1];
1120
1107
/* unsupported views */
1123
p->status= GP_STATUS_ERROR;
1110
p->status = GP_STATUS_ERROR;
1124
1111
if (G.debug & G_DEBUG)
1125
1112
printf("Error: Active view not appropriate for Grease Pencil drawing\n");
1131
1118
/* get gp-data */
1132
gpd_ptr= gpencil_data_get_pointers(C, &p->ownerPtr);
1119
gpd_ptr = gpencil_data_get_pointers(C, &p->ownerPtr);
1133
1120
if (gpd_ptr == NULL) {
1134
p->status= GP_STATUS_ERROR;
1121
p->status = GP_STATUS_ERROR;
1135
1122
if (G.debug & G_DEBUG)
1136
1123
printf("Error: Current context doesn't allow for any Grease Pencil data\n");
1140
1127
/* if no existing GPencil block exists, add one */
1141
1128
if (*gpd_ptr == NULL)
1142
*gpd_ptr= gpencil_data_addnew("GPencil");
1129
*gpd_ptr = gpencil_data_addnew("GPencil");
1146
if (ED_gpencil_session_active()==0) {
1133
if (ED_gpencil_session_active() == 0) {
1147
1134
/* initialize undo stack,
1148
* also, existing undo stack would make buffer drawn */
1135
* also, existing undo stack would make buffer drawn
1149
1137
gpencil_undo_init(p->gpd);
1152
1140
/* clear out buffer (stored in gp-data), in case something contaminated it */
1153
1141
gp_session_validatebuffer(p);
1156
/* set 'default' im2d_settings just in case something that uses this doesn't set it */
1157
p->im2d_settings.sizex= 1;
1158
p->im2d_settings.sizey= 1;
1164
1146
/* init new painting session */
1165
static tGPsdata *gp_session_initpaint (bContext *C)
1147
static tGPsdata *gp_session_initpaint(bContext *C)
1167
1149
tGPsdata *p = NULL;
1169
1151
/* create new context data */
1170
p= MEM_callocN(sizeof(tGPsdata), "GPencil Drawing Data");
1152
p = MEM_callocN(sizeof(tGPsdata), "GPencil Drawing Data");
1172
1154
gp_session_initdata(C, p);
1187
1169
/* free stroke buffer */
1188
1170
if (gpd->sbuffer) {
1189
//printf("\t\tGP - free sbuffer\n");
1171
/* printf("\t\tGP - free sbuffer\n"); */
1190
1172
MEM_freeN(gpd->sbuffer);
1173
gpd->sbuffer = NULL;
1194
1176
/* clear flags */
1195
gpd->sbuffer_size= 0;
1196
gpd->sbuffer_sflag= 0;
1177
gpd->sbuffer_size = 0;
1178
gpd->sbuffer_sflag = 0;
1199
1182
/* init new stroke */
1200
static void gp_paint_initstroke (tGPsdata *p, short paintmode)
1183
static void gp_paint_initstroke(tGPsdata *p, short paintmode)
1202
1185
/* get active layer (or add a new one if non-existent) */
1203
p->gpl= gpencil_layer_getactive(p->gpd);
1186
p->gpl = gpencil_layer_getactive(p->gpd);
1204
1187
if (p->gpl == NULL) {
1205
p->gpl= gpencil_layer_addnew(p->gpd);
1188
p->gpl = gpencil_layer_addnew(p->gpd, "GP_Layer", 1);
1207
1190
if (p->custom_color[3])
1208
1191
copy_v3_v3(p->gpl->color, p->custom_color);
1210
1193
if (p->gpl->flag & GP_LAYER_LOCKED) {
1211
p->status= GP_STATUS_ERROR;
1194
p->status = GP_STATUS_ERROR;
1212
1195
if (G.debug & G_DEBUG)
1213
1196
printf("Error: Cannot paint on locked layer\n");
1217
1200
/* get active frame (add a new one if not matching frame) */
1218
p->gpf= gpencil_layer_getframe(p->gpl, p->scene->r.cfra, 1);
1201
p->gpf = gpencil_layer_getframe(p->gpl, p->scene->r.cfra, 1);
1219
1202
if (p->gpf == NULL) {
1220
p->status= GP_STATUS_ERROR;
1203
p->status = GP_STATUS_ERROR;
1221
1204
if (G.debug & G_DEBUG)
1222
1205
printf("Error: No frame created (gpencil_paint_init)\n");
1263
1246
p->gpd->sbuffer_sflag |= GP_STROKE_3DSPACE;
1267
1250
case SPACE_NODE:
1269
1252
p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1272
#if 0 // XXX other spacetypes to be restored in due course
1273
1256
case SPACE_SEQ:
1275
SpaceSeq *sseq= (SpaceSeq *)p->sa->spacedata.first;
1277
float zoom, zoomx, zoomy;
1279
/* set draw 2d-stroke flag */
1280
p->gpd->sbuffer_sflag |= GP_STROKE_2DIMAGE;
1282
/* calculate zoom factor */
1283
zoom= (float)(SEQ_ZOOM_FAC(sseq->zoom));
1284
if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1285
zoomx = zoom * (p->scene->r.xasp / p->scene->r.yasp);
1289
zoomx = zoomy = zoom;
1291
/* calculate rect size to use to calculate the size of the drawing area
1292
* - We use the size of the output image not the size of the ibuf being shown
1293
* as it is too messy getting the ibuf (and could be too slow). This should be
1294
* a reasonable for most cases anyway.
1296
rectx= (p->scene->r.size * p->scene->r.xsch) / 100;
1297
recty= (p->scene->r.size * p->scene->r.ysch) / 100;
1299
/* set offset and scale values for opertations to use */
1300
p->im2d_settings.sizex= (int)(zoomx * rectx);
1301
p->im2d_settings.sizey= (int)(zoomy * recty);
1302
p->im2d_settings.offsx= (int)((p->sa->winx-p->im2d_settings.sizex)/2 + sseq->xof);
1303
p->im2d_settings.offsy= (int)((p->sa->winy-p->im2d_settings.sizey)/2 + sseq->yof);
1258
p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1307
1262
case SPACE_IMAGE:
1309
SpaceImage *sima= (SpaceImage *)p->sa->spacedata.first;
1264
SpaceImage *sima = (SpaceImage *)p->sa->spacedata.first;
1311
1266
/* only set these flags if the image editor doesn't have an image active,
1312
1267
* otherwise user will be confused by strokes not appearing after they're drawn
1317
1272
/* make strokes be drawn in screen space */
1318
1273
p->gpd->sbuffer_sflag &= ~GP_STROKE_2DSPACE;
1319
1274
p->gpd->flag &= ~GP_DATA_VIEWALIGN;
1322
1277
p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1326
1281
case SPACE_CLIP:
1328
1283
p->gpd->sbuffer_sflag |= GP_STROKE_2DSPACE;
1335
1290
/* finish off a stroke (clears buffer, but doesn't finish the paint operation) */
1336
static void gp_paint_strokeend (tGPsdata *p)
1291
static void gp_paint_strokeend(tGPsdata *p)
1338
1293
/* for surface sketching, need to set the right OpenGL context stuff so that
1339
1294
* the conversions will project the values correctly...
1341
1296
if (gpencil_project_check(p)) {
1342
View3D *v3d= p->sa->spacedata.first;
1297
View3D *v3d = p->sa->spacedata.first;
1344
1299
/* need to restore the original projection settings before packing up */
1345
1300
view3d_region_operator_needs_opengl(p->win, p->ar);
1346
ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1:0);
1301
ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1 : 0);
1349
1304
/* check if doing eraser or not */
1380
1336
/* ------------------------------- */
1382
static void gpencil_draw_exit (bContext *C, wmOperator *op)
1384
tGPsdata *p= op->customdata;
1338
/* Helper callback for drawing the cursor itself */
1339
static void gpencil_draw_eraser(bContext *UNUSED(C), int x, int y, void *p_ptr)
1341
tGPsdata *p = (tGPsdata *)p_ptr;
1343
if (p->paintmode == GP_PAINTMODE_ERASER) {
1346
glTranslatef((float)x, (float)y, 0.0f);
1348
glColor4ub(255, 255, 255, 128);
1350
glEnable(GL_LINE_SMOOTH);
1353
glutil_draw_lined_arc(0.0, M_PI * 2.0, p->radius, 40);
1355
glDisable(GL_BLEND);
1356
glDisable(GL_LINE_SMOOTH);
1362
/* Turn brush cursor in 3D view on/off */
1363
static void gpencil_draw_toggle_eraser_cursor(bContext *C, tGPsdata *p, short enable)
1365
if (p->erasercursor && !enable) {
1367
WM_paint_cursor_end(CTX_wm_manager(C), p->erasercursor);
1368
p->erasercursor = NULL;
1372
p->erasercursor = WM_paint_cursor_activate(CTX_wm_manager(C),
1374
gpencil_draw_eraser, p);
1378
/* ------------------------------- */
1381
static void gpencil_draw_exit(bContext *C, wmOperator *op)
1383
tGPsdata *p = op->customdata;
1386
1385
/* clear undo stack */
1387
1386
gpencil_undo_finish();
1441
1444
/* radius for eraser circle is defined in userprefs now */
1442
p->radius= U.gp_eraser;
1445
p->radius = U.gp_eraser;
1444
1447
/* everything is now setup ok */
1448
1452
/* ------------------------------- */
1450
1454
/* update UI indicators of status, including cursor and header prints */
1451
static void gpencil_draw_status_indicators (tGPsdata *p)
1455
static void gpencil_draw_status_indicators(tGPsdata *p)
1453
1457
/* header prints */
1454
1458
switch (p->status) {
1455
1459
case GP_STATUS_PAINTING:
1456
1460
/* only print this for paint-sessions, otherwise it gets annoying */
1457
1461
if (GPENCIL_SKETCH_SESSIONS_ON(p->scene))
1458
ED_area_headerprint(p->sa, "Grease Pencil: Drawing/erasing stroke... Release to end stroke");
1462
ED_area_headerprint(p->sa, IFACE_("Grease Pencil: Drawing/erasing stroke... Release to end stroke"));
1461
1465
case GP_STATUS_IDLING:
1462
1466
/* print status info */
1463
1467
switch (p->paintmode) {
1464
1468
case GP_PAINTMODE_ERASER:
1465
ED_area_headerprint(p->sa, "Grease Pencil Erase Session: Hold and drag LMB or RMB to erase | ESC/Enter to end");
1469
ED_area_headerprint(p->sa, IFACE_("Grease Pencil Erase Session: Hold and drag LMB or RMB to erase |"
1470
" ESC/Enter to end"));
1467
1472
case GP_PAINTMODE_DRAW_STRAIGHT:
1468
ED_area_headerprint(p->sa, "Grease Pencil Line Session: Hold and drag LMB to draw | ESC/Enter to end");
1473
ED_area_headerprint(p->sa, IFACE_("Grease Pencil Line Session: Hold and drag LMB to draw | "
1474
"ESC/Enter to end"));
1470
1476
case GP_PAINTMODE_DRAW:
1471
ED_area_headerprint(p->sa, "Grease Pencil Freehand Session: Hold and drag LMB to draw | ESC/Enter to end");
1477
ED_area_headerprint(p->sa, IFACE_("Grease Pencil Freehand Session: Hold and drag LMB to draw | "
1478
"ESC/Enter to end"));
1474
1481
default: /* unhandled future cases */
1475
ED_area_headerprint(p->sa, "Grease Pencil Session: ESC/Enter to end");
1482
ED_area_headerprint(p->sa, IFACE_("Grease Pencil Session: ESC/Enter to end"));
1496
1503
gp_stroke_doeraser(p);
1498
1505
/* store used values */
1499
p->mvalo[0]= p->mval[0];
1500
p->mvalo[1]= p->mval[1];
1501
p->opressure= p->pressure;
1506
p->mvalo[0] = p->mval[0];
1507
p->mvalo[1] = p->mval[1];
1508
p->opressure = p->pressure;
1503
1510
/* only add current point to buffer if mouse moved (even though we got an event, it might be just noise) */
1504
1511
else if (gp_stroke_filtermval(p, p->mval, p->mvalo)) {
1505
1512
/* try to add point */
1506
short ok= gp_stroke_addpoint(p, p->mval, p->pressure);
1513
short ok = gp_stroke_addpoint(p, p->mval, p->pressure, p->curtime);
1508
1515
/* handle errors while adding point */
1509
1516
if ((ok == GP_STROKEADD_FULL) || (ok == GP_STROKEADD_OVERFLOW)) {
1510
1517
/* finish off old stroke */
1511
1518
gp_paint_strokeend(p);
1519
/* And start a new one!!! Else, projection errors! */
1520
gp_paint_initstroke(p, p->paintmode);
1513
1522
/* start a new stroke, starting from previous point */
1514
gp_stroke_addpoint(p, p->mvalo, p->opressure);
1515
gp_stroke_addpoint(p, p->mval, p->pressure);
1523
/* XXX Must manually reset inittime... */
1524
/* XXX We only need to reuse previous point if overflow! */
1525
if (ok == GP_STROKEADD_OVERFLOW) {
1526
p->inittime = p->ocurtime;
1527
gp_stroke_addpoint(p, p->mvalo, p->opressure, p->ocurtime);
1530
p->inittime = p->curtime;
1532
gp_stroke_addpoint(p, p->mval, p->pressure, p->curtime);
1517
1534
else if (ok == GP_STROKEADD_INVALID) {
1518
1535
/* the painting operation cannot continue... */
1527
1544
/* store used values */
1528
p->mvalo[0]= p->mval[0];
1529
p->mvalo[1]= p->mval[1];
1530
p->opressure= p->pressure;
1545
p->mvalo[0] = p->mval[0];
1546
p->mvalo[1] = p->mval[1];
1547
p->opressure = p->pressure;
1548
p->ocurtime = p->curtime;
1534
1552
/* handle draw event */
1535
static void gpencil_draw_apply_event (wmOperator *op, wmEvent *event)
1553
static void gpencil_draw_apply_event(wmOperator *op, wmEvent *event)
1537
tGPsdata *p= op->customdata;
1555
tGPsdata *p = op->customdata;
1538
1556
PointerRNA itemptr;
1539
1557
float mousef[2];
1542
/* convert from window-space to area-space mouse coordintes */
1543
// NOTE: float to ints conversions, +1 factor is probably used to ensure a bit more accurate rounding...
1544
p->mval[0]= event->mval[0] + 1;
1545
p->mval[1]= event->mval[1] + 1;
1560
/* convert from window-space to area-space mouse coordinates
1561
* NOTE: float to ints conversions, +1 factor is probably used to ensure a bit more accurate rounding...
1563
p->mval[0] = event->mval[0] + 1;
1564
p->mval[1] = event->mval[1] + 1;
1565
p->curtime = PIL_check_seconds_timer();
1547
1567
/* handle pressure sensitivity (which is supplied by tablets) */
1548
if (event->custom == EVT_DATA_TABLET) {
1549
wmTabletData *wmtab= event->customdata;
1551
tablet= (wmtab->Active != EVT_TABLET_NONE);
1552
p->pressure= wmtab->Pressure;
1554
//if (wmtab->Active == EVT_TABLET_ERASER)
1555
// TODO... this should get caught by the keymaps which call drawing in the first place
1568
if (event->tablet_data) {
1569
wmTabletData *wmtab = event->tablet_data;
1571
tablet = (wmtab->Active != EVT_TABLET_NONE);
1572
p->pressure = wmtab->Pressure;
1574
/* if (wmtab->Active == EVT_TABLET_ERASER) */
1575
/* TODO... this should get caught by the keymaps which call drawing in the first place */
1560
1580
/* fill in stroke data (not actually used directly by gpencil_draw_apply) */
1561
1581
RNA_collection_add(op->ptr, "stroke", &itemptr);
1563
mousef[0]= p->mval[0];
1564
mousef[1]= p->mval[1];
1583
mousef[0] = p->mval[0];
1584
mousef[1] = p->mval[1];
1565
1585
RNA_float_set_array(&itemptr, "mouse", mousef);
1566
1586
RNA_float_set(&itemptr, "pressure", p->pressure);
1567
1587
RNA_boolean_set(&itemptr, "is_start", (p->flags & GP_PAINTFLAG_FIRSTRUN));
1591
1614
/* ------------------------------- */
1593
1616
/* operator 'redo' (i.e. after changing some properties, but also for repeat last) */
1594
static int gpencil_draw_exec (bContext *C, wmOperator *op)
1617
static int gpencil_draw_exec(bContext *C, wmOperator *op)
1596
1619
tGPsdata *p = NULL;
1598
//printf("GPencil - Starting Re-Drawing\n");
1621
/* printf("GPencil - Starting Re-Drawing\n"); */
1600
1623
/* try to initialize context data needed while drawing */
1601
1624
if (!gpencil_draw_init(C, op)) {
1602
1625
if (op->customdata) MEM_freeN(op->customdata);
1603
//printf("\tGP - no valid data\n");
1626
/* printf("\tGP - no valid data\n"); */
1604
1627
return OPERATOR_CANCELLED;
1609
//printf("\tGP - Start redrawing stroke\n");
1632
/* printf("\tGP - Start redrawing stroke\n"); */
1611
1634
/* loop over the stroke RNA elements recorded (i.e. progress of mouse movement),
1612
1635
* setting the relevant values in context at each step, then applying
1614
RNA_BEGIN(op->ptr, itemptr, "stroke") {
1637
RNA_BEGIN(op->ptr, itemptr, "stroke")
1615
1639
float mousef[2];
1617
//printf("\t\tGP - stroke elem\n");
1641
/* printf("\t\tGP - stroke elem\n"); */
1619
1643
/* get relevant data for this point from stroke */
1620
1644
RNA_float_get_array(&itemptr, "mouse", mousef);
1621
1645
p->mval[0] = (int)mousef[0];
1622
1646
p->mval[1] = (int)mousef[1];
1623
p->pressure= RNA_float_get(&itemptr, "pressure");
1647
p->pressure = RNA_float_get(&itemptr, "pressure");
1648
p->curtime = (double)RNA_float_get(&itemptr, "time") + p->inittime;
1625
1650
if (RNA_boolean_get(&itemptr, "is_start")) {
1626
1651
/* if first-run flag isn't set already (i.e. not true first stroke),
1627
1652
* then we must terminate the previous one first before continuing
1629
1654
if ((p->flags & GP_PAINTFLAG_FIRSTRUN) == 0) {
1630
// TODO: both of these ops can set error-status, but we probably don't need to worry
1655
/* TODO: both of these ops can set error-status, but we probably don't need to worry */
1631
1656
gp_paint_strokeend(p);
1632
1657
gp_paint_initstroke(p, p->paintmode);
1679
1705
return OPERATOR_CANCELLED;
1684
// TODO: set any additional settings that we can take from the events?
1685
// TODO? if tablet is erasing, force eraser to be on?
1687
// TODO: move cursor setting stuff to stroke-start so that paintmode can be changed midway...
1710
/* TODO: set any additional settings that we can take from the events?
1711
* TODO? if tablet is erasing, force eraser to be on? */
1713
/* TODO: move cursor setting stuff to stroke-start so that paintmode can be changed midway... */
1689
1715
/* if eraser is on, draw radial aid */
1690
1716
if (p->paintmode == GP_PAINTMODE_ERASER) {
1691
// TODO: this involves mucking around with radial control, so we leave this for now..
1717
gpencil_draw_toggle_eraser_cursor(C, p, TRUE);
1694
1720
/* set cursor */
1695
1721
if (p->paintmode == GP_PAINTMODE_ERASER)
1696
WM_cursor_modal(win, BC_CROSSCURSOR); // XXX need a better cursor
1722
WM_cursor_modal(win, BC_CROSSCURSOR); /* XXX need a better cursor */
1698
1724
WM_cursor_modal(win, BC_PAINTBRUSHCURSOR);
1701
1727
* painting should start immediately. Otherwise, this was called from a toolbar, in which
1702
1728
* case we should wait for the mouse to be clicked.
1730
if (event->val == KM_PRESS) {
1705
1731
/* hotkey invoked - start drawing */
1706
//printf("\tGP - set first spot\n");
1707
p->status= GP_STATUS_PAINTING;
1732
/* printf("\tGP - set first spot\n"); */
1733
p->status = GP_STATUS_PAINTING;
1709
1735
/* handle the initial drawing - i.e. for just doing a simple dot */
1710
1736
gpencil_draw_apply_event(op, event);
1713
1739
/* toolbar invoked - don't start drawing yet... */
1714
//printf("\tGP - hotkey invoked... waiting for click-drag\n");
1740
/* printf("\tGP - hotkey invoked... waiting for click-drag\n"); */
1717
WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL, NULL);
1743
WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
1718
1744
/* add a modal handler for this operator, so that we can then draw continuous strokes */
1719
1745
WM_event_add_modal_handler(C, op);
1720
1746
return OPERATOR_RUNNING_MODAL;
1723
1749
/* gpencil modal operator stores area, which can be removed while using it (like fullscreen) */
1724
static int gpencil_area_exists(bContext *C, ScrArea *satest)
1750
static int gpencil_area_exists(bContext *C, ScrArea *sa_test)
1726
bScreen *sc= CTX_wm_screen(C);
1729
for (sa= sc->areabase.first; sa; sa= sa->next) {
1752
bScreen *sc = CTX_wm_screen(C);
1753
return (BLI_findindex(&sc->areabase, sa_test) != -1);
1737
1756
static tGPsdata *gpencil_stroke_begin(bContext *C, wmOperator *op)
1739
tGPsdata *p= op->customdata;
1758
tGPsdata *p = op->customdata;
1741
1760
/* we must check that we're still within the area that we're set up to work from
1742
1761
* otherwise we could crash (see bug #20586)
1744
1763
if (CTX_wm_area(C) != p->sa) {
1745
1764
printf("\t\t\tGP - wrong area execution abort!\n");
1746
p->status= GP_STATUS_ERROR;
1765
p->status = GP_STATUS_ERROR;
1749
//printf("\t\tGP - start stroke\n");
1768
/* printf("\t\tGP - start stroke\n"); */
1751
1770
/* we may need to set up paint env again if we're resuming */
1752
// XXX: watch it with the paintmode! in future, it'd be nice to allow changing paint-mode when in sketching-sessions
1753
// XXX: with tablet events, we may event want to check for eraser here, for nicer tablet support
1771
/* XXX: watch it with the paintmode! in future,
1772
* it'd be nice to allow changing paint-mode when in sketching-sessions */
1773
/* XXX: with tablet events, we may event want to check for eraser here, for nicer tablet support */
1755
1775
if (gp_session_initdata(C, p))
1756
1776
gp_paint_initstroke(p, p->paintmode);
1758
1778
if (p->status != GP_STATUS_ERROR)
1759
p->status= GP_STATUS_PAINTING;
1779
p->status = GP_STATUS_PAINTING;
1761
1781
return op->customdata;
1764
1784
static void gpencil_stroke_end(wmOperator *op)
1766
tGPsdata *p= op->customdata;
1786
tGPsdata *p = op->customdata;
1768
1788
gp_paint_cleanup(p);
1772
1792
gp_session_cleanup(p);
1774
p->status= GP_STATUS_IDLING;
1794
p->status = GP_STATUS_IDLING;
1781
1801
/* events handling during interactive drawing part of operator */
1782
static int gpencil_draw_modal (bContext *C, wmOperator *op, wmEvent *event)
1802
static int gpencil_draw_modal(bContext *C, wmOperator *op, wmEvent *event)
1784
tGPsdata *p= op->customdata;
1785
int estate = OPERATOR_PASS_THROUGH; /* default exit state - not handled, so let others have a share of the pie */
1804
tGPsdata *p = op->customdata;
1805
int estate = OPERATOR_PASS_THROUGH; /* default exit state - pass through to support MMB view nav, etc. */
1787
// if (event->type == NDOF_MOTION)
1788
// return OPERATOR_PASS_THROUGH;
1789
// -------------------------------
1790
// [mce] Not quite what I was looking
1791
// for, but a good start! GP continues to
1792
// draw on the screen while the 3D mouse
1793
// moves the viewpoint. Problem is that
1794
// the stroke is converted to 3D only after
1795
// it is finished. This approach should work
1796
// better in tools that immediately apply
1807
/* if (event->type == NDOF_MOTION)
1808
* return OPERATOR_PASS_THROUGH;
1809
* -------------------------------
1810
* [mce] Not quite what I was looking
1811
* for, but a good start! GP continues to
1812
* draw on the screen while the 3D mouse
1813
* moves the viewpoint. Problem is that
1814
* the stroke is converted to 3D only after
1815
* it is finished. This approach should work
1816
* better in tools that immediately apply
1820
/* we don't pass on key events, GP is used with key-modifiers - prevents Dkey to insert drivers */
1821
if (ISKEYBOARD(event->type)) {
1822
if (ELEM4(event->type, LEFTARROWKEY, DOWNARROWKEY, RIGHTARROWKEY, UPARROWKEY)) {
1823
/* allow some keys - for frame changing: [#33412] */
1826
estate = OPERATOR_RUNNING_MODAL;
1799
1830
//printf("\tGP - handle modal event...\n");
1801
/* exit painting mode (and/or end current stroke) */
1832
/* exit painting mode (and/or end current stroke)
1833
* NOTE: cannot do RIGHTMOUSE (as is standard for cancelling) as that would break polyline [#32647]
1802
1835
if (ELEM4(event->type, RETKEY, PADENTER, ESCKEY, SPACEKEY)) {
1803
1836
/* exit() ends the current stroke before cleaning up */
1804
//printf("\t\tGP - end of paint op + end of stroke\n");
1805
p->status= GP_STATUS_DONE;
1837
/* printf("\t\tGP - end of paint op + end of stroke\n"); */
1838
p->status = GP_STATUS_DONE;
1806
1839
estate = OPERATOR_FINISHED;
1809
/* toggle painting mode upon mouse-button movement */
1842
/* toggle painting mode upon mouse-button movement
1843
* - LEFTMOUSE = standard drawing (all) / straight line drawing (all) / polyline (toolbox only)
1844
* - RIGHTMOUSE = polyline (hotkey) / eraser (all)
1845
* (Disabling RIGHTMOUSE case here results in bugs like [#32647])
1810
1847
if (ELEM(event->type, LEFTMOUSE, RIGHTMOUSE)) {
1811
1848
/* if painting, end stroke */
1812
1849
if (p->status == GP_STATUS_PAINTING) {
1814
1852
/* basically, this should be mouse-button up = end stroke
1815
1853
* BUT what happens next depends on whether we 'painting sessions' is enabled
1817
1855
sketch |= GPENCIL_SKETCH_SESSIONS_ON(p->scene);
1818
1856
/* polyline drawing is also 'sketching' -- all knots should be added during one session */
1819
sketch |= p->paintmode == GP_PAINTMODE_DRAW_POLY;
1857
sketch |= (p->paintmode == GP_PAINTMODE_DRAW_POLY);
1822
1860
/* end stroke only, and then wait to resume painting soon */
1823
//printf("\t\tGP - end stroke only\n");
1861
/* printf("\t\tGP - end stroke only\n"); */
1824
1862
gpencil_stroke_end(op);
1826
1864
/* we've just entered idling state, so this event was processed (but no others yet) */
1827
1865
estate = OPERATOR_RUNNING_MODAL;
1829
1867
/* stroke could be smoothed, send notifier to refresh screen */
1830
WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL);
1868
WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
1833
//printf("\t\tGP - end of stroke + op\n");
1834
p->status= GP_STATUS_DONE;
1871
/* printf("\t\tGP - end of stroke + op\n"); */
1872
p->status = GP_STATUS_DONE;
1835
1873
estate = OPERATOR_FINISHED;
1838
1876
else if (event->val == KM_PRESS) {
1839
1877
/* not painting, so start stroke (this should be mouse-button down) */
1840
p= gpencil_stroke_begin(C, op);
1878
p = gpencil_stroke_begin(C, op);
1842
1880
if (p->status == GP_STATUS_ERROR) {
1843
1881
estate = OPERATOR_CANCELLED;
1847
1885
p->status = GP_STATUS_IDLING;
1865
1903
/* event handled, so just tag as running modal */
1866
//printf("\t\t\t\tGP - add point handled!\n");
1904
/* printf("\t\t\t\tGP - add point handled!\n"); */
1867
1905
estate = OPERATOR_RUNNING_MODAL;
1909
else if ((p->paintmode == GP_PAINTMODE_ERASER) &&
1910
ELEM4(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE, PADPLUSKEY, PADMINUS))
1912
/* just resize the brush (local version)
1913
* TODO: fix the hardcoded size jumps (set to make a visible difference) and hardcoded keys
1915
/* printf("\t\tGP - resize eraser\n"); */
1916
switch (event->type) {
1917
case WHEELUPMOUSE: /* larger */
1922
case WHEELDOWNMOUSE: /* smaller */
1932
ED_region_tag_redraw(p->ar); /* just active area for now, since doing whole screen is too slow */
1934
/* event handled, so just tag as running modal */
1935
estate = OPERATOR_RUNNING_MODAL;
1870
1937
/* there shouldn't be any other events, but just in case there are, let's swallow them
1871
* (i.e. to prevent problems with with undo)
1938
* (i.e. to prevent problems with undo)
1874
1941
/* swallow event to save ourselves trouble */
1908
1978
/* ------------------------------- */
1910
1980
static EnumPropertyItem prop_gpencil_drawmodes[] = {
1911
{GP_PAINTMODE_DRAW, "DRAW", 0, "Draw Freehand", ""},
1912
{GP_PAINTMODE_DRAW_STRAIGHT, "DRAW_STRAIGHT", 0, "Draw Straight Lines", ""},
1913
{GP_PAINTMODE_DRAW_POLY, "DRAW_POLY", 0, "Draw Poly Line", ""},
1914
{GP_PAINTMODE_ERASER, "ERASER", 0, "Eraser", ""},
1981
{GP_PAINTMODE_DRAW, "DRAW", 0, "Draw Freehand", "Draw freehand stroke(s)"},
1982
{GP_PAINTMODE_DRAW_STRAIGHT, "DRAW_STRAIGHT", 0, "Draw Straight Lines", "Draw straight line segment(s)"},
1983
{GP_PAINTMODE_DRAW_POLY, "DRAW_POLY", 0, "Draw Poly Line", "Click to place endpoints of straight line segments (connected)"},
1984
{GP_PAINTMODE_ERASER, "ERASER", 0, "Eraser", "Erase Grease Pencil strokes"},
1915
1985
{0, NULL, 0, NULL, NULL}
1918
void GPENCIL_OT_draw (wmOperatorType *ot)
1988
void GPENCIL_OT_draw(wmOperatorType *ot)
1920
1990
/* identifiers */
1921
1991
ot->name = "Grease Pencil Draw";