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