4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
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"
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
44 #include "BKE_context.h"
45 #include "BKE_fcurve.h"
46 #include "BKE_screen.h"
47 #include "BKE_utildefines.h"
49 #include "ED_space_api.h"
50 #include "ED_screen.h"
51 #include "ED_anim_api.h"
52 #include "ED_markers.h"
59 #include "UI_interface.h"
60 #include "UI_resources.h"
61 #include "UI_view2d.h"
63 #include "graph_intern.h" // own include
65 /* ******************** manage regions ********************* */
67 ARegion *graph_has_buttons_region(ScrArea *sa)
71 for (ar= sa->regionbase.first; ar; ar= ar->next) {
72 if(ar->regiontype==RGN_TYPE_UI)
76 /* add subdiv level; after main window */
77 for (ar= sa->regionbase.first; ar; ar= ar->next) {
78 if(ar->regiontype==RGN_TYPE_WINDOW)
83 if(ar==NULL) return NULL;
85 arnew= MEM_callocN(sizeof(ARegion), "buttons for nla");
87 BLI_insertlinkafter(&sa->regionbase, ar, arnew);
88 arnew->regiontype= RGN_TYPE_UI;
89 arnew->alignment= RGN_ALIGN_RIGHT;
91 arnew->flag = RGN_FLAG_HIDDEN;
97 /* ******************** default callbacks for ipo space ***************** */
99 static SpaceLink *graph_new(const bContext *C)
101 Scene *scene= CTX_data_scene(C);
105 /* Graph Editor - general stuff */
106 sipo= MEM_callocN(sizeof(SpaceIpo), "init graphedit");
107 sipo->spacetype= SPACE_IPO;
109 sipo->autosnap= SACTSNAP_FRAME;
111 /* allocate DopeSheet data for Graph Editor */
112 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
115 ar= MEM_callocN(sizeof(ARegion), "header for graphedit");
117 BLI_addtail(&sipo->regionbase, ar);
118 ar->regiontype= RGN_TYPE_HEADER;
119 ar->alignment= RGN_ALIGN_BOTTOM;
122 ar= MEM_callocN(sizeof(ARegion), "channels area for graphedit");
124 BLI_addtail(&sipo->regionbase, ar);
125 ar->regiontype= RGN_TYPE_CHANNELS;
126 ar->alignment= RGN_ALIGN_LEFT;
128 ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
131 ar= MEM_callocN(sizeof(ARegion), "buttons area for graphedit");
133 BLI_addtail(&sipo->regionbase, ar);
134 ar->regiontype= RGN_TYPE_UI;
135 ar->alignment= RGN_ALIGN_RIGHT;
136 ar->flag = RGN_FLAG_HIDDEN;
139 ar= MEM_callocN(sizeof(ARegion), "main area for graphedit");
141 BLI_addtail(&sipo->regionbase, ar);
142 ar->regiontype= RGN_TYPE_WINDOW;
144 ar->v2d.tot.xmin= 0.0f;
145 ar->v2d.tot.ymin= (float)scene->r.sfra - 10.0f;
146 ar->v2d.tot.xmax= (float)scene->r.efra;
147 ar->v2d.tot.ymax= 10.0f;
149 ar->v2d.cur= ar->v2d.tot;
151 ar->v2d.min[0]= 0.01f;
152 ar->v2d.min[1]= 0.01f;
154 ar->v2d.max[0]= MAXFRAMEF;
155 ar->v2d.max[1]= 50000.0f;
157 ar->v2d.scroll= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
158 ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
162 return (SpaceLink *)sipo;
165 /* not spacelink itself */
166 static void graph_free(SpaceLink *sl)
168 SpaceIpo *si= (SpaceIpo *)sl;
171 BLI_freelistN(&si->ads->chanbase);
175 if (si->ghostCurves.first)
176 free_fcurves(&si->ghostCurves);
180 /* spacetype; init callback */
181 static void graph_init(struct wmWindowManager *wm, ScrArea *sa)
183 SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
185 /* init dopesheet data if non-existant (i.e. for old files) */
186 if (sipo->ads == NULL)
187 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
189 ED_area_tag_refresh(sa);
192 static SpaceLink *graph_duplicate(SpaceLink *sl)
194 SpaceIpo *sipon= MEM_dupallocN(sl);
196 /* clear or remove stuff from old */
197 BLI_duplicatelist(&sipon->ghostCurves, &((SpaceIpo *)sl)->ghostCurves);
198 sipon->ads= MEM_dupallocN(sipon->ads);
200 return (SpaceLink *)sipon;
203 /* add handlers, stuff you only do once or on area/region changes */
204 static void graph_main_area_init(wmWindowManager *wm, ARegion *ar)
208 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
211 keymap= WM_keymap_listbase(wm, "GraphEdit Keys", SPACE_IPO, 0); /* XXX weak? */
212 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
213 keymap= WM_keymap_listbase(wm, "GraphEdit Generic", SPACE_IPO, 0);
214 WM_event_add_keymap_handler(&ar->handlers, keymap);
217 static void graph_main_area_draw(const bContext *C, ARegion *ar)
219 /* draw entirely, view changes should be handled here */
220 SpaceIpo *sipo= CTX_wm_space_graph(C);
222 View2D *v2d= &ar->v2d;
224 View2DScrollers *scrollers;
226 short unitx=0, unity=V2D_UNIT_VALUES, flag=0;
228 /* clear and setup matrix */
229 UI_GetThemeColor3fv(TH_BACK, col);
230 glClearColor(col[0], col[1], col[2], 0.0);
231 glClear(GL_COLOR_BUFFER_BIT);
233 UI_view2d_view_ortho(C, v2d);
236 unitx= (sipo->flag & SIPO_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
237 grid= UI_view2d_grid_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
238 UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
241 if (ANIM_animdata_get_context(C, &ac)) {
242 /* draw ghost curves */
243 graph_draw_ghost_curves(&ac, sipo, ar, grid);
245 /* draw curves twice - unselected, then selected, so that the are fewer occlusion problems */
246 graph_draw_curves(&ac, sipo, ar, grid, 0);
247 graph_draw_curves(&ac, sipo, ar, grid, 1);
249 /* XXX the slow way to set tot rect... but for nice sliders needed (ton) */
250 get_graph_keyframe_extents(&ac, &v2d->tot.xmin, &v2d->tot.xmax, &v2d->tot.ymin, &v2d->tot.ymax);
251 /* extra offset so that these items are visible */
252 v2d->tot.xmin -= 10.0f;
253 v2d->tot.xmax += 10.0f;
256 /* only free grid after drawing data, as we need to use it to determine sampling rate */
257 UI_view2d_grid_free(grid);
260 if (sipo->flag & SIPO_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
261 if ((sipo->flag & SIPO_NODRAWCFRANUM)==0) flag |= DRAWCFRA_SHOW_NUMBOX;
262 ANIM_draw_cfra(C, v2d, flag);
265 UI_view2d_view_orthoSpecial(C, v2d, 1);
266 draw_markers_time(C, 0);
269 UI_view2d_view_ortho(C, v2d);
270 ANIM_draw_previewrange(C, v2d);
272 /* reset view matrix */
273 UI_view2d_view_restore(C);
276 // FIXME: args for scrollers depend on the type of data being shown...
277 scrollers= UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
278 UI_view2d_scrollers_draw(C, v2d, scrollers);
279 UI_view2d_scrollers_free(scrollers);
282 static void graph_channel_area_init(wmWindowManager *wm, ARegion *ar)
286 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
289 keymap= WM_keymap_listbase(wm, "Animation_Channels", 0, 0); /* XXX weak? */
290 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
291 keymap= WM_keymap_listbase(wm, "GraphEdit Generic", SPACE_IPO, 0);
292 WM_event_add_keymap_handler(&ar->handlers, keymap);
295 static void graph_channel_area_draw(const bContext *C, ARegion *ar)
297 SpaceIpo *sipo= CTX_wm_space_graph(C);
299 View2D *v2d= &ar->v2d;
300 View2DScrollers *scrollers;
303 /* clear and setup matrix */
304 UI_GetThemeColor3fv(TH_BACK, col);
305 glClearColor(col[0], col[1], col[2], 0.0);
306 glClear(GL_COLOR_BUFFER_BIT);
308 UI_view2d_view_ortho(C, v2d);
311 if (ANIM_animdata_get_context(C, &ac)) {
312 graph_draw_channel_names(&ac, sipo, ar);
315 /* reset view matrix */
316 UI_view2d_view_restore(C);
319 scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
320 UI_view2d_scrollers_draw(C, v2d, scrollers);
321 UI_view2d_scrollers_free(scrollers);
324 /* add handlers, stuff you only do once or on area/region changes */
325 static void graph_header_area_init(wmWindowManager *wm, ARegion *ar)
327 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
330 static void graph_header_area_draw(const bContext *C, ARegion *ar)
335 if(ED_screen_area_active(C))
336 UI_GetThemeColor3fv(TH_HEADER, col);
338 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
340 glClearColor(col[0], col[1], col[2], 0.0);
341 glClear(GL_COLOR_BUFFER_BIT);
343 /* set view2d view matrix for scrolling (without scrollers) */
344 UI_view2d_view_ortho(C, &ar->v2d);
346 graph_header_buttons(C, ar);
348 /* restore view matrix? */
349 UI_view2d_view_restore(C);
352 /* add handlers, stuff you only do once or on area/region changes */
353 static void graph_buttons_area_init(wmWindowManager *wm, ARegion *ar)
357 ED_region_panels_init(wm, ar);
359 keymap= WM_keymap_listbase(wm, "GraphEdit Generic", SPACE_IPO, 0);
360 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
363 static void graph_buttons_area_draw(const bContext *C, ARegion *ar)
365 ED_region_panels(C, ar, 1, NULL, -1);
368 static void graph_region_listener(ARegion *ar, wmNotifier *wmn)
370 /* context changes */
371 switch(wmn->category) {
373 ED_region_tag_redraw(ar);
380 ED_region_tag_redraw(ar);
389 ED_region_tag_redraw(ar);
394 if(wmn->data==ND_KEYS)
395 ED_region_tag_redraw(ar);
400 /* editor level listener */
401 static void graph_listener(ScrArea *sa, wmNotifier *wmn)
403 /* context changes */
404 switch (wmn->category) {
406 ED_area_tag_refresh(sa);
409 /*switch (wmn->data) {
412 ED_area_tag_refresh(sa);
415 ED_area_tag_refresh(sa);
418 /*switch (wmn->data) {
421 ED_area_tag_refresh(sa);
424 ED_area_tag_refresh(sa);
427 if(wmn->data==ND_KEYS)
428 ED_area_tag_refresh(sa);
434 static void graph_refresh(const bContext *C, ScrArea *sa)
436 SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
439 /* updates to data needed depends on Graph Editor mode... */
440 switch (sipo->mode) {
441 case SIPO_MODE_ANIMATION: /* all animation */
447 case SIPO_MODE_DRIVERS: /* drivers only */
454 /* region updates? */
455 // XXX resizing y-extents of tot should go here?
457 /* init/adjust F-Curve colors */
458 if (ANIM_animdata_get_context(C, &ac)) {
459 ListBase anim_data = {NULL, NULL};
464 /* build list of F-Curves which will be visible as channels in channel-region
465 * - we don't include ANIMFILTER_CURVEVISIBLE filter, as that will result in a
466 * mismatch between channel-colors and the drawn curves
468 filter= (ANIMFILTER_VISIBLE|ANIMFILTER_CURVESONLY);
469 items= ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
471 /* loop over F-Curves, assigning colors */
472 for (ale=anim_data.first, i=0; ale; ale= ale->next, i++) {
473 FCurve *fcu= (FCurve *)ale->data;
475 /* set color of curve here */
476 switch (fcu->color_mode) {
477 case FCURVE_COLOR_CUSTOM:
478 /* User has defined a custom color for this curve already (we assume it's not going to cause clashes with text colors),
479 * which should be left alone... Nothing needs to be done here.
483 case FCURVE_COLOR_AUTO_RGB:
485 /* F-Curve's array index is automatically mapped to RGB values. This works best of 3-value vectors.
486 * TODO: find a way to module the hue so that not all curves have same color...
489 /* standard table of colors to use */
490 const float _colorsets[4][3]=
492 {1.0f, 0.0f, 0.0f}, /* red */
493 {0.0f, 1.0f, 0.0f}, /* green */
494 {0.0f, 0.0f, 1.0f}, /* blue */
495 {0.3f, 0.8f, 1.0f}, /* 'unknown' color - bluish so as to not conflict with handles */
498 /* simply copy the relevant color over to the F-Curve */
499 if ((fcu->array_index >= 0) && (fcu->array_index < 3)) {
500 /* if the index is within safe bounds, use index to access table */
501 VECCOPY(fcu->color, _colorsets[fcu->array_index]);
504 /* use the 'unknown' color... */
505 VECCOPY(fcu->color, _colorsets[3]);
510 case FCURVE_COLOR_AUTO_RAINBOW:
513 /* determine color 'automatically' using 'magic function' which uses the given args
514 * of current item index + total items to determine some RGB color
516 ipo_rainbow(i, items, fcu->color);
523 BLI_freelistN(&anim_data);
527 /* only called once, from space/spacetypes.c */
528 void ED_spacetype_ipo(void)
530 SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype ipo");
533 st->spaceid= SPACE_IPO;
536 st->free= graph_free;
537 st->init= graph_init;
538 st->duplicate= graph_duplicate;
539 st->operatortypes= graphedit_operatortypes;
540 st->keymap= graphedit_keymap;
541 st->listener= graph_listener;
542 st->refresh= graph_refresh;
544 /* regions: main window */
545 art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
546 art->regionid = RGN_TYPE_WINDOW;
547 art->init= graph_main_area_init;
548 art->draw= graph_main_area_draw;
549 art->listener= graph_region_listener;
550 art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
552 BLI_addhead(&st->regiontypes, art);
554 /* regions: header */
555 art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
556 art->regionid = RGN_TYPE_HEADER;
557 art->minsizey= HEADERY;
558 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
559 art->listener= graph_region_listener;
560 art->init= graph_header_area_init;
561 art->draw= graph_header_area_draw;
563 BLI_addhead(&st->regiontypes, art);
565 /* regions: channels */
566 art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
567 art->regionid = RGN_TYPE_CHANNELS;
568 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 */
569 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
570 art->listener= graph_region_listener;
571 art->init= graph_channel_area_init;
572 art->draw= graph_channel_area_draw;
574 BLI_addhead(&st->regiontypes, art);
576 /* regions: UI buttons */
577 art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
578 art->regionid = RGN_TYPE_UI;
580 art->keymapflag= ED_KEYMAP_UI;
581 art->listener= graph_region_listener;
582 art->init= graph_buttons_area_init;
583 art->draw= graph_buttons_area_draw;
585 BLI_addhead(&st->regiontypes, art);
587 graph_buttons_register(art);
589 BKE_spacetype_register(st);