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