2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_nla/space_nla.c
35 #include "DNA_anim_types.h"
36 #include "DNA_scene_types.h"
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
45 #include "BKE_context.h"
46 #include "BKE_global.h"
48 #include "BKE_screen.h"
50 #include "ED_space_api.h"
51 #include "ED_anim_api.h"
52 #include "ED_markers.h"
53 #include "ED_screen.h"
60 #include "UI_resources.h"
61 #include "UI_view2d.h"
63 #include "nla_intern.h" /* own include */
65 /* ******************** manage regions ********************* */
67 ARegion *nla_has_buttons_region(ScrArea *sa)
71 ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
74 /* add subdiv level; after main */
75 ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
78 if (ar == NULL) return NULL;
80 arnew = MEM_callocN(sizeof(ARegion), "buttons for nla");
82 BLI_insertlinkafter(&sa->regionbase, ar, arnew);
83 arnew->regiontype = RGN_TYPE_UI;
84 arnew->alignment = RGN_ALIGN_RIGHT;
86 arnew->flag = RGN_FLAG_HIDDEN;
93 /* ******************** default callbacks for nla space ***************** */
95 static SpaceLink *nla_new(const bContext *C)
97 Scene *scene = CTX_data_scene(C);
98 ScrArea *sa = CTX_wm_area(C);
102 snla = MEM_callocN(sizeof(SpaceNla), "initnla");
103 snla->spacetype = SPACE_NLA;
105 /* allocate DopeSheet data for NLA Editor */
106 snla->ads = MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
107 snla->ads->source = (ID *)scene;
109 /* set auto-snapping settings */
110 snla->autosnap = SACTSNAP_FRAME;
113 ar = MEM_callocN(sizeof(ARegion), "header for nla");
115 BLI_addtail(&snla->regionbase, ar);
116 ar->regiontype = RGN_TYPE_HEADER;
117 ar->alignment = RGN_ALIGN_BOTTOM;
119 /* channel list region */
120 ar = MEM_callocN(sizeof(ARegion), "channel list for nla");
121 BLI_addtail(&snla->regionbase, ar);
122 ar->regiontype = RGN_TYPE_CHANNELS;
123 ar->alignment = RGN_ALIGN_LEFT;
125 /* only need to set these settings since this will use the 'stack' configuration */
126 ar->v2d.scroll = V2D_SCROLL_BOTTOM;
127 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
130 ar = MEM_callocN(sizeof(ARegion), "buttons area for nla");
132 BLI_addtail(&snla->regionbase, ar);
133 ar->regiontype = RGN_TYPE_UI;
134 ar->alignment = RGN_ALIGN_RIGHT;
135 ar->flag = RGN_FLAG_HIDDEN;
138 ar = MEM_callocN(sizeof(ARegion), "main area for nla");
140 BLI_addtail(&snla->regionbase, ar);
141 ar->regiontype = RGN_TYPE_WINDOW;
143 ar->v2d.tot.xmin = (float)(SFRA - 10);
144 ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
145 ar->v2d.tot.xmax = (float)(EFRA + 10);
146 ar->v2d.tot.ymax = 0.0f;
148 ar->v2d.cur = ar->v2d.tot;
150 ar->v2d.min[0] = 0.0f;
151 ar->v2d.min[1] = 0.0f;
153 ar->v2d.max[0] = MAXFRAMEF;
154 ar->v2d.max[1] = 10000.0f;
156 ar->v2d.minzoom = 0.01f;
157 ar->v2d.maxzoom = 50;
158 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
159 ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
160 ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
161 ar->v2d.keepofs = V2D_KEEPOFS_Y;
162 ar->v2d.align = V2D_ALIGN_NO_POS_Y;
163 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
165 return (SpaceLink *)snla;
168 /* not spacelink itself */
169 static void nla_free(SpaceLink *sl)
171 SpaceNla *snla = (SpaceNla *) sl;
174 BLI_freelistN(&snla->ads->chanbase);
175 MEM_freeN(snla->ads);
180 /* spacetype; init callback */
181 static void nla_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
183 SpaceNla *snla = (SpaceNla *)sa->spacedata.first;
185 /* init dopesheet data if non-existant (i.e. for old files) */
186 if (snla->ads == NULL) {
187 snla->ads = MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
188 snla->ads->source = (ID *)G.main->scene.first; // XXX this is bad, but we need this to be set correct
191 ED_area_tag_refresh(sa);
194 static SpaceLink *nla_duplicate(SpaceLink *sl)
196 SpaceNla *snlan = MEM_dupallocN(sl);
198 /* clear or remove stuff from old */
199 snlan->ads = MEM_dupallocN(snlan->ads);
201 return (SpaceLink *)snlan;
204 /* add handlers, stuff you only do once or on area/region changes */
205 static void nla_channel_area_init(wmWindowManager *wm, ARegion *ar)
209 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
212 /* own channels map first to override some channel keymaps */
213 keymap = WM_keymap_find(wm->defaultconf, "NLA Channels", SPACE_NLA, 0);
214 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
215 /* now generic channels map for everything else that can apply */
216 keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
217 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
219 keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
220 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
223 /* draw entirely, view changes should be handled here */
224 static void nla_channel_area_draw(const bContext *C, ARegion *ar)
227 View2D *v2d = &ar->v2d;
228 View2DScrollers *scrollers;
230 /* clear and setup matrix */
231 UI_ThemeClearColor(TH_BACK);
232 glClear(GL_COLOR_BUFFER_BIT);
234 UI_view2d_view_ortho(v2d);
237 if (ANIM_animdata_get_context(C, &ac)) {
238 draw_nla_channel_list((bContext *)C, &ac, ar);
241 /* reset view matrix */
242 UI_view2d_view_restore(C);
245 scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
246 UI_view2d_scrollers_draw(C, v2d, scrollers);
247 UI_view2d_scrollers_free(scrollers);
251 /* add handlers, stuff you only do once or on area/region changes */
252 static void nla_main_area_init(wmWindowManager *wm, ARegion *ar)
256 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
259 keymap = WM_keymap_find(wm->defaultconf, "NLA Editor", SPACE_NLA, 0);
260 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
261 keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
262 WM_event_add_keymap_handler(&ar->handlers, keymap);
265 static void nla_main_area_draw(const bContext *C, ARegion *ar)
267 /* draw entirely, view changes should be handled here */
268 SpaceNla *snla = CTX_wm_space_nla(C);
270 View2D *v2d = &ar->v2d;
272 View2DScrollers *scrollers;
273 short unit = 0, flag = 0;
275 /* clear and setup matrix */
276 UI_ThemeClearColor(TH_BACK);
277 glClear(GL_COLOR_BUFFER_BIT);
279 UI_view2d_view_ortho(v2d);
282 unit = (snla->flag & SNLA_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
283 grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
284 UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
285 UI_view2d_grid_free(grid);
288 if (ANIM_animdata_get_context(C, &ac)) {
289 /* strips and backdrops */
290 draw_nla_main_data(&ac, snla, ar);
292 /* text draw cached, in pixelspace now */
293 UI_view2d_text_cache_draw(ar);
296 UI_view2d_view_ortho(v2d);
299 if (snla->flag & SNLA_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
300 if ((snla->flag & SNLA_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
301 ANIM_draw_cfra(C, v2d, flag);
304 UI_view2d_view_orthoSpecial(ar, v2d, 1);
305 draw_markers_time(C, 0);
308 UI_view2d_view_ortho(v2d);
309 ANIM_draw_previewrange(C, v2d);
311 /* reset view matrix */
312 UI_view2d_view_restore(C);
315 scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
316 UI_view2d_scrollers_draw(C, v2d, scrollers);
317 UI_view2d_scrollers_free(scrollers);
321 /* add handlers, stuff you only do once or on area/region changes */
322 static void nla_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
324 ED_region_header_init(ar);
327 static void nla_header_area_draw(const bContext *C, ARegion *ar)
329 ED_region_header(C, ar);
332 /* add handlers, stuff you only do once or on area/region changes */
333 static void nla_buttons_area_init(wmWindowManager *wm, ARegion *ar)
337 ED_region_panels_init(wm, ar);
339 keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
340 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
343 static void nla_buttons_area_draw(const bContext *C, ARegion *ar)
345 ED_region_panels(C, ar, 1, NULL, -1);
348 static void nla_region_listener(ARegion *ar, wmNotifier *wmn)
350 /* context changes */
351 switch (wmn->category) {
353 ED_region_tag_redraw(ar);
360 ED_region_tag_redraw(ar);
369 ED_region_tag_redraw(ar);
374 if (wmn->data == ND_KEYS)
375 ED_region_tag_redraw(ar);
381 static void nla_main_area_listener(ARegion *ar, wmNotifier *wmn)
383 /* context changes */
384 switch (wmn->category) {
386 ED_region_tag_redraw(ar);
390 case ND_RENDER_OPTIONS:
394 ED_region_tag_redraw(ar);
404 ED_region_tag_redraw(ar);
409 switch (wmn->action) {
411 ED_region_tag_redraw(ar);
416 if (wmn->action == NA_RENAME)
417 ED_region_tag_redraw(ar);
420 if (wmn->data == ND_KEYS)
421 ED_region_tag_redraw(ar);
425 static void nla_channel_area_listener(ARegion *ar, wmNotifier *wmn)
427 /* context changes */
428 switch (wmn->category) {
430 ED_region_tag_redraw(ar);
435 ED_region_tag_redraw(ar);
444 ED_region_tag_redraw(ar);
449 if (wmn->action == NA_RENAME)
450 ED_region_tag_redraw(ar);
454 if (wmn->data == ND_KEYS)
455 ED_region_tag_redraw(ar);
459 /* editor level listener */
460 static void nla_listener(ScrArea *sa, wmNotifier *wmn)
462 /* context changes */
463 switch (wmn->category) {
465 // TODO: filter specific types of changes?
466 ED_area_tag_refresh(sa);
473 ED_area_tag_refresh(sa);
477 ED_area_tag_refresh(sa);
485 ED_area_tag_refresh(sa);
490 if (wmn->data == ND_SPACE_NLA)
491 ED_area_tag_redraw(sa);
496 /* only called once, from space/spacetypes.c */
497 void ED_spacetype_nla(void)
499 SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype nla");
502 st->spaceid = SPACE_NLA;
503 strncpy(st->name, "NLA", BKE_ST_MAXNAME);
508 st->duplicate = nla_duplicate;
509 st->operatortypes = nla_operatortypes;
510 st->listener = nla_listener;
511 st->keymap = nla_keymap;
513 /* regions: main window */
514 art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
515 art->regionid = RGN_TYPE_WINDOW;
516 art->init = nla_main_area_init;
517 art->draw = nla_main_area_draw;
518 art->listener = nla_main_area_listener;
519 art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
521 BLI_addhead(&st->regiontypes, art);
523 /* regions: header */
524 art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
525 art->regionid = RGN_TYPE_HEADER;
526 art->prefsizey = HEADERY;
527 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
529 art->init = nla_header_area_init;
530 art->draw = nla_header_area_draw;
532 BLI_addhead(&st->regiontypes, art);
534 /* regions: channels */
535 art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
536 art->regionid = RGN_TYPE_CHANNELS;
537 art->prefsizex = 200;
538 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
540 art->init = nla_channel_area_init;
541 art->draw = nla_channel_area_draw;
542 art->listener = nla_channel_area_listener;
544 BLI_addhead(&st->regiontypes, art);
546 /* regions: UI buttons */
547 art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
548 art->regionid = RGN_TYPE_UI;
549 art->prefsizex = 200;
550 art->keymapflag = ED_KEYMAP_UI;
551 art->listener = nla_region_listener;
552 art->init = nla_buttons_area_init;
553 art->draw = nla_buttons_area_draw;
555 BLI_addhead(&st->regiontypes, art);
557 nla_buttons_register(art);
560 BKE_spacetype_register(st);