Grease Pencil notifier/listener cleanup
[blender.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         /* data */
189         if (ANIM_animdata_get_context(C, &ac)) {
190                 draw_channel_strips(&ac, saction, ar);
191         }
192         
193         /* current frame */
194         if (saction->flag & SACTION_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
195         if ((saction->flag & SACTION_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
196         ANIM_draw_cfra(C, v2d, flag);
197         
198         /* markers */
199         UI_view2d_view_orthoSpecial(ar, v2d, 1);
200         
201         flag = (ac.markers && (ac.markers != &ac.scene->markers)) ? DRAW_MARKERS_LOCAL : 0;
202         draw_markers_time(C, flag);
203         
204         /* preview range */
205         UI_view2d_view_ortho(v2d);
206         ANIM_draw_previewrange(C, v2d);
207         
208         /* reset view matrix */
209         UI_view2d_view_restore(C);
210         
211         /* scrollers */
212         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
213         UI_view2d_scrollers_draw(C, v2d, scrollers);
214         UI_view2d_scrollers_free(scrollers);
215 }
216
217 /* add handlers, stuff you only do once or on area/region changes */
218 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
219 {
220         wmKeyMap *keymap;
221         
222         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
223         
224         /* own keymap */
225         keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
226         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
227 }
228
229 static void action_channel_area_draw(const bContext *C, ARegion *ar)
230 {
231         /* draw entirely, view changes should be handled here */
232         bAnimContext ac;
233         View2D *v2d = &ar->v2d;
234         View2DScrollers *scrollers;
235         
236         /* clear and setup matrix */
237         UI_ThemeClearColor(TH_BACK);
238         glClear(GL_COLOR_BUFFER_BIT);
239         
240         UI_view2d_view_ortho(v2d);
241         
242         /* data */
243         if (ANIM_animdata_get_context(C, &ac)) {
244                 draw_channel_names((bContext *)C, &ac, ar);
245         }
246         
247         /* reset view matrix */
248         UI_view2d_view_restore(C);
249         
250         /* scrollers */
251         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
252         UI_view2d_scrollers_draw(C, v2d, scrollers);
253         UI_view2d_scrollers_free(scrollers);
254 }
255
256
257 /* add handlers, stuff you only do once or on area/region changes */
258 static void action_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
259 {
260         ED_region_header_init(ar);
261 }
262
263 static void action_header_area_draw(const bContext *C, ARegion *ar)
264 {
265         ED_region_header(C, ar);
266 }
267
268 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
269 {
270         /* context changes */
271         switch (wmn->category) {
272                 case NC_ANIMATION:
273                         ED_region_tag_redraw(ar);
274                         break;
275                 case NC_SCENE:
276                         switch (wmn->data) {
277                                 case ND_OB_ACTIVE:
278                                 case ND_FRAME:
279                                         ED_region_tag_redraw(ar);
280                                         break;
281                         }
282                         break;
283                 case NC_OBJECT:
284                         switch (wmn->data) {
285                                 case ND_BONE_ACTIVE:
286                                 case ND_BONE_SELECT:
287                                 case ND_KEYS:
288                                         ED_region_tag_redraw(ar);
289                                         break;
290                                 case ND_MODIFIER:
291                                         if (wmn->action == NA_RENAME)
292                                                 ED_region_tag_redraw(ar);
293                                         break;
294                         }
295                         break;
296                 case NC_ID:
297                         if (wmn->action == NA_RENAME)
298                                 ED_region_tag_redraw(ar);
299                         break;
300                 default:
301                         if (wmn->data == ND_KEYS)
302                                 ED_region_tag_redraw(ar);
303         }
304 }
305
306 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
307 {
308         /* context changes */
309         switch (wmn->category) {
310                 case NC_ANIMATION:
311                         ED_region_tag_redraw(ar);
312                         break;
313                 case NC_SCENE:
314                         switch (wmn->data) {
315                                 case ND_RENDER_OPTIONS:
316                                 case ND_OB_ACTIVE:
317                                 case ND_FRAME:
318                                 case ND_MARKERS:
319                                         ED_region_tag_redraw(ar);
320                                         break;
321                         }
322                         break;
323                 case NC_OBJECT:
324                         switch (wmn->data) {
325                                 case ND_TRANSFORM:
326                                         /* moving object shouldn't need to redraw action */
327                                         break;
328                                 case ND_BONE_ACTIVE:
329                                 case ND_BONE_SELECT:
330                                 case ND_KEYS:
331                                         ED_region_tag_redraw(ar);
332                                         break;
333                         }
334                         break;
335                 case NC_NODE:
336                         switch (wmn->action) {
337                                 case NA_EDITED:
338                                         ED_region_tag_redraw(ar);
339                                         break;
340                         }
341                         break;
342                 case NC_ID:
343                         if (wmn->action == NA_RENAME)
344                                 ED_region_tag_redraw(ar);
345                         break;
346                                 
347                 default:
348                         if (wmn->data == ND_KEYS)
349                                 ED_region_tag_redraw(ar);
350         }
351 }
352
353 /* editor level listener */
354 static void action_listener(ScrArea *sa, wmNotifier *wmn)
355 {
356         SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
357         
358         /* context changes */
359         switch (wmn->category) {
360                 case NC_GPENCIL:
361                         if (wmn->action == NA_EDITED) {
362                                 /* only handle this event in GPencil mode for performance considerations */
363                                 if (saction->mode == SACTCONT_GPENCIL)
364                                         ED_area_tag_redraw(sa);
365                         }
366                         break;
367                 case NC_ANIMATION:
368                         /* for NLA tweakmode enter/exit, need complete refresh */
369                         if (wmn->data == ND_NLA_ACTCHANGE) {
370                                 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
371                                 ED_area_tag_refresh(sa);
372                         }
373                         /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
374                         else if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED))
375                                 ED_area_tag_redraw(sa);
376                         else
377                                 ED_area_tag_refresh(sa);
378                         break;
379                 case NC_SCENE:
380                         switch (wmn->data) {    
381                                 case ND_OB_ACTIVE:  /* selection changed, so force refresh to flush (needs flag set to do syncing) */
382                                 case ND_OB_SELECT:
383                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
384                                         ED_area_tag_refresh(sa);
385                                         break;
386                                         
387                                 default: /* just redrawing the view will do */
388                                         ED_area_tag_redraw(sa);
389                                         break;
390                         }
391                         break;
392                 case NC_OBJECT:
393                         switch (wmn->data) {
394                                 case ND_BONE_SELECT:    /* selection changed, so force refresh to flush (needs flag set to do syncing) */
395                                 case ND_BONE_ACTIVE:
396                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
397                                         ED_area_tag_refresh(sa);
398                                         break;
399                                 case ND_TRANSFORM:
400                                         /* moving object shouldn't need to redraw action */
401                                         break;
402                                 default: /* just redrawing the view will do */
403                                         ED_area_tag_redraw(sa);
404                                         break;
405                         }
406                         break;
407                 case NC_MASK:
408                         if (saction->mode == SACTCONT_MASK) {
409                                 switch (wmn->data) {
410                                         case ND_DATA:
411                                                 ED_area_tag_refresh(sa);
412                                                 ED_area_tag_redraw(sa);
413                                                 break;
414                                         default: /* just redrawing the view will do */
415                                                 ED_area_tag_redraw(sa);
416                                                 break;
417                                 }
418                         }
419                         break;
420                 case NC_NODE:
421                         if (wmn->action == NA_SELECTED) {
422                                 /* selection changed, so force refresh to flush (needs flag set to do syncing) */
423                                 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
424                                 ED_area_tag_refresh(sa);
425                         }
426                         break;
427                 case NC_SPACE:
428                         switch (wmn->data) {
429                                 case ND_SPACE_DOPESHEET:
430                                         ED_area_tag_redraw(sa);
431                                         break;
432                                 case ND_SPACE_CHANGED:
433                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
434                                         ED_area_tag_refresh(sa);
435                                         break;
436                         }                       
437                         break;
438                 case NC_WINDOW:
439                         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
440                                 /* force redraw/refresh after undo/redo - [#28962] */
441                                 ED_area_tag_refresh(sa);
442                         }
443                         break;
444         }
445 }
446
447 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
448 {
449         /* context changes */
450         switch (wmn->category) {
451                 case NC_SCENE:
452                         switch (wmn->data) {
453                                 case ND_OB_ACTIVE:
454                                         ED_region_tag_redraw(ar);
455                                         break;
456                         }
457                         break;
458                 case NC_ID:
459                         if (wmn->action == NA_RENAME)
460                                 ED_region_tag_redraw(ar);
461                         break;
462         }
463 }
464
465 static void action_refresh(const bContext *C, ScrArea *sa)
466 {
467         SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
468         
469         /* update the state of the animchannels in response to changes from the data they represent 
470          * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
471          */
472         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
473                 ARegion *ar;
474                 
475                 /* Perform syncing of channel state incl. selection
476                  * Active action setting also occurs here (as part of anim channel filtering in anim_filter.c)
477                  */
478                 ANIM_sync_animchannels_to_data(C);
479                 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
480                 
481                 /* Tag everything for redraw
482                  * - Regions (such as header) need to be manually tagged for redraw too
483                  *   or else they don't update [#28962]
484                  */
485                 ED_area_tag_redraw(sa);
486                 for (ar = sa->regionbase.first; ar; ar = ar->next)
487                         ED_region_tag_redraw(ar);
488         }
489         
490         /* region updates? */
491         // XXX re-sizing y-extents of tot should go here?
492 }
493
494 /* only called once, from space/spacetypes.c */
495 void ED_spacetype_action(void)
496 {
497         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype action");
498         ARegionType *art;
499         
500         st->spaceid = SPACE_ACTION;
501         strncpy(st->name, "Action", BKE_ST_MAXNAME);
502         
503         st->new = action_new;
504         st->free = action_free;
505         st->init = action_init;
506         st->duplicate = action_duplicate;
507         st->operatortypes = action_operatortypes;
508         st->keymap = action_keymap;
509         st->listener = action_listener;
510         st->refresh = action_refresh;
511         
512         /* regions: main window */
513         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
514         art->regionid = RGN_TYPE_WINDOW;
515         art->init = action_main_area_init;
516         art->draw = action_main_area_draw;
517         art->listener = action_main_area_listener;
518         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
519
520         BLI_addhead(&st->regiontypes, art);
521         
522         /* regions: header */
523         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
524         art->regionid = RGN_TYPE_HEADER;
525         art->prefsizey = HEADERY;
526         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
527         
528         art->init = action_header_area_init;
529         art->draw = action_header_area_draw;
530         art->listener = action_header_area_listener;
531         
532         BLI_addhead(&st->regiontypes, art);
533         
534         /* regions: channels */
535         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
536         art->regionid = RGN_TYPE_CHANNELS;
537         art->prefsizex = 200;
538         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
539         
540         art->init = action_channel_area_init;
541         art->draw = action_channel_area_draw;
542         art->listener = action_channel_area_listener;
543         
544         BLI_addhead(&st->regiontypes, art);
545         
546         
547         BKE_spacetype_register(st);
548 }
549