b6de8e7fb5942bfb7e8378a32979d45ea9bfa9e2
[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         // 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 /* -------------- */
217
218 /* active AnimData */
219 static void nla_panel_animdata (const bContext *C, Panel *pa)
220 {
221         PointerRNA adt_ptr;
222         /* AnimData *adt; */
223         uiLayout *layout= pa->layout;
224         uiLayout *row;
225         uiBlock *block;
226         
227         /* check context and also validity of pointer */
228         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
229                 return;
230
231         /* adt= adt_ptr.data; */
232         
233         block= uiLayoutGetBlock(layout);
234         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
235         
236         /* Active Action Properties ------------------------------------- */
237         /* action */
238         row= uiLayoutRow(layout, 1);
239                 uiTemplateID(row, (bContext *)C, &adt_ptr, "action", "ACTION_OT_new", NULL, NULL /*"ACTION_OT_unlink"*/); // XXX: need to make these operators
240         
241         /* extrapolation */
242         row= uiLayoutRow(layout, 1);
243                 uiItemR(row, &adt_ptr, "action_extrapolation", 0, NULL, ICON_NONE);
244         
245         /* blending */
246         row= uiLayoutRow(layout, 1);
247                 uiItemR(row, &adt_ptr, "action_blend_type", 0, NULL, ICON_NONE);
248                 
249         /* influence */
250         row= uiLayoutRow(layout, 1);
251                 uiItemR(row, &adt_ptr, "action_influence", 0, NULL, ICON_NONE);
252 }
253
254 /* active NLA-Track */
255 static void nla_panel_track (const bContext *C, Panel *pa)
256 {
257         PointerRNA nlt_ptr;
258         uiLayout *layout= pa->layout;
259         uiLayout *row;
260         uiBlock *block;
261         
262         /* check context and also validity of pointer */
263         if (!nla_panel_context(C, NULL, &nlt_ptr, NULL))
264                 return;
265         
266         block= uiLayoutGetBlock(layout);
267         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
268         
269         /* Info - Active NLA-Context:Track ----------------------  */
270         row= uiLayoutRow(layout, 1);
271                 uiItemR(row, &nlt_ptr, "name", 0, NULL, ICON_NLA);
272 }
273
274 /* generic settings for active NLA-Strip */
275 static void nla_panel_properties(const bContext *C, Panel *pa)
276 {
277         PointerRNA strip_ptr;
278         uiLayout *layout= pa->layout;
279         uiLayout *column, *row, *subcol;
280         uiBlock *block;
281         
282         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
283                 return;
284         
285         block= uiLayoutGetBlock(layout);
286         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
287         
288         /* Strip Properties ------------------------------------- */
289         /* strip type */
290         row= uiLayoutColumn(layout, 1);
291                 uiItemR(row, &strip_ptr, "name", 0, NULL, ICON_NLA); // XXX icon?
292                 uiItemR(row, &strip_ptr, "type", 0, NULL, ICON_NONE);
293         
294         /* strip extents */
295         column= uiLayoutColumn(layout, 1);
296                 uiItemL(column, "Strip Extents:", ICON_NONE);
297                 uiItemR(column, &strip_ptr, "frame_start", 0, NULL, ICON_NONE);
298                 uiItemR(column, &strip_ptr, "frame_end", 0, NULL, ICON_NONE);
299         
300         /* extrapolation */
301         row= uiLayoutRow(layout, 1);
302                 uiItemR(row, &strip_ptr, "extrapolation", 0, NULL, ICON_NONE);
303         
304         /* blending */
305         row= uiLayoutRow(layout, 1);
306                 uiItemR(row, &strip_ptr, "blend_type", 0, NULL, ICON_NONE);
307                 
308         /* blend in/out + autoblending
309          *      - blend in/out can only be set when autoblending is off
310          */
311         column= uiLayoutColumn(layout, 1);
312                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_influence")==0); 
313                 uiItemR(column, &strip_ptr, "use_auto_blend", 0, NULL, ICON_NONE); // XXX as toggle?
314                 
315                 subcol= uiLayoutColumn(column, 1);
316                         uiLayoutSetActive(subcol, RNA_boolean_get(&strip_ptr, "use_auto_blend")==0); 
317                         uiItemR(subcol, &strip_ptr, "blend_in", 0, NULL, ICON_NONE);
318                         uiItemR(subcol, &strip_ptr, "blend_out", 0, NULL, ICON_NONE);
319                 
320         /* settings */
321         column= uiLayoutColumn(layout, 1);
322                 uiLayoutSetActive(column, !(RNA_boolean_get(&strip_ptr, "use_animated_influence") || RNA_boolean_get(&strip_ptr, "use_animated_time"))); 
323                 uiItemL(column, "Playback Settings:", ICON_NONE);
324                 uiItemR(column, &strip_ptr, "mute", 0, NULL, ICON_NONE);
325                 uiItemR(column, &strip_ptr, "use_reverse", 0, NULL, ICON_NONE);
326 }
327
328
329 /* action-clip only settings for active NLA-Strip */
330 static void nla_panel_actclip(const bContext *C, Panel *pa)
331 {
332         PointerRNA strip_ptr;
333         uiLayout *layout= pa->layout;
334         uiLayout *column, *row;
335         uiBlock *block;
336
337         /* check context and also validity of pointer */
338         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
339                 return;
340         
341         block= uiLayoutGetBlock(layout);
342         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
343                 
344         /* Strip Properties ------------------------------------- */
345         /* action pointer */
346         row= uiLayoutRow(layout, 1);
347                 uiItemR(row, &strip_ptr, "action", 0, NULL, ICON_ACTION);
348                 
349         /* action extents */
350         // XXX custom names were used here (to avoid the prefixes)... probably not necessary in future?
351         column= uiLayoutColumn(layout, 1);
352                 uiItemL(column, "Action Extents:", ICON_NONE);
353                 uiItemR(column, &strip_ptr, "action_frame_start", 0, "Start Frame", ICON_NONE);
354                 uiItemR(column, &strip_ptr, "action_frame_end", 0, "End Frame", ICON_NONE);
355                 uiItemO(column, NULL, ICON_NONE, "NLA_OT_action_sync_length");
356                 
357         /* action usage */
358         column= uiLayoutColumn(layout, 1);
359                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_time")==0); 
360                 uiItemL(column, "Playback Settings:", ICON_NONE);
361                 uiItemR(column, &strip_ptr, "scale", 0, NULL, ICON_NONE);
362                 uiItemR(column, &strip_ptr, "repeat", 0, NULL, ICON_NONE);
363 }
364
365 /* evaluation settings for active NLA-Strip */
366 static void nla_panel_evaluation(const bContext *C, Panel *pa)
367 {
368         PointerRNA strip_ptr;
369         uiLayout *layout= pa->layout;
370         uiLayout *column, *subcolumn, *subrow;
371         uiBlock *block;
372
373         /* check context and also validity of pointer */
374         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
375                 return;
376                 
377         block= uiLayoutGetBlock(layout);
378         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
379                 
380         column= uiLayoutColumn(layout, 1);
381                 uiItemR(column, &strip_ptr, "use_animated_influence", 0, NULL, ICON_NONE);
382                 
383                 subcolumn= uiLayoutColumn(column, 1);
384                 uiLayoutSetEnabled(subcolumn, RNA_boolean_get(&strip_ptr, "use_animated_influence"));   
385                         uiItemR(subcolumn, &strip_ptr, "influence", 0, NULL, ICON_NONE);
386                 
387         
388         column= uiLayoutColumn(layout, 1);
389                 subrow= uiLayoutRow(column, 0);
390                 uiItemR(subrow, &strip_ptr, "use_animated_time", 0, NULL, ICON_NONE);
391                 uiItemR(subrow, &strip_ptr, "use_animated_time_cyclic", 0, NULL, ICON_NONE);
392
393                 subcolumn= uiLayoutColumn(column, 1);
394                 subrow= uiLayoutRow(subcolumn, 0);
395                 uiLayoutSetEnabled(subrow, RNA_boolean_get(&strip_ptr, "use_animated_time"));
396                         uiItemR(subcolumn, &strip_ptr, "strip_time", 0, NULL, ICON_NONE);
397 }
398
399 /* F-Modifiers for active NLA-Strip */
400 static void nla_panel_modifiers(const bContext *C, Panel *pa)
401 {
402         PointerRNA strip_ptr;
403         NlaStrip *strip;
404         FModifier *fcm;
405         uiLayout *col, *row;
406         uiBlock *block;
407
408         /* check context and also validity of pointer */
409         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
410                 return;
411         strip= strip_ptr.data;
412                 
413         block= uiLayoutGetBlock(pa->layout);
414         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
415         
416         /* 'add modifier' button at top of panel */
417         {
418                 row= uiLayoutRow(pa->layout, 0);
419                 block= uiLayoutGetBlock(row);
420                 
421                 // XXX for now, this will be a operator button which calls a temporary 'add modifier' operator
422                 // FIXME: we need to set the only-active property so that this will only add modifiers for the active strip (not all selected)
423                 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");
424                 
425                 /* copy/paste (as sub-row)*/
426                 row= uiLayoutRow(row, 1);
427                         uiItemO(row, "", ICON_COPYDOWN, "NLA_OT_fmodifier_copy");
428                         uiItemO(row, "", ICON_PASTEDOWN, "NLA_OT_fmodifier_paste");
429         }
430         
431         /* draw each modifier */
432         for (fcm= strip->modifiers.first; fcm; fcm= fcm->next) {
433                 col= uiLayoutColumn(pa->layout, 1);
434                 
435                 ANIM_uiTemplate_fmodifier_draw(col, strip_ptr.id.data, &strip->modifiers, fcm);
436         }
437 }
438
439 /* ******************* general ******************************** */
440
441
442 void nla_buttons_register(ARegionType *art)
443 {
444         PanelType *pt;
445         
446         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata");
447         strcpy(pt->idname, "NLA_PT_animdata");
448         strcpy(pt->label, "Animation Data");
449         pt->draw= nla_panel_animdata;
450         pt->poll= nla_animdata_panel_poll;
451         pt->flag= PNL_DEFAULT_CLOSED;
452         BLI_addtail(&art->paneltypes, pt);
453         
454         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel track");
455         strcpy(pt->idname, "NLA_PT_track");
456         strcpy(pt->label, "Active Track");
457         pt->draw= nla_panel_track;
458         pt->poll= nla_track_panel_poll;
459         BLI_addtail(&art->paneltypes, pt);
460         
461         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
462         strcpy(pt->idname, "NLA_PT_properties");
463         strcpy(pt->label, "Active Strip");
464         pt->draw= nla_panel_properties;
465         pt->poll= nla_strip_panel_poll;
466         BLI_addtail(&art->paneltypes, pt);
467         
468         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
469         strcpy(pt->idname, "NLA_PT_actionclip");
470         strcpy(pt->label, "Action Clip");
471         pt->draw= nla_panel_actclip;
472         pt->poll= nla_strip_actclip_panel_poll;
473         BLI_addtail(&art->paneltypes, pt);
474         
475         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation");
476         strcpy(pt->idname, "NLA_PT_evaluation");
477         strcpy(pt->label, "Evaluation");
478         pt->draw= nla_panel_evaluation;
479         pt->poll= nla_strip_panel_poll;
480         BLI_addtail(&art->paneltypes, pt);
481         
482         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers");
483         strcpy(pt->idname, "NLA_PT_modifiers");
484         strcpy(pt->label, "Modifiers");
485         pt->draw= nla_panel_modifiers;
486         pt->poll= nla_strip_panel_poll;
487         BLI_addtail(&art->paneltypes, pt);
488 }
489
490 static int nla_properties(bContext *C, wmOperator *UNUSED(op))
491 {
492         ScrArea *sa= CTX_wm_area(C);
493         ARegion *ar= nla_has_buttons_region(sa);
494         
495         if(ar)
496                 ED_region_toggle_hidden(C, ar);
497
498         return OPERATOR_FINISHED;
499 }
500
501 void NLA_OT_properties(wmOperatorType *ot)
502 {
503         ot->name= "Properties";
504         ot->idname= "NLA_OT_properties";
505         ot->description= "Toggle display properties panel";
506         
507         ot->exec= nla_properties;
508         ot->poll= ED_operator_nla_active;
509
510         /* flags */
511         ot->flag= 0;
512 }