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