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