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