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)
99 SpaceTimeCache *stc = stime->caches.first;
102 if (!(stime->cache_display & TIME_CACHE_DISPLAY) || (!ob))
105 BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 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;
130 if(pid->cache->cached_frames == NULL)
134 /* make sure we have stc with correct array length */
135 if(stc == NULL || MEM_allocN_len(stc->array) != len*2*sizeof(float)) {
137 MEM_freeN(stc->array);
140 stc = MEM_callocN(sizeof(SpaceTimeCache), "spacetimecache");
141 BLI_addtail(&stime->caches, stc);
144 stc->array = MEM_callocN(len*2*sizeof(float), "SpaceTimeCache array");
147 /* fill the vertex array with a quad for each cached frame */
148 for (i=sta, fp=stc->array; i<=end; i++) {
149 if (pid->cache->cached_frames[i-sta]) {
150 fp[0] = (float)i-0.5f;
154 fp[0] = (float)i-0.5f;
158 fp[0] = (float)i+0.5f;
162 fp[0] = (float)i+0.5f;
169 glTranslatef(0.0, (float)V2D_SCROLL_HEIGHT+yoffs, 0.0);
170 glScalef(1.0, CACHE_DRAW_HEIGHT, 0.0);
173 case PTCACHE_TYPE_SOFTBODY:
174 col[0] = 1.0; col[1] = 0.4; col[2] = 0.02;
177 case PTCACHE_TYPE_PARTICLES:
178 col[0] = 1.0; col[1] = 0.1; col[2] = 0.02;
181 case PTCACHE_TYPE_CLOTH:
182 col[0] = 0.1; col[1] = 0.1; col[2] = 0.75;
185 case PTCACHE_TYPE_SMOKE_DOMAIN:
186 case PTCACHE_TYPE_SMOKE_HIGHRES:
187 col[0] = 0.2; col[1] = 0.2; col[2] = 0.2;
195 glRectf((float)sta, 0.0, (float)end, 1.0);
198 if (pid->cache->flag & PTCACHE_BAKED) {
199 col[0] -= 0.4f; col[1] -= 0.4f; col[2] -= 0.4f;
203 glEnableClientState(GL_VERTEX_ARRAY);
204 glVertexPointer(2, GL_FLOAT, 0, stc->array);
205 glDrawArrays(GL_QUADS, 0, (fp-stc->array)/2);
206 glDisableClientState(GL_VERTEX_ARRAY);
212 yoffs += CACHE_DRAW_HEIGHT;
217 BLI_freelistN(&pidlist);
219 /* free excessive caches */
221 SpaceTimeCache *tmp = stc->next;
222 BLI_remlink(&stime->caches, stc);
223 MEM_freeN(stc->array);
229 static void time_cache_free(SpaceTime *stime)
233 for (stc= stime->caches.first; stc; stc=stc->next) {
235 MEM_freeN(stc->array);
240 BLI_freelistN(&stime->caches);
243 static void time_cache_refresh(SpaceTime *stime)
245 /* Free previous caches to indicate full refresh */
246 time_cache_free(stime);
249 /* helper function - find actkeycolumn that occurs on cframe, or the nearest one if not found */
250 static ActKeyColumn *time_cfra_find_ak (ActKeyColumn *ak, float cframe)
252 ActKeyColumn *akn= NULL;
258 /* check if this is a match, or whether it is in some subtree */
259 if (cframe < ak->cfra)
260 akn= time_cfra_find_ak(ak->left, cframe);
261 else if (cframe > ak->cfra)
262 akn= time_cfra_find_ak(ak->right, cframe);
264 /* if no match found (or found match), just use the current one */
271 /* helper for time_draw_keyframes() */
272 static void time_draw_idblock_keyframes(View2D *v2d, ID *id, short onlysel)
274 bDopeSheet ads= {NULL};
278 /* init binarytree-list for getting keyframes */
279 BLI_dlrbTree_init(&keys);
281 /* init dopesheet settings */
283 ads.filterflag |= ADS_FILTER_ONLYSEL;
285 /* populate tree with keyframe nodes */
286 switch (GS(id->name)) {
288 scene_to_keylist(&ads, (Scene *)id, &keys, NULL);
291 ob_to_keylist(&ads, (Object *)id, &keys, NULL);
295 /* build linked-list for searching */
296 BLI_dlrbTree_linkedlist_sync(&keys);
298 /* start drawing keyframes
299 * - we use the binary-search capabilities of the tree to only start from
300 * the first visible keyframe (last one can then be easily checked)
301 * - draw within a single GL block to be faster
304 for ( ak=time_cfra_find_ak(keys.root, v2d->cur.xmin);
305 (ak) && (ak->cfra <= v2d->cur.xmax);
308 glVertex2f(ak->cfra, v2d->tot.ymin);
309 glVertex2f(ak->cfra, v2d->tot.ymax);
313 /* free temp stuff */
314 BLI_dlrbTree_free(&keys);
317 /* draw keyframe lines for timeline */
318 static void time_draw_keyframes(const bContext *C, SpaceTime *stime, ARegion *ar)
320 Scene *scene= CTX_data_scene(C);
321 Object *ob= CTX_data_active_object(C);
322 View2D *v2d= &ar->v2d;
323 short onlysel= (stime->flag & TIME_ONLYACTSEL);
325 /* draw scene keyframes first
326 * - don't try to do this when only drawing active/selected data keyframes,
327 * since this can become quite slow
329 if (scene && onlysel==0) {
331 glColor3ub(0xDD, 0xA7, 0x00);
332 time_draw_idblock_keyframes(v2d, (ID *)scene, onlysel);
335 /* draw keyframes from selected objects
336 * - only do the active object if in posemode (i.e. showing only keyframes for the bones)
337 * OR the onlysel flag was set, which means that only active object's keyframes should
340 glColor3ub(0xDD, 0xD7, 0x00);
342 if (ob && ((ob->mode == OB_MODE_POSE) || onlysel)) {
343 /* draw keyframes for active object only */
344 time_draw_idblock_keyframes(v2d, (ID *)ob, onlysel);
347 short active_done = 0;
349 /* draw keyframes from all selected objects */
350 CTX_DATA_BEGIN(C, Object*, obsel, selected_objects)
352 /* last arg is 0, since onlysel doesn't apply here... */
353 time_draw_idblock_keyframes(v2d, (ID *)obsel, 0);
355 /* if this object is the active one, set flag so that we don't draw again */
361 /* if active object hasn't been done yet, draw it... */
362 if (ob && (active_done == 0))
363 time_draw_idblock_keyframes(v2d, (ID *)ob, 0);
367 /* ---------------- */
369 static void time_refresh(const bContext *UNUSED(C), ScrArea *sa)
371 /* find the main timeline region and refresh cache display*/
372 ARegion *ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
374 SpaceTime *stime = (SpaceTime *)sa->spacedata.first;
375 time_cache_refresh(stime);
379 /* editor level listener */
380 static void time_listener(ScrArea *sa, wmNotifier *wmn)
383 /* mainly for updating cache display */
384 switch (wmn->category) {
391 ED_area_tag_refresh(sa);
392 ED_area_tag_redraw(sa);
400 ED_area_tag_refresh(sa);
405 Scene *scene = wmn->reference;
407 for (ar= sa->regionbase.first; ar; ar= ar->next) {
408 if (ar->regiontype==RGN_TYPE_WINDOW) {
409 ar->v2d.tot.xmin= (float)(SFRA - 4);
410 ar->v2d.tot.xmax= (float)(EFRA + 4);
419 case ND_SPACE_CHANGED:
420 ED_area_tag_refresh(sa);
426 ED_area_tag_refresh(sa);
432 /* ---------------- */
434 /* add handlers, stuff you only do once or on area/region changes */
435 static void time_main_area_init(wmWindowManager *wm, ARegion *ar)
439 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
442 keymap= WM_keymap_find(wm->defaultconf, "Timeline", SPACE_TIME, 0);
443 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
446 static void time_main_area_draw(const bContext *C, ARegion *ar)
448 /* draw entirely, view changes should be handled here */
449 Scene *scene= CTX_data_scene(C);
450 SpaceTime *stime= CTX_wm_space_time(C);
451 Object *obact = CTX_data_active_object(C);
452 View2D *v2d= &ar->v2d;
454 View2DScrollers *scrollers;
457 /* clear and setup matrix */
458 UI_ThemeClearColor(TH_BACK);
459 glClear(GL_COLOR_BUFFER_BIT);
461 UI_view2d_view_ortho(v2d);
464 unit= (stime->flag & TIME_DRAWFRAMES)? V2D_UNIT_FRAMES: V2D_UNIT_SECONDS;
465 grid= UI_view2d_grid_calc(scene, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
466 UI_view2d_grid_draw(v2d, grid, (V2D_VERTICAL_LINES|V2D_VERTICAL_AXIS));
467 UI_view2d_grid_free(grid);
469 /* start and end frame */
470 time_draw_sfra_efra(scene, v2d);
473 flag = DRAWCFRA_WIDE; /* this is only really needed on frames where there's a keyframe, but this will do... */
474 if ((stime->flag & TIME_DRAWFRAMES)==0) flag |= DRAWCFRA_UNIT_SECONDS;
475 if (stime->flag & TIME_CFRA_NUM) flag |= DRAWCFRA_SHOW_NUMBOX;
476 ANIM_draw_cfra(C, v2d, flag);
478 UI_view2d_view_ortho(v2d);
481 time_draw_keyframes(C, stime, ar);
484 UI_view2d_view_orthoSpecial(ar, v2d, 1);
485 draw_markers_time(C, 0);
488 time_draw_cache(stime, obact);
490 /* reset view matrix */
491 UI_view2d_view_restore(C);
494 scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
495 UI_view2d_scrollers_draw(C, v2d, scrollers);
496 UI_view2d_scrollers_free(scrollers);
499 static void time_main_area_listener(ARegion *ar, wmNotifier *wmn)
501 /* context changes */
502 switch(wmn->category) {
504 if(wmn->data == ND_SPACE_TIME)
505 ED_region_tag_redraw(ar);
509 ED_region_tag_redraw(ar);
519 case ND_RENDER_OPTIONS:
520 ED_region_tag_redraw(ar);
526 /* ************************ header time area region *********************** */
528 /* add handlers, stuff you only do once or on area/region changes */
529 static void time_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
531 ED_region_header_init(ar);
534 static void time_header_area_draw(const bContext *C, ARegion *ar)
536 ED_region_header(C, ar);
539 static void time_header_area_listener(ARegion *ar, wmNotifier *wmn)
541 /* context changes */
542 switch(wmn->category) {
544 if(wmn->data==ND_ANIMPLAY)
545 ED_region_tag_redraw(ar);
554 case ND_RENDER_OPTIONS:
555 ED_region_tag_redraw(ar);
560 if(wmn->data == ND_SPACE_TIME)
561 ED_region_tag_redraw(ar);
566 /* ******************** default callbacks for time space ***************** */
568 static SpaceLink *time_new(const bContext *C)
570 Scene *scene= CTX_data_scene(C);
574 stime= MEM_callocN(sizeof(SpaceTime), "inittime");
576 stime->spacetype= SPACE_TIME;
577 stime->redraws= TIME_ALL_3D_WIN|TIME_ALL_ANIM_WIN; // XXX: depreceated
578 stime->flag |= TIME_DRAWFRAMES;
581 ar= MEM_callocN(sizeof(ARegion), "header for time");
583 BLI_addtail(&stime->regionbase, ar);
584 ar->regiontype= RGN_TYPE_HEADER;
585 ar->alignment= RGN_ALIGN_BOTTOM;
588 ar= MEM_callocN(sizeof(ARegion), "main area for time");
590 BLI_addtail(&stime->regionbase, ar);
591 ar->regiontype= RGN_TYPE_WINDOW;
593 ar->v2d.tot.xmin= (float)(SFRA - 4);
594 ar->v2d.tot.ymin= 0.0f;
595 ar->v2d.tot.xmax= (float)(EFRA + 4);
596 ar->v2d.tot.ymax= 50.0f;
598 ar->v2d.cur= ar->v2d.tot;
600 ar->v2d.min[0]= 1.0f;
601 ar->v2d.min[1]= 50.0f;
603 ar->v2d.max[0]= MAXFRAMEF;
604 ar->v2d.max[1]= 50.0;
606 ar->v2d.minzoom= 0.1f;
607 ar->v2d.maxzoom= 10.0;
609 ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
610 ar->v2d.align |= V2D_ALIGN_NO_NEG_Y;
611 ar->v2d.keepofs |= V2D_LOCKOFS_Y;
612 ar->v2d.keepzoom |= V2D_LOCKZOOM_Y;
615 return (SpaceLink*)stime;
618 /* not spacelink itself */
619 static void time_free(SpaceLink *sl)
621 SpaceTime *stime= (SpaceTime *)sl;
623 time_cache_free(stime);
625 /* spacetype; init callback in ED_area_initialize() */
626 /* init is called to (re)initialize an existing editor (file read, screen changes) */
627 /* validate spacedata, add own area level handlers */
628 static void time_init(wmWindowManager *UNUSED(wm), ScrArea *sa)
630 SpaceTime *stime= (SpaceTime *)sa->spacedata.first;
632 time_cache_free(stime);
634 /* enable all cache display */
635 stime->cache_display |= TIME_CACHE_DISPLAY;
636 stime->cache_display |= (TIME_CACHE_SOFTBODY|TIME_CACHE_PARTICLES);
637 stime->cache_display |= (TIME_CACHE_CLOTH|TIME_CACHE_SMOKE);
640 static SpaceLink *time_duplicate(SpaceLink *sl)
642 SpaceTime *stime= (SpaceTime *)sl;
643 SpaceTime *stimen= MEM_dupallocN(stime);
645 stimen->caches.first = stimen->caches.last = NULL;
647 return (SpaceLink *)stimen;
650 /* only called once, from space_api/spacetypes.c */
651 /* it defines all callbacks to maintain spaces */
652 void ED_spacetype_time(void)
654 SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype time");
657 st->spaceid= SPACE_TIME;
658 strncpy(st->name, "Timeline", BKE_ST_MAXNAME);
663 st->duplicate= time_duplicate;
664 st->operatortypes= time_operatortypes;
666 st->listener= time_listener;
667 st->refresh= time_refresh;
669 /* regions: main window */
670 art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
671 art->regionid = RGN_TYPE_WINDOW;
672 art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_MARKERS|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
674 art->init= time_main_area_init;
675 art->draw= time_main_area_draw;
676 art->listener= time_main_area_listener;
677 art->keymap= time_keymap;
678 BLI_addhead(&st->regiontypes, art);
680 /* regions: header */
681 art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
682 art->regionid = RGN_TYPE_HEADER;
683 art->prefsizey= HEADERY;
684 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
686 art->init= time_header_area_init;
687 art->draw= time_header_area_draw;
688 art->listener= time_header_area_listener;
689 BLI_addhead(&st->regiontypes, art);
691 BKE_spacetype_register(st);