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