remove unused includes from editors/space_*
[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 "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_animsys.h"
45 #include "BKE_nla.h"
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_screen.h"
49
50 #include "ED_anim_api.h"
51 #include "ED_keyframes_edit.h"
52 #include "ED_screen.h"
53
54 #include "RNA_access.h"
55 #include "RNA_define.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "UI_view2d.h"
61
62 #include "nla_intern.h" // own include
63
64 /* *********************************************** */
65 /* Operators for NLA channels-list which need to be different from the standard Animation Editor ones */
66
67 /* ******************** Mouse-Click Operator *********************** */
68 /* Depending on the channel that was clicked on, the mouse click will activate whichever
69  * part of the channel is relevant.
70  *
71  * NOTE: eventually, this should probably be phased out when many of these things are replaced with buttons
72  *      --> Most channels are now selection only...
73  */
74
75 static int mouse_nla_channels (bAnimContext *ac, float x, int channel_index, short selectmode)
76 {
77         ListBase anim_data = {NULL, NULL};
78         bAnimListElem *ale;
79         int filter;
80         View2D *v2d= &ac->ar->v2d;
81         int notifierFlags = 0;
82         
83         /* get the channel that was clicked on */
84                 /* filter channels */
85         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CHANNELS);
86         filter= ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
87         
88                 /* get channel from index */
89         ale= BLI_findlink(&anim_data, channel_index);
90         if (ale == NULL) {
91                 /* channel not found */
92                 if (G.f & G_DEBUG)
93                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels() \n", channel_index);
94                 
95                 BLI_freelistN(&anim_data);
96                 return 0;
97         }
98         
99         /* action to take depends on what channel we've got */
100         // WARNING: must keep this in sync with the equivalent function in anim_channels_edit.c
101         switch (ale->type) {
102                 case ANIMTYPE_SCENE:
103                 {
104                         Scene *sce= (Scene *)ale->data;
105                         AnimData *adt= sce->adt;
106                         
107                         /* set selection status */
108                         if (selectmode == SELECT_INVERT) {
109                                 /* swap select */
110                                 sce->flag ^= SCE_DS_SELECTED;
111                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
112                         }
113                         else {
114                                 sce->flag |= SCE_DS_SELECTED;
115                                 if (adt) adt->flag |= ADT_UI_SELECTED;
116                         }
117                         
118                         notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
119                 }
120                         break;
121                 case ANIMTYPE_OBJECT:
122                 {
123                         bDopeSheet *ads= (bDopeSheet *)ac->data;
124                         Scene *sce= (Scene *)ads->source;
125                         Base *base= (Base *)ale->data;
126                         Object *ob= base->object;
127                         AnimData *adt= ob->adt;
128                         
129                         if (nlaedit_is_tweakmode_on(ac) == 0) {
130                                 /* set selection status */
131                                 if (selectmode == SELECT_INVERT) {
132                                         /* swap select */
133                                         base->flag ^= SELECT;
134                                         ob->flag= base->flag;
135                                         
136                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
137                                 }
138                                 else {
139                                         Base *b;
140                                         
141                                         /* deselect all */
142                                         // TODO: should this deselect all other types of channels too?
143                                         for (b= sce->base.first; b; b= b->next) {
144                                                 b->flag &= ~SELECT;
145                                                 b->object->flag= b->flag;
146                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED|ADT_UI_ACTIVE);
147                                         }
148                                         
149                                         /* select object now */
150                                         base->flag |= SELECT;
151                                         ob->flag |= SELECT;
152                                         if (adt) adt->flag |= ADT_UI_SELECTED;
153                                 }
154                                 
155                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
156                                         adt->flag |= ADT_UI_ACTIVE;
157                                 
158                                 /* notifiers - channel was selected */
159                                 notifierFlags |= (ND_ANIMCHAN|NA_SELECTED);
160                         }
161                 }
162                         break;
163                         
164                 case ANIMTYPE_FILLACTD: /* Action Expander */
165                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
166                 case ANIMTYPE_DSLAM:
167                 case ANIMTYPE_DSCAM:
168                 case ANIMTYPE_DSCUR:
169                 case ANIMTYPE_DSSKEY:
170                 case ANIMTYPE_DSWOR:
171                 case ANIMTYPE_DSNTREE:
172                 case ANIMTYPE_DSPART:
173                 case ANIMTYPE_DSMBALL:
174                 case ANIMTYPE_DSARM:
175                 case ANIMTYPE_DSMESH:
176                 case ANIMTYPE_DSTEX:
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         Scene *scene;
300         ARegion *ar;
301         View2D *v2d;
302         int mval[2], channel_index;
303         int notifierFlags = 0;
304         short selectmode;
305         float x, y;
306         
307         /* get editor data */
308         if (ANIM_animdata_get_context(C, &ac) == 0)
309                 return OPERATOR_CANCELLED;
310                 
311         /* get useful pointers from animation context data */
312         scene= ac.scene;
313         ar= ac.ar;
314         v2d= &ar->v2d;
315         
316         /* get mouse coordinates (in region coordinates) */
317         mval[0]= (event->x - ar->winrct.xmin);
318         mval[1]= (event->y - ar->winrct.ymin);
319         
320         /* select mode is either replace (deselect all, then add) or add/extend */
321         if (RNA_boolean_get(op->ptr, "extend"))
322                 selectmode= SELECT_INVERT;
323         else
324                 selectmode= SELECT_REPLACE;
325         
326         /* figure out which channel user clicked in 
327          * Note: although channels technically start at y= NLACHANNEL_FIRST, we need to adjust by half a channel's height
328          *              so that the tops of channels get caught ok. Since NLACHANNEL_FIRST is really NLACHANNEL_HEIGHT, we simply use
329          *              NLACHANNEL_HEIGHT_HALF.
330          */
331         UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
332         UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP, 0, (float)NLACHANNEL_HEIGHT_HALF, x, y, NULL, &channel_index);
333         
334         /* handle mouse-click in the relevant channel then */
335         notifierFlags= mouse_nla_channels(&ac, x, channel_index, selectmode);
336         
337         /* set notifier that things have changed */
338         WM_event_add_notifier(C, NC_ANIMATION|notifierFlags, NULL);
339         
340         return OPERATOR_FINISHED;
341 }
342  
343 void NLA_OT_channels_click (wmOperatorType *ot)
344 {
345         /* identifiers */
346         ot->name= "Mouse Click on Channels";
347         ot->idname= "NLA_OT_channels_click";
348         
349         /* api callbacks */
350         ot->invoke= nlachannels_mouseclick_invoke;
351         ot->poll= ED_operator_nla_active;
352         
353         /* flags */
354         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
355         
356         /* id-props */
357         RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
358 }
359
360 /* *********************************************** */
361 /* Special Operators */
362
363 /* ******************** Add Tracks Operator ***************************** */
364 /* Add NLA Tracks to the same AnimData block as a selected track, or above the selected tracks */
365
366 static int nlaedit_add_tracks_exec (bContext *C, wmOperator *op)
367 {
368         bAnimContext ac;
369         
370         ListBase anim_data = {NULL, NULL};
371         bAnimListElem *ale;
372         int filter;
373         
374         AnimData *lastAdt = NULL;
375         short above_sel= RNA_boolean_get(op->ptr, "above_selected");
376         
377         /* get editor data */
378         if (ANIM_animdata_get_context(C, &ac) == 0)
379                 return OPERATOR_CANCELLED;
380                 
381         /* get a list of the AnimData blocks being shown in the NLA */
382         filter= (ANIMFILTER_VISIBLE | ANIMFILTER_NLATRACKS | ANIMFILTER_SEL);
383         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
384         
385         /* add tracks... */
386         for (ale= anim_data.first; ale; ale= ale->next) {
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         /* 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 *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                 NlaTrack *nlt= (NlaTrack *)ale->data;
454                 AnimData *adt= ale->adt;
455                 
456                 /* call delete on this track - deletes all strips too */
457                 free_nlatrack(&adt->nla_tracks, nlt);
458         }
459         
460         /* free temp data */
461         BLI_freelistN(&anim_data);
462         
463         /* set notifier that things have changed */
464         WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
465         
466         /* done */
467         return OPERATOR_FINISHED;
468 }
469
470 void NLA_OT_delete_tracks (wmOperatorType *ot)
471 {
472         /* identifiers */
473         ot->name= "Delete Tracks";
474         ot->idname= "NLA_OT_delete_tracks";
475         ot->description= "Delete selected NLA-Tracks and the strips they contain";
476         
477         /* api callbacks */
478         ot->exec= nlaedit_delete_tracks_exec;
479         ot->poll= nlaop_poll_tweakmode_off;
480         
481         /* flags */
482         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
483 }
484
485 /* *********************************************** */