2.5
[blender.git] / source / blender / editors / space_graph / space_graph.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_anim_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_space_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_context.h"
45 #include "BKE_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         ED_area_tag_refresh(sa);
146 }
147
148 static SpaceLink *graph_duplicate(SpaceLink *sl)
149 {
150         SpaceIpo *sipon= MEM_dupallocN(sl);
151         
152         /* clear or remove stuff from old */
153         //sipon->ipokey.first= sipon->ipokey.last= NULL;
154         sipon->ads= MEM_dupallocN(sipon->ads);
155         
156         return (SpaceLink *)sipon;
157 }
158
159 /* add handlers, stuff you only do once or on area/region changes */
160 static void graph_main_area_init(wmWindowManager *wm, ARegion *ar)
161 {
162         ListBase *keymap;
163         
164         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
165         
166         /* own keymap */
167         keymap= WM_keymap_listbase(wm, "GraphEdit Keys", SPACE_IPO, 0); /* XXX weak? */
168         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
169 }
170
171 static void graph_main_area_draw(const bContext *C, ARegion *ar)
172 {
173         /* draw entirely, view changes should be handled here */
174         SpaceIpo *sipo= (SpaceIpo*)CTX_wm_space_data(C);
175         bAnimContext ac;
176         View2D *v2d= &ar->v2d;
177         View2DGrid *grid;
178         View2DScrollers *scrollers;
179         float col[3];
180         short unitx=0, unity=V2D_UNIT_VALUES, flag=0;
181         
182         /* clear and setup matrix */
183         UI_GetThemeColor3fv(TH_BACK, col);
184         glClearColor(col[0], col[1], col[2], 0.0);
185         glClear(GL_COLOR_BUFFER_BIT);
186         
187         UI_view2d_view_ortho(C, v2d);
188         
189         /* grid */
190         unitx= (sipo->flag & SIPO_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
191         grid= UI_view2d_grid_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
192         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
193         UI_view2d_grid_free(grid);
194         
195         /* draw data */
196         if (ANIM_animdata_get_context(C, &ac)) {
197                 graph_draw_curves(&ac, sipo, ar);
198         }
199         
200         /* current frame */
201         if (sipo->flag & SIPO_DRAWTIME)         flag |= DRAWCFRA_UNIT_SECONDS;
202         if ((sipo->flag & SIPO_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
203         ANIM_draw_cfra(C, v2d, flag);
204         
205         /* markers */
206         UI_view2d_view_orthoSpecial(C, v2d, 1);
207         draw_markers_time(C, 0);
208         
209         /* preview range */
210         UI_view2d_view_ortho(C, v2d);
211         ANIM_draw_previewrange(C, v2d);
212         
213         /* reset view matrix */
214         UI_view2d_view_restore(C);
215         
216         /* scrollers */
217                 // FIXME: args for scrollers depend on the type of data being shown...
218         scrollers= UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
219         UI_view2d_scrollers_draw(C, v2d, scrollers);
220         UI_view2d_scrollers_free(scrollers);
221 }
222
223 static void graph_channel_area_init(wmWindowManager *wm, ARegion *ar)
224 {
225         ListBase *keymap;
226         
227         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
228         
229         /* own keymap */
230         keymap= WM_keymap_listbase(wm, "Animation_Channels", 0, 0);     /* XXX weak? */
231         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
232 }
233
234 static void graph_channel_area_draw(const bContext *C, ARegion *ar)
235 {
236         SpaceIpo *sipo= (SpaceIpo *)CTX_wm_space_data(C);
237         bAnimContext ac;
238         View2D *v2d= &ar->v2d;
239         View2DScrollers *scrollers;
240         float col[3];
241         
242         /* clear and setup matrix */
243         UI_GetThemeColor3fv(TH_SHADE2, col);
244         glClearColor(col[0], col[1], col[2], 0.0);
245         glClear(GL_COLOR_BUFFER_BIT);
246         
247         UI_view2d_view_ortho(C, v2d);
248         
249         /* draw channels */
250         if (ANIM_animdata_get_context(C, &ac)) {
251                 graph_draw_channel_names(&ac, sipo, ar);
252         }
253         
254         /* reset view matrix */
255         UI_view2d_view_restore(C);
256         
257         /* scrollers */
258         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
259         UI_view2d_scrollers_draw(C, v2d, scrollers);
260         UI_view2d_scrollers_free(scrollers);
261 }
262
263 /* add handlers, stuff you only do once or on area/region changes */
264 static void graph_header_area_init(wmWindowManager *wm, ARegion *ar)
265 {
266         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
267 }
268
269 static void graph_header_area_draw(const bContext *C, ARegion *ar)
270 {
271         float col[3];
272         
273         /* clear */
274         if(ED_screen_area_active(C))
275                 UI_GetThemeColor3fv(TH_HEADER, col);
276         else
277                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
278         
279         glClearColor(col[0], col[1], col[2], 0.0);
280         glClear(GL_COLOR_BUFFER_BIT);
281         
282         /* set view2d view matrix for scrolling (without scrollers) */
283         UI_view2d_view_ortho(C, &ar->v2d);
284         
285         graph_header_buttons(C, ar);
286         
287         /* restore view matrix? */
288         UI_view2d_view_restore(C);
289 }
290
291 static void graph_region_listener(ARegion *ar, wmNotifier *wmn)
292 {
293         /* context changes */
294         switch(wmn->category) {
295                 case NC_SCENE:
296                         switch(wmn->data) {
297                                 case ND_OB_ACTIVE:
298                                 case ND_FRAME:
299                                 case ND_MARKERS:
300                                         ED_region_tag_redraw(ar);
301                                         break;
302                         }
303                         break;
304                 case NC_OBJECT:
305                         switch(wmn->data) {
306                                 case ND_BONE_ACTIVE:
307                                 case ND_BONE_SELECT:
308                                 case ND_KEYS:
309                                         ED_region_tag_redraw(ar);
310                                         break;
311                         }
312                         break;
313                 default:
314                         if(wmn->data==ND_KEYS)
315                                 ED_region_tag_redraw(ar);
316                                 
317         }
318 }
319
320 /* editor level listener */
321 static void graph_listener(ScrArea *sa, wmNotifier *wmn)
322 {
323         /* context changes */
324         switch (wmn->category) {
325                 case NC_SCENE:
326                         /*switch (wmn->data) {
327                                 case ND_OB_ACTIVE:
328                                 case ND_OB_SELECT:
329                                         ED_area_tag_refresh(sa);
330                                         break;
331                         }*/
332                         ED_area_tag_refresh(sa);
333                         break;
334                 case NC_OBJECT:
335                         /*switch (wmn->data) {
336                                 case ND_BONE_SELECT:
337                                 case ND_BONE_ACTIVE:
338                                         ED_area_tag_refresh(sa);
339                                         break;
340                         }*/
341                         ED_area_tag_refresh(sa);
342                         break;
343                 default:
344                         if(wmn->data==ND_KEYS)
345                                 ED_area_tag_refresh(sa);
346         }
347 }
348
349
350
351 static void graph_refresh(const bContext *C, ScrArea *sa)
352 {
353         SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
354         bAnimContext ac;
355         
356         /* updates to data needed depends on Graph Editor mode... */
357         switch (sipo->mode) {
358                 case SIPO_MODE_ANIMATION: /* all animation */
359                 {
360                         
361                 }
362                         break;
363                 
364                 case SIPO_MODE_DRIVERS: /* drivers only  */
365                 {
366                 
367                 }
368                         break; 
369         }
370         
371         /* region updates? */
372         // XXX resizing y-extents of tot should go here?
373         
374         /* init/adjust F-Curve colors */
375         if (ANIM_animdata_get_context(C, &ac)) {
376                 ListBase anim_data = {NULL, NULL};
377                 bAnimListElem *ale;
378                 int filter;
379                 int items, i;
380                 
381                 /* build list of F-Curves which will be visible as channels in channel-region
382                  *      - we don't include ANIMFILTER_CURVEVISIBLE filter, as that will result in a 
383                  *        mismatch between channel-colors and the drawn curves
384                  */
385                 filter= (ANIMFILTER_VISIBLE|ANIMFILTER_CURVESONLY);
386                 items= ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
387                 
388                 /* loop over F-Curves, assigning colors */
389                 for (ale=anim_data.first, i=0; ale; ale= ale->next, i++) {
390                         FCurve *fcu= (FCurve *)ale->data;
391                         
392                         /* set color of curve here */
393                         switch (fcu->color_mode) {
394                                 case FCURVE_COLOR_CUSTOM:
395                                         /* User has defined a custom color for this curve already (we assume it's not going to cause clashes with text colors),
396                                          * which should be left alone... Nothing needs to be done here.
397                                          */
398                                         break;
399                                         
400                                 case FCURVE_COLOR_AUTO_RGB:
401                                 {
402                                         /* F-Curve's array index is automatically mapped to RGB values. This works best of 3-value vectors. 
403                                          * TODO: find a way to module the hue so that not all curves have same color...
404                                          */
405                                         
406                                         /* standard table of colors to use */
407                                         const float _colorsets[4][3]= 
408                                         {
409                                                 {1.0f, 0.0f, 0.0f}, /* red */
410                                                 {0.0f, 1.0f, 0.0f}, /* green */
411                                                 {0.0f, 0.0f, 1.0f}, /* blue */
412                                                 {0.3f, 0.8f, 1.0f}, /* 'unknown' color - bluish so as to not conflict with handles */
413                                         };
414                                         
415                                         /* simply copy the relevant color over to the F-Curve */
416                                         if ((fcu->array_index >= 0) && (fcu->array_index < 3)) {
417                                                 /* if the index is within safe bounds, use index to access table */
418                                                 VECCOPY(fcu->color, _colorsets[fcu->array_index]);
419                                         }
420                                         else {
421                                                 /* use the 'unknown' color... */
422                                                 VECCOPY(fcu->color, _colorsets[3]);
423                                         }
424                                 }
425                                         break;
426                                 
427                                 case FCURVE_COLOR_AUTO_RAINBOW:
428                                 default:
429                                 {
430                                         /* determine color 'automatically' using 'magic function' which uses the given args
431                                          * of current item index + total items to determine some RGB color
432                                          */
433                                         ipo_rainbow(i, items, fcu->color);
434                                 }
435                                         break;
436                         }
437                 }
438                 
439                 /* free temp list */
440                 BLI_freelistN(&anim_data);
441         }
442 }
443
444 /* only called once, from space/spacetypes.c */
445 void ED_spacetype_ipo(void)
446 {
447         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype ipo");
448         ARegionType *art;
449         
450         st->spaceid= SPACE_IPO;
451         
452         st->new= graph_new;
453         st->free= graph_free;
454         st->init= graph_init;
455         st->duplicate= graph_duplicate;
456         st->operatortypes= graphedit_operatortypes;
457         st->keymap= graphedit_keymap;
458         st->listener= graph_listener;
459         st->refresh= graph_refresh;
460         
461         /* regions: main window */
462         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
463         art->regionid = RGN_TYPE_WINDOW;
464         art->init= graph_main_area_init;
465         art->draw= graph_main_area_draw;
466         art->listener= graph_region_listener;
467         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
468
469         BLI_addhead(&st->regiontypes, art);
470         
471         /* regions: header */
472         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
473         art->regionid = RGN_TYPE_HEADER;
474         art->minsizey= HEADERY;
475         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
476         art->listener= graph_region_listener;
477         art->init= graph_header_area_init;
478         art->draw= graph_header_area_draw;
479         
480         BLI_addhead(&st->regiontypes, art);
481         
482         /* regions: channels */
483         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
484         art->regionid = RGN_TYPE_CHANNELS;
485         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 */
486         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
487         art->listener= graph_region_listener;
488         art->init= graph_channel_area_init;
489         art->draw= graph_channel_area_draw;
490         
491         BLI_addhead(&st->regiontypes, art);
492         
493         
494         BKE_spacetype_register(st);
495 }
496