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