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