Merged changes in the trunk up to revision 39896.
[blender-staging.git] / source / blender / editors / space_nla / nla_buttons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation, Joshua Leung
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_nla/nla_buttons.c
28  *  \ingroup spnla
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include <float.h>
36
37 #include "DNA_anim_types.h"
38
39 #include "BLI_utildefines.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_math.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_editVert.h"
46 #include "BLI_rand.h"
47
48 #include "BKE_nla.h"
49 #include "BKE_context.h"
50 #include "BKE_screen.h"
51
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "RNA_access.h"
57
58 #include "ED_anim_api.h"
59 #include "ED_screen.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63
64 #include "nla_intern.h" // own include
65
66
67 /* ******************* nla editor space & buttons ************** */
68
69 #define B_NOP           1
70 #define B_REDR          2
71
72 /* -------------- */
73
74 static void do_nla_region_buttons(bContext *C, void *UNUSED(arg), int event)
75 {
76         //Scene *scene= CTX_data_scene(C);
77         
78         switch(event) {
79
80         }
81         
82         /* default for now */
83         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
84         WM_event_add_notifier(C, NC_SCENE|ND_TRANSFORM, NULL);
85 }
86
87 static int nla_panel_context(const bContext *C, PointerRNA *adt_ptr, PointerRNA *nlt_ptr, PointerRNA *strip_ptr)
88 {
89         bAnimContext ac;
90         bAnimListElem *ale= NULL;
91         ListBase anim_data = {NULL, NULL};
92         short found=0;
93         int filter;
94         
95         /* for now, only draw if we could init the anim-context info (necessary for all animation-related tools) 
96          * to work correctly is able to be correctly retrieved. There's no point showing empty panels?
97          */
98         if (ANIM_animdata_get_context(C, &ac) == 0) 
99                 return 0;
100         
101         /* extract list of active channel(s), of which we should only take the first one 
102          *      - we need the channels flag to get the active AnimData block when there are no NLA Tracks
103          */
104         // XXX: double-check active!
105         filter= (ANIMFILTER_DATA_VISIBLE|ANIMFILTER_LIST_VISIBLE|ANIMFILTER_ACTIVE|ANIMFILTER_LIST_CHANNELS);
106         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
107         
108         for (ale= anim_data.first; ale; ale= ale->next) {
109                 switch (ale->type) {
110                         case ANIMTYPE_NLATRACK: /* NLA Track - The primary data type which should get caught */
111                         {
112                                 NlaTrack *nlt= (NlaTrack *)ale->data;
113                                 AnimData *adt= ale->adt;
114                                 
115                                 /* found it, now set the pointers */
116                                 if (adt_ptr) {
117                                         /* AnimData pointer */
118                                         RNA_pointer_create(ale->id, &RNA_AnimData, adt, adt_ptr);
119                                 }
120                                 if (nlt_ptr) {
121                                         /* NLA-Track pointer */
122                                         RNA_pointer_create(ale->id, &RNA_NlaTrack, nlt, nlt_ptr);
123                                 }
124                                 if (strip_ptr) {
125                                         /* NLA-Strip pointer */
126                                         NlaStrip *strip= BKE_nlastrip_find_active(nlt);
127                                         RNA_pointer_create(ale->id, &RNA_NlaStrip, strip, strip_ptr);
128                                 }
129                                 
130                                 found= 1;
131                         }
132                                 break;
133                                 
134                         case ANIMTYPE_SCENE:    /* Top-Level Widgets doubling up as datablocks */
135                         case ANIMTYPE_OBJECT:
136                         case ANIMTYPE_FILLACTD: /* Action Expander */
137                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
138                         case ANIMTYPE_DSLAM:
139                         case ANIMTYPE_DSCAM:
140                         case ANIMTYPE_DSCUR:
141                         case ANIMTYPE_DSSKEY:
142                         case ANIMTYPE_DSWOR:
143                         case ANIMTYPE_DSNTREE:
144                         case ANIMTYPE_DSPART:
145                         case ANIMTYPE_DSMBALL:
146                         case ANIMTYPE_DSARM:
147                         case ANIMTYPE_DSLINESTYLE:
148                         case ANIMTYPE_DSSPK:
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 static int nla_strip_eval_panel_poll(const bContext *C, PanelType *UNUSED(pt))
216 {
217         PointerRNA ptr;
218         NlaStrip *strip;
219         
220         if (!nla_panel_context(C, NULL, NULL, &ptr))
221                 return 0;
222         if (ptr.data == NULL)
223                 return 0;
224         
225         strip= ptr.data;
226         
227         if (strip->type == NLASTRIP_TYPE_SOUND)
228                 return 0;
229                 
230         return 1;
231 }
232
233 /* -------------- */
234
235 /* active AnimData */
236 static void nla_panel_animdata (const bContext *C, Panel *pa)
237 {
238         PointerRNA adt_ptr;
239         /* AnimData *adt; */
240         uiLayout *layout= pa->layout;
241         uiLayout *row;
242         uiBlock *block;
243         
244         /* check context and also validity of pointer */
245         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
246                 return;
247
248         /* adt= adt_ptr.data; */
249         
250         block= uiLayoutGetBlock(layout);
251         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
252         
253         /* Active Action Properties ------------------------------------- */
254         /* action */
255         row= uiLayoutRow(layout, 1);
256                 uiTemplateID(row, (bContext *)C, &adt_ptr, "action", "ACTION_OT_new", NULL, NULL /*"ACTION_OT_unlink"*/); // XXX: need to make these operators
257         
258         /* extrapolation */
259         row= uiLayoutRow(layout, 1);
260                 uiItemR(row, &adt_ptr, "action_extrapolation", 0, NULL, ICON_NONE);
261         
262         /* blending */
263         row= uiLayoutRow(layout, 1);
264                 uiItemR(row, &adt_ptr, "action_blend_type", 0, NULL, ICON_NONE);
265                 
266         /* influence */
267         row= uiLayoutRow(layout, 1);
268                 uiItemR(row, &adt_ptr, "action_influence", 0, NULL, ICON_NONE);
269 }
270
271 /* active NLA-Track */
272 static void nla_panel_track (const bContext *C, Panel *pa)
273 {
274         PointerRNA nlt_ptr;
275         uiLayout *layout= pa->layout;
276         uiLayout *row;
277         uiBlock *block;
278         
279         /* check context and also validity of pointer */
280         if (!nla_panel_context(C, NULL, &nlt_ptr, NULL))
281                 return;
282         
283         block= uiLayoutGetBlock(layout);
284         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
285         
286         /* Info - Active NLA-Context:Track ----------------------  */
287         row= uiLayoutRow(layout, 1);
288                 uiItemR(row, &nlt_ptr, "name", 0, NULL, ICON_NLA);
289 }
290
291 /* generic settings for active NLA-Strip */
292 static void nla_panel_properties(const bContext *C, Panel *pa)
293 {
294         PointerRNA strip_ptr;
295         uiLayout *layout= pa->layout;
296         uiLayout *column, *row, *subcol;
297         uiBlock *block;
298         short showEvalProps = 1;
299         
300         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
301                 return;
302         
303         block= uiLayoutGetBlock(layout);
304         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
305         
306         /* Strip Properties ------------------------------------- */
307         /* strip type */
308         row= uiLayoutColumn(layout, 1);
309                 uiItemR(row, &strip_ptr, "name", 0, NULL, ICON_NLA); // XXX icon?
310                 uiItemR(row, &strip_ptr, "type", 0, NULL, ICON_NONE);
311         
312         /* strip extents */
313         column= uiLayoutColumn(layout, 1);
314                 uiItemL(column, "Strip Extents:", ICON_NONE);
315                 uiItemR(column, &strip_ptr, "frame_start", 0, NULL, ICON_NONE);
316                 uiItemR(column, &strip_ptr, "frame_end", 0, NULL, ICON_NONE);
317         
318         /* Evaluation-Related Strip Properties ------------------ */
319         
320         /* sound properties strips don't have these settings */
321         if (RNA_enum_get(&strip_ptr, "type") == NLASTRIP_TYPE_SOUND)
322                 showEvalProps = 0;
323         
324         /* only show if allowed to... */
325         if (showEvalProps) {
326                 /* extrapolation */
327                 row= uiLayoutRow(layout, 1);
328                         uiItemR(row, &strip_ptr, "extrapolation", 0, NULL, ICON_NONE);
329                 
330                 /* blending */
331                 row= uiLayoutRow(layout, 1);
332                         uiItemR(row, &strip_ptr, "blend_type", 0, NULL, ICON_NONE);
333                         
334                 /* blend in/out + autoblending
335                  *      - blend in/out can only be set when autoblending is off
336                  */
337                 column= uiLayoutColumn(layout, 1);
338                         uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_influence")==0); 
339                         uiItemR(column, &strip_ptr, "use_auto_blend", 0, NULL, ICON_NONE); // XXX as toggle?
340                         
341                         subcol= uiLayoutColumn(column, 1);
342                                 uiLayoutSetActive(subcol, RNA_boolean_get(&strip_ptr, "use_auto_blend")==0); 
343                                 uiItemR(subcol, &strip_ptr, "blend_in", 0, NULL, ICON_NONE);
344                                 uiItemR(subcol, &strip_ptr, "blend_out", 0, NULL, ICON_NONE);
345                         
346                 /* settings */
347                 column= uiLayoutColumn(layout, 1);
348                         uiLayoutSetActive(column, !(RNA_boolean_get(&strip_ptr, "use_animated_influence") || RNA_boolean_get(&strip_ptr, "use_animated_time"))); 
349                         uiItemL(column, "Playback Settings:", ICON_NONE);
350                         uiItemR(column, &strip_ptr, "mute", 0, NULL, ICON_NONE);
351                         uiItemR(column, &strip_ptr, "use_reverse", 0, NULL, ICON_NONE);
352         }
353 }
354
355
356 /* action-clip only settings for active NLA-Strip */
357 static void nla_panel_actclip(const bContext *C, Panel *pa)
358 {
359         PointerRNA strip_ptr;
360         uiLayout *layout= pa->layout;
361         uiLayout *column, *row;
362         uiBlock *block;
363
364         /* check context and also validity of pointer */
365         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
366                 return;
367         
368         block= uiLayoutGetBlock(layout);
369         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
370                 
371         /* Strip Properties ------------------------------------- */
372         /* action pointer */
373         row= uiLayoutRow(layout, 1);
374                 uiItemR(row, &strip_ptr, "action", 0, NULL, ICON_ACTION);
375                 
376         /* action extents */
377         // XXX custom names were used here (to avoid the prefixes)... probably not necessary in future?
378         column= uiLayoutColumn(layout, 1);
379                 uiItemL(column, "Action Extents:", ICON_NONE);
380                 uiItemR(column, &strip_ptr, "action_frame_start", 0, "Start Frame", ICON_NONE);
381                 uiItemR(column, &strip_ptr, "action_frame_end", 0, "End Frame", ICON_NONE);
382                 uiItemO(column, NULL, ICON_NONE, "NLA_OT_action_sync_length");
383                 
384         /* action usage */
385         column= uiLayoutColumn(layout, 1);
386                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_time")==0); 
387                 uiItemL(column, "Playback Settings:", ICON_NONE);
388                 uiItemR(column, &strip_ptr, "scale", 0, NULL, ICON_NONE);
389                 uiItemR(column, &strip_ptr, "repeat", 0, NULL, ICON_NONE);
390 }
391
392 /* evaluation settings for active NLA-Strip */
393 static void nla_panel_evaluation(const bContext *C, Panel *pa)
394 {
395         PointerRNA strip_ptr;
396         uiLayout *layout= pa->layout;
397         uiLayout *column, *subcolumn, *subrow;
398         uiBlock *block;
399
400         /* check context and also validity of pointer */
401         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
402                 return;
403                 
404         block= uiLayoutGetBlock(layout);
405         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
406                 
407         column= uiLayoutColumn(layout, 1);
408                 uiItemR(column, &strip_ptr, "use_animated_influence", 0, NULL, ICON_NONE);
409                 
410                 subcolumn= uiLayoutColumn(column, 1);
411                 uiLayoutSetEnabled(subcolumn, RNA_boolean_get(&strip_ptr, "use_animated_influence"));   
412                         uiItemR(subcolumn, &strip_ptr, "influence", 0, NULL, ICON_NONE);
413                 
414         
415         column= uiLayoutColumn(layout, 1);
416                 subrow= uiLayoutRow(column, 0);
417                 uiItemR(subrow, &strip_ptr, "use_animated_time", 0, NULL, ICON_NONE);
418                 uiItemR(subrow, &strip_ptr, "use_animated_time_cyclic", 0, NULL, ICON_NONE);
419
420                 subcolumn= uiLayoutColumn(column, 1);
421                 subrow= uiLayoutRow(subcolumn, 0);
422                 uiLayoutSetEnabled(subrow, RNA_boolean_get(&strip_ptr, "use_animated_time"));
423                         uiItemR(subcolumn, &strip_ptr, "strip_time", 0, NULL, ICON_NONE);
424 }
425
426 /* F-Modifiers for active NLA-Strip */
427 static void nla_panel_modifiers(const bContext *C, Panel *pa)
428 {
429         PointerRNA strip_ptr;
430         NlaStrip *strip;
431         FModifier *fcm;
432         uiLayout *col, *row;
433         uiBlock *block;
434
435         /* check context and also validity of pointer */
436         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
437                 return;
438         strip= strip_ptr.data;
439                 
440         block= uiLayoutGetBlock(pa->layout);
441         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
442         
443         /* 'add modifier' button at top of panel */
444         {
445                 row= uiLayoutRow(pa->layout, 0);
446                 block= uiLayoutGetBlock(row);
447                 
448                 // XXX for now, this will be a operator button which calls a temporary 'add modifier' operator
449                 // FIXME: we need to set the only-active property so that this will only add modifiers for the active strip (not all selected)
450                 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");
451                 
452                 /* copy/paste (as sub-row)*/
453                 row= uiLayoutRow(row, 1);
454                         uiItemO(row, "", ICON_COPYDOWN, "NLA_OT_fmodifier_copy");
455                         uiItemO(row, "", ICON_PASTEDOWN, "NLA_OT_fmodifier_paste");
456         }
457         
458         /* draw each modifier */
459         for (fcm= strip->modifiers.first; fcm; fcm= fcm->next) {
460                 col= uiLayoutColumn(pa->layout, 1);
461                 
462                 ANIM_uiTemplate_fmodifier_draw(col, strip_ptr.id.data, &strip->modifiers, fcm);
463         }
464 }
465
466 /* ******************* general ******************************** */
467
468
469 void nla_buttons_register(ARegionType *art)
470 {
471         PanelType *pt;
472         
473         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata");
474         strcpy(pt->idname, "NLA_PT_animdata");
475         strcpy(pt->label, "Animation Data");
476         pt->draw= nla_panel_animdata;
477         pt->poll= nla_animdata_panel_poll;
478         pt->flag= PNL_DEFAULT_CLOSED;
479         BLI_addtail(&art->paneltypes, pt);
480         
481         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel track");
482         strcpy(pt->idname, "NLA_PT_track");
483         strcpy(pt->label, "Active Track");
484         pt->draw= nla_panel_track;
485         pt->poll= nla_track_panel_poll;
486         BLI_addtail(&art->paneltypes, pt);
487         
488         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
489         strcpy(pt->idname, "NLA_PT_properties");
490         strcpy(pt->label, "Active Strip");
491         pt->draw= nla_panel_properties;
492         pt->poll= nla_strip_panel_poll;
493         BLI_addtail(&art->paneltypes, pt);
494         
495         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
496         strcpy(pt->idname, "NLA_PT_actionclip");
497         strcpy(pt->label, "Action Clip");
498         pt->draw= nla_panel_actclip;
499         pt->poll= nla_strip_actclip_panel_poll;
500         BLI_addtail(&art->paneltypes, pt);
501         
502         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation");
503         strcpy(pt->idname, "NLA_PT_evaluation");
504         strcpy(pt->label, "Evaluation");
505         pt->draw= nla_panel_evaluation;
506         pt->poll= nla_strip_eval_panel_poll;
507         BLI_addtail(&art->paneltypes, pt);
508         
509         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers");
510         strcpy(pt->idname, "NLA_PT_modifiers");
511         strcpy(pt->label, "Modifiers");
512         pt->draw= nla_panel_modifiers;
513         pt->poll= nla_strip_eval_panel_poll;
514         BLI_addtail(&art->paneltypes, pt);
515 }
516
517 static int nla_properties(bContext *C, wmOperator *UNUSED(op))
518 {
519         ScrArea *sa= CTX_wm_area(C);
520         ARegion *ar= nla_has_buttons_region(sa);
521         
522         if(ar)
523                 ED_region_toggle_hidden(C, ar);
524
525         return OPERATOR_FINISHED;
526 }
527
528 void NLA_OT_properties(wmOperatorType *ot)
529 {
530         ot->name= "Properties";
531         ot->idname= "NLA_OT_properties";
532         ot->description= "Toggle display properties panel";
533         
534         ot->exec= nla_properties;
535         ot->poll= ED_operator_nla_active;
536
537         /* flags */
538         ot->flag= 0;
539 }