Cleanup: indentation, long lines
[blender.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_blenlib.h"
44
45 #include "BLT_translation.h"
46
47 #include "BKE_nla.h"
48 #include "BKE_context.h"
49 #include "BKE_screen.h"
50
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "RNA_access.h"
56
57 #include "ED_anim_api.h"
58 #include "ED_screen.h"
59
60 #include "UI_interface.h"
61 #include "UI_resources.h"
62
63 #include "nla_intern.h" // own include
64
65
66 /* ******************* nla editor space & buttons ************** */
67
68 /* -------------- */
69
70 static void do_nla_region_buttons(bContext *C, void *UNUSED(arg), int UNUSED(event))
71 {
72         //Scene *scene = CTX_data_scene(C);
73 #if 0
74         switch (event) {
75
76         }
77 #endif
78         /* default for now */
79         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
80         WM_event_add_notifier(C, NC_SCENE | ND_TRANSFORM, NULL);
81 }
82
83 bool nla_panel_context(const bContext *C, PointerRNA *adt_ptr, PointerRNA *nlt_ptr, PointerRNA *strip_ptr)
84 {
85         bAnimContext ac;
86         bAnimListElem *ale = NULL;
87         ListBase anim_data = {NULL, NULL};
88         short found = 0; /* not bool, since we need to indicate "found but not ideal" status */
89         int filter;
90         
91         /* for now, only draw if we could init the anim-context info (necessary for all animation-related tools) 
92          * to work correctly is able to be correctly retrieved. There's no point showing empty panels?
93          */
94         if (ANIM_animdata_get_context(C, &ac) == 0) 
95                 return false;
96         
97         /* extract list of active channel(s), of which we should only take the first one 
98          *      - we need the channels flag to get the active AnimData block when there are no NLA Tracks
99          */
100         // XXX: double-check active!
101         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ACTIVE | ANIMFILTER_LIST_CHANNELS);
102         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
103         
104         for (ale = anim_data.first; ale; ale = ale->next) {
105                 switch (ale->type) {
106                         case ANIMTYPE_NLATRACK: /* NLA Track - The primary data type which should get caught */
107                         {
108                                 NlaTrack *nlt = (NlaTrack *)ale->data;
109                                 AnimData *adt = ale->adt;
110                                 
111                                 /* found it, now set the pointers */
112                                 if (adt_ptr) {
113                                         /* AnimData pointer */
114                                         RNA_pointer_create(ale->id, &RNA_AnimData, adt, adt_ptr);
115                                 }
116                                 if (nlt_ptr) {
117                                         /* NLA-Track pointer */
118                                         RNA_pointer_create(ale->id, &RNA_NlaTrack, nlt, nlt_ptr);
119                                 }
120                                 if (strip_ptr) {
121                                         /* NLA-Strip pointer */
122                                         NlaStrip *strip = BKE_nlastrip_find_active(nlt);
123                                         RNA_pointer_create(ale->id, &RNA_NlaStrip, strip, strip_ptr);
124                                 }
125                                 
126                                 found = 1;
127                                 break;
128                         }
129                         case ANIMTYPE_SCENE:    /* Top-Level Widgets doubling up as datablocks */
130                         case ANIMTYPE_OBJECT:
131                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
132                         case ANIMTYPE_DSLAM:
133                         case ANIMTYPE_DSCAM:
134                         case ANIMTYPE_DSCACHEFILE:
135                         case ANIMTYPE_DSCUR:
136                         case ANIMTYPE_DSSKEY:
137                         case ANIMTYPE_DSWOR:
138                         case ANIMTYPE_DSNTREE:
139                         case ANIMTYPE_DSPART:
140                         case ANIMTYPE_DSMBALL:
141                         case ANIMTYPE_DSARM:
142                         case ANIMTYPE_DSMESH:
143                         case ANIMTYPE_DSTEX:
144                         case ANIMTYPE_DSLAT:
145                         case ANIMTYPE_DSLINESTYLE:
146                         case ANIMTYPE_DSSPK:
147                         case ANIMTYPE_DSGPENCIL:
148                         {
149                                 /* for these channels, we only do AnimData */
150                                 if (ale->adt && adt_ptr) {
151                                         ID *id;
152                                         
153                                         if ((ale->data == NULL) || (ale->type == ANIMTYPE_OBJECT)) {
154                                                 /* ale->data is not an ID block! */
155                                                 id = ale->id;
156                                         }
157                                         else {
158                                                 /* ale->data is always the proper ID block we need, but ale->id may not be (i.e. for textures) */
159                                                 id = (ID *)ale->data;
160                                         }
161                                         
162                                         /* AnimData pointer */
163                                         if (adt_ptr) {
164                                                 RNA_pointer_create(id, &RNA_AnimData, ale->adt, adt_ptr);
165                                         }
166                                         
167                                         /* set found status to -1, since setting to 1 would break the loop 
168                                          * and potentially skip an active NLA-Track in some cases...
169                                          */
170                                         found = -1;
171                                 }
172                                 break;
173                         }
174                 }
175                 
176                 if (found > 0)
177                         break;
178         }
179         
180         /* free temp data */
181         ANIM_animdata_freelist(&anim_data);
182         
183         return (found != 0);
184 }
185
186 #if 0
187 static int nla_panel_poll(const bContext *C, PanelType *pt)
188 {
189         return nla_panel_context(C, NULL, NULL);
190 }
191 #endif
192
193 static int nla_animdata_panel_poll(const bContext *C, PanelType *UNUSED(pt))
194 {
195         PointerRNA ptr;
196         return (nla_panel_context(C, &ptr, NULL, NULL) && (ptr.data != NULL));
197 }
198
199 static int nla_track_panel_poll(const bContext *C, PanelType *UNUSED(pt))
200 {
201         PointerRNA ptr;
202         return (nla_panel_context(C, NULL, &ptr, NULL) && (ptr.data != NULL));
203 }
204
205 static int nla_strip_panel_poll(const bContext *C, PanelType *UNUSED(pt))
206 {
207         PointerRNA ptr;
208         return (nla_panel_context(C, NULL, NULL, &ptr) && (ptr.data != NULL));
209 }
210
211 static int nla_strip_actclip_panel_poll(const bContext *C, PanelType *UNUSED(pt))
212 {
213         PointerRNA ptr;
214         NlaStrip *strip;
215         
216         if (!nla_panel_context(C, NULL, NULL, &ptr))
217                 return 0;
218         if (ptr.data == NULL)
219                 return 0;
220         
221         strip = ptr.data;
222         return (strip->type == NLASTRIP_TYPE_CLIP);
223 }
224
225 static int nla_strip_eval_panel_poll(const bContext *C, PanelType *UNUSED(pt))
226 {
227         PointerRNA ptr;
228         NlaStrip *strip;
229         
230         if (!nla_panel_context(C, NULL, NULL, &ptr))
231                 return 0;
232         if (ptr.data == NULL)
233                 return 0;
234         
235         strip = ptr.data;
236         
237         if (strip->type == NLASTRIP_TYPE_SOUND)
238                 return 0;
239                 
240         return 1;
241 }
242
243 /* -------------- */
244
245 /* active AnimData */
246 static void nla_panel_animdata(const bContext *C, Panel *pa)
247 {
248         PointerRNA adt_ptr;
249         /* AnimData *adt; */
250         uiLayout *layout = pa->layout;
251         uiLayout *row;
252         uiBlock *block;
253         
254         /* check context and also validity of pointer */
255         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
256                 return;
257
258         /* adt = adt_ptr.data; */
259         
260         block = uiLayoutGetBlock(layout);
261         UI_block_func_handle_set(block, do_nla_region_buttons, NULL);
262         
263         /* AnimData Source Properties ----------------------------------- */
264         
265         /* icon + id-block name of block where AnimData came from to prevent 
266          * accidentally changing the properties of the wrong action
267          */
268         if (adt_ptr.id.data) {
269                 ID *id = adt_ptr.id.data;
270                 PointerRNA id_ptr;
271                 
272                 RNA_id_pointer_create(id, &id_ptr);
273                 
274                 /* ID-block name > AnimData */
275                 row = uiLayoutRow(layout, true);
276                 uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_LEFT);
277                 
278                 uiItemL(row, id->name + 2, RNA_struct_ui_icon(id_ptr.type));  /* id-block (src) */
279                 uiItemL(row, "", VICO_SMALL_TRI_RIGHT_VEC);                   /* expander */
280                 uiItemL(row, IFACE_("Animation Data"), ICON_ANIM_DATA);       /* animdata */
281                 
282                 uiItemS(layout);
283         }
284         
285         /* Active Action Properties ------------------------------------- */
286         /* action */
287         row = uiLayoutRow(layout, true);
288         uiTemplateID(row, (bContext *)C, &adt_ptr, "action", "ACTION_OT_new", NULL, "NLA_OT_action_unlink");
289         
290         /* extrapolation */
291         row = uiLayoutRow(layout, true);
292         uiItemR(row, &adt_ptr, "action_extrapolation", 0, NULL, ICON_NONE);
293         
294         /* blending */
295         row = uiLayoutRow(layout, true);
296         uiItemR(row, &adt_ptr, "action_blend_type", 0, NULL, ICON_NONE);
297                 
298         /* influence */
299         row = uiLayoutRow(layout, true);
300         uiItemR(row, &adt_ptr, "action_influence", 0, NULL, ICON_NONE);
301 }
302
303 /* active NLA-Track */
304 static void nla_panel_track(const bContext *C, Panel *pa)
305 {
306         PointerRNA nlt_ptr;
307         uiLayout *layout = pa->layout;
308         uiLayout *row;
309         uiBlock *block;
310         
311         /* check context and also validity of pointer */
312         if (!nla_panel_context(C, NULL, &nlt_ptr, NULL))
313                 return;
314         
315         block = uiLayoutGetBlock(layout);
316         UI_block_func_handle_set(block, do_nla_region_buttons, NULL);
317         
318         /* Info - Active NLA-Context:Track ----------------------  */
319         row = uiLayoutRow(layout, true);
320         uiItemR(row, &nlt_ptr, "name", 0, NULL, ICON_NLA);
321 }
322
323 /* generic settings for active NLA-Strip */
324 static void nla_panel_properties(const bContext *C, Panel *pa)
325 {
326         PointerRNA strip_ptr;
327         uiLayout *layout = pa->layout;
328         uiLayout *column, *row, *sub;
329         uiBlock *block;
330         short showEvalProps = 1;
331         
332         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
333                 return;
334         
335         block = uiLayoutGetBlock(layout);
336         UI_block_func_handle_set(block, do_nla_region_buttons, NULL);
337         
338         /* Strip Properties ------------------------------------- */
339         /* strip type */
340         row = uiLayoutColumn(layout, true);
341         uiItemR(row, &strip_ptr, "name", 0, NULL, ICON_NLA);     // XXX icon?
342         uiItemR(row, &strip_ptr, "type", 0, NULL, ICON_NONE);
343         
344         /* strip extents */
345         column = uiLayoutColumn(layout, true);
346         uiItemL(column, IFACE_("Strip Extents:"), ICON_NONE);
347         uiItemR(column, &strip_ptr, "frame_start", 0, NULL, ICON_NONE);
348         uiItemR(column, &strip_ptr, "frame_end", 0, NULL, ICON_NONE);
349         
350         /* Evaluation-Related Strip Properties ------------------ */
351         
352         /* sound properties strips don't have these settings */
353         if (RNA_enum_get(&strip_ptr, "type") == NLASTRIP_TYPE_SOUND)
354                 showEvalProps = 0;
355         
356         /* only show if allowed to... */
357         if (showEvalProps) {
358                 /* extrapolation */
359                 row = uiLayoutRow(layout, true);
360                 uiItemR(row, &strip_ptr, "extrapolation", 0, NULL, ICON_NONE);
361                 
362                 /* blending */
363                 row = uiLayoutRow(layout, true);
364                 uiItemR(row, &strip_ptr, "blend_type", 0, NULL, ICON_NONE);
365                         
366                 /* blend in/out + autoblending
367                  *      - blend in/out can only be set when autoblending is off
368                  */
369                 column = uiLayoutColumn(layout, true);
370                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_influence") == false);
371                 uiItemR(column, &strip_ptr, "use_auto_blend", 0, NULL, ICON_NONE);     // XXX as toggle?
372
373                 sub = uiLayoutColumn(column, true);
374                 uiLayoutSetActive(sub, RNA_boolean_get(&strip_ptr, "use_auto_blend") == false);
375                 uiItemR(sub, &strip_ptr, "blend_in", 0, NULL, ICON_NONE);
376                 uiItemR(sub, &strip_ptr, "blend_out", 0, NULL, ICON_NONE);
377                         
378                 /* settings */
379                 column = uiLayoutColumn(layout, true);
380                 uiLayoutSetActive(column, !(RNA_boolean_get(&strip_ptr, "use_animated_influence") || RNA_boolean_get(&strip_ptr, "use_animated_time")));
381                 uiItemL(column, IFACE_("Playback Settings:"), ICON_NONE);
382                 uiItemR(column, &strip_ptr, "mute", 0, NULL, ICON_NONE);
383                 uiItemR(column, &strip_ptr, "use_reverse", 0, NULL, ICON_NONE);
384         }
385 }
386
387
388 /* action-clip only settings for active NLA-Strip */
389 static void nla_panel_actclip(const bContext *C, Panel *pa)
390 {
391         PointerRNA strip_ptr;
392         uiLayout *layout = pa->layout;
393         uiLayout *column, *row;
394         uiBlock *block;
395
396         /* check context and also validity of pointer */
397         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
398                 return;
399         
400         block = uiLayoutGetBlock(layout);
401         UI_block_func_handle_set(block, do_nla_region_buttons, NULL);
402                 
403         /* Strip Properties ------------------------------------- */
404         /* action pointer */
405         row = uiLayoutRow(layout, true);
406         uiItemR(row, &strip_ptr, "action", 0, NULL, ICON_ACTION);
407                 
408         /* action extents */
409         // XXX custom names were used here (to avoid the prefixes)... probably not necessary in future?
410         column = uiLayoutColumn(layout, true);
411         uiItemL(column, IFACE_("Action Extents:"), ICON_NONE);
412         uiItemR(column, &strip_ptr, "action_frame_start", 0, IFACE_("Start Frame"), ICON_NONE);
413         uiItemR(column, &strip_ptr, "action_frame_end", 0, IFACE_("End Frame"), ICON_NONE);
414         
415         // XXX: this layout may actually be too abstract and confusing, and may be better using standard column layout
416         row = uiLayoutRow(layout, false);
417         uiItemR(row, &strip_ptr, "use_sync_length", 0, IFACE_("Sync Length"), ICON_NONE);
418         uiItemO(row, IFACE_("Now"), ICON_FILE_REFRESH, "NLA_OT_action_sync_length");
419                 
420         /* action usage */
421         column = uiLayoutColumn(layout, true);
422         uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "use_animated_time") == false);
423         uiItemL(column, IFACE_("Playback Settings:"), ICON_NONE);
424         uiItemR(column, &strip_ptr, "scale", 0, NULL, ICON_NONE);
425         uiItemR(column, &strip_ptr, "repeat", 0, NULL, ICON_NONE);
426 }
427
428 /* evaluation settings for active NLA-Strip */
429 static void nla_panel_evaluation(const bContext *C, Panel *pa)
430 {
431         PointerRNA strip_ptr;
432         uiLayout *layout = pa->layout;
433         uiLayout *col, *sub;
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                 
440         block = uiLayoutGetBlock(layout);
441         UI_block_func_handle_set(block, do_nla_region_buttons, NULL);
442                 
443         col = uiLayoutColumn(layout, true);
444         uiItemR(col, &strip_ptr, "use_animated_influence", 0, NULL, ICON_NONE);
445         
446         sub = uiLayoutColumn(col, true);
447         uiLayoutSetEnabled(sub, RNA_boolean_get(&strip_ptr, "use_animated_influence"));
448         uiItemR(sub, &strip_ptr, "influence", 0, NULL, ICON_NONE);
449
450         col = uiLayoutColumn(layout, true);
451         sub = uiLayoutRow(col, false);
452         uiItemR(sub, &strip_ptr, "use_animated_time", 0, NULL, ICON_NONE);
453         uiItemR(sub, &strip_ptr, "use_animated_time_cyclic", 0, NULL, ICON_NONE);
454
455         sub = uiLayoutRow(col, false);
456         uiLayoutSetEnabled(sub, RNA_boolean_get(&strip_ptr, "use_animated_time"));
457         uiItemR(sub, &strip_ptr, "strip_time", 0, NULL, ICON_NONE);
458 }
459
460 /* F-Modifiers for active NLA-Strip */
461 static void nla_panel_modifiers(const bContext *C, Panel *pa)
462 {
463         PointerRNA strip_ptr;
464         NlaStrip *strip;
465         FModifier *fcm;
466         uiLayout *col, *row;
467         uiBlock *block;
468
469         /* check context and also validity of pointer */
470         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
471                 return;
472         strip = strip_ptr.data;
473                 
474         block = uiLayoutGetBlock(pa->layout);
475         UI_block_func_handle_set(block, do_nla_region_buttons, NULL);
476         
477         /* 'add modifier' button at top of panel */
478         {
479                 row = uiLayoutRow(pa->layout, false);
480                 block = uiLayoutGetBlock(row);
481                 
482                 // FIXME: we need to set the only-active property so that this will only add modifiers for the active strip (not all selected)
483                 uiItemMenuEnumO(row, (bContext *)C, "NLA_OT_fmodifier_add", "type", IFACE_("Add Modifier"), ICON_NONE);
484                 
485                 /* copy/paste (as sub-row) */
486                 row = uiLayoutRow(row, true);
487                 uiItemO(row, "", ICON_COPYDOWN, "NLA_OT_fmodifier_copy");
488                 uiItemO(row, "", ICON_PASTEDOWN, "NLA_OT_fmodifier_paste");
489         }
490         
491         /* draw each modifier */
492         for (fcm = strip->modifiers.first; fcm; fcm = fcm->next) {
493                 col = uiLayoutColumn(pa->layout, true);
494                 
495                 ANIM_uiTemplate_fmodifier_draw(col, strip_ptr.id.data, &strip->modifiers, fcm);
496         }
497 }
498
499 /* ******************* general ******************************** */
500
501
502 void nla_buttons_register(ARegionType *art)
503 {
504         PanelType *pt;
505
506         pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata");
507         strcpy(pt->idname, "NLA_PT_animdata");
508         strcpy(pt->label, N_("Animation Data"));
509         strcpy(pt->category, "Animations");
510         strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
511         pt->draw = nla_panel_animdata;
512         pt->poll = nla_animdata_panel_poll;
513         pt->flag = PNL_DEFAULT_CLOSED;
514         BLI_addtail(&art->paneltypes, pt);
515
516         pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel track");
517         strcpy(pt->idname, "NLA_PT_track");
518         strcpy(pt->label, N_("Active Track"));
519         strcpy(pt->category, "Animations");
520         strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
521         pt->draw = nla_panel_track;
522         pt->poll = nla_track_panel_poll;
523         BLI_addtail(&art->paneltypes, pt);
524
525         pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
526         strcpy(pt->idname, "NLA_PT_properties");
527         strcpy(pt->label, N_("Active Strip"));
528         strcpy(pt->category, "Animations");
529         strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
530         pt->draw = nla_panel_properties;
531         pt->poll = nla_strip_panel_poll;
532         BLI_addtail(&art->paneltypes, pt);
533
534         pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
535         strcpy(pt->idname, "NLA_PT_actionclip");
536         strcpy(pt->label, N_("Action Clip"));
537         strcpy(pt->category, "Animations");
538         strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
539         pt->draw = nla_panel_actclip;
540         pt->poll = nla_strip_actclip_panel_poll;
541         BLI_addtail(&art->paneltypes, pt);
542
543         pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation");
544         strcpy(pt->idname, "NLA_PT_evaluation");
545         strcpy(pt->label, N_("Evaluation"));
546         strcpy(pt->category, "Animations");
547         strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
548         pt->draw = nla_panel_evaluation;
549         pt->poll = nla_strip_eval_panel_poll;
550         BLI_addtail(&art->paneltypes, pt);
551
552         pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers");
553         strcpy(pt->idname, "NLA_PT_modifiers");
554         strcpy(pt->label, N_("Modifiers"));
555         strcpy(pt->category, "Modifiers");
556         strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
557         pt->draw = nla_panel_modifiers;
558         pt->poll = nla_strip_eval_panel_poll;
559         BLI_addtail(&art->paneltypes, pt);
560 }
561
562 static int nla_properties_toggle_exec(bContext *C, wmOperator *UNUSED(op))
563 {
564         ScrArea *sa = CTX_wm_area(C);
565         ARegion *ar = nla_has_buttons_region(sa);
566         
567         if (ar)
568                 ED_region_toggle_hidden(C, ar);
569
570         return OPERATOR_FINISHED;
571 }
572
573 void NLA_OT_properties(wmOperatorType *ot)
574 {
575         ot->name = "Properties";
576         ot->idname = "NLA_OT_properties";
577         ot->description = "Toggle the properties region visibility";
578         
579         ot->exec = nla_properties_toggle_exec;
580         ot->poll = ED_operator_nla_active;
581
582         /* flags */
583         ot->flag = 0;
584 }