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