Fix #34941: Space.draw_handler_add now supports PRE_VIEW and POST_VIEW callbacks
[blender-staging.git] / source / blender / editors / space_action / space_action.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_action/space_action.c
28  *  \ingroup spaction
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_action_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_screen.h"
47
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 "ED_space_api.h"
59 #include "ED_screen.h"
60 #include "ED_anim_api.h"
61 #include "ED_markers.h"
62
63 #include "action_intern.h"  /* own include */
64
65 /* ******************** default callbacks for action space ***************** */
66
67 static SpaceLink *action_new(const bContext *C)
68 {
69         ScrArea *sa = CTX_wm_area(C);
70         SpaceAction *saction;
71         ARegion *ar;
72         
73         saction = MEM_callocN(sizeof(SpaceAction), "initaction");
74         saction->spacetype = SPACE_ACTION;
75         
76         saction->autosnap = SACTSNAP_FRAME;
77         saction->mode = SACTCONT_DOPESHEET;
78         
79         saction->ads.filterflag |= ADS_FILTER_SUMMARY;
80         
81         /* header */
82         ar = MEM_callocN(sizeof(ARegion), "header for action");
83         
84         BLI_addtail(&saction->regionbase, ar);
85         ar->regiontype = RGN_TYPE_HEADER;
86         ar->alignment = RGN_ALIGN_BOTTOM;
87         
88         /* channel list region */
89         ar = MEM_callocN(sizeof(ARegion), "channel area for action");
90         BLI_addtail(&saction->regionbase, ar);
91         ar->regiontype = RGN_TYPE_CHANNELS;
92         ar->alignment = RGN_ALIGN_LEFT;
93         
94         /* only need to set scroll settings, as this will use 'listview' v2d configuration */
95         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
96         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
97         
98         /* main area */
99         ar = MEM_callocN(sizeof(ARegion), "main area for action");
100         
101         BLI_addtail(&saction->regionbase, ar);
102         ar->regiontype = RGN_TYPE_WINDOW;
103         
104         ar->v2d.tot.xmin = -10.0f;
105         ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
106         ar->v2d.tot.xmax = (float)(sa->winx);
107         ar->v2d.tot.ymax = 0.0f;
108         
109         ar->v2d.cur = ar->v2d.tot;
110         
111         ar->v2d.min[0] = 0.0f;
112         ar->v2d.min[1] = 0.0f;
113         
114         ar->v2d.max[0] = MAXFRAMEF;
115         ar->v2d.max[1] = FLT_MAX;
116
117         ar->v2d.minzoom = 0.01f;
118         ar->v2d.maxzoom = 50;
119         ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
120         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
121         ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
122         ar->v2d.keepofs = V2D_KEEPOFS_Y;
123         ar->v2d.align = V2D_ALIGN_NO_POS_Y;
124         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
125         
126         return (SpaceLink *)saction;
127 }
128
129 /* not spacelink itself */
130 static void action_free(SpaceLink *UNUSED(sl))
131 {       
132 //      SpaceAction *saction = (SpaceAction *) sl;
133 }
134
135
136 /* spacetype; init callback */
137 static void action_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
138 {
139         SpaceAction *saction = sa->spacedata.first;
140         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
141 }
142
143 static SpaceLink *action_duplicate(SpaceLink *sl)
144 {
145         SpaceAction *sactionn = MEM_dupallocN(sl);
146         
147         /* clear or remove stuff from old */
148         
149         return (SpaceLink *)sactionn;
150 }
151
152
153
154 /* add handlers, stuff you only do once or on area/region changes */
155 static void action_main_area_init(wmWindowManager *wm, ARegion *ar)
156 {
157         wmKeyMap *keymap;
158         
159         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
160         
161         /* own keymap */
162         keymap = WM_keymap_find(wm->defaultconf, "Dopesheet", SPACE_ACTION, 0);
163         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
164 }
165
166 static void action_main_area_draw(const bContext *C, ARegion *ar)
167 {
168         /* draw entirely, view changes should be handled here */
169         SpaceAction *saction = CTX_wm_space_action(C);
170         bAnimContext ac;
171         View2D *v2d = &ar->v2d;
172         View2DGrid *grid;
173         View2DScrollers *scrollers;
174         short unit = 0, flag = 0;
175         
176         /* clear and setup matrix */
177         UI_ThemeClearColor(TH_BACK);
178         glClear(GL_COLOR_BUFFER_BIT);
179         
180         UI_view2d_view_ortho(v2d);
181         
182         /* time grid */
183         unit = (saction->flag & SACTION_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
184         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
185         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
186         UI_view2d_grid_free(grid);
187         
188         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
189
190         /* data */
191         if (ANIM_animdata_get_context(C, &ac)) {
192                 draw_channel_strips(&ac, saction, ar);
193         }
194         
195         /* current frame */
196         if (saction->flag & SACTION_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
197         if ((saction->flag & SACTION_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
198         ANIM_draw_cfra(C, v2d, flag);
199         
200         /* markers */
201         UI_view2d_view_orthoSpecial(ar, v2d, 1);
202         
203         flag = (ac.markers && (ac.markers != &ac.scene->markers)) ? DRAW_MARKERS_LOCAL : 0;
204         draw_markers_time(C, flag);
205         
206         /* preview range */
207         UI_view2d_view_ortho(v2d);
208         ANIM_draw_previewrange(C, v2d);
209
210         /* callback */
211         UI_view2d_view_ortho(v2d);
212         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
213         
214         /* reset view matrix */
215         UI_view2d_view_restore(C);
216         
217         /* scrollers */
218         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
219         UI_view2d_scrollers_draw(C, v2d, scrollers);
220         UI_view2d_scrollers_free(scrollers);
221 }
222
223 /* add handlers, stuff you only do once or on area/region changes */
224 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
225 {
226         wmKeyMap *keymap;
227         
228         /* ensure the 2d view sync works - main region has bottom scroller */
229         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
230         
231         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
232         
233         /* own keymap */
234         keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
235         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
236 }
237
238 static void action_channel_area_draw(const bContext *C, ARegion *ar)
239 {
240         /* draw entirely, view changes should be handled here */
241         bAnimContext ac;
242         View2D *v2d = &ar->v2d;
243         
244         /* clear and setup matrix */
245         UI_ThemeClearColor(TH_BACK);
246         glClear(GL_COLOR_BUFFER_BIT);
247         
248         UI_view2d_view_ortho(v2d);
249         
250         /* data */
251         if (ANIM_animdata_get_context(C, &ac)) {
252                 draw_channel_names((bContext *)C, &ac, ar);
253         }
254         
255         /* reset view matrix */
256         UI_view2d_view_restore(C);
257         
258         /* no scrollers here */
259 }
260
261
262 /* add handlers, stuff you only do once or on area/region changes */
263 static void action_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
264 {
265         ED_region_header_init(ar);
266 }
267
268 static void action_header_area_draw(const bContext *C, ARegion *ar)
269 {
270         ED_region_header(C, ar);
271 }
272
273 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
274 {
275         /* context changes */
276         switch (wmn->category) {
277                 case NC_ANIMATION:
278                         ED_region_tag_redraw(ar);
279                         break;
280                 case NC_SCENE:
281                         switch (wmn->data) {
282                                 case ND_OB_ACTIVE:
283                                 case ND_FRAME:
284                                         ED_region_tag_redraw(ar);
285                                         break;
286                         }
287                         break;
288                 case NC_OBJECT:
289                         switch (wmn->data) {
290                                 case ND_BONE_ACTIVE:
291                                 case ND_BONE_SELECT:
292                                 case ND_KEYS:
293                                         ED_region_tag_redraw(ar);
294                                         break;
295                                 case ND_MODIFIER:
296                                         if (wmn->action == NA_RENAME)
297                                                 ED_region_tag_redraw(ar);
298                                         break;
299                         }
300                         break;
301                 case NC_ID:
302                         if (wmn->action == NA_RENAME)
303                                 ED_region_tag_redraw(ar);
304                         break;
305                 default:
306                         if (wmn->data == ND_KEYS)
307                                 ED_region_tag_redraw(ar);
308         }
309 }
310
311 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
312 {
313         /* context changes */
314         switch (wmn->category) {
315                 case NC_ANIMATION:
316                         ED_region_tag_redraw(ar);
317                         break;
318                 case NC_SCENE:
319                         switch (wmn->data) {
320                                 case ND_RENDER_OPTIONS:
321                                 case ND_OB_ACTIVE:
322                                 case ND_FRAME:
323                                 case ND_MARKERS:
324                                         ED_region_tag_redraw(ar);
325                                         break;
326                         }
327                         break;
328                 case NC_OBJECT:
329                         switch (wmn->data) {
330                                 case ND_TRANSFORM:
331                                         /* moving object shouldn't need to redraw action */
332                                         break;
333                                 case ND_BONE_ACTIVE:
334                                 case ND_BONE_SELECT:
335                                 case ND_KEYS:
336                                         ED_region_tag_redraw(ar);
337                                         break;
338                         }
339                         break;
340                 case NC_NODE:
341                         switch (wmn->action) {
342                                 case NA_EDITED:
343                                         ED_region_tag_redraw(ar);
344                                         break;
345                         }
346                         break;
347                 case NC_ID:
348                         if (wmn->action == NA_RENAME)
349                                 ED_region_tag_redraw(ar);
350                         break;
351                                 
352                 default:
353                         if (wmn->data == ND_KEYS)
354                                 ED_region_tag_redraw(ar);
355         }
356 }
357
358 /* editor level listener */
359 static void action_listener(ScrArea *sa, wmNotifier *wmn)
360 {
361         SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
362         
363         /* context changes */
364         switch (wmn->category) {
365                 case NC_GPENCIL:
366                         if (wmn->action == NA_EDITED) {
367                                 /* only handle this event in GPencil mode for performance considerations */
368                                 if (saction->mode == SACTCONT_GPENCIL)
369                                         ED_area_tag_redraw(sa);
370                         }
371                         break;
372                 case NC_ANIMATION:
373                         /* for NLA tweakmode enter/exit, need complete refresh */
374                         if (wmn->data == ND_NLA_ACTCHANGE) {
375                                 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
376                                 ED_area_tag_refresh(sa);
377                         }
378                         /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
379                         else if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED))
380                                 ED_area_tag_redraw(sa);
381                         else
382                                 ED_area_tag_refresh(sa);
383                         break;
384                 case NC_SCENE:
385                         switch (wmn->data) {
386                                 case ND_OB_ACTIVE:  /* selection changed, so force refresh to flush (needs flag set to do syncing) */
387                                 case ND_OB_SELECT:
388                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
389                                         ED_area_tag_refresh(sa);
390                                         break;
391                                         
392                                 default: /* just redrawing the view will do */
393                                         ED_area_tag_redraw(sa);
394                                         break;
395                         }
396                         break;
397                 case NC_OBJECT:
398                         switch (wmn->data) {
399                                 case ND_BONE_SELECT:    /* selection changed, so force refresh to flush (needs flag set to do syncing) */
400                                 case ND_BONE_ACTIVE:
401                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
402                                         ED_area_tag_refresh(sa);
403                                         break;
404                                 case ND_TRANSFORM:
405                                         /* moving object shouldn't need to redraw action */
406                                         break;
407                                 default: /* just redrawing the view will do */
408                                         ED_area_tag_redraw(sa);
409                                         break;
410                         }
411                         break;
412                 case NC_MASK:
413                         if (saction->mode == SACTCONT_MASK) {
414                                 switch (wmn->data) {
415                                         case ND_DATA:
416                                                 ED_area_tag_refresh(sa);
417                                                 ED_area_tag_redraw(sa);
418                                                 break;
419                                         default: /* just redrawing the view will do */
420                                                 ED_area_tag_redraw(sa);
421                                                 break;
422                                 }
423                         }
424                         break;
425                 case NC_NODE:
426                         if (wmn->action == NA_SELECTED) {
427                                 /* selection changed, so force refresh to flush (needs flag set to do syncing) */
428                                 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
429                                 ED_area_tag_refresh(sa);
430                         }
431                         break;
432                 case NC_SPACE:
433                         switch (wmn->data) {
434                                 case ND_SPACE_DOPESHEET:
435                                         ED_area_tag_redraw(sa);
436                                         break;
437                                 case ND_SPACE_CHANGED:
438                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
439                                         ED_area_tag_refresh(sa);
440                                         break;
441                         }
442                         break;
443                 case NC_WINDOW:
444                         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
445                                 /* force redraw/refresh after undo/redo - [#28962] */
446                                 ED_area_tag_refresh(sa);
447                         }
448                         break;
449         }
450 }
451
452 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
453 {
454         /* context changes */
455         switch (wmn->category) {
456                 case NC_SCENE:
457                         switch (wmn->data) {
458                                 case ND_OB_ACTIVE:
459                                         ED_region_tag_redraw(ar);
460                                         break;
461                         }
462                         break;
463                 case NC_ID:
464                         if (wmn->action == NA_RENAME)
465                                 ED_region_tag_redraw(ar);
466                         break;
467         }
468 }
469
470 static void action_refresh(const bContext *C, ScrArea *sa)
471 {
472         SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
473         
474         /* update the state of the animchannels in response to changes from the data they represent 
475          * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
476          */
477         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
478                 ARegion *ar;
479                 
480                 /* Perform syncing of channel state incl. selection
481                  * Active action setting also occurs here (as part of anim channel filtering in anim_filter.c)
482                  */
483                 ANIM_sync_animchannels_to_data(C);
484                 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
485                 
486                 /* Tag everything for redraw
487                  * - Regions (such as header) need to be manually tagged for redraw too
488                  *   or else they don't update [#28962]
489                  */
490                 ED_area_tag_redraw(sa);
491                 for (ar = sa->regionbase.first; ar; ar = ar->next)
492                         ED_region_tag_redraw(ar);
493         }
494         
495         /* region updates? */
496         // XXX re-sizing y-extents of tot should go here?
497 }
498
499 /* only called once, from space/spacetypes.c */
500 void ED_spacetype_action(void)
501 {
502         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype action");
503         ARegionType *art;
504         
505         st->spaceid = SPACE_ACTION;
506         strncpy(st->name, "Action", BKE_ST_MAXNAME);
507         
508         st->new = action_new;
509         st->free = action_free;
510         st->init = action_init;
511         st->duplicate = action_duplicate;
512         st->operatortypes = action_operatortypes;
513         st->keymap = action_keymap;
514         st->listener = action_listener;
515         st->refresh = action_refresh;
516         
517         /* regions: main window */
518         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
519         art->regionid = RGN_TYPE_WINDOW;
520         art->init = action_main_area_init;
521         art->draw = action_main_area_draw;
522         art->listener = action_main_area_listener;
523         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
524
525         BLI_addhead(&st->regiontypes, art);
526         
527         /* regions: header */
528         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
529         art->regionid = RGN_TYPE_HEADER;
530         art->prefsizey = HEADERY;
531         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
532         
533         art->init = action_header_area_init;
534         art->draw = action_header_area_draw;
535         art->listener = action_header_area_listener;
536         
537         BLI_addhead(&st->regiontypes, art);
538         
539         /* regions: channels */
540         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
541         art->regionid = RGN_TYPE_CHANNELS;
542         art->prefsizex = 200;
543         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
544         
545         art->init = action_channel_area_init;
546         art->draw = action_channel_area_draw;
547         art->listener = action_channel_area_listener;
548         
549         BLI_addhead(&st->regiontypes, art);
550         
551         
552         BKE_spacetype_register(st);
553 }
554