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