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