2.5 - Assorted comments, and made the default size of the Action Editor more sensible.
[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= -2.0f;
104         ar->v2d.tot.ymin= (float)(-sa->winy);
105         ar->v2d.tot.xmax= 100.0f;
106         ar->v2d.tot.ymax= 0.0f;
107         
108         ar->v2d.cur.xmin= -2.0f;
109         ar->v2d.cur.ymin= (float)(-sa->winy);
110         ar->v2d.cur.xmax= 100.0f;
111         ar->v2d.cur.ymax= 0.0f;
112         
113         ar->v2d.min[0]= 0.0f;
114         ar->v2d.min[1]= 0.0f;
115         
116         ar->v2d.max[0]= MAXFRAMEF;
117         ar->v2d.max[1]= 2000.0f;
118         
119         ar->v2d.minzoom= 0.01f;
120         ar->v2d.maxzoom= 50;
121         ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
122         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
123         ar->v2d.keepzoom= V2D_LOCKZOOM_Y;
124         ar->v2d.align= V2D_ALIGN_NO_POS_Y;
125         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
126         
127         return (SpaceLink *)saction;
128 }
129
130 /* not spacelink itself */
131 static void action_free(SpaceLink *sl)
132 {       
133 //      SpaceAction *saction= (SpaceAction*) sl;
134         
135 }
136
137
138 /* spacetype; init callback */
139 static void action_init(struct wmWindowManager *wm, ScrArea *sa)
140 {
141
142 }
143
144 static SpaceLink *action_duplicate(SpaceLink *sl)
145 {
146         SpaceAction *sactionn= MEM_dupallocN(sl);
147         
148         /* clear or remove stuff from old */
149         
150         return (SpaceLink *)sactionn;
151 }
152
153
154
155 /* add handlers, stuff you only do once or on area/region changes */
156 static void action_main_area_init(wmWindowManager *wm, ARegion *ar)
157 {
158         ListBase *keymap;
159         
160         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
161         
162         /* own keymap */
163         keymap= WM_keymap_listbase(wm, "Action_Keys", SPACE_ACTION, 0); /* XXX weak? */
164         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
165 }
166
167 static void action_main_area_draw(const bContext *C, ARegion *ar)
168 {
169         /* draw entirely, view changes should be handled here */
170         SpaceAction *saction= (SpaceAction*)CTX_wm_space_data(C);
171         bAnimContext ac;
172         View2D *v2d= &ar->v2d;
173         View2DGrid *grid;
174         View2DScrollers *scrollers;
175         float col[3];
176         short unit=0, flag=0;
177         
178         /* clear and setup matrix */
179         UI_GetThemeColor3fv(TH_BACK, col);
180         glClearColor(col[0], col[1], col[2], 0.0);
181         glClear(GL_COLOR_BUFFER_BIT);
182         
183         UI_view2d_view_ortho(C, v2d);
184         
185         /* time grid */
186         unit= (saction->flag & SACTION_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
187         grid= UI_view2d_grid_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
188         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
189         UI_view2d_grid_free(grid);
190         
191         /* data */
192         if (ANIM_animdata_get_context(C, &ac)) {
193                 draw_channel_strips(&ac, saction, ar);
194         }
195         
196         /* current frame */
197         if (saction->flag & SACTION_DRAWTIME)   flag |= DRAWCFRA_UNIT_SECONDS;
198         if ((saction->flag & SACTION_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
199         ANIM_draw_cfra(C, v2d, flag);
200         
201         /* markers */
202         UI_view2d_view_orthoSpecial(C, v2d, 1);
203         draw_markers_time(C, 0);
204         
205         /* preview range */
206         UI_view2d_view_ortho(C, v2d);
207         ANIM_draw_previewrange(C, v2d);
208         
209         /* reset view matrix */
210         UI_view2d_view_restore(C);
211         
212         /* scrollers */
213         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
214         UI_view2d_scrollers_draw(C, v2d, scrollers);
215         UI_view2d_scrollers_free(scrollers);
216 }
217
218 /* add handlers, stuff you only do once or on area/region changes */
219 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
220 {
221         ListBase *keymap;
222         
223         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
224         
225         /* own keymap */
226         keymap= WM_keymap_listbase(wm, "Action_Channels", SPACE_ACTION, 0);     /* XXX weak? */
227         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
228 }
229
230 static void action_channel_area_draw(const bContext *C, ARegion *ar)
231 {
232         /* draw entirely, view changes should be handled here */
233         SpaceAction *saction= (SpaceAction*)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_BACK, 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         /* data */
247         if (ANIM_animdata_get_context(C, &ac)) {
248                 draw_channel_names(&ac, saction, 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
261 /* add handlers, stuff you only do once or on area/region changes */
262 static void action_header_area_init(wmWindowManager *wm, ARegion *ar)
263 {
264         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
265 }
266
267 static void action_header_area_draw(const bContext *C, ARegion *ar)
268 {
269         float col[3];
270         
271         /* clear */
272         if(ED_screen_area_active(C))
273                 UI_GetThemeColor3fv(TH_HEADER, col);
274         else
275                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
276         
277         glClearColor(col[0], col[1], col[2], 0.0);
278         glClear(GL_COLOR_BUFFER_BIT);
279         
280         /* set view2d view matrix for scrolling (without scrollers) */
281         UI_view2d_view_ortho(C, &ar->v2d);
282         
283         action_header_buttons(C, ar);
284         
285         /* restore view matrix? */
286         UI_view2d_view_restore(C);
287 }
288
289 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
290 {
291         /* context changes */
292         switch(wmn->type) {
293                 case WM_NOTE_MARKERS_CHANGED:
294                         ED_region_tag_redraw(ar);
295                         break;
296         }
297 }
298
299 /* only called once, from space/spacetypes.c */
300 void ED_spacetype_action(void)
301 {
302         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
303         ARegionType *art;
304         
305         st->spaceid= SPACE_ACTION;
306         
307         st->new= action_new;
308         st->free= action_free;
309         st->init= action_init;
310         st->duplicate= action_duplicate;
311         st->operatortypes= action_operatortypes;
312         st->keymap= action_keymap;
313         
314         /* regions: main window */
315         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
316         art->regionid = RGN_TYPE_WINDOW;
317         art->init= action_main_area_init;
318         art->draw= action_main_area_draw;
319         art->listener= action_main_area_listener;
320         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION;
321
322         BLI_addhead(&st->regiontypes, art);
323         
324         /* regions: header */
325         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
326         art->regionid = RGN_TYPE_HEADER;
327         art->minsizey= HEADERY;
328         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
329         
330         art->init= action_header_area_init;
331         art->draw= action_header_area_draw;
332         
333         BLI_addhead(&st->regiontypes, art);
334         
335         /* regions: channels */
336         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
337         art->regionid = RGN_TYPE_CHANNELS;
338         art->minsizex= 200;
339         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
340         
341         art->init= action_channel_area_init;
342         art->draw= action_channel_area_draw;
343         
344         BLI_addhead(&st->regiontypes, art);
345         
346         
347         BKE_spacetype_register(st);
348 }
349