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