~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/gpencil.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
/* --------- Memory Management ------------ */
55
55
 
56
56
/* Free strokes belonging to a gp-frame */
57
 
void free_gpencil_strokes (bGPDframe *gpf)
 
57
void free_gpencil_strokes(bGPDframe *gpf)
58
58
{
59
59
        bGPDstroke *gps, *gpsn;
60
60
        
62
62
        if (gpf == NULL) return;
63
63
        
64
64
        /* free strokes */
65
 
        for (gps= gpf->strokes.first; gps; gps= gpsn) {
66
 
                gpsn= gps->next;
 
65
        for (gps = gpf->strokes.first; gps; gps = gpsn) {
 
66
                gpsn = gps->next;
67
67
                
68
68
                /* free stroke memory arrays, then stroke itself */
69
69
                if (gps->points) MEM_freeN(gps->points);
72
72
}
73
73
 
74
74
/* Free all of a gp-layer's frames */
75
 
void free_gpencil_frames (bGPDlayer *gpl)
 
75
void free_gpencil_frames(bGPDlayer *gpl)
76
76
{
77
77
        bGPDframe *gpf, *gpfn;
78
78
        
80
80
        if (gpl == NULL) return;
81
81
        
82
82
        /* free frames */
83
 
        for (gpf= gpl->frames.first; gpf; gpf= gpfn) {
84
 
                gpfn= gpf->next;
 
83
        for (gpf = gpl->frames.first; gpf; gpf = gpfn) {
 
84
                gpfn = gpf->next;
85
85
                
86
86
                /* free strokes and their associated memory */
87
87
                free_gpencil_strokes(gpf);
88
88
                BLI_freelinkN(&gpl->frames, gpf);
89
89
        }
 
90
        gpl->actframe = NULL;
90
91
}
91
92
 
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)
94
95
{
95
96
        bGPDlayer *gpl, *gpln;
96
97
        
97
98
        /* error checking */
98
99
        if (list == NULL) return;
99
100
        
100
 
        /* delete layers*/
101
 
        for (gpl= list->first; gpl; gpl= gpln) {
102
 
                gpln= gpl->next;
 
101
        /* delete layers */
 
102
        for (gpl = list->first; gpl; gpl = gpln) {
 
103
                gpln = gpl->next;
103
104
                
104
105
                /* free layers and their data */
105
106
                free_gpencil_frames(gpl);
108
109
}
109
110
 
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)
112
113
{
113
114
        /* free layers */
114
115
        free_gpencil_layers(&gpd->layers);
117
118
/* -------- Container Creation ---------- */
118
119
 
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)
121
122
{
122
123
        bGPDframe *gpf, *gf;
123
 
        short state=0;
 
124
        short state = 0;
124
125
        
125
126
        /* error checking */
126
127
        if ((gpl == NULL) || (cframe <= 0))
127
128
                return NULL;
128
129
                
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;
132
133
        
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) {
138
 
                                state= -1;
 
139
                                state = -1;
139
140
                                break;
140
141
                        }
141
142
                        
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);
145
 
                                state= 1;
 
146
                                state = 1;
146
147
                                break;
147
148
                        }
148
149
                }
163
164
}
164
165
 
165
166
/* add a new gp-layer and make it the active layer */
166
 
bGPDlayer *gpencil_layer_addnew (bGPdata *gpd)
 
167
bGPDlayer *gpencil_layer_addnew(bGPdata *gpd, const char *name, int setactive)
167
168
{
168
169
        bGPDlayer *gpl;
169
170
        
172
173
                return NULL;
173
174
                
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");
176
177
        
177
178
        /* add to datablock */
178
179
        BLI_addtail(&gpd->layers, gpl);
179
180
        
180
181
        /* set basic settings */
181
 
        gpl->color[3]= 0.9f;
 
182
        gpl->color[3] = 0.9f;
182
183
        gpl->thickness = 3;
183
184
        
184
185
        /* auto-name */
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));
187
188
        
188
189
        /* make this one the active one */
189
 
        gpencil_layer_setactive(gpd, gpl);
 
190
        if (setactive)
 
191
                gpencil_layer_setactive(gpd, gpl);
190
192
        
191
193
        /* return layer */
192
194
        return gpl;
193
195
}
194
196
 
195
197
/* add a new gp-datablock */
196
 
bGPdata *gpencil_data_addnew (const char name[])
 
198
bGPdata *gpencil_data_addnew(const char name[])
197
199
{
198
200
        bGPdata *gpd;
199
201
        
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);
202
204
        
203
205
        /* initial settings */
204
 
        gpd->flag = (GP_DATA_DISPINFO|GP_DATA_EXPAND);
 
206
        gpd->flag = (GP_DATA_DISPINFO | GP_DATA_EXPAND);
205
207
        
206
208
        /* for now, stick to view is also enabled by default
207
209
         * since this is more useful...
214
216
/* -------- Data Duplication ---------- */
215
217
 
216
218
/* make a copy of a given gpencil frame */
217
 
bGPDframe *gpencil_frame_duplicate (bGPDframe *src)
 
219
bGPDframe *gpencil_frame_duplicate(bGPDframe *src)
218
220
{
219
221
        bGPDstroke *gps, *gpsd;
220
222
        bGPDframe *dst;
224
226
                return NULL;
225
227
                
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;
229
231
        
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);
236
238
                
237
239
                BLI_addtail(&dst->strokes, gpsd);
238
240
        }
242
244
}
243
245
 
244
246
/* make a copy of a given gpencil layer */
245
 
bGPDlayer *gpencil_layer_duplicate (bGPDlayer *src)
 
247
bGPDlayer *gpencil_layer_duplicate(bGPDlayer *src)
246
248
{
247
249
        bGPDframe *gpf, *gpfd;
248
250
        bGPDlayer *dst;
252
254
                return NULL;
253
255
                
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;
257
259
        
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);
264
266
                
265
267
                /* if source frame was the current layer's 'active' frame, reassign that too */
266
268
                if (gpf == dst->actframe)
267
 
                        dst->actframe= gpfd;
 
269
                        dst->actframe = gpfd;
268
270
        }
269
271
        
270
272
        /* return new layer */
272
274
}
273
275
 
274
276
/* make a copy of a given gpencil datablock */
275
 
bGPdata *gpencil_data_duplicate (bGPdata *src)
 
277
bGPdata *gpencil_data_duplicate(bGPdata *src)
276
278
{
277
279
        bGPDlayer *gpl, *gpld;
278
280
        bGPdata *dst;
282
284
                return NULL;
283
285
        
284
286
        /* make a copy of the base-data */
285
 
        dst= MEM_dupallocN(src);
 
287
        dst = MEM_dupallocN(src);
286
288
        
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);
293
295
        }
294
296
        
299
301
/* -------- GP-Frame API ---------- */
300
302
 
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)
303
305
{
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 */
306
308
        
307
309
        /* error checking */
321
323
 
322
324
/* -------- GP-Layer API ---------- */
323
325
 
 
326
bGPDframe *BKE_gpencil_layer_find_frame(bGPDlayer *gpl, int cframe)
 
327
{
 
328
        bGPDframe *gpf;
 
329
 
 
330
        for (gpf = gpl->frames.last; gpf; gpf = gpf->prev) {
 
331
                if (gpf->framenum == cframe) {
 
332
                        return gpf;
 
333
                }
 
334
        }
 
335
 
 
336
        return NULL;
 
337
}
 
338
 
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)
327
342
 */
328
 
bGPDframe *gpencil_layer_getframe (bGPDlayer *gpl, int cframe, short addnew)
 
343
bGPDframe *gpencil_layer_getframe(bGPDlayer *gpl, int cframe, short addnew)
329
344
{
330
345
        bGPDframe *gpf = NULL;
331
346
        short found = 0;
336
351
        
337
352
        /* check if there is already an active frame */
338
353
        if (gpl->actframe) {
339
 
                gpf= gpl->actframe;
 
354
                gpf = gpl->actframe;
340
355
                
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
343
358
                 */
344
 
                if (gpl->flag & (GP_LAYER_LOCKED|GP_LAYER_FRAMELOCK))
 
359
                if (gpl->flag & (GP_LAYER_LOCKED | GP_LAYER_FRAMELOCK))
345
360
                        return gpf;
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) 
349
364
                
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) {
354
 
                                        found= 1;
 
369
                                        found = 1;
355
370
                                        break;
356
371
                                }
357
372
                                else if ((gpf->next) && (gpf->next->framenum > cframe)) {
358
 
                                        found= 1;
 
373
                                        found = 1;
359
374
                                        break;
360
375
                                }
361
376
                        }
363
378
                        /* set the appropriate frame */
364
379
                        if (addnew) {
365
380
                                if ((found) && (gpf->framenum == cframe))
366
 
                                        gpl->actframe= gpf;
 
381
                                        gpl->actframe = gpf;
367
382
                                else
368
 
                                        gpl->actframe= gpencil_frame_addnew(gpl, cframe);
 
383
                                        gpl->actframe = gpencil_frame_addnew(gpl, cframe);
369
384
                        }
370
385
                        else if (found)
371
 
                                gpl->actframe= gpf;
 
386
                                gpl->actframe = gpf;
372
387
                        else
373
 
                                gpl->actframe= gpl->frames.last;
 
388
                                gpl->actframe = gpl->frames.last;
374
389
                }
375
390
                else {
376
 
                        for (; gpf; gpf= gpf->prev) {
 
391
                        for (; gpf; gpf = gpf->prev) {
377
392
                                if (gpf->framenum <= cframe) {
378
 
                                        found= 1;
 
393
                                        found = 1;
379
394
                                        break;
380
395
                                }
381
396
                        }
383
398
                        /* set the appropriate frame */
384
399
                        if (addnew) {
385
400
                                if ((found) && (gpf->framenum == cframe))
386
 
                                        gpl->actframe= gpf;
 
401
                                        gpl->actframe = gpf;
387
402
                                else
388
 
                                        gpl->actframe= gpencil_frame_addnew(gpl, cframe);
 
403
                                        gpl->actframe = gpencil_frame_addnew(gpl, cframe);
389
404
                        }
390
405
                        else if (found)
391
 
                                gpl->actframe= gpf;
 
406
                                gpl->actframe = gpf;
392
407
                        else
393
 
                                gpl->actframe= gpl->frames.first;
 
408
                                gpl->actframe = gpl->frames.first;
394
409
                }
395
410
        }
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;
400
415
                
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) {
405
 
                                        found= 1;
 
420
                                        found = 1;
406
421
                                        break;
407
422
                                }
408
423
                        }
409
424
                }
410
425
                else {
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) {
414
 
                                        found= 1;
 
429
                                        found = 1;
415
430
                                        break;
416
431
                                }
417
432
                        }
420
435
                /* set the appropriate frame */
421
436
                if (addnew) {
422
437
                        if ((found) && (gpf->framenum == cframe))
423
 
                                gpl->actframe= gpf;
 
438
                                gpl->actframe = gpf;
424
439
                        else
425
 
                                gpl->actframe= gpencil_frame_addnew(gpl, cframe);
 
440
                                gpl->actframe = gpencil_frame_addnew(gpl, cframe);
426
441
                }
427
442
                else if (found)
428
 
                        gpl->actframe= gpf;
 
443
                        gpl->actframe = gpf;
429
444
                else {
430
445
                        /* unresolved errogenous situation! */
431
446
                        printf("Error: cannot find appropriate gp-frame\n");
435
450
        else {
436
451
                /* currently no frames (add if allowed to) */
437
452
                if (addnew)
438
 
                        gpl->actframe= gpencil_frame_addnew(gpl, cframe);
 
453
                        gpl->actframe = gpencil_frame_addnew(gpl, cframe);
439
454
                else {
440
455
                        /* don't do anything... this may be when no frames yet! */
441
456
                        /* gpl->actframe should still be NULL */
447
462
}
448
463
 
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)
451
466
{
452
467
        /* error checking */
453
468
        if (ELEM(NULL, gpl, gpf))
460
475
}
461
476
 
462
477
/* get the active gp-layer for editing */
463
 
bGPDlayer *gpencil_layer_getactive (bGPdata *gpd)
 
478
bGPDlayer *gpencil_layer_getactive(bGPdata *gpd)
464
479
{
465
480
        bGPDlayer *gpl;
466
481
        
469
484
                return NULL;
470
485
                
471
486
        /* loop over layers until found (assume only one active) */
472
 
        for (gpl=gpd->layers.first; gpl; gpl=gpl->next) {
 
487
        for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
473
488
                if (gpl->flag & GP_LAYER_ACTIVE)
474
489
                        return gpl;
475
490
        }
479
494
}
480
495
 
481
496
/* set the active gp-layer */
482
 
void gpencil_layer_setactive (bGPdata *gpd, bGPDlayer *active)
 
497
void gpencil_layer_setactive(bGPdata *gpd, bGPDlayer *active)
483
498
{
484
499
        bGPDlayer *gpl;
485
500
        
488
503
                return;
489
504
                
490
505
        /* loop over layers deactivating all */
491
 
        for (gpl=gpd->layers.first; gpl; gpl=gpl->next)
 
506
        for (gpl = gpd->layers.first; gpl; gpl = gpl->next)
492
507
                gpl->flag &= ~GP_LAYER_ACTIVE;
493
508
        
494
509
        /* set as active one */
496
511
}
497
512
 
498
513
/* delete the active gp-layer */
499
 
void gpencil_layer_delactive (bGPdata *gpd)
 
514
void gpencil_layer_delete(bGPdata *gpd, bGPDlayer *gpl)
500
515
{
501
 
        bGPDlayer *gpl= gpencil_layer_getactive(gpd);
502
 
        
503
516
        /* error checking */
504
517
        if (ELEM(NULL, gpd, gpl)) 
505
518
                return;
506
519
        
507
 
        /* free layer */        
 
520
        /* free layer */
508
521
        free_gpencil_frames(gpl);
509
522
        BLI_freelinkN(&gpd->layers, gpl);
510
523
}