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