Cleanup: comment line length (editors)
[blender.git] / source / blender / editors / space_nla / nla_channels.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Joshua Leung (major recode)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_nla/nla_channels.c
28  *  \ingroup spnla
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36
37 #include "DNA_anim_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_animsys.h"
45 #include "BKE_nla.h"
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_screen.h"
49 #include "BKE_report.h"
50
51 #include "ED_anim_api.h"
52 #include "ED_keyframes_edit.h"
53 #include "ED_object.h"
54 #include "ED_screen.h"
55
56 #include "RNA_access.h"
57 #include "RNA_define.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "UI_view2d.h"
63
64 #include "nla_intern.h" // own include
65
66 /* *********************************************** */
67 /* Operators for NLA channels-list which need to be different
68  * from the standard Animation Editor ones */
69
70 /* ******************** Mouse-Click Operator *********************** */
71 /* Depending on the channel that was clicked on, the mouse click will activate whichever
72  * part of the channel is relevant.
73  *
74  * NOTE: eventually, this should probably be phased out when many of these things are replaced with buttons
75  * --> Most channels are now selection only...
76  */
77
78 static int mouse_nla_channels(bContext *C, bAnimContext *ac, float x, int channel_index, short selectmode)
79 {
80         ListBase anim_data = {NULL, NULL};
81         bAnimListElem *ale;
82         int filter;
83
84         View2D *v2d = &ac->ar->v2d;
85         int notifierFlags = 0;
86
87         /* get the channel that was clicked on */
88         /* filter channels */
89         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
90         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
91
92         /* get channel from index */
93         ale = BLI_findlink(&anim_data, channel_index);
94         if (ale == NULL) {
95                 /* channel not found */
96                 if (G.debug & G_DEBUG)
97                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels()\n", channel_index);
98
99                 ANIM_animdata_freelist(&anim_data);
100                 return 0;
101         }
102
103         /* action to take depends on what channel we've got */
104         // WARNING: must keep this in sync with the equivalent function in anim_channels_edit.c
105         switch (ale->type) {
106                 case ANIMTYPE_SCENE:
107                 {
108                         Scene *sce = (Scene *)ale->data;
109                         AnimData *adt = sce->adt;
110
111                         /* set selection status */
112                         if (selectmode == SELECT_INVERT) {
113                                 /* swap select */
114                                 sce->flag ^= SCE_DS_SELECTED;
115                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
116                         }
117                         else {
118                                 sce->flag |= SCE_DS_SELECTED;
119                                 if (adt) adt->flag |= ADT_UI_SELECTED;
120                         }
121
122                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
123                         break;
124                 }
125                 case ANIMTYPE_OBJECT:
126                 {
127                         bDopeSheet *ads = (bDopeSheet *)ac->data;
128                         Scene *sce = (Scene *)ads->source;
129                         Base *base = (Base *)ale->data;
130                         Object *ob = base->object;
131                         AnimData *adt = ob->adt;
132
133                         if (nlaedit_is_tweakmode_on(ac) == 0 && (ob->restrictflag & OB_RESTRICT_SELECT) == 0) {
134                                 /* set selection status */
135                                 if (selectmode == SELECT_INVERT) {
136                                         /* swap select */
137                                         base->flag ^= SELECT;
138                                         ob->flag = base->flag;
139
140                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
141                                 }
142                                 else {
143                                         Base *b;
144
145                                         /* deselect all */
146                                         /* TODO: should this deselect all other types of channels too? */
147                                         for (b = sce->base.first; b; b = b->next) {
148                                                 b->flag &= ~SELECT;
149                                                 b->object->flag = b->flag;
150                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED | ADT_UI_ACTIVE);
151                                         }
152
153                                         /* select object now */
154                                         base->flag |= SELECT;
155                                         ob->flag |= SELECT;
156                                         if (adt) adt->flag |= ADT_UI_SELECTED;
157                                 }
158
159                                 /* change active object - regardless of whether it is now selected [T37883] */
160                                 ED_base_object_activate(C, base); /* adds notifier */
161
162                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
163                                         adt->flag |= ADT_UI_ACTIVE;
164
165                                 /* notifiers - channel was selected */
166                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
167                         }
168                         break;
169                 }
170                 case ANIMTYPE_FILLACTD: /* Action Expander */
171                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
172                 case ANIMTYPE_DSLAM:
173                 case ANIMTYPE_DSCAM:
174                 case ANIMTYPE_DSCACHEFILE:
175                 case ANIMTYPE_DSCUR:
176                 case ANIMTYPE_DSSKEY:
177                 case ANIMTYPE_DSWOR:
178                 case ANIMTYPE_DSNTREE:
179                 case ANIMTYPE_DSPART:
180                 case ANIMTYPE_DSMBALL:
181                 case ANIMTYPE_DSARM:
182                 case ANIMTYPE_DSMESH:
183                 case ANIMTYPE_DSTEX:
184                 case ANIMTYPE_DSLAT:
185                 case ANIMTYPE_DSLINESTYLE:
186                 case ANIMTYPE_DSSPK:
187                 case ANIMTYPE_DSGPENCIL:
188                 {
189                         /* sanity checking... */
190                         if (ale->adt) {
191                                 /* select/deselect */
192                                 if (selectmode == SELECT_INVERT) {
193                                         /* inverse selection status of this AnimData block only */
194                                         ale->adt->flag ^= ADT_UI_SELECTED;
195                                 }
196                                 else {
197                                         /* select AnimData block by itself */
198                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
199                                         ale->adt->flag |= ADT_UI_SELECTED;
200                                 }
201
202                                 /* set active? */
203                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
204                                         ale->adt->flag |= ADT_UI_ACTIVE;
205                         }
206
207                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
208                         break;
209                 }
210                 case ANIMTYPE_NLATRACK:
211                 {
212                         NlaTrack *nlt = (NlaTrack *)ale->data;
213                         AnimData *adt = ale->adt;
214                         short offset;
215
216                         /* offset for start of channel (on LHS of channel-list) */
217                         if (ale->id) {
218                                 /* special exception for materials and particles */
219                                 if (ELEM(GS(ale->id->name), ID_MA, ID_PA))
220                                         offset = 21 + NLACHANNEL_BUTTON_WIDTH;
221                                 else
222                                         offset = 14;
223                         }
224                         else
225                                 offset = 0;
226
227                         if (x >= (v2d->cur.xmax - NLACHANNEL_BUTTON_WIDTH)) {
228                                 /* toggle protection (only if there's a toggle there) */
229                                 nlt->flag ^= NLATRACK_PROTECTED;
230
231                                 /* notifier flags - channel was edited */
232                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
233                         }
234                         else if (x >= (v2d->cur.xmax - 2 * NLACHANNEL_BUTTON_WIDTH)) {
235                                 /* toggle mute */
236                                 nlt->flag ^= NLATRACK_MUTED;
237
238                                 /* notifier flags - channel was edited */
239                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
240                         }
241                         else if (x <= ((NLACHANNEL_BUTTON_WIDTH * 2) + offset)) {
242                                 /* toggle 'solo' */
243                                 BKE_nlatrack_solo_toggle(adt, nlt);
244
245                                 /* notifier flags - channel was edited */
246                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
247                         }
248                         else if (nlaedit_is_tweakmode_on(ac) == 0) {
249                                 /* set selection */
250                                 if (selectmode == SELECT_INVERT) {
251                                         /* inverse selection status of this F-Curve only */
252                                         nlt->flag ^= NLATRACK_SELECTED;
253                                 }
254                                 else {
255                                         /* select F-Curve by itself */
256                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
257                                         nlt->flag |= NLATRACK_SELECTED;
258                                 }
259
260                                 /* if NLA-Track is selected now,
261                                  * make NLA-Track the 'active' one in the visible list */
262                                 if (nlt->flag & NLATRACK_SELECTED)
263                                         ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, nlt, ANIMTYPE_NLATRACK);
264
265                                 /* notifier flags - channel was selected */
266                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
267                         }
268                         break;
269                 }
270                 case ANIMTYPE_NLAACTION:
271                 {
272                         AnimData *adt = BKE_animdata_from_id(ale->id);
273
274                         /* button region... */
275                         if (x >= (v2d->cur.xmax - NLACHANNEL_BUTTON_WIDTH)) {
276                                 if (nlaedit_is_tweakmode_on(ac) == 0) {
277                                         /* 'push-down' action - only usable when not in TweakMode */
278                                         /* TODO: make this use the operator instead of calling the function directly
279                                          * however, calling the operator requires that we supply the args,
280                                          * and that works with proper buttons only */
281                                         BKE_nla_action_pushdown(adt);
282                                 }
283                                 else {
284                                         /* when in tweakmode, this button becomes the toggle for mapped editing */
285                                         adt->flag ^= ADT_NLA_EDIT_NOMAP;
286                                 }
287
288                                 /* changes to NLA-Action occurred */
289                                 notifierFlags |= ND_NLA_ACTCHANGE;
290                         }
291                         /* OR rest of name... */
292                         else {
293                                 /* NOTE: rest of NLA-Action name doubles for operating on the AnimData block
294                                  * - this is useful when there's no clear divider, and makes more sense in
295                                  *   the case of users trying to use this to change actions
296                                  * - in tweakmode, clicking here gets us out of tweakmode, as changing selection
297                                  *   while in tweakmode is really evil!
298                                  * - we disable "solo" flags too, to make it easier to work with stashed actions
299                                  *   with less trouble
300                                  */
301                                 if (nlaedit_is_tweakmode_on(ac)) {
302                                         /* exit tweakmode immediately */
303                                         nlaedit_disable_tweakmode(ac, true);
304
305                                         /* changes to NLA-Action occurred */
306                                         notifierFlags |= ND_NLA_ACTCHANGE;
307                                 }
308                                 else {
309                                         /* select/deselect */
310                                         if (selectmode == SELECT_INVERT) {
311                                                 /* inverse selection status of this AnimData block only */
312                                                 adt->flag ^= ADT_UI_SELECTED;
313                                         }
314                                         else {
315                                                 /* select AnimData block by itself */
316                                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
317                                                 adt->flag |= ADT_UI_SELECTED;
318                                         }
319
320                                         /* set active? */
321                                         if (adt->flag & ADT_UI_SELECTED)
322                                                 adt->flag |= ADT_UI_ACTIVE;
323
324                                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
325                                 }
326                         }
327                         break;
328                 }
329                 default:
330                         if (G.debug & G_DEBUG)
331                                 printf("Error: Invalid channel type in mouse_nla_channels()\n");
332                         break;
333         }
334
335         /* free channels */
336         ANIM_animdata_freelist(&anim_data);
337
338         /* return the notifier-flags set */
339         return notifierFlags;
340 }
341
342 /* ------------------- */
343
344 /* handle clicking */
345 static int nlachannels_mouseclick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
346 {
347         bAnimContext ac;
348         SpaceNla *snla;
349         ARegion *ar;
350         View2D *v2d;
351         int channel_index;
352         int notifierFlags = 0;
353         short selectmode;
354         float x, y;
355
356         /* get editor data */
357         if (ANIM_animdata_get_context(C, &ac) == 0)
358                 return OPERATOR_CANCELLED;
359
360         /* get useful pointers from animation context data */
361         snla = (SpaceNla *)ac.sl;
362         ar = ac.ar;
363         v2d = &ar->v2d;
364
365         /* select mode is either replace (deselect all, then add) or add/extend */
366         if (RNA_boolean_get(op->ptr, "extend"))
367                 selectmode = SELECT_INVERT;
368         else
369                 selectmode = SELECT_REPLACE;
370
371         /* figure out which channel user clicked in
372          * Note: although channels technically start at y= NLACHANNEL_FIRST, we need to adjust by half a channel's height
373          *      so that the tops of channels get caught ok. Since NLACHANNEL_FIRST is really NLACHANNEL_HEIGHT, we simply use
374          *      NLACHANNEL_HEIGHT_HALF.
375          */
376         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, &y);
377         UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
378
379         /* handle mouse-click in the relevant channel then */
380         notifierFlags = mouse_nla_channels(C, &ac, x, channel_index, selectmode);
381
382         /* set notifier that things have changed */
383         WM_event_add_notifier(C, NC_ANIMATION | notifierFlags, NULL);
384
385         return OPERATOR_FINISHED;
386 }
387
388 void NLA_OT_channels_click(wmOperatorType *ot)
389 {
390         PropertyRNA *prop;
391
392         /* identifiers */
393         ot->name = "Mouse Click on NLA Channels";
394         ot->idname = "NLA_OT_channels_click";
395         ot->description = "Handle clicks to select NLA channels";
396
397         /* api callbacks */
398         ot->invoke = nlachannels_mouseclick_invoke;
399         ot->poll = ED_operator_nla_active;
400
401         /* flags */
402         ot->flag = OPTYPE_UNDO;
403
404         /* props */
405         prop = RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
406         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
407 }
408
409 /* *********************************************** */
410 /* Special Operators */
411
412 /* ******************** Action Push Down ******************************** */
413
414 static int nlachannels_pushdown_exec(bContext *C, wmOperator *op)
415 {
416         bAnimContext ac;
417         AnimData *adt = NULL;
418         int channel_index = RNA_int_get(op->ptr, "channel_index");
419
420         /* get editor data */
421         if (ANIM_animdata_get_context(C, &ac) == 0)
422                 return OPERATOR_CANCELLED;
423
424         /* get anim-channel to use (or more specifically, the animdata block behind it) */
425         if (channel_index == -1) {
426                 PointerRNA adt_ptr = {{NULL}};
427
428                 /* active animdata block */
429                 if (nla_panel_context(C, &adt_ptr, NULL, NULL) == 0 || (adt_ptr.data == NULL)) {
430                         BKE_report(op->reports, RPT_ERROR, "No active AnimData block to use "
431                                    "(select a data-block expander first or set the appropriate flags on an AnimData block)");
432                         return OPERATOR_CANCELLED;
433                 }
434                 else {
435                         adt = adt_ptr.data;
436                 }
437         }
438         else {
439                 /* indexed channel */
440                 ListBase anim_data = {NULL, NULL};
441                 bAnimListElem *ale;
442                 int filter;
443
444                 /* filter channels */
445                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
446                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
447
448                 /* get channel from index */
449                 ale = BLI_findlink(&anim_data, channel_index);
450                 if (ale == NULL) {
451                         BKE_reportf(op->reports, RPT_ERROR, "No animation channel found at index %d", channel_index);
452                         ANIM_animdata_freelist(&anim_data);
453                         return OPERATOR_CANCELLED;
454                 }
455                 else if (ale->type != ANIMTYPE_NLAACTION) {
456                         BKE_reportf(op->reports, RPT_ERROR, "Animation channel at index %d is not a NLA 'Active Action' channel", channel_index);
457                         ANIM_animdata_freelist(&anim_data);
458                         return OPERATOR_CANCELLED;
459                 }
460
461                 /* grab AnimData from the channel */
462                 adt = ale->adt;
463
464                 /* we don't need anything here anymore, so free it all */
465                 ANIM_animdata_freelist(&anim_data);
466         }
467
468         /* double-check that we are free to push down here... */
469         if (adt == NULL) {
470                 BKE_report(op->reports, RPT_WARNING, "Internal Error - AnimData block is not valid");
471                 return OPERATOR_CANCELLED;
472         }
473         else if (nlaedit_is_tweakmode_on(&ac)) {
474                 BKE_report(op->reports, RPT_WARNING,
475                            "Cannot push down actions while tweaking a strip's action, exit tweak mode first");
476                 return OPERATOR_CANCELLED;
477         }
478         else if (adt->action == NULL) {
479                 BKE_report(op->reports, RPT_WARNING, "No active action to push down");
480                 return OPERATOR_CANCELLED;
481         }
482         else {
483                 /* 'push-down' action - only usable when not in TweakMode */
484                 BKE_nla_action_pushdown(adt);
485         }
486
487         /* set notifier that things have changed */
488         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
489         return OPERATOR_FINISHED;
490 }
491
492 void NLA_OT_action_pushdown(wmOperatorType *ot)
493 {
494         /* identifiers */
495         ot->name = "Push Down Action";
496         ot->idname = "NLA_OT_action_pushdown";
497         ot->description = "Push action down onto the top of the NLA stack as a new strip";
498
499         /* callbacks */
500         ot->exec = nlachannels_pushdown_exec;
501         ot->poll = nlaop_poll_tweakmode_off;
502
503         /* flags */
504         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
505
506         /* properties */
507         ot->prop = RNA_def_int(ot->srna, "channel_index", -1, -1, INT_MAX, "Channel Index",
508                                "Index of NLA action channel to perform pushdown operation on",
509                                0, INT_MAX);
510         RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
511 }
512
513 /* ******************** Action Unlink ******************************** */
514
515 static bool nla_action_unlink_poll(bContext *C)
516 {
517         if (ED_operator_nla_active(C)) {
518                 return nla_panel_context(C, NULL, NULL, NULL);
519         }
520
521         /* something failed... */
522         return false;
523 }
524
525 static int nla_action_unlink_exec(bContext *C, wmOperator *op)
526 {
527         PointerRNA adt_ptr;
528         AnimData *adt;
529
530         /* check context and also validity of pointer */
531         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
532                 return OPERATOR_CANCELLED;
533
534         /* get animdata */
535         adt = adt_ptr.data;
536         if (adt == NULL)
537                 return OPERATOR_CANCELLED;
538
539         /* do unlinking */
540         if (adt && adt->action) {
541                 bool force_delete = RNA_boolean_get(op->ptr, "force_delete");
542                 ED_animedit_unlink_action(C, adt_ptr.id.data, adt, adt->action, op->reports, force_delete);
543         }
544
545         return OPERATOR_FINISHED;
546 }
547
548 static int nla_action_unlink_invoke(bContext *C, wmOperator *op, const wmEvent *evt)
549 {
550         /* NOTE: this is hardcoded to match the behaviour for the unlink button (in interface_templates.c) */
551         RNA_boolean_set(op->ptr, "force_delete", evt->shift != 0);
552         return nla_action_unlink_exec(C, op);
553 }
554
555 void NLA_OT_action_unlink(wmOperatorType *ot)
556 {
557         PropertyRNA *prop;
558
559         /* identifiers */
560         ot->name = "Unlink Action";
561         ot->idname = "NLA_OT_action_unlink";
562         ot->description = "Unlink this action from the active action slot (and/or exit Tweak Mode)";
563
564         /* callbacks */
565         ot->invoke = nla_action_unlink_invoke;
566         ot->exec = nla_action_unlink_exec;
567         ot->poll = nla_action_unlink_poll;
568
569         /* properties */
570         prop = RNA_def_boolean(ot->srna, "force_delete", false, "Force Delete",
571                                "Clear Fake User and remove copy stashed in this datablock's NLA stack");
572         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
573 }
574
575 /* ******************** Add Tracks Operator ***************************** */
576 /* Add NLA Tracks to the same AnimData block as a selected track, or above the selected tracks */
577
578 /* helper - add NLA Tracks alongside existing ones */
579 bool nlaedit_add_tracks_existing(bAnimContext *ac, bool above_sel)
580 {
581         ListBase anim_data = {NULL, NULL};
582         bAnimListElem *ale;
583         int filter;
584         AnimData *lastAdt = NULL;
585         bool added = false;
586
587         /* get a list of the (selected) NLA Tracks being shown in the NLA */
588         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
589         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
590
591         /* add tracks... */
592         for (ale = anim_data.first; ale; ale = ale->next) {
593                 if (ale->type == ANIMTYPE_NLATRACK) {
594                         NlaTrack *nlt = (NlaTrack *)ale->data;
595                         AnimData *adt = ale->adt;
596
597                         /* check if just adding a new track above this one,
598                          * or whether we're adding a new one to the top of the stack that this one belongs to
599                          */
600                         if (above_sel) {
601                                 /* just add a new one above this one */
602                                 BKE_nlatrack_add(adt, nlt);
603                                 added = true;
604                         }
605                         else if ((lastAdt == NULL) || (adt != lastAdt)) {
606                                 /* add one track to the top of the owning AnimData's stack,
607                                  * then don't add anymore to this stack */
608                                 BKE_nlatrack_add(adt, NULL);
609                                 lastAdt = adt;
610                                 added = true;
611                         }
612                 }
613         }
614
615         /* free temp data */
616         ANIM_animdata_freelist(&anim_data);
617
618         return added;
619 }
620
621 /* helper - add NLA Tracks to empty (and selected) AnimData blocks */
622 bool nlaedit_add_tracks_empty(bAnimContext *ac)
623 {
624         ListBase anim_data = {NULL, NULL};
625         bAnimListElem *ale;
626         int filter;
627         bool added = false;
628
629         /* get a list of the selected AnimData blocks in the NLA */
630         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
631         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
632
633         /* check if selected AnimData blocks are empty, and add tracks if so... */
634         for (ale = anim_data.first; ale; ale = ale->next) {
635                 AnimData *adt = ale->adt;
636
637                 /* sanity check */
638                 BLI_assert(adt->flag & ADT_UI_SELECTED);
639
640                 /* ensure it is empty */
641                 if (BLI_listbase_is_empty(&adt->nla_tracks)) {
642                         /* add new track to this AnimData block then */
643                         BKE_nlatrack_add(adt, NULL);
644                         added = true;
645                 }
646         }
647
648         /* cleanup */
649         ANIM_animdata_freelist(&anim_data);
650
651         return added;
652 }
653
654 /* ----- */
655
656 static int nlaedit_add_tracks_exec(bContext *C, wmOperator *op)
657 {
658         bAnimContext ac;
659         bool above_sel = RNA_boolean_get(op->ptr, "above_selected");
660         bool op_done = false;
661
662         /* get editor data */
663         if (ANIM_animdata_get_context(C, &ac) == 0)
664                 return OPERATOR_CANCELLED;
665
666         /* perform adding in two passes - existing first so that we don't double up for empty */
667         op_done |= nlaedit_add_tracks_existing(&ac, above_sel);
668         op_done |= nlaedit_add_tracks_empty(&ac);
669
670         /* done? */
671         if (op_done) {
672                 /* set notifier that things have changed */
673                 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
674
675                 /* done */
676                 return OPERATOR_FINISHED;
677         }
678         else {
679                 /* failed to add any tracks */
680                 BKE_report(op->reports, RPT_WARNING,
681                            "Select an existing NLA Track or an empty action line first");
682
683                 /* not done */
684                 return OPERATOR_CANCELLED;
685         }
686 }
687
688 void NLA_OT_tracks_add(wmOperatorType *ot)
689 {
690         /* identifiers */
691         ot->name = "Add Tracks";
692         ot->idname = "NLA_OT_tracks_add";
693         ot->description = "Add NLA-Tracks above/after the selected tracks";
694
695         /* api callbacks */
696         ot->exec = nlaedit_add_tracks_exec;
697         ot->poll = nlaop_poll_tweakmode_off;
698
699         /* flags */
700         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
701
702         /* properties */
703         RNA_def_boolean(ot->srna, "above_selected", 0, "Above Selected", "Add a new NLA Track above every existing selected one");
704 }
705
706 /* ******************** Delete Tracks Operator ***************************** */
707 /* Delete selected NLA Tracks */
708
709 static int nlaedit_delete_tracks_exec(bContext *C, wmOperator *UNUSED(op))
710 {
711         bAnimContext ac;
712
713         ListBase anim_data = {NULL, NULL};
714         bAnimListElem *ale;
715         int filter;
716
717         /* get editor data */
718         if (ANIM_animdata_get_context(C, &ac) == 0)
719                 return OPERATOR_CANCELLED;
720
721         /* get a list of the AnimData blocks being shown in the NLA */
722         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
723         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
724
725         /* delete tracks */
726         for (ale = anim_data.first; ale; ale = ale->next) {
727                 if (ale->type == ANIMTYPE_NLATRACK) {
728                         NlaTrack *nlt = (NlaTrack *)ale->data;
729                         AnimData *adt = ale->adt;
730
731                         /* if track is currently 'solo', then AnimData should have its
732                          * 'has solo' flag disabled
733                          */
734                         if (nlt->flag & NLATRACK_SOLO)
735                                 adt->flag &= ~ADT_NLA_SOLO_TRACK;
736
737                         /* call delete on this track - deletes all strips too */
738                         BKE_nlatrack_free(&adt->nla_tracks, nlt);
739                 }
740         }
741
742         /* free temp data */
743         ANIM_animdata_freelist(&anim_data);
744
745         /* set notifier that things have changed */
746         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
747
748         /* done */
749         return OPERATOR_FINISHED;
750 }
751
752 void NLA_OT_tracks_delete(wmOperatorType *ot)
753 {
754         /* identifiers */
755         ot->name = "Delete Tracks";
756         ot->idname = "NLA_OT_tracks_delete";
757         ot->description = "Delete selected NLA-Tracks and the strips they contain";
758
759         /* api callbacks */
760         ot->exec = nlaedit_delete_tracks_exec;
761         ot->poll = nlaop_poll_tweakmode_off;
762
763         /* flags */
764         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
765 }
766
767 /* *********************************************** */
768 /* AnimData Related Operators */
769
770 /* ******************** Include Objects Operator ***************************** */
771 /* Include selected objects in NLA Editor, by giving them AnimData blocks
772  * NOTE: This doesn't help for non-object AnimData, where we do not have any effective
773  *       selection mechanism in place. Unfortunately, this means that non-object AnimData
774  *       once again becomes a second-class citizen here. However, at least for the most
775  *       common use case, we now have a nice shortcut again.
776  */
777
778 static int nlaedit_objects_add_exec(bContext *C, wmOperator *UNUSED(op))
779 {
780         bAnimContext ac;
781         SpaceNla *snla;
782
783         /* get editor data */
784         if (ANIM_animdata_get_context(C, &ac) == 0)
785                 return OPERATOR_CANCELLED;
786
787         /* ensure that filters are set so that the effect will be immediately visible */
788         snla = (SpaceNla *)ac.sl;
789         if (snla && snla->ads) {
790                 snla->ads->filterflag &= ~ADS_FILTER_NLA_NOACT;
791         }
792
793         /* operate on selected objects... */
794         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
795         {
796                 /* ensure that object has AnimData... that's all */
797                 BKE_animdata_add_id(&ob->id);
798         }
799         CTX_DATA_END;
800
801         /* set notifier that things have changed */
802         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
803
804         /* done */
805         return OPERATOR_FINISHED;
806 }
807
808 void NLA_OT_selected_objects_add(wmOperatorType *ot)
809 {
810         /* identifiers */
811         ot->name = "Include Selected Objects";
812         ot->idname = "NLA_OT_selected_objects_add";
813         ot->description = "Make selected objects appear in NLA Editor by adding Animation Data";
814
815         /* api callbacks */
816         ot->exec = nlaedit_objects_add_exec;
817         ot->poll = nlaop_poll_tweakmode_off;
818
819         /* flags */
820         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
821 }
822
823 /* *********************************************** */