merge with 2.5 at r18679
[blender.git] / source / blender / editors / space_action / space_action.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_listBase.h"
33 #include "DNA_action_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
43 #include "BLI_rand.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48
49 #include "ED_space_api.h"
50 #include "ED_screen.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 "ED_anim_api.h"
62 #include "ED_keyframes_draw.h"
63 #include "ED_markers.h"
64
65 #include "action_intern.h"      // own include
66
67 /* ******************** default callbacks for action space ***************** */
68
69 static SpaceLink *action_new(const bContext *C)
70 {
71         ScrArea *sa= CTX_wm_area(C);
72         SpaceAction *saction;
73         ARegion *ar;
74         
75         saction= MEM_callocN(sizeof(SpaceAction), "initaction");
76         saction->spacetype= SPACE_ACTION;
77         saction->autosnap = SACTSNAP_FRAME;
78         
79         
80         /* header */
81         ar= MEM_callocN(sizeof(ARegion), "header for action");
82         
83         BLI_addtail(&saction->regionbase, ar);
84         ar->regiontype= RGN_TYPE_HEADER;
85         ar->alignment= RGN_ALIGN_BOTTOM;
86         
87         /* channel list region */
88         ar= MEM_callocN(sizeof(ARegion), "channel area for action");
89         BLI_addtail(&saction->regionbase, ar);
90         ar->regiontype= RGN_TYPE_CHANNELS;
91         ar->alignment= RGN_ALIGN_LEFT;
92         
93                 /* only need to set scroll settings, as this will use 'listview' v2d configuration */
94         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
95         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
96         
97         /* main area */
98         ar= MEM_callocN(sizeof(ARegion), "main area for action");
99         
100         BLI_addtail(&saction->regionbase, ar);
101         ar->regiontype= RGN_TYPE_WINDOW;
102         
103         ar->v2d.tot.xmin= -10.0f;
104         ar->v2d.tot.ymin= (float)(-sa->winy);
105         ar->v2d.tot.xmax= (float)(sa->winx);
106         ar->v2d.tot.ymax= 0.0f;
107         
108         ar->v2d.cur = ar->v2d.tot;
109         
110         ar->v2d.min[0]= 0.0f;
111         ar->v2d.min[1]= 0.0f;
112         
113         ar->v2d.max[0]= MAXFRAMEF;
114         ar->v2d.max[1]= 10000.0f;
115         
116         ar->v2d.minzoom= 0.01f;
117         ar->v2d.maxzoom= 50;
118         ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
119         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
120         ar->v2d.keepzoom= V2D_LOCKZOOM_Y;
121         ar->v2d.align= V2D_ALIGN_NO_POS_Y;
122         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
123         
124         return (SpaceLink *)saction;
125 }
126
127 /* not spacelink itself */
128 static void action_free(SpaceLink *sl)
129 {       
130 //      SpaceAction *saction= (SpaceAction*) sl;
131         
132 }
133
134
135 /* spacetype; init callback */
136 static void action_init(struct wmWindowManager *wm, ScrArea *sa)
137 {
138
139 }
140
141 static SpaceLink *action_duplicate(SpaceLink *sl)
142 {
143         SpaceAction *sactionn= MEM_dupallocN(sl);
144         
145         /* clear or remove stuff from old */
146         
147         return (SpaceLink *)sactionn;
148 }
149
150
151
152 /* add handlers, stuff you only do once or on area/region changes */
153 static void action_main_area_init(wmWindowManager *wm, ARegion *ar)
154 {
155         ListBase *keymap;
156         
157         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
158         
159         /* own keymap */
160         keymap= WM_keymap_listbase(wm, "Action_Keys", SPACE_ACTION, 0); /* XXX weak? */
161         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
162 }
163
164 static void action_main_area_draw(const bContext *C, ARegion *ar)
165 {
166         /* draw entirely, view changes should be handled here */
167         SpaceAction *saction= (SpaceAction*)CTX_wm_space_data(C);
168         bAnimContext ac;
169         View2D *v2d= &ar->v2d;
170         View2DGrid *grid;
171         View2DScrollers *scrollers;
172         float col[3];
173         short unit=0, flag=0;
174         
175         /* clear and setup matrix */
176         UI_GetThemeColor3fv(TH_BACK, col);
177         glClearColor(col[0], col[1], col[2], 0.0);
178         glClear(GL_COLOR_BUFFER_BIT);
179         
180         UI_view2d_view_ortho(C, v2d);
181         
182         /* time grid */
183         unit= (saction->flag & SACTION_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
184         grid= UI_view2d_grid_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
185         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
186         UI_view2d_grid_free(grid);
187         
188         /* data */
189         if (ANIM_animdata_get_context(C, &ac)) {
190                 draw_channel_strips(&ac, saction, ar);
191         }
192         
193         /* current frame */
194         if (saction->flag & SACTION_DRAWTIME)   flag |= DRAWCFRA_UNIT_SECONDS;
195         if ((saction->flag & SACTION_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
196         ANIM_draw_cfra(C, v2d, flag);
197         
198         /* markers */
199         UI_view2d_view_orthoSpecial(C, v2d, 1);
200         draw_markers_time(C, 0);
201         
202         /* preview range */
203         UI_view2d_view_ortho(C, v2d);
204         ANIM_draw_previewrange(C, v2d);
205         
206         /* reset view matrix */
207         UI_view2d_view_restore(C);
208         
209         /* scrollers */
210         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
211         UI_view2d_scrollers_draw(C, v2d, scrollers);
212         UI_view2d_scrollers_free(scrollers);
213 }
214
215 /* add handlers, stuff you only do once or on area/region changes */
216 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
217 {
218         ListBase *keymap;
219         
220         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
221         
222         /* own keymap */
223         keymap= WM_keymap_listbase(wm, "Animation_Channels", 0, 0);     /* XXX weak? */
224         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
225 }
226
227 static void action_channel_area_draw(const bContext *C, ARegion *ar)
228 {
229         /* draw entirely, view changes should be handled here */
230         SpaceAction *saction= (SpaceAction*)CTX_wm_space_data(C);
231         bAnimContext ac;
232         View2D *v2d= &ar->v2d;
233         View2DScrollers *scrollers;
234         float col[3];
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(C, v2d);
242         
243         /* data */
244         if (ANIM_animdata_get_context(C, &ac)) {
245                 draw_channel_names(&ac, saction, ar);
246         }
247         
248         /* reset view matrix */
249         UI_view2d_view_restore(C);
250         
251         /* scrollers */
252         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
253         UI_view2d_scrollers_draw(C, v2d, scrollers);
254         UI_view2d_scrollers_free(scrollers);
255 }
256
257
258 /* add handlers, stuff you only do once or on area/region changes */
259 static void action_header_area_init(wmWindowManager *wm, ARegion *ar)
260 {
261         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
262 }
263
264 static void action_header_area_draw(const bContext *C, ARegion *ar)
265 {
266         float col[3];
267         
268         /* clear */
269         if(ED_screen_area_active(C))
270                 UI_GetThemeColor3fv(TH_HEADER, col);
271         else
272                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
273         
274         glClearColor(col[0], col[1], col[2], 0.0);
275         glClear(GL_COLOR_BUFFER_BIT);
276         
277         /* set view2d view matrix for scrolling (without scrollers) */
278         UI_view2d_view_ortho(C, &ar->v2d);
279         
280         action_header_buttons(C, ar);
281         
282         /* restore view matrix? */
283         UI_view2d_view_restore(C);
284 }
285
286 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
287 {
288         /* context changes */
289         switch(wmn->category) {
290                 case NC_SCENE:
291                         switch(wmn->data) {
292                                 case ND_OB_ACTIVE:
293                                         ED_region_tag_redraw(ar);
294                                         break;
295                         }
296                         break;
297                 case NC_OBJECT:
298                         switch(wmn->data) {
299                                 case ND_BONE_ACTIVE:
300                                 case ND_BONE_SELECT:
301                                         ED_region_tag_redraw(ar);
302                                         break;
303                         }
304                         break;
305         }
306 }
307
308 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
309 {
310         /* context changes */
311         switch(wmn->category) {
312                 case NC_SCENE:
313                         switch(wmn->data) {
314                                 case ND_OB_ACTIVE:
315                                 case ND_FRAME:
316                                 case ND_MARKERS:
317                                         ED_region_tag_redraw(ar);
318                                         break;
319                         }
320                         break;
321                 case NC_OBJECT:
322                         switch(wmn->data) {
323                                 case ND_BONE_ACTIVE:
324                                 case ND_BONE_SELECT:
325                                         ED_region_tag_redraw(ar);
326                                         break;
327                         }
328                         break;
329         }
330 }
331
332 /* editor level listener */
333 static void action_listener(ScrArea *sa, wmNotifier *wmn)
334 {
335         /* context changes */
336         switch (wmn->category) {
337                 case NC_SCENE:
338                         /*switch (wmn->data) {
339                                 case ND_OB_ACTIVE:
340                                 case ND_OB_SELECT:
341                                         ED_area_tag_refresh(sa);
342                                         break;
343                         }*/
344                         ED_area_tag_refresh(sa);
345                         break;
346                 case NC_OBJECT:
347                         /*switch (wmn->data) {
348                                 case ND_BONE_SELECT:
349                                 case ND_BONE_ACTIVE:
350                                         ED_area_tag_refresh(sa);
351                                         break;
352                         }*/
353                         ED_area_tag_refresh(sa);
354                         break;
355         }
356 }
357
358 static void action_refresh(const bContext *C, ScrArea *sa)
359 {
360         SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
361         
362         /* updates to data needed depends on Action Editor mode... */
363         switch (saction->mode) {
364                 case SACTCONT_DOPESHEET: /* DopeSheet - for now, just all armatures... */
365                 {
366                         
367                 }
368                         break;
369                 
370                 case SACTCONT_ACTION: /* Action Editor - just active object will do */
371                 {
372                         Object *ob= CTX_data_active_object(C);
373                         
374                         /* sync changes to bones to the corresponding action channels */
375                         ANIM_pose_to_action_sync(ob, sa);
376                 }
377                         break; 
378         }
379         
380         /* region updates? */
381         // XXX resizing y-extents of tot should go here?
382 }
383
384 /* only called once, from space/spacetypes.c */
385 void ED_spacetype_action(void)
386 {
387         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
388         ARegionType *art;
389         
390         st->spaceid= SPACE_ACTION;
391         
392         st->new= action_new;
393         st->free= action_free;
394         st->init= action_init;
395         st->duplicate= action_duplicate;
396         st->operatortypes= action_operatortypes;
397         st->keymap= action_keymap;
398         st->listener= action_listener;
399         st->refresh= action_refresh;
400         
401         /* regions: main window */
402         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
403         art->regionid = RGN_TYPE_WINDOW;
404         art->init= action_main_area_init;
405         art->draw= action_main_area_draw;
406         art->listener= action_main_area_listener;
407         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
408
409         BLI_addhead(&st->regiontypes, art);
410         
411         /* regions: header */
412         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
413         art->regionid = RGN_TYPE_HEADER;
414         art->minsizey= HEADERY;
415         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
416         
417         art->init= action_header_area_init;
418         art->draw= action_header_area_draw;
419         
420         BLI_addhead(&st->regiontypes, art);
421         
422         /* regions: channels */
423         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
424         art->regionid = RGN_TYPE_CHANNELS;
425         art->minsizex= 200;
426         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
427         
428         art->init= action_channel_area_init;
429         art->draw= action_channel_area_draw;
430         art->listener= action_channel_area_listener;
431         
432         BLI_addhead(&st->regiontypes, art);
433         
434         
435         BKE_spacetype_register(st);
436 }
437