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