8aceb647fe2b727812079b9c6e5d309b6923de8b
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_nla_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_rand.h"
44
45 #include "BKE_animsys.h"
46 #include "BKE_action.h"
47 #include "BKE_nla.h"
48 #include "BKE_colortools.h"
49 #include "BKE_context.h"
50 #include "BKE_global.h"
51 #include "BKE_main.h"
52 #include "BKE_screen.h"
53 #include "BKE_utildefines.h"
54
55 #include "ED_anim_api.h"
56 #include "ED_markers.h"
57 #include "ED_space_api.h"
58 #include "ED_screen.h"
59
60 #include "BIF_gl.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 #include "UI_interface.h"
66 #include "UI_resources.h"
67 #include "UI_view2d.h"
68
69 #include "nla_intern.h" // own include
70
71 /* ******************** manage regions ********************* */
72
73 ARegion *nla_has_buttons_region(ScrArea *sa)
74 {
75         ARegion *ar, *arnew;
76         
77         for (ar= sa->regionbase.first; ar; ar= ar->next) {
78                 if (ar->regiontype==RGN_TYPE_UI)
79                         return ar;
80         }
81         
82         /* add subdiv level; after main */
83         for (ar= sa->regionbase.first; ar; ar= ar->next) {
84                 if (ar->regiontype==RGN_TYPE_WINDOW)
85                         break;
86         }
87         
88         /* is error! */
89         if (ar==NULL) return NULL;
90         
91         arnew= MEM_callocN(sizeof(ARegion), "buttons for nla");
92         
93         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
94         arnew->regiontype= RGN_TYPE_UI;
95         arnew->alignment= RGN_ALIGN_RIGHT;
96         
97         arnew->flag = RGN_FLAG_HIDDEN;
98         
99         return arnew;
100 }
101
102
103
104 /* ******************** default callbacks for nla space ***************** */
105
106 static SpaceLink *nla_new(const bContext *C)
107 {
108         Scene *scene= CTX_data_scene(C);
109         ARegion *ar;
110         SpaceNla *snla;
111         
112         snla= MEM_callocN(sizeof(SpaceNla), "initnla");
113         snla->spacetype= SPACE_NLA;
114         
115         /* allocate DopeSheet data for NLA Editor */
116         snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
117         snla->ads->source= (ID *)scene;
118         
119         /* set auto-snapping settings */
120         snla->autosnap = SACTSNAP_FRAME;
121         
122         /* header */
123         ar= MEM_callocN(sizeof(ARegion), "header for nla");
124         
125         BLI_addtail(&snla->regionbase, ar);
126         ar->regiontype= RGN_TYPE_HEADER;
127         ar->alignment= RGN_ALIGN_BOTTOM;
128         
129         /* channel list region */
130         ar= MEM_callocN(sizeof(ARegion), "channel list for nla");
131         BLI_addtail(&snla->regionbase, ar);
132         ar->regiontype= RGN_TYPE_CHANNELS;
133         ar->alignment= RGN_ALIGN_LEFT;
134         
135                 /* only need to set these settings since this will use the 'stack' configuration */
136         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
137         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
138         
139         /* ui buttons */
140         ar= MEM_callocN(sizeof(ARegion), "buttons area for nla");
141         
142         BLI_addtail(&snla->regionbase, ar);
143         ar->regiontype= RGN_TYPE_UI;
144         ar->alignment= RGN_ALIGN_RIGHT;
145         ar->flag = RGN_FLAG_HIDDEN;
146         
147         /* main area */
148         ar= MEM_callocN(sizeof(ARegion), "main area for nla");
149         
150         BLI_addtail(&snla->regionbase, ar);
151         ar->regiontype= RGN_TYPE_WINDOW;
152         
153         ar->v2d.tot.xmin= (float)(SFRA-10);
154         ar->v2d.tot.ymin= -500.0f;
155         ar->v2d.tot.xmax= (float)(EFRA+10);
156         ar->v2d.tot.ymax= 0.0f;
157         
158         ar->v2d.cur = ar->v2d.tot;
159         
160         ar->v2d.min[0]= 0.0f;
161         ar->v2d.min[1]= 0.0f;
162         
163         ar->v2d.max[0]= MAXFRAMEF;
164         ar->v2d.max[1]= 10000.0f;
165         
166         ar->v2d.minzoom= 0.01f;
167         ar->v2d.maxzoom= 50;
168         ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
169         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
170         ar->v2d.keepzoom= V2D_LOCKZOOM_Y;
171         ar->v2d.align= V2D_ALIGN_NO_POS_Y;
172         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
173         
174         return (SpaceLink *)snla;
175 }
176
177 /* not spacelink itself */
178 static void nla_free(SpaceLink *sl)
179 {       
180         SpaceNla *snla= (SpaceNla*) sl;
181         
182         if (snla->ads) {
183                 BLI_freelistN(&snla->ads->chanbase);
184                 MEM_freeN(snla->ads);
185         }
186 }
187
188
189 /* spacetype; init callback */
190 static void nla_init(struct wmWindowManager *wm, ScrArea *sa)
191 {
192         SpaceNla *snla= (SpaceNla *)sa->spacedata.first;
193         
194         /* init dopesheet data if non-existant (i.e. for old files) */
195         if (snla->ads == NULL) {
196                 snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
197                 snla->ads->source= (ID *)G.main->scene.first; // XXX this is bad, but we need this to be set correct
198         }
199
200         ED_area_tag_refresh(sa);
201 }
202
203 static SpaceLink *nla_duplicate(SpaceLink *sl)
204 {
205         SpaceNla *snlan= MEM_dupallocN(sl);
206         
207         /* clear or remove stuff from old */
208         snlan->ads= MEM_dupallocN(snlan->ads);
209         
210         return (SpaceLink *)snlan;
211 }
212
213 /* add handlers, stuff you only do once or on area/region changes */
214 static void nla_channel_area_init(wmWindowManager *wm, ARegion *ar)
215 {
216         wmKeyMap *keymap;
217         
218         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
219         
220         /* own keymap */
221         // TODO: cannot use generic copy, need special NLA version
222         keymap= WM_keymap_find(wm->defaultconf, "NLA Channels", SPACE_NLA, 0);
223         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
224         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
225         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
226 }
227
228 /* draw entirely, view changes should be handled here */
229 static void nla_channel_area_draw(const bContext *C, ARegion *ar)
230 {
231         SpaceNla *snla= CTX_wm_space_nla(C);
232         bAnimContext ac;
233         View2D *v2d= &ar->v2d;
234         View2DScrollers *scrollers;
235         float col[3];
236         
237         /* clear and setup matrix */
238         UI_GetThemeColor3fv(TH_BACK, col);
239         glClearColor(col[0], col[1], col[2], 0.0);
240         glClear(GL_COLOR_BUFFER_BIT);
241         
242         UI_view2d_view_ortho(C, v2d);
243         
244         /* data */
245         if (ANIM_animdata_get_context(C, &ac)) {
246                 draw_nla_channel_list((bContext *)C, &ac, snla, ar);
247         }
248         
249         /* reset view matrix */
250         UI_view2d_view_restore(C);
251         
252         /* scrollers */
253         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
254         UI_view2d_scrollers_draw(C, v2d, scrollers);
255         UI_view2d_scrollers_free(scrollers);
256 }
257
258
259 /* add handlers, stuff you only do once or on area/region changes */
260 static void nla_main_area_init(wmWindowManager *wm, ARegion *ar)
261 {
262         wmKeyMap *keymap;
263         
264         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
265         
266         /* own keymap */
267         keymap= WM_keymap_find(wm->defaultconf, "NLA Data", SPACE_NLA, 0);
268         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
269         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
270         WM_event_add_keymap_handler(&ar->handlers, keymap);
271 }
272
273 static void nla_main_area_draw(const bContext *C, ARegion *ar)
274 {
275         /* draw entirely, view changes should be handled here */
276         SpaceNla *snla= CTX_wm_space_nla(C);
277         bAnimContext ac;
278         View2D *v2d= &ar->v2d;
279         View2DGrid *grid;
280         View2DScrollers *scrollers;
281         float col[3];
282         short unit=0, flag=0;
283         
284         /* clear and setup matrix */
285         UI_GetThemeColor3fv(TH_BACK, col);
286         glClearColor(col[0], col[1], col[2], 0.0);
287         glClear(GL_COLOR_BUFFER_BIT);
288         
289         UI_view2d_view_ortho(C, v2d);
290         
291         /* time grid */
292         unit= (snla->flag & SNLA_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
293         grid= UI_view2d_grid_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
294         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
295         UI_view2d_grid_free(grid);
296         
297         /* data */
298         if (ANIM_animdata_get_context(C, &ac)) {
299                 /* strips and backdrops */
300                 draw_nla_main_data(&ac, snla, ar);
301                 
302                 /* text draw cached, in pixelspace now */
303                 UI_view2d_text_cache_draw(ar);
304         }
305         
306         UI_view2d_view_ortho(C, v2d);
307         
308         /* current frame */
309         if (snla->flag & SNLA_DRAWTIME)         flag |= DRAWCFRA_UNIT_SECONDS;
310         if ((snla->flag & SNLA_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
311         ANIM_draw_cfra(C, v2d, flag);
312         
313         /* markers */
314         UI_view2d_view_orthoSpecial(C, v2d, 1);
315         draw_markers_time(C, 0);
316         
317         /* preview range */
318         UI_view2d_view_ortho(C, v2d);
319         ANIM_draw_previewrange(C, v2d);
320         
321         /* reset view matrix */
322         UI_view2d_view_restore(C);
323         
324         /* scrollers */
325         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
326         UI_view2d_scrollers_draw(C, v2d, scrollers);
327         UI_view2d_scrollers_free(scrollers);
328 }
329
330
331 /* add handlers, stuff you only do once or on area/region changes */
332 static void nla_header_area_init(wmWindowManager *wm, ARegion *ar)
333 {
334         ED_region_header_init(ar);
335 }
336
337 static void nla_header_area_draw(const bContext *C, ARegion *ar)
338 {
339         ED_region_header(C, ar);
340 }
341
342 /* add handlers, stuff you only do once or on area/region changes */
343 static void nla_buttons_area_init(wmWindowManager *wm, ARegion *ar)
344 {
345         wmKeyMap *keymap;
346         
347         ED_region_panels_init(wm, ar);
348         
349         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
350         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
351 }
352
353 static void nla_buttons_area_draw(const bContext *C, ARegion *ar)
354 {
355         ED_region_panels(C, ar, 1, NULL, -1);
356 }
357
358 static void nla_region_listener(ARegion *ar, wmNotifier *wmn)
359 {
360         /* context changes */
361         switch(wmn->category) {
362                 case NC_ANIMATION:
363                         ED_region_tag_redraw(ar);
364                         break;
365                 case NC_SCENE:
366                         switch(wmn->data) {
367                                 case ND_OB_ACTIVE:
368                                 case ND_FRAME:
369                                 case ND_MARKERS:
370                                         ED_region_tag_redraw(ar);
371                                         break;
372                         }
373                         break;
374                 case NC_OBJECT:
375                         switch(wmn->data) {
376                                 case ND_BONE_ACTIVE:
377                                 case ND_BONE_SELECT:
378                                 case ND_KEYS:
379                                         ED_region_tag_redraw(ar);
380                                         break;
381                         }
382                         break;
383                 default:
384                         if(wmn->data==ND_KEYS)
385                                 ED_region_tag_redraw(ar);
386                         break;
387         }
388 }
389
390
391 static void nla_main_area_listener(ARegion *ar, wmNotifier *wmn)
392 {
393         /* context changes */
394         switch(wmn->category) {
395                 case NC_ANIMATION:
396                         ED_region_tag_redraw(ar);
397                         break;
398                 case NC_SCENE:
399                         switch(wmn->data) {
400                                 case ND_RENDER_OPTIONS:
401                                 case ND_OB_ACTIVE:
402                                 case ND_FRAME:
403                                 case ND_MARKERS:
404                                         ED_region_tag_redraw(ar);
405                                         break;
406                         }
407                         break;
408                 case NC_OBJECT:
409                         switch(wmn->data) {
410                                 case ND_BONE_ACTIVE:
411                                 case ND_BONE_SELECT:
412                                 case ND_KEYS:
413                                 case ND_TRANSFORM:
414                                         ED_region_tag_redraw(ar);
415                                         break;
416                         }
417                         break;
418                 case NC_NODE:
419                         switch(wmn->action) {
420                                 case NA_EDITED:
421                                         ED_region_tag_redraw(ar);
422                                         break;
423                         }
424                         break;
425                 case NC_ID:
426                         switch(wmn->data) {
427                                 case ND_ID_RENAME:
428                                         ED_region_tag_redraw(ar);
429                                         break;
430                         }
431                         break;
432                 default:
433                         if(wmn->data==ND_KEYS)
434                                 ED_region_tag_redraw(ar);
435         }
436 }
437
438 static void nla_channel_area_listener(ARegion *ar, wmNotifier *wmn)
439 {
440         /* context changes */
441         switch(wmn->category) {
442                 case NC_ANIMATION:
443                         ED_region_tag_redraw(ar);
444                         break;
445                 case NC_SCENE:
446                         switch(wmn->data) {
447                                 case ND_OB_ACTIVE:
448                                         ED_region_tag_redraw(ar);
449                                         break;
450                         }
451                         break;
452                 case NC_OBJECT:
453                         switch(wmn->data) {
454                                 case ND_BONE_ACTIVE:
455                                 case ND_BONE_SELECT:
456                                 case ND_KEYS:
457                                         ED_region_tag_redraw(ar);
458                                         break;
459                         }
460                         break;
461                 case NC_ID:
462                         switch(wmn->data) {
463                                 case ND_ID_RENAME:
464                                         ED_region_tag_redraw(ar);
465                                         break;
466                         }
467                         break;
468                 default:
469                         if(wmn->data==ND_KEYS)
470                                 ED_region_tag_redraw(ar);
471         }
472 }
473
474 /* editor level listener */
475 static void nla_listener(ScrArea *sa, wmNotifier *wmn)
476 {
477         /* context changes */
478         switch (wmn->category) {
479                 case NC_ANIMATION:
480                         // TODO: filter specific types of changes?
481                         ED_area_tag_refresh(sa);
482                         break;
483                 case NC_SCENE:
484                         /*switch (wmn->data) {
485                                 case ND_OB_ACTIVE:
486                                 case ND_OB_SELECT:
487                                         ED_area_tag_refresh(sa);
488                                         break;
489                         }*/
490                         ED_area_tag_refresh(sa);
491                         break;
492                 case NC_OBJECT:
493                         /*switch (wmn->data) {
494                                 case ND_BONE_SELECT:
495                                 case ND_BONE_ACTIVE:
496                                         ED_area_tag_refresh(sa);
497                                         break;
498                         }*/
499                         ED_area_tag_refresh(sa);
500                         break;
501                 case NC_SPACE:
502                         if(wmn->data == ND_SPACE_NLA)
503                                 ED_area_tag_redraw(sa);
504                         break;
505         }
506 }
507
508 /* only called once, from space/spacetypes.c */
509 void ED_spacetype_nla(void)
510 {
511         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype nla");
512         ARegionType *art;
513         
514         st->spaceid= SPACE_NLA;
515         
516         st->new= nla_new;
517         st->free= nla_free;
518         st->init= nla_init;
519         st->duplicate= nla_duplicate;
520         st->operatortypes= nla_operatortypes;
521         st->listener= nla_listener;
522         st->keymap= nla_keymap;
523         
524         /* regions: main window */
525         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
526         art->regionid = RGN_TYPE_WINDOW;
527         art->init= nla_main_area_init;
528         art->draw= nla_main_area_draw;
529         art->listener= nla_main_area_listener;
530         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
531
532         BLI_addhead(&st->regiontypes, art);
533         
534         /* regions: header */
535         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
536         art->regionid = RGN_TYPE_HEADER;
537         art->minsizey= HEADERY;
538         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
539         
540         art->init= nla_header_area_init;
541         art->draw= nla_header_area_draw;
542         
543         BLI_addhead(&st->regiontypes, art);
544         
545         /* regions: channels */
546         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
547         art->regionid = RGN_TYPE_CHANNELS;
548         art->minsizex= 200;
549         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
550         
551         art->init= nla_channel_area_init;
552         art->draw= nla_channel_area_draw;
553         art->listener= nla_channel_area_listener;
554         
555         BLI_addhead(&st->regiontypes, art);
556         
557         /* regions: UI buttons */
558         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
559         art->regionid = RGN_TYPE_UI;
560         art->minsizex= 200;
561         art->keymapflag= ED_KEYMAP_UI;
562         art->listener= nla_region_listener;
563         art->init= nla_buttons_area_init;
564         art->draw= nla_buttons_area_draw;
565         
566         BLI_addhead(&st->regiontypes, art);
567
568         nla_buttons_register(art);
569         
570         
571         BKE_spacetype_register(st);
572 }
573