2.5 - Animation Editors - common drawing stuff
[blender-staging.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_action_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_colortools.h"
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
47
48 #include "ED_space_api.h"
49 #include "ED_screen.h"
50
51 #include "BIF_gl.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58 #include "UI_view2d.h"
59
60 #include "ED_anim_api.h"
61 #include "ED_markers.h"
62
63 #include "action_intern.h"      // own include
64
65 /* ******************** default callbacks for action space ***************** */
66
67 static SpaceLink *action_new(void)
68 {
69         ARegion *ar;
70         SpaceAction *saction;
71         
72         saction= MEM_callocN(sizeof(SpaceAction), "initaction");
73         saction->spacetype= SPACE_ACTION;
74         saction->autosnap = SACTSNAP_FRAME;
75         
76         
77         /* header */
78         ar= MEM_callocN(sizeof(ARegion), "header for action");
79         
80         BLI_addtail(&saction->regionbase, ar);
81         ar->regiontype= RGN_TYPE_HEADER;
82         ar->alignment= RGN_ALIGN_BOTTOM;
83         
84         /* channel list region */
85         ar= MEM_callocN(sizeof(ARegion), "channel area for action");
86         BLI_addtail(&saction->regionbase, ar);
87         ar->regiontype= RGN_TYPE_CHANNELS;
88         ar->alignment= RGN_ALIGN_LEFT;
89         
90                 /* only need to set scroll settings, as this will use 'listview' v2d configuration */
91         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
92         ar->v2d.flag = V2D_VIEWSYNC_Y;
93         
94         /* main area */
95         ar= MEM_callocN(sizeof(ARegion), "main area for action");
96         
97         BLI_addtail(&saction->regionbase, ar);
98         ar->regiontype= RGN_TYPE_WINDOW;
99         
100         ar->v2d.tot.xmin= -2.0f;
101         ar->v2d.tot.ymin= -2000.0f;
102         ar->v2d.tot.xmax= 100.0f;
103         ar->v2d.tot.ymax= 0.0f;
104         
105         ar->v2d.cur.xmin= -2.0f;
106         ar->v2d.cur.ymin= -200.0f;
107         ar->v2d.cur.xmax= 100.0f;
108         ar->v2d.cur.ymax= -20.0f;
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]= 2000.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         
123         return (SpaceLink *)saction;
124 }
125
126 /* not spacelink itself */
127 static void action_free(SpaceLink *sl)
128 {       
129 //      SpaceAction *saction= (SpaceAction*) sl;
130         
131 }
132
133
134 /* spacetype; init callback */
135 static void action_init(struct wmWindowManager *wm, ScrArea *sa)
136 {
137
138 }
139
140 static SpaceLink *action_duplicate(SpaceLink *sl)
141 {
142         SpaceAction *sactionn= MEM_dupallocN(sl);
143         
144         /* clear or remove stuff from old */
145         
146         return (SpaceLink *)sactionn;
147 }
148
149
150
151 /* add handlers, stuff you only do once or on area/region changes */
152 static void action_main_area_init(wmWindowManager *wm, ARegion *ar)
153 {
154         ListBase *keymap;
155         
156         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
157         
158         /* own keymap */
159         keymap= WM_keymap_listbase(wm, "Action", SPACE_ACTION, 0);      /* XXX weak? */
160         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
161 }
162
163 static void action_main_area_draw(const bContext *C, ARegion *ar)
164 {
165         /* draw entirely, view changes should be handled here */
166         SpaceAction *saction= (SpaceAction*)CTX_wm_space_data(C);
167         View2D *v2d= &ar->v2d;
168         View2DGrid *grid;
169         View2DScrollers *scrollers;
170         float col[3];
171         short unit=0, 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         /* time grid */
181         unit= (saction->flag & SACTION_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
182         grid= UI_view2d_grid_calc(C, v2d, unit, V2D_GRID_NOCLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
183         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
184         UI_view2d_grid_free(grid);
185         
186         /* data? */
187                 
188         /* current frame */
189         if (saction->flag & SACTION_DRAWTIME)   flag |= DRAWCFRA_UNIT_SECONDS;
190         if ((saction->flag & SACTION_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
191         ANIM_draw_cfra(C, v2d, flag);
192         
193         /* markers */
194         UI_view2d_view_orthoSpecial(C, v2d, 1);
195         draw_markers_time(C, 0);
196         
197         /* preview range */
198         UI_view2d_view_ortho(C, v2d);
199         ANIM_draw_previewrange(C, v2d);
200         
201         /* reset view matrix */
202         UI_view2d_view_restore(C);
203         
204         /* scrollers */
205         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_NOCLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
206         UI_view2d_scrollers_draw(C, v2d, scrollers);
207         UI_view2d_scrollers_free(scrollers);
208 }
209
210 void action_operatortypes(void)
211 {
212         
213 }
214
215 void action_keymap(struct wmWindowManager *wm)
216 {
217         
218 }
219
220 /* add handlers, stuff you only do once or on area/region changes */
221 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
222 {
223         ListBase *keymap;
224         
225         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
226         
227         /* own keymap */
228         keymap= WM_keymap_listbase(wm, "Action", SPACE_ACTION, 0);      /* XXX weak? */
229         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
230 }
231
232 static void action_channel_area_draw(const bContext *C, ARegion *ar)
233 {
234         /* draw entirely, view changes should be handled here */
235         //SpaceAction *saction= C->area->spacedata.first;
236         View2D *v2d= &ar->v2d;
237         View2DScrollers *scrollers;
238         float col[3];
239         
240         /* clear and setup matrix */
241         UI_GetThemeColor3fv(TH_BACK, col);
242         glClearColor(col[0], col[1], col[2], 0.0);
243         glClear(GL_COLOR_BUFFER_BIT);
244         
245         UI_view2d_view_ortho(C, v2d);
246         
247         /* data... */
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 }
292
293 /* only called once, from space/spacetypes.c */
294 void ED_spacetype_action(void)
295 {
296         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
297         ARegionType *art;
298         
299         st->spaceid= SPACE_ACTION;
300         
301         st->new= action_new;
302         st->free= action_free;
303         st->init= action_init;
304         st->duplicate= action_duplicate;
305         st->operatortypes= action_operatortypes;
306         st->keymap= action_keymap;
307         
308         /* regions: main window */
309         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
310         art->regionid = RGN_TYPE_WINDOW;
311         art->init= action_main_area_init;
312         art->draw= action_main_area_draw;
313         art->listener= action_main_area_listener;
314         art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_MARKERS;
315
316         BLI_addhead(&st->regiontypes, art);
317         
318         /* regions: header */
319         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
320         art->regionid = RGN_TYPE_HEADER;
321         art->minsizey= HEADERY;
322         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
323         
324         art->init= action_header_area_init;
325         art->draw= action_header_area_draw;
326         
327         BLI_addhead(&st->regiontypes, art);
328         
329         /* regions: channels */
330         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
331         art->regionid = RGN_TYPE_CHANNELS;
332         art->minsizex= 200;
333         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
334         
335         art->init= action_channel_area_init;
336         art->draw= action_channel_area_draw;
337         
338         BLI_addhead(&st->regiontypes, art);
339         
340         
341         BKE_spacetype_register(st);
342 }
343