Merged changes in the trunk up to revision 39368.
[blender-staging.git] / source / blender / editors / space_nla / nla_buttons.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.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation, Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_nla/nla_buttons.c
30  *  \ingroup spnla
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <math.h>
37 #include <float.h>
38
39 #include "DNA_anim_types.h"
40
41 #include "BLI_utildefines.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_math.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_editVert.h"
48 #include "BLI_rand.h"
49
50 #include "BKE_nla.h"
51 #include "BKE_context.h"
52 #include "BKE_screen.h"
53
54
55 #include "WM_api.h"
56 #include "WM_types.h"
57
58 #include "RNA_access.h"
59
60 #include "ED_anim_api.h"
61 #include "ED_screen.h"
62
63 #include "UI_interface.h"
64 #include "UI_resources.h"
65
66 #include "nla_intern.h" // own include
67
68
69 /* ******************* nla editor space & buttons ************** */
70
71 #define B_NOP           1
72 #define B_REDR          2
73
74 /* -------------- */
75
76 static void do_nla_region_buttons(bContext *C, void *UNUSED(arg), int event)
77 {
78         //Scene *scene= CTX_data_scene(C);
79         
80         switch(event) {
81
82         }
83         
84         /* default for now */
85         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
86         WM_event_add_notifier(C, NC_SCENE|ND_TRANSFORM, NULL);
87 }
88
89 static int nla_panel_context(const bContext *C, PointerRNA *adt_ptr, PointerRNA *nlt_ptr, PointerRNA *strip_ptr)
90 {
91         bAnimContext ac;
92         bAnimListElem *ale= NULL;
93         ListBase anim_data = {NULL, NULL};
94         short found=0;
95         int filter;
96         
97         /* for now, only draw if we could init the anim-context info (necessary for all animation-related tools) 
98          * to work correctly is able to be correctly retrieved. There's no point showing empty panels?
99          */
100         if (ANIM_animdata_get_context(C, &ac) == 0) 
101                 return 0;
102         
103         /* extract list of active channel(s), of which we should only take the first one 
104          *      - we need the channels flag to get the active AnimData block when there are no NLA Tracks
105          */
106         filter= (ANIMFILTER_VISIBLE|ANIMFILTER_ACTIVE|ANIMFILTER_CHANNELS);
107         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
108         
109         for (ale= anim_data.first; ale; ale= ale->next) {
110                 switch (ale->type) {
111                         case ANIMTYPE_NLATRACK: /* NLA Track - The primary data type which should get caught */
112                         {
113                                 NlaTrack *nlt= (NlaTrack *)ale->data;
114                                 AnimData *adt= ale->adt;
115                                 
116                                 /* found it, now set the pointers */
117                                 if (adt_ptr) {
118                                         /* AnimData pointer */
119                                         RNA_pointer_create(ale->id, &RNA_AnimData, adt, adt_ptr);
120                                 }
121                                 if (nlt_ptr) {
122                                         /* NLA-Track pointer */
123                                         RNA_pointer_create(ale->id, &RNA_NlaTrack, nlt, nlt_ptr);
124                                 }
125                                 if (strip_ptr) {
126                                         /* NLA-Strip pointer */
127                                         NlaStrip *strip= BKE_nlastrip_find_active(nlt);
128                                         RNA_pointer_create(ale->id, &RNA_NlaStrip, strip, strip_ptr);
129                                 }
130                                 
131                                 found= 1;
132                         }
133                                 break;
134                                 
135                         case ANIMTYPE_SCENE:    /* Top-Level Widgets doubling up as datablocks */
136                         case ANIMTYPE_OBJECT:
137                         case ANIMTYPE_FILLACTD: /* Action Expander */
138                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
139                         case ANIMTYPE_DSLAM:
140                         case ANIMTYPE_DSCAM:
141                         case ANIMTYPE_DSCUR:
142                         case ANIMTYPE_DSSKEY:
143                         case ANIMTYPE_DSWOR:
144                         case ANIMTYPE_DSNTREE:
145                         case ANIMTYPE_DSPART:
146                         case ANIMTYPE_DSMBALL:
147                         case ANIMTYPE_DSARM:
148                         case ANIMTYPE_DSLINESTYLE:
149                         {
150                                 /* for these channels, we only do AnimData */
151                                 if (ale->id && ale->adt) {
152                                         if (adt_ptr) {
153                                                 /* AnimData pointer */
154                                                 RNA_pointer_create(ale->id, &RNA_AnimData, ale->adt, adt_ptr);
155                                                 
156                                                 /* set found status to -1, since setting to 1 would break the loop 
157                                                  * and potentially skip an active NLA-Track in some cases...
158                                                  */
159                                                 found= -1;
160                                         }
161                                 }
162                         }       
163                                 break;
164                 }
165                 
166                 if (found > 0)
167                         break;
168         }
169         
170         /* free temp data */
171         BLI_freelistN(&anim_data);
172         
173         return found;
174 }
175
176 #if 0
177 static int nla_panel_poll(const bContext *C, PanelType *pt)
178 {
179         return nla_panel_context(C, NULL, NULL);
180 }
181 #endif
182
183 static int nla_animdata_panel_poll(const bContext *C, PanelType *UNUSED(pt))
184 {
185         PointerRNA ptr;
186         return (nla_panel_context(C, &ptr, NULL, NULL) && (ptr.data != NULL));
187 }
188
189 static int nla_track_panel_poll(const bContext *C, PanelType *UNUSED(pt))
190 {
191         PointerRNA ptr;
192         return (nla_panel_context(C, NULL, &ptr, NULL) && (ptr.data != NULL));
193 }
194
195 static int nla_strip_panel_poll(const bContext *C, PanelType *UNUSED(pt))
196 {
197         PointerRNA ptr;
198         return (nla_panel_context(C, NULL, NULL, &ptr) && (ptr.data != NULL));
199 }
200
201 static int nla_strip_actclip_panel_poll(const bContext *C, PanelType *UNUSED(pt))
202 {
203         PointerRNA ptr;
204         NlaStrip *strip;
205         
206         if (!nla_panel_context(C, NULL, NULL, &ptr))
207                 return 0;
208         if (ptr.data == NULL)
209                 return 0;
210         
211         strip= ptr.data;
212         return (strip->type == NLASTRIP_TYPE_CLIP);
213 }
214
215 /* -------------- */
216
217 /* active AnimData */
218 static void nla_panel_animdata (const bContext *C, Panel *pa)
219 {
220         PointerRNA adt_ptr;
221         /* AnimData *adt; */
222         uiLayout *layout= pa->layout;
223         uiLayout *row;
224         uiBlock *block;
225         
226         /* check context and also validity of pointer */
227         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
228                 return;
229
230         /* adt= adt_ptr.data; */
231         
232         block= uiLayoutGetBlock(layout);
233         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
234         
235         /* Active Action Properties ------------------------------------- */
236         /* action */
237         row= uiLayoutRow(layout, 1);
238                 uiTemplateID(row, (bContext *)C, &adt_ptr, "action", "ACTION_OT_new", NULL, NULL /*"ACTION_OT_unlink"*/); // XXX: need to make these operators
239         
240         /* extrapolation */
241         row= uiLayoutRow(layout, 1);
242                 uiItemR(row, &adt_ptr, "action_extrapolation", 0, NULL, ICON_NONE);
243         
244         /* blending */
245         row= uiLayoutRow(layout, 1);
246                 uiItemR(row, &adt_ptr, "action_blend_type", 0, NULL, ICON_NONE);
247                 
248         /* influence */
249         row= uiLayoutRow(layout, 1);
250                 uiItemR(row, &adt_ptr, "action_influence", 0, NULL, ICON_NONE);
251 }
252
253 /* active NLA-Track */
254 static void nla_panel_track (const bContext *C, Panel *pa)
255 {
256         PointerRNA nlt_ptr;
257         uiLayout *layout= pa->layout;
258         uiLayout *row;
259         uiBlock *block;
260         
261         /* check context and also validity of pointer */
262         if (!nla_panel_context(C, NULL, &nlt_ptr, NULL))
263                 return;
264         
265         block= uiLayoutGetBlock(layout);
266         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
267         
268         /* Info - Active NLA-Context:Track ----------------------  */
269         row= uiLayoutRow(layout, 1);
270                 uiItemR(row, &nlt_ptr, "name", 0, NULL, ICON_NLA);
271 }
272
273 /* generic settings for active NLA-Strip */
274 static void nla_panel_properties(const bContext *C, Panel *pa)
275 {
276         PointerRNA strip_ptr;
277         uiLayout *layout= pa->layout;
278         uiLayout *column, *row, *subcol;
279         uiBlock *block;
280         
281         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
282                 return;
283         
284         block= uiLayoutGetBlock(layout);
285         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
286         
287         /* Strip Properties ------------------------------------- */
288         /* strip type */
289         row= uiLayoutColumn(layout, 1);
290                 uiItemR(row, &strip_ptr, "name", 0, NULL, ICON_NLA); // XXX icon?
291                 uiItemR(row, &strip_ptr, "type", 0, NULL, ICON_NONE);
292         
293         /* strip extents */
294         column= uiLayoutColumn(layout, 1);
295                 uiItemL(column, "Strip Extents:", ICON_NONE);
296                 uiItemR(column, &strip_ptr, "frame_start", 0, NULL, ICON_NONE);
297                 uiItemR(column, &strip_ptr, "frame_end", 0, NULL, ICON_NONE);
298         
299         /* extrapolation */
300         row= uiLayoutRow(layout, 1);
301                 uiItemR(row, &strip_ptr, "extrapolation", 0, NULL, ICON_NONE);
302         
303         /* blending */
304         row= uiLayoutRow(layout, 1);
305                 uiItemR(row, &strip_ptr, "blend_type", 0, NULL, ICON_NONE);
306                 
307         /* blend in/out + autoblending
308          *      - blend in/out can only be set when autoblending is off
309          */
310         column= uiLayoutColumn(layout, 1);
311                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_influence")==0); 
312                 uiItemR(column, &strip_ptr, "use_auto_blend", 0, NULL, ICON_NONE); // XXX as toggle?
313                 
314                 subcol= uiLayoutColumn(column, 1);
315                         uiLayoutSetActive(subcol, RNA_boolean_get(&strip_ptr, "use_auto_blend")==0); 
316                         uiItemR(subcol, &strip_ptr, "blend_in", 0, NULL, ICON_NONE);
317                         uiItemR(subcol, &strip_ptr, "blend_out", 0, NULL, ICON_NONE);
318                 
319         /* settings */
320         column= uiLayoutColumn(layout, 1);
321                 uiLayoutSetActive(column, !(RNA_boolean_get(&strip_ptr, "use_animated_influence") || RNA_boolean_get(&strip_ptr, "use_animated_time"))); 
322                 uiItemL(column, "Playback Settings:", ICON_NONE);
323                 uiItemR(column, &strip_ptr, "mute", 0, NULL, ICON_NONE);
324                 uiItemR(column, &strip_ptr, "use_reverse", 0, NULL, ICON_NONE);
325 }
326
327
328 /* action-clip only settings for active NLA-Strip */
329 static void nla_panel_actclip(const bContext *C, Panel *pa)
330 {
331         PointerRNA strip_ptr;
332         uiLayout *layout= pa->layout;
333         uiLayout *column, *row;
334         uiBlock *block;
335
336         /* check context and also validity of pointer */
337         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
338                 return;
339         
340         block= uiLayoutGetBlock(layout);
341         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
342                 
343         /* Strip Properties ------------------------------------- */
344         /* action pointer */
345         row= uiLayoutRow(layout, 1);
346                 uiItemR(row, &strip_ptr, "action", 0, NULL, ICON_ACTION);
347                 
348         /* action extents */
349         // XXX custom names were used here (to avoid the prefixes)... probably not necessary in future?
350         column= uiLayoutColumn(layout, 1);
351                 uiItemL(column, "Action Extents:", ICON_NONE);
352                 uiItemR(column, &strip_ptr, "action_frame_start", 0, "Start Frame", ICON_NONE);
353                 uiItemR(column, &strip_ptr, "action_frame_end", 0, "End Frame", ICON_NONE);
354                 uiItemO(column, NULL, ICON_NONE, "NLA_OT_action_sync_length");
355                 
356         /* action usage */
357         column= uiLayoutColumn(layout, 1);
358                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_time")==0); 
359                 uiItemL(column, "Playback Settings:", ICON_NONE);
360                 uiItemR(column, &strip_ptr, "scale", 0, NULL, ICON_NONE);
361                 uiItemR(column, &strip_ptr, "repeat", 0, NULL, ICON_NONE);
362 }
363
364 /* evaluation settings for active NLA-Strip */
365 static void nla_panel_evaluation(const bContext *C, Panel *pa)
366 {
367         PointerRNA strip_ptr;
368         uiLayout *layout= pa->layout;
369         uiLayout *column, *subcolumn, *subrow;
370         uiBlock *block;
371
372         /* check context and also validity of pointer */
373         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
374                 return;
375                 
376         block= uiLayoutGetBlock(layout);
377         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
378                 
379         column= uiLayoutColumn(layout, 1);
380                 uiItemR(column, &strip_ptr, "use_animated_influence", 0, NULL, ICON_NONE);
381                 
382                 subcolumn= uiLayoutColumn(column, 1);
383                 uiLayoutSetEnabled(subcolumn, RNA_boolean_get(&strip_ptr, "use_animated_influence"));   
384                         uiItemR(subcolumn, &strip_ptr, "influence", 0, NULL, ICON_NONE);
385                 
386         
387         column= uiLayoutColumn(layout, 1);
388                 subrow= uiLayoutRow(column, 0);
389                 uiItemR(subrow, &strip_ptr, "use_animated_time", 0, NULL, ICON_NONE);
390                 uiItemR(subrow, &strip_ptr, "use_animated_time_cyclic", 0, NULL, ICON_NONE);
391
392                 subcolumn= uiLayoutColumn(column, 1);
393                 subrow= uiLayoutRow(subcolumn, 0);
394                 uiLayoutSetEnabled(subrow, RNA_boolean_get(&strip_ptr, "use_animated_time"));
395                         uiItemR(subcolumn, &strip_ptr, "strip_time", 0, NULL, ICON_NONE);
396 }
397
398 /* F-Modifiers for active NLA-Strip */
399 static void nla_panel_modifiers(const bContext *C, Panel *pa)
400 {
401         PointerRNA strip_ptr;
402         NlaStrip *strip;
403         FModifier *fcm;
404         uiLayout *col, *row;
405         uiBlock *block;
406
407         /* check context and also validity of pointer */
408         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
409                 return;
410         strip= strip_ptr.data;
411                 
412         block= uiLayoutGetBlock(pa->layout);
413         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
414         
415         /* 'add modifier' button at top of panel */
416         {
417                 row= uiLayoutRow(pa->layout, 0);
418                 block= uiLayoutGetBlock(row);
419                 
420                 // XXX for now, this will be a operator button which calls a temporary 'add modifier' operator
421                 // FIXME: we need to set the only-active property so that this will only add modifiers for the active strip (not all selected)
422                 uiDefButO(block, BUT, "NLA_OT_fmodifier_add", WM_OP_INVOKE_REGION_WIN, "Add Modifier", 10, 0, 150, 20, "Adds a new F-Modifier for the active NLA Strip");
423                 
424                 /* copy/paste (as sub-row)*/
425                 row= uiLayoutRow(row, 1);
426                         uiItemO(row, "", ICON_COPYDOWN, "NLA_OT_fmodifier_copy");
427                         uiItemO(row, "", ICON_PASTEDOWN, "NLA_OT_fmodifier_paste");
428         }
429         
430         /* draw each modifier */
431         for (fcm= strip->modifiers.first; fcm; fcm= fcm->next) {
432                 col= uiLayoutColumn(pa->layout, 1);
433                 
434                 ANIM_uiTemplate_fmodifier_draw(col, strip_ptr.id.data, &strip->modifiers, fcm);
435         }
436 }
437
438 /* ******************* general ******************************** */
439
440
441 void nla_buttons_register(ARegionType *art)
442 {
443         PanelType *pt;
444         
445         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata");
446         strcpy(pt->idname, "NLA_PT_animdata");
447         strcpy(pt->label, "Animation Data");
448         pt->draw= nla_panel_animdata;
449         pt->poll= nla_animdata_panel_poll;
450         pt->flag= PNL_DEFAULT_CLOSED;
451         BLI_addtail(&art->paneltypes, pt);
452         
453         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel track");
454         strcpy(pt->idname, "NLA_PT_track");
455         strcpy(pt->label, "Active Track");
456         pt->draw= nla_panel_track;
457         pt->poll= nla_track_panel_poll;
458         BLI_addtail(&art->paneltypes, pt);
459         
460         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
461         strcpy(pt->idname, "NLA_PT_properties");
462         strcpy(pt->label, "Active Strip");
463         pt->draw= nla_panel_properties;
464         pt->poll= nla_strip_panel_poll;
465         BLI_addtail(&art->paneltypes, pt);
466         
467         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
468         strcpy(pt->idname, "NLA_PT_actionclip");
469         strcpy(pt->label, "Action Clip");
470         pt->draw= nla_panel_actclip;
471         pt->poll= nla_strip_actclip_panel_poll;
472         BLI_addtail(&art->paneltypes, pt);
473         
474         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation");
475         strcpy(pt->idname, "NLA_PT_evaluation");
476         strcpy(pt->label, "Evaluation");
477         pt->draw= nla_panel_evaluation;
478         pt->poll= nla_strip_panel_poll;
479         BLI_addtail(&art->paneltypes, pt);
480         
481         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers");
482         strcpy(pt->idname, "NLA_PT_modifiers");
483         strcpy(pt->label, "Modifiers");
484         pt->draw= nla_panel_modifiers;
485         pt->poll= nla_strip_panel_poll;
486         BLI_addtail(&art->paneltypes, pt);
487 }
488
489 static int nla_properties(bContext *C, wmOperator *UNUSED(op))
490 {
491         ScrArea *sa= CTX_wm_area(C);
492         ARegion *ar= nla_has_buttons_region(sa);
493         
494         if(ar)
495                 ED_region_toggle_hidden(C, ar);
496
497         return OPERATOR_FINISHED;
498 }
499
500 void NLA_OT_properties(wmOperatorType *ot)
501 {
502         ot->name= "Properties";
503         ot->idname= "NLA_OT_properties";
504         ot->description= "Toggle display properties panel";
505         
506         ot->exec= nla_properties;
507         ot->poll= ED_operator_nla_active;
508
509         /* flags */
510         ot->flag= 0;
511 }