Merging trunk up to r38329.
[blender.git] / source / blender / editors / space_nla / nla_channels.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung (major recode)
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_nla/nla_channels.c
30  *  \ingroup spnla
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <math.h>
38
39 #include "DNA_anim_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_math.h"
45 #include "BLI_rand.h"
46 #include "BLI_utildefines.h"
47
48 #include "BKE_animsys.h"
49 #include "BKE_nla.h"
50 #include "BKE_context.h"
51 #include "BKE_global.h"
52 #include "BKE_screen.h"
53
54 #include "ED_anim_api.h"
55 #include "ED_keyframes_edit.h"
56 #include "ED_screen.h"
57
58 #include "RNA_access.h"
59 #include "RNA_define.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "UI_view2d.h"
65
66 #include "nla_intern.h" // own include
67
68 /* *********************************************** */
69 /* Operators for NLA channels-list which need to be different from the standard Animation Editor ones */
70
71 /* ******************** Mouse-Click Operator *********************** */
72 /* Depending on the channel that was clicked on, the mouse click will activate whichever
73  * part of the channel is relevant.
74  *
75  * NOTE: eventually, this should probably be phased out when many of these things are replaced with buttons
76  *      --> Most channels are now selection only...
77  */
78
79 static int mouse_nla_channels (bAnimContext *ac, float x, int channel_index, short selectmode)
80 {
81         ListBase anim_data = {NULL, NULL};
82         bAnimListElem *ale;
83         int filter;
84         
85         View2D *v2d= &ac->ar->v2d;
86         int notifierFlags = 0;
87         
88         /* get the channel that was clicked on */
89                 /* filter channels */
90         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
91         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
92         
93                 /* get channel from index */
94         ale= BLI_findlink(&anim_data, channel_index);
95         if (ale == NULL) {
96                 /* channel not found */
97                 if (G.f & G_DEBUG)
98                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels() \n", channel_index);
99                 
100                 BLI_freelistN(&anim_data);
101                 return 0;
102         }
103         
104         /* action to take depends on what channel we've got */
105         // WARNING: must keep this in sync with the equivalent function in anim_channels_edit.c
106         switch (ale->type) {
107                 case ANIMTYPE_SCENE:
108                 {
109                         Scene *sce= (Scene *)ale->data;
110                         AnimData *adt= sce->adt;
111                         
112                         /* set selection status */
113                         if (selectmode == SELECT_INVERT) {
114                                 /* swap select */
115                                 sce->flag ^= SCE_DS_SELECTED;
116                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
117                         }
118                         else {
119                                 sce->flag |= SCE_DS_SELECTED;
120                                 if (adt) adt->flag |= ADT_UI_SELECTED;
121                         }
122                         
123                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
124                 }
125                         break;
126                 case ANIMTYPE_OBJECT:
127                 {
128                         bDopeSheet *ads= (bDopeSheet *)ac->data;
129                         Scene *sce= (Scene *)ads->source;
130                         Base *base= (Base *)ale->data;
131                         Object *ob= base->object;
132                         AnimData *adt= ob->adt;
133                         
134                         if (nlaedit_is_tweakmode_on(ac) == 0) {
135                                 /* set selection status */
136                                 if (selectmode == SELECT_INVERT) {
137                                         /* swap select */
138                                         base->flag ^= SELECT;
139                                         ob->flag= base->flag;
140                                         
141                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
142                                 }
143                                 else {
144                                         Base *b;
145                                         
146                                         /* deselect all */
147                                         // TODO: should this deselect all other types of channels too?
148                                         for (b= sce->base.first; b; b= b->next) {
149                                                 b->flag &= ~SELECT;
150                                                 b->object->flag= b->flag;
151                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED|ADT_UI_ACTIVE);
152                                         }
153                                         
154                                         /* select object now */
155                                         base->flag |= SELECT;
156                                         ob->flag |= SELECT;
157                                         if (adt) adt->flag |= ADT_UI_SELECTED;
158                                 }
159                                 
160                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
161                                         adt->flag |= ADT_UI_ACTIVE;
162                                 
163                                 /* notifiers - channel was selected */
164                                 notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
165                         }
166                 }
167                         break;
168                         
169                 case ANIMTYPE_FILLACTD: /* Action Expander */
170                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
171                 case ANIMTYPE_DSLAM:
172                 case ANIMTYPE_DSCAM:
173                 case ANIMTYPE_DSCUR:
174                 case ANIMTYPE_DSSKEY:
175                 case ANIMTYPE_DSWOR:
176                 case ANIMTYPE_DSNTREE:
177                 case ANIMTYPE_DSPART:
178                 case ANIMTYPE_DSMBALL:
179                 case ANIMTYPE_DSARM:
180                 case ANIMTYPE_DSMESH:
181                 case ANIMTYPE_DSTEX:
182                 case ANIMTYPE_DSLAT:
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.f & 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         /* identifiers */
348         ot->name= "Mouse Click on NLA Channels";
349         ot->idname= "NLA_OT_channels_click";
350         ot->description= "Handle clicks to select NLA channels";
351         
352         /* api callbacks */
353         ot->invoke= nlachannels_mouseclick_invoke;
354         ot->poll= ED_operator_nla_active;
355         
356         /* flags */
357         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
358         
359         /* id-props */
360         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
361 }
362
363 /* *********************************************** */
364 /* Special Operators */
365
366 /* ******************** Add Tracks Operator ***************************** */
367 /* Add NLA Tracks to the same AnimData block as a selected track, or above the selected tracks */
368
369 static int nlaedit_add_tracks_exec (bContext *C, wmOperator *op)
370 {
371         bAnimContext ac;
372         
373         ListBase anim_data = {NULL, NULL};
374         bAnimListElem *ale;
375         int filter;
376         
377         AnimData *lastAdt = NULL;
378         short above_sel= RNA_boolean_get(op->ptr, "above_selected");
379         
380         /* get editor data */
381         if (ANIM_animdata_get_context(C, &ac) == 0)
382                 return OPERATOR_CANCELLED;
383                 
384         /* get a list of the AnimData blocks being shown in the NLA */
385         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
386         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
387         
388         /* add tracks... */
389         for (ale= anim_data.first; ale; ale= ale->next) {
390                 if (ale->type == ANIMTYPE_NLATRACK) {
391                         NlaTrack *nlt= (NlaTrack *)ale->data;
392                         AnimData *adt= ale->adt;
393                         
394                         /* check if just adding a new track above this one,
395                          * or whether we're adding a new one to the top of the stack that this one belongs to
396                          */
397                         if (above_sel) {
398                                 /* just add a new one above this one */
399                                 add_nlatrack(adt, nlt);
400                         }
401                         else if ((lastAdt == NULL) || (adt != lastAdt)) {
402                                 /* add one track to the top of the owning AnimData's stack, then don't add anymore to this stack */
403                                 add_nlatrack(adt, NULL);
404                                 lastAdt= adt;
405                         }
406                 }
407         }
408         
409         /* free temp data */
410         BLI_freelistN(&anim_data);
411         
412         /* set notifier that things have changed */
413         WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
414         
415         /* done */
416         return OPERATOR_FINISHED;
417 }
418
419 void NLA_OT_tracks_add (wmOperatorType *ot)
420 {
421         /* identifiers */
422         ot->name= "Add Track(s)";
423         ot->idname= "NLA_OT_tracks_add";
424         ot->description= "Add NLA-Tracks above/after the selected tracks";
425         
426         /* api callbacks */
427         ot->exec= nlaedit_add_tracks_exec;
428         ot->poll= nlaop_poll_tweakmode_off;
429         
430         /* flags */
431         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
432         
433         /* properties */
434         RNA_def_boolean(ot->srna, "above_selected", 0, "Above Selected", "Add a new NLA Track above every existing selected one.");
435 }
436
437 /* ******************** Delete Tracks Operator ***************************** */
438 /* Delete selected NLA Tracks */
439
440 static int nlaedit_delete_tracks_exec (bContext *C, wmOperator *UNUSED(op))
441 {
442         bAnimContext ac;
443         
444         ListBase anim_data = {NULL, NULL};
445         bAnimListElem *ale;
446         int filter;
447         
448         /* get editor data */
449         if (ANIM_animdata_get_context(C, &ac) == 0)
450                 return OPERATOR_CANCELLED;
451                 
452         /* get a list of the AnimData blocks being shown in the NLA */
453         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
454         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
455         
456         /* delete tracks */
457         for (ale= anim_data.first; ale; ale= ale->next) {
458                 if(ale->type == ANIMTYPE_NLATRACK) {
459                         NlaTrack *nlt= (NlaTrack *)ale->data;
460                         AnimData *adt= ale->adt;
461                         
462                         /* if track is currently 'solo', then AnimData should have its
463                          * 'has solo' flag disabled
464                          */
465                         if (nlt->flag & NLATRACK_SOLO)
466                                 adt->flag &= ~ADT_NLA_SOLO_TRACK;
467                         
468                         /* call delete on this track - deletes all strips too */
469                         free_nlatrack(&adt->nla_tracks, nlt);
470                 }
471         }
472         
473         /* free temp data */
474         BLI_freelistN(&anim_data);
475         
476         /* set notifier that things have changed */
477         WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
478         
479         /* done */
480         return OPERATOR_FINISHED;
481 }
482
483 void NLA_OT_delete_tracks (wmOperatorType *ot)
484 {
485         /* identifiers */
486         ot->name= "Delete Tracks";
487         ot->idname= "NLA_OT_delete_tracks";
488         ot->description= "Delete selected NLA-Tracks and the strips they contain";
489         
490         /* api callbacks */
491         ot->exec= nlaedit_delete_tracks_exec;
492         ot->poll= nlaop_poll_tweakmode_off;
493         
494         /* flags */
495         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
496 }
497
498 /* *********************************************** */