Merged revision(s) 58452-58584 from trunk/blender into soc-2013-dingto.
[blender.git] / source / blender / editors / space_nla / space_nla.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_nla/space_nla.c
28  *  \ingroup spnla
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_anim_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_main.h"
47 #include "BKE_screen.h"
48
49 #include "ED_space_api.h"
50 #include "ED_anim_api.h"
51 #include "ED_markers.h"
52 #include "ED_screen.h"
53
54 #include "BIF_gl.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #include "nla_intern.h" /* own include */
63
64 /* ******************** manage regions ********************* */
65
66 ARegion *nla_has_buttons_region(ScrArea *sa)
67 {
68         ARegion *ar, *arnew;
69
70         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
71         if (ar) return ar;
72
73         /* add subdiv level; after main */
74         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
75
76         /* is error! */
77         if (ar == NULL) return NULL;
78         
79         arnew = MEM_callocN(sizeof(ARegion), "buttons for nla");
80         
81         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
82         arnew->regiontype = RGN_TYPE_UI;
83         arnew->alignment = RGN_ALIGN_RIGHT;
84         
85         arnew->flag = RGN_FLAG_HIDDEN;
86         
87         return arnew;
88 }
89
90
91
92 /* ******************** default callbacks for nla space ***************** */
93
94 static SpaceLink *nla_new(const bContext *C)
95 {
96         Scene *scene = CTX_data_scene(C);
97         ScrArea *sa = CTX_wm_area(C);
98         ARegion *ar;
99         SpaceNla *snla;
100         
101         snla = MEM_callocN(sizeof(SpaceNla), "initnla");
102         snla->spacetype = SPACE_NLA;
103         
104         /* allocate DopeSheet data for NLA Editor */
105         snla->ads = MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
106         snla->ads->source = (ID *)scene;
107         
108         /* set auto-snapping settings */
109         snla->autosnap = SACTSNAP_FRAME;
110         
111         /* header */
112         ar = MEM_callocN(sizeof(ARegion), "header for nla");
113         
114         BLI_addtail(&snla->regionbase, ar);
115         ar->regiontype = RGN_TYPE_HEADER;
116         ar->alignment = RGN_ALIGN_BOTTOM;
117         
118         /* channel list region */
119         ar = MEM_callocN(sizeof(ARegion), "channel list for nla");
120         BLI_addtail(&snla->regionbase, ar);
121         ar->regiontype = RGN_TYPE_CHANNELS;
122         ar->alignment = RGN_ALIGN_LEFT;
123         
124         /* only need to set these settings since this will use the 'stack' configuration */
125         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
126         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
127         
128         /* ui buttons */
129         ar = MEM_callocN(sizeof(ARegion), "buttons area for nla");
130         
131         BLI_addtail(&snla->regionbase, ar);
132         ar->regiontype = RGN_TYPE_UI;
133         ar->alignment = RGN_ALIGN_RIGHT;
134         ar->flag = RGN_FLAG_HIDDEN;
135         
136         /* main area */
137         ar = MEM_callocN(sizeof(ARegion), "main area for nla");
138         
139         BLI_addtail(&snla->regionbase, ar);
140         ar->regiontype = RGN_TYPE_WINDOW;
141         
142         ar->v2d.tot.xmin = (float)(SFRA - 10);
143         ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
144         ar->v2d.tot.xmax = (float)(EFRA + 10);
145         ar->v2d.tot.ymax = 0.0f;
146         
147         ar->v2d.cur = ar->v2d.tot;
148         
149         ar->v2d.min[0] = 0.0f;
150         ar->v2d.min[1] = 0.0f;
151         
152         ar->v2d.max[0] = MAXFRAMEF;
153         ar->v2d.max[1] = 10000.0f;
154
155         ar->v2d.minzoom = 0.01f;
156         ar->v2d.maxzoom = 50;
157         ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
158         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
159         ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
160         ar->v2d.keepofs = V2D_KEEPOFS_Y;
161         ar->v2d.align = V2D_ALIGN_NO_POS_Y;
162         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
163         
164         return (SpaceLink *)snla;
165 }
166
167 /* not spacelink itself */
168 static void nla_free(SpaceLink *sl)
169 {       
170         SpaceNla *snla = (SpaceNla *) sl;
171         
172         if (snla->ads) {
173                 BLI_freelistN(&snla->ads->chanbase);
174                 MEM_freeN(snla->ads);
175         }
176 }
177
178
179 /* spacetype; init callback */
180 static void nla_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
181 {
182         SpaceNla *snla = (SpaceNla *)sa->spacedata.first;
183         
184         /* init dopesheet data if non-existant (i.e. for old files) */
185         if (snla->ads == NULL) {
186                 snla->ads = MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
187                 snla->ads->source = (ID *)G.main->scene.first; // XXX this is bad, but we need this to be set correct
188         }
189
190         ED_area_tag_refresh(sa);
191 }
192
193 static SpaceLink *nla_duplicate(SpaceLink *sl)
194 {
195         SpaceNla *snlan = MEM_dupallocN(sl);
196         
197         /* clear or remove stuff from old */
198         snlan->ads = MEM_dupallocN(snlan->ads);
199         
200         return (SpaceLink *)snlan;
201 }
202
203 /* add handlers, stuff you only do once or on area/region changes */
204 static void nla_channel_area_init(wmWindowManager *wm, ARegion *ar)
205 {
206         wmKeyMap *keymap;
207         
208         /* ensure the 2d view sync works - main region has bottom scroller */
209         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
210         
211         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
212         
213         /* own keymap */
214         /* own channels map first to override some channel keymaps */
215         keymap = WM_keymap_find(wm->defaultconf, "NLA Channels", SPACE_NLA, 0);
216         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
217         /* now generic channels map for everything else that can apply */
218         keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
219         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
220         
221         keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
222         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
223 }
224
225 /* draw entirely, view changes should be handled here */
226 static void nla_channel_area_draw(const bContext *C, ARegion *ar)
227 {
228         bAnimContext ac;
229         View2D *v2d = &ar->v2d;
230         View2DScrollers *scrollers;
231         
232         /* clear and setup matrix */
233         UI_ThemeClearColor(TH_BACK);
234         glClear(GL_COLOR_BUFFER_BIT);
235         
236         UI_view2d_view_ortho(v2d);
237         
238         /* data */
239         if (ANIM_animdata_get_context(C, &ac)) {
240                 draw_nla_channel_list((bContext *)C, &ac, ar);
241         }
242         
243         /* reset view matrix */
244         UI_view2d_view_restore(C);
245         
246         /* scrollers */
247         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
248         UI_view2d_scrollers_draw(C, v2d, scrollers);
249         UI_view2d_scrollers_free(scrollers);
250 }
251
252
253 /* add handlers, stuff you only do once or on area/region changes */
254 static void nla_main_area_init(wmWindowManager *wm, ARegion *ar)
255 {
256         wmKeyMap *keymap;
257         
258         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
259         
260         /* own keymap */
261         keymap = WM_keymap_find(wm->defaultconf, "NLA Editor", SPACE_NLA, 0);
262         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
263         keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
264         WM_event_add_keymap_handler(&ar->handlers, keymap);
265 }
266
267 static void nla_main_area_draw(const bContext *C, ARegion *ar)
268 {
269         /* draw entirely, view changes should be handled here */
270         SpaceNla *snla = CTX_wm_space_nla(C);
271         bAnimContext ac;
272         View2D *v2d = &ar->v2d;
273         View2DGrid *grid;
274         View2DScrollers *scrollers;
275         short unit = 0, flag = 0;
276         
277         /* clear and setup matrix */
278         UI_ThemeClearColor(TH_BACK);
279         glClear(GL_COLOR_BUFFER_BIT);
280         
281         UI_view2d_view_ortho(v2d);
282         
283         /* time grid */
284         unit = (snla->flag & SNLA_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
285         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
286         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
287         UI_view2d_grid_free(grid);
288         
289         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
290
291         /* data */
292         if (ANIM_animdata_get_context(C, &ac)) {
293                 /* strips and backdrops */
294                 draw_nla_main_data(&ac, snla, ar);
295                 
296                 /* text draw cached, in pixelspace now */
297                 UI_view2d_text_cache_draw(ar);
298         }
299         
300         UI_view2d_view_ortho(v2d);
301         
302         /* current frame */
303         if (snla->flag & SNLA_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
304         if ((snla->flag & SNLA_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
305         ANIM_draw_cfra(C, v2d, flag);
306         
307         /* markers */
308         UI_view2d_view_orthoSpecial(ar, v2d, 1);
309         draw_markers_time(C, 0);
310         
311         /* preview range */
312         UI_view2d_view_ortho(v2d);
313         ANIM_draw_previewrange(C, v2d, 0);
314         
315         /* callback */
316         UI_view2d_view_ortho(v2d);
317         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
318
319         /* reset view matrix */
320         UI_view2d_view_restore(C);
321         
322         /* scrollers */
323         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
324         UI_view2d_scrollers_draw(C, v2d, scrollers);
325         UI_view2d_scrollers_free(scrollers);
326 }
327
328
329 /* add handlers, stuff you only do once or on area/region changes */
330 static void nla_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
331 {
332         ED_region_header_init(ar);
333 }
334
335 static void nla_header_area_draw(const bContext *C, ARegion *ar)
336 {
337         ED_region_header(C, ar);
338 }
339
340 /* add handlers, stuff you only do once or on area/region changes */
341 static void nla_buttons_area_init(wmWindowManager *wm, ARegion *ar)
342 {
343         wmKeyMap *keymap;
344         
345         ED_region_panels_init(wm, ar);
346         
347         keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
348         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
349 }
350
351 static void nla_buttons_area_draw(const bContext *C, ARegion *ar)
352 {
353         ED_region_panels(C, ar, 1, NULL, -1);
354 }
355
356 static void nla_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
357 {
358         /* context changes */
359         switch (wmn->category) {
360                 case NC_ANIMATION:
361                         ED_region_tag_redraw(ar);
362                         break;
363                 case NC_SCENE:
364                         switch (wmn->data) {
365                                 case ND_OB_ACTIVE:
366                                 case ND_FRAME:
367                                 case ND_MARKERS:
368                                         ED_region_tag_redraw(ar);
369                                         break;
370                         }
371                         break;
372                 case NC_OBJECT:
373                         switch (wmn->data) {
374                                 case ND_BONE_ACTIVE:
375                                 case ND_BONE_SELECT:
376                                 case ND_KEYS:
377                                         ED_region_tag_redraw(ar);
378                                         break;
379                         }
380                         break;
381                 default:
382                         if (wmn->data == ND_KEYS)
383                                 ED_region_tag_redraw(ar);
384                         break;
385         }
386 }
387
388
389 static void nla_main_area_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
390 {
391         /* context changes */
392         switch (wmn->category) {
393                 case NC_ANIMATION:
394                         ED_region_tag_redraw(ar);
395                         break;
396                 case NC_SCENE:
397                         switch (wmn->data) {
398                                 case ND_RENDER_OPTIONS:
399                                 case ND_OB_ACTIVE:
400                                 case ND_FRAME:
401                                 case ND_MARKERS:
402                                         ED_region_tag_redraw(ar);
403                                         break;
404                         }
405                         break;
406                 case NC_OBJECT:
407                         switch (wmn->data) {
408                                 case ND_BONE_ACTIVE:
409                                 case ND_BONE_SELECT:
410                                 case ND_KEYS:
411                                 case ND_TRANSFORM:
412                                         ED_region_tag_redraw(ar);
413                                         break;
414                         }
415                         break;
416                 case NC_NODE:
417                         switch (wmn->action) {
418                                 case NA_EDITED:
419                                         ED_region_tag_redraw(ar);
420                                         break;
421                         }
422                         break;
423                 case NC_ID:
424                         if (wmn->action == NA_RENAME)
425                                 ED_region_tag_redraw(ar);
426                         break;
427                 default:
428                         if (wmn->data == ND_KEYS)
429                                 ED_region_tag_redraw(ar);
430                         break;
431         }
432 }
433
434 static void nla_channel_area_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
435 {
436         /* context changes */
437         switch (wmn->category) {
438                 case NC_ANIMATION:
439                         ED_region_tag_redraw(ar);
440                         break;
441                 case NC_SCENE:
442                         switch (wmn->data) {
443                                 case ND_OB_ACTIVE:
444                                         ED_region_tag_redraw(ar);
445                                         break;
446                         }
447                         break;
448                 case NC_OBJECT:
449                         switch (wmn->data) {
450                                 case ND_BONE_ACTIVE:
451                                 case ND_BONE_SELECT:
452                                 case ND_KEYS:
453                                         ED_region_tag_redraw(ar);
454                                         break;
455                         }
456                         break;
457                 case NC_ID:
458                         if (wmn->action == NA_RENAME)
459                                 ED_region_tag_redraw(ar);
460                         break;
461                 default:
462                         if (wmn->data == ND_KEYS)
463                                 ED_region_tag_redraw(ar);
464                         break;
465         }
466 }
467
468 /* editor level listener */
469 static void nla_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn)
470 {
471         /* context changes */
472         switch (wmn->category) {
473                 case NC_ANIMATION:
474                         // TODO: filter specific types of changes?
475                         ED_area_tag_refresh(sa);
476                         break;
477                 case NC_SCENE:
478 #if 0
479                         switch (wmn->data) {
480                                 case ND_OB_ACTIVE:
481                                 case ND_OB_SELECT:
482                                         ED_area_tag_refresh(sa);
483                                         break;
484                         }
485 #endif
486                         ED_area_tag_refresh(sa);
487                         break;
488                 case NC_OBJECT:
489                         switch (wmn->data) {
490                                 case ND_TRANSFORM:
491                                         /* do nothing */
492                                         break;
493                                 default:
494                                         ED_area_tag_refresh(sa);
495                                         break;
496                         }
497                         break;
498                 case NC_SPACE:
499                         if (wmn->data == ND_SPACE_NLA)
500                                 ED_area_tag_redraw(sa);
501                         break;
502         }
503 }
504
505 /* only called once, from space/spacetypes.c */
506 void ED_spacetype_nla(void)
507 {
508         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype nla");
509         ARegionType *art;
510         
511         st->spaceid = SPACE_NLA;
512         strncpy(st->name, "NLA", BKE_ST_MAXNAME);
513         
514         st->new = nla_new;
515         st->free = nla_free;
516         st->init = nla_init;
517         st->duplicate = nla_duplicate;
518         st->operatortypes = nla_operatortypes;
519         st->listener = nla_listener;
520         st->keymap = nla_keymap;
521         
522         /* regions: main window */
523         art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
524         art->regionid = RGN_TYPE_WINDOW;
525         art->init = nla_main_area_init;
526         art->draw = nla_main_area_draw;
527         art->listener = nla_main_area_listener;
528         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
529
530         BLI_addhead(&st->regiontypes, art);
531         
532         /* regions: header */
533         art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
534         art->regionid = RGN_TYPE_HEADER;
535         art->prefsizey = HEADERY;
536         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
537         
538         art->init = nla_header_area_init;
539         art->draw = nla_header_area_draw;
540         
541         BLI_addhead(&st->regiontypes, art);
542         
543         /* regions: channels */
544         art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
545         art->regionid = RGN_TYPE_CHANNELS;
546         art->prefsizex = 200;
547         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
548         
549         art->init = nla_channel_area_init;
550         art->draw = nla_channel_area_draw;
551         art->listener = nla_channel_area_listener;
552         
553         BLI_addhead(&st->regiontypes, art);
554         
555         /* regions: UI buttons */
556         art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
557         art->regionid = RGN_TYPE_UI;
558         art->prefsizex = 200;
559         art->keymapflag = ED_KEYMAP_UI;
560         art->listener = nla_region_listener;
561         art->init = nla_buttons_area_init;
562         art->draw = nla_buttons_area_draw;
563         
564         BLI_addhead(&st->regiontypes, art);
565
566         nla_buttons_register(art);
567         
568         
569         BKE_spacetype_register(st);
570 }