patch [#20342] Notifier patch for modifier rename and particle system rename
[blender.git] / source / blender / editors / space_graph / space_graph.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_anim_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_space_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_main.h"
47 #include "BKE_fcurve.h"
48 #include "BKE_screen.h"
49 #include "BKE_utildefines.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_interface.h"
62 #include "UI_resources.h"
63 #include "UI_view2d.h"
64
65 #include "graph_intern.h"       // own include
66
67 /* ******************** manage regions ********************* */
68
69 ARegion *graph_has_buttons_region(ScrArea *sa)
70 {
71         ARegion *ar, *arnew;
72         
73         for (ar= sa->regionbase.first; ar; ar= ar->next) {
74                 if (ar->regiontype==RGN_TYPE_UI)
75                         return ar;
76         }
77         
78         /* add subdiv level; after main */
79         for (ar= sa->regionbase.first; ar; ar= ar->next) {
80                 if (ar->regiontype==RGN_TYPE_WINDOW)
81                         break;
82         }
83         
84         /* is error! */
85         if (ar==NULL) return NULL;
86         
87         arnew= MEM_callocN(sizeof(ARegion), "buttons for graph");
88         
89         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
90         arnew->regiontype= RGN_TYPE_UI;
91         arnew->alignment= RGN_ALIGN_RIGHT;
92         
93         arnew->flag = RGN_FLAG_HIDDEN;
94         
95         return arnew;
96 }
97
98
99 /* ******************** default callbacks for ipo space ***************** */
100
101 static SpaceLink *graph_new(const bContext *C)
102 {
103         Scene *scene= CTX_data_scene(C);
104         ARegion *ar;
105         SpaceIpo *sipo;
106         
107         /* Graph Editor - general stuff */
108         sipo= MEM_callocN(sizeof(SpaceIpo), "init graphedit");
109         sipo->spacetype= SPACE_IPO;
110         
111         sipo->autosnap= SACTSNAP_FRAME;
112         
113         /* allocate DopeSheet data for Graph Editor */
114         sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
115         sipo->ads->source= (ID *)scene;
116         
117         /* header */
118         ar= MEM_callocN(sizeof(ARegion), "header for graphedit");
119         
120         BLI_addtail(&sipo->regionbase, ar);
121         ar->regiontype= RGN_TYPE_HEADER;
122         ar->alignment= RGN_ALIGN_BOTTOM;
123         
124         /* channels */
125         ar= MEM_callocN(sizeof(ARegion), "channels area for graphedit");
126         
127         BLI_addtail(&sipo->regionbase, ar);
128         ar->regiontype= RGN_TYPE_CHANNELS;
129         ar->alignment= RGN_ALIGN_LEFT;
130         
131         ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
132         
133         /* ui buttons */
134         ar= MEM_callocN(sizeof(ARegion), "buttons area for graphedit");
135         
136         BLI_addtail(&sipo->regionbase, ar);
137         ar->regiontype= RGN_TYPE_UI;
138         ar->alignment= RGN_ALIGN_RIGHT;
139         ar->flag = RGN_FLAG_HIDDEN;
140         
141         /* main area */
142         ar= MEM_callocN(sizeof(ARegion), "main area for graphedit");
143         
144         BLI_addtail(&sipo->regionbase, ar);
145         ar->regiontype= RGN_TYPE_WINDOW;
146         
147         ar->v2d.tot.xmin= 0.0f;
148         ar->v2d.tot.ymin= (float)scene->r.sfra - 10.0f;
149         ar->v2d.tot.xmax= (float)scene->r.efra;
150         ar->v2d.tot.ymax= 10.0f;
151         
152         ar->v2d.cur= ar->v2d.tot;
153         
154         ar->v2d.min[0]= 0.001f;
155         ar->v2d.min[1]= 0.001f;
156         
157         ar->v2d.max[0]= MAXFRAMEF;
158         ar->v2d.max[1]= 50000.0f;
159         
160         ar->v2d.scroll= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
161         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
162         
163         ar->v2d.keeptot= 0;
164         
165         return (SpaceLink *)sipo;
166 }
167
168 /* not spacelink itself */
169 static void graph_free(SpaceLink *sl)
170 {       
171         SpaceIpo *si= (SpaceIpo *)sl;
172         
173         if (si->ads) {
174                 BLI_freelistN(&si->ads->chanbase);
175                 MEM_freeN(si->ads);
176         }
177         
178         if (si->ghostCurves.first)
179                 free_fcurves(&si->ghostCurves);
180 }
181
182
183 /* spacetype; init callback */
184 static void graph_init(struct wmWindowManager *wm, ScrArea *sa)
185 {
186         SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
187         
188         /* init dopesheet data if non-existant (i.e. for old files) */
189         if (sipo->ads == NULL) {
190                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
191                 sipo->ads->source= (ID *)(G.main->scene.first); // FIXME: this is a really nasty hack here for now...
192         }
193
194         ED_area_tag_refresh(sa);
195 }
196
197 static SpaceLink *graph_duplicate(SpaceLink *sl)
198 {
199         SpaceIpo *sipon= MEM_dupallocN(sl);
200         
201         /* clear or remove stuff from old */
202         BLI_duplicatelist(&sipon->ghostCurves, &((SpaceIpo *)sl)->ghostCurves);
203         sipon->ads= MEM_dupallocN(sipon->ads);
204         
205         return (SpaceLink *)sipon;
206 }
207
208 /* add handlers, stuff you only do once or on area/region changes */
209 static void graph_main_area_init(wmWindowManager *wm, ARegion *ar)
210 {
211         wmKeyMap *keymap;
212         
213         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
214         
215         /* own keymap */
216         keymap= WM_keymap_find(wm->defaultconf, "GraphEdit Keys", 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, "GraphEdit Generic", SPACE_IPO, 0);
219         WM_event_add_keymap_handler(&ar->handlers, keymap);
220 }
221
222 static void graph_main_area_draw(const bContext *C, ARegion *ar)
223 {
224         /* draw entirely, view changes should be handled here */
225         SpaceIpo *sipo= CTX_wm_space_graph(C);
226         bAnimContext ac;
227         View2D *v2d= &ar->v2d;
228         View2DGrid *grid;
229         View2DScrollers *scrollers;
230         float col[3];
231         short unitx=0, unity=V2D_UNIT_VALUES, flag=0;
232         
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);
237         
238         UI_view2d_view_ortho(C, v2d);
239         
240         /* grid */
241         unitx= (sipo->flag & SIPO_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
242         grid= UI_view2d_grid_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
243         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
244         
245         /* draw data */
246         if (ANIM_animdata_get_context(C, &ac)) {
247                 /* draw ghost curves */
248                 graph_draw_ghost_curves(&ac, sipo, ar, grid);
249                 
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);
253                 
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);
256                 /* extra offset so that these items are visible */
257                 v2d->tot.xmin -= 10.0f;
258                 v2d->tot.xmax += 10.0f;
259         }
260         
261         /* only free grid after drawing data, as we need to use it to determine sampling rate */
262         UI_view2d_grid_free(grid);
263         
264         /* horizontal component of value-cursor (value line before the current frame line) */
265         if ((sipo->flag & SIPO_NODRAWCURSOR)==0) {
266                 float vec[2];
267                 
268                 /* Draw a green line to indicate the cursor value */
269                 vec[1]= sipo->cursorVal;
270                 
271                 UI_ThemeColorShadeAlpha(TH_CFRAME, -10, -50);
272                 glLineWidth(2.0);
273                 
274                 glEnable(GL_BLEND);
275                 glBegin(GL_LINE_STRIP);
276                         vec[0]= v2d->cur.xmin;
277                         glVertex2fv(vec);
278                         
279                         vec[0]= v2d->cur.xmax;
280                         glVertex2fv(vec);
281                 glEnd(); // GL_LINE_STRIP
282                 glDisable(GL_BLEND);
283         }
284         
285         /* current frame */
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);
289         
290         /* markers */
291         UI_view2d_view_orthoSpecial(C, v2d, 1);
292         draw_markers_time(C, 0);
293         
294         /* preview range */
295         UI_view2d_view_ortho(C, v2d);
296         ANIM_draw_previewrange(C, v2d);
297         
298         /* reset view matrix */
299         UI_view2d_view_restore(C);
300         
301         /* scrollers */
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);
306 }
307
308 static void graph_channel_area_init(wmWindowManager *wm, ARegion *ar)
309 {
310         wmKeyMap *keymap;
311         
312         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
313         
314         /* own keymap */
315         keymap= WM_keymap_find(wm->defaultconf, "Animation_Channels", 0, 0);
316         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
317         keymap= WM_keymap_find(wm->defaultconf, "GraphEdit Generic", SPACE_IPO, 0);
318         WM_event_add_keymap_handler(&ar->handlers, keymap);
319 }
320
321 static void graph_channel_area_draw(const bContext *C, ARegion *ar)
322 {
323         SpaceIpo *sipo= CTX_wm_space_graph(C);
324         bAnimContext ac;
325         View2D *v2d= &ar->v2d;
326         View2DScrollers *scrollers;
327         float col[3];
328         
329         /* clear and setup matrix */
330         UI_GetThemeColor3fv(TH_BACK, col);
331         glClearColor(col[0], col[1], col[2], 0.0);
332         glClear(GL_COLOR_BUFFER_BIT);
333         
334         UI_view2d_view_ortho(C, v2d);
335         
336         /* draw channels */
337         if (ANIM_animdata_get_context(C, &ac)) {
338                 graph_draw_channel_names((bContext*)C, &ac, sipo, ar);
339         }
340         
341         /* reset view matrix */
342         UI_view2d_view_restore(C);
343         
344         /* scrollers */
345         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
346         UI_view2d_scrollers_draw(C, v2d, scrollers);
347         UI_view2d_scrollers_free(scrollers);
348 }
349
350 /* add handlers, stuff you only do once or on area/region changes */
351 static void graph_header_area_init(wmWindowManager *wm, ARegion *ar)
352 {
353         ED_region_header_init(ar);
354 }
355
356 static void graph_header_area_draw(const bContext *C, ARegion *ar)
357 {
358         ED_region_header(C, ar);
359 }
360
361 /* add handlers, stuff you only do once or on area/region changes */
362 static void graph_buttons_area_init(wmWindowManager *wm, ARegion *ar)
363 {
364         wmKeyMap *keymap;
365         
366         ED_region_panels_init(wm, ar);
367
368         keymap= WM_keymap_find(wm->defaultconf, "GraphEdit Generic", SPACE_IPO, 0);
369         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
370 }
371
372 static void graph_buttons_area_draw(const bContext *C, ARegion *ar)
373 {
374         ED_region_panels(C, ar, 1, NULL, -1);
375 }
376
377 static void graph_region_listener(ARegion *ar, wmNotifier *wmn)
378 {
379         /* context changes */
380         switch(wmn->category) {
381                 case NC_ANIMATION:
382                         ED_region_tag_redraw(ar);
383                         break;
384                 case NC_SCENE:
385                         switch(wmn->data) {
386                                 case ND_RENDER_OPTIONS:
387                                 case ND_OB_ACTIVE:
388                                 case ND_FRAME:
389                                 case ND_MARKERS:
390                                         ED_region_tag_redraw(ar);
391                                         break;
392                         }
393                         break;
394                 case NC_OBJECT:
395                         switch(wmn->data) {
396                                 case ND_BONE_ACTIVE:
397                                 case ND_BONE_SELECT:
398                                 case ND_KEYS:
399                                         ED_region_tag_redraw(ar);
400                                         break;
401                                 case ND_MODIFIER:
402                                         if(wmn->action == NA_RENAME)
403                                                 ED_region_tag_redraw(ar);
404                                         break;
405                         }
406                         break;
407                 case NC_NODE:
408                         switch(wmn->action) {
409                                 case NA_EDITED:
410                                         ED_region_tag_redraw(ar);
411                                         break;
412                         }
413                         break;
414                 case NC_ID:
415                         if(wmn->action == NA_RENAME)
416                                 ED_region_tag_redraw(ar);
417                         break;
418                 default:
419                         if(wmn->data==ND_KEYS)
420                                 ED_region_tag_redraw(ar);
421                                 
422         }
423 }
424
425 /* editor level listener */
426 static void graph_listener(ScrArea *sa, wmNotifier *wmn)
427 {
428         /* context changes */
429         switch (wmn->category) {
430                 case NC_ANIMATION:
431                         ED_area_tag_refresh(sa);
432                         break;
433                 case NC_SCENE:
434                         /*switch (wmn->data) {
435                                 case ND_OB_ACTIVE:
436                                 case ND_OB_SELECT:
437                                         ED_area_tag_refresh(sa);
438                                         break;
439                         }*/
440                         ED_area_tag_refresh(sa);
441                         break;
442                 case NC_OBJECT:
443                         /*switch (wmn->data) {
444                                 case ND_BONE_SELECT:
445                                 case ND_BONE_ACTIVE:
446                                         ED_area_tag_refresh(sa);
447                                         break;
448                         }*/
449                         ED_area_tag_refresh(sa);
450                         break;
451                 case NC_SPACE:
452                         if(wmn->data == ND_SPACE_GRAPH)
453                                 ED_area_tag_redraw(sa);
454                         break;
455                 default:
456                         if(wmn->data==ND_KEYS)
457                                 ED_area_tag_refresh(sa);
458         }
459 }
460
461
462
463 static void graph_refresh(const bContext *C, ScrArea *sa)
464 {
465         SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
466         bAnimContext ac;
467         
468         /* updates to data needed depends on Graph Editor mode... */
469         switch (sipo->mode) {
470                 case SIPO_MODE_ANIMATION: /* all animation */
471                 {
472                         
473                 }
474                         break;
475                 
476                 case SIPO_MODE_DRIVERS: /* drivers only  */
477                 {
478                 
479                 }
480                         break; 
481         }
482         
483         /* region updates? */
484         // XXX resizing y-extents of tot should go here?
485         
486         /* init/adjust F-Curve colors */
487         if (ANIM_animdata_get_context(C, &ac)) {
488                 ListBase anim_data = {NULL, NULL};
489                 bAnimListElem *ale;
490                 int filter;
491                 int items, i;
492                 
493                 /* build list of F-Curves which will be visible as channels in channel-region
494                  *      - we don't include ANIMFILTER_CURVEVISIBLE filter, as that will result in a 
495                  *        mismatch between channel-colors and the drawn curves
496                  */
497                 filter= (ANIMFILTER_VISIBLE|ANIMFILTER_CURVESONLY);
498                 items= ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
499                 
500                 /* loop over F-Curves, assigning colors */
501                 for (ale=anim_data.first, i=0; ale; ale= ale->next, i++) {
502                         FCurve *fcu= (FCurve *)ale->data;
503                         
504                         /* set color of curve here */
505                         switch (fcu->color_mode) {
506                                 case FCURVE_COLOR_CUSTOM:
507                                         /* User has defined a custom color for this curve already (we assume it's not going to cause clashes with text colors),
508                                          * which should be left alone... Nothing needs to be done here.
509                                          */
510                                         break;
511                                         
512                                 case FCURVE_COLOR_AUTO_RGB:
513                                 {
514                                         /* F-Curve's array index is automatically mapped to RGB values. This works best of 3-value vectors. 
515                                          * TODO: find a way to module the hue so that not all curves have same color...
516                                          */
517                                         
518                                         /* standard table of colors to use */
519                                         const float _colorsets[4][3]= 
520                                         {
521                                                 {1.0f, 0.0f, 0.0f}, /* red */
522                                                 {0.0f, 1.0f, 0.0f}, /* green */
523                                                 {0.0f, 0.0f, 1.0f}, /* blue */
524                                                 {0.3f, 0.8f, 1.0f}, /* 'unknown' color - bluish so as to not conflict with handles */
525                                         };
526                                         
527                                         /* simply copy the relevant color over to the F-Curve */
528                                         if ((fcu->array_index >= 0) && (fcu->array_index < 3)) {
529                                                 /* if the index is within safe bounds, use index to access table */
530                                                 VECCOPY(fcu->color, _colorsets[fcu->array_index]);
531                                         }
532                                         else {
533                                                 /* use the 'unknown' color... */
534                                                 VECCOPY(fcu->color, _colorsets[3]);
535                                         }
536                                 }
537                                         break;
538                                 
539                                 case FCURVE_COLOR_AUTO_RAINBOW:
540                                 default:
541                                 {
542                                         /* determine color 'automatically' using 'magic function' which uses the given args
543                                          * of current item index + total items to determine some RGB color
544                                          */
545                                         ipo_rainbow(i, items, fcu->color);
546                                 }
547                                         break;
548                         }
549                 }
550                 
551                 /* free temp list */
552                 BLI_freelistN(&anim_data);
553         }
554 }
555
556 /* only called once, from space/spacetypes.c */
557 void ED_spacetype_ipo(void)
558 {
559         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype ipo");
560         ARegionType *art;
561         
562         st->spaceid= SPACE_IPO;
563         
564         st->new= graph_new;
565         st->free= graph_free;
566         st->init= graph_init;
567         st->duplicate= graph_duplicate;
568         st->operatortypes= graphedit_operatortypes;
569         st->keymap= graphedit_keymap;
570         st->listener= graph_listener;
571         st->refresh= graph_refresh;
572         
573         /* regions: main window */
574         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
575         art->regionid = RGN_TYPE_WINDOW;
576         art->init= graph_main_area_init;
577         art->draw= graph_main_area_draw;
578         art->listener= graph_region_listener;
579         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
580
581         BLI_addhead(&st->regiontypes, art);
582         
583         /* regions: header */
584         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
585         art->regionid = RGN_TYPE_HEADER;
586         art->minsizey= HEADERY;
587         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
588         art->listener= graph_region_listener;
589         art->init= graph_header_area_init;
590         art->draw= graph_header_area_draw;
591         
592         BLI_addhead(&st->regiontypes, art);
593         
594         /* regions: channels */
595         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
596         art->regionid = RGN_TYPE_CHANNELS;
597         art->minsizex= 200+V2D_SCROLL_WIDTH; /* 200 is the 'standard', but due to scrollers, we want a bit more to fit the lock icons in */
598         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
599         art->listener= graph_region_listener;
600         art->init= graph_channel_area_init;
601         art->draw= graph_channel_area_draw;
602         
603         BLI_addhead(&st->regiontypes, art);
604         
605         /* regions: UI buttons */
606         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
607         art->regionid = RGN_TYPE_UI;
608         art->minsizex= 200;
609         art->keymapflag= ED_KEYMAP_UI;
610         art->listener= graph_region_listener;
611         art->init= graph_buttons_area_init;
612         art->draw= graph_buttons_area_draw;
613         
614         BLI_addhead(&st->regiontypes, art);
615
616         graph_buttons_register(art);
617         
618         BKE_spacetype_register(st);
619 }
620