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_time/space_time.c
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_dlrbTree.h"
42 #include "BLI_utildefines.h"
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_screen.h"
47 #include "BKE_pointcache.h"
49 #include "ED_anim_api.h"
50 #include "ED_keyframes_draw.h"
51 #include "ED_screen.h"
57 #include "BIF_glutil.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
62 #include "ED_space_api.h"
63 #include "ED_markers.h"
65 #include "time_intern.h"
67 /* ************************ main time area region *********************** */
69 static void time_draw_sfra_efra(Scene *scene, View2D *v2d)
71 /* draw darkened area outside of active timeline
72 * frame range used is preview range or scene range
74 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
76 glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
79 glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
80 glRectf((float)PEFRA, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
83 glRectf(v2d->cur.xmin, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
87 UI_ThemeColorShade(TH_BACK, -60);
88 /* thin lines where the actual frames are */
89 fdrawline((float)PSFRA, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
90 fdrawline((float)PEFRA, v2d->cur.ymin, (float)PEFRA, v2d->cur.ymax);
93 #define CACHE_DRAW_HEIGHT 3.0f
95 static void time_draw_cache(SpaceTime *stime, Object *ob, Scene *scene)
99 SpaceTimeCache *stc = stime->caches.first;
102 if (!(stime->cache_display & TIME_CACHE_DISPLAY) || (!ob))
105 BKE_ptcache_ids_from_object(&pidlist, ob, scene, 0);
107 /* iterate over pointcaches on the active object,
108 * add spacetimecache and vertex array for each */
109 for (pid = pidlist.first; pid; pid = pid->next) {
111 int i, sta = pid->cache->startframe, end = pid->cache->endframe;
112 int len = (end - sta + 1) * 4;
115 case PTCACHE_TYPE_SOFTBODY:
116 if (!(stime->cache_display & TIME_CACHE_SOFTBODY)) continue;
118 case PTCACHE_TYPE_PARTICLES:
119 if (!(stime->cache_display & TIME_CACHE_PARTICLES)) continue;
121 case PTCACHE_TYPE_CLOTH:
122 if (!(stime->cache_display & TIME_CACHE_CLOTH)) continue;
124 case PTCACHE_TYPE_SMOKE_DOMAIN:
125 case PTCACHE_TYPE_SMOKE_HIGHRES:
126 if (!(stime->cache_display & TIME_CACHE_SMOKE)) continue;
128 case PTCACHE_TYPE_DYNAMICPAINT:
129 if (!(stime->cache_display & TIME_CACHE_DYNAMICPAINT)) continue;
131 case PTCACHE_TYPE_RIGIDBODY:
132 if (!(stime->cache_display & TIME_CACHE_RIGIDBODY)) continue;
136 if (pid->cache->cached_frames == NULL)
139 /* make sure we have stc with correct array length */
140 if (stc == NULL || MEM_allocN_len(stc->array) != len * 2 * sizeof(float)) {
142 MEM_freeN(stc->array);
145 stc = MEM_callocN(sizeof(SpaceTimeCache), "spacetimecache");
146 BLI_addtail(&stime->caches, stc);
149 stc->array = MEM_callocN(len * 2 * sizeof(float), "SpaceTimeCache array");
152 /* fill the vertex array with a quad for each cached frame */
153 for (i = sta, fp = stc->array; i <= end; i++) {
154 if (pid->cache->cached_frames[i - sta]) {
155 fp[0] = (float)i - 0.5f;
159 fp[0] = (float)i - 0.5f;
163 fp[0] = (float)i + 0.5f;
167 fp[0] = (float)i + 0.5f;
174 glTranslatef(0.0, (float)V2D_SCROLL_HEIGHT + yoffs, 0.0);
175 glScalef(1.0, CACHE_DRAW_HEIGHT, 0.0);
178 case PTCACHE_TYPE_SOFTBODY:
179 col[0] = 1.0; col[1] = 0.4; col[2] = 0.02;
182 case PTCACHE_TYPE_PARTICLES:
183 col[0] = 1.0; col[1] = 0.1; col[2] = 0.02;
186 case PTCACHE_TYPE_CLOTH:
187 col[0] = 0.1; col[1] = 0.1; col[2] = 0.75;
190 case PTCACHE_TYPE_SMOKE_DOMAIN:
191 case PTCACHE_TYPE_SMOKE_HIGHRES:
192 col[0] = 0.2; col[1] = 0.2; col[2] = 0.2;
195 case PTCACHE_TYPE_DYNAMICPAINT:
196 col[0] = 1.0; col[1] = 0.1; col[2] = 0.75;
199 case PTCACHE_TYPE_RIGIDBODY:
200 col[0] = 1.0; col[1] = 0.6; col[2] = 0.0;
205 col[0] = 1.0; col[1] = 0.0; col[2] = 1.0;
212 glRectf((float)sta, 0.0, (float)end, 1.0);
215 if (pid->cache->flag & PTCACHE_BAKED) {
216 col[0] -= 0.4f; col[1] -= 0.4f; col[2] -= 0.4f;
218 else if (pid->cache->flag & PTCACHE_OUTDATED) {
219 col[0] += 0.4f; col[1] += 0.4f; col[2] += 0.4f;
223 glEnableClientState(GL_VERTEX_ARRAY);
224 glVertexPointer(2, GL_FLOAT, 0, stc->array);
225 glDrawArrays(GL_QUADS, 0, (fp - stc->array) / 2);
226 glDisableClientState(GL_VERTEX_ARRAY);
232 yoffs += CACHE_DRAW_HEIGHT;
237 BLI_freelistN(&pidlist);
239 /* free excessive caches */
241 SpaceTimeCache *tmp = stc->next;
242 BLI_remlink(&stime->caches, stc);
243 MEM_freeN(stc->array);
249 static void time_cache_free(SpaceTime *stime)
253 for (stc = stime->caches.first; stc; stc = stc->next) {
255 MEM_freeN(stc->array);
260 BLI_freelistN(&stime->caches);
263 static void time_cache_refresh(SpaceTime *stime)
265 /* Free previous caches to indicate full refresh */
266 time_cache_free(stime);
269 /* helper function - find actkeycolumn that occurs on cframe, or the nearest one if not found */
270 static ActKeyColumn *time_cfra_find_ak(ActKeyColumn *ak, float cframe)
272 ActKeyColumn *akn = NULL;
278 /* check if this is a match, or whether it is in some subtree */
279 if (cframe < ak->cfra)
280 akn = time_cfra_find_ak(ak->left, cframe);
281 else if (cframe > ak->cfra)
282 akn = time_cfra_find_ak(ak->right, cframe);
284 /* if no match found (or found match), just use the current one */
291 /* helper for time_draw_keyframes() */
292 static void time_draw_idblock_keyframes(View2D *v2d, ID *id, short onlysel)
294 bDopeSheet ads = {NULL};
298 /* init binarytree-list for getting keyframes */
299 BLI_dlrbTree_init(&keys);
301 /* init dopesheet settings */
303 ads.filterflag |= ADS_FILTER_ONLYSEL;
305 /* populate tree with keyframe nodes */
306 switch (GS(id->name)) {
308 scene_to_keylist(&ads, (Scene *)id, &keys, NULL);
311 ob_to_keylist(&ads, (Object *)id, &keys, NULL);
315 /* build linked-list for searching */
316 BLI_dlrbTree_linkedlist_sync(&keys);
318 /* start drawing keyframes
319 * - we use the binary-search capabilities of the tree to only start from
320 * the first visible keyframe (last one can then be easily checked)
321 * - draw within a single GL block to be faster
324 for (ak = time_cfra_find_ak(keys.root, v2d->cur.xmin);
325 (ak) && (ak->cfra <= v2d->cur.xmax);
328 glVertex2f(ak->cfra, v2d->tot.ymin);
329 glVertex2f(ak->cfra, v2d->tot.ymax);
333 /* free temp stuff */
334 BLI_dlrbTree_free(&keys);
337 /* draw keyframe lines for timeline */
338 static void time_draw_keyframes(const bContext *C, SpaceTime *stime, ARegion *ar)
340 Scene *scene = CTX_data_scene(C);
341 Object *ob = CTX_data_active_object(C);
342 View2D *v2d = &ar->v2d;
343 short onlysel = (stime->flag & TIME_ONLYACTSEL);
345 /* draw scene keyframes first
346 * - don't try to do this when only drawing active/selected data keyframes,
347 * since this can become quite slow
349 if (scene && onlysel == 0) {
351 glColor3ub(0xDD, 0xA7, 0x00);
352 time_draw_idblock_keyframes(v2d, (ID *)scene, onlysel);
355 /* draw keyframes from selected objects
356 * - only do the active object if in posemode (i.e. showing only keyframes for the bones)
357 * OR the onlysel flag was set, which means that only active object's keyframes should
360 glColor3ub(0xDD, 0xD7, 0x00);
362 if (ob && ((ob->mode == OB_MODE_POSE) || onlysel)) {
363 /* draw keyframes for active object only */
364 time_draw_idblock_keyframes(v2d, (ID *)ob, onlysel);
367 short active_done = FALSE;
369 /* draw keyframes from all selected objects */
370 CTX_DATA_BEGIN (C, Object *, obsel, selected_objects)
372 /* last arg is 0, since onlysel doesn't apply here... */
373 time_draw_idblock_keyframes(v2d, (ID *)obsel, 0);
375 /* if this object is the active one, set flag so that we don't draw again */
381 /* if active object hasn't been done yet, draw it... */
382 if (ob && (active_done == 0))
383 time_draw_idblock_keyframes(v2d, (ID *)ob, 0);
387 /* ---------------- */
389 static void time_refresh(const bContext *UNUSED(C), ScrArea *sa)
391 /* find the main timeline region and refresh cache display*/
392 ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
394 SpaceTime *stime = (SpaceTime *)sa->spacedata.first;
395 time_cache_refresh(stime);
399 /* editor level listener */
400 static void time_listener(ScrArea *sa, wmNotifier *wmn)
403 /* mainly for updating cache display */
404 switch (wmn->category) {
412 ED_area_tag_refresh(sa);
413 ED_area_tag_redraw(sa);
421 ED_area_tag_refresh(sa);
426 Scene *scene = wmn->reference;
428 for (ar = sa->regionbase.first; ar; ar = ar->next) {
429 if (ar->regiontype == RGN_TYPE_WINDOW) {
430 ar->v2d.tot.xmin = (float)(SFRA - 4);
431 ar->v2d.tot.xmax = (float)(EFRA + 4);
440 case ND_SPACE_CHANGED:
441 ED_area_tag_refresh(sa);
447 ED_area_tag_refresh(sa);
453 /* ---------------- */
455 /* add handlers, stuff you only do once or on area/region changes */
456 static void time_main_area_init(wmWindowManager *wm, ARegion *ar)
460 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
463 keymap = WM_keymap_find(wm->defaultconf, "Timeline", SPACE_TIME, 0);
464 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
467 static void time_main_area_draw(const bContext *C, ARegion *ar)
469 /* draw entirely, view changes should be handled here */
470 Scene *scene = CTX_data_scene(C);
471 SpaceTime *stime = CTX_wm_space_time(C);
472 Object *obact = CTX_data_active_object(C);
473 View2D *v2d = &ar->v2d;
475 View2DScrollers *scrollers;
478 /* clear and setup matrix */
479 UI_ThemeClearColor(TH_BACK);
480 glClear(GL_COLOR_BUFFER_BIT);
482 UI_view2d_view_ortho(v2d);
485 unit = (stime->flag & TIME_DRAWFRAMES) ? V2D_UNIT_FRAMES : V2D_UNIT_SECONDS;
486 grid = UI_view2d_grid_calc(scene, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
487 UI_view2d_grid_draw(v2d, grid, (V2D_VERTICAL_LINES | V2D_VERTICAL_AXIS));
488 UI_view2d_grid_free(grid);
490 ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
492 /* start and end frame */
493 time_draw_sfra_efra(scene, v2d);
496 flag = DRAWCFRA_WIDE; /* this is only really needed on frames where there's a keyframe, but this will do... */
497 if ((stime->flag & TIME_DRAWFRAMES) == 0) flag |= DRAWCFRA_UNIT_SECONDS;
498 if (stime->flag & TIME_CFRA_NUM) flag |= DRAWCFRA_SHOW_NUMBOX;
499 ANIM_draw_cfra(C, v2d, flag);
501 UI_view2d_view_ortho(v2d);
504 time_draw_keyframes(C, stime, ar);
507 UI_view2d_view_orthoSpecial(ar, v2d, 1);
508 draw_markers_time(C, 0);
511 time_draw_cache(stime, obact, scene);
514 UI_view2d_view_ortho(v2d);
515 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
517 /* reset view matrix */
518 UI_view2d_view_restore(C);
521 scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
522 UI_view2d_scrollers_draw(C, v2d, scrollers);
523 UI_view2d_scrollers_free(scrollers);
526 static void time_main_area_listener(ARegion *ar, wmNotifier *wmn)
528 /* context changes */
529 switch (wmn->category) {
531 if (wmn->data == ND_SPACE_TIME)
532 ED_region_tag_redraw(ar);
536 ED_region_tag_redraw(ar);
546 case ND_RENDER_OPTIONS:
547 ED_region_tag_redraw(ar);
553 /* ************************ header time area region *********************** */
555 /* add handlers, stuff you only do once or on area/region changes */
556 static void time_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
558 ED_region_header_init(ar);
561 static void time_header_area_draw(const bContext *C, ARegion *ar)
563 ED_region_header(C, ar);
566 static void time_header_area_listener(ARegion *ar, wmNotifier *wmn)
568 /* context changes */
569 switch (wmn->category) {
571 if (wmn->data == ND_ANIMPLAY)
572 ED_region_tag_redraw(ar);
581 case ND_RENDER_OPTIONS:
582 ED_region_tag_redraw(ar);
587 if (wmn->data == ND_SPACE_TIME)
588 ED_region_tag_redraw(ar);
593 /* ******************** default callbacks for time space ***************** */
595 static SpaceLink *time_new(const bContext *C)
597 Scene *scene = CTX_data_scene(C);
601 stime = MEM_callocN(sizeof(SpaceTime), "inittime");
603 stime->spacetype = SPACE_TIME;
604 stime->flag |= TIME_DRAWFRAMES;
607 ar = MEM_callocN(sizeof(ARegion), "header for time");
609 BLI_addtail(&stime->regionbase, ar);
610 ar->regiontype = RGN_TYPE_HEADER;
611 ar->alignment = RGN_ALIGN_BOTTOM;
614 ar = MEM_callocN(sizeof(ARegion), "main area for time");
616 BLI_addtail(&stime->regionbase, ar);
617 ar->regiontype = RGN_TYPE_WINDOW;
619 ar->v2d.tot.xmin = (float)(SFRA - 4);
620 ar->v2d.tot.ymin = 0.0f;
621 ar->v2d.tot.xmax = (float)(EFRA + 4);
622 ar->v2d.tot.ymax = 50.0f;
624 ar->v2d.cur = ar->v2d.tot;
626 ar->v2d.min[0] = 1.0f;
627 ar->v2d.min[1] = 50.0f;
629 ar->v2d.max[0] = MAXFRAMEF;
630 ar->v2d.max[1] = 50.0;
632 ar->v2d.minzoom = 0.1f;
633 ar->v2d.maxzoom = 10.0;
635 ar->v2d.scroll |= (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
636 ar->v2d.align |= V2D_ALIGN_NO_NEG_Y;
637 ar->v2d.keepofs |= V2D_LOCKOFS_Y;
638 ar->v2d.keepzoom |= V2D_LOCKZOOM_Y;
641 return (SpaceLink *)stime;
644 /* not spacelink itself */
645 static void time_free(SpaceLink *sl)
647 SpaceTime *stime = (SpaceTime *)sl;
649 time_cache_free(stime);
651 /* spacetype; init callback in ED_area_initialize() */
652 /* init is called to (re)initialize an existing editor (file read, screen changes) */
653 /* validate spacedata, add own area level handlers */
654 static void time_init(wmWindowManager *UNUSED(wm), ScrArea *sa)
656 SpaceTime *stime = (SpaceTime *)sa->spacedata.first;
658 time_cache_free(stime);
660 /* enable all cache display */
661 stime->cache_display |= TIME_CACHE_DISPLAY;
662 stime->cache_display |= (TIME_CACHE_SOFTBODY | TIME_CACHE_PARTICLES);
663 stime->cache_display |= (TIME_CACHE_CLOTH | TIME_CACHE_SMOKE | TIME_CACHE_DYNAMICPAINT);
664 stime->cache_display |= TIME_CACHE_RIGIDBODY;
667 static SpaceLink *time_duplicate(SpaceLink *sl)
669 SpaceTime *stime = (SpaceTime *)sl;
670 SpaceTime *stimen = MEM_dupallocN(stime);
672 stimen->caches.first = stimen->caches.last = NULL;
674 return (SpaceLink *)stimen;
677 /* only called once, from space_api/spacetypes.c */
678 /* it defines all callbacks to maintain spaces */
679 void ED_spacetype_time(void)
681 SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype time");
684 st->spaceid = SPACE_TIME;
685 strncpy(st->name, "Timeline", BKE_ST_MAXNAME);
688 st->free = time_free;
689 st->init = time_init;
690 st->duplicate = time_duplicate;
691 st->operatortypes = time_operatortypes;
693 st->listener = time_listener;
694 st->refresh = time_refresh;
696 /* regions: main window */
697 art = MEM_callocN(sizeof(ARegionType), "spacetype time region");
698 art->regionid = RGN_TYPE_WINDOW;
699 art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
701 art->init = time_main_area_init;
702 art->draw = time_main_area_draw;
703 art->listener = time_main_area_listener;
704 art->keymap = time_keymap;
705 BLI_addhead(&st->regiontypes, art);
707 /* regions: header */
708 art = MEM_callocN(sizeof(ARegionType), "spacetype time region");
709 art->regionid = RGN_TYPE_HEADER;
710 art->prefsizey = HEADERY;
711 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
713 art->init = time_header_area_init;
714 art->draw = time_header_area_draw;
715 art->listener = time_header_area_listener;
716 BLI_addhead(&st->regiontypes, art);
718 BKE_spacetype_register(st);