NLA SoC: Merge from 2.5 20441 to 20570 (HEAD)
[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 #include "DNA_world_types.h"
70
71 #include "MEM_guardedalloc.h"
72
73 #include "BLI_blenlib.h"
74
75 #include "BKE_context.h"
76 #include "BKE_global.h"
77 #include "BKE_key.h"
78 #include "BKE_object.h"
79 #include "BKE_material.h"
80 #include "BKE_screen.h"
81 #include "BKE_utildefines.h"
82
83 #include "ED_anim_api.h"
84 #include "ED_types.h"
85 #include "ED_util.h"
86
87 #include "WM_api.h"
88 #include "WM_types.h"
89
90 #include "BIF_gl.h"
91 #include "BIF_glutil.h"
92
93 #include "UI_interface.h"
94 #include "UI_resources.h"
95 #include "UI_view2d.h"
96
97 /* ************************************************************ */
98 /* Blender Context <-> Animation Context mapping */
99
100 /* ----------- Private Stuff - Action Editor ------------- */
101
102 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
103 /* Note: there's a similar function in key.c (ob_get_key) */
104 Key *actedit_get_shapekeys (bAnimContext *ac, SpaceAction *saction) 
105 {
106     Scene *scene= ac->scene;
107     Object *ob;
108     Key *key;
109         
110     ob = OBACT;
111     if (ob == NULL) 
112                 return NULL;
113         
114         /* XXX pinning is not available in 'ShapeKey' mode... */
115         //if (saction->pin) return NULL;
116         
117         /* shapekey data is stored with geometry data */
118         switch (ob->type) {
119                 case OB_MESH:
120                         key= ((Mesh *)ob->data)->key;
121                         break;
122                         
123                 case OB_LATTICE:
124                         key= ((Lattice *)ob->data)->key;
125                         break;
126                         
127                 case OB_CURVE:
128                 case OB_SURF:
129                         key= ((Curve *)ob->data)->key;
130                         break;
131                         
132                 default:
133                         return NULL;
134         }
135         
136         if (key) {
137                 if (key->type == KEY_RELATIVE)
138                         return key;
139         }
140         
141     return NULL;
142 }
143
144 /* Get data being edited in Action Editor (depending on current 'mode') */
145 static short actedit_get_context (bAnimContext *ac, SpaceAction *saction)
146 {
147         /* sync settings with current view status, then return appropriate data */
148         switch (saction->mode) {
149                 case SACTCONT_ACTION: /* 'Action Editor' */
150                         /* if not pinned, sync with active object */
151                         if (saction->pin == 0) {
152                                 if (ac->obact && ac->obact->adt)
153                                         saction->action = ac->obact->adt->action;
154                                 else
155                                         saction->action= NULL;
156                         }
157                         
158                         ac->datatype= ANIMCONT_ACTION;
159                         ac->data= saction->action;
160                         
161                         ac->mode= saction->mode;
162                         return 1;
163                         
164                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
165                         ac->datatype= ANIMCONT_SHAPEKEY;
166                         ac->data= actedit_get_shapekeys(ac, saction);
167                         
168                         ac->mode= saction->mode;
169                         return 1;
170                         
171                 case SACTCONT_GPENCIL: /* Grease Pencil */ // XXX review how this mode is handled...
172                         ac->datatype=ANIMCONT_GPENCIL;
173                         //ac->data= CTX_wm_screen(C); // FIXME: add that dopesheet type thing here!
174                         ac->data= NULL; // !!!
175                         
176                         ac->mode= saction->mode;
177                         return 1;
178                         
179                 case SACTCONT_DOPESHEET: /* DopeSheet */
180                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
181                         saction->ads.source= (ID *)ac->scene;
182                         
183                         ac->datatype= ANIMCONT_DOPESHEET;
184                         ac->data= &saction->ads;
185                         
186                         ac->mode= saction->mode;
187                         return 1;
188                 
189                 default: /* unhandled yet */
190                         ac->datatype= ANIMCONT_NONE;
191                         ac->data= NULL;
192                         
193                         ac->mode= -1;
194                         return 0;
195         }
196 }
197
198 /* ----------- Private Stuff - Graph Editor ------------- */
199
200 /* Get data being edited in Graph Editor (depending on current 'mode') */
201 static short graphedit_get_context (bAnimContext *ac, SpaceIpo *sipo)
202 {
203         /* init dopesheet data if non-existant (i.e. for old files) */
204         if (sipo->ads == NULL)
205                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
206         
207         /* sync settings with current view status, then return appropriate data */
208         switch (sipo->mode) {
209                 case SIPO_MODE_ANIMATION:       /* Animation F-Curve Editor */
210                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
211                         sipo->ads->source= (ID *)ac->scene;
212                         sipo->ads->filterflag &= ~ADS_FILTER_ONLYDRIVERS;
213                         
214                         ac->datatype= ANIMCONT_FCURVES;
215                         ac->data= sipo->ads;
216                         
217                         ac->mode= sipo->mode;
218                         return 1;
219                 
220                 case SIPO_MODE_DRIVERS:         /* Driver F-Curve Editor */
221                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
222                         sipo->ads->source= (ID *)ac->scene;
223                         sipo->ads->filterflag |= ADS_FILTER_ONLYDRIVERS;
224                         
225                         ac->datatype= ANIMCONT_DRIVERS;
226                         ac->data= sipo->ads;
227                         
228                         ac->mode= sipo->mode;
229                         return 1;
230                 
231                 default: /* unhandled yet */
232                         ac->datatype= ANIMCONT_NONE;
233                         ac->data= NULL;
234                         
235                         ac->mode= -1;
236                         return 0;
237         }
238 }
239
240 /* ----------- Private Stuff - NLA Editor ------------- */
241
242 /* Get data being edited in Graph Editor (depending on current 'mode') */
243 static short nlaedit_get_context (bAnimContext *ac, SpaceNla *snla)
244 {
245         /* init dopesheet data if non-existant (i.e. for old files) */
246         if (snla->ads == NULL)
247                 snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
248         
249         /* sync settings with current view status, then return appropriate data */
250         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
251         snla->ads->source= (ID *)ac->scene;
252         snla->ads->filterflag |= ADS_FILTER_ONLYNLA;
253         
254         ac->datatype= ANIMCONT_NLA;
255         ac->data= snla->ads;
256         
257         return 1;
258 }
259
260 /* ----------- Public API --------------- */
261
262 /* Obtain current anim-data context, given that context info from Blender context has already been set 
263  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
264  *        allocation/freeing costs (which are not that avoidable with channels).
265  */
266 short ANIM_animdata_context_getdata (bAnimContext *ac)
267 {
268         ScrArea *sa= ac->sa;
269         short ok= 0;
270         
271         /* context depends on editor we are currently in */
272         if (sa) {
273                 switch (sa->spacetype) {
274                         case SPACE_ACTION:
275                         {
276                                 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
277                                 ok= actedit_get_context(ac, saction);
278                         }
279                                 break;
280                                 
281                         case SPACE_IPO:
282                         {
283                                 SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
284                                 ok= graphedit_get_context(ac, sipo);
285                         }
286                                 break;
287                                 
288                         case SPACE_NLA:
289                         {
290                                 SpaceNla *snla= (SpaceNla *)sa->spacedata.first;
291                                 ok= nlaedit_get_context(ac, snla);
292                         }
293                                 break;
294                 }
295         }
296         
297         /* check if there's any valid data */
298         if (ok && ac->data)
299                 return 1;
300         else
301                 return 0;
302 }
303
304 /* Obtain current anim-data context from Blender Context info 
305  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
306  *        allocation/freeing costs (which are not that avoidable with channels).
307  *      - Clears data and sets the information from Blender Context which is useful
308  */
309 short ANIM_animdata_get_context (const bContext *C, bAnimContext *ac)
310 {
311         ScrArea *sa= CTX_wm_area(C);
312         ARegion *ar= CTX_wm_region(C);
313         Scene *scene= CTX_data_scene(C);
314         
315         /* clear old context info */
316         if (ac == NULL) return 0;
317         memset(ac, 0, sizeof(bAnimContext));
318         
319         /* get useful default context settings from context */
320         ac->scene= scene;
321         if (scene) {
322                 ac->markers= &scene->markers;           
323                 ac->obact= (scene->basact)?  scene->basact->object : NULL;
324         }
325         ac->sa= sa;
326         ac->ar= ar;
327         ac->spacetype= (sa) ? sa->spacetype : 0;
328         ac->regiontype= (ar) ? ar->regiontype : 0;
329         
330         /* get data context info */
331         return ANIM_animdata_context_getdata(ac);
332 }
333
334 /* ************************************************************ */
335 /* Blender Data <-- Filter --> Channels to be operated on */
336
337 /* quick macro to test if AnimData is usable */
338 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
339
340 /* quick macro to test if AnimData is usable for drivers */
341 #define ANIMDATA_HAS_DRIVERS(id) ((id)->adt && (id)->adt->drivers.first)
342
343 /* quick macro to test if AnimData is usable for NLA */
344 #define ANIMDATA_HAS_NLA(id) ((id)->adt && (id)->adt->nla_tracks.first)
345
346 /* quick macro to test for all three avove usability tests, performing the appropriate provided 
347  * action for each when the AnimData context is appropriate. 
348  *
349  * Priority order for this goes (most important, to least): NLA, Drivers, Keyframes
350  *
351  *      - id: ID block which should have an AnimData pointer following it immediately, to use
352  *      - nlaOk: line or block of code to execute for NLA case
353  *      - driversOk: line or block of code to execute for Drivers case
354  *      - keysOk: line or block of code for Keyframes case
355  */
356 #define ANIMDATA_FILTER_CASES(id, nlaOk, driversOk, keysOk) \
357         {\
358                 if (ads->filterflag & ADS_FILTER_ONLYNLA) {\
359                         if (ANIMDATA_HAS_NLA(id)) {\
360                                 nlaOk\
361                         }\
362                         else if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) && ANIMDATA_HAS_KEYS(id)) {\
363                                 nlaOk\
364                         }\
365                 }\
366                 else if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {\
367                         if (ANIMDATA_HAS_DRIVERS(id)) {\
368                                 driversOk\
369                         }\
370                 }\
371                 else {\
372                         if (ANIMDATA_HAS_KEYS(id)) {\
373                                 keysOk\
374                         }\
375                 }\
376         }
377
378
379 /* quick macro to test if a anim-channel (F-Curve, Group, etc.) is selected in an acceptable way */
380 #define ANIMCHANNEL_SELOK(test_func) \
381                 ( !(filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) || \
382                   ((filter_mode & ANIMFILTER_SEL) && test_func) || \
383                   ((filter_mode & ANIMFILTER_UNSEL) && test_func==0) ) 
384
385 /* ----------- 'Private' Stuff --------------- */
386
387 /* this function allocates memory for a new bAnimListElem struct for the 
388  * provided animation channel-data. 
389  */
390 bAnimListElem *make_new_animlistelem (void *data, short datatype, void *owner, short ownertype, ID *owner_id)
391 {
392         bAnimListElem *ale= NULL;
393         
394         /* only allocate memory if there is data to convert */
395         if (data) {
396                 /* allocate and set generic data */
397                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
398                 
399                 ale->data= data;
400                 ale->type= datatype;
401                         // XXX what is the point of the owner data?
402                 ale->owner= owner;
403                 ale->ownertype= ownertype;
404                 
405                 ale->id= owner_id;
406                 
407                 /* do specifics */
408                 switch (datatype) {
409                         case ANIMTYPE_SCENE:
410                         {
411                                 Scene *sce= (Scene *)data;
412                                 
413                                 ale->flag= sce->flag;
414                                 
415                                 ale->key_data= sce;
416                                 ale->datatype= ALE_SCE;
417                         }
418                                 break;
419                         case ANIMTYPE_OBJECT:
420                         {
421                                 Base *base= (Base *)data;
422                                 Object *ob= base->object;
423                                 
424                                 ale->flag= ob->flag;
425                                 
426                                 ale->key_data= ob;
427                                 ale->datatype= ALE_OB;
428                         }
429                                 break;
430                         case ANIMTYPE_FILLACTD:
431                         {
432                                 bAction *act= (bAction *)data;
433                                 
434                                 ale->flag= act->flag;
435                                 
436                                 ale->key_data= act;
437                                 ale->datatype= ALE_ACT;
438                         }
439                                 break;
440                         case ANIMTYPE_FILLDRIVERS:
441                         {
442                                 AnimData *adt= (AnimData *)data;
443                                 
444                                 ale->flag= adt->flag;
445                                 
446                                         // XXX... drivers don't show summary for now
447                                 ale->key_data= NULL;
448                                 ale->datatype= ALE_NONE;
449                         }
450                                 break;
451                         case ANIMTYPE_FILLMATD:
452                         {
453                                 Object *ob= (Object *)data;
454                                 
455                                 ale->flag= FILTER_MAT_OBJC(ob);
456                                 
457                                 ale->key_data= NULL;
458                                 ale->datatype= ALE_NONE;
459                         }
460                                 break;
461                         
462                         case ANIMTYPE_DSMAT:
463                         {
464                                 Material *ma= (Material *)data;
465                                 AnimData *adt= ma->adt;
466                                 
467                                 ale->flag= FILTER_MAT_OBJD(ma);
468                                 
469                                 ale->key_data= (adt) ? adt->action : NULL;
470                                 ale->datatype= ALE_ACT;
471                         }
472                                 break;
473                         case ANIMTYPE_DSLAM:
474                         {
475                                 Lamp *la= (Lamp *)data;
476                                 AnimData *adt= la->adt;
477                                 
478                                 ale->flag= FILTER_LAM_OBJD(la);
479                                 
480                                 ale->key_data= (adt) ? adt->action : NULL;
481                                 ale->datatype= ALE_ACT;
482                         }
483                                 break;
484                         case ANIMTYPE_DSCAM:
485                         {
486                                 Camera *ca= (Camera *)data;
487                                 AnimData *adt= ca->adt;
488                                 
489                                 ale->flag= FILTER_CAM_OBJD(ca);
490                                 
491                                 ale->key_data= (adt) ? adt->action : NULL;
492                                 ale->datatype= ALE_ACT;
493                         }
494                                 break;
495                         case ANIMTYPE_DSCUR:
496                         {
497                                 Curve *cu= (Curve *)data;
498                                 AnimData *adt= cu->adt;
499                                 
500                                 ale->flag= FILTER_CUR_OBJD(cu);
501                                 
502                                 ale->key_data= (adt) ? adt->action : NULL;
503                                 ale->datatype= ALE_ACT;
504                         }
505                                 break;
506                         case ANIMTYPE_DSSKEY:
507                         {
508                                 Key *key= (Key *)data;
509                                 AnimData *adt= key->adt;
510                                 
511                                 ale->flag= FILTER_SKE_OBJD(key); 
512                                 
513                                 ale->key_data= (adt) ? adt->action : NULL;
514                                 ale->datatype= ALE_ACT;
515                         }
516                                 break;
517                         case ANIMTYPE_DSWOR:
518                         {
519                                 World *wo= (World *)data;
520                                 AnimData *adt= wo->adt;
521                                 
522                                 ale->flag= FILTER_WOR_SCED(wo); 
523                                 
524                                 ale->key_data= (adt) ? adt->action : NULL;
525                                 ale->datatype= ALE_ACT;
526                         }
527                                 break;
528                                 
529                         case ANIMTYPE_GROUP:
530                         {
531                                 bActionGroup *agrp= (bActionGroup *)data;
532                                 
533                                 ale->flag= agrp->flag;
534                                 
535                                 ale->key_data= NULL;
536                                 ale->datatype= ALE_GROUP;
537                         }
538                                 break;
539                         case ANIMTYPE_FCURVE:
540                         {
541                                 FCurve *fcu= (FCurve *)data;
542                                 
543                                 ale->flag= fcu->flag;
544                                 
545                                 ale->key_data= fcu;
546                                 ale->datatype= ALE_FCURVE;
547                         }
548                                 break;
549                         
550                         case ANIMTYPE_GPLAYER:
551                         {
552                                 bGPDlayer *gpl= (bGPDlayer *)data;
553                                 
554                                 ale->flag= gpl->flag;
555                                 
556                                 ale->key_data= NULL;
557                                 ale->datatype= ALE_GPFRAME;
558                         }
559                                 break;
560                                 
561                         case ANIMTYPE_NLATRACK:
562                         {
563                                 NlaTrack *nlt= (NlaTrack *)data;
564                                 
565                                 ale->flag= nlt->flag;
566                                 
567                                         // XXX or should this be done some other way?
568                                 ale->key_data= &nlt->strips;
569                                 ale->datatype= ALE_NLASTRIP;
570                         }
571                                 break;
572                         case ANIMTYPE_NLAACTION:
573                         {
574                                 /* nothing to include for now... nothing editable from NLA-perspective here */
575                                 ale->key_data= NULL;
576                                 ale->datatype= ALE_NONE;
577                         }
578                                 break;
579                 }
580         }
581         
582         /* return created datatype */
583         return ale;
584 }
585  
586 /* ----------------------------------------- */
587
588
589 static int animdata_filter_fcurves (ListBase *anim_data, FCurve *first, bActionGroup *grp, void *owner, short ownertype, int filter_mode, ID *owner_id)
590 {
591         bAnimListElem *ale = NULL;
592         FCurve *fcu;
593         int items = 0;
594         
595         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
596          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
597          */
598         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
599                 /* only include if visible (Graph Editor check, not channels check) */
600                 if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
601                         /* only work with this channel and its subchannels if it is editable */
602                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
603                                 /* only include this curve if selected in a way consistent with the filtering requirements */
604                                 if ( ANIMCHANNEL_SELOK(SEL_FCU(fcu)) ) {
605                                         /* only include if this curve is active */
606                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
607                                                 ale= make_new_animlistelem(fcu, ANIMTYPE_FCURVE, owner, ownertype, owner_id);
608                                                 
609                                                 if (ale) {
610                                                         BLI_addtail(anim_data, ale);
611                                                         items++;
612                                                 }
613                                         }
614                                 }
615                         }
616                 }
617         }
618         
619         /* return the number of items added to the list */
620         return items;
621 }
622
623 static int animdata_filter_action (ListBase *anim_data, bAction *act, int filter_mode, void *owner, short ownertype, ID *owner_id)
624 {
625         bAnimListElem *ale=NULL;
626         bActionGroup *agrp;
627         FCurve *lastchan=NULL;
628         int items = 0;
629         
630         /* loop over groups */
631         //       XXX in future, we need to be prepared for nestled groups...
632         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
633                 /* add this group as a channel first */
634                 if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
635                         /* check if filtering by selection */
636                         if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
637                                 ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE, owner_id);
638                                 if (ale) {
639                                         BLI_addtail(anim_data, ale);
640                                         items++;
641                                 }
642                         }
643                 }
644                 
645                 /* store reference to last channel of group */
646                 if (agrp->channels.last) 
647                         lastchan= agrp->channels.last;
648                 
649                 
650                 /* there are some situations, where only the channels of the action group should get considered */
651                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
652                         /* filters here are a bit convoulted...
653                          *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
654                          *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
655                          *
656                          * cases when we should include F-Curves inside group:
657                          *      - we don't care about visibility
658                          *      - group is expanded
659                          *      - we're interested in keyframes, but not if they appear in selected channels
660                          */
661                         // XXX what was the selection check here for again?
662                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || 
663                                  ( /*ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) &&*/ (filter_mode & ANIMFILTER_CURVESONLY) ) ) 
664                         {
665                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
666                                         // XXX the 'owner' info here needs review...
667                                         items += animdata_filter_fcurves(anim_data, agrp->channels.first, agrp, owner, ownertype, filter_mode, owner_id);
668                                         
669                                         /* remove group from filtered list if last element is group 
670                                          * (i.e. only if group had channels, which were all hidden)
671                                          */
672                                         // XXX this is really hacky... it should be fixed in a much more elegant way!
673                                         if ( (ale) && (anim_data->last == ale) && 
674                                                  (ale->data == agrp) && (agrp->channels.first) ) 
675                                         {
676                                                 BLI_freelinkN(anim_data, ale);
677                                                 items--;
678                                         }
679                                 }
680                         }
681                 }
682         }
683         
684         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
685         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
686                 // XXX the 'owner' info here needs review...
687                 items += animdata_filter_fcurves(anim_data, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
688         }
689         
690         /* return the number of items added to the list */
691         return items;
692 }
693
694 static int animdata_filter_nla (ListBase *anim_data, AnimData *adt, int filter_mode, void *owner, short ownertype, ID *owner_id)
695 {
696         bAnimListElem *ale;
697         NlaTrack *nlt;
698         int items = 0;
699         
700         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
701         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
702                 /* only work with this channel and its subchannels if it is editable */
703                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
704                         /* only include this track if selected in a way consistent with the filtering requirements */
705                         if ( ANIMCHANNEL_SELOK(SEL_NLT(nlt)) ) {
706                                 /* only include if this track is active */
707                                 // XXX keep this?
708                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
709                                         ale= make_new_animlistelem(nlt, ANIMTYPE_NLATRACK, owner, ownertype, owner_id);
710                                                 
711                                         if (ale) {
712                                                 BLI_addtail(anim_data, ale);
713                                                 items++;
714                                         }
715                                 }
716                         }
717                 }
718         }
719         
720         /* if showing channels, include active action */
721         if (filter_mode & ANIMFILTER_CHANNELS) {
722                 /* there isn't really anything editable here, so skip if need editable */
723                 // TODO: currently, selection isn't checked since it doesn't matter
724                 if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
725                         /* just add the action track now (this MUST appear for drawing)
726                          *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
727                          *        overwrite this with the real value - REVIEW THIS...
728                          */
729                         ale= make_new_animlistelem((void *)(&adt->action), ANIMTYPE_NLAACTION, owner, ownertype, owner_id);
730                         ale->data= (adt->action) ? adt->action : NULL;
731                                 
732                         if (ale) {
733                                 BLI_addtail(anim_data, ale);
734                                 items++;
735                         }
736                 }
737         }
738         
739         /* return the number of items added to the list */
740         return items;
741 }
742
743 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode, void *owner, short ownertype, ID *owner_id)
744 {
745         bAnimListElem *ale;
746         KeyBlock *kb;
747         //FCurve *fcu;
748         int i, items=0;
749         
750         /* are we filtering for display or editing */
751         if (filter_mode & ANIMFILTER_CHANNELS) {
752                 /* for display - loop over shapekeys, adding ipo-curve references where needed */
753                 kb= key->block.first;
754                 
755                 /* loop through possible shapekeys, manually creating entries */
756                 for (i= 1; i < key->totkey; i++) {
757                         ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
758                         kb = kb->next; /* do this even on the first try, as the first is 'Basis' (which doesn't get included) */
759                         
760                         ale->data= kb;
761                         ale->type= ANIMTYPE_SHAPEKEY; /* 'abused' usage of this type */
762                         ale->owner= key;
763                         ale->ownertype= ANIMTYPE_SHAPEKEY;
764                         ale->datatype= ALE_NONE;
765                         ale->index = i;
766                         
767 #if 0 // XXX fixme... old system
768                         if (key->ipo) {
769                                 for (icu= key->ipo->curve.first; icu; icu=icu->next) {
770                                         if (icu->adrcode == i) {
771                                                 ale->key_data= icu;
772                                                 ale->datatype= ALE_ICU;
773                                                 break;
774                                         }
775                                 }
776                         }
777 #endif // XXX fixme... old system
778                         
779                         ale->id= owner_id;
780                         
781                         BLI_addtail(anim_data, ale);
782                         items++;
783                 }
784         }
785         else {
786 #if 0 // XXX fixme... old system
787                 /* loop over ipo curves if present - for editing */
788                 if (key->ipo) {
789                         if (filter_mode & ANIMFILTER_IPOKEYS) {
790                                 ale= make_new_animlistelem(key->ipo, ANIMTYPE_IPO, key, ANIMTYPE_SHAPEKEY);
791                                 if (ale) {
792                                         if (owned) ale->id= owner;
793                                         BLI_addtail(anim_data, ale);
794                                         items++;
795                                 }
796                         }
797                         else {
798                                 items += animdata_filter_ipocurves(anim_data, key->ipo, filter_mode, key, ANIMTYPE_SHAPEKEY, (owned)?(owner):(NULL));
799                         }
800                 }
801 #endif // XXX fixme... old system
802         }
803         
804         /* return the number of items added to the list */
805         return items;
806 }
807  
808 #if 0
809 // FIXME: switch this to use the bDopeSheet...
810 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
811 {
812         bAnimListElem *ale;
813         ScrArea *sa, *curarea;
814         bGPdata *gpd;
815         bGPDlayer *gpl;
816         int items = 0;
817         
818         /* check if filtering types are appropriate */
819         {
820                 /* special hack for fullscreen area (which must be this one then):
821                  *      - we use the curarea->full as screen to get spaces from, since the
822                  *        old (pre-fullscreen) screen was stored there...
823                  *      - this is needed as all data would otherwise disappear
824                  */
825                 // XXX need to get new alternative for curarea
826                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
827                         sc= curarea->full;
828                 
829                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
830                 for (sa= sc->areabase.first; sa; sa= sa->next) {
831                         /* try to get gp data */
832                         // XXX need to put back grease pencil api...
833                         gpd= gpencil_data_getactive(sa);
834                         if (gpd == NULL) continue;
835                         
836                         /* add gpd as channel too (if for drawing, and it has layers) */
837                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
838                                 /* add to list */
839                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
840                                 if (ale) {
841                                         BLI_addtail(anim_data, ale);
842                                         items++;
843                                 }
844                         }
845                         
846                         /* only add layers if they will be visible (if drawing channels) */
847                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
848                                 /* loop over layers as the conditions are acceptable */
849                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
850                                         /* only if selected */
851                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
852                                                 /* only if editable */
853                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
854                                                         /* add to list */
855                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
856                                                         if (ale) {
857                                                                 BLI_addtail(anim_data, ale);
858                                                                 items++;
859                                                         }
860                                                 }
861                                         }
862                                 }
863                         }
864                 }
865         }
866         
867         /* return the number of items added to the list */
868         return items;
869 }
870 #endif 
871
872
873 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
874 {
875         ListBase mats = {NULL, NULL};
876         LinkData *ld;
877         
878         bAnimListElem *ale=NULL;
879         Object *ob= base->object;
880         int items=0, a=0;
881         
882         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
883         for (a=0; a < ob->totcol; a++) {
884                 Material *ma= give_current_material(ob, a);
885                 short ok = 0;
886                 
887                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
888                 if (ELEM(NULL, ma, ma->adt)) 
889                         continue;
890                 
891                 /* check if ok */
892                 ANIMDATA_FILTER_CASES(ma, 
893                         ok=1;, 
894                         ok=1;, 
895                         ok=1;)
896                 if (ok == 0) continue;
897                 
898                 /* make a temp list elem for this */
899                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
900                 ld->data= ma;
901                 BLI_addtail(&mats, ld);
902         }
903         
904         /* if there were no channels found, no need to carry on */
905         if (mats.first == NULL)
906                 return 0;
907         
908         /* include materials-expand widget? */
909         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
910                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
911                 if (ale) {
912                         BLI_addtail(anim_data, ale);
913                         items++;
914                 }
915         }
916         
917         /* add materials? */
918         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
919                 /* for each material in cache, add channels  */
920                 for (ld= mats.first; ld; ld= ld->next) {
921                         Material *ma= (Material *)ld->data;
922                         
923                         /* include material-expand widget? */
924                         // hmm... do we need to store the index of this material in the array anywhere?
925                         if (filter_mode & ANIMFILTER_CHANNELS) {
926                                 ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
927                                 if (ale) {
928                                         BLI_addtail(anim_data, ale);
929                                         items++;
930                                 }
931                         }
932                         
933                         /* add material's animation data */
934                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
935                                 ANIMDATA_FILTER_CASES(ma, 
936                                         items += animdata_filter_nla(anim_data, ma->adt, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
937                                         items += animdata_filter_fcurves(anim_data, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
938                                         items += animdata_filter_action(anim_data, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
939                         }
940                 }
941         }
942         
943         /* free cache */
944         BLI_freelistN(&mats);
945         
946         /* return the number of items added to the list */
947         return items;
948 }
949
950 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
951 {
952         bAnimListElem *ale=NULL;
953         Object *ob= base->object;
954         IdAdtTemplate *iat= ob->data;
955         AnimData *adt= iat->adt;
956         short type=0, expanded=0;
957         int items= 0;
958         
959         /* get settings based on data type */
960         switch (ob->type) {
961                 case OB_CAMERA: /* ------- Camera ------------ */
962                 {
963                         Camera *ca= (Camera *)ob->data;
964                         
965                         type= ANIMTYPE_DSCAM;
966                         expanded= FILTER_CAM_OBJD(ca);
967                 }
968                         break;
969                 case OB_LAMP: /* ---------- Lamp ----------- */
970                 {
971                         Lamp *la= (Lamp *)ob->data;
972                         
973                         type= ANIMTYPE_DSLAM;
974                         expanded= FILTER_LAM_OBJD(la);
975                 }
976                         break;
977                 case OB_CURVE: /* ------- Curve ---------- */
978                 {
979                         Curve *cu= (Curve *)ob->data;
980                         
981                         type= ANIMTYPE_DSCUR;
982                         expanded= FILTER_CUR_OBJD(cu);
983                 }
984                         break;
985         }
986         
987         /* special exception for drivers instead of action */
988         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
989                 expanded= EXPANDED_DRVD(adt);
990         
991         /* include data-expand widget? */
992         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {               
993                 ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
994                 if (ale) BLI_addtail(anim_data, ale);
995         }
996         
997         /* add object-data animation channels? */
998         if ((expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
999                 /* filtering for channels - nla, drivers, keyframes */
1000                 ANIMDATA_FILTER_CASES(iat, 
1001                         items+= animdata_filter_nla(anim_data, iat->adt, filter_mode, iat, type, (ID *)iat);,
1002                         items+= animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
1003                         items += animdata_filter_action(anim_data, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
1004         }
1005         
1006         /* return the number of items added to the list */
1007         return items;
1008 }
1009
1010 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1011 {
1012         bAnimListElem *ale=NULL;
1013         AnimData *adt = NULL;
1014         Object *ob= base->object;
1015         Key *key= ob_get_key(ob);
1016         short obdata_ok = 0;
1017         int items = 0;
1018         
1019         /* add this object as a channel first */
1020         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
1021                 /* check if filtering by selection */
1022                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
1023                         ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, NULL);
1024                         if (ale) {
1025                                 BLI_addtail(anim_data, ale);
1026                                 items++;
1027                         }
1028                 }
1029         }
1030         
1031         /* if collapsed, don't go any further (unless adding keyframes only) */
1032         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & ANIMFILTER_CURVESONLY) )
1033                 return items;
1034         
1035         /* Action, Drivers, or NLA */
1036         if (ob->adt) {
1037                 adt= ob->adt;
1038                 ANIMDATA_FILTER_CASES(ob,
1039                         { /* nla */
1040 #if 0
1041                                 /* include nla-expand widget? */
1042                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1043                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_OBJECT, (ID *)ob);
1044                                         if (ale) {
1045                                                 BLI_addtail(anim_data, ale);
1046                                                 items++;
1047                                         }
1048                                 }
1049 #endif
1050                                 
1051                                 /* add NLA tracks */
1052                                 items += animdata_filter_nla(anim_data, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1053                         },
1054                         { /* drivers */
1055                                 /* include drivers-expand widget? */
1056                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1057                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1058                                         if (ale) {
1059                                                 BLI_addtail(anim_data, ale);
1060                                                 items++;
1061                                         }
1062                                 }
1063                                 
1064                                 /* add F-Curve channels (drivers are F-Curves) */
1065                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1066                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1067                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1068                                 }
1069                         },
1070                         { /* action (keyframes) */
1071                                 /* include action-expand widget? */
1072                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1073                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1074                                         if (ale) {
1075                                                 BLI_addtail(anim_data, ale);
1076                                                 items++;
1077                                         }
1078                                 }
1079                                 
1080                                 /* add F-Curve channels? */
1081                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1082                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1083                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1084                                 }
1085                         }
1086                 )
1087         }
1088         
1089         
1090         /* ShapeKeys? */
1091         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1092                 adt= key->adt;
1093                 ANIMDATA_FILTER_CASES(key,
1094                         { /* nla */
1095 #if 0
1096                                 /* include nla-expand widget? */
1097                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1098                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_OBJECT, (ID *)ob);
1099                                         if (ale) {
1100                                                 BLI_addtail(anim_data, ale);
1101                                                 items++;
1102                                         }
1103                                 }
1104 #endif
1105                                 
1106                                 /* add NLA tracks */
1107                                 items += animdata_filter_nla(anim_data, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1108                         },
1109                         { /* drivers */
1110                                 /* include shapekey-expand widget? */
1111                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1112                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1113                                         if (ale) {
1114                                                 BLI_addtail(anim_data, ale);
1115                                                 items++;
1116                                         }
1117                                 }
1118                                 
1119                                 /* add channels */
1120                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1121                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1122                                 }
1123                         },
1124                         { /* action (keyframes) */
1125                                 /* include shapekey-expand widget? */
1126                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1127                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1128                                         if (ale) {
1129                                                 BLI_addtail(anim_data, ale);
1130                                                 items++;
1131                                         }
1132                                 }
1133                                 
1134                                 /* add channels */
1135                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1136                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1137                                 }
1138                         }
1139                 )
1140         }
1141
1142         /* Materials? */
1143         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1144                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1145         
1146         /* Object Data */
1147         switch (ob->type) {
1148                 case OB_CAMERA: /* ------- Camera ------------ */
1149                 {
1150                         Camera *ca= (Camera *)ob->data;
1151                         
1152                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1153                                 ANIMDATA_FILTER_CASES(ca,
1154                                         obdata_ok= 1;,
1155                                         obdata_ok= 1;,
1156                                         obdata_ok= 1;)
1157                         }
1158                 }
1159                         break;
1160                 case OB_LAMP: /* ---------- Lamp ----------- */
1161                 {
1162                         Lamp *la= (Lamp *)ob->data;
1163                         
1164                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1165                                 ANIMDATA_FILTER_CASES(la,
1166                                         obdata_ok= 1;,
1167                                         obdata_ok= 1;,
1168                                         obdata_ok= 1;)
1169                         }
1170                 }
1171                         break;
1172                 case OB_CURVE: /* ------- Curve ---------- */
1173                 {
1174                         Curve *cu= (Curve *)ob->data;
1175                         
1176                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1177                                 ANIMDATA_FILTER_CASES(cu,
1178                                         obdata_ok= 1;,
1179                                         obdata_ok= 1;,
1180                                         obdata_ok= 1;)
1181                         }
1182                 }
1183                         break;
1184         }
1185         if (obdata_ok) 
1186                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1187         
1188         /* return the number of items added to the list */
1189         return items;
1190 }       
1191
1192 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1193 {
1194         World *wo= sce->world;
1195         AnimData *adt= NULL;
1196         bAnimListElem *ale;
1197         int items = 0;
1198         
1199         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1200         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
1201                 /* check if filtering by selection */
1202                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1203                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1204                         if (ale) {
1205                                 BLI_addtail(anim_data, ale);
1206                                 items++;
1207                         }
1208                 }
1209         }
1210         
1211         /* if collapsed, don't go any further (unless adding keyframes only) */
1212         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & ANIMFILTER_CURVESONLY) )
1213                 return items;
1214                 
1215         /* Action, Drivers, or NLA  for Scene */
1216         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1217                 adt= sce->adt;
1218                 ANIMDATA_FILTER_CASES(sce,
1219                         { /* nla */
1220 #if 0
1221                                 /* include nla-expand widget? */
1222                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1223                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_SCENE (ID *)sce);
1224                                         if (ale) {
1225                                                 BLI_addtail(anim_data, ale);
1226                                                 items++;
1227                                         }
1228                                 }
1229 #endif
1230                                 
1231                                 /* add NLA tracks */
1232                                 items += animdata_filter_nla(anim_data, adt, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1233                         },
1234                         { /* drivers */
1235                                 /* include drivers-expand widget? */
1236                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1237                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1238                                         if (ale) {
1239                                                 BLI_addtail(anim_data, ale);
1240                                                 items++;
1241                                         }
1242                                 }
1243                                 
1244                                 /* add F-Curve channels (drivers are F-Curves) */
1245                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1246                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1247                                 }
1248                         },
1249                         { /* action */
1250                                 /* include action-expand widget? */
1251                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1252                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1253                                         if (ale) {
1254                                                 BLI_addtail(anim_data, ale);
1255                                                 items++;
1256                                         }
1257                                 }
1258                                 
1259                                 /* add F-Curve channels? */
1260                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1261                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1262                                 }
1263                         }
1264                 )
1265         }
1266         
1267         /* world */
1268         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1269                 /* Action, Drivers, or NLA  for World */
1270                 adt= wo->adt;
1271                 ANIMDATA_FILTER_CASES(wo,
1272                         { /* nla */
1273 #if 0
1274                                 /* include nla-expand widget? */
1275                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1276                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_DSWOR (ID *)wo);
1277                                         if (ale) {
1278                                                 BLI_addtail(anim_data, ale);
1279                                                 items++;
1280                                         }
1281                                 }
1282 #endif
1283                                 
1284                                 /* add NLA tracks */
1285                                 items += animdata_filter_nla(anim_data, adt, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1286                         },
1287                         { /* drivers */
1288                                 /* include world-expand widget? */
1289                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1290                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1291                                         if (ale) {
1292                                                 BLI_addtail(anim_data, ale);
1293                                                 items++;
1294                                         }
1295                                 }
1296                                 
1297                                 /* add F-Curve channels (drivers are F-Curves) */
1298                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1299                                         // XXX owner info is messed up now...
1300                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1301                                 }
1302                         },
1303                         { /* action */
1304                                 /* include world-expand widget? */
1305                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1306                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1307                                         if (ale) {
1308                                                 BLI_addtail(anim_data, ale);
1309                                                 items++;
1310                                         }
1311                                 }
1312                                 
1313                                 /* add channels */
1314                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1315                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1316                                 }
1317                         }
1318                 )
1319         }
1320         
1321         /* return the number of items added to the list */
1322         return items;
1323 }
1324
1325 // 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)
1326 static int animdata_filter_dopesheet (ListBase *anim_data, bDopeSheet *ads, int filter_mode)
1327 {
1328         Scene *sce= (Scene *)ads->source;
1329         Base *base;
1330         int items = 0;
1331         
1332         /* check that we do indeed have a scene */
1333         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1334                 printf("DopeSheet Error: Not scene! \n");
1335                 return 0;
1336         }
1337         
1338         /* scene-linked animation */
1339         // TODO: sequencer, composite nodes - are we to include those here too?
1340         {
1341                 short sceOk= 0, worOk= 0;
1342                 
1343                 /* check filtering-flags if ok */
1344                 ANIMDATA_FILTER_CASES(sce, 
1345                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1346                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1347                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);)
1348                 if (sce->world) {
1349                         ANIMDATA_FILTER_CASES(sce->world, 
1350                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1351                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1352                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);)
1353                 }
1354                 
1355                 /* check if not all bad (i.e. so there is something to show) */
1356                 if ( !(!sceOk && !worOk) ) {
1357                         /* add scene data to the list of filtered channels */
1358                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1359                 }
1360         }
1361         
1362         
1363         /* loop over all bases in the scene */
1364         for (base= sce->base.first; base; base= base->next) {
1365                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1366                 if (base->object) {
1367                         Object *ob= base->object;
1368                         Key *key= ob_get_key(ob);
1369                         short actOk=1, keyOk=1, dataOk=1, matOk=1;
1370                         
1371                         /* firstly, check if object can be included, by the following fanimors:
1372                          *      - if only visible, must check for layer and also viewport visibility
1373                          *      - if only selected, must check if object is selected 
1374                          *      - there must be animation data to edit
1375                          */
1376                         // TODO: if cache is implemented, just check name here, and then 
1377                         if (filter_mode & ANIMFILTER_VISIBLE) {
1378                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1379                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1380                                 
1381                                 /* outliner restrict-flag */
1382                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1383                         }
1384                         
1385                         /* additionally, dopesheet filtering also affects what objects to consider */
1386                         if (ads->filterflag) {
1387                                 /* check selection and object type filters */
1388                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1389                                         /* only selected should be shown */
1390                                         continue;
1391                                 }
1392                                 
1393                                 /* check filters for datatypes */
1394                                         /* object */
1395                                 actOk= 0;
1396                                 keyOk= 0;
1397                                 ANIMDATA_FILTER_CASES(ob, 
1398                                         actOk= 1;, 
1399                                         actOk= 1;, 
1400                                         actOk= 1;)
1401                                 if (key) {
1402                                         /* shapekeys */
1403                                         ANIMDATA_FILTER_CASES(key, 
1404                                                 keyOk= 1;, 
1405                                                 keyOk= 1;, 
1406                                                 keyOk= 1;)
1407                                 }
1408                                 
1409                                 /* materials - only for geometric types */
1410                                 matOk= 0; /* by default, not ok... */
1411                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1412                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1413                                 {
1414                                         int a;
1415                                         
1416                                         /* firstly check that we actuallly have some materials */
1417                                         for (a=0; a < ob->totcol; a++) {
1418                                                 Material *ma= give_current_material(ob, a);
1419                                                 
1420                                                 /* if material has relevant animation data, break */
1421                                                 ANIMDATA_FILTER_CASES(ma, 
1422                                                         matOk= 1;, 
1423                                                         matOk= 1;, 
1424                                                         matOk= 1;)
1425                                                         
1426                                                 if (matOk) 
1427                                                         break;
1428                                         }
1429                                 }
1430                                 
1431                                 /* data */
1432                                 switch (ob->type) {
1433                                         case OB_CAMERA: /* ------- Camera ------------ */
1434                                         {
1435                                                 Camera *ca= (Camera *)ob->data;
1436                                                 dataOk= 0;
1437                                                 ANIMDATA_FILTER_CASES(ca, 
1438                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1439                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1440                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);)
1441                                         }
1442                                                 break;
1443                                         case OB_LAMP: /* ---------- Lamp ----------- */
1444                                         {
1445                                                 Lamp *la= (Lamp *)ob->data;
1446                                                 dataOk= 0;
1447                                                 ANIMDATA_FILTER_CASES(la, 
1448                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1449                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1450                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);)
1451                                         }
1452                                                 break;
1453                                         default: /* --- other --- */
1454                                                 dataOk= 0;
1455                                                 break;
1456                                 }
1457                                 
1458                                 /* check if all bad (i.e. nothing to show) */
1459                                 if (!actOk && !keyOk && !dataOk && !matOk)
1460                                         continue;
1461                         }
1462                         else {
1463                                 /* check data-types */
1464                                 actOk= ANIMDATA_HAS_KEYS(ob);
1465                                 keyOk= (key != NULL);
1466                                 
1467                                 /* materials - only for geometric types */
1468                                 matOk= 0; /* by default, not ok... */
1469                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
1470                                 {
1471                                         int a;
1472                                         
1473                                         /* firstly check that we actuallly have some materials */
1474                                         for (a=0; a < ob->totcol; a++) {
1475                                                 Material *ma= give_current_material(ob, a);
1476                                                 
1477                                                 if ((ma) && ANIMDATA_HAS_KEYS(ma)) {
1478                                                         matOk= 1;
1479                                                         break;
1480                                                 }
1481                                         }
1482                                 }
1483                                 
1484                                 /* data */
1485                                 switch (ob->type) {
1486                                         case OB_CAMERA: /* ------- Camera ------------ */
1487                                         {
1488                                                 Camera *ca= (Camera *)ob->data;
1489                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1490                                         }
1491                                                 break;
1492                                         case OB_LAMP: /* ---------- Lamp ----------- */
1493                                         {
1494                                                 Lamp *la= (Lamp *)ob->data;
1495                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1496                                         }
1497                                                 break;
1498                                         case OB_CURVE: /* -------- Curve ---------- */
1499                                         {
1500                                                 Curve *cu= (Curve *)ob->data;
1501                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1502                                         }
1503                                                 break;
1504                                         default: /* --- other --- */
1505                                                 dataOk= 0;
1506                                                 break;
1507                                 }
1508                                 
1509                                 /* check if all bad (i.e. nothing to show) */
1510                                 if (!actOk && !keyOk && !dataOk && !matOk)
1511                                         continue;
1512                         }
1513                         
1514                         /* since we're still here, this object should be usable */
1515                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1516                 }
1517         }
1518         
1519         /* return the number of items in the list */
1520         return items;
1521 }
1522
1523 /* ----------- Public API --------------- */
1524
1525 /* This function filters the active data source to leave only animation channels suitable for
1526  * usage by the caller. It will return the length of the list 
1527  * 
1528  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
1529  *              will be placed for use.
1530  *      filter_mode: how should the data be filtered - bitmapping accessed flags
1531  */
1532 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
1533 {
1534         int items = 0;
1535         
1536         /* only filter data if there's somewhere to put it */
1537         if (data && anim_data) {
1538                 bAnimListElem *ale, *next;
1539                 Object *obact= (ac) ? ac->obact : NULL;
1540                 
1541                 /* firstly filter the data */
1542                 switch (datatype) {
1543                         case ANIMCONT_ACTION:
1544                                 items= animdata_filter_action(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1545                                 break;
1546                                 
1547                         case ANIMCONT_SHAPEKEY:
1548                                 items= animdata_filter_shapekey(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1549                                 break;
1550                                 
1551                         case ANIMCONT_GPENCIL:
1552                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
1553                                 break;
1554                                 
1555                         case ANIMCONT_DOPESHEET:
1556                         case ANIMCONT_FCURVES:
1557                         case ANIMCONT_DRIVERS:
1558                         case ANIMCONT_NLA:
1559                                 items= animdata_filter_dopesheet(anim_data, data, filter_mode);
1560                                 break;
1561                 }
1562                         
1563                 /* remove any weedy entries */
1564                 // XXX this is weedy code!
1565                 for (ale= anim_data->first; ale; ale= next) {
1566                         next= ale->next;
1567                         
1568                         if (ale->type == ANIMTYPE_NONE) {
1569                                 items--;
1570                                 BLI_freelinkN(anim_data, ale);
1571                         }
1572                 }
1573         }
1574         
1575         /* return the number of items in the list */
1576         return items;
1577 }
1578
1579 /* ************************************************************ */