doxygen: blender/editors tagged.
[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         View2D *v2d= &ac->ar->v2d;
85         int notifierFlags = 0;
86         
87         /* get the channel that was clicked on */
88                 /* filter channels */
89         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CHANNELS);
90         filter= 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.f & G_DEBUG)
97                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels() \n", channel_index);
98                 
99                 BLI_freelistN(&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                 }
124                         break;
125                 case ANIMTYPE_OBJECT:
126                 {
127                         bDopeSheet *ads= (bDopeSheet *)ac->data;
128                         Scene *sce= (Scene *)ads->source;
129                         Base *base= (Base *)ale->data;
130                         Object *ob= base->object;
131                         AnimData *adt= ob->adt;
132                         
133                         if (nlaedit_is_tweakmode_on(ac) == 0) {
134                                 /* set selection status */
135                                 if (selectmode == SELECT_INVERT) {
136                                         /* swap select */
137                                         base->flag ^= SELECT;
138                                         ob->flag= base->flag;
139                                         
140                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
141                                 }
142                                 else {
143                                         Base *b;
144                                         
145                                         /* deselect all */
146                                         // TODO: should this deselect all other types of channels too?
147                                         for (b= sce->base.first; b; b= b->next) {
148                                                 b->flag &= ~SELECT;
149                                                 b->object->flag= b->flag;
150                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED|ADT_UI_ACTIVE);
151                                         }
152                                         
153                                         /* select object now */
154                                         base->flag |= SELECT;
155                                         ob->flag |= SELECT;
156                                         if (adt) adt->flag |= ADT_UI_SELECTED;
157                                 }
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                 }
166                         break;
167                         
168                 case ANIMTYPE_FILLACTD: /* Action Expander */
169                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
170                 case ANIMTYPE_DSLAM:
171                 case ANIMTYPE_DSCAM:
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                 {
183                         /* sanity checking... */
184                         if (ale->adt) {
185                                 /* select/deselect */
186                                 if (selectmode == SELECT_INVERT) {
187                                         /* inverse selection status of this AnimData block only */
188                                         ale->adt->flag ^= ADT_UI_SELECTED;
189                                 }
190                                 else {
191                                         /* select AnimData block by itself */
192                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
193                                         ale->adt->flag |= ADT_UI_SELECTED;
194                                 }
195                                 
196                                 /* set active? */
197                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
198                                         ale->adt->flag |= ADT_UI_ACTIVE;
199                         }
200                         
201                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
202                 }       
203                         break;
204                         
205                 case ANIMTYPE_NLATRACK:
206                 {
207                         NlaTrack *nlt= (NlaTrack *)ale->data;
208                         AnimData *adt= ale->adt;
209                         short offset;
210                         
211                         /* offset for start of channel (on LHS of channel-list) */
212                         if (ale->id) {
213                                 /* special exception for materials and particles */
214                                 if (ELEM(GS(ale->id->name),ID_MA,ID_PA))
215                                         offset= 21 + NLACHANNEL_BUTTON_WIDTH;
216                                 else
217                                         offset= 14;
218                         }
219                         else
220                                 offset= 0;
221                         
222                         if (x >= (v2d->cur.xmax-NLACHANNEL_BUTTON_WIDTH)) {
223                                 /* toggle protection (only if there's a toggle there) */
224                                 nlt->flag ^= NLATRACK_PROTECTED;
225                                 
226                                 /* notifier flags - channel was edited */
227                                 notifierFlags |= (ND_ANIMCHAN|NA_EDITED);
228                         }
229                         else if (x >= (v2d->cur.xmax-2*NLACHANNEL_BUTTON_WIDTH)) {
230                                 /* toggle mute */
231                                 nlt->flag ^= NLATRACK_MUTED;
232                                 
233                                 /* notifier flags - channel was edited */
234                                 notifierFlags |= (ND_ANIMCHAN|NA_EDITED);
235                         }
236                         else if (x <= ((NLACHANNEL_BUTTON_WIDTH*2)+offset)) {
237                                 /* toggle 'solo' */
238                                 BKE_nlatrack_solo_toggle(adt, nlt);
239                                 
240                                 /* notifier flags - channel was edited */
241                                 notifierFlags |= (ND_ANIMCHAN|NA_EDITED);
242                         }
243                         else if (nlaedit_is_tweakmode_on(ac) == 0) {
244                                 /* set selection */
245                                 if (selectmode == SELECT_INVERT) {
246                                         /* inverse selection status of this F-Curve only */
247                                         nlt->flag ^= NLATRACK_SELECTED;
248                                 }
249                                 else {
250                                         /* select F-Curve by itself */
251                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
252                                         nlt->flag |= NLATRACK_SELECTED;
253                                 }
254                                 
255                                 /* if NLA-Track is selected now, make NLA-Track the 'active' one in the visible list */
256                                 if (nlt->flag & NLATRACK_SELECTED)
257                                         ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, nlt, ANIMTYPE_NLATRACK);
258                                         
259                                 /* notifier flags - channel was selected */
260                                 notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
261                         }
262                 }
263                         break;
264                 case ANIMTYPE_NLAACTION:
265                 {
266                         AnimData *adt= BKE_animdata_from_id(ale->id);
267                         
268                         if (x >= (v2d->cur.xmax-NLACHANNEL_BUTTON_WIDTH)) {
269                                 if (nlaedit_is_tweakmode_on(ac) == 0) {
270                                         /* 'push-down' action - only usable when not in TweakMode */
271                                         // TODO: make this use the operator instead of calling the function directly
272                                         //      however, calling the operator requires that we supply the args, and that works with proper buttons only
273                                         BKE_nla_action_pushdown(adt);
274                                 }
275                                 else {
276                                         /* when in tweakmode, this button becomes the toggle for mapped editing */
277                                         adt->flag ^= ADT_NLA_EDIT_NOMAP;
278                                 }
279                                 
280                                 /* changes to NLA-Action occurred */
281                                 notifierFlags |= ND_NLA_ACTCHANGE;
282                         }
283                 }
284                         break;
285                         
286                 default:
287                         if (G.f & G_DEBUG)
288                                 printf("Error: Invalid channel type in mouse_nla_channels() \n");
289         }
290         
291         /* free channels */
292         BLI_freelistN(&anim_data);
293         
294         /* return the notifier-flags set */
295         return notifierFlags;
296 }
297
298 /* ------------------- */
299
300 /* handle clicking */
301 static int nlachannels_mouseclick_invoke(bContext *C, wmOperator *op, wmEvent *event)
302 {
303         bAnimContext ac;
304         ARegion *ar;
305         View2D *v2d;
306         int mval[2], channel_index;
307         int notifierFlags = 0;
308         short selectmode;
309         float x, y;
310         
311         /* get editor data */
312         if (ANIM_animdata_get_context(C, &ac) == 0)
313                 return OPERATOR_CANCELLED;
314                 
315         /* get useful pointers from animation context data */
316         ar= ac.ar;
317         v2d= &ar->v2d;
318         
319         /* get mouse coordinates (in region coordinates) */
320         mval[0]= (event->x - ar->winrct.xmin);
321         mval[1]= (event->y - ar->winrct.ymin);
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, mval[0], mval[1], &x, &y);
335         UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP, 0, (float)NLACHANNEL_HEIGHT_HALF, 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_VISIBLE | ANIMFILTER_NLATRACKS | 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_VISIBLE | ANIMFILTER_NLATRACKS | 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 /* *********************************************** */