Bugfix #24099: nla content moves out of sync - with fix
[blender.git] / source / blender / editors / space_nla / space_nla.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_anim_types.h"
33 #include "DNA_scene_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_rand.h"
40
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_main.h"
44 #include "BKE_screen.h"
45
46 #include "ED_anim_api.h"
47 #include "ED_markers.h"
48 #include "ED_screen.h"
49
50 #include "BIF_gl.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
57
58 #include "nla_intern.h" // own include
59
60 /* ******************** manage regions ********************* */
61
62 ARegion *nla_has_buttons_region(ScrArea *sa)
63 {
64         ARegion *ar, *arnew;
65         
66         for (ar= sa->regionbase.first; ar; ar= ar->next) {
67                 if (ar->regiontype==RGN_TYPE_UI)
68                         return ar;
69         }
70         
71         /* add subdiv level; after main */
72         for (ar= sa->regionbase.first; ar; ar= ar->next) {
73                 if (ar->regiontype==RGN_TYPE_WINDOW)
74                         break;
75         }
76         
77         /* is error! */
78         if (ar==NULL) return NULL;
79         
80         arnew= MEM_callocN(sizeof(ARegion), "buttons for nla");
81         
82         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
83         arnew->regiontype= RGN_TYPE_UI;
84         arnew->alignment= RGN_ALIGN_RIGHT;
85         
86         arnew->flag = RGN_FLAG_HIDDEN;
87         
88         return arnew;
89 }
90
91
92
93 /* ******************** default callbacks for nla space ***************** */
94
95 static SpaceLink *nla_new(const bContext *C)
96 {
97         Scene *scene= CTX_data_scene(C);
98         ScrArea *sa= CTX_wm_area(C);
99         ARegion *ar;
100         SpaceNla *snla;
101         
102         snla= MEM_callocN(sizeof(SpaceNla), "initnla");
103         snla->spacetype= SPACE_NLA;
104         
105         /* allocate DopeSheet data for NLA Editor */
106         snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
107         snla->ads->source= (ID *)scene;
108         
109         /* set auto-snapping settings */
110         snla->autosnap = SACTSNAP_FRAME;
111         
112         /* header */
113         ar= MEM_callocN(sizeof(ARegion), "header for nla");
114         
115         BLI_addtail(&snla->regionbase, ar);
116         ar->regiontype= RGN_TYPE_HEADER;
117         ar->alignment= RGN_ALIGN_BOTTOM;
118         
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;
124         
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;
128         
129         /* ui buttons */
130         ar= MEM_callocN(sizeof(ARegion), "buttons area for nla");
131         
132         BLI_addtail(&snla->regionbase, ar);
133         ar->regiontype= RGN_TYPE_UI;
134         ar->alignment= RGN_ALIGN_RIGHT;
135         ar->flag = RGN_FLAG_HIDDEN;
136         
137         /* main area */
138         ar= MEM_callocN(sizeof(ARegion), "main area for nla");
139         
140         BLI_addtail(&snla->regionbase, ar);
141         ar->regiontype= RGN_TYPE_WINDOW;
142         
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;
147         
148         ar->v2d.cur = ar->v2d.tot;
149         
150         ar->v2d.min[0]= 0.0f;
151         ar->v2d.min[1]= 0.0f;
152         
153         ar->v2d.max[0]= MAXFRAMEF;
154         ar->v2d.max[1]= 10000.0f;
155         
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;
164         
165         return (SpaceLink *)snla;
166 }
167
168 /* not spacelink itself */
169 static void nla_free(SpaceLink *sl)
170 {       
171         SpaceNla *snla= (SpaceNla*) sl;
172         
173         if (snla->ads) {
174                 BLI_freelistN(&snla->ads->chanbase);
175                 MEM_freeN(snla->ads);
176         }
177 }
178
179
180 /* spacetype; init callback */
181 static void nla_init(struct wmWindowManager *wm, ScrArea *sa)
182 {
183         SpaceNla *snla= (SpaceNla *)sa->spacedata.first;
184         
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
189         }
190
191         ED_area_tag_refresh(sa);
192 }
193
194 static SpaceLink *nla_duplicate(SpaceLink *sl)
195 {
196         SpaceNla *snlan= MEM_dupallocN(sl);
197         
198         /* clear or remove stuff from old */
199         snlan->ads= MEM_dupallocN(snlan->ads);
200         
201         return (SpaceLink *)snlan;
202 }
203
204 /* add handlers, stuff you only do once or on area/region changes */
205 static void nla_channel_area_init(wmWindowManager *wm, ARegion *ar)
206 {
207         wmKeyMap *keymap;
208         
209         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
210         
211         /* own keymap */
212         // TODO: cannot use generic copy, need special NLA version
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         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
216         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
217 }
218
219 /* draw entirely, view changes should be handled here */
220 static void nla_channel_area_draw(const bContext *C, ARegion *ar)
221 {
222         SpaceNla *snla= CTX_wm_space_nla(C);
223         bAnimContext ac;
224         View2D *v2d= &ar->v2d;
225         View2DScrollers *scrollers;
226         
227         /* clear and setup matrix */
228         UI_ThemeClearColor(TH_BACK);
229         glClear(GL_COLOR_BUFFER_BIT);
230         
231         UI_view2d_view_ortho(v2d);
232         
233         /* data */
234         if (ANIM_animdata_get_context(C, &ac)) {
235                 draw_nla_channel_list((bContext *)C, &ac, snla, ar);
236         }
237         
238         /* reset view matrix */
239         UI_view2d_view_restore(C);
240         
241         /* scrollers */
242         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
243         UI_view2d_scrollers_draw(C, v2d, scrollers);
244         UI_view2d_scrollers_free(scrollers);
245 }
246
247
248 /* add handlers, stuff you only do once or on area/region changes */
249 static void nla_main_area_init(wmWindowManager *wm, ARegion *ar)
250 {
251         wmKeyMap *keymap;
252         
253         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
254         
255         /* own keymap */
256         keymap= WM_keymap_find(wm->defaultconf, "NLA Editor", SPACE_NLA, 0);
257         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
258         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
259         WM_event_add_keymap_handler(&ar->handlers, keymap);
260 }
261
262 static void nla_main_area_draw(const bContext *C, ARegion *ar)
263 {
264         /* draw entirely, view changes should be handled here */
265         SpaceNla *snla= CTX_wm_space_nla(C);
266         bAnimContext ac;
267         View2D *v2d= &ar->v2d;
268         View2DGrid *grid;
269         View2DScrollers *scrollers;
270         short unit=0, flag=0;
271         
272         /* clear and setup matrix */
273         UI_ThemeClearColor(TH_BACK);
274         glClear(GL_COLOR_BUFFER_BIT);
275         
276         UI_view2d_view_ortho(v2d);
277         
278         /* time grid */
279         unit= (snla->flag & SNLA_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
280         grid= UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
281         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
282         UI_view2d_grid_free(grid);
283         
284         /* data */
285         if (ANIM_animdata_get_context(C, &ac)) {
286                 /* strips and backdrops */
287                 draw_nla_main_data(&ac, snla, ar);
288                 
289                 /* text draw cached, in pixelspace now */
290                 UI_view2d_text_cache_draw(ar);
291         }
292         
293         UI_view2d_view_ortho(v2d);
294         
295         /* current frame */
296         if (snla->flag & SNLA_DRAWTIME)         flag |= DRAWCFRA_UNIT_SECONDS;
297         if ((snla->flag & SNLA_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
298         ANIM_draw_cfra(C, v2d, flag);
299         
300         /* markers */
301         UI_view2d_view_orthoSpecial(ar, v2d, 1);
302         draw_markers_time(C, 0);
303         
304         /* preview range */
305         UI_view2d_view_ortho(v2d);
306         ANIM_draw_previewrange(C, v2d);
307         
308         /* reset view matrix */
309         UI_view2d_view_restore(C);
310         
311         /* scrollers */
312         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
313         UI_view2d_scrollers_draw(C, v2d, scrollers);
314         UI_view2d_scrollers_free(scrollers);
315 }
316
317
318 /* add handlers, stuff you only do once or on area/region changes */
319 static void nla_header_area_init(wmWindowManager *wm, ARegion *ar)
320 {
321         ED_region_header_init(ar);
322 }
323
324 static void nla_header_area_draw(const bContext *C, ARegion *ar)
325 {
326         ED_region_header(C, ar);
327 }
328
329 /* add handlers, stuff you only do once or on area/region changes */
330 static void nla_buttons_area_init(wmWindowManager *wm, ARegion *ar)
331 {
332         wmKeyMap *keymap;
333         
334         ED_region_panels_init(wm, ar);
335         
336         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
337         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
338 }
339
340 static void nla_buttons_area_draw(const bContext *C, ARegion *ar)
341 {
342         ED_region_panels(C, ar, 1, NULL, -1);
343 }
344
345 static void nla_region_listener(ARegion *ar, wmNotifier *wmn)
346 {
347         /* context changes */
348         switch(wmn->category) {
349                 case NC_ANIMATION:
350                         ED_region_tag_redraw(ar);
351                         break;
352                 case NC_SCENE:
353                         switch(wmn->data) {
354                                 case ND_OB_ACTIVE:
355                                 case ND_FRAME:
356                                 case ND_MARKERS:
357                                         ED_region_tag_redraw(ar);
358                                         break;
359                         }
360                         break;
361                 case NC_OBJECT:
362                         switch(wmn->data) {
363                                 case ND_BONE_ACTIVE:
364                                 case ND_BONE_SELECT:
365                                 case ND_KEYS:
366                                         ED_region_tag_redraw(ar);
367                                         break;
368                         }
369                         break;
370                 default:
371                         if(wmn->data==ND_KEYS)
372                                 ED_region_tag_redraw(ar);
373                         break;
374         }
375 }
376
377
378 static void nla_main_area_listener(ARegion *ar, wmNotifier *wmn)
379 {
380         /* context changes */
381         switch(wmn->category) {
382                 case NC_ANIMATION:
383                         ED_region_tag_redraw(ar);
384                         break;
385                 case NC_SCENE:
386                         switch(wmn->data) {
387                                 case ND_RENDER_OPTIONS:
388                                 case ND_OB_ACTIVE:
389                                 case ND_FRAME:
390                                 case ND_MARKERS:
391                                         ED_region_tag_redraw(ar);
392                                         break;
393                         }
394                         break;
395                 case NC_OBJECT:
396                         switch(wmn->data) {
397                                 case ND_BONE_ACTIVE:
398                                 case ND_BONE_SELECT:
399                                 case ND_KEYS:
400                                 case ND_TRANSFORM:
401                                         ED_region_tag_redraw(ar);
402                                         break;
403                         }
404                         break;
405                 case NC_NODE:
406                         switch(wmn->action) {
407                                 case NA_EDITED:
408                                         ED_region_tag_redraw(ar);
409                                         break;
410                         }
411                         break;
412                 case NC_ID:
413                         if(wmn->action == NA_RENAME)
414                                 ED_region_tag_redraw(ar);
415                         break;
416                 default:
417                         if(wmn->data==ND_KEYS)
418                                 ED_region_tag_redraw(ar);
419         }
420 }
421
422 static void nla_channel_area_listener(ARegion *ar, wmNotifier *wmn)
423 {
424         /* context changes */
425         switch(wmn->category) {
426                 case NC_ANIMATION:
427                         ED_region_tag_redraw(ar);
428                         break;
429                 case NC_SCENE:
430                         switch(wmn->data) {
431                                 case ND_OB_ACTIVE:
432                                         ED_region_tag_redraw(ar);
433                                         break;
434                         }
435                         break;
436                 case NC_OBJECT:
437                         switch(wmn->data) {
438                                 case ND_BONE_ACTIVE:
439                                 case ND_BONE_SELECT:
440                                 case ND_KEYS:
441                                         ED_region_tag_redraw(ar);
442                                         break;
443                         }
444                         break;
445                 case NC_ID:
446                         if(wmn->action == NA_RENAME)
447                                 ED_region_tag_redraw(ar);
448                         break;
449
450                 default:
451                         if(wmn->data==ND_KEYS)
452                                 ED_region_tag_redraw(ar);
453         }
454 }
455
456 /* editor level listener */
457 static void nla_listener(ScrArea *sa, wmNotifier *wmn)
458 {
459         /* context changes */
460         switch (wmn->category) {
461                 case NC_ANIMATION:
462                         // TODO: filter specific types of changes?
463                         ED_area_tag_refresh(sa);
464                         break;
465                 case NC_SCENE:
466                         /*switch (wmn->data) {
467                                 case ND_OB_ACTIVE:
468                                 case ND_OB_SELECT:
469                                         ED_area_tag_refresh(sa);
470                                         break;
471                         }*/
472                         ED_area_tag_refresh(sa);
473                         break;
474                 case NC_OBJECT:
475                         switch (wmn->data) {
476                                 case ND_TRANSFORM:
477                                         /* do nothing */
478                                         break;
479                                 default:
480                                         ED_area_tag_refresh(sa);
481                                         break;
482                         }
483                         break;
484                 case NC_SPACE:
485                         if(wmn->data == ND_SPACE_NLA)
486                                 ED_area_tag_redraw(sa);
487                         break;
488         }
489 }
490
491 /* only called once, from space/spacetypes.c */
492 void ED_spacetype_nla(void)
493 {
494         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype nla");
495         ARegionType *art;
496         
497         st->spaceid= SPACE_NLA;
498         strncpy(st->name, "NLA", BKE_ST_MAXNAME);
499         
500         st->new= nla_new;
501         st->free= nla_free;
502         st->init= nla_init;
503         st->duplicate= nla_duplicate;
504         st->operatortypes= nla_operatortypes;
505         st->listener= nla_listener;
506         st->keymap= nla_keymap;
507         
508         /* regions: main window */
509         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
510         art->regionid = RGN_TYPE_WINDOW;
511         art->init= nla_main_area_init;
512         art->draw= nla_main_area_draw;
513         art->listener= nla_main_area_listener;
514         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
515
516         BLI_addhead(&st->regiontypes, art);
517         
518         /* regions: header */
519         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
520         art->regionid = RGN_TYPE_HEADER;
521         art->prefsizey= HEADERY;
522         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
523         
524         art->init= nla_header_area_init;
525         art->draw= nla_header_area_draw;
526         
527         BLI_addhead(&st->regiontypes, art);
528         
529         /* regions: channels */
530         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
531         art->regionid = RGN_TYPE_CHANNELS;
532         art->prefsizex= 200;
533         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
534         
535         art->init= nla_channel_area_init;
536         art->draw= nla_channel_area_draw;
537         art->listener= nla_channel_area_listener;
538         
539         BLI_addhead(&st->regiontypes, art);
540         
541         /* regions: UI buttons */
542         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
543         art->regionid = RGN_TYPE_UI;
544         art->prefsizex= 200;
545         art->keymapflag= ED_KEYMAP_UI;
546         art->listener= nla_region_listener;
547         art->init= nla_buttons_area_init;
548         art->draw= nla_buttons_area_draw;
549         
550         BLI_addhead(&st->regiontypes, art);
551
552         nla_buttons_register(art);
553         
554         
555         BKE_spacetype_register(st);
556 }
557