Merged changes in the trunk up to revision 51718.
[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_math.h"
43 #include "BLI_rand.h"
44 #include "BLI_utildefines.h"
45
46 #include "BKE_animsys.h"
47 #include "BKE_nla.h"
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_screen.h"
51
52 #include "ED_anim_api.h"
53 #include "ED_keyframes_edit.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 from the standard Animation Editor ones */
68
69 /* ******************** Mouse-Click Operator *********************** */
70 /* Depending on the channel that was clicked on, the mouse click will activate whichever
71  * part of the channel is relevant.
72  *
73  * NOTE: eventually, this should probably be phased out when many of these things are replaced with buttons
74  *      --> Most channels are now selection only...
75  */
76
77 static int mouse_nla_channels(bAnimContext *ac, float x, int channel_index, short selectmode)
78 {
79         ListBase anim_data = {NULL, NULL};
80         bAnimListElem *ale;
81         int filter;
82         
83         View2D *v2d = &ac->ar->v2d;
84         int notifierFlags = 0;
85         
86         /* get the channel that was clicked on */
87         /* filter channels */
88         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
89         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
90         
91         /* get channel from index */
92         ale = BLI_findlink(&anim_data, channel_index);
93         if (ale == NULL) {
94                 /* channel not found */
95                 if (G.debug & G_DEBUG)
96                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels()\n", channel_index);
97                 
98                 BLI_freelistN(&anim_data);
99                 return 0;
100         }
101         
102         /* action to take depends on what channel we've got */
103         // WARNING: must keep this in sync with the equivalent function in anim_channels_edit.c
104         switch (ale->type) {
105                 case ANIMTYPE_SCENE:
106                 {
107                         Scene *sce = (Scene *)ale->data;
108                         AnimData *adt = sce->adt;
109                         
110                         /* set selection status */
111                         if (selectmode == SELECT_INVERT) {
112                                 /* swap select */
113                                 sce->flag ^= SCE_DS_SELECTED;
114                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
115                         }
116                         else {
117                                 sce->flag |= SCE_DS_SELECTED;
118                                 if (adt) adt->flag |= ADT_UI_SELECTED;
119                         }
120                         
121                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
122                 }
123                 break;
124                 case ANIMTYPE_OBJECT:
125                 {
126                         bDopeSheet *ads = (bDopeSheet *)ac->data;
127                         Scene *sce = (Scene *)ads->source;
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) {
133                                 /* set selection status */
134                                 if (selectmode == SELECT_INVERT) {
135                                         /* swap select */
136                                         base->flag ^= SELECT;
137                                         ob->flag = base->flag;
138                                         
139                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
140                                 }
141                                 else {
142                                         Base *b;
143
144                                         /* deselect all */
145                                         /* TODO: should this deselect all other types of channels too? */
146                                         for (b = sce->base.first; b; b = b->next) {
147                                                 b->flag &= ~SELECT;
148                                                 b->object->flag = b->flag;
149                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED | ADT_UI_ACTIVE);
150                                         }
151                                         
152                                         /* select object now */
153                                         base->flag |= SELECT;
154                                         ob->flag |= SELECT;
155                                         if (adt) adt->flag |= ADT_UI_SELECTED;
156                                 }
157                                 
158                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
159                                         adt->flag |= ADT_UI_ACTIVE;
160                                 
161                                 /* notifiers - channel was selected */
162                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
163                         }
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_DSCUR:
172                 case ANIMTYPE_DSSKEY:
173                 case ANIMTYPE_DSWOR:
174                 case ANIMTYPE_DSNTREE:
175                 case ANIMTYPE_DSPART:
176                 case ANIMTYPE_DSMBALL:
177                 case ANIMTYPE_DSARM:
178                 case ANIMTYPE_DSMESH:
179                 case ANIMTYPE_DSTEX:
180                 case ANIMTYPE_DSLAT:
181                 case ANIMTYPE_DSLINESTYLE:
182                 case ANIMTYPE_DSSPK:
183                 {
184                         /* sanity checking... */
185                         if (ale->adt) {
186                                 /* select/deselect */
187                                 if (selectmode == SELECT_INVERT) {
188                                         /* inverse selection status of this AnimData block only */
189                                         ale->adt->flag ^= ADT_UI_SELECTED;
190                                 }
191                                 else {
192                                         /* select AnimData block by itself */
193                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
194                                         ale->adt->flag |= ADT_UI_SELECTED;
195                                 }
196                                 
197                                 /* set active? */
198                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
199                                         ale->adt->flag |= ADT_UI_ACTIVE;
200                         }
201                         
202                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
203                 }
204                 break;
205                         
206                 case ANIMTYPE_NLATRACK:
207                 {
208                         NlaTrack *nlt = (NlaTrack *)ale->data;
209                         AnimData *adt = ale->adt;
210                         short offset;
211                         
212                         /* offset for start of channel (on LHS of channel-list) */
213                         if (ale->id) {
214                                 /* special exception for materials and particles */
215                                 if (ELEM(GS(ale->id->name), ID_MA, ID_PA))
216                                         offset = 21 + NLACHANNEL_BUTTON_WIDTH;
217                                 else
218                                         offset = 14;
219                         }
220                         else
221                                 offset = 0;
222                         
223                         if (x >= (v2d->cur.xmax - NLACHANNEL_BUTTON_WIDTH)) {
224                                 /* toggle protection (only if there's a toggle there) */
225                                 nlt->flag ^= NLATRACK_PROTECTED;
226                                 
227                                 /* notifier flags - channel was edited */
228                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
229                         }
230                         else if (x >= (v2d->cur.xmax - 2 * NLACHANNEL_BUTTON_WIDTH)) {
231                                 /* toggle mute */
232                                 nlt->flag ^= NLATRACK_MUTED;
233                                 
234                                 /* notifier flags - channel was edited */
235                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
236                         }
237                         else if (x <= ((NLACHANNEL_BUTTON_WIDTH * 2) + offset)) {
238                                 /* toggle 'solo' */
239                                 BKE_nlatrack_solo_toggle(adt, nlt);
240                                 
241                                 /* notifier flags - channel was edited */
242                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
243                         }
244                         else if (nlaedit_is_tweakmode_on(ac) == 0) {
245                                 /* set selection */
246                                 if (selectmode == SELECT_INVERT) {
247                                         /* inverse selection status of this F-Curve only */
248                                         nlt->flag ^= NLATRACK_SELECTED;
249                                 }
250                                 else {
251                                         /* select F-Curve by itself */
252                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
253                                         nlt->flag |= NLATRACK_SELECTED;
254                                 }
255                                 
256                                 /* if NLA-Track is selected now, make NLA-Track the 'active' one in the visible list */
257                                 if (nlt->flag & NLATRACK_SELECTED)
258                                         ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, nlt, ANIMTYPE_NLATRACK);
259                                         
260                                 /* notifier flags - channel was selected */
261                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
262                         }
263                 }
264                 break;
265                 case ANIMTYPE_NLAACTION:
266                 {
267                         AnimData *adt = BKE_animdata_from_id(ale->id);
268                         
269                         if (x >= (v2d->cur.xmax - NLACHANNEL_BUTTON_WIDTH)) {
270                                 if (nlaedit_is_tweakmode_on(ac) == 0) {
271                                         /* 'push-down' action - only usable when not in TweakMode */
272                                         /* TODO: make this use the operator instead of calling the function directly
273                                          *  however, calling the operator requires that we supply the args, and that works with proper buttons only */
274                                         BKE_nla_action_pushdown(adt);
275                                 }
276                                 else {
277                                         /* when in tweakmode, this button becomes the toggle for mapped editing */
278                                         adt->flag ^= ADT_NLA_EDIT_NOMAP;
279                                 }
280                                 
281                                 /* changes to NLA-Action occurred */
282                                 notifierFlags |= ND_NLA_ACTCHANGE;
283                         }
284                 }
285                 break;
286                         
287                 default:
288                         if (G.debug & G_DEBUG)
289                                 printf("Error: Invalid channel type in mouse_nla_channels()\n");
290         }
291         
292         /* free channels */
293         BLI_freelistN(&anim_data);
294         
295         /* return the notifier-flags set */
296         return notifierFlags;
297 }
298
299 /* ------------------- */
300
301 /* handle clicking */
302 static int nlachannels_mouseclick_invoke(bContext *C, wmOperator *op, wmEvent *event)
303 {
304         bAnimContext ac;
305         SpaceNla *snla;
306         ARegion *ar;
307         View2D *v2d;
308         int channel_index;
309         int notifierFlags = 0;
310         short selectmode;
311         float x, y;
312         
313         /* get editor data */
314         if (ANIM_animdata_get_context(C, &ac) == 0)
315                 return OPERATOR_CANCELLED;
316                 
317         /* get useful pointers from animation context data */
318         snla = (SpaceNla *)ac.sl;
319         ar = ac.ar;
320         v2d = &ar->v2d;
321         
322         /* select mode is either replace (deselect all, then add) or add/extend */
323         if (RNA_boolean_get(op->ptr, "extend"))
324                 selectmode = SELECT_INVERT;
325         else
326                 selectmode = SELECT_REPLACE;
327         
328         /* figure out which channel user clicked in 
329          * Note: although channels technically start at y= NLACHANNEL_FIRST, we need to adjust by half a channel's height
330          *              so that the tops of channels get caught ok. Since NLACHANNEL_FIRST is really NLACHANNEL_HEIGHT, we simply use
331          *              NLACHANNEL_HEIGHT_HALF.
332          */
333         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, &y);
334         UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
335         
336         /* handle mouse-click in the relevant channel then */
337         notifierFlags = mouse_nla_channels(&ac, x, channel_index, selectmode);
338         
339         /* set notifier that things have changed */
340         WM_event_add_notifier(C, NC_ANIMATION | notifierFlags, NULL);
341         
342         return OPERATOR_FINISHED;
343 }
344  
345 void NLA_OT_channels_click(wmOperatorType *ot)
346 {
347         PropertyRNA *prop;
348         
349         /* identifiers */
350         ot->name = "Mouse Click on NLA Channels";
351         ot->idname = "NLA_OT_channels_click";
352         ot->description = "Handle clicks to select NLA channels";
353         
354         /* api callbacks */
355         ot->invoke = nlachannels_mouseclick_invoke;
356         ot->poll = ED_operator_nla_active;
357         
358         /* flags */
359         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
360         
361         /* props */
362         prop = RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
363         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
364 }
365
366 /* *********************************************** */
367 /* Special Operators */
368
369 /* ******************** Add Tracks Operator ***************************** */
370 /* Add NLA Tracks to the same AnimData block as a selected track, or above the selected tracks */
371
372 static int nlaedit_add_tracks_exec(bContext *C, wmOperator *op)
373 {
374         bAnimContext ac;
375         
376         ListBase anim_data = {NULL, NULL};
377         bAnimListElem *ale;
378         int filter;
379         
380         AnimData *lastAdt = NULL;
381         short above_sel = RNA_boolean_get(op->ptr, "above_selected");
382         
383         /* get editor data */
384         if (ANIM_animdata_get_context(C, &ac) == 0)
385                 return OPERATOR_CANCELLED;
386                 
387         /* get a list of the AnimData blocks being shown in the NLA */
388         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
389         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
390         
391         /* add tracks... */
392         for (ale = anim_data.first; ale; ale = ale->next) {
393                 if (ale->type == ANIMTYPE_NLATRACK) {
394                         NlaTrack *nlt = (NlaTrack *)ale->data;
395                         AnimData *adt = ale->adt;
396                         
397                         /* check if just adding a new track above this one,
398                          * or whether we're adding a new one to the top of the stack that this one belongs to
399                          */
400                         if (above_sel) {
401                                 /* just add a new one above this one */
402                                 add_nlatrack(adt, nlt);
403                         }
404                         else if ((lastAdt == NULL) || (adt != lastAdt)) {
405                                 /* add one track to the top of the owning AnimData's stack, then don't add anymore to this stack */
406                                 add_nlatrack(adt, NULL);
407                                 lastAdt = adt;
408                         }
409                 }
410         }
411         
412         /* free temp data */
413         BLI_freelistN(&anim_data);
414         
415         /* set notifier that things have changed */
416         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
417         
418         /* done */
419         return OPERATOR_FINISHED;
420 }
421
422 void NLA_OT_tracks_add(wmOperatorType *ot)
423 {
424         /* identifiers */
425         ot->name = "Add Track(s)";
426         ot->idname = "NLA_OT_tracks_add";
427         ot->description = "Add NLA-Tracks above/after the selected tracks";
428         
429         /* api callbacks */
430         ot->exec = nlaedit_add_tracks_exec;
431         ot->poll = nlaop_poll_tweakmode_off;
432         
433         /* flags */
434         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
435         
436         /* properties */
437         RNA_def_boolean(ot->srna, "above_selected", 0, "Above Selected", "Add a new NLA Track above every existing selected one");
438 }
439
440 /* ******************** Delete Tracks Operator ***************************** */
441 /* Delete selected NLA Tracks */
442
443 static int nlaedit_delete_tracks_exec(bContext *C, wmOperator *UNUSED(op))
444 {
445         bAnimContext ac;
446         
447         ListBase anim_data = {NULL, NULL};
448         bAnimListElem *ale;
449         int filter;
450         
451         /* get editor data */
452         if (ANIM_animdata_get_context(C, &ac) == 0)
453                 return OPERATOR_CANCELLED;
454                 
455         /* get a list of the AnimData blocks being shown in the NLA */
456         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
457         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
458         
459         /* delete tracks */
460         for (ale = anim_data.first; ale; ale = ale->next) {
461                 if (ale->type == ANIMTYPE_NLATRACK) {
462                         NlaTrack *nlt = (NlaTrack *)ale->data;
463                         AnimData *adt = ale->adt;
464                         
465                         /* if track is currently 'solo', then AnimData should have its
466                          * 'has solo' flag disabled
467                          */
468                         if (nlt->flag & NLATRACK_SOLO)
469                                 adt->flag &= ~ADT_NLA_SOLO_TRACK;
470                         
471                         /* call delete on this track - deletes all strips too */
472                         free_nlatrack(&adt->nla_tracks, nlt);
473                 }
474         }
475         
476         /* free temp data */
477         BLI_freelistN(&anim_data);
478         
479         /* set notifier that things have changed */
480         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
481         
482         /* done */
483         return OPERATOR_FINISHED;
484 }
485
486 void NLA_OT_delete_tracks(wmOperatorType *ot)
487 {
488         /* identifiers */
489         ot->name = "Delete Tracks";
490         ot->idname = "NLA_OT_delete_tracks";
491         ot->description = "Delete selected NLA-Tracks and the strips they contain";
492         
493         /* api callbacks */
494         ot->exec = nlaedit_delete_tracks_exec;
495         ot->poll = nlaop_poll_tweakmode_off;
496         
497         /* flags */
498         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
499 }
500
501 /* *********************************************** */