Merged changes in the trunk up to revision 42116.
[blender.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 "BLF_translation.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         // XXX: double-check active!
107         filter= (ANIMFILTER_DATA_VISIBLE|ANIMFILTER_LIST_VISIBLE|ANIMFILTER_ACTIVE|ANIMFILTER_LIST_CHANNELS);
108         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
109         
110         for (ale= anim_data.first; ale; ale= ale->next) {
111                 switch (ale->type) {
112                         case ANIMTYPE_NLATRACK: /* NLA Track - The primary data type which should get caught */
113                         {
114                                 NlaTrack *nlt= (NlaTrack *)ale->data;
115                                 AnimData *adt= ale->adt;
116                                 
117                                 /* found it, now set the pointers */
118                                 if (adt_ptr) {
119                                         /* AnimData pointer */
120                                         RNA_pointer_create(ale->id, &RNA_AnimData, adt, adt_ptr);
121                                 }
122                                 if (nlt_ptr) {
123                                         /* NLA-Track pointer */
124                                         RNA_pointer_create(ale->id, &RNA_NlaTrack, nlt, nlt_ptr);
125                                 }
126                                 if (strip_ptr) {
127                                         /* NLA-Strip pointer */
128                                         NlaStrip *strip= BKE_nlastrip_find_active(nlt);
129                                         RNA_pointer_create(ale->id, &RNA_NlaStrip, strip, strip_ptr);
130                                 }
131                                 
132                                 found= 1;
133                         }
134                                 break;
135                                 
136                         case ANIMTYPE_SCENE:    /* Top-Level Widgets doubling up as datablocks */
137                         case ANIMTYPE_OBJECT:
138                         case ANIMTYPE_FILLACTD: /* Action Expander */
139                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
140                         case ANIMTYPE_DSLAM:
141                         case ANIMTYPE_DSCAM:
142                         case ANIMTYPE_DSCUR:
143                         case ANIMTYPE_DSSKEY:
144                         case ANIMTYPE_DSWOR:
145                         case ANIMTYPE_DSNTREE:
146                         case ANIMTYPE_DSPART:
147                         case ANIMTYPE_DSMBALL:
148                         case ANIMTYPE_DSARM:
149                         case ANIMTYPE_DSLINESTYLE:
150                         case ANIMTYPE_DSSPK:
151                         {
152                                 /* for these channels, we only do AnimData */
153                                 if (ale->id && ale->adt) {
154                                         if (adt_ptr) {
155                                                 /* AnimData pointer */
156                                                 RNA_pointer_create(ale->id, &RNA_AnimData, ale->adt, adt_ptr);
157                                                 
158                                                 /* set found status to -1, since setting to 1 would break the loop 
159                                                  * and potentially skip an active NLA-Track in some cases...
160                                                  */
161                                                 found= -1;
162                                         }
163                                 }
164                         }       
165                                 break;
166                 }
167                 
168                 if (found > 0)
169                         break;
170         }
171         
172         /* free temp data */
173         BLI_freelistN(&anim_data);
174         
175         return found;
176 }
177
178 #if 0
179 static int nla_panel_poll(const bContext *C, PanelType *pt)
180 {
181         return nla_panel_context(C, NULL, NULL);
182 }
183 #endif
184
185 static int nla_animdata_panel_poll(const bContext *C, PanelType *UNUSED(pt))
186 {
187         PointerRNA ptr;
188         return (nla_panel_context(C, &ptr, NULL, NULL) && (ptr.data != NULL));
189 }
190
191 static int nla_track_panel_poll(const bContext *C, PanelType *UNUSED(pt))
192 {
193         PointerRNA ptr;
194         return (nla_panel_context(C, NULL, &ptr, NULL) && (ptr.data != NULL));
195 }
196
197 static int nla_strip_panel_poll(const bContext *C, PanelType *UNUSED(pt))
198 {
199         PointerRNA ptr;
200         return (nla_panel_context(C, NULL, NULL, &ptr) && (ptr.data != NULL));
201 }
202
203 static int nla_strip_actclip_panel_poll(const bContext *C, PanelType *UNUSED(pt))
204 {
205         PointerRNA ptr;
206         NlaStrip *strip;
207         
208         if (!nla_panel_context(C, NULL, NULL, &ptr))
209                 return 0;
210         if (ptr.data == NULL)
211                 return 0;
212         
213         strip= ptr.data;
214         return (strip->type == NLASTRIP_TYPE_CLIP);
215 }
216
217 static int nla_strip_eval_panel_poll(const bContext *C, PanelType *UNUSED(pt))
218 {
219         PointerRNA ptr;
220         NlaStrip *strip;
221         
222         if (!nla_panel_context(C, NULL, NULL, &ptr))
223                 return 0;
224         if (ptr.data == NULL)
225                 return 0;
226         
227         strip= ptr.data;
228         
229         if (strip->type == NLASTRIP_TYPE_SOUND)
230                 return 0;
231                 
232         return 1;
233 }
234
235 /* -------------- */
236
237 /* active AnimData */
238 static void nla_panel_animdata (const bContext *C, Panel *pa)
239 {
240         PointerRNA adt_ptr;
241         /* AnimData *adt; */
242         uiLayout *layout= pa->layout;
243         uiLayout *row;
244         uiBlock *block;
245         
246         /* check context and also validity of pointer */
247         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
248                 return;
249
250         /* adt= adt_ptr.data; */
251         
252         block= uiLayoutGetBlock(layout);
253         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
254         
255         /* Active Action Properties ------------------------------------- */
256         /* action */
257         row= uiLayoutRow(layout, 1);
258                 uiTemplateID(row, (bContext *)C, &adt_ptr, "action", "ACTION_OT_new", NULL, NULL /*"ACTION_OT_unlink"*/); // XXX: need to make these operators
259         
260         /* extrapolation */
261         row= uiLayoutRow(layout, 1);
262                 uiItemR(row, &adt_ptr, "action_extrapolation", 0, NULL, ICON_NONE);
263         
264         /* blending */
265         row= uiLayoutRow(layout, 1);
266                 uiItemR(row, &adt_ptr, "action_blend_type", 0, NULL, ICON_NONE);
267                 
268         /* influence */
269         row= uiLayoutRow(layout, 1);
270                 uiItemR(row, &adt_ptr, "action_influence", 0, NULL, ICON_NONE);
271 }
272
273 /* active NLA-Track */
274 static void nla_panel_track (const bContext *C, Panel *pa)
275 {
276         PointerRNA nlt_ptr;
277         uiLayout *layout= pa->layout;
278         uiLayout *row;
279         uiBlock *block;
280         
281         /* check context and also validity of pointer */
282         if (!nla_panel_context(C, NULL, &nlt_ptr, NULL))
283                 return;
284         
285         block= uiLayoutGetBlock(layout);
286         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
287         
288         /* Info - Active NLA-Context:Track ----------------------  */
289         row= uiLayoutRow(layout, 1);
290                 uiItemR(row, &nlt_ptr, "name", 0, NULL, ICON_NLA);
291 }
292
293 /* generic settings for active NLA-Strip */
294 static void nla_panel_properties(const bContext *C, Panel *pa)
295 {
296         PointerRNA strip_ptr;
297         uiLayout *layout= pa->layout;
298         uiLayout *column, *row, *sub;
299         uiBlock *block;
300         short showEvalProps = 1;
301         
302         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
303                 return;
304         
305         block= uiLayoutGetBlock(layout);
306         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
307         
308         /* Strip Properties ------------------------------------- */
309         /* strip type */
310         row= uiLayoutColumn(layout, 1);
311                 uiItemR(row, &strip_ptr, "name", 0, NULL, ICON_NLA); // XXX icon?
312                 uiItemR(row, &strip_ptr, "type", 0, NULL, ICON_NONE);
313         
314         /* strip extents */
315         column= uiLayoutColumn(layout, 1);
316                 uiItemL(column, "Strip Extents:", ICON_NONE);
317                 uiItemR(column, &strip_ptr, "frame_start", 0, NULL, ICON_NONE);
318                 uiItemR(column, &strip_ptr, "frame_end", 0, NULL, ICON_NONE);
319         
320         /* Evaluation-Related Strip Properties ------------------ */
321         
322         /* sound properties strips don't have these settings */
323         if (RNA_enum_get(&strip_ptr, "type") == NLASTRIP_TYPE_SOUND)
324                 showEvalProps = 0;
325         
326         /* only show if allowed to... */
327         if (showEvalProps) {
328                 /* extrapolation */
329                 row= uiLayoutRow(layout, 1);
330                         uiItemR(row, &strip_ptr, "extrapolation", 0, NULL, ICON_NONE);
331                 
332                 /* blending */
333                 row= uiLayoutRow(layout, 1);
334                         uiItemR(row, &strip_ptr, "blend_type", 0, NULL, ICON_NONE);
335                         
336                 /* blend in/out + autoblending
337                  *      - blend in/out can only be set when autoblending is off
338                  */
339                 column= uiLayoutColumn(layout, 1);
340                         uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_influence")==0); 
341                         uiItemR(column, &strip_ptr, "use_auto_blend", 0, NULL, ICON_NONE); // XXX as toggle?
342                         
343                         sub= uiLayoutColumn(column, 1);
344                                 uiLayoutSetActive(sub, RNA_boolean_get(&strip_ptr, "use_auto_blend")==0); 
345                                 uiItemR(sub, &strip_ptr, "blend_in", 0, NULL, ICON_NONE);
346                                 uiItemR(sub, &strip_ptr, "blend_out", 0, NULL, ICON_NONE);
347                         
348                 /* settings */
349                 column= uiLayoutColumn(layout, 1);
350                         uiLayoutSetActive(column, !(RNA_boolean_get(&strip_ptr, "use_animated_influence") || RNA_boolean_get(&strip_ptr, "use_animated_time"))); 
351                         uiItemL(column, "Playback Settings:", ICON_NONE);
352                         uiItemR(column, &strip_ptr, "mute", 0, NULL, ICON_NONE);
353                         uiItemR(column, &strip_ptr, "use_reverse", 0, NULL, ICON_NONE);
354         }
355 }
356
357
358 /* action-clip only settings for active NLA-Strip */
359 static void nla_panel_actclip(const bContext *C, Panel *pa)
360 {
361         PointerRNA strip_ptr;
362         uiLayout *layout= pa->layout;
363         uiLayout *column, *row;
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         /* Strip Properties ------------------------------------- */
374         /* action pointer */
375         row= uiLayoutRow(layout, 1);
376                 uiItemR(row, &strip_ptr, "action", 0, NULL, ICON_ACTION);
377                 
378         /* action extents */
379         // XXX custom names were used here (to avoid the prefixes)... probably not necessary in future?
380         column= uiLayoutColumn(layout, 1);
381                 uiItemL(column, "Action Extents:", ICON_NONE);
382                 uiItemR(column, &strip_ptr, "action_frame_start", 0, "Start Frame", ICON_NONE);
383                 uiItemR(column, &strip_ptr, "action_frame_end", 0, "End Frame", ICON_NONE);
384                 uiItemO(column, NULL, ICON_NONE, "NLA_OT_action_sync_length");
385                 
386         /* action usage */
387         column= uiLayoutColumn(layout, 1);
388                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_time")==0); 
389                 uiItemL(column, "Playback Settings:", ICON_NONE);
390                 uiItemR(column, &strip_ptr, "scale", 0, NULL, ICON_NONE);
391                 uiItemR(column, &strip_ptr, "repeat", 0, NULL, ICON_NONE);
392 }
393
394 /* evaluation settings for active NLA-Strip */
395 static void nla_panel_evaluation(const bContext *C, Panel *pa)
396 {
397         PointerRNA strip_ptr;
398         uiLayout *layout= pa->layout;
399         uiLayout *col, *sub;
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                 
406         block= uiLayoutGetBlock(layout);
407         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
408                 
409         col= uiLayoutColumn(layout, 1);
410         uiItemR(col, &strip_ptr, "use_animated_influence", 0, NULL, ICON_NONE);
411         
412         sub= uiLayoutColumn(col, 1);
413         uiLayoutSetEnabled(sub, RNA_boolean_get(&strip_ptr, "use_animated_influence")); 
414         uiItemR(sub, &strip_ptr, "influence", 0, NULL, ICON_NONE);
415
416         col= uiLayoutColumn(layout, 1);
417         sub= uiLayoutRow(col, 0);
418         uiItemR(sub, &strip_ptr, "use_animated_time", 0, NULL, ICON_NONE);
419         uiItemR(sub, &strip_ptr, "use_animated_time_cyclic", 0, NULL, ICON_NONE);
420
421         sub= uiLayoutRow(col, 0);
422         uiLayoutSetEnabled(sub, RNA_boolean_get(&strip_ptr, "use_animated_time"));
423         uiItemR(sub, &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, IFACE_("Add Modifier"), 10, 0, 150, 20,
451                                 TIP_("Adds a new F-Modifier for the active NLA Strip"));
452                 
453                 /* copy/paste (as sub-row)*/
454                 row= uiLayoutRow(row, 1);
455                         uiItemO(row, "", ICON_COPYDOWN, "NLA_OT_fmodifier_copy");
456                         uiItemO(row, "", ICON_PASTEDOWN, "NLA_OT_fmodifier_paste");
457         }
458         
459         /* draw each modifier */
460         for (fcm= strip->modifiers.first; fcm; fcm= fcm->next) {
461                 col= uiLayoutColumn(pa->layout, 1);
462                 
463                 ANIM_uiTemplate_fmodifier_draw(col, strip_ptr.id.data, &strip->modifiers, fcm);
464         }
465 }
466
467 /* ******************* general ******************************** */
468
469
470 void nla_buttons_register(ARegionType *art)
471 {
472         PanelType *pt;
473         
474         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata");
475         strcpy(pt->idname, "NLA_PT_animdata");
476         strcpy(pt->label, "Animation Data");
477         pt->draw= nla_panel_animdata;
478         pt->poll= nla_animdata_panel_poll;
479         pt->flag= PNL_DEFAULT_CLOSED;
480         BLI_addtail(&art->paneltypes, pt);
481         
482         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel track");
483         strcpy(pt->idname, "NLA_PT_track");
484         strcpy(pt->label, "Active Track");
485         pt->draw= nla_panel_track;
486         pt->poll= nla_track_panel_poll;
487         BLI_addtail(&art->paneltypes, pt);
488         
489         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
490         strcpy(pt->idname, "NLA_PT_properties");
491         strcpy(pt->label, "Active Strip");
492         pt->draw= nla_panel_properties;
493         pt->poll= nla_strip_panel_poll;
494         BLI_addtail(&art->paneltypes, pt);
495         
496         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
497         strcpy(pt->idname, "NLA_PT_actionclip");
498         strcpy(pt->label, "Action Clip");
499         pt->draw= nla_panel_actclip;
500         pt->poll= nla_strip_actclip_panel_poll;
501         BLI_addtail(&art->paneltypes, pt);
502         
503         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation");
504         strcpy(pt->idname, "NLA_PT_evaluation");
505         strcpy(pt->label, "Evaluation");
506         pt->draw= nla_panel_evaluation;
507         pt->poll= nla_strip_eval_panel_poll;
508         BLI_addtail(&art->paneltypes, pt);
509         
510         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers");
511         strcpy(pt->idname, "NLA_PT_modifiers");
512         strcpy(pt->label, "Modifiers");
513         pt->draw= nla_panel_modifiers;
514         pt->poll= nla_strip_eval_panel_poll;
515         BLI_addtail(&art->paneltypes, pt);
516 }
517
518 static int nla_properties(bContext *C, wmOperator *UNUSED(op))
519 {
520         ScrArea *sa= CTX_wm_area(C);
521         ARegion *ar= nla_has_buttons_region(sa);
522         
523         if(ar)
524                 ED_region_toggle_hidden(C, ar);
525
526         return OPERATOR_FINISHED;
527 }
528
529 void NLA_OT_properties(wmOperatorType *ot)
530 {
531         ot->name= "Properties";
532         ot->idname= "NLA_OT_properties";
533         ot->description= "Toggle display properties panel";
534         
535         ot->exec= nla_properties;
536         ot->poll= ED_operator_nla_active;
537
538         /* flags */
539         ot->flag= 0;
540 }