code cleanup: use const events for modal and invoke operators.
[blender.git] / source / blender / editors / space_nla / nla_channels.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Joshua Leung (major recode)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_nla/nla_channels.c
28  *  \ingroup spnla
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36
37 #include "DNA_anim_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_rand.h"
44 #include "BLI_utildefines.h"
45
46 #include "BKE_animsys.h"
47 #include "BKE_nla.h"
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_screen.h"
51
52 #include "ED_anim_api.h"
53 #include "ED_keyframes_edit.h"
54 #include "ED_screen.h"
55
56 #include "RNA_access.h"
57 #include "RNA_define.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "UI_view2d.h"
63
64 #include "nla_intern.h" // own include
65
66 /* *********************************************** */
67 /* Operators for NLA channels-list which need to be different from the standard Animation Editor ones */
68
69 /* ******************** Mouse-Click Operator *********************** */
70 /* Depending on the channel that was clicked on, the mouse click will activate whichever
71  * part of the channel is relevant.
72  *
73  * NOTE: eventually, this should probably be phased out when many of these things are replaced with buttons
74  *      --> Most channels are now selection only...
75  */
76
77 static int mouse_nla_channels(bAnimContext *ac, float x, int channel_index, short selectmode)
78 {
79         ListBase anim_data = {NULL, NULL};
80         bAnimListElem *ale;
81         int filter;
82         
83         View2D *v2d = &ac->ar->v2d;
84         int notifierFlags = 0;
85         
86         /* get the channel that was clicked on */
87         /* filter channels */
88         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
89         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
90         
91         /* get channel from index */
92         ale = BLI_findlink(&anim_data, channel_index);
93         if (ale == NULL) {
94                 /* channel not found */
95                 if (G.debug & G_DEBUG)
96                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels()\n", channel_index);
97                 
98                 BLI_freelistN(&anim_data);
99                 return 0;
100         }
101         
102         /* action to take depends on what channel we've got */
103         // WARNING: must keep this in sync with the equivalent function in anim_channels_edit.c
104         switch (ale->type) {
105                 case ANIMTYPE_SCENE:
106                 {
107                         Scene *sce = (Scene *)ale->data;
108                         AnimData *adt = sce->adt;
109                         
110                         /* set selection status */
111                         if (selectmode == SELECT_INVERT) {
112                                 /* swap select */
113                                 sce->flag ^= SCE_DS_SELECTED;
114                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
115                         }
116                         else {
117                                 sce->flag |= SCE_DS_SELECTED;
118                                 if (adt) adt->flag |= ADT_UI_SELECTED;
119                         }
120                         
121                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
122                 }
123                 break;
124                 case ANIMTYPE_OBJECT:
125                 {
126                         bDopeSheet *ads = (bDopeSheet *)ac->data;
127                         Scene *sce = (Scene *)ads->source;
128                         Base *base = (Base *)ale->data;
129                         Object *ob = base->object;
130                         AnimData *adt = ob->adt;
131                         
132                         if (nlaedit_is_tweakmode_on(ac) == 0) {
133                                 /* set selection status */
134                                 if (selectmode == SELECT_INVERT) {
135                                         /* swap select */
136                                         base->flag ^= SELECT;
137                                         ob->flag = base->flag;
138                                         
139                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
140                                 }
141                                 else {
142                                         Base *b;
143
144                                         /* deselect all */
145                                         /* TODO: should this deselect all other types of channels too? */
146                                         for (b = sce->base.first; b; b = b->next) {
147                                                 b->flag &= ~SELECT;
148                                                 b->object->flag = b->flag;
149                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED | ADT_UI_ACTIVE);
150                                         }
151                                         
152                                         /* select object now */
153                                         base->flag |= SELECT;
154                                         ob->flag |= SELECT;
155                                         if (adt) adt->flag |= ADT_UI_SELECTED;
156                                 }
157                                 
158                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
159                                         adt->flag |= ADT_UI_ACTIVE;
160                                 
161                                 /* notifiers - channel was selected */
162                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
163                         }
164                 }
165                 break;
166                         
167                 case ANIMTYPE_FILLACTD: /* Action Expander */
168                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
169                 case ANIMTYPE_DSLAM:
170                 case ANIMTYPE_DSCAM:
171                 case ANIMTYPE_DSCUR:
172                 case ANIMTYPE_DSSKEY:
173                 case ANIMTYPE_DSWOR:
174                 case ANIMTYPE_DSNTREE:
175                 case ANIMTYPE_DSPART:
176                 case ANIMTYPE_DSMBALL:
177                 case ANIMTYPE_DSARM:
178                 case ANIMTYPE_DSMESH:
179                 case ANIMTYPE_DSTEX:
180                 case ANIMTYPE_DSLAT:
181                 case ANIMTYPE_DSSPK:
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.debug & 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, const wmEvent *event)
302 {
303         bAnimContext ac;
304         SpaceNla *snla;
305         ARegion *ar;
306         View2D *v2d;
307         int channel_index;
308         int notifierFlags = 0;
309         short selectmode;
310         float x, y;
311         
312         /* get editor data */
313         if (ANIM_animdata_get_context(C, &ac) == 0)
314                 return OPERATOR_CANCELLED;
315                 
316         /* get useful pointers from animation context data */
317         snla = (SpaceNla *)ac.sl;
318         ar = ac.ar;
319         v2d = &ar->v2d;
320         
321         /* select mode is either replace (deselect all, then add) or add/extend */
322         if (RNA_boolean_get(op->ptr, "extend"))
323                 selectmode = SELECT_INVERT;
324         else
325                 selectmode = SELECT_REPLACE;
326         
327         /* figure out which channel user clicked in 
328          * Note: although channels technically start at y= NLACHANNEL_FIRST, we need to adjust by half a channel's height
329          *              so that the tops of channels get caught ok. Since NLACHANNEL_FIRST is really NLACHANNEL_HEIGHT, we simply use
330          *              NLACHANNEL_HEIGHT_HALF.
331          */
332         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, &y);
333         UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
334         
335         /* handle mouse-click in the relevant channel then */
336         notifierFlags = mouse_nla_channels(&ac, x, channel_index, selectmode);
337         
338         /* set notifier that things have changed */
339         WM_event_add_notifier(C, NC_ANIMATION | notifierFlags, NULL);
340         
341         return OPERATOR_FINISHED;
342 }
343  
344 void NLA_OT_channels_click(wmOperatorType *ot)
345 {
346         PropertyRNA *prop;
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         /* props */
361         prop = RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
362         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
363 }
364
365 /* *********************************************** */
366 /* Special Operators */
367
368 /* ******************** Add Tracks Operator ***************************** */
369 /* Add NLA Tracks to the same AnimData block as a selected track, or above the selected tracks */
370
371 static int nlaedit_add_tracks_exec(bContext *C, wmOperator *op)
372 {
373         bAnimContext ac;
374         
375         ListBase anim_data = {NULL, NULL};
376         bAnimListElem *ale;
377         int filter;
378         
379         AnimData *lastAdt = NULL;
380         short above_sel = RNA_boolean_get(op->ptr, "above_selected");
381         
382         /* get editor data */
383         if (ANIM_animdata_get_context(C, &ac) == 0)
384                 return OPERATOR_CANCELLED;
385                 
386         /* get a list of the AnimData blocks being shown in the NLA */
387         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
388         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
389         
390         /* add tracks... */
391         for (ale = anim_data.first; ale; ale = ale->next) {
392                 if (ale->type == ANIMTYPE_NLATRACK) {
393                         NlaTrack *nlt = (NlaTrack *)ale->data;
394                         AnimData *adt = ale->adt;
395                         
396                         /* check if just adding a new track above this one,
397                          * or whether we're adding a new one to the top of the stack that this one belongs to
398                          */
399                         if (above_sel) {
400                                 /* just add a new one above this one */
401                                 add_nlatrack(adt, nlt);
402                         }
403                         else if ((lastAdt == NULL) || (adt != lastAdt)) {
404                                 /* add one track to the top of the owning AnimData's stack, then don't add anymore to this stack */
405                                 add_nlatrack(adt, NULL);
406                                 lastAdt = adt;
407                         }
408                 }
409         }
410         
411         /* free temp data */
412         BLI_freelistN(&anim_data);
413         
414         /* set notifier that things have changed */
415         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
416         
417         /* done */
418         return OPERATOR_FINISHED;
419 }
420
421 void NLA_OT_tracks_add(wmOperatorType *ot)
422 {
423         /* identifiers */
424         ot->name = "Add Track(s)";
425         ot->idname = "NLA_OT_tracks_add";
426         ot->description = "Add NLA-Tracks above/after the selected tracks";
427         
428         /* api callbacks */
429         ot->exec = nlaedit_add_tracks_exec;
430         ot->poll = nlaop_poll_tweakmode_off;
431         
432         /* flags */
433         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
434         
435         /* properties */
436         RNA_def_boolean(ot->srna, "above_selected", 0, "Above Selected", "Add a new NLA Track above every existing selected one");
437 }
438
439 /* ******************** Delete Tracks Operator ***************************** */
440 /* Delete selected NLA Tracks */
441
442 static int nlaedit_delete_tracks_exec(bContext *C, wmOperator *UNUSED(op))
443 {
444         bAnimContext ac;
445         
446         ListBase anim_data = {NULL, NULL};
447         bAnimListElem *ale;
448         int filter;
449         
450         /* get editor data */
451         if (ANIM_animdata_get_context(C, &ac) == 0)
452                 return OPERATOR_CANCELLED;
453                 
454         /* get a list of the AnimData blocks being shown in the NLA */
455         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL);
456         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
457         
458         /* delete tracks */
459         for (ale = anim_data.first; ale; ale = ale->next) {
460                 if (ale->type == ANIMTYPE_NLATRACK) {
461                         NlaTrack *nlt = (NlaTrack *)ale->data;
462                         AnimData *adt = ale->adt;
463                         
464                         /* if track is currently 'solo', then AnimData should have its
465                          * 'has solo' flag disabled
466                          */
467                         if (nlt->flag & NLATRACK_SOLO)
468                                 adt->flag &= ~ADT_NLA_SOLO_TRACK;
469                         
470                         /* call delete on this track - deletes all strips too */
471                         free_nlatrack(&adt->nla_tracks, nlt);
472                 }
473         }
474         
475         /* free temp data */
476         BLI_freelistN(&anim_data);
477         
478         /* set notifier that things have changed */
479         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
480         
481         /* done */
482         return OPERATOR_FINISHED;
483 }
484
485 void NLA_OT_delete_tracks(wmOperatorType *ot)
486 {
487         /* identifiers */
488         ot->name = "Delete Tracks";
489         ot->idname = "NLA_OT_delete_tracks";
490         ot->description = "Delete selected NLA-Tracks and the strips they contain";
491         
492         /* api callbacks */
493         ot->exec = nlaedit_delete_tracks_exec;
494         ot->poll = nlaop_poll_tweakmode_off;
495         
496         /* flags */
497         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
498 }
499
500 /* *********************************************** */