remove/comment unused vars
[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 Channels";
345         ot->idname= "NLA_OT_channels_click";
346         
347         /* api callbacks */
348         ot->invoke= nlachannels_mouseclick_invoke;
349         ot->poll= ED_operator_nla_active;
350         
351         /* flags */
352         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
353         
354         /* id-props */
355         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
356 }
357
358 /* *********************************************** */
359 /* Special Operators */
360
361 /* ******************** Add Tracks Operator ***************************** */
362 /* Add NLA Tracks to the same AnimData block as a selected track, or above the selected tracks */
363
364 static int nlaedit_add_tracks_exec (bContext *C, wmOperator *op)
365 {
366         bAnimContext ac;
367         
368         ListBase anim_data = {NULL, NULL};
369         bAnimListElem *ale;
370         int filter;
371         
372         AnimData *lastAdt = NULL;
373         short above_sel= RNA_boolean_get(op->ptr, "above_selected");
374         
375         /* get editor data */
376         if (ANIM_animdata_get_context(C, &ac) == 0)
377                 return OPERATOR_CANCELLED;
378                 
379         /* get a list of the AnimData blocks being shown in the NLA */
380         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_NLATRACKS | ANIMFILTER_SEL);
381         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
382         
383         /* add tracks... */
384         for (ale= anim_data.first; ale; ale= ale->next) {
385                 if(ale->type == ANIMTYPE_NLATRACK) {
386                         NlaTrack *nlt= (NlaTrack *)ale->data;
387                         AnimData *adt= ale->adt;
388                         
389                         /* check if just adding a new track above this one,
390                          * or whether we're adding a new one to the top of the stack that this one belongs to
391                          */
392                         if (above_sel) {
393                                 /* just add a new one above this one */
394                                 add_nlatrack(adt, nlt);
395                         }
396                         else if ((lastAdt == NULL) || (adt != lastAdt)) {
397                                 /* add one track to the top of the owning AnimData's stack, then don't add anymore to this stack */
398                                 add_nlatrack(adt, NULL);
399                                 lastAdt= adt;
400                         }
401                 }
402         }
403         
404         /* free temp data */
405         BLI_freelistN(&anim_data);
406         
407         /* set notifier that things have changed */
408         WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
409         
410         /* done */
411         return OPERATOR_FINISHED;
412 }
413
414 void NLA_OT_tracks_add (wmOperatorType *ot)
415 {
416         /* identifiers */
417         ot->name= "Add Track(s)";
418         ot->idname= "NLA_OT_tracks_add";
419         ot->description= "Add NLA-Tracks above/after the selected tracks";
420         
421         /* api callbacks */
422         ot->exec= nlaedit_add_tracks_exec;
423         ot->poll= nlaop_poll_tweakmode_off;
424         
425         /* flags */
426         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
427         
428         /* properties */
429         RNA_def_boolean(ot->srna, "above_selected", 0, "Above Selected", "Add a new NLA Track above every existing selected one.");
430 }
431
432 /* ******************** Delete Tracks Operator ***************************** */
433 /* Delete selected NLA Tracks */
434
435 static int nlaedit_delete_tracks_exec (bContext *C, wmOperator *UNUSED(op))
436 {
437         bAnimContext ac;
438         
439         ListBase anim_data = {NULL, NULL};
440         bAnimListElem *ale;
441         int filter;
442         
443         /* get editor data */
444         if (ANIM_animdata_get_context(C, &ac) == 0)
445                 return OPERATOR_CANCELLED;
446                 
447         /* get a list of the AnimData blocks being shown in the NLA */
448         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_NLATRACKS | ANIMFILTER_SEL);
449         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
450         
451         /* delete tracks */
452         for (ale= anim_data.first; ale; ale= ale->next) {
453                 if(ale->type == ANIMTYPE_NLATRACK) {
454                         NlaTrack *nlt= (NlaTrack *)ale->data;
455                         AnimData *adt= ale->adt;
456                         
457                         /* if track is currently 'solo', then AnimData should have its
458                          * 'has solo' flag disabled
459                          */
460                         if (nlt->flag & NLATRACK_SOLO)
461                                 adt->flag &= ~ADT_NLA_SOLO_TRACK;
462                         
463                         /* call delete on this track - deletes all strips too */
464                         free_nlatrack(&adt->nla_tracks, nlt);
465                 }
466         }
467         
468         /* free temp data */
469         BLI_freelistN(&anim_data);
470         
471         /* set notifier that things have changed */
472         WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
473         
474         /* done */
475         return OPERATOR_FINISHED;
476 }
477
478 void NLA_OT_delete_tracks (wmOperatorType *ot)
479 {
480         /* identifiers */
481         ot->name= "Delete Tracks";
482         ot->idname= "NLA_OT_delete_tracks";
483         ot->description= "Delete selected NLA-Tracks and the strips they contain";
484         
485         /* api callbacks */
486         ot->exec= nlaedit_delete_tracks_exec;
487         ot->poll= nlaop_poll_tweakmode_off;
488         
489         /* flags */
490         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
491 }
492
493 /* *********************************************** */