62
62
if (gpf == NULL) return;
65
for (gps= gpf->strokes.first; gps; gps= gpsn) {
65
for (gps = gpf->strokes.first; gps; gps = gpsn) {
68
68
/* free stroke memory arrays, then stroke itself */
69
69
if (gps->points) MEM_freeN(gps->points);
80
80
if (gpl == NULL) return;
83
for (gpf= gpl->frames.first; gpf; gpf= gpfn) {
83
for (gpf = gpl->frames.first; gpf; gpf = gpfn) {
86
86
/* free strokes and their associated memory */
87
87
free_gpencil_strokes(gpf);
88
88
BLI_freelinkN(&gpl->frames, gpf);
92
93
/* Free all of the gp-layers for a viewport (list should be &gpd->layers or so) */
93
void free_gpencil_layers (ListBase *list)
94
void free_gpencil_layers(ListBase *list)
95
96
bGPDlayer *gpl, *gpln;
97
98
/* error checking */
98
99
if (list == NULL) return;
101
for (gpl= list->first; gpl; gpl= gpln) {
102
for (gpl = list->first; gpl; gpl = gpln) {
104
105
/* free layers and their data */
105
106
free_gpencil_frames(gpl);
110
111
/* Free all of GPencil datablock's related data, but not the block itself */
111
void free_gpencil_data (bGPdata *gpd)
112
void BKE_gpencil_free(bGPdata *gpd)
113
114
/* free layers */
114
115
free_gpencil_layers(&gpd->layers);
117
118
/* -------- Container Creation ---------- */
119
120
/* add a new gp-frame to the given layer */
120
bGPDframe *gpencil_frame_addnew (bGPDlayer *gpl, int cframe)
121
bGPDframe *gpencil_frame_addnew(bGPDlayer *gpl, int cframe)
122
123
bGPDframe *gpf, *gf;
125
126
/* error checking */
126
127
if ((gpl == NULL) || (cframe <= 0))
129
130
/* allocate memory for this frame */
130
gpf= MEM_callocN(sizeof(bGPDframe), "bGPDframe");
131
gpf->framenum= cframe;
131
gpf = MEM_callocN(sizeof(bGPDframe), "bGPDframe");
132
gpf->framenum = cframe;
133
134
/* find appropriate place to add frame */
134
135
if (gpl->frames.first) {
135
for (gf= gpl->frames.first; gf; gf= gf->next) {
136
for (gf = gpl->frames.first; gf; gf = gf->next) {
136
137
/* check if frame matches one that is supposed to be added */
137
138
if (gf->framenum == cframe) {
142
143
/* if current frame has already exceeded the frame to add, add before */
143
144
if (gf->framenum > cframe) {
144
145
BLI_insertlinkbefore(&gpl->frames, gf, gpf);
174
175
/* allocate memory for frame and add to end of list */
175
gpl= MEM_callocN(sizeof(bGPDlayer), "bGPDlayer");
176
gpl = MEM_callocN(sizeof(bGPDlayer), "bGPDlayer");
177
178
/* add to datablock */
178
179
BLI_addtail(&gpd->layers, gpl);
180
181
/* set basic settings */
182
gpl->color[3] = 0.9f;
182
183
gpl->thickness = 3;
185
strcpy(gpl->info, "GP_Layer");
186
BLI_strncpy(gpl->info, name, sizeof(gpl->info));
186
187
BLI_uniquename(&gpd->layers, gpl, "GP_Layer", '.', offsetof(bGPDlayer, info), sizeof(gpl->info));
188
189
/* make this one the active one */
189
gpencil_layer_setactive(gpd, gpl);
191
gpencil_layer_setactive(gpd, gpl);
191
193
/* return layer */
195
197
/* add a new gp-datablock */
196
bGPdata *gpencil_data_addnew (const char name[])
198
bGPdata *gpencil_data_addnew(const char name[])
200
202
/* allocate memory for a new block */
201
gpd= alloc_libblock(&G.main->gpencil, ID_GD, name);
203
gpd = BKE_libblock_alloc(&G.main->gpencil, ID_GD, name);
203
205
/* initial settings */
204
gpd->flag = (GP_DATA_DISPINFO|GP_DATA_EXPAND);
206
gpd->flag = (GP_DATA_DISPINFO | GP_DATA_EXPAND);
206
208
/* for now, stick to view is also enabled by default
207
209
* since this is more useful...
226
228
/* make a copy of the source frame */
227
dst= MEM_dupallocN(src);
228
dst->prev= dst->next= NULL;
229
dst = MEM_dupallocN(src);
230
dst->prev = dst->next = NULL;
230
232
/* copy strokes */
231
dst->strokes.first = dst->strokes.last= NULL;
232
for (gps= src->strokes.first; gps; gps= gps->next) {
233
dst->strokes.first = dst->strokes.last = NULL;
234
for (gps = src->strokes.first; gps; gps = gps->next) {
233
235
/* make copy of source stroke, then adjust pointer to points too */
234
gpsd= MEM_dupallocN(gps);
235
gpsd->points= MEM_dupallocN(gps->points);
236
gpsd = MEM_dupallocN(gps);
237
gpsd->points = MEM_dupallocN(gps->points);
237
239
BLI_addtail(&dst->strokes, gpsd);
254
256
/* make a copy of source layer */
255
dst= MEM_dupallocN(src);
256
dst->prev= dst->next= NULL;
257
dst = MEM_dupallocN(src);
258
dst->prev = dst->next = NULL;
258
260
/* copy frames */
259
dst->frames.first= dst->frames.last= NULL;
260
for (gpf= src->frames.first; gpf; gpf= gpf->next) {
261
dst->frames.first = dst->frames.last = NULL;
262
for (gpf = src->frames.first; gpf; gpf = gpf->next) {
261
263
/* make a copy of source frame */
262
gpfd= gpencil_frame_duplicate(gpf);
264
gpfd = gpencil_frame_duplicate(gpf);
263
265
BLI_addtail(&dst->frames, gpfd);
265
267
/* if source frame was the current layer's 'active' frame, reassign that too */
266
268
if (gpf == dst->actframe)
269
dst->actframe = gpfd;
270
272
/* return new layer */
284
286
/* make a copy of the base-data */
285
dst= MEM_dupallocN(src);
287
dst = MEM_dupallocN(src);
287
289
/* copy layers */
288
dst->layers.first= dst->layers.last= NULL;
289
for (gpl= src->layers.first; gpl; gpl= gpl->next) {
290
dst->layers.first = dst->layers.last = NULL;
291
for (gpl = src->layers.first; gpl; gpl = gpl->next) {
290
292
/* make a copy of source layer and its data */
291
gpld= gpencil_layer_duplicate(gpl);
293
gpld = gpencil_layer_duplicate(gpl);
292
294
BLI_addtail(&dst->layers, gpld);
299
301
/* -------- GP-Frame API ---------- */
301
303
/* delete the last stroke of the given frame */
302
void gpencil_frame_delete_laststroke (bGPDlayer *gpl, bGPDframe *gpf)
304
void gpencil_frame_delete_laststroke(bGPDlayer *gpl, bGPDframe *gpf)
304
bGPDstroke *gps= (gpf) ? gpf->strokes.last : NULL;
306
bGPDstroke *gps = (gpf) ? gpf->strokes.last : NULL;
305
307
int cfra = (gpf) ? gpf->framenum : 0; /* assume that the current frame was not locked */
307
309
/* error checking */
322
324
/* -------- GP-Layer API ---------- */
326
bGPDframe *BKE_gpencil_layer_find_frame(bGPDlayer *gpl, int cframe)
330
for (gpf = gpl->frames.last; gpf; gpf = gpf->prev) {
331
if (gpf->framenum == cframe) {
324
339
/* get the appropriate gp-frame from a given layer
325
340
* - this sets the layer's actframe var (if allowed to)
326
341
* - extension beyond range (if first gp-frame is after all frame in interest and cannot add)
328
bGPDframe *gpencil_layer_getframe (bGPDlayer *gpl, int cframe, short addnew)
343
bGPDframe *gpencil_layer_getframe(bGPDlayer *gpl, int cframe, short addnew)
330
345
bGPDframe *gpf = NULL;
337
352
/* check if there is already an active frame */
338
353
if (gpl->actframe) {
341
356
/* do not allow any changes to layer's active frame if layer is locked from changes
342
357
* or if the layer has been set to stay on the current frame
344
if (gpl->flag & (GP_LAYER_LOCKED|GP_LAYER_FRAMELOCK))
359
if (gpl->flag & (GP_LAYER_LOCKED | GP_LAYER_FRAMELOCK))
346
361
/* do not allow any changes to actframe if frame has painting tag attached to it */
347
362
if (gpf->flag & GP_FRAME_PAINT)
350
365
/* try to find matching frame */
351
366
if (gpf->framenum < cframe) {
352
for (; gpf; gpf= gpf->next) {
367
for (; gpf; gpf = gpf->next) {
353
368
if (gpf->framenum == cframe) {
357
372
else if ((gpf->next) && (gpf->next->framenum > cframe)) {
363
378
/* set the appropriate frame */
365
380
if ((found) && (gpf->framenum == cframe))
368
gpl->actframe= gpencil_frame_addnew(gpl, cframe);
383
gpl->actframe = gpencil_frame_addnew(gpl, cframe);
373
gpl->actframe= gpl->frames.last;
388
gpl->actframe = gpl->frames.last;
376
for (; gpf; gpf= gpf->prev) {
391
for (; gpf; gpf = gpf->prev) {
377
392
if (gpf->framenum <= cframe) {
383
398
/* set the appropriate frame */
385
400
if ((found) && (gpf->framenum == cframe))
388
gpl->actframe= gpencil_frame_addnew(gpl, cframe);
403
gpl->actframe = gpencil_frame_addnew(gpl, cframe);
393
gpl->actframe= gpl->frames.first;
408
gpl->actframe = gpl->frames.first;
396
411
else if (gpl->frames.first) {
397
412
/* check which of the ends to start checking from */
398
const int first= ((bGPDframe *)(gpl->frames.first))->framenum;
399
const int last= ((bGPDframe *)(gpl->frames.last))->framenum;
413
const int first = ((bGPDframe *)(gpl->frames.first))->framenum;
414
const int last = ((bGPDframe *)(gpl->frames.last))->framenum;
401
if (abs(cframe-first) > abs(cframe-last)) {
416
if (abs(cframe - first) > abs(cframe - last)) {
402
417
/* find gp-frame which is less than or equal to cframe */
403
for (gpf= gpl->frames.last; gpf; gpf= gpf->prev) {
418
for (gpf = gpl->frames.last; gpf; gpf = gpf->prev) {
404
419
if (gpf->framenum <= cframe) {
411
426
/* find gp-frame which is less than or equal to cframe */
412
for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
427
for (gpf = gpl->frames.first; gpf; gpf = gpf->next) {
413
428
if (gpf->framenum <= cframe) {
420
435
/* set the appropriate frame */
422
437
if ((found) && (gpf->framenum == cframe))
425
gpl->actframe= gpencil_frame_addnew(gpl, cframe);
440
gpl->actframe = gpencil_frame_addnew(gpl, cframe);
430
445
/* unresolved errogenous situation! */
431
446
printf("Error: cannot find appropriate gp-frame\n");
436
451
/* currently no frames (add if allowed to) */
438
gpl->actframe= gpencil_frame_addnew(gpl, cframe);
453
gpl->actframe = gpencil_frame_addnew(gpl, cframe);
440
455
/* don't do anything... this may be when no frames yet! */
441
456
/* gpl->actframe should still be NULL */
449
464
/* delete the given frame from a layer */
450
void gpencil_layer_delframe (bGPDlayer *gpl, bGPDframe *gpf)
465
void gpencil_layer_delframe(bGPDlayer *gpl, bGPDframe *gpf)
452
467
/* error checking */
453
468
if (ELEM(NULL, gpl, gpf))
498
513
/* delete the active gp-layer */
499
void gpencil_layer_delactive (bGPdata *gpd)
514
void gpencil_layer_delete(bGPdata *gpd, bGPDlayer *gpl)
501
bGPDlayer *gpl= gpencil_layer_getactive(gpd);
503
516
/* error checking */
504
517
if (ELEM(NULL, gpd, gpl))
508
521
free_gpencil_frames(gpl);
509
522
BLI_freelinkN(&gpd->layers, gpl);