Graph Editor: Visibility toggles
[blender.git] / source / blender / editors / animation / anim_filter.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* This file contains a system used to provide a layer of abstraction between sources
30  * of animation data and tools in Animation Editors. The method used here involves 
31  * generating a list of edit structures which enable tools to naively perform the actions 
32  * they require without all the boiler-plate associated with loops within loops and checking 
33  * for cases to ignore. 
34  *
35  * While this is primarily used for the Action/Dopesheet Editor (and its accessory modes),
36  * the IPO Editor also uses this for it's channel list and for determining which curves
37  * are being edited.
38  *
39  * Note: much of the original system this was based on was built before the creation of the RNA
40  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
41  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this 
42  * system, so if any such work does occur, it should only be used for the internals used here...
43  *
44  * -- Joshua Leung, Dec 2008
45  */
46
47 #include <string.h>
48 #include <stdio.h>
49
50 #include "DNA_listBase.h"
51 #include "DNA_ID.h"
52 #include "DNA_anim_types.h"
53 #include "DNA_action_types.h"
54 #include "DNA_constraint_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_curve_types.h"
57 #include "DNA_gpencil_types.h"
58 #include "DNA_ipo_types.h"
59 #include "DNA_lamp_types.h"
60 #include "DNA_lattice_types.h"
61 #include "DNA_key_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_mesh_types.h"
64 #include "DNA_object_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_screen_types.h"
68 #include "DNA_windowmanager_types.h"
69
70 #include "MEM_guardedalloc.h"
71
72 #include "BLI_blenlib.h"
73
74 #include "BKE_context.h"
75 #include "BKE_global.h"
76 #include "BKE_key.h"
77 #include "BKE_object.h"
78 #include "BKE_material.h"
79 #include "BKE_screen.h"
80 #include "BKE_utildefines.h"
81
82 #include "ED_anim_api.h"
83 #include "ED_types.h"
84 #include "ED_util.h"
85
86 #include "WM_api.h"
87 #include "WM_types.h"
88
89 #include "BIF_gl.h"
90 #include "BIF_glutil.h"
91
92 #include "UI_interface.h"
93 #include "UI_resources.h"
94 #include "UI_view2d.h"
95
96 /* ************************************************************ */
97 /* Blender Context <-> Animation Context mapping */
98
99 /* ----------- Private Stuff - Action Editor ------------- */
100
101 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
102 /* Note: there's a similar function in key.c (ob_get_key) */
103 Key *actedit_get_shapekeys (bAnimContext *ac, SpaceAction *saction) 
104 {
105     Scene *scene= ac->scene;
106     Object *ob;
107     Key *key;
108         
109     ob = OBACT;
110     if (ob == NULL) 
111                 return NULL;
112         
113         /* XXX pinning is not available in 'ShapeKey' mode... */
114         //if (saction->pin) return NULL;
115         
116         /* shapekey data is stored with geometry data */
117         switch (ob->type) {
118                 case OB_MESH:
119                         key= ((Mesh *)ob->data)->key;
120                         break;
121                         
122                 case OB_LATTICE:
123                         key= ((Lattice *)ob->data)->key;
124                         break;
125                         
126                 case OB_CURVE:
127                 case OB_SURF:
128                         key= ((Curve *)ob->data)->key;
129                         break;
130                         
131                 default:
132                         return NULL;
133         }
134         
135         if (key) {
136                 if (key->type == KEY_RELATIVE)
137                         return key;
138         }
139         
140     return NULL;
141 }
142
143 /* Get data being edited in Action Editor (depending on current 'mode') */
144 static short actedit_get_context (bAnimContext *ac, SpaceAction *saction)
145 {
146         /* sync settings with current view status, then return appropriate data */
147         switch (saction->mode) {
148                 case SACTCONT_ACTION: /* 'Action Editor' */
149                         /* if not pinned, sync with active object */
150                         if (saction->pin == 0) {
151                                 if (ac->obact && ac->obact->adt)
152                                         saction->action = ac->obact->adt->action;
153                                 else
154                                         saction->action= NULL;
155                         }
156                         
157                         ac->datatype= ANIMCONT_ACTION;
158                         ac->data= saction->action;
159                         
160                         ac->mode= saction->mode;
161                         return 1;
162                         
163                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
164                         ac->datatype= ANIMCONT_SHAPEKEY;
165                         ac->data= actedit_get_shapekeys(ac, saction);
166                         
167                         ac->mode= saction->mode;
168                         return 1;
169                         
170                 case SACTCONT_GPENCIL: /* Grease Pencil */ // XXX review how this mode is handled...
171                         ac->datatype=ANIMCONT_GPENCIL;
172                         //ac->data= CTX_wm_screen(C); // FIXME: add that dopesheet type thing here!
173                         ac->data= NULL; // !!!
174                         
175                         ac->mode= saction->mode;
176                         return 1;
177                         
178                 case SACTCONT_DOPESHEET: /* DopeSheet */
179                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
180                         saction->ads.source= (ID *)ac->scene;
181                         
182                         ac->datatype= ANIMCONT_DOPESHEET;
183                         ac->data= &saction->ads;
184                         
185                         ac->mode= saction->mode;
186                         return 1;
187                 
188                 default: /* unhandled yet */
189                         ac->datatype= ANIMCONT_NONE;
190                         ac->data= NULL;
191                         
192                         ac->mode= -1;
193                         return 0;
194         }
195 }
196
197 /* ----------- Private Stuff - IPO Editor ------------- */
198
199 /* Get data being edited in Graph Editor (depending on current 'mode') */
200 static short graphedit_get_context (bAnimContext *ac, SpaceIpo *sipo)
201 {
202         /* init dopesheet data if non-existant (i.e. for old files) */
203         if (sipo->ads == NULL)
204                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
205         
206         /* sync settings with current view status, then return appropriate data */
207         switch (sipo->mode) {
208                 case SIPO_MODE_ANIMATION:       /* Animation F-Curve Editor */
209                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
210                         sipo->ads->source= (ID *)ac->scene;
211                         sipo->ads->filterflag &= ~ADS_FILTER_ONLYDRIVERS;
212                         
213                         ac->datatype= ANIMCONT_FCURVES;
214                         ac->data= sipo->ads;
215                         
216                         ac->mode= sipo->mode;
217                         return 1;
218                 
219                 case SIPO_MODE_DRIVERS:         /* Driver F-Curve Editor */
220                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
221                         sipo->ads->source= (ID *)ac->scene;
222                         sipo->ads->filterflag |= ADS_FILTER_ONLYDRIVERS;
223                         
224                         ac->datatype= ANIMCONT_DRIVERS;
225                         ac->data= sipo->ads;
226                         
227                         ac->mode= sipo->mode;
228                         return 1;
229                 
230                 default: /* unhandled yet */
231                         ac->datatype= ANIMCONT_NONE;
232                         ac->data= NULL;
233                         
234                         ac->mode= -1;
235                         return 0;
236         }
237 }
238
239 /* ----------- Public API --------------- */
240
241 /* Obtain current anim-data context, given that context info from Blender context has already been set 
242  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
243  *        allocation/freeing costs (which are not that avoidable with channels).
244  */
245 short ANIM_animdata_context_getdata (bAnimContext *ac)
246 {
247         ScrArea *sa= ac->sa;
248         short ok= 0;
249         
250         /* context depends on editor we are currently in */
251         if (sa) {
252                 switch (sa->spacetype) {
253                         case SPACE_ACTION:
254                         {
255                                 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
256                                 ok= actedit_get_context(ac, saction);
257                         }
258                                 break;
259                                 
260                         case SPACE_IPO:
261                         {
262                                 SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
263                                 ok= graphedit_get_context(ac, sipo);
264                         }
265                                 break;
266                 }
267         }
268         
269         /* check if there's any valid data */
270         if (ok && ac->data)
271                 return 1;
272         else
273                 return 0;
274 }
275
276 /* Obtain current anim-data context from Blender Context info 
277  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
278  *        allocation/freeing costs (which are not that avoidable with channels).
279  *      - Clears data and sets the information from Blender Context which is useful
280  */
281 short ANIM_animdata_get_context (const bContext *C, bAnimContext *ac)
282 {
283         ScrArea *sa= CTX_wm_area(C);
284         ARegion *ar= CTX_wm_region(C);
285         Scene *scene= CTX_data_scene(C);
286         
287         /* clear old context info */
288         if (ac == NULL) return 0;
289         memset(ac, 0, sizeof(bAnimContext));
290         
291         /* get useful default context settings from context */
292         ac->scene= scene;
293         ac->obact= (scene && scene->basact)?  scene->basact->object : NULL;
294         ac->sa= sa;
295         ac->ar= ar;
296         ac->spacetype= (sa) ? sa->spacetype : 0;
297         ac->regiontype= (ar) ? ar->regiontype : 0;
298         
299         /* get data context info */
300         return ANIM_animdata_context_getdata(ac);
301 }
302
303 /* ************************************************************ */
304 /* Blender Data <-- Filter --> Channels to be operated on */
305
306 /* quick macro to test if AnimData is usable */
307 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
308
309 /* quick macro to test if AnimData is usable for drivers */
310 #define ANIMDATA_HAS_DRIVERS(id) ((id)->adt && (id)->adt->drivers.first)
311
312 /* ----------- 'Private' Stuff --------------- */
313
314 /* this function allocates memory for a new bAnimListElem struct for the 
315  * provided animation channel-data. 
316  */
317 bAnimListElem *make_new_animlistelem (void *data, short datatype, void *owner, short ownertype, ID *owner_id)
318 {
319         bAnimListElem *ale= NULL;
320         
321         /* only allocate memory if there is data to convert */
322         if (data) {
323                 /* allocate and set generic data */
324                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
325                 
326                 ale->data= data;
327                 ale->type= datatype;
328                         // XXX what is the point of the owner data?
329                 ale->owner= owner;
330                 ale->ownertype= ownertype;
331                 
332                 ale->id= owner_id;
333                 
334                 /* do specifics */
335                 switch (datatype) {
336                         case ANIMTYPE_OBJECT:
337                         {
338                                 Base *base= (Base *)data;
339                                 Object *ob= base->object;
340                                 
341                                 ale->flag= ob->flag;
342                                 
343                                 ale->key_data= ob;
344                                 ale->datatype= ALE_OB;
345                         }
346                                 break;
347                         case ANIMTYPE_FILLACTD:
348                         {
349                                 bAction *act= (bAction *)data;
350                                 
351                                 ale->flag= act->flag;
352                                 
353                                 ale->key_data= act;
354                                 ale->datatype= ALE_ACT;
355                         }
356                                 break;
357                         case ANIMTYPE_FILLDRIVERS:
358                         {
359                                 AnimData *adt= (AnimData *)data;
360                                 
361                                 ale->flag= adt->flag;
362                                 
363                                         // XXX... drivers don't show summary for now
364                                 ale->key_data= NULL;
365                                 ale->datatype= ALE_NONE;
366                         }
367                                 break;
368                         case ANIMTYPE_FILLMATD:
369                         {
370                                 Object *ob= (Object *)data;
371                                 
372                                 ale->flag= FILTER_MAT_OBJC(ob);
373                                 
374                                 ale->key_data= NULL;
375                                 ale->datatype= ALE_NONE;
376                         }
377                                 break;
378                         
379                         case ANIMTYPE_DSMAT:
380                         {
381                                 Material *ma= (Material *)data;
382                                 AnimData *adt= ma->adt;
383                                 
384                                 ale->flag= FILTER_MAT_OBJD(ma);
385                                 
386                                 ale->key_data= (adt) ? adt->action : NULL;
387                                 ale->datatype= ALE_ACT;
388                         }
389                                 break;
390                         case ANIMTYPE_DSLAM:
391                         {
392                                 Lamp *la= (Lamp *)data;
393                                 AnimData *adt= la->adt;
394                                 
395                                 ale->flag= FILTER_LAM_OBJD(la);
396                                 
397                                 ale->key_data= (adt) ? adt->action : NULL;
398                                 ale->datatype= ALE_ACT;
399                         }
400                                 break;
401                         case ANIMTYPE_DSCAM:
402                         {
403                                 Camera *ca= (Camera *)data;
404                                 AnimData *adt= ca->adt;
405                                 
406                                 ale->flag= FILTER_CAM_OBJD(ca);
407                                 
408                                 ale->key_data= (adt) ? adt->action : NULL;
409                                 ale->datatype= ALE_ACT;
410                         }
411                                 break;
412                         case ANIMTYPE_DSCUR:
413                         {
414                                 Curve *cu= (Curve *)data;
415                                 AnimData *adt= cu->adt;
416                                 
417                                 ale->flag= FILTER_CUR_OBJD(cu);
418                                 
419                                 ale->key_data= (adt) ? adt->action : NULL;
420                                 ale->datatype= ALE_ACT;
421                         }
422                                 break;
423                         case ANIMTYPE_DSSKEY:
424                         {
425                                 Key *key= (Key *)data;
426                                 AnimData *adt= key->adt;
427                                 
428                                 ale->flag= FILTER_SKE_OBJD(key); 
429                                 
430                                 ale->key_data= (adt) ? adt->action : NULL;
431                                 ale->datatype= ALE_ACT;
432                         }
433                                 break;
434                                 
435                         case ANIMTYPE_GROUP:
436                         {
437                                 bActionGroup *agrp= (bActionGroup *)data;
438                                 
439                                 ale->flag= agrp->flag;
440                                 
441                                 ale->key_data= NULL;
442                                 ale->datatype= ALE_GROUP;
443                         }
444                                 break;
445                         case ANIMTYPE_FCURVE:
446                         {
447                                 FCurve *fcu= (FCurve *)data;
448                                 
449                                 ale->flag= fcu->flag;
450                                 
451                                 ale->key_data= fcu;
452                                 ale->datatype= ALE_FCURVE;
453                         }
454                                 break;
455                         
456                         case ANIMTYPE_GPLAYER:
457                         {
458                                 bGPDlayer *gpl= (bGPDlayer *)data;
459                                 
460                                 ale->flag= gpl->flag;
461                                 
462                                 ale->key_data= NULL;
463                                 ale->datatype= ALE_GPFRAME;
464                         }
465                                 break;
466                 }
467         }
468         
469         /* return created datatype */
470         return ale;
471 }
472  
473 /* ----------------------------------------- */
474
475
476 static int animdata_filter_fcurves (ListBase *anim_data, FCurve *first, bActionGroup *grp, void *owner, short ownertype, int filter_mode, ID *owner_id)
477 {
478         bAnimListElem *ale = NULL;
479         FCurve *fcu;
480         int items = 0;
481         
482         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
483          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
484          */
485         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
486                 /* only include if visible (Graph Editor check, not channels check) */
487                 if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
488                         /* only work with this channel and its subchannels if it is editable */
489                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
490                                 /* only include this curve if selected */
491                                 if (!(filter_mode & ANIMFILTER_SEL) || (SEL_FCU(fcu))) {
492                                         /* owner/ownertype will be either object or action-channel, depending if it was dopesheet or part of an action */
493                                         ale= make_new_animlistelem(fcu, ANIMTYPE_FCURVE, owner, ownertype, owner_id);
494                                         
495                                         if (ale) {
496                                                 BLI_addtail(anim_data, ale);
497                                                 items++;
498                                         }
499                                 }
500                         }
501                 }
502         }
503         
504         /* return the number of items added to the list */
505         return items;
506 }
507
508 static int animdata_filter_action (ListBase *anim_data, bAction *act, int filter_mode, void *owner, short ownertype, ID *owner_id)
509 {
510         bAnimListElem *ale=NULL;
511         bActionGroup *agrp;
512         FCurve *lastchan=NULL;
513         int items = 0;
514         
515         /* loop over groups */
516         //       XXX in future, we need to be prepared for nestled groups...
517         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
518                 /* add this group as a channel first */
519                 if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
520                         /* check if filtering by selection */
521                         if ( !(filter_mode & ANIMFILTER_SEL) || SEL_AGRP(agrp) ) {
522                                 ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE, owner_id);
523                                 if (ale) {
524                                         BLI_addtail(anim_data, ale);
525                                         items++;
526                                 }
527                         }
528                 }
529                 
530                 /* store reference to last channel of group */
531                 if (agrp->channels.last) 
532                         lastchan= agrp->channels.last;
533                 
534                 
535                 /* there are some situations, where only the channels of the animive group should get considered */
536                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
537                         /* filters here are a bit convoulted...
538                          *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
539                          *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
540                          *
541                          * cases when we should include F-Curves inside group:
542                          *      - we don't care about visibility
543                          *      - group is expanded
544                          *      - we're interested in keyframes, but not if they appear in selected channels
545                          */
546                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || 
547                                  ( (!(filter_mode & ANIMFILTER_SEL) || (SEL_AGRP(agrp))) && 
548                                    (filter_mode & ANIMFILTER_CURVESONLY) ) ) 
549                         {
550                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
551                                         // XXX the 'owner' info here needs review...
552                                         items += animdata_filter_fcurves(anim_data, agrp->channels.first, agrp, owner, ownertype, filter_mode, owner_id);
553                                         
554                                         /* remove group from filtered list if last element is group 
555                                          * (i.e. only if group had channels, which were all hidden)
556                                          */
557                                         // XXX this is really hacky... it should be fixed in a much more elegant way!
558                                         if ( (ale) && (anim_data->last == ale) && 
559                                                  (ale->data == agrp) && (agrp->channels.first) ) 
560                                         {
561                                                 BLI_freelinkN(anim_data, ale);
562                                                 items--;
563                                         }
564                                 }
565                         }
566                 }
567         }
568         
569         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
570         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
571                 // XXX the 'owner' info here needs review...
572                 items += animdata_filter_fcurves(anim_data, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
573         }
574         
575         /* return the number of items added to the list */
576         return items;
577 }
578
579 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode, void *owner, short ownertype, ID *owner_id)
580 {
581         bAnimListElem *ale;
582         KeyBlock *kb;
583         //FCurve *fcu;
584         int i, items=0;
585         
586         /* are we filtering for display or editing */
587         if (filter_mode & ANIMFILTER_CHANNELS) {
588                 /* for display - loop over shapekeys, adding ipo-curve references where needed */
589                 kb= key->block.first;
590                 
591                 /* loop through possible shapekeys, manually creating entries */
592                 for (i= 1; i < key->totkey; i++) {
593                         ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
594                         kb = kb->next; /* do this even on the first try, as the first is 'Basis' (which doesn't get included) */
595                         
596                         ale->data= kb;
597                         ale->type= ANIMTYPE_SHAPEKEY; /* 'abused' usage of this type */
598                         ale->owner= key;
599                         ale->ownertype= ANIMTYPE_SHAPEKEY;
600                         ale->datatype= ALE_NONE;
601                         ale->index = i;
602                         
603 #if 0 // XXX fixme... old system
604                         if (key->ipo) {
605                                 for (icu= key->ipo->curve.first; icu; icu=icu->next) {
606                                         if (icu->adrcode == i) {
607                                                 ale->key_data= icu;
608                                                 ale->datatype= ALE_ICU;
609                                                 break;
610                                         }
611                                 }
612                         }
613 #endif // XXX fixme... old system
614                         
615                         ale->id= owner_id;
616                         
617                         BLI_addtail(anim_data, ale);
618                         items++;
619                 }
620         }
621         else {
622 #if 0 // XXX fixme... old system
623                 /* loop over ipo curves if present - for editing */
624                 if (key->ipo) {
625                         if (filter_mode & ANIMFILTER_IPOKEYS) {
626                                 ale= make_new_animlistelem(key->ipo, ANIMTYPE_IPO, key, ANIMTYPE_SHAPEKEY);
627                                 if (ale) {
628                                         if (owned) ale->id= owner;
629                                         BLI_addtail(anim_data, ale);
630                                         items++;
631                                 }
632                         }
633                         else {
634                                 items += animdata_filter_ipocurves(anim_data, key->ipo, filter_mode, key, ANIMTYPE_SHAPEKEY, (owned)?(owner):(NULL));
635                         }
636                 }
637 #endif // XXX fixme... old system
638         }
639         
640         /* return the number of items added to the list */
641         return items;
642 }
643  
644 #if 0
645 // FIXME: switch this to use the bDopeSheet...
646 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
647 {
648         bAnimListElem *ale;
649         ScrArea *sa, *curarea;
650         bGPdata *gpd;
651         bGPDlayer *gpl;
652         int items = 0;
653         
654         /* check if filtering types are appropriate */
655         {
656                 /* special hack for fullscreen area (which must be this one then):
657                  *      - we use the curarea->full as screen to get spaces from, since the
658                  *        old (pre-fullscreen) screen was stored there...
659                  *      - this is needed as all data would otherwise disappear
660                  */
661                 // XXX need to get new alternative for curarea
662                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
663                         sc= curarea->full;
664                 
665                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
666                 for (sa= sc->areabase.first; sa; sa= sa->next) {
667                         /* try to get gp data */
668                         // XXX need to put back grease pencil api...
669                         gpd= gpencil_data_getactive(sa);
670                         if (gpd == NULL) continue;
671                         
672                         /* add gpd as channel too (if for drawing, and it has layers) */
673                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
674                                 /* add to list */
675                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
676                                 if (ale) {
677                                         BLI_addtail(anim_data, ale);
678                                         items++;
679                                 }
680                         }
681                         
682                         /* only add layers if they will be visible (if drawing channels) */
683                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
684                                 /* loop over layers as the conditions are acceptable */
685                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
686                                         /* only if selected */
687                                         if (!(filter_mode & ANIMFILTER_SEL) || SEL_GPL(gpl)) {
688                                                 /* only if editable */
689                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
690                                                         /* add to list */
691                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
692                                                         if (ale) {
693                                                                 BLI_addtail(anim_data, ale);
694                                                                 items++;
695                                                         }
696                                                 }
697                                         }
698                                 }
699                         }
700                 }
701         }
702         
703         /* return the number of items added to the list */
704         return items;
705 }
706 #endif 
707
708
709 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
710 {
711         ListBase mats = {NULL, NULL};
712         LinkData *ld;
713         
714         bAnimListElem *ale=NULL;
715         Object *ob= base->object;
716         int items=0, a=0;
717         
718         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
719         for (a=0; a < ob->totcol; a++) {
720                 Material *ma= give_current_material(ob, a);
721                 
722                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
723                 if (ELEM3(NULL, ma, ma->adt, ma->adt->action)) continue;
724                 
725                 /* make a temp list elem for this */
726                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
727                 ld->data= ma;
728                 BLI_addtail(&mats, ld);
729         }
730         
731         /* if there were no channels found, no need to carry on */
732         if (mats.first == NULL)
733                 return 0;
734         
735         /* include materials-expand widget? */
736         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
737                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
738                 if (ale) {
739                         BLI_addtail(anim_data, ale);
740                         items++;
741                 }
742         }
743         
744         /* add materials? */
745         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
746                 /* for each material in cache, add channels  */
747                 for (ld= mats.first; ld; ld= ld->next) {
748                         Material *ma= (Material *)ld->data;
749                         
750                         /* include material-expand widget? */
751                         // hmm... do we need to store the index of this material in the array anywhere?
752                         if (filter_mode & ANIMFILTER_CHANNELS) {
753                                 ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
754                                 if (ale) {
755                                         BLI_addtail(anim_data, ale);
756                                         items++;
757                                 }
758                         }
759                         
760                         /* add material's F-Curve channels? */
761                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
762                                 //items += animdata_filter_ipocurves(anim_data, ma->ipo, filter_mode, base, ANIMTYPE_OBJECT, (ID *)ma);
763                                         // XXX the 'owner' info here is still subject to improvement
764                                 items += animdata_filter_action(anim_data, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);
765                         }
766                 }
767         }
768         
769         /* free cache */
770         BLI_freelistN(&mats);
771         
772         /* return the number of items added to the list */
773         return items;
774 }
775
776 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
777 {
778         bAnimListElem *ale=NULL;
779         Object *ob= base->object;
780         IdAdtTemplate *iat= ob->data;
781         AnimData *adt= iat->adt;
782         short type=0, expanded=0;
783         int items= 0;
784         
785         /* get settings based on data type */
786         switch (ob->type) {
787                 case OB_CAMERA: /* ------- Camera ------------ */
788                 {
789                         Camera *ca= (Camera *)ob->data;
790                         
791                         type= ANIMTYPE_DSCAM;
792                         expanded= FILTER_CAM_OBJD(ca);
793                 }
794                         break;
795                 case OB_LAMP: /* ---------- Lamp ----------- */
796                 {
797                         Lamp *la= (Lamp *)ob->data;
798                         
799                         type= ANIMTYPE_DSLAM;
800                         expanded= FILTER_LAM_OBJD(la);
801                 }
802                         break;
803                 case OB_CURVE: /* ------- Curve ---------- */
804                 {
805                         Curve *cu= (Curve *)ob->data;
806                         
807                         type= ANIMTYPE_DSCUR;
808                         expanded= FILTER_CUR_OBJD(cu);
809                 }
810                         break;
811         }
812         
813         /* special exception for drivers instead of action */
814         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
815                 expanded= EXPANDED_DRVD(adt);
816         
817         /* include data-expand widget? */
818         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {               
819                 ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
820                 if (ale) BLI_addtail(anim_data, ale);
821         }
822         
823         /* add object-data animation channels? */
824         if ((expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
825                 /* Action or Drivers? */
826                 if ((ads->filterflag & ADS_FILTER_ONLYDRIVERS) == 0) {
827                         // XXX the 'owner' info here is still subject to improvement
828                         items += animdata_filter_action(anim_data, iat->adt->action, filter_mode, iat, type, (ID *)iat);
829                 }
830                 else {
831                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
832                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);
833                 }
834         }
835         
836         /* return the number of items added to the list */
837         return items;
838 }
839
840 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
841 {
842         bAnimListElem *ale=NULL;
843         Scene *sce= (Scene *)ads->source;
844         Object *ob= base->object;
845         Key *key= ob_get_key(ob);
846         int items = 0;
847         
848         /* add this object as a channel first */
849         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
850                 /* check if filtering by selection */
851                 if ( !(filter_mode & ANIMFILTER_SEL) || ((base->flag & SELECT) || (base == sce->basact)) ) {
852                         ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, NULL);
853                         if (ale) {
854                                 BLI_addtail(anim_data, ale);
855                                 items++;
856                         }
857                 }
858         }
859         
860         /* if collapsed, don't go any further (unless adding keyframes only) */
861         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & ANIMFILTER_CURVESONLY) )
862                 return items;
863         
864         /* Action or Drivers */
865         if ((ads->filterflag & ADS_FILTER_ONLYDRIVERS) == 0) {
866                 /* Action? */
867                 if (ANIMDATA_HAS_KEYS(ob) /*&& !(ads->filterflag & ADS_FILTER_NOACTS)*/) {
868                         AnimData *adt= ob->adt;
869                         
870                         /* include action-expand widget? */
871                         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
872                                 ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
873                                 if (ale) {
874                                         BLI_addtail(anim_data, ale);
875                                         items++;
876                                 }
877                         }
878                         
879                         /* add F-Curve channels? */
880                         if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
881                                 // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
882                                 items += animdata_filter_action(anim_data, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
883                         }
884                 }
885         }
886         else {
887                 /* Drivers */
888                 if (ANIMDATA_HAS_DRIVERS(ob)) {
889                         AnimData *adt= ob->adt;
890                         
891                         /* include drivers-expand widget? */
892                         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
893                                 ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
894                                 if (ale) {
895                                         BLI_addtail(anim_data, ale);
896                                         items++;
897                                 }
898                         }
899                         
900                         /* add F-Curve channels (drivers are F-Curves) */
901                         if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
902                                 // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
903                                 items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
904                         }
905                 }
906         }
907         
908         /* ShapeKeys? */
909         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
910                 /* Animation or Drivers */
911                 if ((ads->filterflag & ADS_FILTER_ONLYDRIVERS) == 0) {
912                         /* include shapekey-expand widget? */
913                         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
914                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
915                                 if (ale) {
916                                         BLI_addtail(anim_data, ale);
917                                         items++;
918                                 }
919                         }
920                         
921                         /* add channels */
922                         if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
923                                 items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
924                         }
925                 }
926                 else {
927                         /* Drivers */
928                         if (ANIMDATA_HAS_DRIVERS(key)) {
929                                 AnimData *adt= key->adt;
930                                 
931                                 /* include shapekey-expand widget? */
932                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
933                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
934                                         if (ale) {
935                                                 BLI_addtail(anim_data, ale);
936                                                 items++;
937                                         }
938                                 }
939                                 
940                                 /* add F-Curve channels (drivers are F-Curves) */
941                                 if (FILTER_SKE_OBJD(key)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
942                                         // XXX owner info is messed up now...
943                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)key);
944                                 }
945                         }
946                 }
947         }
948         
949
950         /* Materials? */
951         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
952                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
953         
954         /* Object Data */
955         switch (ob->type) {
956                 case OB_CAMERA: /* ------- Camera ------------ */
957                 {
958                         Camera *ca= (Camera *)ob->data;
959                         
960                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
961                                 if ((ads->filterflag & ADS_FILTER_ONLYDRIVERS)==0) {
962                                         if (ANIMDATA_HAS_KEYS(ca))
963                                                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
964                                 }
965                                 else {
966                                         if (ANIMDATA_HAS_DRIVERS(ca))
967                                                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
968                                 }
969                         }
970                 }
971                         break;
972                 case OB_LAMP: /* ---------- Lamp ----------- */
973                 {
974                         Lamp *la= (Lamp *)ob->data;
975                         
976                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
977                                 if ((ads->filterflag & ADS_FILTER_ONLYDRIVERS)==0) {
978                                         if (ANIMDATA_HAS_KEYS(la))
979                                                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
980                                 }
981                                 else {
982                                         if (ANIMDATA_HAS_DRIVERS(la))
983                                                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
984                                 }
985                         }
986                 }
987                         break;
988                 case OB_CURVE: /* ------- Curve ---------- */
989                 {
990                         Curve *cu= (Curve *)ob->data;
991                         
992                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
993                                 if ((ads->filterflag & ADS_FILTER_ONLYDRIVERS)==0) {
994                                         if (ANIMDATA_HAS_KEYS(cu))
995                                                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
996                                 }
997                                 else {
998                                         if (ANIMDATA_HAS_DRIVERS(cu))
999                                                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1000                                 }
1001                         }
1002                 }
1003                         break;
1004         }
1005         
1006         /* return the number of items added to the list */
1007         return items;
1008 }       
1009
1010 // TODO: implement pinning... (if and when pinning is done, what we need to do is to provide freeing mechanisms - to protect against data that was deleted)
1011 static int animdata_filter_dopesheet (ListBase *anim_data, bDopeSheet *ads, int filter_mode)
1012 {
1013         Scene *sce= (Scene *)ads->source;
1014         Base *base;
1015         int items = 0;
1016         
1017         /* check that we do indeed have a scene */
1018         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1019                 printf("DopeSheet Error: Not scene! \n");
1020                 return 0;
1021         }
1022         
1023         /* loop over all bases in the scene */
1024         for (base= sce->base.first; base; base= base->next) {
1025                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1026                 if (base->object) {
1027                         Object *ob= base->object;
1028                         Key *key= ob_get_key(ob);
1029                         short actOk, keyOk, dataOk;
1030                         
1031                         /* firstly, check if object can be included, by the following fanimors:
1032                          *      - if only visible, must check for layer and also viewport visibility
1033                          *      - if only selected, must check if object is selected 
1034                          *      - there must be animation data to edit
1035                          */
1036                         // TODO: if cache is implemented, just check name here, and then 
1037                         if (filter_mode & ANIMFILTER_VISIBLE) {
1038                                 /* layer visibility */
1039                                 if ((ob->lay & sce->lay)==0) continue;
1040                                 
1041                                 /* outliner restrict-flag */
1042                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1043                         }
1044                         
1045                         /* additionally, dopesheet filtering also affects what objects to consider */
1046                         if (ads->filterflag) {
1047                                 /* check selection and object type filters */
1048                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1049                                         /* only selected should be shown */
1050                                         continue;
1051                                 }
1052 #if 0
1053                                 if ((ads->filterflag & ADS_FILTER_NOARM) && (ob->type == OB_ARMATURE)) {
1054                                         /* not showing armatures  */
1055                                         continue;
1056                                 }
1057                                 if ((ads->filterflag & ADS_FILTER_NOOBJ) && (ob->type != OB_ARMATURE)) {
1058                                         /* not showing objects that aren't armatures */
1059                                         continue;
1060                                 }
1061 #endif
1062                                 
1063                                 /* check filters for datatypes */
1064                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {
1065                                         actOk= (ANIMDATA_HAS_DRIVERS(ob));
1066                                         keyOk= ((key) && ANIMDATA_HAS_DRIVERS(key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS));
1067                                 }
1068                                 else {
1069                                         actOk= (ANIMDATA_HAS_KEYS(ob) /*&& !(ads->filterflag & ADS_FILTER_NOACTS)*/);
1070                                         keyOk= ((key) && ANIMDATA_HAS_KEYS(key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS));
1071                                 }
1072                                 
1073                                 switch (ob->type) {
1074                                         case OB_CAMERA: /* ------- Camera ------------ */
1075                                         {
1076                                                 Camera *ca= (Camera *)ob->data;
1077                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1078                                                         dataOk= (ANIMDATA_HAS_DRIVERS(ca) && !(ads->filterflag & ADS_FILTER_NOCAM));
1079                                                 else
1080                                                         dataOk= (ANIMDATA_HAS_KEYS(ca) && !(ads->filterflag & ADS_FILTER_NOCAM));                                               
1081                                         }
1082                                                 break;
1083                                         case OB_LAMP: /* ---------- Lamp ----------- */
1084                                         {
1085                                                 Lamp *la= (Lamp *)ob->data;
1086                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1087                                                         dataOk= (ANIMDATA_HAS_DRIVERS(la) && !(ads->filterflag & ADS_FILTER_NOLAM));
1088                                                 else
1089                                                         dataOk= (ANIMDATA_HAS_KEYS(la) && !(ads->filterflag & ADS_FILTER_NOLAM));       
1090                                         }
1091                                                 break;
1092                                         case OB_CURVE: /* -------- Curve ---------- */
1093                                         {
1094                                                 Curve *cu= (Curve *)ob->data;
1095                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1096                                                         dataOk= (ANIMDATA_HAS_DRIVERS(cu) && !(ads->filterflag & ADS_FILTER_NOCUR));
1097                                                 else
1098                                                         dataOk= (ANIMDATA_HAS_KEYS(cu) && !(ads->filterflag & ADS_FILTER_NOCUR));       
1099                                         }
1100                                                 break;
1101                                         default: /* --- other --- */
1102                                                 dataOk= 0;
1103                                                 break;
1104                                 }
1105                                 
1106                                 /* check if all bad (i.e. nothing to show) */
1107                                 if (!actOk && !keyOk && !dataOk)
1108                                         continue;
1109                         }
1110                         else {
1111                                 /* check data-types */
1112                                 actOk= ANIMDATA_HAS_KEYS(ob);
1113                                 keyOk= (key != NULL);
1114                                 
1115                                 switch (ob->type) {
1116                                         case OB_CAMERA: /* ------- Camera ------------ */
1117                                         {
1118                                                 Camera *ca= (Camera *)ob->data;
1119                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1120                                         }
1121                                                 break;
1122                                         case OB_LAMP: /* ---------- Lamp ----------- */
1123                                         {
1124                                                 Lamp *la= (Lamp *)ob->data;
1125                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1126                                         }
1127                                                 break;
1128                                         case OB_CURVE: /* -------- Curve ---------- */
1129                                         {
1130                                                 Curve *cu= (Curve *)ob->data;
1131                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1132                                         }
1133                                                 break;
1134                                         default: /* --- other --- */
1135                                                 dataOk= 0;
1136                                                 break;
1137                                 }
1138                                 
1139                                 /* check if all bad (i.e. nothing to show) */
1140                                 if (!actOk && !keyOk && !dataOk)
1141                                         continue;
1142                         }
1143                         
1144                         /* since we're still here, this object should be usable */
1145                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1146                 }
1147         }
1148         
1149         /* return the number of items in the list */
1150         return items;
1151 }
1152
1153 /* ----------- Public API --------------- */
1154
1155 /* This function filters the active data source to leave only animation channels suitable for
1156  * usage by the caller. It will return the length of the list 
1157  * 
1158  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
1159  *              will be placed for use.
1160  *      filter_mode: how should the data be filtered - bitmapping accessed flags
1161  */
1162 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
1163 {
1164         int items = 0;
1165         
1166         /* only filter data if there's somewhere to put it */
1167         if (data && anim_data) {
1168                 bAnimListElem *ale, *next;
1169                 Object *obact= (ac) ? ac->obact : NULL;
1170                 
1171                 /* firstly filter the data */
1172                 switch (datatype) {
1173                         case ANIMCONT_ACTION:
1174                                 items= animdata_filter_action(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1175                                 break;
1176                                 
1177                         case ANIMCONT_SHAPEKEY:
1178                                 items= animdata_filter_shapekey(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1179                                 break;
1180                                 
1181                         case ANIMCONT_GPENCIL:
1182                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
1183                                 break;
1184                                 
1185                         case ANIMCONT_DOPESHEET:
1186                         case ANIMCONT_FCURVES:
1187                         case ANIMCONT_DRIVERS:
1188                                 items= animdata_filter_dopesheet(anim_data, data, filter_mode);
1189                                 break;
1190                 }
1191                         
1192                 /* remove any weedy entries */
1193                 // XXX this is weedy code!
1194                 for (ale= anim_data->first; ale; ale= next) {
1195                         next= ale->next;
1196                         
1197                         if (ale->type == ANIMTYPE_NONE) {
1198                                 items--;
1199                                 BLI_freelinkN(anim_data, ale);
1200                         }
1201                 }
1202         }
1203         
1204         /* return the number of items in the list */
1205         return items;
1206 }
1207
1208 /* ************************************************************ */