Bye bye vile relics of extinct version control systems,
[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 "BKE_nla.h"
49 #include "BKE_context.h"
50 #include "BKE_screen.h"
51
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "RNA_access.h"
57
58 #include "ED_anim_api.h"
59 #include "ED_screen.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63
64 #include "nla_intern.h" // own include
65
66
67 /* ******************* nla editor space & buttons ************** */
68
69 #define B_NOP           1
70 #define B_REDR          2
71
72 /* -------------- */
73
74 static void do_nla_region_buttons(bContext *C, void *UNUSED(arg), int event)
75 {
76         //Scene *scene= CTX_data_scene(C);
77         
78         switch(event) {
79
80         }
81         
82         /* default for now */
83         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
84         WM_event_add_notifier(C, NC_SCENE|ND_TRANSFORM, NULL);
85 }
86
87 static int nla_panel_context(const bContext *C, PointerRNA *adt_ptr, PointerRNA *nlt_ptr, PointerRNA *strip_ptr)
88 {
89         bAnimContext ac;
90         bAnimListElem *ale= NULL;
91         ListBase anim_data = {NULL, NULL};
92         short found=0;
93         int filter;
94         
95         /* for now, only draw if we could init the anim-context info (necessary for all animation-related tools) 
96          * to work correctly is able to be correctly retrieved. There's no point showing empty panels?
97          */
98         if (ANIM_animdata_get_context(C, &ac) == 0) 
99                 return 0;
100         
101         /* extract list of active channel(s), of which we should only take the first one 
102          *      - we need the channels flag to get the active AnimData block when there are no NLA Tracks
103          */
104         // XXX: double-check active!
105         filter= (ANIMFILTER_DATA_VISIBLE|ANIMFILTER_LIST_VISIBLE|ANIMFILTER_ACTIVE|ANIMFILTER_LIST_CHANNELS);
106         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
107         
108         for (ale= anim_data.first; ale; ale= ale->next) {
109                 switch (ale->type) {
110                         case ANIMTYPE_NLATRACK: /* NLA Track - The primary data type which should get caught */
111                         {
112                                 NlaTrack *nlt= (NlaTrack *)ale->data;
113                                 AnimData *adt= ale->adt;
114                                 
115                                 /* found it, now set the pointers */
116                                 if (adt_ptr) {
117                                         /* AnimData pointer */
118                                         RNA_pointer_create(ale->id, &RNA_AnimData, adt, adt_ptr);
119                                 }
120                                 if (nlt_ptr) {
121                                         /* NLA-Track pointer */
122                                         RNA_pointer_create(ale->id, &RNA_NlaTrack, nlt, nlt_ptr);
123                                 }
124                                 if (strip_ptr) {
125                                         /* NLA-Strip pointer */
126                                         NlaStrip *strip= BKE_nlastrip_find_active(nlt);
127                                         RNA_pointer_create(ale->id, &RNA_NlaStrip, strip, strip_ptr);
128                                 }
129                                 
130                                 found= 1;
131                         }
132                                 break;
133                                 
134                         case ANIMTYPE_SCENE:    /* Top-Level Widgets doubling up as datablocks */
135                         case ANIMTYPE_OBJECT:
136                         case ANIMTYPE_FILLACTD: /* Action Expander */
137                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
138                         case ANIMTYPE_DSLAM:
139                         case ANIMTYPE_DSCAM:
140                         case ANIMTYPE_DSCUR:
141                         case ANIMTYPE_DSSKEY:
142                         case ANIMTYPE_DSWOR:
143                         case ANIMTYPE_DSNTREE:
144                         case ANIMTYPE_DSPART:
145                         case ANIMTYPE_DSMBALL:
146                         case ANIMTYPE_DSARM:
147                         case ANIMTYPE_DSSPK:
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 static int nla_strip_eval_panel_poll(const bContext *C, PanelType *UNUSED(pt))
215 {
216         PointerRNA ptr;
217         NlaStrip *strip;
218         
219         if (!nla_panel_context(C, NULL, NULL, &ptr))
220                 return 0;
221         if (ptr.data == NULL)
222                 return 0;
223         
224         strip= ptr.data;
225         
226         if (strip->type == NLASTRIP_TYPE_SOUND)
227                 return 0;
228                 
229         return 1;
230 }
231
232 /* -------------- */
233
234 /* active AnimData */
235 static void nla_panel_animdata (const bContext *C, Panel *pa)
236 {
237         PointerRNA adt_ptr;
238         /* AnimData *adt; */
239         uiLayout *layout= pa->layout;
240         uiLayout *row;
241         uiBlock *block;
242         
243         /* check context and also validity of pointer */
244         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
245                 return;
246
247         /* adt= adt_ptr.data; */
248         
249         block= uiLayoutGetBlock(layout);
250         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
251         
252         /* Active Action Properties ------------------------------------- */
253         /* action */
254         row= uiLayoutRow(layout, 1);
255                 uiTemplateID(row, (bContext *)C, &adt_ptr, "action", "ACTION_OT_new", NULL, NULL /*"ACTION_OT_unlink"*/); // XXX: need to make these operators
256         
257         /* extrapolation */
258         row= uiLayoutRow(layout, 1);
259                 uiItemR(row, &adt_ptr, "action_extrapolation", 0, NULL, ICON_NONE);
260         
261         /* blending */
262         row= uiLayoutRow(layout, 1);
263                 uiItemR(row, &adt_ptr, "action_blend_type", 0, NULL, ICON_NONE);
264                 
265         /* influence */
266         row= uiLayoutRow(layout, 1);
267                 uiItemR(row, &adt_ptr, "action_influence", 0, NULL, ICON_NONE);
268 }
269
270 /* active NLA-Track */
271 static void nla_panel_track (const bContext *C, Panel *pa)
272 {
273         PointerRNA nlt_ptr;
274         uiLayout *layout= pa->layout;
275         uiLayout *row;
276         uiBlock *block;
277         
278         /* check context and also validity of pointer */
279         if (!nla_panel_context(C, NULL, &nlt_ptr, NULL))
280                 return;
281         
282         block= uiLayoutGetBlock(layout);
283         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
284         
285         /* Info - Active NLA-Context:Track ----------------------  */
286         row= uiLayoutRow(layout, 1);
287                 uiItemR(row, &nlt_ptr, "name", 0, NULL, ICON_NLA);
288 }
289
290 /* generic settings for active NLA-Strip */
291 static void nla_panel_properties(const bContext *C, Panel *pa)
292 {
293         PointerRNA strip_ptr;
294         uiLayout *layout= pa->layout;
295         uiLayout *column, *row, *subcol;
296         uiBlock *block;
297         short showEvalProps = 1;
298         
299         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
300                 return;
301         
302         block= uiLayoutGetBlock(layout);
303         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
304         
305         /* Strip Properties ------------------------------------- */
306         /* strip type */
307         row= uiLayoutColumn(layout, 1);
308                 uiItemR(row, &strip_ptr, "name", 0, NULL, ICON_NLA); // XXX icon?
309                 uiItemR(row, &strip_ptr, "type", 0, NULL, ICON_NONE);
310         
311         /* strip extents */
312         column= uiLayoutColumn(layout, 1);
313                 uiItemL(column, "Strip Extents:", ICON_NONE);
314                 uiItemR(column, &strip_ptr, "frame_start", 0, NULL, ICON_NONE);
315                 uiItemR(column, &strip_ptr, "frame_end", 0, NULL, ICON_NONE);
316         
317         /* Evaluation-Related Strip Properties ------------------ */
318         
319         /* sound properties strips don't have these settings */
320         if (RNA_enum_get(&strip_ptr, "type") == NLASTRIP_TYPE_SOUND)
321                 showEvalProps = 0;
322         
323         /* only show if allowed to... */
324         if (showEvalProps) {
325                 /* extrapolation */
326                 row= uiLayoutRow(layout, 1);
327                         uiItemR(row, &strip_ptr, "extrapolation", 0, NULL, ICON_NONE);
328                 
329                 /* blending */
330                 row= uiLayoutRow(layout, 1);
331                         uiItemR(row, &strip_ptr, "blend_type", 0, NULL, ICON_NONE);
332                         
333                 /* blend in/out + autoblending
334                  *      - blend in/out can only be set when autoblending is off
335                  */
336                 column= uiLayoutColumn(layout, 1);
337                         uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_influence")==0); 
338                         uiItemR(column, &strip_ptr, "use_auto_blend", 0, NULL, ICON_NONE); // XXX as toggle?
339                         
340                         subcol= uiLayoutColumn(column, 1);
341                                 uiLayoutSetActive(subcol, RNA_boolean_get(&strip_ptr, "use_auto_blend")==0); 
342                                 uiItemR(subcol, &strip_ptr, "blend_in", 0, NULL, ICON_NONE);
343                                 uiItemR(subcol, &strip_ptr, "blend_out", 0, NULL, ICON_NONE);
344                         
345                 /* settings */
346                 column= uiLayoutColumn(layout, 1);
347                         uiLayoutSetActive(column, !(RNA_boolean_get(&strip_ptr, "use_animated_influence") || RNA_boolean_get(&strip_ptr, "use_animated_time"))); 
348                         uiItemL(column, "Playback Settings:", ICON_NONE);
349                         uiItemR(column, &strip_ptr, "mute", 0, NULL, ICON_NONE);
350                         uiItemR(column, &strip_ptr, "use_reverse", 0, NULL, ICON_NONE);
351         }
352 }
353
354
355 /* action-clip only settings for active NLA-Strip */
356 static void nla_panel_actclip(const bContext *C, Panel *pa)
357 {
358         PointerRNA strip_ptr;
359         uiLayout *layout= pa->layout;
360         uiLayout *column, *row;
361         uiBlock *block;
362
363         /* check context and also validity of pointer */
364         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
365                 return;
366         
367         block= uiLayoutGetBlock(layout);
368         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
369                 
370         /* Strip Properties ------------------------------------- */
371         /* action pointer */
372         row= uiLayoutRow(layout, 1);
373                 uiItemR(row, &strip_ptr, "action", 0, NULL, ICON_ACTION);
374                 
375         /* action extents */
376         // XXX custom names were used here (to avoid the prefixes)... probably not necessary in future?
377         column= uiLayoutColumn(layout, 1);
378                 uiItemL(column, "Action Extents:", ICON_NONE);
379                 uiItemR(column, &strip_ptr, "action_frame_start", 0, "Start Frame", ICON_NONE);
380                 uiItemR(column, &strip_ptr, "action_frame_end", 0, "End Frame", ICON_NONE);
381                 uiItemO(column, NULL, ICON_NONE, "NLA_OT_action_sync_length");
382                 
383         /* action usage */
384         column= uiLayoutColumn(layout, 1);
385                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_time")==0); 
386                 uiItemL(column, "Playback Settings:", ICON_NONE);
387                 uiItemR(column, &strip_ptr, "scale", 0, NULL, ICON_NONE);
388                 uiItemR(column, &strip_ptr, "repeat", 0, NULL, ICON_NONE);
389 }
390
391 /* evaluation settings for active NLA-Strip */
392 static void nla_panel_evaluation(const bContext *C, Panel *pa)
393 {
394         PointerRNA strip_ptr;
395         uiLayout *layout= pa->layout;
396         uiLayout *column, *subcolumn, *subrow;
397         uiBlock *block;
398
399         /* check context and also validity of pointer */
400         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
401                 return;
402                 
403         block= uiLayoutGetBlock(layout);
404         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
405                 
406         column= uiLayoutColumn(layout, 1);
407                 uiItemR(column, &strip_ptr, "use_animated_influence", 0, NULL, ICON_NONE);
408                 
409                 subcolumn= uiLayoutColumn(column, 1);
410                 uiLayoutSetEnabled(subcolumn, RNA_boolean_get(&strip_ptr, "use_animated_influence"));   
411                         uiItemR(subcolumn, &strip_ptr, "influence", 0, NULL, ICON_NONE);
412                 
413         
414         column= uiLayoutColumn(layout, 1);
415                 subrow= uiLayoutRow(column, 0);
416                 uiItemR(subrow, &strip_ptr, "use_animated_time", 0, NULL, ICON_NONE);
417                 uiItemR(subrow, &strip_ptr, "use_animated_time_cyclic", 0, NULL, ICON_NONE);
418
419                 subcolumn= uiLayoutColumn(column, 1);
420                 subrow= uiLayoutRow(subcolumn, 0);
421                 uiLayoutSetEnabled(subrow, RNA_boolean_get(&strip_ptr, "use_animated_time"));
422                         uiItemR(subcolumn, &strip_ptr, "strip_time", 0, NULL, ICON_NONE);
423 }
424
425 /* F-Modifiers for active NLA-Strip */
426 static void nla_panel_modifiers(const bContext *C, Panel *pa)
427 {
428         PointerRNA strip_ptr;
429         NlaStrip *strip;
430         FModifier *fcm;
431         uiLayout *col, *row;
432         uiBlock *block;
433
434         /* check context and also validity of pointer */
435         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
436                 return;
437         strip= strip_ptr.data;
438                 
439         block= uiLayoutGetBlock(pa->layout);
440         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
441         
442         /* 'add modifier' button at top of panel */
443         {
444                 row= uiLayoutRow(pa->layout, 0);
445                 block= uiLayoutGetBlock(row);
446                 
447                 // XXX for now, this will be a operator button which calls a temporary 'add modifier' operator
448                 // FIXME: we need to set the only-active property so that this will only add modifiers for the active strip (not all selected)
449                 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");
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 }