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