44ed692ba484a6ca83802509c861df1abc3345ed
[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 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
258 {
259         /* context changes */
260         switch(wmn->category) {
261                 case NC_SCENE:
262                         switch(wmn->data) {
263                                 case ND_OB_ACTIVE:
264                                         ED_region_tag_redraw(ar);
265                                         break;
266                         }
267                         break;
268                 case NC_OBJECT:
269                         switch(wmn->data) {
270                                 case ND_BONE_ACTIVE:
271                                 case ND_BONE_SELECT:
272                                         ED_region_tag_redraw(ar);
273                                         break;
274                         }
275                         break;
276         }
277 }
278
279
280 /* add handlers, stuff you only do once or on area/region changes */
281 static void action_header_area_init(wmWindowManager *wm, ARegion *ar)
282 {
283         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
284 }
285
286 static void action_header_area_draw(const bContext *C, ARegion *ar)
287 {
288         float col[3];
289         
290         /* clear */
291         if(ED_screen_area_active(C))
292                 UI_GetThemeColor3fv(TH_HEADER, col);
293         else
294                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
295         
296         glClearColor(col[0], col[1], col[2], 0.0);
297         glClear(GL_COLOR_BUFFER_BIT);
298         
299         /* set view2d view matrix for scrolling (without scrollers) */
300         UI_view2d_view_ortho(C, &ar->v2d);
301         
302         action_header_buttons(C, ar);
303         
304         /* restore view matrix? */
305         UI_view2d_view_restore(C);
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 /* only called once, from space/spacetypes.c */
333 void ED_spacetype_action(void)
334 {
335         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
336         ARegionType *art;
337         
338         st->spaceid= SPACE_ACTION;
339         
340         st->new= action_new;
341         st->free= action_free;
342         st->init= action_init;
343         st->duplicate= action_duplicate;
344         st->operatortypes= action_operatortypes;
345         st->keymap= action_keymap;
346         
347         /* regions: main window */
348         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
349         art->regionid = RGN_TYPE_WINDOW;
350         art->init= action_main_area_init;
351         art->draw= action_main_area_draw;
352         art->listener= action_main_area_listener;
353         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION;
354
355         BLI_addhead(&st->regiontypes, art);
356         
357         /* regions: header */
358         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
359         art->regionid = RGN_TYPE_HEADER;
360         art->minsizey= HEADERY;
361         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
362         
363         art->init= action_header_area_init;
364         art->draw= action_header_area_draw;
365         
366         BLI_addhead(&st->regiontypes, art);
367         
368         /* regions: channels */
369         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
370         art->regionid = RGN_TYPE_CHANNELS;
371         art->minsizex= 200;
372         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
373         
374         art->init= action_channel_area_init;
375         art->draw= action_channel_area_draw;
376         art->listener= action_channel_area_listener;
377         
378         BLI_addhead(&st->regiontypes, art);
379         
380         
381         BKE_spacetype_register(st);
382 }
383