Fix #34941: Space.draw_handler_add now supports PRE_VIEW and POST_VIEW callbacks
[blender-staging.git] / source / blender / editors / space_nla / space_nla.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_nla/space_nla.c
28  *  \ingroup spnla
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_anim_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_rand.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_main.h"
48 #include "BKE_screen.h"
49
50 #include "ED_space_api.h"
51 #include "ED_anim_api.h"
52 #include "ED_markers.h"
53 #include "ED_screen.h"
54
55 #include "BIF_gl.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "UI_resources.h"
61 #include "UI_view2d.h"
62
63 #include "nla_intern.h" /* own include */
64
65 /* ******************** manage regions ********************* */
66
67 ARegion *nla_has_buttons_region(ScrArea *sa)
68 {
69         ARegion *ar, *arnew;
70
71         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
72         if (ar) return ar;
73
74         /* add subdiv level; after main */
75         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
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.keepofs = V2D_KEEPOFS_Y;
162         ar->v2d.align = V2D_ALIGN_NO_POS_Y;
163         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
164         
165         return (SpaceLink *)snla;
166 }
167
168 /* not spacelink itself */
169 static void nla_free(SpaceLink *sl)
170 {       
171         SpaceNla *snla = (SpaceNla *) sl;
172         
173         if (snla->ads) {
174                 BLI_freelistN(&snla->ads->chanbase);
175                 MEM_freeN(snla->ads);
176         }
177 }
178
179
180 /* spacetype; init callback */
181 static void nla_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
182 {
183         SpaceNla *snla = (SpaceNla *)sa->spacedata.first;
184         
185         /* init dopesheet data if non-existant (i.e. for old files) */
186         if (snla->ads == NULL) {
187                 snla->ads = MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
188                 snla->ads->source = (ID *)G.main->scene.first; // XXX this is bad, but we need this to be set correct
189         }
190
191         ED_area_tag_refresh(sa);
192 }
193
194 static SpaceLink *nla_duplicate(SpaceLink *sl)
195 {
196         SpaceNla *snlan = MEM_dupallocN(sl);
197         
198         /* clear or remove stuff from old */
199         snlan->ads = MEM_dupallocN(snlan->ads);
200         
201         return (SpaceLink *)snlan;
202 }
203
204 /* add handlers, stuff you only do once or on area/region changes */
205 static void nla_channel_area_init(wmWindowManager *wm, ARegion *ar)
206 {
207         wmKeyMap *keymap;
208         
209         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
210         
211         /* own keymap */
212         /* own channels map first to override some channel keymaps */
213         keymap = WM_keymap_find(wm->defaultconf, "NLA Channels", SPACE_NLA, 0);
214         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
215         /* now generic channels map for everything else that can apply */
216         keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
217         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
218         
219         keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
220         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
221 }
222
223 /* draw entirely, view changes should be handled here */
224 static void nla_channel_area_draw(const bContext *C, ARegion *ar)
225 {
226         bAnimContext ac;
227         View2D *v2d = &ar->v2d;
228         View2DScrollers *scrollers;
229         
230         /* clear and setup matrix */
231         UI_ThemeClearColor(TH_BACK);
232         glClear(GL_COLOR_BUFFER_BIT);
233         
234         UI_view2d_view_ortho(v2d);
235         
236         /* data */
237         if (ANIM_animdata_get_context(C, &ac)) {
238                 draw_nla_channel_list((bContext *)C, &ac, ar);
239         }
240         
241         /* reset view matrix */
242         UI_view2d_view_restore(C);
243         
244         /* scrollers */
245         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
246         UI_view2d_scrollers_draw(C, v2d, scrollers);
247         UI_view2d_scrollers_free(scrollers);
248 }
249
250
251 /* add handlers, stuff you only do once or on area/region changes */
252 static void nla_main_area_init(wmWindowManager *wm, ARegion *ar)
253 {
254         wmKeyMap *keymap;
255         
256         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
257         
258         /* own keymap */
259         keymap = WM_keymap_find(wm->defaultconf, "NLA Editor", SPACE_NLA, 0);
260         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
261         keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
262         WM_event_add_keymap_handler(&ar->handlers, keymap);
263 }
264
265 static void nla_main_area_draw(const bContext *C, ARegion *ar)
266 {
267         /* draw entirely, view changes should be handled here */
268         SpaceNla *snla = CTX_wm_space_nla(C);
269         bAnimContext ac;
270         View2D *v2d = &ar->v2d;
271         View2DGrid *grid;
272         View2DScrollers *scrollers;
273         short unit = 0, flag = 0;
274         
275         /* clear and setup matrix */
276         UI_ThemeClearColor(TH_BACK);
277         glClear(GL_COLOR_BUFFER_BIT);
278         
279         UI_view2d_view_ortho(v2d);
280         
281         /* time grid */
282         unit = (snla->flag & SNLA_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
283         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
284         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
285         UI_view2d_grid_free(grid);
286         
287         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
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         /* callback */
314         UI_view2d_view_ortho(v2d);
315         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
316
317         /* reset view matrix */
318         UI_view2d_view_restore(C);
319         
320         /* scrollers */
321         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
322         UI_view2d_scrollers_draw(C, v2d, scrollers);
323         UI_view2d_scrollers_free(scrollers);
324 }
325
326
327 /* add handlers, stuff you only do once or on area/region changes */
328 static void nla_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
329 {
330         ED_region_header_init(ar);
331 }
332
333 static void nla_header_area_draw(const bContext *C, ARegion *ar)
334 {
335         ED_region_header(C, ar);
336 }
337
338 /* add handlers, stuff you only do once or on area/region changes */
339 static void nla_buttons_area_init(wmWindowManager *wm, ARegion *ar)
340 {
341         wmKeyMap *keymap;
342         
343         ED_region_panels_init(wm, ar);
344         
345         keymap = WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
346         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
347 }
348
349 static void nla_buttons_area_draw(const bContext *C, ARegion *ar)
350 {
351         ED_region_panels(C, ar, 1, NULL, -1);
352 }
353
354 static void nla_region_listener(ARegion *ar, wmNotifier *wmn)
355 {
356         /* context changes */
357         switch (wmn->category) {
358                 case NC_ANIMATION:
359                         ED_region_tag_redraw(ar);
360                         break;
361                 case NC_SCENE:
362                         switch (wmn->data) {
363                                 case ND_OB_ACTIVE:
364                                 case ND_FRAME:
365                                 case ND_MARKERS:
366                                         ED_region_tag_redraw(ar);
367                                         break;
368                         }
369                         break;
370                 case NC_OBJECT:
371                         switch (wmn->data) {
372                                 case ND_BONE_ACTIVE:
373                                 case ND_BONE_SELECT:
374                                 case ND_KEYS:
375                                         ED_region_tag_redraw(ar);
376                                         break;
377                         }
378                         break;
379                 default:
380                         if (wmn->data == ND_KEYS)
381                                 ED_region_tag_redraw(ar);
382                         break;
383         }
384 }
385
386
387 static void nla_main_area_listener(ARegion *ar, wmNotifier *wmn)
388 {
389         /* context changes */
390         switch (wmn->category) {
391                 case NC_ANIMATION:
392                         ED_region_tag_redraw(ar);
393                         break;
394                 case NC_SCENE:
395                         switch (wmn->data) {
396                                 case ND_RENDER_OPTIONS:
397                                 case ND_OB_ACTIVE:
398                                 case ND_FRAME:
399                                 case ND_MARKERS:
400                                         ED_region_tag_redraw(ar);
401                                         break;
402                         }
403                         break;
404                 case NC_OBJECT:
405                         switch (wmn->data) {
406                                 case ND_BONE_ACTIVE:
407                                 case ND_BONE_SELECT:
408                                 case ND_KEYS:
409                                 case ND_TRANSFORM:
410                                         ED_region_tag_redraw(ar);
411                                         break;
412                         }
413                         break;
414                 case NC_NODE:
415                         switch (wmn->action) {
416                                 case NA_EDITED:
417                                         ED_region_tag_redraw(ar);
418                                         break;
419                         }
420                         break;
421                 case NC_ID:
422                         if (wmn->action == NA_RENAME)
423                                 ED_region_tag_redraw(ar);
424                         break;
425                 default:
426                         if (wmn->data == ND_KEYS)
427                                 ED_region_tag_redraw(ar);
428         }
429 }
430
431 static void nla_channel_area_listener(ARegion *ar, wmNotifier *wmn)
432 {
433         /* context changes */
434         switch (wmn->category) {
435                 case NC_ANIMATION:
436                         ED_region_tag_redraw(ar);
437                         break;
438                 case NC_SCENE:
439                         switch (wmn->data) {
440                                 case ND_OB_ACTIVE:
441                                         ED_region_tag_redraw(ar);
442                                         break;
443                         }
444                         break;
445                 case NC_OBJECT:
446                         switch (wmn->data) {
447                                 case ND_BONE_ACTIVE:
448                                 case ND_BONE_SELECT:
449                                 case ND_KEYS:
450                                         ED_region_tag_redraw(ar);
451                                         break;
452                         }
453                         break;
454                 case NC_ID:
455                         if (wmn->action == NA_RENAME)
456                                 ED_region_tag_redraw(ar);
457                         break;
458
459                 default:
460                         if (wmn->data == ND_KEYS)
461                                 ED_region_tag_redraw(ar);
462         }
463 }
464
465 /* editor level listener */
466 static void nla_listener(ScrArea *sa, wmNotifier *wmn)
467 {
468         /* context changes */
469         switch (wmn->category) {
470                 case NC_ANIMATION:
471                         // TODO: filter specific types of changes?
472                         ED_area_tag_refresh(sa);
473                         break;
474                 case NC_SCENE:
475 #if 0
476                         switch (wmn->data) {
477                                 case ND_OB_ACTIVE:
478                                 case ND_OB_SELECT:
479                                         ED_area_tag_refresh(sa);
480                                         break;
481                         }
482 #endif
483                         ED_area_tag_refresh(sa);
484                         break;
485                 case NC_OBJECT:
486                         switch (wmn->data) {
487                                 case ND_TRANSFORM:
488                                         /* do nothing */
489                                         break;
490                                 default:
491                                         ED_area_tag_refresh(sa);
492                                         break;
493                         }
494                         break;
495                 case NC_SPACE:
496                         if (wmn->data == ND_SPACE_NLA)
497                                 ED_area_tag_redraw(sa);
498                         break;
499         }
500 }
501
502 /* only called once, from space/spacetypes.c */
503 void ED_spacetype_nla(void)
504 {
505         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype nla");
506         ARegionType *art;
507         
508         st->spaceid = SPACE_NLA;
509         strncpy(st->name, "NLA", BKE_ST_MAXNAME);
510         
511         st->new = nla_new;
512         st->free = nla_free;
513         st->init = nla_init;
514         st->duplicate = nla_duplicate;
515         st->operatortypes = nla_operatortypes;
516         st->listener = nla_listener;
517         st->keymap = nla_keymap;
518         
519         /* regions: main window */
520         art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
521         art->regionid = RGN_TYPE_WINDOW;
522         art->init = nla_main_area_init;
523         art->draw = nla_main_area_draw;
524         art->listener = nla_main_area_listener;
525         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
526
527         BLI_addhead(&st->regiontypes, art);
528         
529         /* regions: header */
530         art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
531         art->regionid = RGN_TYPE_HEADER;
532         art->prefsizey = HEADERY;
533         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
534         
535         art->init = nla_header_area_init;
536         art->draw = nla_header_area_draw;
537         
538         BLI_addhead(&st->regiontypes, art);
539         
540         /* regions: channels */
541         art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
542         art->regionid = RGN_TYPE_CHANNELS;
543         art->prefsizex = 200;
544         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
545         
546         art->init = nla_channel_area_init;
547         art->draw = nla_channel_area_draw;
548         art->listener = nla_channel_area_listener;
549         
550         BLI_addhead(&st->regiontypes, art);
551         
552         /* regions: UI buttons */
553         art = MEM_callocN(sizeof(ARegionType), "spacetype nla region");
554         art->regionid = RGN_TYPE_UI;
555         art->prefsizex = 200;
556         art->keymapflag = ED_KEYMAP_UI;
557         art->listener = nla_region_listener;
558         art->init = nla_buttons_area_init;
559         art->draw = nla_buttons_area_draw;
560         
561         BLI_addhead(&st->regiontypes, art);
562
563         nla_buttons_register(art);
564         
565         
566         BKE_spacetype_register(st);
567 }