30e130e34ef6c90cb67b9dd8633903749ec85685
[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                         {
144                                 /* for these channels, we only do AnimData */
145                                 if (ale->id && ale->adt) {
146                                         if (adt_ptr) {
147                                                 /* AnimData pointer */
148                                                 RNA_pointer_create(ale->id, &RNA_AnimData, ale->adt, adt_ptr);
149                                                 
150                                                 /* set found status to -1, since setting to 1 would break the loop 
151                                                  * and potentially skip an active NLA-Track in some cases...
152                                                  */
153                                                 found= -1;
154                                         }
155                                 }
156                         }       
157                                 break;
158                 }
159                 
160                 if (found > 0)
161                         break;
162         }
163         
164         /* free temp data */
165         BLI_freelistN(&anim_data);
166         
167         return found;
168 }
169
170 #if 0
171 static int nla_panel_poll(const bContext *C, PanelType *pt)
172 {
173         return nla_panel_context(C, NULL, NULL);
174 }
175 #endif
176
177 static int nla_animdata_panel_poll(const bContext *C, PanelType *UNUSED(pt))
178 {
179         PointerRNA ptr;
180         return (nla_panel_context(C, &ptr, NULL, NULL) && (ptr.data != NULL));
181 }
182
183 static int nla_track_panel_poll(const bContext *C, PanelType *UNUSED(pt))
184 {
185         PointerRNA ptr;
186         return (nla_panel_context(C, NULL, &ptr, NULL) && (ptr.data != NULL));
187 }
188
189 static int nla_strip_panel_poll(const bContext *C, PanelType *UNUSED(pt))
190 {
191         PointerRNA ptr;
192         return (nla_panel_context(C, NULL, NULL, &ptr) && (ptr.data != NULL));
193 }
194
195 static int nla_strip_actclip_panel_poll(const bContext *C, PanelType *UNUSED(pt))
196 {
197         PointerRNA ptr;
198         NlaStrip *strip;
199         
200         if (!nla_panel_context(C, NULL, NULL, &ptr))
201                 return 0;
202         if (ptr.data == NULL)
203                 return 0;
204         
205         strip= ptr.data;
206         return (strip->type == NLASTRIP_TYPE_CLIP);
207 }
208
209 /* -------------- */
210
211 /* active AnimData */
212 static void nla_panel_animdata (const bContext *C, Panel *pa)
213 {
214         PointerRNA adt_ptr;
215         /* AnimData *adt; */
216         uiLayout *layout= pa->layout;
217         uiLayout *row;
218         uiBlock *block;
219         
220         /* check context and also validity of pointer */
221         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
222                 return;
223
224         /* adt= adt_ptr.data; */
225         
226         block= uiLayoutGetBlock(layout);
227         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
228         
229         /* Active Action Properties ------------------------------------- */
230         /* action */
231         row= uiLayoutRow(layout, 1);
232                 uiTemplateID(row, (bContext *)C, &adt_ptr, "action", "ACTION_OT_new", NULL, NULL /*"ACTION_OT_unlink"*/); // XXX: need to make these operators
233         
234         /* extrapolation */
235         row= uiLayoutRow(layout, 1);
236                 uiItemR(row, &adt_ptr, "action_extrapolation", 0, NULL, ICON_NONE);
237         
238         /* blending */
239         row= uiLayoutRow(layout, 1);
240                 uiItemR(row, &adt_ptr, "action_blend_type", 0, NULL, ICON_NONE);
241                 
242         /* influence */
243         row= uiLayoutRow(layout, 1);
244                 uiItemR(row, &adt_ptr, "action_influence", 0, NULL, ICON_NONE);
245 }
246
247 /* active NLA-Track */
248 static void nla_panel_track (const bContext *C, Panel *pa)
249 {
250         PointerRNA nlt_ptr;
251         uiLayout *layout= pa->layout;
252         uiLayout *row;
253         uiBlock *block;
254         
255         /* check context and also validity of pointer */
256         if (!nla_panel_context(C, NULL, &nlt_ptr, NULL))
257                 return;
258         
259         block= uiLayoutGetBlock(layout);
260         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
261         
262         /* Info - Active NLA-Context:Track ----------------------  */
263         row= uiLayoutRow(layout, 1);
264                 uiItemR(row, &nlt_ptr, "name", 0, NULL, ICON_NLA);
265 }
266
267 /* generic settings for active NLA-Strip */
268 static void nla_panel_properties(const bContext *C, Panel *pa)
269 {
270         PointerRNA strip_ptr;
271         uiLayout *layout= pa->layout;
272         uiLayout *column, *row, *subcol;
273         uiBlock *block;
274         
275         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
276                 return;
277         
278         block= uiLayoutGetBlock(layout);
279         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
280         
281         /* Strip Properties ------------------------------------- */
282         /* strip type */
283         row= uiLayoutColumn(layout, 1);
284                 uiItemR(row, &strip_ptr, "name", 0, NULL, ICON_NLA); // XXX icon?
285                 uiItemR(row, &strip_ptr, "type", 0, NULL, ICON_NONE);
286         
287         /* strip extents */
288         column= uiLayoutColumn(layout, 1);
289                 uiItemL(column, "Strip Extents:", ICON_NONE);
290                 uiItemR(column, &strip_ptr, "frame_start", 0, NULL, ICON_NONE);
291                 uiItemR(column, &strip_ptr, "frame_end", 0, NULL, ICON_NONE);
292         
293         /* extrapolation */
294         row= uiLayoutRow(layout, 1);
295                 uiItemR(row, &strip_ptr, "extrapolation", 0, NULL, ICON_NONE);
296         
297         /* blending */
298         row= uiLayoutRow(layout, 1);
299                 uiItemR(row, &strip_ptr, "blend_type", 0, NULL, ICON_NONE);
300                 
301         /* blend in/out + autoblending
302          *      - blend in/out can only be set when autoblending is off
303          */
304         column= uiLayoutColumn(layout, 1);
305                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_influence")==0); 
306                 uiItemR(column, &strip_ptr, "use_auto_blend", 0, NULL, ICON_NONE); // XXX as toggle?
307                 
308                 subcol= uiLayoutColumn(column, 1);
309                         uiLayoutSetActive(subcol, RNA_boolean_get(&strip_ptr, "use_auto_blend")==0); 
310                         uiItemR(subcol, &strip_ptr, "blend_in", 0, NULL, ICON_NONE);
311                         uiItemR(subcol, &strip_ptr, "blend_out", 0, NULL, ICON_NONE);
312                 
313         /* settings */
314         column= uiLayoutColumn(layout, 1);
315                 uiLayoutSetActive(column, !(RNA_boolean_get(&strip_ptr, "use_animated_influence") || RNA_boolean_get(&strip_ptr, "use_animated_time"))); 
316                 uiItemL(column, "Playback Settings:", ICON_NONE);
317                 uiItemR(column, &strip_ptr, "mute", 0, NULL, ICON_NONE);
318                 uiItemR(column, &strip_ptr, "use_reverse", 0, NULL, ICON_NONE);
319 }
320
321
322 /* action-clip only settings for active NLA-Strip */
323 static void nla_panel_actclip(const bContext *C, Panel *pa)
324 {
325         PointerRNA strip_ptr;
326         uiLayout *layout= pa->layout;
327         uiLayout *column, *row;
328         uiBlock *block;
329
330         /* check context and also validity of pointer */
331         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
332                 return;
333         
334         block= uiLayoutGetBlock(layout);
335         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
336                 
337         /* Strip Properties ------------------------------------- */
338         /* action pointer */
339         row= uiLayoutRow(layout, 1);
340                 uiItemR(row, &strip_ptr, "action", 0, NULL, ICON_ACTION);
341                 
342         /* action extents */
343         // XXX custom names were used here (to avoid the prefixes)... probably not necessary in future?
344         column= uiLayoutColumn(layout, 1);
345                 uiItemL(column, "Action Extents:", ICON_NONE);
346                 uiItemR(column, &strip_ptr, "action_frame_start", 0, "Start Frame", ICON_NONE);
347                 uiItemR(column, &strip_ptr, "action_frame_end", 0, "End Frame", ICON_NONE);
348                 uiItemO(column, NULL, ICON_NONE, "NLA_OT_action_sync_length");
349                 
350         /* action usage */
351         column= uiLayoutColumn(layout, 1);
352                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_time")==0); 
353                 uiItemL(column, "Playback Settings:", ICON_NONE);
354                 uiItemR(column, &strip_ptr, "scale", 0, NULL, ICON_NONE);
355                 uiItemR(column, &strip_ptr, "repeat", 0, NULL, ICON_NONE);
356 }
357
358 /* evaluation settings for active NLA-Strip */
359 static void nla_panel_evaluation(const bContext *C, Panel *pa)
360 {
361         PointerRNA strip_ptr;
362         uiLayout *layout= pa->layout;
363         uiLayout *column, *subcolumn, *subrow;
364         uiBlock *block;
365
366         /* check context and also validity of pointer */
367         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
368                 return;
369                 
370         block= uiLayoutGetBlock(layout);
371         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
372                 
373         column= uiLayoutColumn(layout, 1);
374                 uiItemR(column, &strip_ptr, "use_animated_influence", 0, NULL, ICON_NONE);
375                 
376                 subcolumn= uiLayoutColumn(column, 1);
377                 uiLayoutSetEnabled(subcolumn, RNA_boolean_get(&strip_ptr, "use_animated_influence"));   
378                         uiItemR(subcolumn, &strip_ptr, "influence", 0, NULL, ICON_NONE);
379                 
380         
381         column= uiLayoutColumn(layout, 1);
382                 subrow= uiLayoutRow(column, 0);
383                 uiItemR(subrow, &strip_ptr, "use_animated_time", 0, NULL, ICON_NONE);
384                 uiItemR(subrow, &strip_ptr, "use_animated_time_cyclic", 0, NULL, ICON_NONE);
385
386                 subcolumn= uiLayoutColumn(column, 1);
387                 subrow= uiLayoutRow(subcolumn, 0);
388                 uiLayoutSetEnabled(subrow, RNA_boolean_get(&strip_ptr, "use_animated_time"));
389                         uiItemR(subcolumn, &strip_ptr, "strip_time", 0, NULL, ICON_NONE);
390 }
391
392 /* F-Modifiers for active NLA-Strip */
393 static void nla_panel_modifiers(const bContext *C, Panel *pa)
394 {
395         PointerRNA strip_ptr;
396         NlaStrip *strip;
397         FModifier *fcm;
398         uiLayout *col, *row;
399         uiBlock *block;
400
401         /* check context and also validity of pointer */
402         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
403                 return;
404         strip= strip_ptr.data;
405                 
406         block= uiLayoutGetBlock(pa->layout);
407         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
408         
409         /* 'add modifier' button at top of panel */
410         {
411                 row= uiLayoutRow(pa->layout, 0);
412                 block= uiLayoutGetBlock(row);
413                 
414                 // XXX for now, this will be a operator button which calls a temporary 'add modifier' operator
415                 // FIXME: we need to set the only-active property so that this will only add modifiers for the active strip (not all selected)
416                 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");
417                 
418                 /* copy/paste (as sub-row)*/
419                 row= uiLayoutRow(row, 1);
420                         uiItemO(row, "", ICON_COPYDOWN, "NLA_OT_fmodifier_copy");
421                         uiItemO(row, "", ICON_PASTEDOWN, "NLA_OT_fmodifier_paste");
422         }
423         
424         /* draw each modifier */
425         for (fcm= strip->modifiers.first; fcm; fcm= fcm->next) {
426                 col= uiLayoutColumn(pa->layout, 1);
427                 
428                 ANIM_uiTemplate_fmodifier_draw(col, strip_ptr.id.data, &strip->modifiers, fcm);
429         }
430 }
431
432 /* ******************* general ******************************** */
433
434
435 void nla_buttons_register(ARegionType *art)
436 {
437         PanelType *pt;
438         
439         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata");
440         strcpy(pt->idname, "NLA_PT_animdata");
441         strcpy(pt->label, "Animation Data");
442         pt->draw= nla_panel_animdata;
443         pt->poll= nla_animdata_panel_poll;
444         pt->flag= PNL_DEFAULT_CLOSED;
445         BLI_addtail(&art->paneltypes, pt);
446         
447         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel track");
448         strcpy(pt->idname, "NLA_PT_track");
449         strcpy(pt->label, "Active Track");
450         pt->draw= nla_panel_track;
451         pt->poll= nla_track_panel_poll;
452         BLI_addtail(&art->paneltypes, pt);
453         
454         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
455         strcpy(pt->idname, "NLA_PT_properties");
456         strcpy(pt->label, "Active Strip");
457         pt->draw= nla_panel_properties;
458         pt->poll= nla_strip_panel_poll;
459         BLI_addtail(&art->paneltypes, pt);
460         
461         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
462         strcpy(pt->idname, "NLA_PT_actionclip");
463         strcpy(pt->label, "Action Clip");
464         pt->draw= nla_panel_actclip;
465         pt->poll= nla_strip_actclip_panel_poll;
466         BLI_addtail(&art->paneltypes, pt);
467         
468         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation");
469         strcpy(pt->idname, "NLA_PT_evaluation");
470         strcpy(pt->label, "Evaluation");
471         pt->draw= nla_panel_evaluation;
472         pt->poll= nla_strip_panel_poll;
473         BLI_addtail(&art->paneltypes, pt);
474         
475         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers");
476         strcpy(pt->idname, "NLA_PT_modifiers");
477         strcpy(pt->label, "Modifiers");
478         pt->draw= nla_panel_modifiers;
479         pt->poll= nla_strip_panel_poll;
480         BLI_addtail(&art->paneltypes, pt);
481 }
482
483 static int nla_properties(bContext *C, wmOperator *UNUSED(op))
484 {
485         ScrArea *sa= CTX_wm_area(C);
486         ARegion *ar= nla_has_buttons_region(sa);
487         
488         if(ar)
489                 ED_region_toggle_hidden(C, ar);
490
491         return OPERATOR_FINISHED;
492 }
493
494 void NLA_OT_properties(wmOperatorType *ot)
495 {
496         ot->name= "Properties";
497         ot->idname= "NLA_OT_properties";
498         ot->description= "Toggle display properties panel";
499         
500         ot->exec= nla_properties;
501         ot->poll= ED_operator_nla_active;
502         
503         /* flags */
504         ot->flag= 0;
505 }