Fix #34941: Space.draw_handler_add now supports PRE_VIEW and POST_VIEW callbacks
[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         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
491
492         /* start and end frame */
493         time_draw_sfra_efra(scene, v2d);
494         
495         /* current frame */
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);
500         
501         UI_view2d_view_ortho(v2d);
502         
503         /* keyframes */
504         time_draw_keyframes(C, stime, ar);
505         
506         /* markers */
507         UI_view2d_view_orthoSpecial(ar, v2d, 1);
508         draw_markers_time(C, 0);
509         
510         /* caches */
511         time_draw_cache(stime, obact, scene);
512         
513         /* callback */
514         UI_view2d_view_ortho(v2d);
515         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
516
517         /* reset view matrix */
518         UI_view2d_view_restore(C);
519         
520         /* scrollers */
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);
524 }
525
526 static void time_main_area_listener(ARegion *ar, wmNotifier *wmn)
527 {
528         /* context changes */
529         switch (wmn->category) {
530                 case NC_SPACE:
531                         if (wmn->data == ND_SPACE_TIME)
532                                 ED_region_tag_redraw(ar);
533                         break;
534
535                 case NC_ANIMATION:
536                         ED_region_tag_redraw(ar);
537                         break;
538                 
539                 case NC_SCENE:
540                         switch (wmn->data) {
541                                 case ND_OB_SELECT:
542                                 case ND_OB_ACTIVE:
543                                 case ND_FRAME:
544                                 case ND_FRAME_RANGE:
545                                 case ND_KEYINGSET:
546                                 case ND_RENDER_OPTIONS:
547                                         ED_region_tag_redraw(ar);
548                                         break;
549                         }
550         }
551 }
552
553 /* ************************ header time area region *********************** */
554
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)
557 {
558         ED_region_header_init(ar);
559 }
560
561 static void time_header_area_draw(const bContext *C, ARegion *ar)
562 {
563         ED_region_header(C, ar);
564 }
565
566 static void time_header_area_listener(ARegion *ar, wmNotifier *wmn)
567 {
568         /* context changes */
569         switch (wmn->category) {
570                 case NC_SCREEN:
571                         if (wmn->data == ND_ANIMPLAY)
572                                 ED_region_tag_redraw(ar);
573                         break;
574
575                 case NC_SCENE:
576                         switch (wmn->data) {
577                                 case ND_OB_SELECT:
578                                 case ND_FRAME:
579                                 case ND_FRAME_RANGE:
580                                 case ND_KEYINGSET:
581                                 case ND_RENDER_OPTIONS:
582                                         ED_region_tag_redraw(ar);
583                                         break;
584                         }
585
586                 case NC_SPACE:
587                         if (wmn->data == ND_SPACE_TIME)
588                                 ED_region_tag_redraw(ar);
589                         break;
590         }
591 }
592
593 /* ******************** default callbacks for time space ***************** */
594
595 static SpaceLink *time_new(const bContext *C)
596 {
597         Scene *scene = CTX_data_scene(C);
598         ARegion *ar;
599         SpaceTime *stime;
600
601         stime = MEM_callocN(sizeof(SpaceTime), "inittime");
602
603         stime->spacetype = SPACE_TIME;
604         stime->flag |= TIME_DRAWFRAMES;
605
606         /* header */
607         ar = MEM_callocN(sizeof(ARegion), "header for time");
608         
609         BLI_addtail(&stime->regionbase, ar);
610         ar->regiontype = RGN_TYPE_HEADER;
611         ar->alignment = RGN_ALIGN_BOTTOM;
612         
613         /* main area */
614         ar = MEM_callocN(sizeof(ARegion), "main area for time");
615         
616         BLI_addtail(&stime->regionbase, ar);
617         ar->regiontype = RGN_TYPE_WINDOW;
618         
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;
623         
624         ar->v2d.cur = ar->v2d.tot;
625
626         ar->v2d.min[0] = 1.0f;
627         ar->v2d.min[1] = 50.0f;
628
629         ar->v2d.max[0] = MAXFRAMEF;
630         ar->v2d.max[1] = 50.0;
631
632         ar->v2d.minzoom = 0.1f;
633         ar->v2d.maxzoom = 10.0;
634
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;
639
640
641         return (SpaceLink *)stime;
642 }
643
644 /* not spacelink itself */
645 static void time_free(SpaceLink *sl)
646 {
647         SpaceTime *stime = (SpaceTime *)sl;
648         
649         time_cache_free(stime);
650 }
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)
655 {
656         SpaceTime *stime = (SpaceTime *)sa->spacedata.first;
657         
658         time_cache_free(stime);
659         
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;
665 }
666
667 static SpaceLink *time_duplicate(SpaceLink *sl)
668 {
669         SpaceTime *stime = (SpaceTime *)sl;
670         SpaceTime *stimen = MEM_dupallocN(stime);
671         
672         stimen->caches.first = stimen->caches.last = NULL;
673         
674         return (SpaceLink *)stimen;
675 }
676
677 /* only called once, from space_api/spacetypes.c */
678 /* it defines all callbacks to maintain spaces */
679 void ED_spacetype_time(void)
680 {
681         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype time");
682         ARegionType *art;
683         
684         st->spaceid = SPACE_TIME;
685         strncpy(st->name, "Timeline", BKE_ST_MAXNAME);
686         
687         st->new = time_new;
688         st->free = time_free;
689         st->init = time_init;
690         st->duplicate = time_duplicate;
691         st->operatortypes = time_operatortypes;
692         st->keymap = NULL;
693         st->listener = time_listener;
694         st->refresh = time_refresh;
695         
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;
700         
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);
706         
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;
712         
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);
717                 
718         BKE_spacetype_register(st);
719 }