e5bd9e62c74235812a301b1132fc898df76e2084
[blender-staging.git] / source / blender / editors / space_time / space_time.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_time/space_time.c
28  *  \ingroup sptime
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_dlrbTree.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_screen.h"
47 #include "BKE_pointcache.h"
48
49 #include "ED_anim_api.h"
50 #include "ED_keyframes_draw.h"
51 #include "ED_screen.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "BIF_gl.h"
57 #include "BIF_glutil.h"
58
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #include "ED_space_api.h"
63 #include "ED_markers.h"
64
65 #include "time_intern.h"
66
67 /* ************************ main time area region *********************** */
68
69 static void time_draw_sfra_efra(Scene *scene, View2D *v2d)
70 {       
71         /* draw darkened area outside of active timeline 
72          * frame range used is preview range or scene range 
73          */
74         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
75         glEnable(GL_BLEND);
76         glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
77                 
78         if (PSFRA < PEFRA) {
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);
81         }
82         else {
83                 glRectf(v2d->cur.xmin, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
84         }
85         glDisable(GL_BLEND);
86
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);
91 }
92
93 #define CACHE_DRAW_HEIGHT   3.0f
94
95 static void time_draw_cache(SpaceTime *stime, Object *ob, Scene *scene)
96 {
97         PTCacheID *pid;
98         ListBase pidlist;
99         SpaceTimeCache *stc = stime->caches.first;
100         float yoffs = 0.f;
101         
102         if (!(stime->cache_display & TIME_CACHE_DISPLAY) || (!ob))
103                 return;
104
105         BKE_ptcache_ids_from_object(&pidlist, ob, scene, 0);
106
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) {
110                 float col[4], *fp;
111                 int i, sta = pid->cache->startframe, end = pid->cache->endframe;
112                 int len = (end - sta + 1) * 4;
113
114                 switch (pid->type) {
115                         case PTCACHE_TYPE_SOFTBODY:
116                                 if (!(stime->cache_display & TIME_CACHE_SOFTBODY)) continue;
117                                 break;
118                         case PTCACHE_TYPE_PARTICLES:
119                                 if (!(stime->cache_display & TIME_CACHE_PARTICLES)) continue;
120                                 break;
121                         case PTCACHE_TYPE_CLOTH:
122                                 if (!(stime->cache_display & TIME_CACHE_CLOTH)) continue;
123                                 break;
124                         case PTCACHE_TYPE_SMOKE_DOMAIN:
125                         case PTCACHE_TYPE_SMOKE_HIGHRES:
126                                 if (!(stime->cache_display & TIME_CACHE_SMOKE)) continue;
127                                 break;
128                         case PTCACHE_TYPE_DYNAMICPAINT:
129                                 if (!(stime->cache_display & TIME_CACHE_DYNAMICPAINT)) continue;
130                                 break;
131                         case PTCACHE_TYPE_RIGIDBODY:
132                                 if (!(stime->cache_display & TIME_CACHE_RIGIDBODY)) continue;
133                                 break;
134                 }
135
136                 if (pid->cache->cached_frames == NULL)
137                         continue;
138
139                 /* make sure we have stc with correct array length */
140                 if (stc == NULL || MEM_allocN_len(stc->array) != len * 2 * sizeof(float)) {
141                         if (stc) {
142                                 MEM_freeN(stc->array);
143                         }
144                         else {
145                                 stc = MEM_callocN(sizeof(SpaceTimeCache), "spacetimecache");
146                                 BLI_addtail(&stime->caches, stc);
147                         }
148
149                         stc->array = MEM_callocN(len * 2 * sizeof(float), "SpaceTimeCache array");
150                 }
151
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;
156                                 fp[1] = 0.0;
157                                 fp += 2;
158                                 
159                                 fp[0] = (float)i - 0.5f;
160                                 fp[1] = 1.0;
161                                 fp += 2;
162                                 
163                                 fp[0] = (float)i + 0.5f;
164                                 fp[1] = 1.0;
165                                 fp += 2;
166                                 
167                                 fp[0] = (float)i + 0.5f;
168                                 fp[1] = 0.0;
169                                 fp += 2;
170                         }
171                 }
172                 
173                 glPushMatrix();
174                 glTranslatef(0.0, (float)V2D_SCROLL_HEIGHT + yoffs, 0.0);
175                 glScalef(1.0, CACHE_DRAW_HEIGHT, 0.0);
176                 
177                 switch (pid->type) {
178                         case PTCACHE_TYPE_SOFTBODY:
179                                 col[0] = 1.0;   col[1] = 0.4;   col[2] = 0.02;
180                                 col[3] = 0.1;
181                                 break;
182                         case PTCACHE_TYPE_PARTICLES:
183                                 col[0] = 1.0;   col[1] = 0.1;   col[2] = 0.02;
184                                 col[3] = 0.1;
185                                 break;
186                         case PTCACHE_TYPE_CLOTH:
187                                 col[0] = 0.1;   col[1] = 0.1;   col[2] = 0.75;
188                                 col[3] = 0.1;
189                                 break;
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;
193                                 col[3] = 0.1;
194                                 break;
195                         case PTCACHE_TYPE_DYNAMICPAINT:
196                                 col[0] = 1.0;   col[1] = 0.1;   col[2] = 0.75;
197                                 col[3] = 0.1;
198                                 break;
199                         case PTCACHE_TYPE_RIGIDBODY:
200                                 col[0] = 1.0;   col[1] = 0.6;   col[2] = 0.0;
201                                 col[3] = 0.1;
202                                 break;
203                         default:
204                                 BLI_assert(0);
205                                 col[0] = 1.0;   col[1] = 0.0;   col[2] = 1.0;
206                                 col[3] = 0.1;
207                 }
208                 glColor4fv(col);
209                 
210                 glEnable(GL_BLEND);
211                 
212                 glRectf((float)sta, 0.0, (float)end, 1.0);
213                 
214                 col[3] = 0.4f;
215                 if (pid->cache->flag & PTCACHE_BAKED) {
216                         col[0] -= 0.4f; col[1] -= 0.4f; col[2] -= 0.4f;
217                 }
218                 else if (pid->cache->flag & PTCACHE_OUTDATED) {
219                         col[0] += 0.4f; col[1] += 0.4f; col[2] += 0.4f;
220                 }
221                 glColor4fv(col);
222                 
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);
227                 
228                 glDisable(GL_BLEND);
229                 
230                 glPopMatrix();
231                 
232                 yoffs += CACHE_DRAW_HEIGHT;
233
234                 stc = stc->next;
235         }
236
237         BLI_freelistN(&pidlist);
238
239         /* free excessive caches */
240         while (stc) {
241                 SpaceTimeCache *tmp = stc->next;
242                 BLI_remlink(&stime->caches, stc);
243                 MEM_freeN(stc->array);
244                 MEM_freeN(stc);
245                 stc = tmp;
246         }
247 }
248
249 static void time_cache_free(SpaceTime *stime)
250 {
251         SpaceTimeCache *stc;
252         
253         for (stc = stime->caches.first; stc; stc = stc->next) {
254                 if (stc->array) {
255                         MEM_freeN(stc->array);
256                         stc->array = NULL;
257                 }
258         }
259         
260         BLI_freelistN(&stime->caches);
261 }
262
263 static void time_cache_refresh(SpaceTime *stime)
264 {
265         /* Free previous caches to indicate full refresh */
266         time_cache_free(stime);
267 }
268
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)
271 {
272         ActKeyColumn *akn = NULL;
273         
274         /* sanity checks */
275         if (ak == NULL)
276                 return NULL;
277         
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);
283                 
284         /* if no match found (or found match), just use the current one */
285         if (akn == NULL)
286                 return ak;
287         else
288                 return akn;
289 }
290
291 /* helper for time_draw_keyframes() */
292 static void time_draw_idblock_keyframes(View2D *v2d, ID *id, short onlysel)
293 {
294         bDopeSheet ads = {NULL};
295         DLRBT_Tree keys;
296         ActKeyColumn *ak;
297         
298         /* init binarytree-list for getting keyframes */
299         BLI_dlrbTree_init(&keys);
300         
301         /* init dopesheet settings */
302         if (onlysel)
303                 ads.filterflag |= ADS_FILTER_ONLYSEL;
304         
305         /* populate tree with keyframe nodes */
306         switch (GS(id->name)) {
307                 case ID_SCE:
308                         scene_to_keylist(&ads, (Scene *)id, &keys, NULL);
309                         break;
310                 case ID_OB:
311                         ob_to_keylist(&ads, (Object *)id, &keys, NULL);
312                         break;
313         }
314                 
315         /* build linked-list for searching */
316         BLI_dlrbTree_linkedlist_sync(&keys);
317         
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
322          */
323         glBegin(GL_LINES);
324         for (ak = time_cfra_find_ak(keys.root, v2d->cur.xmin);
325              (ak) && (ak->cfra <= v2d->cur.xmax);
326              ak = ak->next)
327         {
328                 glVertex2f(ak->cfra, v2d->tot.ymin);
329                 glVertex2f(ak->cfra, v2d->tot.ymax);
330         }
331         glEnd(); // GL_LINES
332                 
333         /* free temp stuff */
334         BLI_dlrbTree_free(&keys);
335 }
336
337 /* draw keyframe lines for timeline */
338 static void time_draw_keyframes(const bContext *C, SpaceTime *stime, ARegion *ar)
339 {
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);
344         
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
348          */
349         if (scene && onlysel == 0) {
350                 /* set draw color */
351                 glColor3ub(0xDD, 0xA7, 0x00);
352                 time_draw_idblock_keyframes(v2d, (ID *)scene, onlysel);
353         }
354         
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
358          *    be considered
359          */
360         glColor3ub(0xDD, 0xD7, 0x00);
361         
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);
365         }
366         else {
367                 short active_done = FALSE;
368                 
369                 /* draw keyframes from all selected objects */
370                 CTX_DATA_BEGIN (C, Object *, obsel, selected_objects)
371                 {
372                         /* last arg is 0, since onlysel doesn't apply here... */
373                         time_draw_idblock_keyframes(v2d, (ID *)obsel, 0);
374                         
375                         /* if this object is the active one, set flag so that we don't draw again */
376                         if (obsel == ob)
377                                 active_done = TRUE;
378                 }
379                 CTX_DATA_END;
380                 
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);
384         }
385 }
386
387 /* ---------------- */
388
389 static void time_refresh(const bContext *UNUSED(C), ScrArea *sa)
390 {
391         /* find the main timeline region and refresh cache display*/
392         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
393         if (ar) {
394                 SpaceTime *stime = (SpaceTime *)sa->spacedata.first;
395                 time_cache_refresh(stime);
396         }
397 }
398
399 /* editor level listener */
400 static void time_listener(ScrArea *sa, wmNotifier *wmn)
401 {
402
403         /* mainly for updating cache display */
404         switch (wmn->category) {
405                 case NC_OBJECT:
406                         switch (wmn->data) {
407                                 case ND_BONE_ACTIVE:
408                                 case ND_POINTCACHE:
409                                 case ND_MODIFIER:
410                                 case ND_PARTICLE:
411                                 case ND_KEYS:
412                                         ED_area_tag_refresh(sa);
413                                         ED_area_tag_redraw(sa);
414                                         break;
415                         }
416                         break;
417                 case NC_SCENE:
418                         switch (wmn->data) {
419                                 case ND_OB_ACTIVE:
420                                 case ND_FRAME:
421                                         ED_area_tag_refresh(sa);
422                                         break;
423                                 case ND_FRAME_RANGE:
424                                 {
425                                         ARegion *ar;
426                                         Scene *scene = wmn->reference;
427
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);
432                                                         break;
433                                                 }
434                                         }
435                                 }
436                                 break;
437                         }
438                 case NC_SPACE:
439                         switch (wmn->data) {
440                                 case ND_SPACE_CHANGED:
441                                         ED_area_tag_refresh(sa);
442                                         break;
443                         }
444                 case NC_WM:
445                         switch (wmn->data) {
446                                 case ND_FILEREAD:
447                                         ED_area_tag_refresh(sa);
448                                         break;
449                         }
450         }
451 }
452
453 /* ---------------- */
454
455 /* add handlers, stuff you only do once or on area/region changes */
456 static void time_main_area_init(wmWindowManager *wm, ARegion *ar)
457 {
458         wmKeyMap *keymap;
459         
460         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
461         
462         /* own keymap */
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);
465 }
466
467 static void time_main_area_draw(const bContext *C, ARegion *ar)
468 {
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;
474         View2DGrid *grid;
475         View2DScrollers *scrollers;
476         int unit, flag = 0;
477         
478         /* clear and setup matrix */
479         UI_ThemeClearColor(TH_BACK);
480         glClear(GL_COLOR_BUFFER_BIT);
481         
482         UI_view2d_view_ortho(v2d);
483         
484         /* grid */
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);
489         
490         /* start and end frame */
491         time_draw_sfra_efra(scene, v2d);
492         
493         /* current frame */
494         flag = DRAWCFRA_WIDE; /* this is only really needed on frames where there's a keyframe, but this will do... */
495         if ((stime->flag & TIME_DRAWFRAMES) == 0)  flag |= DRAWCFRA_UNIT_SECONDS;
496         if (stime->flag & TIME_CFRA_NUM)           flag |= DRAWCFRA_SHOW_NUMBOX;
497         ANIM_draw_cfra(C, v2d, flag);
498         
499         UI_view2d_view_ortho(v2d);
500         
501         /* keyframes */
502         time_draw_keyframes(C, stime, ar);
503         
504         /* markers */
505         UI_view2d_view_orthoSpecial(ar, v2d, 1);
506         draw_markers_time(C, 0);
507         
508         /* caches */
509         time_draw_cache(stime, obact, scene);
510         
511         /* reset view matrix */
512         UI_view2d_view_restore(C);
513         
514         /* scrollers */
515         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
516         UI_view2d_scrollers_draw(C, v2d, scrollers);
517         UI_view2d_scrollers_free(scrollers);
518 }
519
520 static void time_main_area_listener(ARegion *ar, wmNotifier *wmn)
521 {
522         /* context changes */
523         switch (wmn->category) {
524                 case NC_SPACE:
525                         if (wmn->data == ND_SPACE_TIME)
526                                 ED_region_tag_redraw(ar);
527                         break;
528
529                 case NC_ANIMATION:
530                         ED_region_tag_redraw(ar);
531                         break;
532                 
533                 case NC_SCENE:
534                         switch (wmn->data) {
535                                 case ND_OB_SELECT:
536                                 case ND_OB_ACTIVE:
537                                 case ND_FRAME:
538                                 case ND_FRAME_RANGE:
539                                 case ND_KEYINGSET:
540                                 case ND_RENDER_OPTIONS:
541                                         ED_region_tag_redraw(ar);
542                                         break;
543                         }
544         }
545 }
546
547 /* ************************ header time area region *********************** */
548
549 /* add handlers, stuff you only do once or on area/region changes */
550 static void time_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
551 {
552         ED_region_header_init(ar);
553 }
554
555 static void time_header_area_draw(const bContext *C, ARegion *ar)
556 {
557         ED_region_header(C, ar);
558 }
559
560 static void time_header_area_listener(ARegion *ar, wmNotifier *wmn)
561 {
562         /* context changes */
563         switch (wmn->category) {
564                 case NC_SCREEN:
565                         if (wmn->data == ND_ANIMPLAY)
566                                 ED_region_tag_redraw(ar);
567                         break;
568
569                 case NC_SCENE:
570                         switch (wmn->data) {
571                                 case ND_OB_SELECT:
572                                 case ND_FRAME:
573                                 case ND_FRAME_RANGE:
574                                 case ND_KEYINGSET:
575                                 case ND_RENDER_OPTIONS:
576                                         ED_region_tag_redraw(ar);
577                                         break;
578                         }
579
580                 case NC_SPACE:
581                         if (wmn->data == ND_SPACE_TIME)
582                                 ED_region_tag_redraw(ar);
583                         break;
584         }
585 }
586
587 /* ******************** default callbacks for time space ***************** */
588
589 static SpaceLink *time_new(const bContext *C)
590 {
591         Scene *scene = CTX_data_scene(C);
592         ARegion *ar;
593         SpaceTime *stime;
594
595         stime = MEM_callocN(sizeof(SpaceTime), "inittime");
596
597         stime->spacetype = SPACE_TIME;
598         stime->flag |= TIME_DRAWFRAMES;
599
600         /* header */
601         ar = MEM_callocN(sizeof(ARegion), "header for time");
602         
603         BLI_addtail(&stime->regionbase, ar);
604         ar->regiontype = RGN_TYPE_HEADER;
605         ar->alignment = RGN_ALIGN_BOTTOM;
606         
607         /* main area */
608         ar = MEM_callocN(sizeof(ARegion), "main area for time");
609         
610         BLI_addtail(&stime->regionbase, ar);
611         ar->regiontype = RGN_TYPE_WINDOW;
612         
613         ar->v2d.tot.xmin = (float)(SFRA - 4);
614         ar->v2d.tot.ymin = 0.0f;
615         ar->v2d.tot.xmax = (float)(EFRA + 4);
616         ar->v2d.tot.ymax = 50.0f;
617         
618         ar->v2d.cur = ar->v2d.tot;
619
620         ar->v2d.min[0] = 1.0f;
621         ar->v2d.min[1] = 50.0f;
622
623         ar->v2d.max[0] = MAXFRAMEF;
624         ar->v2d.max[1] = 50.0;
625
626         ar->v2d.minzoom = 0.1f;
627         ar->v2d.maxzoom = 10.0;
628
629         ar->v2d.scroll |= (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
630         ar->v2d.align |= V2D_ALIGN_NO_NEG_Y;
631         ar->v2d.keepofs |= V2D_LOCKOFS_Y;
632         ar->v2d.keepzoom |= V2D_LOCKZOOM_Y;
633
634
635         return (SpaceLink *)stime;
636 }
637
638 /* not spacelink itself */
639 static void time_free(SpaceLink *sl)
640 {
641         SpaceTime *stime = (SpaceTime *)sl;
642         
643         time_cache_free(stime);
644 }
645 /* spacetype; init callback in ED_area_initialize() */
646 /* init is called to (re)initialize an existing editor (file read, screen changes) */
647 /* validate spacedata, add own area level handlers */
648 static void time_init(wmWindowManager *UNUSED(wm), ScrArea *sa)
649 {
650         SpaceTime *stime = (SpaceTime *)sa->spacedata.first;
651         
652         time_cache_free(stime);
653         
654         /* enable all cache display */
655         stime->cache_display |= TIME_CACHE_DISPLAY;
656         stime->cache_display |= (TIME_CACHE_SOFTBODY | TIME_CACHE_PARTICLES);
657         stime->cache_display |= (TIME_CACHE_CLOTH | TIME_CACHE_SMOKE | TIME_CACHE_DYNAMICPAINT);
658         stime->cache_display |= TIME_CACHE_RIGIDBODY;
659 }
660
661 static SpaceLink *time_duplicate(SpaceLink *sl)
662 {
663         SpaceTime *stime = (SpaceTime *)sl;
664         SpaceTime *stimen = MEM_dupallocN(stime);
665         
666         stimen->caches.first = stimen->caches.last = NULL;
667         
668         return (SpaceLink *)stimen;
669 }
670
671 /* only called once, from space_api/spacetypes.c */
672 /* it defines all callbacks to maintain spaces */
673 void ED_spacetype_time(void)
674 {
675         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype time");
676         ARegionType *art;
677         
678         st->spaceid = SPACE_TIME;
679         strncpy(st->name, "Timeline", BKE_ST_MAXNAME);
680         
681         st->new = time_new;
682         st->free = time_free;
683         st->init = time_init;
684         st->duplicate = time_duplicate;
685         st->operatortypes = time_operatortypes;
686         st->keymap = NULL;
687         st->listener = time_listener;
688         st->refresh = time_refresh;
689         
690         /* regions: main window */
691         art = MEM_callocN(sizeof(ARegionType), "spacetype time region");
692         art->regionid = RGN_TYPE_WINDOW;
693         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
694         
695         art->init = time_main_area_init;
696         art->draw = time_main_area_draw;
697         art->listener = time_main_area_listener;
698         art->keymap = time_keymap;
699         BLI_addhead(&st->regiontypes, art);
700         
701         /* regions: header */
702         art = MEM_callocN(sizeof(ARegionType), "spacetype time region");
703         art->regionid = RGN_TYPE_HEADER;
704         art->prefsizey = HEADERY;
705         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
706         
707         art->init = time_header_area_init;
708         art->draw = time_header_area_draw;
709         art->listener = time_header_area_listener;
710         BLI_addhead(&st->regiontypes, art);
711                 
712         BKE_spacetype_register(st);
713 }