Fix crash in id remapping of Graph editor.
[blender.git] / source / blender / editors / space_graph / space_graph.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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. 
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_graph/space_graph.c
28  *  \ingroup spgraph
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_anim_types.h"
36 #include "DNA_group_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_main.h"
48 #include "BKE_fcurve.h"
49 #include "BKE_screen.h"
50
51 #include "ED_space_api.h"
52 #include "ED_screen.h"
53 #include "ED_anim_api.h"
54 #include "ED_markers.h"
55
56 #include "BIF_gl.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
63
64 #include "graph_intern.h"   // own include
65
66 /* ******************** manage regions ********************* */
67
68 ARegion *graph_has_buttons_region(ScrArea *sa)
69 {
70         ARegion *ar, *arnew;
71         
72         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
73         if (ar) return ar;
74
75         /* add subdiv level; after main */
76         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
77
78         /* is error! */
79         if (ar == NULL) return NULL;
80         
81         arnew = MEM_callocN(sizeof(ARegion), "buttons for graph");
82         
83         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
84         arnew->regiontype = RGN_TYPE_UI;
85         arnew->alignment = RGN_ALIGN_RIGHT;
86         
87         arnew->flag = RGN_FLAG_HIDDEN;
88         
89         return arnew;
90 }
91
92
93 /* ******************** default callbacks for ipo space ***************** */
94
95 static SpaceLink *graph_new(const bContext *C)
96 {
97         Scene *scene = CTX_data_scene(C);
98         ARegion *ar;
99         SpaceIpo *sipo;
100         
101         /* Graph Editor - general stuff */
102         sipo = MEM_callocN(sizeof(SpaceIpo), "init graphedit");
103         sipo->spacetype = SPACE_IPO;
104         
105         sipo->autosnap = SACTSNAP_FRAME;
106         
107         /* allocate DopeSheet data for Graph Editor */
108         sipo->ads = MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
109         sipo->ads->source = (ID *)scene;
110         
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;
114         
115         /* header */
116         ar = MEM_callocN(sizeof(ARegion), "header for graphedit");
117         
118         BLI_addtail(&sipo->regionbase, ar);
119         ar->regiontype = RGN_TYPE_HEADER;
120         ar->alignment = RGN_ALIGN_BOTTOM;
121         
122         /* channels */
123         ar = MEM_callocN(sizeof(ARegion), "channels region for graphedit");
124         
125         BLI_addtail(&sipo->regionbase, ar);
126         ar->regiontype = RGN_TYPE_CHANNELS;
127         ar->alignment = RGN_ALIGN_LEFT;
128         
129         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
130         
131         /* ui buttons */
132         ar = MEM_callocN(sizeof(ARegion), "buttons region for graphedit");
133         
134         BLI_addtail(&sipo->regionbase, ar);
135         ar->regiontype = RGN_TYPE_UI;
136         ar->alignment = RGN_ALIGN_RIGHT;
137         ar->flag = RGN_FLAG_HIDDEN;
138         
139         /* main region */
140         ar = MEM_callocN(sizeof(ARegion), "main region for graphedit");
141         
142         BLI_addtail(&sipo->regionbase, ar);
143         ar->regiontype = RGN_TYPE_WINDOW;
144         
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;
149         
150         ar->v2d.cur = ar->v2d.tot;
151         
152         ar->v2d.min[0] = FLT_MIN;
153         ar->v2d.min[1] = FLT_MIN;
154
155         ar->v2d.max[0] = MAXFRAMEF;
156         ar->v2d.max[1] = FLT_MAX;
157         
158         ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
159         ar->v2d.scroll |= (V2D_SCROLL_LEFT | V2D_SCROLL_SCALE_VERTICAL);
160         
161         ar->v2d.keeptot = 0;
162         
163         return (SpaceLink *)sipo;
164 }
165
166 /* not spacelink itself */
167 static void graph_free(SpaceLink *sl)
168 {       
169         SpaceIpo *si = (SpaceIpo *)sl;
170         
171         if (si->ads) {
172                 BLI_freelistN(&si->ads->chanbase);
173                 MEM_freeN(si->ads);
174         }
175         
176         if (si->ghostCurves.first)
177                 free_fcurves(&si->ghostCurves);
178 }
179
180
181 /* spacetype; init callback */
182 static void graph_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
183 {
184         SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
185         
186         /* init dopesheet data if non-existent (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...
190         }
191         
192         /* force immediate init of any invalid F-Curve colors */
193         /* XXX: but, don't do SIPO_TEMP_NEEDCHANSYNC (i.e. channel select state sync)
194          * as this is run on each region resize; setting this here will cause selection
195          * state to be lost on area/region resizing. [#35744]
196          */
197         ED_area_tag_refresh(sa);
198 }
199
200 static SpaceLink *graph_duplicate(SpaceLink *sl)
201 {
202         SpaceIpo *sipon = MEM_dupallocN(sl);
203         
204         /* clear or remove stuff from old */
205         BLI_duplicatelist(&sipon->ghostCurves, &((SpaceIpo *)sl)->ghostCurves);
206         sipon->ads = MEM_dupallocN(sipon->ads);
207         
208         return (SpaceLink *)sipon;
209 }
210
211 /* add handlers, stuff you only do once or on area/region changes */
212 static void graph_main_region_init(wmWindowManager *wm, ARegion *ar)
213 {
214         wmKeyMap *keymap;
215         
216         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
217         
218         /* own keymap */
219         keymap = WM_keymap_find(wm->defaultconf, "Graph Editor", SPACE_IPO, 0);
220         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
221         keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
222         WM_event_add_keymap_handler(&ar->handlers, keymap);
223 }
224
225 static void graph_main_region_draw(const bContext *C, ARegion *ar)
226 {
227         /* draw entirely, view changes should be handled here */
228         SpaceIpo *sipo = CTX_wm_space_graph(C);
229         bAnimContext ac;
230         View2D *v2d = &ar->v2d;
231         View2DGrid *grid;
232         View2DScrollers *scrollers;
233         float col[3];
234         short unitx = 0, unity = V2D_UNIT_VALUES, flag = 0;
235         
236         /* clear and setup matrix */
237         UI_GetThemeColor3fv(TH_BACK, col);
238         glClearColor(col[0], col[1], col[2], 0.0);
239         glClear(GL_COLOR_BUFFER_BIT);
240         
241         UI_view2d_view_ortho(v2d);
242         
243         /* grid */
244         unitx = ((sipo->mode == SIPO_MODE_ANIMATION) && (sipo->flag & SIPO_DRAWTIME)) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
245         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
246         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
247         
248         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
249
250         /* draw data */
251         if (ANIM_animdata_get_context(C, &ac)) {
252                 /* draw ghost curves */
253                 graph_draw_ghost_curves(&ac, sipo, ar);
254                 
255                 /* draw curves twice - unselected, then selected, so that the are fewer occlusion problems */
256                 graph_draw_curves(&ac, sipo, ar, grid, 0);
257                 graph_draw_curves(&ac, sipo, ar, grid, 1);
258                 
259                 /* XXX the slow way to set tot rect... but for nice sliders needed (ton) */
260                 get_graph_keyframe_extents(&ac, &v2d->tot.xmin, &v2d->tot.xmax, &v2d->tot.ymin, &v2d->tot.ymax, false, true);
261                 /* extra offset so that these items are visible */
262                 v2d->tot.xmin -= 10.0f;
263                 v2d->tot.xmax += 10.0f;
264         }
265         
266         /* only free grid after drawing data, as we need to use it to determine sampling rate */
267         UI_view2d_grid_free(grid);
268         
269         /* horizontal component of value-cursor (value line before the current frame line) */
270         if ((sipo->flag & SIPO_NODRAWCURSOR) == 0) {
271
272                 float y = sipo->cursorVal;
273                 
274                 /* Draw a green line to indicate the cursor value */
275                 UI_ThemeColorShadeAlpha(TH_CFRAME, -10, -50);
276                 glEnable(GL_BLEND);
277                 glLineWidth(2.0);
278
279                 glBegin(GL_LINES);
280                 glVertex2f(v2d->cur.xmin, y);
281                 glVertex2f(v2d->cur.xmax, y);
282                 glEnd();
283
284                 glDisable(GL_BLEND);
285         }
286         
287         /* current frame or vertical component of vertical component of the cursor */
288         if (sipo->mode == SIPO_MODE_DRIVERS) {
289                 /* cursor x-value */
290                 float x = sipo->cursorTime;
291                 
292                 /* to help differentiate this from the current frame, draw slightly darker like the horizontal one */
293                 UI_ThemeColorShadeAlpha(TH_CFRAME, -40, -50);
294                 glEnable(GL_BLEND);
295                 glLineWidth(2.0);
296                 
297                 glBegin(GL_LINES);
298                 glVertex2f(x, v2d->cur.ymin);
299                 glVertex2f(x, v2d->cur.ymax);
300                 glEnd();
301
302                 glDisable(GL_BLEND);
303         }
304         else {
305                 /* current frame */
306                 if (sipo->flag & SIPO_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
307                 if ((sipo->flag & SIPO_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
308                 ANIM_draw_cfra(C, v2d, flag);
309         }
310         
311         /* markers */
312         UI_view2d_view_orthoSpecial(ar, v2d, 1);
313         ED_markers_draw(C, DRAW_MARKERS_MARGIN);
314         
315         /* preview range */
316         UI_view2d_view_ortho(v2d);
317         ANIM_draw_previewrange(C, v2d, 0);
318         
319         /* callback */
320         UI_view2d_view_ortho(v2d);
321         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
322
323         /* reset view matrix */
324         UI_view2d_view_restore(C);
325         
326         /* scrollers */
327         // FIXME: args for scrollers depend on the type of data being shown...
328         scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
329         UI_view2d_scrollers_draw(C, v2d, scrollers);
330         UI_view2d_scrollers_free(scrollers);
331 }
332
333 static void graph_channel_region_init(wmWindowManager *wm, ARegion *ar)
334 {
335         wmKeyMap *keymap;
336         
337         /* make sure we keep the hide flags */
338         ar->v2d.scroll |= (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
339         ar->v2d.scroll &= ~(V2D_SCROLL_LEFT | V2D_SCROLL_TOP);  /* prevent any noise of past */
340         ar->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
341         ar->v2d.scroll |= V2D_SCROLL_VERTICAL_HIDE;
342         
343         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
344         
345         /* own keymap */
346         keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
347         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
348         keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
349         WM_event_add_keymap_handler(&ar->handlers, keymap);
350 }
351
352 static void graph_channel_region_draw(const bContext *C, ARegion *ar)
353 {
354         bAnimContext ac;
355         View2D *v2d = &ar->v2d;
356         View2DScrollers *scrollers;
357         float col[3];
358         
359         /* clear and setup matrix */
360         UI_GetThemeColor3fv(TH_BACK, col);
361         glClearColor(col[0], col[1], col[2], 0.0);
362         glClear(GL_COLOR_BUFFER_BIT);
363         
364         UI_view2d_view_ortho(v2d);
365         
366         /* draw channels */
367         if (ANIM_animdata_get_context(C, &ac)) {
368                 graph_draw_channel_names((bContext *)C, &ac, ar);
369         }
370         
371         /* reset view matrix */
372         UI_view2d_view_restore(C);
373         
374         /* scrollers */
375         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
376         UI_view2d_scrollers_draw(C, v2d, scrollers);
377         UI_view2d_scrollers_free(scrollers);
378 }
379
380 /* add handlers, stuff you only do once or on area/region changes */
381 static void graph_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
382 {
383         ED_region_header_init(ar);
384 }
385
386 static void graph_header_region_draw(const bContext *C, ARegion *ar)
387 {
388         ED_region_header(C, ar);
389 }
390
391 /* add handlers, stuff you only do once or on area/region changes */
392 static void graph_buttons_region_init(wmWindowManager *wm, ARegion *ar)
393 {
394         wmKeyMap *keymap;
395         
396         ED_region_panels_init(wm, ar);
397
398         keymap = WM_keymap_find(wm->defaultconf, "Graph Editor Generic", SPACE_IPO, 0);
399         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
400 }
401
402 static void graph_buttons_region_draw(const bContext *C, ARegion *ar)
403 {
404         ED_region_panels(C, ar, NULL, -1, true);
405 }
406
407 static void graph_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
408 {
409         /* context changes */
410         switch (wmn->category) {
411                 case NC_ANIMATION:
412                         ED_region_tag_redraw(ar);
413                         break;
414                 case NC_SCENE:
415                         switch (wmn->data) {
416                                 case ND_RENDER_OPTIONS:
417                                 case ND_OB_ACTIVE:
418                                 case ND_FRAME:
419                                 case ND_MARKERS:
420                                         ED_region_tag_redraw(ar);
421                                         break;
422                                 case ND_SEQUENCER:
423                                         if (wmn->action == NA_SELECTED)
424                                                 ED_region_tag_redraw(ar);
425                                         break;
426                         }
427                         break;
428                 case NC_OBJECT:
429                         switch (wmn->data) {
430                                 case ND_BONE_ACTIVE:
431                                 case ND_BONE_SELECT:
432                                 case ND_KEYS:
433                                         ED_region_tag_redraw(ar);
434                                         break;
435                                 case ND_MODIFIER:
436                                         if (wmn->action == NA_RENAME)
437                                                 ED_region_tag_redraw(ar);
438                                         break;
439                         }
440                         break;
441                 case NC_NODE:
442                         switch (wmn->action) {
443                                 case NA_EDITED:
444                                 case NA_SELECTED:
445                                         ED_region_tag_redraw(ar);
446                                         break;
447                         }
448                         break;
449                 case NC_ID:
450                         if (wmn->action == NA_RENAME)
451                                 ED_region_tag_redraw(ar);
452                         break;
453                 default:
454                         if (wmn->data == ND_KEYS)
455                                 ED_region_tag_redraw(ar);
456                         break;
457                                 
458         }
459 }
460
461 /* editor level listener */
462 static void graph_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn)
463 {
464         SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
465         
466         /* context changes */
467         switch (wmn->category) {
468                 case NC_ANIMATION:
469                         /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
470                         if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED))
471                                 ED_area_tag_redraw(sa);
472                         else
473                                 ED_area_tag_refresh(sa);
474                         break;
475                 case NC_SCENE:
476                         switch (wmn->data) {
477                                 case ND_OB_ACTIVE:  /* selection changed, so force refresh to flush (needs flag set to do syncing)  */
478                                 case ND_OB_SELECT:
479                                         sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
480                                         ED_area_tag_refresh(sa);
481                                         break;
482                                         
483                                 default: /* just redrawing the view will do */
484                                         ED_area_tag_redraw(sa);
485                                         break;
486                         }
487                         break;
488                 case NC_OBJECT:
489                         switch (wmn->data) {
490                                 case ND_BONE_SELECT:    /* selection changed, so force refresh to flush (needs flag set to do syncing) */
491                                 case ND_BONE_ACTIVE:
492                                         sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
493                                         ED_area_tag_refresh(sa);
494                                         break;
495                                 case ND_TRANSFORM:
496                                         break; /*do nothing*/
497                                         
498                                 default: /* just redrawing the view will do */
499                                         ED_area_tag_redraw(sa);
500                                         break;
501                         }
502                         break;
503                 case NC_NODE:
504                         if (wmn->action == NA_SELECTED) {
505                                 /* selection changed, so force refresh to flush (needs flag set to do syncing) */
506                                 sipo->flag |= SIPO_TEMP_NEEDCHANSYNC;
507                                 ED_area_tag_refresh(sa);
508                         }
509                         break;
510                 case NC_SPACE:
511                         if (wmn->data == ND_SPACE_GRAPH)
512                                 ED_area_tag_redraw(sa);
513                         break;
514                 case NC_WINDOW:
515                         if (sipo->flag & SIPO_TEMP_NEEDCHANSYNC) {
516                                 /* force redraw/refresh after undo/redo - prevents "black curve" problem */
517                                 ED_area_tag_refresh(sa);
518                         }
519                         break;
520                         
521                         // XXX: restore the case below if not enough updates occur...
522                         //default:
523                         //      if (wmn->data == ND_KEYS)
524                         //              ED_area_tag_redraw(sa);
525         }
526 }
527
528 /* Update F-Curve colors */
529 static void graph_refresh_fcurve_colors(const bContext *C)
530 {
531         bAnimContext ac;
532         
533         ListBase anim_data = {NULL, NULL};
534         bAnimListElem *ale;
535         size_t items;
536         int filter;
537         int i;
538         
539         if (ANIM_animdata_get_context(C, &ac) == false)
540                 return;
541         
542         UI_SetTheme(SPACE_IPO, RGN_TYPE_WINDOW);
543         
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
547          */
548         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_NODUPLIS);
549         items = ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
550         
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;
554                 
555                 /* set color of curve here */
556                 switch (fcu->color_mode) {
557                         case FCURVE_COLOR_CUSTOM:
558                         {
559                                 /* User has defined a custom color for this curve already (we assume it's not going to cause clashes with text colors),
560                                  * which should be left alone... Nothing needs to be done here.
561                                  */
562                                 break;
563                         }
564                         case FCURVE_COLOR_AUTO_RGB:
565                         {
566                                 /* F-Curve's array index is automatically mapped to RGB values. This works best of 3-value vectors. 
567                                  * TODO: find a way to module the hue so that not all curves have same color...
568                                  */
569                                 float *col = fcu->color;
570                                 
571                                 switch (fcu->array_index) {
572                                         case 0:
573                                                 UI_GetThemeColor3fv(TH_AXIS_X, col);
574                                                 break;
575                                         case 1:
576                                                 UI_GetThemeColor3fv(TH_AXIS_Y, col);
577                                                 break;
578                                         case 2:
579                                                 UI_GetThemeColor3fv(TH_AXIS_Z, col);
580                                                 break;
581                                         default:
582                                                 /* 'unknown' color - bluish so as to not conflict with handles */
583                                                 col[0] = 0.3f; col[1] = 0.8f; col[2] = 1.0f;
584                                                 break;
585                                 }
586                                 break;
587                         }
588                         case FCURVE_COLOR_AUTO_YRGB:
589                         {
590                                 /* Like FCURVE_COLOR_AUTO_RGB, except this is for quaternions... */
591                                 float *col = fcu->color;
592                                 
593                                 switch (fcu->array_index) {
594                                         case 1:
595                                                 UI_GetThemeColor3fv(TH_AXIS_X, col);
596                                                 break;
597                                         case 2:
598                                                 UI_GetThemeColor3fv(TH_AXIS_Y, col);
599                                                 break;
600                                         case 3:
601                                                 UI_GetThemeColor3fv(TH_AXIS_Z, col);
602                                                 break;
603                                         
604                                         case 0:
605                                         {
606                                                 /* Special Case: "W" channel should be yellowish, so blend X and Y channel colors... */
607                                                 float c1[3], c2[3];
608                                                 float h1[3], h2[3];
609                                                 float hresult[3];
610                                                 
611                                                 /* - get colors (rgb) */
612                                                 UI_GetThemeColor3fv(TH_AXIS_X, c1);
613                                                 UI_GetThemeColor3fv(TH_AXIS_Y, c2);
614                                                 
615                                                 /* - perform blending in HSV space (to keep brightness similar) */
616                                                 rgb_to_hsv_v(c1, h1);
617                                                 rgb_to_hsv_v(c2, h2);
618                                                 
619                                                 interp_v3_v3v3(hresult, h1, h2, 0.5f);
620                                                 
621                                                 /* - convert back to RGB for display */
622                                                 hsv_to_rgb_v(hresult, col);
623                                                 break;
624                                         }
625                                         
626                                         default:
627                                                 /* 'unknown' color - bluish so as to not conflict with handles */
628                                                 col[0] = 0.3f; col[1] = 0.8f; col[2] = 1.0f;
629                                                 break;
630                                 }
631                                 break;
632                         }
633                         case FCURVE_COLOR_AUTO_RAINBOW:
634                         default:
635                         {
636                                 /* determine color 'automatically' using 'magic function' which uses the given args
637                                  * of current item index + total items to determine some RGB color
638                                  */
639                                 getcolor_fcurve_rainbow(i, items, fcu->color);
640                                 break;
641                         }
642                 }
643         }
644         
645         /* free temp list */
646         ANIM_animdata_freelist(&anim_data);
647 }
648
649 static void graph_refresh(const bContext *C, ScrArea *sa)
650 {
651         SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
652         
653         /* updates to data needed depends on Graph Editor mode... */
654         switch (sipo->mode) {
655                 case SIPO_MODE_ANIMATION: /* all animation */
656                 {
657                         break;
658                 }
659                 
660                 case SIPO_MODE_DRIVERS: /* drivers only  */
661                 {
662                         break;
663                 }
664         }
665         
666         /* region updates? */
667         // XXX re-sizing y-extents of tot should go here?
668         
669         /* update the state of the animchannels in response to changes from the data they represent 
670          * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
671          */
672         if (sipo->flag & SIPO_TEMP_NEEDCHANSYNC) {
673                 ANIM_sync_animchannels_to_data(C);
674                 sipo->flag &= ~SIPO_TEMP_NEEDCHANSYNC;
675                 ED_area_tag_redraw(sa);
676         }
677         
678         /* init/adjust F-Curve colors */
679         graph_refresh_fcurve_colors(C);
680 }
681
682 static void graph_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
683 {
684         SpaceIpo *sgraph = (SpaceIpo *)slink;
685
686         if (!ELEM(GS(old_id->name), ID_GR)) {
687                 return;
688         }
689
690         if (sgraph->ads && (ID *)sgraph->ads->filter_grp == old_id) {
691                 sgraph->ads->filter_grp = (Group *)new_id;
692         }
693 }
694
695 /* only called once, from space/spacetypes.c */
696 void ED_spacetype_ipo(void)
697 {
698         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype ipo");
699         ARegionType *art;
700         
701         st->spaceid = SPACE_IPO;
702         strncpy(st->name, "Graph", BKE_ST_MAXNAME);
703         
704         st->new = graph_new;
705         st->free = graph_free;
706         st->init = graph_init;
707         st->duplicate = graph_duplicate;
708         st->operatortypes = graphedit_operatortypes;
709         st->keymap = graphedit_keymap;
710         st->listener = graph_listener;
711         st->refresh = graph_refresh;
712         st->id_remap = graph_id_remap;
713
714         /* regions: main window */
715         art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
716         art->regionid = RGN_TYPE_WINDOW;
717         art->init = graph_main_region_init;
718         art->draw = graph_main_region_draw;
719         art->listener = graph_region_listener;
720         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
721
722         BLI_addhead(&st->regiontypes, art);
723         
724         /* regions: header */
725         art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
726         art->regionid = RGN_TYPE_HEADER;
727         art->prefsizey = HEADERY;
728         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
729         art->listener = graph_region_listener;
730         art->init = graph_header_region_init;
731         art->draw = graph_header_region_draw;
732         
733         BLI_addhead(&st->regiontypes, art);
734         
735         /* regions: channels */
736         art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
737         art->regionid = RGN_TYPE_CHANNELS;
738         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 */
739         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
740         art->listener = graph_region_listener;
741         art->init = graph_channel_region_init;
742         art->draw = graph_channel_region_draw;
743         
744         BLI_addhead(&st->regiontypes, art);
745         
746         /* regions: UI buttons */
747         art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
748         art->regionid = RGN_TYPE_UI;
749         art->prefsizex = 200;
750         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
751         art->listener = graph_region_listener;
752         art->init = graph_buttons_region_init;
753         art->draw = graph_buttons_region_draw;
754         
755         BLI_addhead(&st->regiontypes, art);
756
757         graph_buttons_register(art);
758         
759         BKE_spacetype_register(st);
760 }
761