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