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