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