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"
43 #include "BLI_utildefines.h"
45 #include "BKE_context.h"
46 #include "BKE_global.h"
48 #include "BKE_fcurve.h"
49 #include "BKE_screen.h"
51 #include "ED_space_api.h"
52 #include "ED_screen.h"
53 #include "ED_anim_api.h"
54 #include "ED_markers.h"
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
64 #include "graph_intern.h" // own include
66 /* ******************** manage regions ********************* */
68 ARegion *graph_has_buttons_region(ScrArea *sa)
72 ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
75 /* add subdiv level; after main */
76 ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
79 if (ar == NULL) return NULL;
81 arnew = MEM_callocN(sizeof(ARegion), "buttons for graph");
83 BLI_insertlinkafter(&sa->regionbase, ar, arnew);
84 arnew->regiontype = RGN_TYPE_UI;
85 arnew->alignment = RGN_ALIGN_RIGHT;
87 arnew->flag = RGN_FLAG_HIDDEN;
93 /* ******************** default callbacks for ipo space ***************** */
95 static SpaceLink *graph_new(const bContext *C)
97 Scene *scene = CTX_data_scene(C);
101 /* Graph Editor - general stuff */
102 sipo = MEM_callocN(sizeof(SpaceIpo), "init graphedit");
103 sipo->spacetype = SPACE_IPO;
105 sipo->autosnap = SACTSNAP_FRAME;
107 /* allocate DopeSheet data for Graph Editor */
108 sipo->ads = MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
109 sipo->ads->source = (ID *)scene;
111 /* settings for making it easier by default to just see what you're interested in tweaking */
112 sipo->ads->filterflag |= ADS_FILTER_ONLYSEL;
113 sipo->flag |= SIPO_SELVHANDLESONLY;
116 ar = MEM_callocN(sizeof(ARegion), "header for graphedit");
118 BLI_addtail(&sipo->regionbase, ar);
119 ar->regiontype = RGN_TYPE_HEADER;
120 ar->alignment = RGN_ALIGN_BOTTOM;
123 ar = MEM_callocN(sizeof(ARegion), "channels area for graphedit");
125 BLI_addtail(&sipo->regionbase, ar);
126 ar->regiontype = RGN_TYPE_CHANNELS;
127 ar->alignment = RGN_ALIGN_LEFT;
129 ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
132 ar = MEM_callocN(sizeof(ARegion), "buttons area for graphedit");
134 BLI_addtail(&sipo->regionbase, ar);
135 ar->regiontype = RGN_TYPE_UI;
136 ar->alignment = RGN_ALIGN_RIGHT;
137 ar->flag = RGN_FLAG_HIDDEN;
140 ar = MEM_callocN(sizeof(ARegion), "main area for graphedit");
142 BLI_addtail(&sipo->regionbase, ar);
143 ar->regiontype = RGN_TYPE_WINDOW;
145 ar->v2d.tot.xmin = 0.0f;
146 ar->v2d.tot.ymin = (float)scene->r.sfra - 10.0f;
147 ar->v2d.tot.xmax = (float)scene->r.efra;
148 ar->v2d.tot.ymax = 10.0f;
150 ar->v2d.cur = ar->v2d.tot;
152 ar->v2d.min[0] = FLT_MIN;
153 ar->v2d.min[1] = FLT_MIN;
155 ar->v2d.max[0] = MAXFRAMEF;
156 ar->v2d.max[1] = FLT_MAX;
158 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
159 ar->v2d.scroll |= (V2D_SCROLL_LEFT | V2D_SCROLL_SCALE_VERTICAL);
163 return (SpaceLink *)sipo;
166 /* not spacelink itself */
167 static void graph_free(SpaceLink *sl)
169 SpaceIpo *si = (SpaceIpo *)sl;
172 BLI_freelistN(&si->ads->chanbase);
176 if (si->ghostCurves.first)
177 free_fcurves(&si->ghostCurves);
181 /* spacetype; init callback */
182 static void graph_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
184 SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
186 /* init dopesheet data if non-existant (i.e. for old files) */
187 if (sipo->ads == NULL) {
188 sipo->ads = MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
189 sipo->ads->source = (ID *)(G.main->scene.first); // FIXME: this is a really nasty hack here for now...
192 /* force immediate init of any invalid F-Curve colors */
193 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
194 ED_area_tag_refresh(sa);
197 static SpaceLink *graph_duplicate(SpaceLink *sl)
199 SpaceIpo *sipon = MEM_dupallocN(sl);
201 /* clear or remove stuff from old */
202 BLI_duplicatelist(&sipon->ghostCurves, &((SpaceIpo *)sl)->ghostCurves);
203 sipon->ads = MEM_dupallocN(sipon->ads);
205 return (SpaceLink *)sipon;
208 /* add handlers, stuff you only do once or on area/region changes */
209 static void graph_main_area_init(wmWindowManager *wm, ARegion *ar)
213 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
216 keymap = WM_keymap_find(wm->defaultconf, "Graph Editor", SPACE_IPO, 0);
217 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
218 keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
219 WM_event_add_keymap_handler(&ar->handlers, keymap);
222 static void graph_main_area_draw(const bContext *C, ARegion *ar)
224 /* draw entirely, view changes should be handled here */
225 SpaceIpo *sipo = CTX_wm_space_graph(C);
227 View2D *v2d = &ar->v2d;
229 View2DScrollers *scrollers;
231 short unitx = 0, unity = V2D_UNIT_VALUES, flag = 0;
233 /* clear and setup matrix */
234 UI_GetThemeColor3fv(TH_BACK, col);
235 glClearColor(col[0], col[1], col[2], 0.0);
236 glClear(GL_COLOR_BUFFER_BIT);
238 UI_view2d_view_ortho(v2d);
241 unitx = (sipo->flag & SIPO_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
242 grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
243 UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
246 if (ANIM_animdata_get_context(C, &ac)) {
247 /* draw ghost curves */
248 graph_draw_ghost_curves(&ac, sipo, ar);
250 /* draw curves twice - unselected, then selected, so that the are fewer occlusion problems */
251 graph_draw_curves(&ac, sipo, ar, grid, 0);
252 graph_draw_curves(&ac, sipo, ar, grid, 1);
254 /* XXX the slow way to set tot rect... but for nice sliders needed (ton) */
255 get_graph_keyframe_extents(&ac, &v2d->tot.xmin, &v2d->tot.xmax, &v2d->tot.ymin, &v2d->tot.ymax, FALSE, TRUE);
256 /* extra offset so that these items are visible */
257 v2d->tot.xmin -= 10.0f;
258 v2d->tot.xmax += 10.0f;
261 /* only free grid after drawing data, as we need to use it to determine sampling rate */
262 UI_view2d_grid_free(grid);
264 /* horizontal component of value-cursor (value line before the current frame line) */
265 if ((sipo->flag & SIPO_NODRAWCURSOR) == 0) {
268 /* Draw a green line to indicate the cursor value */
269 vec[1] = sipo->cursorVal;
271 UI_ThemeColorShadeAlpha(TH_CFRAME, -10, -50);
275 glBegin(GL_LINE_STRIP);
276 vec[0] = v2d->cur.xmin;
279 vec[0] = v2d->cur.xmax;
281 glEnd(); // GL_LINE_STRIP
286 if (sipo->flag & SIPO_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
287 if ((sipo->flag & SIPO_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
288 ANIM_draw_cfra(C, v2d, flag);
291 UI_view2d_view_orthoSpecial(ar, v2d, 1);
292 draw_markers_time(C, 0);
295 UI_view2d_view_ortho(v2d);
296 ANIM_draw_previewrange(C, v2d);
298 /* reset view matrix */
299 UI_view2d_view_restore(C);
302 // FIXME: args for scrollers depend on the type of data being shown...
303 scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
304 UI_view2d_scrollers_draw(C, v2d, scrollers);
305 UI_view2d_scrollers_free(scrollers);
308 static void graph_channel_area_init(wmWindowManager *wm, ARegion *ar)
312 /* make sure we keep the hide flags */
313 ar->v2d.scroll |= (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
314 ar->v2d.scroll &= ~(V2D_SCROLL_LEFT | V2D_SCROLL_TOP); /* prevent any noise of past */
315 ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
316 ar->v2d.scroll |= V2D_SCROLL_VERTICAL_HIDE;
318 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
321 keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
322 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
323 keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
324 WM_event_add_keymap_handler(&ar->handlers, keymap);
327 static void graph_channel_area_draw(const bContext *C, ARegion *ar)
330 View2D *v2d = &ar->v2d;
331 View2DScrollers *scrollers;
334 /* clear and setup matrix */
335 UI_GetThemeColor3fv(TH_BACK, col);
336 glClearColor(col[0], col[1], col[2], 0.0);
337 glClear(GL_COLOR_BUFFER_BIT);
339 UI_view2d_view_ortho(v2d);
342 if (ANIM_animdata_get_context(C, &ac)) {
343 graph_draw_channel_names((bContext *)C, &ac, ar);
346 /* reset view matrix */
347 UI_view2d_view_restore(C);
350 scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
351 UI_view2d_scrollers_draw(C, v2d, scrollers);
352 UI_view2d_scrollers_free(scrollers);
355 /* add handlers, stuff you only do once or on area/region changes */
356 static void graph_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
358 ED_region_header_init(ar);
361 static void graph_header_area_draw(const bContext *C, ARegion *ar)
363 ED_region_header(C, ar);
366 /* add handlers, stuff you only do once or on area/region changes */
367 static void graph_buttons_area_init(wmWindowManager *wm, ARegion *ar)
371 ED_region_panels_init(wm, ar);
373 keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
374 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
377 static void graph_buttons_area_draw(const bContext *C, ARegion *ar)
379 ED_region_panels(C, ar, 1, NULL, -1);
382 static void graph_region_listener(ARegion *ar, wmNotifier *wmn)
384 /* context changes */
385 switch (wmn->category) {
387 ED_region_tag_redraw(ar);
391 case ND_RENDER_OPTIONS:
395 ED_region_tag_redraw(ar);
398 if (wmn->action == NA_SELECTED)
399 ED_region_tag_redraw(ar);
408 ED_region_tag_redraw(ar);
411 if (wmn->action == NA_RENAME)
412 ED_region_tag_redraw(ar);
417 switch (wmn->action) {
420 ED_region_tag_redraw(ar);
425 if (wmn->action == NA_RENAME)
426 ED_region_tag_redraw(ar);
429 if (wmn->data == ND_KEYS)
430 ED_region_tag_redraw(ar);
435 /* editor level listener */
436 static void graph_listener(ScrArea *sa, wmNotifier *wmn)
438 SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
440 /* context changes */
441 switch (wmn->category) {
443 /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
444 if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED))
445 ED_area_tag_redraw(sa);
447 ED_area_tag_refresh(sa);
451 case ND_OB_ACTIVE: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
453 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
454 ED_area_tag_refresh(sa);
457 default: /* just redrawing the view will do */
458 ED_area_tag_redraw(sa);
464 case ND_BONE_SELECT: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
466 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
467 ED_area_tag_refresh(sa);
470 break; /*do nothing*/
472 default: /* just redrawing the view will do */
473 ED_area_tag_redraw(sa);
478 if (wmn->action == NA_SELECTED) {
479 /* selection changed, so force refresh to flush (needs flag set to do syncing) */
480 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
481 ED_area_tag_refresh(sa);
485 if (wmn->data == ND_SPACE_GRAPH)
486 ED_area_tag_redraw(sa);
489 if (sipo->flag & SIPO_TEMP_NEEDCHANSYNC) {
490 /* force redraw/refresh after undo/redo - prevents "black curve" problem */
491 ED_area_tag_refresh(sa);
495 // XXX: restore the case below if not enough updates occur...
497 // if (wmn->data == ND_KEYS)
498 // ED_area_tag_redraw(sa);
504 static void graph_refresh(const bContext *C, ScrArea *sa)
506 SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
509 /* updates to data needed depends on Graph Editor mode... */
510 switch (sipo->mode) {
511 case SIPO_MODE_ANIMATION: /* all animation */
517 case SIPO_MODE_DRIVERS: /* drivers only */
524 /* region updates? */
525 // XXX re-sizing y-extents of tot should go here?
527 /* update the state of the animchannels in response to changes from the data they represent
528 * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
530 if (sipo->flag & SIPO_TEMP_NEEDCHANSYNC) {
531 ANIM_sync_animchannels_to_data(C);
532 sipo->flag &= ~SIPO_TEMP_NEEDCHANSYNC;
533 ED_area_tag_redraw(sa);
536 /* init/adjust F-Curve colors */
537 if (ANIM_animdata_get_context(C, &ac)) {
538 ListBase anim_data = {NULL, NULL};
544 /* build list of F-Curves which will be visible as channels in channel-region
545 * - we don't include ANIMFILTER_CURVEVISIBLE filter, as that will result in a
546 * mismatch between channel-colors and the drawn curves
548 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_NODUPLIS);
549 items = ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
551 /* loop over F-Curves, assigning colors */
552 for (ale = anim_data.first, i = 0; ale; ale = ale->next, i++) {
553 FCurve *fcu = (FCurve *)ale->data;
555 /* set color of curve here */
556 switch (fcu->color_mode) {
557 case FCURVE_COLOR_CUSTOM:
558 /* User has defined a custom color for this curve already (we assume it's not going to cause clashes with text colors),
559 * which should be left alone... Nothing needs to be done here.
563 case FCURVE_COLOR_AUTO_RGB:
565 /* F-Curve's array index is automatically mapped to RGB values. This works best of 3-value vectors.
566 * TODO: find a way to module the hue so that not all curves have same color...
568 float *col = fcu->color;
570 switch (fcu->array_index) {
572 UI_GetThemeColor3fv(TH_AXIS_X, col);
575 UI_GetThemeColor3fv(TH_AXIS_Y, col);
578 UI_GetThemeColor3fv(TH_AXIS_Z, col);
581 /* 'unknown' color - bluish so as to not conflict with handles */
582 col[0] = 0.3f; col[1] = 0.8f; col[2] = 1.0f;
588 case FCURVE_COLOR_AUTO_RAINBOW:
591 /* determine color 'automatically' using 'magic function' which uses the given args
592 * of current item index + total items to determine some RGB color
594 getcolor_fcurve_rainbow(i, items, fcu->color);
601 BLI_freelistN(&anim_data);
605 /* only called once, from space/spacetypes.c */
606 void ED_spacetype_ipo(void)
608 SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype ipo");
611 st->spaceid = SPACE_IPO;
612 strncpy(st->name, "Graph", BKE_ST_MAXNAME);
615 st->free = graph_free;
616 st->init = graph_init;
617 st->duplicate = graph_duplicate;
618 st->operatortypes = graphedit_operatortypes;
619 st->keymap = graphedit_keymap;
620 st->listener = graph_listener;
621 st->refresh = graph_refresh;
623 /* regions: main window */
624 art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
625 art->regionid = RGN_TYPE_WINDOW;
626 art->init = graph_main_area_init;
627 art->draw = graph_main_area_draw;
628 art->listener = graph_region_listener;
629 art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
631 BLI_addhead(&st->regiontypes, art);
633 /* regions: header */
634 art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
635 art->regionid = RGN_TYPE_HEADER;
636 art->prefsizey = HEADERY;
637 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
638 art->listener = graph_region_listener;
639 art->init = graph_header_area_init;
640 art->draw = graph_header_area_draw;
642 BLI_addhead(&st->regiontypes, art);
644 /* regions: channels */
645 art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
646 art->regionid = RGN_TYPE_CHANNELS;
647 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 */
648 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
649 art->listener = graph_region_listener;
650 art->init = graph_channel_area_init;
651 art->draw = graph_channel_area_draw;
653 BLI_addhead(&st->regiontypes, art);
655 /* regions: UI buttons */
656 art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
657 art->regionid = RGN_TYPE_UI;
658 art->prefsizex = 200;
659 art->keymapflag = ED_KEYMAP_UI;
660 art->listener = graph_region_listener;
661 art->init = graph_buttons_area_init;
662 art->draw = graph_buttons_area_draw;
664 BLI_addhead(&st->regiontypes, art);
666 graph_buttons_register(art);
668 BKE_spacetype_register(st);