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