77407769f3b2a61a70e26ae131cc9fdc9fdb93da
[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 Editor", 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                         if(wmn->action == NA_RENAME)
427                                 ED_region_tag_redraw(ar);
428                         break;
429                 default:
430                         if(wmn->data==ND_KEYS)
431                                 ED_region_tag_redraw(ar);
432         }
433 }
434
435 static void nla_channel_area_listener(ARegion *ar, wmNotifier *wmn)
436 {
437         /* context changes */
438         switch(wmn->category) {
439                 case NC_ANIMATION:
440                         ED_region_tag_redraw(ar);
441                         break;
442                 case NC_SCENE:
443                         switch(wmn->data) {
444                                 case ND_OB_ACTIVE:
445                                         ED_region_tag_redraw(ar);
446                                         break;
447                         }
448                         break;
449                 case NC_OBJECT:
450                         switch(wmn->data) {
451                                 case ND_BONE_ACTIVE:
452                                 case ND_BONE_SELECT:
453                                 case ND_KEYS:
454                                         ED_region_tag_redraw(ar);
455                                         break;
456                         }
457                         break;
458                 case NC_ID:
459                         if(wmn->action == NA_RENAME)
460                                 ED_region_tag_redraw(ar);
461                         break;
462
463                 default:
464                         if(wmn->data==ND_KEYS)
465                                 ED_region_tag_redraw(ar);
466         }
467 }
468
469 /* editor level listener */
470 static void nla_listener(ScrArea *sa, wmNotifier *wmn)
471 {
472         /* context changes */
473         switch (wmn->category) {
474                 case NC_ANIMATION:
475                         // TODO: filter specific types of changes?
476                         ED_area_tag_refresh(sa);
477                         break;
478                 case NC_SCENE:
479                         /*switch (wmn->data) {
480                                 case ND_OB_ACTIVE:
481                                 case ND_OB_SELECT:
482                                         ED_area_tag_refresh(sa);
483                                         break;
484                         }*/
485                         ED_area_tag_refresh(sa);
486                         break;
487                 case NC_OBJECT:
488                         switch (wmn->data) {
489                                 case ND_TRANSFORM:
490                                         /* do nothing */
491                                         break;
492                                 default:
493                                         ED_area_tag_refresh(sa);
494                                         break;
495                         }
496                         break;
497                 case NC_SPACE:
498                         if(wmn->data == ND_SPACE_NLA)
499                                 ED_area_tag_redraw(sa);
500                         break;
501         }
502 }
503
504 /* only called once, from space/spacetypes.c */
505 void ED_spacetype_nla(void)
506 {
507         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype nla");
508         ARegionType *art;
509         
510         st->spaceid= SPACE_NLA;
511         strncpy(st->name, "NLA", BKE_ST_MAXNAME);
512         
513         st->new= nla_new;
514         st->free= nla_free;
515         st->init= nla_init;
516         st->duplicate= nla_duplicate;
517         st->operatortypes= nla_operatortypes;
518         st->listener= nla_listener;
519         st->keymap= nla_keymap;
520         
521         /* regions: main window */
522         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
523         art->regionid = RGN_TYPE_WINDOW;
524         art->init= nla_main_area_init;
525         art->draw= nla_main_area_draw;
526         art->listener= nla_main_area_listener;
527         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
528
529         BLI_addhead(&st->regiontypes, art);
530         
531         /* regions: header */
532         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
533         art->regionid = RGN_TYPE_HEADER;
534         art->minsizey= HEADERY;
535         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
536         
537         art->init= nla_header_area_init;
538         art->draw= nla_header_area_draw;
539         
540         BLI_addhead(&st->regiontypes, art);
541         
542         /* regions: channels */
543         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
544         art->regionid = RGN_TYPE_CHANNELS;
545         art->minsizex= 200;
546         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
547         
548         art->init= nla_channel_area_init;
549         art->draw= nla_channel_area_draw;
550         art->listener= nla_channel_area_listener;
551         
552         BLI_addhead(&st->regiontypes, art);
553         
554         /* regions: UI buttons */
555         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
556         art->regionid = RGN_TYPE_UI;
557         art->minsizex= 200;
558         art->keymapflag= ED_KEYMAP_UI;
559         art->listener= nla_region_listener;
560         art->init= nla_buttons_area_init;
561         art->draw= nla_buttons_area_draw;
562         
563         BLI_addhead(&st->regiontypes, art);
564
565         nla_buttons_register(art);
566         
567         
568         BKE_spacetype_register(st);
569 }
570