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