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