2.5 - Assorted View2D-Related Cleanups
[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_arithb.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_context.h"
45 #include "BKE_screen.h"
46
47 #include "ED_space_api.h"
48 #include "ED_screen.h"
49 #include "ED_anim_api.h"
50 #include "ED_markers.h"
51
52 #include "BIF_gl.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59 #include "UI_view2d.h"
60
61 #include "graph_intern.h"       // own include
62
63 /* ******************** default callbacks for ipo space ***************** */
64
65 static SpaceLink *graph_new(const bContext *C)
66 {
67         Scene *scene= CTX_data_scene(C);
68         ARegion *ar;
69         SpaceIpo *sipo;
70         
71         /* Graph Editor - general stuff */
72         sipo= MEM_callocN(sizeof(SpaceIpo), "init graphedit");
73         sipo->spacetype= SPACE_IPO;
74         
75         sipo->autosnap= SACTSNAP_FRAME;
76         
77         /* allocate DopeSheet data for Graph Editor */
78         sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
79         
80         /* header */
81         ar= MEM_callocN(sizeof(ARegion), "header for graphedit");
82         
83         BLI_addtail(&sipo->regionbase, ar);
84         ar->regiontype= RGN_TYPE_HEADER;
85         ar->alignment= RGN_ALIGN_BOTTOM;
86         
87         /* channels */
88         ar= MEM_callocN(sizeof(ARegion), "main area for graphedit");
89         
90         BLI_addtail(&sipo->regionbase, ar);
91         ar->regiontype= RGN_TYPE_CHANNELS;
92         ar->alignment= RGN_ALIGN_LEFT;
93         
94         ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
95         
96         /* main area */
97         ar= MEM_callocN(sizeof(ARegion), "main area for graphedit");
98         
99         BLI_addtail(&sipo->regionbase, ar);
100         ar->regiontype= RGN_TYPE_WINDOW;
101         
102         ar->v2d.tot.xmin= 0.0f;
103         ar->v2d.tot.ymin= -10.0f;
104         ar->v2d.tot.xmax= (float)scene->r.efra;
105         ar->v2d.tot.ymax= 10.0f;
106         
107         ar->v2d.cur= ar->v2d.tot;
108         
109         ar->v2d.min[0]= 0.01f;
110         ar->v2d.min[1]= 0.01f;
111         
112         ar->v2d.max[0]= MAXFRAMEF;
113         ar->v2d.max[1]= 50000.0f;
114         
115         ar->v2d.scroll= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
116         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
117         
118         ar->v2d.keeptot= 0;
119         
120         return (SpaceLink *)sipo;
121 }
122
123 /* not spacelink itself */
124 static void graph_free(SpaceLink *sl)
125 {       
126         SpaceIpo *si= (SpaceIpo *)sl;
127         
128         if (si->ads) {
129                 BLI_freelistN(&si->ads->chanbase);
130                 MEM_freeN(si->ads);
131         }
132 }
133
134
135 /* spacetype; init callback */
136 static void graph_init(struct wmWindowManager *wm, ScrArea *sa)
137 {
138         SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
139         
140         /* init dopesheet data if non-existant (i.e. for old files) */
141         if (sipo->ads == NULL)
142                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
143 }
144
145 static SpaceLink *graph_duplicate(SpaceLink *sl)
146 {
147         SpaceIpo *sipon= MEM_dupallocN(sl);
148         
149         /* clear or remove stuff from old */
150         //sipon->ipokey.first= sipon->ipokey.last= NULL;
151         sipon->ads= MEM_dupallocN(sipon->ads);
152         
153         return (SpaceLink *)sipon;
154 }
155
156 /* add handlers, stuff you only do once or on area/region changes */
157 static void graph_main_area_init(wmWindowManager *wm, ARegion *ar)
158 {
159         ListBase *keymap;
160         
161         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
162         
163         /* own keymap */
164         keymap= WM_keymap_listbase(wm, "GraphEdit Keys", SPACE_IPO, 0); /* XXX weak? */
165         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
166 }
167
168 static void graph_main_area_draw(const bContext *C, ARegion *ar)
169 {
170         /* draw entirely, view changes should be handled here */
171         SpaceIpo *sipo= (SpaceIpo*)CTX_wm_space_data(C);
172         bAnimContext ac;
173         View2D *v2d= &ar->v2d;
174         View2DGrid *grid;
175         View2DScrollers *scrollers;
176         float col[3];
177         short unitx=0, unity=V2D_UNIT_VALUES, flag=0;
178         
179         /* clear and setup matrix */
180         UI_GetThemeColor3fv(TH_BACK, col);
181         glClearColor(col[0], col[1], col[2], 0.0);
182         glClear(GL_COLOR_BUFFER_BIT);
183         
184         UI_view2d_view_ortho(C, v2d);
185         
186         /* grid */
187         unitx= (sipo->flag & SIPO_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
188         grid= UI_view2d_grid_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, ar->winx, ar->winy);
189         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
190         UI_view2d_grid_free(grid);
191         
192         /* draw data */
193         if (ANIM_animdata_get_context(C, &ac)) {
194                 graph_draw_curves(&ac, sipo, ar);
195         }
196         
197         /* current frame */
198         if (sipo->flag & SIPO_DRAWTIME)         flag |= DRAWCFRA_UNIT_SECONDS;
199         if ((sipo->flag & SIPO_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
200         ANIM_draw_cfra(C, v2d, flag);
201         
202         /* markers */
203         UI_view2d_view_orthoSpecial(C, v2d, 1);
204         draw_markers_time(C, 0);
205         
206         /* preview range */
207         UI_view2d_view_ortho(C, v2d);
208         ANIM_draw_previewrange(C, v2d);
209         
210         /* reset view matrix */
211         UI_view2d_view_restore(C);
212         
213         /* scrollers */
214                 // FIXME: args for scrollers depend on the type of data being shown...
215         scrollers= UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
216         UI_view2d_scrollers_draw(C, v2d, scrollers);
217         UI_view2d_scrollers_free(scrollers);
218 }
219
220 static void graph_channel_area_init(wmWindowManager *wm, ARegion *ar)
221 {
222         ListBase *keymap;
223         
224         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
225         
226         /* own keymap */
227         keymap= WM_keymap_listbase(wm, "Animation_Channels", 0, 0);     /* XXX weak? */
228         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
229 }
230
231 static void graph_channel_area_draw(const bContext *C, ARegion *ar)
232 {
233         SpaceIpo *sipo= (SpaceIpo *)CTX_wm_space_data(C);
234         bAnimContext ac;
235         View2D *v2d= &ar->v2d;
236         View2DScrollers *scrollers;
237         float col[3];
238         
239         /* clear and setup matrix */
240         UI_GetThemeColor3fv(TH_SHADE2, col);
241         glClearColor(col[0], col[1], col[2], 0.0);
242         glClear(GL_COLOR_BUFFER_BIT);
243         
244         UI_view2d_view_ortho(C, v2d);
245         
246         /* draw channels */
247         if (ANIM_animdata_get_context(C, &ac)) {
248                 graph_draw_channel_names(&ac, sipo, ar);
249         }
250         
251         /* reset view matrix */
252         UI_view2d_view_restore(C);
253         
254         /* scrollers */
255         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
256         UI_view2d_scrollers_draw(C, v2d, scrollers);
257         UI_view2d_scrollers_free(scrollers);
258 }
259
260 /* add handlers, stuff you only do once or on area/region changes */
261 static void graph_header_area_init(wmWindowManager *wm, ARegion *ar)
262 {
263         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
264 }
265
266 static void graph_header_area_draw(const bContext *C, ARegion *ar)
267 {
268         float col[3];
269         
270         /* clear */
271         if(ED_screen_area_active(C))
272                 UI_GetThemeColor3fv(TH_HEADER, col);
273         else
274                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
275         
276         glClearColor(col[0], col[1], col[2], 0.0);
277         glClear(GL_COLOR_BUFFER_BIT);
278         
279         /* set view2d view matrix for scrolling (without scrollers) */
280         UI_view2d_view_ortho(C, &ar->v2d);
281         
282         graph_header_buttons(C, ar);
283         
284         /* restore view matrix? */
285         UI_view2d_view_restore(C);
286 }
287
288 static void graph_region_listener(ARegion *ar, wmNotifier *wmn)
289 {
290         /* context changes */
291         switch(wmn->category) {
292                 case NC_SCENE:
293                         switch(wmn->data) {
294                                 case ND_OB_ACTIVE:
295                                 case ND_FRAME:
296                                 case ND_MARKERS:
297                                         ED_region_tag_redraw(ar);
298                                         break;
299                         }
300                         break;
301                 case NC_OBJECT:
302                         switch(wmn->data) {
303                                 case ND_BONE_ACTIVE:
304                                 case ND_BONE_SELECT:
305                                 case ND_KEYS:
306                                         ED_region_tag_redraw(ar);
307                                         break;
308                         }
309                         break;
310                 default:
311                         if(wmn->data==ND_KEYS)
312                                 ED_region_tag_redraw(ar);
313                                 
314         }
315 }
316
317 /* editor level listener */
318 static void graph_listener(ScrArea *sa, wmNotifier *wmn)
319 {
320         /* context changes */
321         switch (wmn->category) {
322                 case NC_SCENE:
323                         /*switch (wmn->data) {
324                                 case ND_OB_ACTIVE:
325                                 case ND_OB_SELECT:
326                                         ED_area_tag_refresh(sa);
327                                         break;
328                         }*/
329                         ED_area_tag_refresh(sa);
330                         break;
331                 case NC_OBJECT:
332                         /*switch (wmn->data) {
333                                 case ND_BONE_SELECT:
334                                 case ND_BONE_ACTIVE:
335                                         ED_area_tag_refresh(sa);
336                                         break;
337                         }*/
338                         ED_area_tag_refresh(sa);
339                         break;
340         }
341 }
342
343 static void graph_refresh(const bContext *C, ScrArea *sa)
344 {
345         SpaceIpo *sipo = (SpaceIpo *)sa->spacedata.first;
346         
347         /* updates to data needed depends on Graph Editor mode... */
348         switch (sipo->mode) {
349                 case SIPO_MODE_ANIMATION: /* all animation */
350                 {
351                         
352                 }
353                         break;
354                 
355                 case SIPO_MODE_DRIVERS: /* drivers only  */
356                 {
357                         Object *ob= CTX_data_active_object(C);
358                         
359                         /* sync changes to bones to the corresponding action channels */
360                         ANIM_pose_to_action_sync(ob, sa);
361                 }
362                         break; 
363         }
364         
365         /* region updates? */
366         // XXX resizing y-extents of tot should go here?
367 }
368
369 /* only called once, from space/spacetypes.c */
370 void ED_spacetype_ipo(void)
371 {
372         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype ipo");
373         ARegionType *art;
374         
375         st->spaceid= SPACE_IPO;
376         
377         st->new= graph_new;
378         st->free= graph_free;
379         st->init= graph_init;
380         st->duplicate= graph_duplicate;
381         st->operatortypes= graphedit_operatortypes;
382         st->keymap= graphedit_keymap;
383         st->listener= graph_listener;
384         st->refresh= graph_refresh;
385         
386         /* regions: main window */
387         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
388         art->regionid = RGN_TYPE_WINDOW;
389         art->init= graph_main_area_init;
390         art->draw= graph_main_area_draw;
391         art->listener= graph_region_listener;
392         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
393
394         BLI_addhead(&st->regiontypes, art);
395         
396         /* regions: header */
397         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
398         art->regionid = RGN_TYPE_HEADER;
399         art->minsizey= HEADERY;
400         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
401         art->listener= graph_region_listener;
402         art->init= graph_header_area_init;
403         art->draw= graph_header_area_draw;
404         
405         BLI_addhead(&st->regiontypes, art);
406         
407         /* regions: channels */
408         art= MEM_callocN(sizeof(ARegionType), "spacetype graphedit region");
409         art->regionid = RGN_TYPE_CHANNELS;
410         art->minsizex= 214; /* 200 is the 'standard', but due to scrollers, we want a bit more to fit the lock icons in */
411         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
412         art->listener= graph_region_listener;
413         art->init= graph_channel_area_init;
414         art->draw= graph_channel_area_draw;
415         
416         BLI_addhead(&st->regiontypes, art);
417         
418         
419         BKE_spacetype_register(st);
420 }
421