1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup spgraph
22  */
23 
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "DNA_anim_types.h"
28 #include "DNA_collection_types.h"
29 #include "DNA_scene_types.h"
30 
31 #include "MEM_guardedalloc.h"
32 
33 #include "BLI_blenlib.h"
34 #include "BLI_math.h"
35 #include "BLI_utildefines.h"
36 
37 #include "BKE_context.h"
38 #include "BKE_fcurve.h"
39 #include "BKE_screen.h"
40 
41 #include "ED_anim_api.h"
42 #include "ED_markers.h"
43 #include "ED_screen.h"
44 #include "ED_space_api.h"
45 #include "ED_time_scrub_ui.h"
46 
47 #include "GPU_framebuffer.h"
48 #include "GPU_immediate.h"
49 #include "GPU_state.h"
50 
51 #include "WM_api.h"
52 #include "WM_message.h"
53 #include "WM_types.h"
54 
55 #include "RNA_access.h"
56 #include "RNA_define.h"
57 #include "RNA_enum_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 /* ******************** default callbacks for ipo space ***************** */
66 
graph_create(const ScrArea * UNUSED (area),const Scene * scene)67 static SpaceLink *graph_create(const ScrArea *UNUSED(area), const Scene *scene)
68 {
69   ARegion *region;
70   SpaceGraph *sipo;
71 
72   /* Graph Editor - general stuff */
73   sipo = MEM_callocN(sizeof(SpaceGraph), "init graphedit");
74   sipo->spacetype = SPACE_GRAPH;
75 
76   sipo->autosnap = SACTSNAP_FRAME;
77 
78   /* allocate DopeSheet data for Graph Editor */
79   sipo->ads = MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
80   sipo->ads->source = (ID *)scene;
81 
82   /* settings for making it easier by default to just see what you're interested in tweaking */
83   sipo->ads->filterflag |= ADS_FILTER_ONLYSEL;
84   sipo->flag |= SIPO_SELVHANDLESONLY | SIPO_SHOW_MARKERS;
85 
86   /* header */
87   region = MEM_callocN(sizeof(ARegion), "header for graphedit");
88 
89   BLI_addtail(&sipo->regionbase, region);
90   region->regiontype = RGN_TYPE_HEADER;
91   region->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_BOTTOM : RGN_ALIGN_TOP;
92 
93   /* channels */
94   region = MEM_callocN(sizeof(ARegion), "channels region for graphedit");
95 
96   BLI_addtail(&sipo->regionbase, region);
97   region->regiontype = RGN_TYPE_CHANNELS;
98   region->alignment = RGN_ALIGN_LEFT;
99 
100   region->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
101 
102   /* ui buttons */
103   region = MEM_callocN(sizeof(ARegion), "buttons region for graphedit");
104 
105   BLI_addtail(&sipo->regionbase, region);
106   region->regiontype = RGN_TYPE_UI;
107   region->alignment = RGN_ALIGN_RIGHT;
108   region->flag = RGN_FLAG_HIDDEN;
109 
110   /* main region */
111   region = MEM_callocN(sizeof(ARegion), "main region for graphedit");
112 
113   BLI_addtail(&sipo->regionbase, region);
114   region->regiontype = RGN_TYPE_WINDOW;
115 
116   region->v2d.tot.xmin = 0.0f;
117   region->v2d.tot.ymin = (float)scene->r.sfra - 10.0f;
118   region->v2d.tot.xmax = (float)scene->r.efra;
119   region->v2d.tot.ymax = 10.0f;
120 
121   region->v2d.cur = region->v2d.tot;
122 
123   region->v2d.min[0] = FLT_MIN;
124   region->v2d.min[1] = FLT_MIN;
125 
126   region->v2d.max[0] = MAXFRAMEF;
127   region->v2d.max[1] = FLT_MAX;
128 
129   region->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_HORIZONTAL_HANDLES);
130   region->v2d.scroll |= (V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HANDLES);
131 
132   region->v2d.keeptot = 0;
133 
134   return (SpaceLink *)sipo;
135 }
136 
137 /* not spacelink itself */
graph_free(SpaceLink * sl)138 static void graph_free(SpaceLink *sl)
139 {
140   SpaceGraph *si = (SpaceGraph *)sl;
141 
142   if (si->ads) {
143     BLI_freelistN(&si->ads->chanbase);
144     MEM_freeN(si->ads);
145   }
146 
147   if (si->runtime.ghost_curves.first) {
148     BKE_fcurves_free(&si->runtime.ghost_curves);
149   }
150 }
151 
152 /* spacetype; init callback */
graph_init(struct wmWindowManager * wm,ScrArea * area)153 static void graph_init(struct wmWindowManager *wm, ScrArea *area)
154 {
155   SpaceGraph *sipo = (SpaceGraph *)area->spacedata.first;
156 
157   /* init dopesheet data if non-existent (i.e. for old files) */
158   if (sipo->ads == NULL) {
159     sipo->ads = MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
160     sipo->ads->source = (ID *)WM_window_get_active_scene(wm->winactive);
161   }
162 
163   /* force immediate init of any invalid F-Curve colors */
164   /* XXX: but, don't do SIPO_TEMP_NEEDCHANSYNC (i.e. channel select state sync)
165    * as this is run on each region resize; setting this here will cause selection
166    * state to be lost on area/region resizing. T35744.
167    */
168   ED_area_tag_refresh(area);
169 }
170 
graph_duplicate(SpaceLink * sl)171 static SpaceLink *graph_duplicate(SpaceLink *sl)
172 {
173   SpaceGraph *sipon = MEM_dupallocN(sl);
174 
175   /* clear or remove stuff from old */
176   BLI_duplicatelist(&sipon->runtime.ghost_curves, &((SpaceGraph *)sl)->runtime.ghost_curves);
177   sipon->ads = MEM_dupallocN(sipon->ads);
178 
179   return (SpaceLink *)sipon;
180 }
181 
182 /* add handlers, stuff you only do once or on area/region changes */
graph_main_region_init(wmWindowManager * wm,ARegion * region)183 static void graph_main_region_init(wmWindowManager *wm, ARegion *region)
184 {
185   wmKeyMap *keymap;
186 
187   UI_view2d_region_reinit(&region->v2d, V2D_COMMONVIEW_CUSTOM, region->winx, region->winy);
188 
189   /* own keymap */
190   keymap = WM_keymap_ensure(wm->defaultconf, "Graph Editor", SPACE_GRAPH, 0);
191   WM_event_add_keymap_handler_v2d_mask(&region->handlers, keymap);
192   keymap = WM_keymap_ensure(wm->defaultconf, "Graph Editor Generic", SPACE_GRAPH, 0);
193   WM_event_add_keymap_handler(&region->handlers, keymap);
194 }
195 
graph_main_region_draw(const bContext * C,ARegion * region)196 static void graph_main_region_draw(const bContext *C, ARegion *region)
197 {
198   /* draw entirely, view changes should be handled here */
199   SpaceGraph *sipo = CTX_wm_space_graph(C);
200   Scene *scene = CTX_data_scene(C);
201   bAnimContext ac;
202   View2D *v2d = &region->v2d;
203 
204   /* clear and setup matrix */
205   UI_ThemeClearColor(TH_BACK);
206 
207   UI_view2d_view_ortho(v2d);
208 
209   /* grid */
210   bool display_seconds = (sipo->mode == SIPO_MODE_ANIMATION) && (sipo->flag & SIPO_DRAWTIME);
211   UI_view2d_draw_lines_x__frames_or_seconds(v2d, scene, display_seconds);
212   UI_view2d_draw_lines_y__values(v2d);
213 
214   ED_region_draw_cb_draw(C, region, REGION_DRAW_PRE_VIEW);
215 
216   /* start and end frame (in F-Curve mode only) */
217   if (sipo->mode != SIPO_MODE_DRIVERS) {
218     ANIM_draw_framerange(scene, v2d);
219   }
220 
221   /* draw data */
222   if (ANIM_animdata_get_context(C, &ac)) {
223     /* draw ghost curves */
224     graph_draw_ghost_curves(&ac, sipo, region);
225 
226     /* draw curves twice - unselected, then selected, so that the are fewer occlusion problems */
227     graph_draw_curves(&ac, sipo, region, 0);
228     graph_draw_curves(&ac, sipo, region, 1);
229 
230     /* XXX the slow way to set tot rect... but for nice sliders needed (ton) */
231     get_graph_keyframe_extents(
232         &ac, &v2d->tot.xmin, &v2d->tot.xmax, &v2d->tot.ymin, &v2d->tot.ymax, false, true);
233     /* extra offset so that these items are visible */
234     v2d->tot.xmin -= 10.0f;
235     v2d->tot.xmax += 10.0f;
236   }
237 
238   if (((sipo->flag & SIPO_NODRAWCURSOR) == 0) || (sipo->mode == SIPO_MODE_DRIVERS)) {
239     uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
240 
241     immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
242 
243     /* horizontal component of value-cursor (value line before the current frame line) */
244     if ((sipo->flag & SIPO_NODRAWCURSOR) == 0) {
245       float y = sipo->cursorVal;
246 
247       /* Draw a green line to indicate the cursor value */
248       immUniformThemeColorShadeAlpha(TH_CFRAME, -10, -50);
249       GPU_blend(GPU_BLEND_ALPHA);
250       GPU_line_width(2.0);
251 
252       immBegin(GPU_PRIM_LINES, 2);
253       immVertex2f(pos, v2d->cur.xmin, y);
254       immVertex2f(pos, v2d->cur.xmax, y);
255       immEnd();
256 
257       GPU_blend(GPU_BLEND_NONE);
258     }
259 
260     /* current frame or vertical component of vertical component of the cursor */
261     if (sipo->mode == SIPO_MODE_DRIVERS) {
262       /* cursor x-value */
263       float x = sipo->cursorTime;
264 
265       /* to help differentiate this from the current frame,
266        * draw slightly darker like the horizontal one */
267       immUniformThemeColorShadeAlpha(TH_CFRAME, -40, -50);
268       GPU_blend(GPU_BLEND_ALPHA);
269       GPU_line_width(2.0);
270 
271       immBegin(GPU_PRIM_LINES, 2);
272       immVertex2f(pos, x, v2d->cur.ymin);
273       immVertex2f(pos, x, v2d->cur.ymax);
274       immEnd();
275 
276       GPU_blend(GPU_BLEND_NONE);
277     }
278 
279     immUnbindProgram();
280   }
281 
282   /* markers */
283   if (sipo->mode != SIPO_MODE_DRIVERS) {
284     UI_view2d_view_orthoSpecial(region, v2d, 1);
285     int marker_draw_flag = DRAW_MARKERS_MARGIN;
286     if (sipo->flag & SIPO_SHOW_MARKERS) {
287       ED_markers_draw(C, marker_draw_flag);
288     }
289   }
290 
291   /* preview range */
292   if (sipo->mode != SIPO_MODE_DRIVERS) {
293     UI_view2d_view_ortho(v2d);
294     ANIM_draw_previewrange(C, v2d, 0);
295   }
296 
297   /* callback */
298   UI_view2d_view_ortho(v2d);
299   ED_region_draw_cb_draw(C, region, REGION_DRAW_POST_VIEW);
300 
301   /* reset view matrix */
302   UI_view2d_view_restore(C);
303 
304   /* time-scrubbing */
305   ED_time_scrub_draw(region, scene, display_seconds, false);
306 }
307 
graph_main_region_draw_overlay(const bContext * C,ARegion * region)308 static void graph_main_region_draw_overlay(const bContext *C, ARegion *region)
309 {
310   /* draw entirely, view changes should be handled here */
311   const SpaceGraph *sipo = CTX_wm_space_graph(C);
312   const Scene *scene = CTX_data_scene(C);
313   const bool draw_vert_line = sipo->mode != SIPO_MODE_DRIVERS;
314   View2D *v2d = &region->v2d;
315 
316   /* scrubbing region */
317   ED_time_scrub_draw_current_frame(region, scene, sipo->flag & SIPO_DRAWTIME, draw_vert_line);
318 
319   /* scrollers */
320   /* FIXME: args for scrollers depend on the type of data being shown. */
321   UI_view2d_scrollers_draw(v2d, NULL);
322 
323   /* scale numbers */
324   {
325     rcti rect;
326     BLI_rcti_init(
327         &rect, 0, 15 * UI_DPI_FAC, 15 * UI_DPI_FAC, region->winy - UI_TIME_SCRUB_MARGIN_Y);
328     UI_view2d_draw_scale_y__values(region, v2d, &rect, TH_SCROLL_TEXT);
329   }
330 }
331 
graph_channel_region_init(wmWindowManager * wm,ARegion * region)332 static void graph_channel_region_init(wmWindowManager *wm, ARegion *region)
333 {
334   wmKeyMap *keymap;
335 
336   /* make sure we keep the hide flags */
337   region->v2d.scroll |= V2D_SCROLL_RIGHT;
338 
339   /* prevent any noise of past */
340   region->v2d.scroll &= ~(V2D_SCROLL_LEFT | V2D_SCROLL_TOP | V2D_SCROLL_BOTTOM);
341 
342   region->v2d.scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
343   region->v2d.scroll |= V2D_SCROLL_VERTICAL_HIDE;
344 
345   UI_view2d_region_reinit(&region->v2d, V2D_COMMONVIEW_LIST, region->winx, region->winy);
346 
347   /* own keymap */
348   keymap = WM_keymap_ensure(wm->defaultconf, "Animation Channels", 0, 0);
349   WM_event_add_keymap_handler_v2d_mask(&region->handlers, keymap);
350   keymap = WM_keymap_ensure(wm->defaultconf, "Graph Editor Generic", SPACE_GRAPH, 0);
351   WM_event_add_keymap_handler(&region->handlers, keymap);
352 }
353 
graph_channel_region_draw(const bContext * C,ARegion * region)354 static void graph_channel_region_draw(const bContext *C, ARegion *region)
355 {
356   bAnimContext ac;
357   View2D *v2d = &region->v2d;
358 
359   /* clear and setup matrix */
360   UI_ThemeClearColor(TH_BACK);
361 
362   UI_view2d_view_ortho(v2d);
363 
364   /* draw channels */
365   if (ANIM_animdata_get_context(C, &ac)) {
366     graph_draw_channel_names((bContext *)C, &ac, region);
367   }
368 
369   /* channel filter next to scrubbing area */
370   ED_time_scrub_channel_search_draw(C, region, ac.ads);
371 
372   /* reset view matrix */
373   UI_view2d_view_restore(C);
374 
375   /* scrollers */
376   UI_view2d_scrollers_draw(v2d, NULL);
377 }
378 
379 /* add handlers, stuff you only do once or on area/region changes */
graph_header_region_init(wmWindowManager * UNUSED (wm),ARegion * region)380 static void graph_header_region_init(wmWindowManager *UNUSED(wm), ARegion *region)
381 {
382   ED_region_header_init(region);
383 }
384 
graph_header_region_draw(const bContext * C,ARegion * region)385 static void graph_header_region_draw(const bContext *C, ARegion *region)
386 {
387   ED_region_header(C, region);
388 }
389 
390 /* add handlers, stuff you only do once or on area/region changes */
graph_buttons_region_init(wmWindowManager * wm,ARegion * region)391 static void graph_buttons_region_init(wmWindowManager *wm, ARegion *region)
392 {
393   wmKeyMap *keymap;
394 
395   ED_region_panels_init(wm, region);
396 
397   keymap = WM_keymap_ensure(wm->defaultconf, "Graph Editor Generic", SPACE_GRAPH, 0);
398   WM_event_add_keymap_handler_v2d_mask(&region->handlers, keymap);
399 }
400 
graph_buttons_region_draw(const bContext * C,ARegion * region)401 static void graph_buttons_region_draw(const bContext *C, ARegion *region)
402 {
403   ED_region_panels(C, region);
404 }
405 
graph_region_listener(wmWindow * UNUSED (win),ScrArea * UNUSED (area),ARegion * region,wmNotifier * wmn,const Scene * UNUSED (scene))406 static void graph_region_listener(wmWindow *UNUSED(win),
407                                   ScrArea *UNUSED(area),
408                                   ARegion *region,
409                                   wmNotifier *wmn,
410                                   const Scene *UNUSED(scene))
411 {
412   /* context changes */
413   switch (wmn->category) {
414     case NC_ANIMATION:
415       ED_region_tag_redraw(region);
416       break;
417     case NC_SCENE:
418       switch (wmn->data) {
419         case ND_RENDER_OPTIONS:
420         case ND_OB_ACTIVE:
421         case ND_FRAME:
422         case ND_FRAME_RANGE:
423         case ND_MARKERS:
424           ED_region_tag_redraw(region);
425           break;
426         case ND_SEQUENCER:
427           if (wmn->action == NA_SELECTED) {
428             ED_region_tag_redraw(region);
429           }
430           break;
431       }
432       break;
433     case NC_OBJECT:
434       switch (wmn->data) {
435         case ND_BONE_ACTIVE:
436         case ND_BONE_SELECT:
437         case ND_KEYS:
438           ED_region_tag_redraw(region);
439           break;
440         case ND_MODIFIER:
441           if (wmn->action == NA_RENAME) {
442             ED_region_tag_redraw(region);
443           }
444           break;
445       }
446       break;
447     case NC_NODE:
448       switch (wmn->action) {
449         case NA_EDITED:
450         case NA_SELECTED:
451           ED_region_tag_redraw(region);
452           break;
453       }
454       break;
455     case NC_ID:
456       if (wmn->action == NA_RENAME) {
457         ED_region_tag_redraw(region);
458       }
459       break;
460     case NC_SCREEN:
461       if (ELEM(wmn->data, ND_LAYER)) {
462         ED_region_tag_redraw(region);
463       }
464       break;
465     default:
466       if (wmn->data == ND_KEYS) {
467         ED_region_tag_redraw(region);
468       }
469       break;
470   }
471 }
472 
graph_region_message_subscribe(const struct bContext * UNUSED (C),struct WorkSpace * UNUSED (workspace),struct Scene * scene,struct bScreen * screen,struct ScrArea * area,struct ARegion * region,struct wmMsgBus * mbus)473 static void graph_region_message_subscribe(const struct bContext *UNUSED(C),
474                                            struct WorkSpace *UNUSED(workspace),
475                                            struct Scene *scene,
476                                            struct bScreen *screen,
477                                            struct ScrArea *area,
478                                            struct ARegion *region,
479                                            struct wmMsgBus *mbus)
480 {
481   PointerRNA ptr;
482   RNA_pointer_create(&screen->id, &RNA_SpaceGraphEditor, area->spacedata.first, &ptr);
483 
484   wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
485       .owner = region,
486       .user_data = region,
487       .notify = ED_region_do_msg_notify_tag_redraw,
488   };
489 
490   /* Timeline depends on scene properties. */
491   {
492     bool use_preview = (scene->r.flag & SCER_PRV_RANGE);
493     extern PropertyRNA rna_Scene_frame_start;
494     extern PropertyRNA rna_Scene_frame_end;
495     extern PropertyRNA rna_Scene_frame_preview_start;
496     extern PropertyRNA rna_Scene_frame_preview_end;
497     extern PropertyRNA rna_Scene_use_preview_range;
498     extern PropertyRNA rna_Scene_frame_current;
499     const PropertyRNA *props[] = {
500         use_preview ? &rna_Scene_frame_preview_start : &rna_Scene_frame_start,
501         use_preview ? &rna_Scene_frame_preview_end : &rna_Scene_frame_end,
502         &rna_Scene_use_preview_range,
503         &rna_Scene_frame_current,
504     };
505 
506     PointerRNA idptr;
507     RNA_id_pointer_create(&scene->id, &idptr);
508 
509     for (int i = 0; i < ARRAY_SIZE(props); i++) {
510       WM_msg_subscribe_rna(mbus, &idptr, props[i], &msg_sub_value_region_tag_redraw, __func__);
511     }
512   }
513 
514   /* All dopesheet filter settings, etc. affect the drawing of this editor,
515    * also same applies for all animation-related datatypes that may appear here,
516    * so just whitelist the entire structs for updates
517    */
518   {
519     wmMsgParams_RNA msg_key_params = {{0}};
520     StructRNA *type_array[] = {
521         &RNA_DopeSheet, /* dopesheet filters */
522 
523         &RNA_ActionGroup, /* channel groups */
524         &RNA_FCurve,      /* F-Curve */
525         &RNA_Keyframe,
526         &RNA_FCurveSample,
527 
528         &RNA_FModifier, /* F-Modifiers (XXX: Why can't we just do all subclasses too?) */
529         &RNA_FModifierCycles,
530         &RNA_FModifierEnvelope,
531         &RNA_FModifierEnvelopeControlPoint,
532         &RNA_FModifierFunctionGenerator,
533         &RNA_FModifierGenerator,
534         &RNA_FModifierLimits,
535         &RNA_FModifierNoise,
536         &RNA_FModifierPython,
537         &RNA_FModifierStepped,
538     };
539 
540     for (int i = 0; i < ARRAY_SIZE(type_array); i++) {
541       msg_key_params.ptr.type = type_array[i];
542       WM_msg_subscribe_rna_params(
543           mbus, &msg_key_params, &msg_sub_value_region_tag_redraw, __func__);
544     }
545   }
546 }
547 
548 /* editor level listener */
graph_listener(wmWindow * UNUSED (win),ScrArea * area,wmNotifier * wmn,Scene * UNUSED (scene))549 static void graph_listener(wmWindow *UNUSED(win),
550                            ScrArea *area,
551                            wmNotifier *wmn,
552                            Scene *UNUSED(scene))
553 {
554   SpaceGraph *sipo = (SpaceGraph *)area->spacedata.first;
555 
556   /* context changes */
557   switch (wmn->category) {
558     case NC_ANIMATION:
559       /* for selection changes of animation data, we can just redraw...
560        * otherwise autocolor might need to be done again */
561       if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED)) {
562         ED_area_tag_redraw(area);
563       }
564       else {
565         ED_area_tag_refresh(area);
566       }
567       break;
568     case NC_SCENE:
569       switch (wmn->data) {
570         case ND_OB_ACTIVE: /* Selection changed, so force refresh to flush
571                             * (needs flag set to do syncing). */
572         case ND_OB_SELECT:
573           sipo->runtime.flag |= SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC;
574           ED_area_tag_refresh(area);
575           break;
576 
577         default: /* just redrawing the view will do */
578           ED_area_tag_redraw(area);
579           break;
580       }
581       break;
582     case NC_OBJECT:
583       switch (wmn->data) {
584         case ND_BONE_SELECT: /* Selection changed, so force refresh to flush
585                               * (needs flag set to do syncing). */
586         case ND_BONE_ACTIVE:
587           sipo->runtime.flag |= SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC;
588           ED_area_tag_refresh(area);
589           break;
590         case ND_TRANSFORM:
591           break; /*do nothing*/
592 
593         default: /* just redrawing the view will do */
594           ED_area_tag_redraw(area);
595           break;
596       }
597       break;
598     case NC_NODE:
599       if (wmn->action == NA_SELECTED) {
600         /* selection changed, so force refresh to flush (needs flag set to do syncing) */
601         sipo->runtime.flag |= SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC;
602         ED_area_tag_refresh(area);
603       }
604       break;
605     case NC_SPACE:
606       if (wmn->data == ND_SPACE_GRAPH) {
607         ED_area_tag_redraw(area);
608       }
609       break;
610     case NC_WINDOW:
611       if (sipo->runtime.flag &
612           (SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC | SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC_COLOR)) {
613         /* force redraw/refresh after undo/redo - prevents "black curve" problem */
614         ED_area_tag_refresh(area);
615       }
616       break;
617 
618 #if 0 /* XXX: restore the case below if not enough updates occur... */
619     default: {
620       if (wmn->data == ND_KEYS) {
621         ED_area_tag_redraw(area);
622       }
623     }
624 #endif
625   }
626 }
627 
628 /* Update F-Curve colors */
graph_refresh_fcurve_colors(const bContext * C)629 static void graph_refresh_fcurve_colors(const bContext *C)
630 {
631   bAnimContext ac;
632 
633   ListBase anim_data = {NULL, NULL};
634   bAnimListElem *ale;
635   size_t items;
636   int filter;
637   int i;
638 
639   if (ANIM_animdata_get_context(C, &ac) == false) {
640     return;
641   }
642 
643   UI_SetTheme(SPACE_GRAPH, RGN_TYPE_WINDOW);
644 
645   /* build list of F-Curves which will be visible as channels in channel-region
646    * - we don't include ANIMFILTER_CURVEVISIBLE filter, as that will result in a
647    *   mismatch between channel-colors and the drawn curves
648    */
649   filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_NODUPLIS);
650   items = ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
651 
652   /* loop over F-Curves, assigning colors */
653   for (ale = anim_data.first, i = 0; ale; ale = ale->next, i++) {
654     FCurve *fcu = (FCurve *)ale->data;
655 
656     /* set color of curve here */
657     switch (fcu->color_mode) {
658       case FCURVE_COLOR_CUSTOM: {
659         /* User has defined a custom color for this curve already
660          * (we assume it's not going to cause clashes with text colors),
661          * which should be left alone... Nothing needs to be done here.
662          */
663         break;
664       }
665       case FCURVE_COLOR_AUTO_RGB: {
666         /* F-Curve's array index is automatically mapped to RGB values.
667          * This works best of 3-value vectors.
668          * TODO: find a way to module the hue so that not all curves have same color...
669          */
670         float *col = fcu->color;
671 
672         switch (fcu->array_index) {
673           case 0:
674             UI_GetThemeColor3fv(TH_AXIS_X, col);
675             break;
676           case 1:
677             UI_GetThemeColor3fv(TH_AXIS_Y, col);
678             break;
679           case 2:
680             UI_GetThemeColor3fv(TH_AXIS_Z, col);
681             break;
682           default:
683             /* 'unknown' color - bluish so as to not conflict with handles */
684             col[0] = 0.3f;
685             col[1] = 0.8f;
686             col[2] = 1.0f;
687             break;
688         }
689         break;
690       }
691       case FCURVE_COLOR_AUTO_YRGB: {
692         /* Like FCURVE_COLOR_AUTO_RGB, except this is for quaternions... */
693         float *col = fcu->color;
694 
695         switch (fcu->array_index) {
696           case 1:
697             UI_GetThemeColor3fv(TH_AXIS_X, col);
698             break;
699           case 2:
700             UI_GetThemeColor3fv(TH_AXIS_Y, col);
701             break;
702           case 3:
703             UI_GetThemeColor3fv(TH_AXIS_Z, col);
704             break;
705 
706           case 0: {
707             /* Special Case: "W" channel should be yellowish, so blend X and Y channel colors... */
708             float c1[3], c2[3];
709             float h1[3], h2[3];
710             float hresult[3];
711 
712             /* - get colors (rgb) */
713             UI_GetThemeColor3fv(TH_AXIS_X, c1);
714             UI_GetThemeColor3fv(TH_AXIS_Y, c2);
715 
716             /* - perform blending in HSV space (to keep brightness similar) */
717             rgb_to_hsv_v(c1, h1);
718             rgb_to_hsv_v(c2, h2);
719 
720             interp_v3_v3v3(hresult, h1, h2, 0.5f);
721 
722             /* - convert back to RGB for display */
723             hsv_to_rgb_v(hresult, col);
724             break;
725           }
726 
727           default:
728             /* 'unknown' color - bluish so as to not conflict with handles */
729             col[0] = 0.3f;
730             col[1] = 0.8f;
731             col[2] = 1.0f;
732             break;
733         }
734         break;
735       }
736       case FCURVE_COLOR_AUTO_RAINBOW:
737       default: {
738         /* determine color 'automatically' using 'magic function' which uses the given args
739          * of current item index + total items to determine some RGB color
740          */
741         getcolor_fcurve_rainbow(i, items, fcu->color);
742         break;
743       }
744     }
745   }
746 
747   /* free temp list */
748   ANIM_animdata_freelist(&anim_data);
749 }
750 
graph_refresh(const bContext * C,ScrArea * area)751 static void graph_refresh(const bContext *C, ScrArea *area)
752 {
753   SpaceGraph *sipo = (SpaceGraph *)area->spacedata.first;
754 
755   /* updates to data needed depends on Graph Editor mode... */
756   switch (sipo->mode) {
757     case SIPO_MODE_ANIMATION: /* all animation */
758     {
759       break;
760     }
761 
762     case SIPO_MODE_DRIVERS: /* drivers only  */
763     {
764       break;
765     }
766   }
767 
768   /* region updates? */
769   /* XXX re-sizing y-extents of tot should go here? */
770 
771   /* Update the state of the animchannels in response to changes from the data they represent
772    * NOTE: the temp flag is used to indicate when this needs to be done,
773    * and will be cleared once handled. */
774   if (sipo->runtime.flag & SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC) {
775     ANIM_sync_animchannels_to_data(C);
776     sipo->runtime.flag &= ~SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC;
777     ED_area_tag_redraw(area);
778   }
779 
780   /* We could check 'SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC_COLOR', but color is recalculated anyway. */
781   if (sipo->runtime.flag & SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC_COLOR) {
782     sipo->runtime.flag &= ~SIPO_RUNTIME_FLAG_NEED_CHAN_SYNC_COLOR;
783 #if 0 /* Done below. */
784     graph_refresh_fcurve_colors(C);
785 #endif
786     ED_area_tag_redraw(area);
787   }
788 
789   sipo->runtime.flag &= ~(SIPO_RUNTIME_FLAG_TWEAK_HANDLES_LEFT |
790                           SIPO_RUNTIME_FLAG_TWEAK_HANDLES_RIGHT);
791 
792   /* init/adjust F-Curve colors */
793   graph_refresh_fcurve_colors(C);
794 }
795 
graph_id_remap(ScrArea * UNUSED (area),SpaceLink * slink,ID * old_id,ID * new_id)796 static void graph_id_remap(ScrArea *UNUSED(area), SpaceLink *slink, ID *old_id, ID *new_id)
797 {
798   SpaceGraph *sgraph = (SpaceGraph *)slink;
799 
800   if (sgraph->ads) {
801     if ((ID *)sgraph->ads->filter_grp == old_id) {
802       sgraph->ads->filter_grp = (Collection *)new_id;
803     }
804     if ((ID *)sgraph->ads->source == old_id) {
805       sgraph->ads->source = new_id;
806     }
807   }
808 }
809 
graph_space_subtype_get(ScrArea * area)810 static int graph_space_subtype_get(ScrArea *area)
811 {
812   SpaceGraph *sgraph = area->spacedata.first;
813   return sgraph->mode;
814 }
815 
graph_space_subtype_set(ScrArea * area,int value)816 static void graph_space_subtype_set(ScrArea *area, int value)
817 {
818   SpaceGraph *sgraph = area->spacedata.first;
819   sgraph->mode = value;
820 }
821 
graph_space_subtype_item_extend(bContext * UNUSED (C),EnumPropertyItem ** item,int * totitem)822 static void graph_space_subtype_item_extend(bContext *UNUSED(C),
823                                             EnumPropertyItem **item,
824                                             int *totitem)
825 {
826   RNA_enum_items_add(item, totitem, rna_enum_space_graph_mode_items);
827 }
828 
829 /* only called once, from space/spacetypes.c */
ED_spacetype_ipo(void)830 void ED_spacetype_ipo(void)
831 {
832   SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype ipo");
833   ARegionType *art;
834 
835   st->spaceid = SPACE_GRAPH;
836   strncpy(st->name, "Graph", BKE_ST_MAXNAME);
837 
838   st->create = graph_create;
839   st->free = graph_free;
840   st->init = graph_init;
841   st->duplicate = graph_duplicate;
842   st->operatortypes = graphedit_operatortypes;
843   st->keymap = graphedit_keymap;
844   st->listener = graph_listener;
845   st->refresh = graph_refresh;
846   st->id_remap = graph_id_remap;
847   st->space_subtype_item_extend = graph_space_subtype_item_extend;
848   st->space_subtype_get = graph_space_subtype_get;
849   st->space_subtype_set = graph_space_subtype_set;
850 
851   /* regions: main window */
852   art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
853   art->regionid = RGN_TYPE_WINDOW;
854   art->init = graph_main_region_init;
855   art->draw = graph_main_region_draw;
856   art->draw_overlay = graph_main_region_draw_overlay;
857   art->listener = graph_region_listener;
858   art->message_subscribe = graph_region_message_subscribe;
859   art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
860 
861   BLI_addhead(&st->regiontypes, art);
862 
863   /* regions: header */
864   art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
865   art->regionid = RGN_TYPE_HEADER;
866   art->prefsizey = HEADERY;
867   art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
868   art->listener = graph_region_listener;
869   art->init = graph_header_region_init;
870   art->draw = graph_header_region_draw;
871 
872   BLI_addhead(&st->regiontypes, art);
873 
874   /* regions: channels */
875   art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
876   art->regionid = RGN_TYPE_CHANNELS;
877   /* 200 is the 'standard', but due to scrollers, we want a bit more to fit the lock icons in */
878   art->prefsizex = 200 + V2D_SCROLL_WIDTH;
879   art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
880   art->listener = graph_region_listener;
881   art->message_subscribe = graph_region_message_subscribe;
882   art->init = graph_channel_region_init;
883   art->draw = graph_channel_region_draw;
884 
885   BLI_addhead(&st->regiontypes, art);
886 
887   /* regions: UI buttons */
888   art = MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
889   art->regionid = RGN_TYPE_UI;
890   art->prefsizex = UI_SIDEBAR_PANEL_WIDTH;
891   art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
892   art->listener = graph_region_listener;
893   art->init = graph_buttons_region_init;
894   art->draw = graph_buttons_region_draw;
895 
896   BLI_addhead(&st->regiontypes, art);
897 
898   graph_buttons_register(art);
899 
900   art = ED_area_type_hud(st->spaceid);
901   BLI_addhead(&st->regiontypes, art);
902 
903   BKE_spacetype_register(st);
904 }
905