Remove unneeded notifier data added in revision 26219.
[blender-staging.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <float.h>
33
34 #include "DNA_anim_types.h"
35 #include "DNA_action_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_userdef_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_math.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_editVert.h"
47 #include "BLI_rand.h"
48
49 #include "BKE_animsys.h"
50 #include "BKE_nla.h"
51 #include "BKE_action.h"
52 #include "BKE_context.h"
53 #include "BKE_curve.h"
54 #include "BKE_customdata.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_fcurve.h"
57 #include "BKE_object.h"
58 #include "BKE_global.h"
59 #include "BKE_scene.h"
60 #include "BKE_screen.h"
61 #include "BKE_utildefines.h"
62
63 #include "BIF_gl.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "RNA_access.h"
69 #include "RNA_define.h"
70
71 #include "ED_anim_api.h"
72 #include "ED_keyframing.h"
73 #include "ED_screen.h"
74 #include "ED_types.h"
75 #include "ED_util.h"
76
77 #include "UI_interface.h"
78 #include "UI_resources.h"
79 #include "UI_view2d.h"
80
81 #include "nla_intern.h" // own include
82
83
84 /* ******************* nla editor space & buttons ************** */
85
86 #define B_NOP           1
87 #define B_REDR          2
88
89 /* -------------- */
90
91 static void do_nla_region_buttons(bContext *C, void *arg, int event)
92 {
93         //Scene *scene= CTX_data_scene(C);
94         
95         switch(event) {
96
97         }
98         
99         /* default for now */
100         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
101         WM_event_add_notifier(C, NC_SCENE|ND_TRANSFORM, NULL);
102 }
103
104 static int nla_panel_context(const bContext *C, PointerRNA *adt_ptr, PointerRNA *nlt_ptr, PointerRNA *strip_ptr)
105 {
106         bAnimContext ac;
107         bAnimListElem *ale= NULL;
108         ListBase anim_data = {NULL, NULL};
109         short found=0;
110         int filter;
111         
112         /* for now, only draw if we could init the anim-context info (necessary for all animation-related tools) 
113          * to work correctly is able to be correctly retrieved. There's no point showing empty panels?
114          */
115         if (ANIM_animdata_get_context(C, &ac) == 0) 
116                 return 0;
117         
118         /* extract list of active channel(s), of which we should only take the first one 
119          *      - we need the channels flag to get the active AnimData block when there are no NLA Tracks
120          */
121         filter= (ANIMFILTER_VISIBLE|ANIMFILTER_ACTIVE|ANIMFILTER_CHANNELS);
122         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
123         
124         for (ale= anim_data.first; ale; ale= ale->next) {
125                 switch (ale->type) {
126                         case ANIMTYPE_NLATRACK: /* NLA Track - The primary data type which should get caught */
127                         {
128                                 NlaTrack *nlt= (NlaTrack *)ale->data;
129                                 AnimData *adt= ale->adt;
130                                 
131                                 /* found it, now set the pointers */
132                                 if (adt_ptr) {
133                                         /* AnimData pointer */
134                                         RNA_pointer_create(ale->id, &RNA_AnimData, adt, adt_ptr);
135                                 }
136                                 if (nlt_ptr) {
137                                         /* NLA-Track pointer */
138                                         RNA_pointer_create(ale->id, &RNA_NlaTrack, nlt, nlt_ptr);
139                                 }
140                                 if (strip_ptr) {
141                                         /* NLA-Strip pointer */
142                                         NlaStrip *strip= BKE_nlastrip_find_active(nlt);
143                                         RNA_pointer_create(ale->id, &RNA_NlaStrip, strip, strip_ptr);
144                                 }
145                                 
146                                 found= 1;
147                         }
148                                 break;
149                                 
150                         case ANIMTYPE_SCENE:    /* Top-Level Widgets doubling up as datablocks */
151                         case ANIMTYPE_OBJECT:
152                         case ANIMTYPE_FILLACTD: /* Action Expander */
153                         case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
154                         case ANIMTYPE_DSLAM:
155                         case ANIMTYPE_DSCAM:
156                         case ANIMTYPE_DSCUR:
157                         case ANIMTYPE_DSSKEY:
158                         case ANIMTYPE_DSWOR:
159                         case ANIMTYPE_DSNTREE:
160                         case ANIMTYPE_DSPART:
161                         case ANIMTYPE_DSMBALL:
162                         case ANIMTYPE_DSARM:
163                         {
164                                 /* for these channels, we only do AnimData */
165                                 if (ale->id && ale->adt) {
166                                         if (adt_ptr) {
167                                                 /* AnimData pointer */
168                                                 RNA_pointer_create(ale->id, &RNA_AnimData, ale->adt, adt_ptr);
169                                                 
170                                                 /* set found status to -1, since setting to 1 would break the loop 
171                                                  * and potentially skip an active NLA-Track in some cases...
172                                                  */
173                                                 found= -1;
174                                         }
175                                 }
176                         }       
177                                 break;
178                 }
179                 
180                 if (found > 0)
181                         break;
182         }
183         
184         /* free temp data */
185         BLI_freelistN(&anim_data);
186         
187         return found;
188 }
189
190 #if 0
191 static int nla_panel_poll(const bContext *C, PanelType *pt)
192 {
193         return nla_panel_context(C, NULL, NULL);
194 }
195 #endif
196
197 static int nla_animdata_panel_poll(const bContext *C, PanelType *pt)
198 {
199         PointerRNA ptr;
200         return (nla_panel_context(C, &ptr, NULL, NULL) && (ptr.data != NULL));
201 }
202
203 static int nla_track_panel_poll(const bContext *C, PanelType *pt)
204 {
205         PointerRNA ptr;
206         return (nla_panel_context(C, NULL, &ptr, NULL) && (ptr.data != NULL));
207 }
208
209 static int nla_strip_panel_poll(const bContext *C, PanelType *pt)
210 {
211         PointerRNA ptr;
212         return (nla_panel_context(C, NULL, NULL, &ptr) && (ptr.data != NULL));
213 }
214
215 static int nla_strip_actclip_panel_poll(const bContext *C, PanelType *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         return (strip->type == NLASTRIP_TYPE_CLIP);
227 }
228
229 /* -------------- */
230
231 /* active AnimData */
232 static void nla_panel_animdata (const bContext *C, Panel *pa)
233 {
234         PointerRNA adt_ptr;
235         AnimData *adt;
236         uiLayout *layout= pa->layout;
237         uiLayout *row;
238         uiBlock *block;
239         
240         /* check context and also validity of pointer */
241         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
242                 return;
243         adt= adt_ptr.data;
244         
245         block= uiLayoutGetBlock(layout);
246         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
247         
248         /* Active Action Properties ------------------------------------- */
249         /* action */
250         row= uiLayoutRow(layout, 1);
251                 uiTemplateID(row, (bContext *)C, &adt_ptr, "action", "ACTION_OT_new", NULL, NULL /*"ACTION_OT_unlink"*/); // XXX: need to make these operators
252         
253         /* extrapolation */
254         row= uiLayoutRow(layout, 1);
255                 uiItemR(row, NULL, 0, &adt_ptr, "action_extrapolation", 0);
256         
257         /* blending */
258         row= uiLayoutRow(layout, 1);
259                 uiItemR(row, NULL, 0, &adt_ptr, "action_blending", 0);  
260                 
261         /* influence */
262         row= uiLayoutRow(layout, 1);
263                 uiItemR(row, NULL, 0, &adt_ptr, "action_influence", 0);
264 }
265
266 /* active NLA-Track */
267 static void nla_panel_track (const bContext *C, Panel *pa)
268 {
269         PointerRNA nlt_ptr;
270         uiLayout *layout= pa->layout;
271         uiLayout *row;
272         uiBlock *block;
273         
274         /* check context and also validity of pointer */
275         if (!nla_panel_context(C, NULL, &nlt_ptr, NULL))
276                 return;
277         
278         block= uiLayoutGetBlock(layout);
279         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
280         
281         /* Info - Active NLA-Context:Track ----------------------  */
282         row= uiLayoutRow(layout, 1);
283                 uiItemR(row, NULL, ICON_NLA, &nlt_ptr, "name", 0);
284 }
285
286 /* generic settings for active NLA-Strip */
287 static void nla_panel_properties(const bContext *C, Panel *pa)
288 {
289         PointerRNA strip_ptr;
290         uiLayout *layout= pa->layout;
291         uiLayout *column, *row, *subcol;
292         uiBlock *block;
293         
294         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
295                 return;
296         
297         block= uiLayoutGetBlock(layout);
298         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
299         
300         /* Strip Properties ------------------------------------- */
301         /* strip type */
302         row= uiLayoutColumn(layout, 1);
303                 uiItemR(row, NULL, ICON_NLA, &strip_ptr, "name", 0); // XXX icon?
304                 uiItemR(row, NULL, 0, &strip_ptr, "type", 0);
305         
306         /* strip extents */
307         column= uiLayoutColumn(layout, 1);
308                 uiItemL(column, "Strip Extents:", 0);
309                 uiItemR(column, NULL, 0, &strip_ptr, "start_frame", 0);
310                 uiItemR(column, NULL, 0, &strip_ptr, "end_frame", 0);
311         
312         /* extrapolation */
313         row= uiLayoutRow(layout, 1);
314                 uiItemR(row, NULL, 0, &strip_ptr, "extrapolation", 0);
315         
316         /* blending */
317         row= uiLayoutRow(layout, 1);
318                 uiItemR(row, NULL, 0, &strip_ptr, "blending", 0);
319                 
320         /* blend in/out + autoblending
321          *      - blend in/out can only be set when autoblending is off
322          */
323         column= uiLayoutColumn(layout, 1);
324                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "animated_influence")==0); 
325                 uiItemR(column, NULL, 0, &strip_ptr, "auto_blending", 0); // XXX as toggle?
326                 
327                 subcol= uiLayoutColumn(column, 1);
328                         uiLayoutSetActive(subcol, RNA_boolean_get(&strip_ptr, "auto_blending")==0); 
329                         uiItemR(subcol, NULL, 0, &strip_ptr, "blend_in", 0);
330                         uiItemR(subcol, NULL, 0, &strip_ptr, "blend_out", 0);
331                 
332         /* settings */
333         column= uiLayoutColumn(layout, 1);
334                 uiLayoutSetActive(column, !(RNA_boolean_get(&strip_ptr, "animated_influence") || RNA_boolean_get(&strip_ptr, "animated_time"))); 
335                 uiItemL(column, "Playback Settings:", 0);
336                 uiItemR(column, NULL, 0, &strip_ptr, "muted", 0);
337                 uiItemR(column, NULL, 0, &strip_ptr, "reversed", 0);
338 }
339
340
341 /* action-clip only settings for active NLA-Strip */
342 static void nla_panel_actclip(const bContext *C, Panel *pa)
343 {
344         PointerRNA strip_ptr;
345         uiLayout *layout= pa->layout;
346         uiLayout *column, *row;
347         uiBlock *block;
348
349         /* check context and also validity of pointer */
350         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
351                 return;
352         
353         block= uiLayoutGetBlock(layout);
354         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
355                 
356         /* Strip Properties ------------------------------------- */
357         /* action pointer */
358         row= uiLayoutRow(layout, 1);
359                 uiItemR(row, NULL, ICON_ACTION, &strip_ptr, "action", 0);
360                 
361         /* action extents */
362         // XXX custom names were used here (to avoid the prefixes)... probably not necessary in future?
363         column= uiLayoutColumn(layout, 1);
364                 uiItemL(column, "Action Extents:", 0);
365                 uiItemR(column, "Start Frame", 0, &strip_ptr, "action_start_frame", 0);
366                 uiItemR(column, "End Frame", 0, &strip_ptr, "action_end_frame", 0);
367                 uiItemO(column, NULL, 0, "NLA_OT_action_sync_length");
368                 
369         /* action usage */
370         column= uiLayoutColumn(layout, 1);
371                 uiLayoutSetActive(column, RNA_boolean_get(&strip_ptr, "animated_time")==0); 
372                 uiItemL(column, "Playback Settings:", 0);
373                 uiItemR(column, NULL, 0, &strip_ptr, "scale", 0);
374                 uiItemR(column, NULL, 0, &strip_ptr, "repeat", 0);
375 }
376
377 /* evaluation settings for active NLA-Strip */
378 static void nla_panel_evaluation(const bContext *C, Panel *pa)
379 {
380         PointerRNA strip_ptr;
381         uiLayout *layout= pa->layout;
382         uiLayout *column, *subcolumn;
383         uiBlock *block;
384
385         /* check context and also validity of pointer */
386         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
387                 return;
388                 
389         block= uiLayoutGetBlock(layout);
390         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
391                 
392         column= uiLayoutColumn(layout, 1);
393                 uiItemR(column, NULL, 0, &strip_ptr, "animated_influence", 0);
394                 
395                 subcolumn= uiLayoutColumn(column, 1);
396                 uiLayoutSetEnabled(subcolumn, RNA_boolean_get(&strip_ptr, "animated_influence"));       
397                         uiItemR(subcolumn, NULL, 0, &strip_ptr, "influence", 0);
398                 
399         
400         column= uiLayoutColumn(layout, 1);
401                 uiItemR(column, NULL, 0, &strip_ptr, "animated_time", 0);
402                 
403                 subcolumn= uiLayoutColumn(column, 1);
404                 uiLayoutSetEnabled(subcolumn, RNA_boolean_get(&strip_ptr, "animated_time"));
405                         uiItemR(subcolumn, NULL, 0, &strip_ptr, "strip_time", 0);
406 }
407
408 /* F-Modifiers for active NLA-Strip */
409 static void nla_panel_modifiers(const bContext *C, Panel *pa)
410 {
411         PointerRNA strip_ptr;
412         NlaStrip *strip;
413         FModifier *fcm;
414         uiLayout *col, *row;
415         uiBlock *block;
416
417         /* check context and also validity of pointer */
418         if (!nla_panel_context(C, NULL, NULL, &strip_ptr))
419                 return;
420         strip= strip_ptr.data;
421                 
422         block= uiLayoutGetBlock(pa->layout);
423         uiBlockSetHandleFunc(block, do_nla_region_buttons, NULL);
424         
425         /* 'add modifier' button at top of panel */
426         {
427                 row= uiLayoutRow(pa->layout, 0);
428                 block= uiLayoutGetBlock(row);
429                 
430                 // XXX for now, this will be a operator button which calls a temporary 'add modifier' operator
431                 // FIXME: we need to set the only-active property so that this will only add modifiers for the active strip (not all selected)
432                 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");
433         }
434         
435         /* draw each modifier */
436         for (fcm= strip->modifiers.first; fcm; fcm= fcm->next) {
437                 col= uiLayoutColumn(pa->layout, 1);
438                 
439                 ANIM_uiTemplate_fmodifier_draw(col, strip_ptr.id.data, &strip->modifiers, fcm);
440         }
441 }
442
443 /* ******************* general ******************************** */
444
445
446 void nla_buttons_register(ARegionType *art)
447 {
448         PanelType *pt;
449         
450         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata");
451         strcpy(pt->idname, "NLA_PT_animdata");
452         strcpy(pt->label, "Animation Data");
453         pt->draw= nla_panel_animdata;
454         pt->poll= nla_animdata_panel_poll;
455         pt->flag= PNL_DEFAULT_CLOSED;
456         BLI_addtail(&art->paneltypes, pt);
457         
458         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel track");
459         strcpy(pt->idname, "NLA_PT_track");
460         strcpy(pt->label, "Active Track");
461         pt->draw= nla_panel_track;
462         pt->poll= nla_track_panel_poll;
463         BLI_addtail(&art->paneltypes, pt);
464         
465         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
466         strcpy(pt->idname, "NLA_PT_properties");
467         strcpy(pt->label, "Active Strip");
468         pt->draw= nla_panel_properties;
469         pt->poll= nla_strip_panel_poll;
470         BLI_addtail(&art->paneltypes, pt);
471         
472         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel properties");
473         strcpy(pt->idname, "NLA_PT_actionclip");
474         strcpy(pt->label, "Action Clip");
475         pt->draw= nla_panel_actclip;
476         pt->poll= nla_strip_actclip_panel_poll;
477         BLI_addtail(&art->paneltypes, pt);
478         
479         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation");
480         strcpy(pt->idname, "NLA_PT_evaluation");
481         strcpy(pt->label, "Evaluation");
482         pt->draw= nla_panel_evaluation;
483         pt->poll= nla_strip_panel_poll;
484         BLI_addtail(&art->paneltypes, pt);
485         
486         pt= MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers");
487         strcpy(pt->idname, "NLA_PT_modifiers");
488         strcpy(pt->label, "Modifiers");
489         pt->draw= nla_panel_modifiers;
490         pt->poll= nla_strip_panel_poll;
491         BLI_addtail(&art->paneltypes, pt);
492 }
493
494 static int nla_properties(bContext *C, wmOperator *op)
495 {
496         ScrArea *sa= CTX_wm_area(C);
497         ARegion *ar= nla_has_buttons_region(sa);
498         
499         if(ar)
500                 ED_region_toggle_hidden(C, ar);
501
502         return OPERATOR_FINISHED;
503 }
504
505 void NLA_OT_properties(wmOperatorType *ot)
506 {
507         ot->name= "Properties";
508         ot->idname= "NLA_OT_properties";
509         
510         ot->exec= nla_properties;
511         ot->poll= ED_operator_nla_active;
512         
513         /* flags */
514         ot->flag= 0;
515 }