NLA SoC: Merge from 2.5
[blender.git] / source / blender / editors / space_graph / space_graph.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) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_anim_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_space_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_context.h"
45 #include "BKE_fcurve.h"
46 #include "BKE_screen.h"
47 #include "BKE_utildefines.h"
48
49 #include "ED_space_api.h"
50 #include "ED_screen.h"
51 #include "ED_anim_api.h"
52 #include "ED_markers.h"
53
54 #include "BIF_gl.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "UI_interface.h"
60 #include "UI_resources.h"
61 #include "UI_view2d.h"
62
63 #include "graph_intern.h"       // own include
64
65 /* ******************** manage regions ********************* */
66
67 ARegion *graph_has_buttons_region(ScrArea *sa)
68 {
69         ARegion *ar, *arnew;
70         
71         for(ar= sa->regionbase.first; ar; ar= ar->next)
72                 if(ar->regiontype==RGN_TYPE_UI)
73                         return ar;
74         
75         /* add subdiv level; after channel */
76         for(ar= sa->regionbase.first; ar; ar= ar->next)
77                 if(ar->regiontype==RGN_TYPE_CHANNELS)
78                         break;
79         
80         /* is error! */
81         if(ar==NULL) return NULL;
82         
83         arnew= MEM_callocN(sizeof(ARegion), "buttons for view3d");
84         
85         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
86         arnew->regiontype= RGN_TYPE_UI;
87         arnew->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
88         
89         arnew->flag = RGN_FLAG_HIDDEN;
90         
91         return arnew;
92 }
93
94
95 /* ******************** default callbacks for ipo space ***************** */
96
97 static SpaceLink *graph_new(const bContext *C)
98 {
99         Scene *scene= CTX_data_scene(C);
100         ARegion *ar;
101         SpaceIpo *sipo;
102         
103         /* Graph Editor - general stuff */
104         sipo= MEM_callocN(sizeof(SpaceIpo), "init graphedit");
105         sipo->spacetype= SPACE_IPO;
106         
107         sipo->autosnap= SACTSNAP_FRAME;
108         
109         /* allocate DopeSheet data for Graph Editor */
110         sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
111         
112         /* header */
113         ar= MEM_callocN(sizeof(ARegion), "header for graphedit");
114         
115         BLI_addtail(&sipo->regionbase, ar);
116         ar->regiontype= RGN_TYPE_HEADER;
117         ar->alignment= RGN_ALIGN_BOTTOM;
118         
119         /* channels */
120         ar= MEM_callocN(sizeof(ARegion), "main area for graphedit");
121         
122         BLI_addtail(&sipo->regionbase, ar);
123         ar->regiontype= RGN_TYPE_CHANNELS;
124         ar->alignment= RGN_ALIGN_LEFT;
125         
126         ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
127         
128         /* ui buttons */
129         ar= MEM_callocN(sizeof(ARegion), "main area for graphedit");
130         
131         BLI_addtail(&sipo->regionbase, ar);
132         ar->regiontype= RGN_TYPE_UI;
133         ar->alignment= RGN_ALIGN_TOP|RGN_SPLIT_PREV;
134         ar->flag = RGN_FLAG_HIDDEN;
135         
136         /* main area */
137         ar= MEM_callocN(sizeof(ARegion), "main area for graphedit");
138         
139         BLI_addtail(&sipo->regionbase, ar);
140         ar->regiontype= RGN_TYPE_WINDOW;
141         
142         ar->v2d.tot.xmin= 0.0f;
143         ar->v2d.tot.ymin= (float)scene->r.sfra - 10.0f;
144         ar->v2d.tot.xmax= (float)scene->r.efra;
145         ar->v2d.tot.ymax= 10.0f;
146         
147         ar->v2d.cur= ar->v2d.tot;
148         
149         ar->v2d.min[0]= 0.01f;
150         ar->v2d.min[1]= 0.01f;
151         
152         ar->v2d.max[0]= MAXFRAMEF;
153         ar->v2d.max[1]= 50000.0f;
154         
155         ar->v2d.scroll= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
156         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
157         
158         ar->v2d.keeptot= 0;
159         
160         return (SpaceLink *)sipo;
161 }
162
163 /* not spacelink itself */
164 static void graph_free(SpaceLink *sl)
165 {       
166         SpaceIpo *si= (SpaceIpo *)sl;
167         
168         if (si->ads) {
169                 BLI_freelistN(&si->ads->chanbase);
170                 MEM_freeN(si->ads);
171         }
172         
173         if (si->ghostCurves.first)
174                 free_fcurves(&si->ghostCurves);
175 }
176
177
178 /* spacetype; init callback */
179 static void graph_init(struct wmWindowManager *wm, ScrArea *sa)
180 {
181         SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
182         
183         /* init dopesheet data if non-existant (i.e. for old files) */
184         if (sipo->ads == NULL)
185                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
186
187         ED_area_tag_refresh(sa);
188 }
189
190 static SpaceLink *graph_duplicate(SpaceLink *sl)
191 {
192         SpaceIpo *sipon= MEM_dupallocN(sl);
193         
194         /* clear or remove stuff from old */
195         BLI_duplicatelist(&sipon->ghostCurves, &((SpaceIpo *)sl)->ghostCurves);
196         sipon->ads= MEM_dupallocN(sipon->ads);
197         
198         return (SpaceLink *)sipon;
199 }
200
201 /* add handlers, stuff you only do once or on area/region changes */
202 static void graph_main_area_init(wmWindowManager *wm, ARegion *ar)
203 {
204         ListBase *keymap;
205         
206         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
207         
208         /* own keymap */
209         keymap= WM_keymap_listbase(wm, "GraphEdit Keys", SPACE_IPO, 0); /* XXX weak? */
210         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
211         keymap= WM_keymap_listbase(wm, "GraphEdit Generic", SPACE_IPO, 0);
212         WM_event_add_keymap_handler(&ar->handlers, keymap);
213 }
214
215 static void graph_main_area_draw(const bContext *C, ARegion *ar)
216 {
217         /* draw entirely, view changes should be handled here */
218         SpaceIpo *sipo= (SpaceIpo*)CTX_wm_space_data(C);
219         bAnimContext ac;
220         View2D *v2d= &ar->v2d;
221         View2DGrid *grid;
222         View2DScrollers *scrollers;
223         float col[3];
224         short unitx=0, unity=V2D_UNIT_VALUES, flag=0;
225         
226         /* clear and setup matrix */
227         UI_GetThemeColor3fv(TH_BACK, col);
228         glClearColor(col[0], col[1], col[2], 0.0);
229         glClear(GL_COLOR_BUFFER_BIT);
230         
231         UI_view2d_view_ortho(C, v2d);
232         
233         /* grid */
234         unitx= (sipo->flag & SIPO_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
235         grid= UI_view2d_grid_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
236         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
237         
238         /* draw data */
239         if (ANIM_animdata_get_context(C, &ac)) {
240                 /* draw ghost curves */
241                 graph_draw_ghost_curves(&ac, sipo, ar, grid);
242                 
243                 /* draw curves twice - unselected, then selected, so that the are fewer occlusion problems */
244                 graph_draw_curves(&ac, sipo, ar, grid, 0);
245                 graph_draw_curves(&ac, sipo, ar, grid, 1);
246         }
247         
248         /* only free grid after drawing data, as we need to use it to determine sampling rate */
249         UI_view2d_grid_free(grid);
250         
251         /* current frame */
252         if (sipo->flag & SIPO_DRAWTIME)         flag |= DRAWCFRA_UNIT_SECONDS;
253         if ((sipo->flag & SIPO_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
254         ANIM_draw_cfra(C, v2d, flag);
255         
256         /* markers */
257         UI_view2d_view_orthoSpecial(C, v2d, 1);
258         draw_markers_time(C, 0);
259         
260         /* preview range */
261         UI_view2d_view_ortho(C, v2d);
262         ANIM_draw_previewrange(C, v2d);
263         
264         /* reset view matrix */
265         UI_view2d_view_restore(C);
266         
267         /* scrollers */
268                 // FIXME: args for scrollers depend on the type of data being shown...
269         scrollers= UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
270         UI_view2d_scrollers_draw(C, v2d, scrollers);
271         UI_view2d_scrollers_free(scrollers);
272 }
273
274 static void graph_channel_area_init(wmWindowManager *wm, ARegion *ar)
275 {
276         ListBase *keymap;
277         
278         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
279         
280         /* own keymap */
281         keymap= WM_keymap_listbase(wm, "Animation_Channels", 0, 0);     /* XXX weak? */
282         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
283         keymap= WM_keymap_listbase(wm, "GraphEdit Generic", SPACE_IPO, 0);
284         WM_event_add_keymap_handler(&ar->handlers, keymap);
285 }
286
287 static void graph_channel_area_draw(const bContext *C, ARegion *ar)
288 {
289         SpaceIpo *sipo= (SpaceIpo *)CTX_wm_space_data(C);
290         bAnimContext ac;
291         View2D *v2d= &ar->v2d;
292         View2DScrollers *scrollers;
293         float col[3];
294         
295         /* clear and setup matrix */
296         UI_GetThemeColor3fv(TH_BACK, col);
297         glClearColor(col[0], col[1], col[2], 0.0);
298         glClear(GL_COLOR_BUFFER_BIT);
299         
300         UI_view2d_view_ortho(C, v2d);
301         
302         /* draw channels */
303         if (ANIM_animdata_get_context(C, &ac)) {
304                 graph_draw_channel_names(&ac, sipo, ar);
305         }
306         
307         /* reset view matrix */
308         UI_view2d_view_restore(C);
309         
310         /* scrollers */
311         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
312         UI_view2d_scrollers_draw(C, v2d, scrollers);
313         UI_view2d_scrollers_free(scrollers);
314 }
315
316 /* add handlers, stuff you only do once or on area/region changes */
317 static void graph_header_area_init(wmWindowManager *wm, ARegion *ar)
318 {
319         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
320 }
321
322 static void graph_header_area_draw(const bContext *C, ARegion *ar)
323 {
324         float col[3];
325         
326         /* clear */
327         if(ED_screen_area_active(C))
328                 UI_GetThemeColor3fv(TH_HEADER, col);
329         else
330                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
331         
332         glClearColor(col[0], col[1], col[2], 0.0);
333         glClear(GL_COLOR_BUFFER_BIT);
334         
335         /* set view2d view matrix for scrolling (without scrollers) */
336         UI_view2d_view_ortho(C, &ar->v2d);
337         
338         graph_header_buttons(C, ar);
339         
340         /* restore view matrix? */
341         UI_view2d_view_restore(C);
342 }
343
344 /* add handlers, stuff you only do once or on area/region changes */
345 static void graph_buttons_area_init(wmWindowManager *wm, ARegion *ar)
346 {
347         ListBase *keymap;
348         
349         ED_region_panels_init(wm, ar);
350
351         keymap= WM_keymap_listbase(wm, "GraphEdit Generic", SPACE_IPO, 0);
352         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
353 }
354
355 static void graph_buttons_area_draw(const bContext *C, ARegion *ar)
356 {
357         ED_region_panels(C, ar, 1, NULL);
358 }
359
360 static void graph_region_listener(ARegion *ar, wmNotifier *wmn)
361 {
362         /* context changes */
363         switch(wmn->category) {
364                 case NC_SCENE:
365                         switch(wmn->data) {
366                                 case ND_OB_ACTIVE:
367                                 case ND_FRAME:
368                                 case ND_MARKERS:
369                                         ED_region_tag_redraw(ar);
370                                         break;
371                         }
372                         break;
373                 case NC_OBJECT:
374                         switch(wmn->data) {
375                                 case ND_BONE_ACTIVE:
376                                 case ND_BONE_SELECT:
377                                 case ND_KEYS:
378                                         ED_region_tag_redraw(ar);
379                                         break;
380                         }
381                         break;
382                 default:
383                         if(wmn->data==ND_KEYS)
384                                 ED_region_tag_redraw(ar);
385                                 
386         }
387 }
388
389 /* editor level listener */
390 static void graph_listener(ScrArea *sa, wmNotifier *wmn)
391 {
392         /* context changes */
393         switch (wmn->category) {
394                 case NC_SCENE:
395                         /*switch (wmn->data) {
396                                 case ND_OB_ACTIVE:
397                                 case ND_OB_SELECT:
398                                         ED_area_tag_refresh(sa);
399                                         break;
400                         }*/
401                         ED_area_tag_refresh(sa);
402                         break;
403                 case NC_OBJECT:
404                         /*switch (wmn->data) {
405                                 case ND_BONE_SELECT:
406                                 case ND_BONE_ACTIVE:
407                                         ED_area_tag_refresh(sa);
408                                         break;
409                         }*/
410                         ED_area_tag_refresh(sa);
411                         break;
412                 default:
413                         if(wmn->data==ND_KEYS)
414                                 ED_area_tag_refresh(sa);
415         }
416 }
417
418
419
420 static void graph_refresh(const bContext *C, ScrArea *sa)
421 {
422         SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
423         bAnimContext ac;
424         
425         /* updates to data needed depends on Graph Editor mode... */
426         switch (sipo->mode) {
427                 case SIPO_MODE_ANIMATION: /* all animation */
428                 {
429                         
430                 }
431                         break;
432                 
433                 case SIPO_MODE_DRIVERS: /* drivers only  */
434                 {
435                 
436                 }
437                         break; 
438         }
439         
440         /* region updates? */
441         // XXX resizing y-extents of tot should go here?
442         
443         /* init/adjust F-Curve colors */
444         if (ANIM_animdata_get_context(C, &ac)) {
445                 ListBase anim_data = {NULL, NULL};
446                 bAnimListElem *ale;
447                 int filter;
448                 int items, i;
449                 
450                 /* build list of F-Curves which will be visible as channels in channel-region
451                  *      - we don't include ANIMFILTER_CURVEVISIBLE filter, as that will result in a 
452                  *        mismatch between channel-colors and the drawn curves
453                  */
454                 filter= (ANIMFILTER_VISIBLE|ANIMFILTER_CURVESONLY);
455                 items= ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
456                 
457                 /* loop over F-Curves, assigning colors */
458                 for (ale=anim_data.first, i=0; ale; ale= ale->next, i++) {
459                         FCurve *fcu= (FCurve *)ale->data;
460                         
461                         /* set color of curve here */
462                         switch (fcu->color_mode) {
463                                 case FCURVE_COLOR_CUSTOM:
464                                         /* User has defined a custom color for this curve already (we assume it's not going to cause clashes with text colors),
465                                          * which should be left alone... Nothing needs to be done here.
466                                          */
467                                         break;
468                                         
469                                 case FCURVE_COLOR_AUTO_RGB:
470                                 {
471                                         /* F-Curve's array index is automatically mapped to RGB values. This works best of 3-value vectors. 
472                                          * TODO: find a way to module the hue so that not all curves have same color...
473                                          */
474                                         
475                                         /* standard table of colors to use */
476                                         const float _colorsets[4][3]= 
477                                         {
478                                                 {1.0f, 0.0f, 0.0f}, /* red */
479                                                 {0.0f, 1.0f, 0.0f}, /* green */
480                                                 {0.0f, 0.0f, 1.0f}, /* blue */
481                                                 {0.3f, 0.8f, 1.0f}, /* 'unknown' color - bluish so as to not conflict with handles */
482                                         };
483                                         
484                                         /* simply copy the relevant color over to the F-Curve */
485                                         if ((fcu->array_index >= 0) && (fcu->array_index < 3)) {
486                                                 /* if the index is within safe bounds, use index to access table */
487                                                 VECCOPY(fcu->color, _colorsets[fcu->array_index]);
488                                         }
489                                         else {
490                                                 /* use the 'unknown' color... */
491                                                 VECCOPY(fcu->color, _colorsets[3]);
492                                         }
493                                 }
494                                         break;
495                                 
496                                 case FCURVE_COLOR_AUTO_RAINBOW:
497                                 default:
498                                 {
499                                         /* determine color 'automatically' using 'magic function' which uses the given args
500                                          * of current item index + total items to determine some RGB color
501                                          */
502                                         ipo_rainbow(i, items, fcu->color);
503                                 }
504                                         break;
505                         }
506                 }
507                 
508                 /* free temp list */
509                 BLI_freelistN(&anim_data);
510         }
511 }
512
513 /* only called once, from space/spacetypes.c */
514 void ED_spacetype_ipo(void)
515 {
516         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype ipo");
517         ARegionType *art;
518         
519         st->spaceid= SPACE_IPO;
520         
521         st->new= graph_new;
522         st->free= graph_free;
523         st->init= graph_init;
524         st->duplicate= graph_duplicate;
525         st->operatortypes= graphedit_operatortypes;
526         st->keymap= graphedit_keymap;
527         st->listener= graph_listener;
528         st->refresh= graph_refresh;
529         
530         /* regions: main window */
531         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
532         art->regionid = RGN_TYPE_WINDOW;
533         art->init= graph_main_area_init;
534         art->draw= graph_main_area_draw;
535         art->listener= graph_region_listener;
536         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
537
538         BLI_addhead(&st->regiontypes, art);
539         
540         /* regions: header */
541         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
542         art->regionid = RGN_TYPE_HEADER;
543         art->minsizey= HEADERY;
544         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
545         art->listener= graph_region_listener;
546         art->init= graph_header_area_init;
547         art->draw= graph_header_area_draw;
548         
549         BLI_addhead(&st->regiontypes, art);
550         
551         /* regions: channels */
552         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
553         art->regionid = RGN_TYPE_CHANNELS;
554         art->minsizex= 200+V2D_SCROLL_WIDTH; /* 200 is the 'standard', but due to scrollers, we want a bit more to fit the lock icons in */
555         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
556         art->listener= graph_region_listener;
557         art->init= graph_channel_area_init;
558         art->draw= graph_channel_area_draw;
559         
560         BLI_addhead(&st->regiontypes, art);
561         
562         /* regions: UI buttons */
563         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
564         art->regionid = RGN_TYPE_UI;
565         art->minsizey= 200;
566         art->keymapflag= ED_KEYMAP_UI;
567         art->listener= graph_region_listener;
568         art->init= graph_buttons_area_init;
569         art->draw= graph_buttons_area_draw;
570         
571         BLI_addhead(&st->regiontypes, art);
572
573         graph_buttons_register(art);
574         
575         BKE_spacetype_register(st);
576 }
577