2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_graph/space_graph.c
35 #include "DNA_anim_types.h"
36 #include "DNA_scene_types.h"
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
44 #include "BKE_context.h"
45 #include "BKE_global.h"
47 #include "BKE_fcurve.h"
48 #include "BKE_screen.h"
50 #include "ED_space_api.h"
51 #include "ED_screen.h"
52 #include "ED_anim_api.h"
53 #include "ED_markers.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 ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
74 /* add subdiv level; after main */
75 ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
78 if (ar == NULL) return NULL;
80 arnew = MEM_callocN(sizeof(ARegion), "buttons for graph");
82 BLI_insertlinkafter(&sa->regionbase, ar, arnew);
83 arnew->regiontype = RGN_TYPE_UI;
84 arnew->alignment = RGN_ALIGN_RIGHT;
86 arnew->flag = RGN_FLAG_HIDDEN;
92 /* ******************** default callbacks for ipo space ***************** */
94 static SpaceLink *graph_new(const bContext *C)
96 Scene *scene = CTX_data_scene(C);
100 /* Graph Editor - general stuff */
101 sipo = MEM_callocN(sizeof(SpaceIpo), "init graphedit");
102 sipo->spacetype = SPACE_IPO;
104 sipo->autosnap = SACTSNAP_FRAME;
106 /* allocate DopeSheet data for Graph Editor */
107 sipo->ads = MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
108 sipo->ads->source = (ID *)scene;
110 /* settings for making it easier by default to just see what you're interested in tweaking */
111 sipo->ads->filterflag |= ADS_FILTER_ONLYSEL;
112 sipo->flag |= SIPO_SELVHANDLESONLY;
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] = FLT_MIN;
152 ar->v2d.min[1] = FLT_MIN;
154 ar->v2d.max[0] = MAXFRAMEF;
155 ar->v2d.max[1] = FLT_MAX;
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 *UNUSED(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");
188 sipo->ads->source = (ID *)(G.main->scene.first); // FIXME: this is a really nasty hack here for now...
191 /* force immediate init of any invalid F-Curve colors */
192 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
193 ED_area_tag_refresh(sa);
196 static SpaceLink *graph_duplicate(SpaceLink *sl)
198 SpaceIpo *sipon = MEM_dupallocN(sl);
200 /* clear or remove stuff from old */
201 BLI_duplicatelist(&sipon->ghostCurves, &((SpaceIpo *)sl)->ghostCurves);
202 sipon->ads = MEM_dupallocN(sipon->ads);
204 return (SpaceLink *)sipon;
207 /* add handlers, stuff you only do once or on area/region changes */
208 static void graph_main_area_init(wmWindowManager *wm, ARegion *ar)
212 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
215 keymap = WM_keymap_find(wm->defaultconf, "Graph Editor", SPACE_IPO, 0);
216 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
217 keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
218 WM_event_add_keymap_handler(&ar->handlers, keymap);
221 static void graph_main_area_draw(const bContext *C, ARegion *ar)
223 /* draw entirely, view changes should be handled here */
224 SpaceIpo *sipo = CTX_wm_space_graph(C);
226 View2D *v2d = &ar->v2d;
228 View2DScrollers *scrollers;
230 short unitx = 0, unity = V2D_UNIT_VALUES, flag = 0;
232 /* clear and setup matrix */
233 UI_GetThemeColor3fv(TH_BACK, col);
234 glClearColor(col[0], col[1], col[2], 0.0);
235 glClear(GL_COLOR_BUFFER_BIT);
237 UI_view2d_view_ortho(v2d);
240 unitx = (sipo->flag & SIPO_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
241 grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
242 UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
244 ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
247 if (ANIM_animdata_get_context(C, &ac)) {
248 /* draw ghost curves */
249 graph_draw_ghost_curves(&ac, sipo, ar);
251 /* draw curves twice - unselected, then selected, so that the are fewer occlusion problems */
252 graph_draw_curves(&ac, sipo, ar, grid, 0);
253 graph_draw_curves(&ac, sipo, ar, grid, 1);
255 /* XXX the slow way to set tot rect... but for nice sliders needed (ton) */
256 get_graph_keyframe_extents(&ac, &v2d->tot.xmin, &v2d->tot.xmax, &v2d->tot.ymin, &v2d->tot.ymax, FALSE, TRUE);
257 /* extra offset so that these items are visible */
258 v2d->tot.xmin -= 10.0f;
259 v2d->tot.xmax += 10.0f;
262 /* only free grid after drawing data, as we need to use it to determine sampling rate */
263 UI_view2d_grid_free(grid);
265 /* horizontal component of value-cursor (value line before the current frame line) */
266 if ((sipo->flag & SIPO_NODRAWCURSOR) == 0) {
269 /* Draw a green line to indicate the cursor value */
270 vec[1] = sipo->cursorVal;
272 UI_ThemeColorShadeAlpha(TH_CFRAME, -10, -50);
276 glBegin(GL_LINE_STRIP);
277 vec[0] = v2d->cur.xmin;
280 vec[0] = v2d->cur.xmax;
282 glEnd(); // GL_LINE_STRIP
287 if (sipo->flag & SIPO_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
288 if ((sipo->flag & SIPO_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
289 ANIM_draw_cfra(C, v2d, flag);
292 UI_view2d_view_orthoSpecial(ar, v2d, 1);
293 draw_markers_time(C, 0);
296 UI_view2d_view_ortho(v2d);
297 ANIM_draw_previewrange(C, v2d, 0);
300 UI_view2d_view_ortho(v2d);
301 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
303 /* reset view matrix */
304 UI_view2d_view_restore(C);
307 // FIXME: args for scrollers depend on the type of data being shown...
308 scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
309 UI_view2d_scrollers_draw(C, v2d, scrollers);
310 UI_view2d_scrollers_free(scrollers);
313 static void graph_channel_area_init(wmWindowManager *wm, ARegion *ar)
317 /* make sure we keep the hide flags */
318 ar->v2d.scroll |= (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
319 ar->v2d.scroll &= ~(V2D_SCROLL_LEFT | V2D_SCROLL_TOP); /* prevent any noise of past */
320 ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
321 ar->v2d.scroll |= V2D_SCROLL_VERTICAL_HIDE;
323 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
326 keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
327 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
328 keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
329 WM_event_add_keymap_handler(&ar->handlers, keymap);
332 static void graph_channel_area_draw(const bContext *C, ARegion *ar)
335 View2D *v2d = &ar->v2d;
336 View2DScrollers *scrollers;
339 /* clear and setup matrix */
340 UI_GetThemeColor3fv(TH_BACK, col);
341 glClearColor(col[0], col[1], col[2], 0.0);
342 glClear(GL_COLOR_BUFFER_BIT);
344 UI_view2d_view_ortho(v2d);
347 if (ANIM_animdata_get_context(C, &ac)) {
348 graph_draw_channel_names((bContext *)C, &ac, ar);
351 /* reset view matrix */
352 UI_view2d_view_restore(C);
355 scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
356 UI_view2d_scrollers_draw(C, v2d, scrollers);
357 UI_view2d_scrollers_free(scrollers);
360 /* add handlers, stuff you only do once or on area/region changes */
361 static void graph_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
363 ED_region_header_init(ar);
366 static void graph_header_area_draw(const bContext *C, ARegion *ar)
368 ED_region_header(C, ar);
371 /* add handlers, stuff you only do once or on area/region changes */
372 static void graph_buttons_area_init(wmWindowManager *wm, ARegion *ar)
376 ED_region_panels_init(wm, ar);
378 keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
379 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
382 static void graph_buttons_area_draw(const bContext *C, ARegion *ar)
384 ED_region_panels(C, ar, 1, NULL, -1);
387 static void graph_region_listener(ARegion *ar, wmNotifier *wmn)
389 /* context changes */
390 switch (wmn->category) {
392 ED_region_tag_redraw(ar);
396 case ND_RENDER_OPTIONS:
400 ED_region_tag_redraw(ar);
403 if (wmn->action == NA_SELECTED)
404 ED_region_tag_redraw(ar);
413 ED_region_tag_redraw(ar);
416 if (wmn->action == NA_RENAME)
417 ED_region_tag_redraw(ar);
422 switch (wmn->action) {
425 ED_region_tag_redraw(ar);
430 if (wmn->action == NA_RENAME)
431 ED_region_tag_redraw(ar);
434 if (wmn->data == ND_KEYS)
435 ED_region_tag_redraw(ar);
440 /* editor level listener */
441 static void graph_listener(ScrArea *sa, wmNotifier *wmn)
443 SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
445 /* context changes */
446 switch (wmn->category) {
448 /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
449 if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED))
450 ED_area_tag_redraw(sa);
452 ED_area_tag_refresh(sa);
456 case ND_OB_ACTIVE: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
458 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
459 ED_area_tag_refresh(sa);
462 default: /* just redrawing the view will do */
463 ED_area_tag_redraw(sa);
469 case ND_BONE_SELECT: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
471 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
472 ED_area_tag_refresh(sa);
475 break; /*do nothing*/
477 default: /* just redrawing the view will do */
478 ED_area_tag_redraw(sa);
483 if (wmn->action == NA_SELECTED) {
484 /* selection changed, so force refresh to flush (needs flag set to do syncing) */
485 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
486 ED_area_tag_refresh(sa);
490 if (wmn->data == ND_SPACE_GRAPH)
491 ED_area_tag_redraw(sa);
494 if (sipo->flag & SIPO_TEMP_NEEDCHANSYNC) {
495 /* force redraw/refresh after undo/redo - prevents "black curve" problem */
496 ED_area_tag_refresh(sa);
500 // XXX: restore the case below if not enough updates occur...
502 // if (wmn->data == ND_KEYS)
503 // ED_area_tag_redraw(sa);
509 static void graph_refresh(const bContext *C, ScrArea *sa)
511 SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
514 /* updates to data needed depends on Graph Editor mode... */
515 switch (sipo->mode) {
516 case SIPO_MODE_ANIMATION: /* all animation */
522 case SIPO_MODE_DRIVERS: /* drivers only */
529 /* region updates? */
530 // XXX re-sizing y-extents of tot should go here?
532 /* update the state of the animchannels in response to changes from the data they represent
533 * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
535 if (sipo->flag & SIPO_TEMP_NEEDCHANSYNC) {
536 ANIM_sync_animchannels_to_data(C);
537 sipo->flag &= ~SIPO_TEMP_NEEDCHANSYNC;
538 ED_area_tag_redraw(sa);
541 /* init/adjust F-Curve colors */
542 if (ANIM_animdata_get_context(C, &ac)) {
543 ListBase anim_data = {NULL, NULL};
549 /* build list of F-Curves which will be visible as channels in channel-region
550 * - we don't include ANIMFILTER_CURVEVISIBLE filter, as that will result in a
551 * mismatch between channel-colors and the drawn curves
553 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_NODUPLIS);
554 items = ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
556 /* loop over F-Curves, assigning colors */
557 for (ale = anim_data.first, i = 0; ale; ale = ale->next, i++) {
558 FCurve *fcu = (FCurve *)ale->data;
560 /* set color of curve here */
561 switch (fcu->color_mode) {
562 case FCURVE_COLOR_CUSTOM:
563 /* User has defined a custom color for this curve already (we assume it's not going to cause clashes with text colors),
564 * which should be left alone... Nothing needs to be done here.
568 case FCURVE_COLOR_AUTO_RGB:
570 /* F-Curve's array index is automatically mapped to RGB values. This works best of 3-value vectors.
571 * TODO: find a way to module the hue so that not all curves have same color...
573 float *col = fcu->color;
575 switch (fcu->array_index) {
577 UI_GetThemeColor3fv(TH_AXIS_X, col);
580 UI_GetThemeColor3fv(TH_AXIS_Y, col);
583 UI_GetThemeColor3fv(TH_AXIS_Z, col);
586 /* 'unknown' color - bluish so as to not conflict with handles */
587 col[0] = 0.3f; col[1] = 0.8f; col[2] = 1.0f;
593 case FCURVE_COLOR_AUTO_RAINBOW:
596 /* determine color 'automatically' using 'magic function' which uses the given args
597 * of current item index + total items to determine some RGB color
599 getcolor_fcurve_rainbow(i, items, fcu->color);
606 BLI_freelistN(&anim_data);
610 /* only called once, from space/spacetypes.c */
611 void ED_spacetype_ipo(void)
613 SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype ipo");
616 st->spaceid = SPACE_IPO;
617 strncpy(st->name, "Graph", BKE_ST_MAXNAME);
620 st->free = graph_free;
621 st->init = graph_init;
622 st->duplicate = graph_duplicate;
623 st->operatortypes = graphedit_operatortypes;
624 st->keymap = graphedit_keymap;
625 st->listener = graph_listener;
626 st->refresh = graph_refresh;
628 /* regions: main window */
629 art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
630 art->regionid = RGN_TYPE_WINDOW;
631 art->init = graph_main_area_init;
632 art->draw = graph_main_area_draw;
633 art->listener = graph_region_listener;
634 art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
636 BLI_addhead(&st->regiontypes, art);
638 /* regions: header */
639 art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
640 art->regionid = RGN_TYPE_HEADER;
641 art->prefsizey = HEADERY;
642 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
643 art->listener = graph_region_listener;
644 art->init = graph_header_area_init;
645 art->draw = graph_header_area_draw;
647 BLI_addhead(&st->regiontypes, art);
649 /* regions: channels */
650 art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
651 art->regionid = RGN_TYPE_CHANNELS;
652 art->prefsizex = 200 + V2D_SCROLL_WIDTH; /* 200 is the 'standard', but due to scrollers, we want a bit more to fit the lock icons in */
653 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
654 art->listener = graph_region_listener;
655 art->init = graph_channel_area_init;
656 art->draw = graph_channel_area_draw;
658 BLI_addhead(&st->regiontypes, art);
660 /* regions: UI buttons */
661 art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
662 art->regionid = RGN_TYPE_UI;
663 art->prefsizex = 200;
664 art->keymapflag = ED_KEYMAP_UI;
665 art->listener = graph_region_listener;
666 art->init = graph_buttons_area_init;
667 art->draw = graph_buttons_area_draw;
669 BLI_addhead(&st->regiontypes, art);
671 graph_buttons_register(art);
673 BKE_spacetype_register(st);