fec2e28fa0e6b9a8a4a62bb9d4e11ac29a612aff
[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         ARegion *ar;
72         SpaceAction *saction;
73         
74         saction= MEM_callocN(sizeof(SpaceAction), "initaction");
75         saction->spacetype= SPACE_ACTION;
76         saction->autosnap = SACTSNAP_FRAME;
77         
78         
79         /* header */
80         ar= MEM_callocN(sizeof(ARegion), "header for action");
81         
82         BLI_addtail(&saction->regionbase, ar);
83         ar->regiontype= RGN_TYPE_HEADER;
84         ar->alignment= RGN_ALIGN_BOTTOM;
85         
86         /* channel list region */
87         ar= MEM_callocN(sizeof(ARegion), "channel area for action");
88         BLI_addtail(&saction->regionbase, ar);
89         ar->regiontype= RGN_TYPE_CHANNELS;
90         ar->alignment= RGN_ALIGN_LEFT;
91         
92                 /* only need to set scroll settings, as this will use 'listview' v2d configuration */
93         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
94         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
95         
96         /* main area */
97         ar= MEM_callocN(sizeof(ARegion), "main area for action");
98         
99         BLI_addtail(&saction->regionbase, ar);
100         ar->regiontype= RGN_TYPE_WINDOW;
101         
102         ar->v2d.tot.xmin= -2.0f;
103         ar->v2d.tot.ymin= -2000.0f;
104         ar->v2d.tot.xmax= 100.0f;
105         ar->v2d.tot.ymax= 0.0f;
106         
107         ar->v2d.cur.xmin= -2.0f;
108         ar->v2d.cur.ymin= -2000.0f; /* ideally this would be the size of the region */
109         ar->v2d.cur.xmax= 100.0f;
110         ar->v2d.cur.ymax= 0.0f;
111         
112         ar->v2d.min[0]= 0.0f;
113         ar->v2d.min[1]= 0.0f;
114         
115         ar->v2d.max[0]= MAXFRAMEF;
116         ar->v2d.max[1]= 2000.0f;
117         
118         ar->v2d.minzoom= 0.01f;
119         ar->v2d.maxzoom= 50;
120         ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
121         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
122         ar->v2d.keepzoom= V2D_LOCKZOOM_Y;
123         ar->v2d.align= V2D_ALIGN_NO_POS_Y;
124         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
125         
126         return (SpaceLink *)saction;
127 }
128
129 /* not spacelink itself */
130 static void action_free(SpaceLink *sl)
131 {       
132 //      SpaceAction *saction= (SpaceAction*) sl;
133         
134 }
135
136
137 /* spacetype; init callback */
138 static void action_init(struct wmWindowManager *wm, ScrArea *sa)
139 {
140
141 }
142
143 static SpaceLink *action_duplicate(SpaceLink *sl)
144 {
145         SpaceAction *sactionn= MEM_dupallocN(sl);
146         
147         /* clear or remove stuff from old */
148         
149         return (SpaceLink *)sactionn;
150 }
151
152
153
154 /* add handlers, stuff you only do once or on area/region changes */
155 static void action_main_area_init(wmWindowManager *wm, ARegion *ar)
156 {
157         ListBase *keymap;
158         
159         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
160         
161         /* own keymap */
162         keymap= WM_keymap_listbase(wm, "Action_Keys", SPACE_ACTION, 0); /* XXX weak? */
163         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
164 }
165
166 static void action_main_area_draw(const bContext *C, ARegion *ar)
167 {
168         /* draw entirely, view changes should be handled here */
169         SpaceAction *saction= (SpaceAction*)CTX_wm_space_data(C);
170         bAnimContext ac;
171         View2D *v2d= &ar->v2d;
172         View2DGrid *grid;
173         View2DScrollers *scrollers;
174         float col[3];
175         short unit=0, flag=0;
176         
177         /* clear and setup matrix */
178         UI_GetThemeColor3fv(TH_BACK, col);
179         glClearColor(col[0], col[1], col[2], 0.0);
180         glClear(GL_COLOR_BUFFER_BIT);
181         
182         UI_view2d_view_ortho(C, v2d);
183         
184         /* time grid */
185         unit= (saction->flag & SACTION_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
186         grid= UI_view2d_grid_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
187         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
188         UI_view2d_grid_free(grid);
189         
190         /* data */
191         if (ANIM_animdata_get_context(C, &ac)) {
192                 draw_channel_strips(&ac, saction, ar);
193         }
194         
195         /* current frame */
196         if (saction->flag & SACTION_DRAWTIME)   flag |= DRAWCFRA_UNIT_SECONDS;
197         if ((saction->flag & SACTION_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
198         ANIM_draw_cfra(C, v2d, flag);
199         
200         /* markers */
201         UI_view2d_view_orthoSpecial(C, v2d, 1);
202         draw_markers_time(C, 0);
203         
204         /* preview range */
205         UI_view2d_view_ortho(C, v2d);
206         ANIM_draw_previewrange(C, v2d);
207         
208         /* reset view matrix */
209         UI_view2d_view_restore(C);
210         
211         /* scrollers */
212         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
213         UI_view2d_scrollers_draw(C, v2d, scrollers);
214         UI_view2d_scrollers_free(scrollers);
215 }
216
217 /* add handlers, stuff you only do once or on area/region changes */
218 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
219 {
220         ListBase *keymap;
221         
222         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
223         
224         /* own keymap */
225         keymap= WM_keymap_listbase(wm, "Action_Channels", SPACE_ACTION, 0);     /* XXX weak? */
226         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
227 }
228
229 static void action_channel_area_draw(const bContext *C, ARegion *ar)
230 {
231         /* draw entirely, view changes should be handled here */
232         SpaceAction *saction= (SpaceAction*)CTX_wm_space_data(C);
233         bAnimContext ac;
234         View2D *v2d= &ar->v2d;
235         View2DScrollers *scrollers;
236         float col[3];
237         
238         /* clear and setup matrix */
239         UI_GetThemeColor3fv(TH_BACK, col);
240         glClearColor(col[0], col[1], col[2], 0.0);
241         glClear(GL_COLOR_BUFFER_BIT);
242         
243         UI_view2d_view_ortho(C, v2d);
244         
245         /* data */
246         if (ANIM_animdata_get_context(C, &ac)) {
247                 draw_channel_names(&ac, saction, ar);
248         }
249         
250         /* reset view matrix */
251         UI_view2d_view_restore(C);
252         
253         /* scrollers */
254         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
255         UI_view2d_scrollers_draw(C, v2d, scrollers);
256         UI_view2d_scrollers_free(scrollers);
257 }
258
259
260 /* add handlers, stuff you only do once or on area/region changes */
261 static void action_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 action_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         action_header_buttons(C, ar);
283         
284         /* restore view matrix? */
285         UI_view2d_view_restore(C);
286 }
287
288 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
289 {
290         /* context changes */
291         switch(wmn->type) {
292                 case WM_NOTE_MARKERS_CHANGED:
293                         ED_region_tag_redraw(ar);
294                         break;
295         }
296 }
297
298 /* only called once, from space/spacetypes.c */
299 void ED_spacetype_action(void)
300 {
301         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
302         ARegionType *art;
303         
304         st->spaceid= SPACE_ACTION;
305         
306         st->new= action_new;
307         st->free= action_free;
308         st->init= action_init;
309         st->duplicate= action_duplicate;
310         st->operatortypes= action_operatortypes;
311         st->keymap= action_keymap;
312         
313         /* regions: main window */
314         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
315         art->regionid = RGN_TYPE_WINDOW;
316         art->init= action_main_area_init;
317         art->draw= action_main_area_draw;
318         art->listener= action_main_area_listener;
319         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION;
320
321         BLI_addhead(&st->regiontypes, art);
322         
323         /* regions: header */
324         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
325         art->regionid = RGN_TYPE_HEADER;
326         art->minsizey= HEADERY;
327         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
328         
329         art->init= action_header_area_init;
330         art->draw= action_header_area_draw;
331         
332         BLI_addhead(&st->regiontypes, art);
333         
334         /* regions: channels */
335         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
336         art->regionid = RGN_TYPE_CHANNELS;
337         art->minsizex= 200;
338         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
339         
340         art->init= action_channel_area_init;
341         art->draw= action_channel_area_draw;
342         
343         BLI_addhead(&st->regiontypes, art);
344         
345         
346         BKE_spacetype_register(st);
347 }
348