svn merge -r39286:39298 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[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                 case ANIMTYPE_DSSPK:
184                 {
185                         /* sanity checking... */
186                         if (ale->adt) {
187                                 /* select/deselect */
188                                 if (selectmode == SELECT_INVERT) {
189                                         /* inverse selection status of this AnimData block only */
190                                         ale->adt->flag ^= ADT_UI_SELECTED;
191                                 }
192                                 else {
193                                         /* select AnimData block by itself */
194                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
195                                         ale->adt->flag |= ADT_UI_SELECTED;
196                                 }
197                                 
198                                 /* set active? */
199                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
200                                         ale->adt->flag |= ADT_UI_ACTIVE;
201                         }
202                         
203                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
204                 }       
205                         break;
206                         
207                 case ANIMTYPE_NLATRACK:
208                 {
209                         NlaTrack *nlt= (NlaTrack *)ale->data;
210                         AnimData *adt= ale->adt;
211                         short offset;
212                         
213                         /* offset for start of channel (on LHS of channel-list) */
214                         if (ale->id) {
215                                 /* special exception for materials and particles */
216                                 if (ELEM(GS(ale->id->name),ID_MA,ID_PA))
217                                         offset= 21 + NLACHANNEL_BUTTON_WIDTH;
218                                 else
219                                         offset= 14;
220                         }
221                         else
222                                 offset= 0;
223                         
224                         if (x >= (v2d->cur.xmax-NLACHANNEL_BUTTON_WIDTH)) {
225                                 /* toggle protection (only if there's a toggle there) */
226                                 nlt->flag ^= NLATRACK_PROTECTED;
227                                 
228                                 /* notifier flags - channel was edited */
229                                 notifierFlags |= (ND_ANIMCHAN|NA_EDITED);
230                         }
231                         else if (x >= (v2d->cur.xmax-2*NLACHANNEL_BUTTON_WIDTH)) {
232                                 /* toggle mute */
233                                 nlt->flag ^= NLATRACK_MUTED;
234                                 
235                                 /* notifier flags - channel was edited */
236                                 notifierFlags |= (ND_ANIMCHAN|NA_EDITED);
237                         }
238                         else if (x <= ((NLACHANNEL_BUTTON_WIDTH*2)+offset)) {
239                                 /* toggle 'solo' */
240                                 BKE_nlatrack_solo_toggle(adt, nlt);
241                                 
242                                 /* notifier flags - channel was edited */
243                                 notifierFlags |= (ND_ANIMCHAN|NA_EDITED);
244                         }
245                         else if (nlaedit_is_tweakmode_on(ac) == 0) {
246                                 /* set selection */
247                                 if (selectmode == SELECT_INVERT) {
248                                         /* inverse selection status of this F-Curve only */
249                                         nlt->flag ^= NLATRACK_SELECTED;
250                                 }
251                                 else {
252                                         /* select F-Curve by itself */
253                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
254                                         nlt->flag |= NLATRACK_SELECTED;
255                                 }
256                                 
257                                 /* if NLA-Track is selected now, make NLA-Track the 'active' one in the visible list */
258                                 if (nlt->flag & NLATRACK_SELECTED)
259                                         ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, nlt, ANIMTYPE_NLATRACK);
260                                         
261                                 /* notifier flags - channel was selected */
262                                 notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
263                         }
264                 }
265                         break;
266                 case ANIMTYPE_NLAACTION:
267                 {
268                         AnimData *adt= BKE_animdata_from_id(ale->id);
269                         
270                         if (x >= (v2d->cur.xmax-NLACHANNEL_BUTTON_WIDTH)) {
271                                 if (nlaedit_is_tweakmode_on(ac) == 0) {
272                                         /* 'push-down' action - only usable when not in TweakMode */
273                                         // TODO: make this use the operator instead of calling the function directly
274                                         //      however, calling the operator requires that we supply the args, and that works with proper buttons only
275                                         BKE_nla_action_pushdown(adt);
276                                 }
277                                 else {
278                                         /* when in tweakmode, this button becomes the toggle for mapped editing */
279                                         adt->flag ^= ADT_NLA_EDIT_NOMAP;
280                                 }
281                                 
282                                 /* changes to NLA-Action occurred */
283                                 notifierFlags |= ND_NLA_ACTCHANGE;
284                         }
285                 }
286                         break;
287                         
288                 default:
289                         if (G.f & G_DEBUG)
290                                 printf("Error: Invalid channel type in mouse_nla_channels() \n");
291         }
292         
293         /* free channels */
294         BLI_freelistN(&anim_data);
295         
296         /* return the notifier-flags set */
297         return notifierFlags;
298 }
299
300 /* ------------------- */
301
302 /* handle clicking */
303 static int nlachannels_mouseclick_invoke(bContext *C, wmOperator *op, wmEvent *event)
304 {
305         bAnimContext ac;
306         SpaceNla *snla;
307         ARegion *ar;
308         View2D *v2d;
309         int channel_index;
310         int notifierFlags = 0;
311         short selectmode;
312         float x, y;
313         
314         /* get editor data */
315         if (ANIM_animdata_get_context(C, &ac) == 0)
316                 return OPERATOR_CANCELLED;
317                 
318         /* get useful pointers from animation context data */
319         snla= (SpaceNla *)ac.sl;
320         ar= ac.ar;
321         v2d= &ar->v2d;
322         
323         /* select mode is either replace (deselect all, then add) or add/extend */
324         if (RNA_boolean_get(op->ptr, "extend"))
325                 selectmode= SELECT_INVERT;
326         else
327                 selectmode= SELECT_REPLACE;
328         
329         /* figure out which channel user clicked in 
330          * Note: although channels technically start at y= NLACHANNEL_FIRST, we need to adjust by half a channel's height
331          *              so that the tops of channels get caught ok. Since NLACHANNEL_FIRST is really NLACHANNEL_HEIGHT, we simply use
332          *              NLACHANNEL_HEIGHT_HALF.
333          */
334         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, &y);
335         UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
336         
337         /* handle mouse-click in the relevant channel then */
338         notifierFlags= mouse_nla_channels(&ac, x, channel_index, selectmode);
339         
340         /* set notifier that things have changed */
341         WM_event_add_notifier(C, NC_ANIMATION|notifierFlags, NULL);
342         
343         return OPERATOR_FINISHED;
344 }
345  
346 void NLA_OT_channels_click (wmOperatorType *ot)
347 {
348         /* identifiers */
349         ot->name= "Mouse Click on NLA Channels";
350         ot->idname= "NLA_OT_channels_click";
351         ot->description= "Handle clicks to select NLA channels";
352         
353         /* api callbacks */
354         ot->invoke= nlachannels_mouseclick_invoke;
355         ot->poll= ED_operator_nla_active;
356         
357         /* flags */
358         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
359         
360         /* id-props */
361         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
362 }
363
364 /* *********************************************** */
365 /* Special Operators */
366
367 /* ******************** Add Tracks Operator ***************************** */
368 /* Add NLA Tracks to the same AnimData block as a selected track, or above the selected tracks */
369
370 static int nlaedit_add_tracks_exec (bContext *C, wmOperator *op)
371 {
372         bAnimContext ac;
373         
374         ListBase anim_data = {NULL, NULL};
375         bAnimListElem *ale;
376         int filter;
377         
378         AnimData *lastAdt = NULL;
379         short above_sel= RNA_boolean_get(op->ptr, "above_selected");
380         
381         /* get editor data */
382         if (ANIM_animdata_get_context(C, &ac) == 0)
383                 return OPERATOR_CANCELLED;
384                 
385         /* get a list of the AnimData blocks being shown in the NLA */
386         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
387         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
388         
389         /* add tracks... */
390         for (ale= anim_data.first; ale; ale= ale->next) {
391                 if (ale->type == ANIMTYPE_NLATRACK) {
392                         NlaTrack *nlt= (NlaTrack *)ale->data;
393                         AnimData *adt= ale->adt;
394                         
395                         /* check if just adding a new track above this one,
396                          * or whether we're adding a new one to the top of the stack that this one belongs to
397                          */
398                         if (above_sel) {
399                                 /* just add a new one above this one */
400                                 add_nlatrack(adt, nlt);
401                         }
402                         else if ((lastAdt == NULL) || (adt != lastAdt)) {
403                                 /* add one track to the top of the owning AnimData's stack, then don't add anymore to this stack */
404                                 add_nlatrack(adt, NULL);
405                                 lastAdt= adt;
406                         }
407                 }
408         }
409         
410         /* free temp data */
411         BLI_freelistN(&anim_data);
412         
413         /* set notifier that things have changed */
414         WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
415         
416         /* done */
417         return OPERATOR_FINISHED;
418 }
419
420 void NLA_OT_tracks_add (wmOperatorType *ot)
421 {
422         /* identifiers */
423         ot->name= "Add Track(s)";
424         ot->idname= "NLA_OT_tracks_add";
425         ot->description= "Add NLA-Tracks above/after the selected tracks";
426         
427         /* api callbacks */
428         ot->exec= nlaedit_add_tracks_exec;
429         ot->poll= nlaop_poll_tweakmode_off;
430         
431         /* flags */
432         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
433         
434         /* properties */
435         RNA_def_boolean(ot->srna, "above_selected", 0, "Above Selected", "Add a new NLA Track above every existing selected one.");
436 }
437
438 /* ******************** Delete Tracks Operator ***************************** */
439 /* Delete selected NLA Tracks */
440
441 static int nlaedit_delete_tracks_exec (bContext *C, wmOperator *UNUSED(op))
442 {
443         bAnimContext ac;
444         
445         ListBase anim_data = {NULL, NULL};
446         bAnimListElem *ale;
447         int filter;
448         
449         /* get editor data */
450         if (ANIM_animdata_get_context(C, &ac) == 0)
451                 return OPERATOR_CANCELLED;
452                 
453         /* get a list of the AnimData blocks being shown in the NLA */
454         filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
455         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
456         
457         /* delete tracks */
458         for (ale= anim_data.first; ale; ale= ale->next) {
459                 if(ale->type == ANIMTYPE_NLATRACK) {
460                         NlaTrack *nlt= (NlaTrack *)ale->data;
461                         AnimData *adt= ale->adt;
462                         
463                         /* if track is currently 'solo', then AnimData should have its
464                          * 'has solo' flag disabled
465                          */
466                         if (nlt->flag & NLATRACK_SOLO)
467                                 adt->flag &= ~ADT_NLA_SOLO_TRACK;
468                         
469                         /* call delete on this track - deletes all strips too */
470                         free_nlatrack(&adt->nla_tracks, nlt);
471                 }
472         }
473         
474         /* free temp data */
475         BLI_freelistN(&anim_data);
476         
477         /* set notifier that things have changed */
478         WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
479         
480         /* done */
481         return OPERATOR_FINISHED;
482 }
483
484 void NLA_OT_delete_tracks (wmOperatorType *ot)
485 {
486         /* identifiers */
487         ot->name= "Delete Tracks";
488         ot->idname= "NLA_OT_delete_tracks";
489         ot->description= "Delete selected NLA-Tracks and the strips they contain";
490         
491         /* api callbacks */
492         ot->exec= nlaedit_delete_tracks_exec;
493         ot->poll= nlaop_poll_tweakmode_off;
494         
495         /* flags */
496         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
497 }
498
499 /* *********************************************** */