style cleanup: changes to brace placement / newlines - for/while/if/switch
[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_rand.h"
46
47 #include "BLF_translation.h"
48
49 #include "BKE_nla.h"
50 #include "BKE_context.h"
51 #include "BKE_screen.h"
52
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "RNA_access.h"
58
59 #include "ED_anim_api.h"
60 #include "ED_screen.h"
61
62 #include "UI_interface.h"
63 #include "UI_resources.h"
64
65 #include "nla_intern.h" // own include
66
67
68 /* ******************* nla editor space & buttons ************** */
69
70 #define B_NOP           1
71 #define B_REDR          2
72
73 /* -------------- */
74
75 static void do_nla_region_buttons(bContext *C, void *UNUSED(arg), int event)
76 {
77         //Scene *scene= CTX_data_scene(C);
78         
79         switch (event) {
80
81         }
82         
83         /* default for now */
84         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
85         WM_event_add_notifier(C, NC_SCENE|ND_TRANSFORM, NULL);
86 }
87
88 static int nla_panel_context(const bContext *C, PointerRNA *adt_ptr, PointerRNA *nlt_ptr, PointerRNA *strip_ptr)
89 {
90         bAnimContext ac;
91         bAnimListElem *ale= NULL;
92         ListBase anim_data = {NULL, NULL};
93         short found=0;
94         int filter;
95         
96         /* for now, only draw if we could init the anim-context info (necessary for all animation-related tools) 
97          * to work correctly is able to be correctly retrieved. There's no point showing empty panels?
98          */
99         if (ANIM_animdata_get_context(C, &ac) == 0) 
100                 return 0;
101         
102         /* extract list of active channel(s), of which we should only take the first one 
103          *      - we need the channels flag to get the active AnimData block when there are no NLA Tracks
104          */
105         // XXX: double-check active!
106         filter= (ANIMFILTER_DATA_VISIBLE|ANIMFILTER_LIST_VISIBLE|ANIMFILTER_ACTIVE|ANIMFILTER_LIST_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                         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, *sub;
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                         sub= uiLayoutColumn(column, 1);
342                                 uiLayoutSetActive(sub, RNA_boolean_get(&strip_ptr, "use_auto_blend")==0); 
343                                 uiItemR(sub, &strip_ptr, "blend_in", 0, NULL, ICON_NONE);
344                                 uiItemR(sub, &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 *col, *sub;
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         col= uiLayoutColumn(layout, 1);
408         uiItemR(col, &strip_ptr, "use_animated_influence", 0, NULL, ICON_NONE);
409         
410         sub= uiLayoutColumn(col, 1);
411         uiLayoutSetEnabled(sub, RNA_boolean_get(&strip_ptr, "use_animated_influence")); 
412         uiItemR(sub, &strip_ptr, "influence", 0, NULL, ICON_NONE);
413
414         col= uiLayoutColumn(layout, 1);
415         sub= uiLayoutRow(col, 0);
416         uiItemR(sub, &strip_ptr, "use_animated_time", 0, NULL, ICON_NONE);
417         uiItemR(sub, &strip_ptr, "use_animated_time_cyclic", 0, NULL, ICON_NONE);
418
419         sub= uiLayoutRow(col, 0);
420         uiLayoutSetEnabled(sub, RNA_boolean_get(&strip_ptr, "use_animated_time"));
421         uiItemR(sub, &strip_ptr, "strip_time", 0, NULL, ICON_NONE);
422 }
423
424 /* F-Modifiers for active NLA-Strip */
425 static void nla_panel_modifiers(const bContext *C, Panel *pa)
426 {
427         PointerRNA strip_ptr;
428         NlaStrip *strip;
429         FModifier *fcm;
430         uiLayout *col, *row;
431         uiBlock *block;
432
433         /* check context and also validity of pointer */
434         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
435                 return;
436         strip= strip_ptr.data;
437                 
438         block= uiLayoutGetBlock(pa->layout);
439         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
440         
441         /* 'add modifier' button at top of panel */
442         {
443                 row= uiLayoutRow(pa->layout, 0);
444                 block= uiLayoutGetBlock(row);
445                 
446                 // XXX for now, this will be a operator button which calls a temporary 'add modifier' operator
447                 // FIXME: we need to set the only-active property so that this will only add modifiers for the active strip (not all selected)
448                 uiDefButO(block, BUT, "NLA_OT_fmodifier_add", WM_OP_INVOKE_REGION_WIN, IFACE_("Add Modifier"), 10, 0, 150, 20,
449                                 TIP_("Adds a new F-Modifier for the active NLA Strip"));
450                 
451                 /* copy/paste (as sub-row)*/
452                 row= uiLayoutRow(row, 1);
453                         uiItemO(row, "", ICON_COPYDOWN, "NLA_OT_fmodifier_copy");
454                         uiItemO(row, "", ICON_PASTEDOWN, "NLA_OT_fmodifier_paste");
455         }
456         
457         /* draw each modifier */
458         for (fcm= strip->modifiers.first; fcm; fcm= fcm->next) {
459                 col= uiLayoutColumn(pa->layout, 1);
460                 
461                 ANIM_uiTemplate_fmodifier_draw(col, strip_ptr.id.data, &strip->modifiers, fcm);
462         }
463 }
464
465 /* ******************* general ******************************** */
466
467
468 void nla_buttons_register(ARegionType *art)
469 {
470         PanelType *pt;
471         
472         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata");
473         strcpy(pt->idname, "NLA_PT_animdata");
474         strcpy(pt->label, "Animation Data");
475         pt->draw= nla_panel_animdata;
476         pt->poll= nla_animdata_panel_poll;
477         pt->flag= PNL_DEFAULT_CLOSED;
478         BLI_addtail(&art->paneltypes, pt);
479         
480         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel track");
481         strcpy(pt->idname, "NLA_PT_track");
482         strcpy(pt->label, "Active Track");
483         pt->draw= nla_panel_track;
484         pt->poll= nla_track_panel_poll;
485         BLI_addtail(&art->paneltypes, pt);
486         
487         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
488         strcpy(pt->idname, "NLA_PT_properties");
489         strcpy(pt->label, "Active Strip");
490         pt->draw= nla_panel_properties;
491         pt->poll= nla_strip_panel_poll;
492         BLI_addtail(&art->paneltypes, pt);
493         
494         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
495         strcpy(pt->idname, "NLA_PT_actionclip");
496         strcpy(pt->label, "Action Clip");
497         pt->draw= nla_panel_actclip;
498         pt->poll= nla_strip_actclip_panel_poll;
499         BLI_addtail(&art->paneltypes, pt);
500         
501         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation");
502         strcpy(pt->idname, "NLA_PT_evaluation");
503         strcpy(pt->label, "Evaluation");
504         pt->draw= nla_panel_evaluation;
505         pt->poll= nla_strip_eval_panel_poll;
506         BLI_addtail(&art->paneltypes, pt);
507         
508         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers");
509         strcpy(pt->idname, "NLA_PT_modifiers");
510         strcpy(pt->label, "Modifiers");
511         pt->draw= nla_panel_modifiers;
512         pt->poll= nla_strip_eval_panel_poll;
513         BLI_addtail(&art->paneltypes, pt);
514 }
515
516 static int nla_properties(bContext *C, wmOperator *UNUSED(op))
517 {
518         ScrArea *sa= CTX_wm_area(C);
519         ARegion *ar= nla_has_buttons_region(sa);
520         
521         if (ar)
522                 ED_region_toggle_hidden(C, ar);
523
524         return OPERATOR_FINISHED;
525 }
526
527 void NLA_OT_properties(wmOperatorType *ot)
528 {
529         ot->name = "Properties";
530         ot->idname = "NLA_OT_properties";
531         ot->description = "Toggle display properties panel";
532         
533         ot->exec = nla_properties;
534         ot->poll = ED_operator_nla_active;
535
536         /* flags */
537         ot->flag = 0;
538 }