c8660179945fd6dada876ca87af3382fb64df5e0
[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
137 /* spacetype; init callback */
138 static void action_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
139 {
140         SpaceAction *saction = sa->spacedata.first;
141         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
142 }
143
144 static SpaceLink *action_duplicate(SpaceLink *sl)
145 {
146         SpaceAction *sactionn = MEM_dupallocN(sl);
147         
148         /* clear or remove stuff from old */
149         
150         return (SpaceLink *)sactionn;
151 }
152
153
154
155 /* add handlers, stuff you only do once or on area/region changes */
156 static void action_main_area_init(wmWindowManager *wm, ARegion *ar)
157 {
158         wmKeyMap *keymap;
159         
160         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
161         
162         /* own keymap */
163         keymap = WM_keymap_find(wm->defaultconf, "Dopesheet", SPACE_ACTION, 0);
164         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
165 }
166
167 static void action_main_area_draw(const bContext *C, ARegion *ar)
168 {
169         /* draw entirely, view changes should be handled here */
170         SpaceAction *saction = CTX_wm_space_action(C);
171         bAnimContext ac;
172         View2D *v2d = &ar->v2d;
173         View2DGrid *grid;
174         View2DScrollers *scrollers;
175         short unit = 0, flag = 0;
176         
177         /* clear and setup matrix */
178         UI_ThemeClearColor(TH_BACK);
179         glClear(GL_COLOR_BUFFER_BIT);
180         
181         UI_view2d_view_ortho(v2d);
182         
183         /* time grid */
184         unit = (saction->flag & SACTION_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
185         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
186         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
187         UI_view2d_grid_free(grid);
188         
189         /* data */
190         if (ANIM_animdata_get_context(C, &ac)) {
191                 draw_channel_strips(&ac, saction, ar);
192         }
193         
194         /* current frame */
195         if (saction->flag & SACTION_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
196         if ((saction->flag & SACTION_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
197         ANIM_draw_cfra(C, v2d, flag);
198         
199         /* markers */
200         UI_view2d_view_orthoSpecial(ar, v2d, 1);
201         
202         flag = (ac.markers && (ac.markers != &ac.scene->markers)) ? DRAW_MARKERS_LOCAL : 0;
203         draw_markers_time(C, flag);
204         
205         /* preview range */
206         UI_view2d_view_ortho(v2d);
207         ANIM_draw_previewrange(C, v2d);
208         
209         /* reset view matrix */
210         UI_view2d_view_restore(C);
211         
212         /* scrollers */
213         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
214         UI_view2d_scrollers_draw(C, v2d, scrollers);
215         UI_view2d_scrollers_free(scrollers);
216 }
217
218 /* add handlers, stuff you only do once or on area/region changes */
219 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
220 {
221         wmKeyMap *keymap;
222         
223         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
224         
225         /* own keymap */
226         keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
227         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
228 }
229
230 static void action_channel_area_draw(const bContext *C, ARegion *ar)
231 {
232         /* draw entirely, view changes should be handled here */
233         bAnimContext ac;
234         View2D *v2d = &ar->v2d;
235         View2DScrollers *scrollers;
236         
237         /* clear and setup matrix */
238         UI_ThemeClearColor(TH_BACK);
239         glClear(GL_COLOR_BUFFER_BIT);
240         
241         UI_view2d_view_ortho(v2d);
242         
243         /* data */
244         if (ANIM_animdata_get_context(C, &ac)) {
245                 draw_channel_names((bContext *)C, &ac, ar);
246         }
247         
248         /* reset view matrix */
249         UI_view2d_view_restore(C);
250         
251         /* scrollers */
252         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
253         UI_view2d_scrollers_draw(C, v2d, scrollers);
254         UI_view2d_scrollers_free(scrollers);
255 }
256
257
258 /* add handlers, stuff you only do once or on area/region changes */
259 static void action_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
260 {
261         ED_region_header_init(ar);
262 }
263
264 static void action_header_area_draw(const bContext *C, ARegion *ar)
265 {
266         ED_region_header(C, ar);
267 }
268
269 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
270 {
271         /* context changes */
272         switch (wmn->category) {
273                 case NC_ANIMATION:
274                         ED_region_tag_redraw(ar);
275                         break;
276                 case NC_SCENE:
277                         switch (wmn->data) {
278                                 case ND_OB_ACTIVE:
279                                 case ND_FRAME:
280                                         ED_region_tag_redraw(ar);
281                                         break;
282                         }
283                         break;
284                 case NC_OBJECT:
285                         switch (wmn->data) {
286                                 case ND_BONE_ACTIVE:
287                                 case ND_BONE_SELECT:
288                                 case ND_KEYS:
289                                         ED_region_tag_redraw(ar);
290                                         break;
291                                 case ND_MODIFIER:
292                                         if (wmn->action == NA_RENAME)
293                                                 ED_region_tag_redraw(ar);
294                                         break;
295                         }
296                         break;
297                 case NC_ID:
298                         if (wmn->action == NA_RENAME)
299                                 ED_region_tag_redraw(ar);
300                         break;
301                 default:
302                         if (wmn->data == ND_KEYS)
303                                 ED_region_tag_redraw(ar);
304         }
305 }
306
307 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
308 {
309         /* context changes */
310         switch (wmn->category) {
311                 case NC_ANIMATION:
312                         ED_region_tag_redraw(ar);
313                         break;
314                 case NC_SCENE:
315                         switch (wmn->data) {
316                                 case ND_RENDER_OPTIONS:
317                                 case ND_OB_ACTIVE:
318                                 case ND_FRAME:
319                                 case ND_MARKERS:
320                                         ED_region_tag_redraw(ar);
321                                         break;
322                         }
323                         break;
324                 case NC_OBJECT:
325                         switch (wmn->data) {
326                                 case ND_TRANSFORM:
327                                         /* moving object shouldn't need to redraw action */
328                                         break;
329                                 case ND_BONE_ACTIVE:
330                                 case ND_BONE_SELECT:
331                                 case ND_KEYS:
332                                         ED_region_tag_redraw(ar);
333                                         break;
334                         }
335                         break;
336                 case NC_NODE:
337                         switch (wmn->action) {
338                                 case NA_EDITED:
339                                         ED_region_tag_redraw(ar);
340                                         break;
341                         }
342                         break;
343                 case NC_ID:
344                         if (wmn->action == NA_RENAME)
345                                 ED_region_tag_redraw(ar);
346                         break;
347                                 
348                 default:
349                         if (wmn->data == ND_KEYS)
350                                 ED_region_tag_redraw(ar);
351         }
352 }
353
354 /* editor level listener */
355 static void action_listener(ScrArea *sa, wmNotifier *wmn)
356 {
357         SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
358         
359         /* context changes */
360         switch (wmn->category) {
361                 case NC_SCREEN:
362                         if (wmn->data == ND_GPENCIL) {
363                                 /* only handle this event in GPencil mode for performance considerations */
364                                 if (saction->mode == SACTCONT_GPENCIL)
365                                         ED_area_tag_redraw(sa);
366                         }
367                         break;
368                 case NC_ANIMATION:
369                         /* for NLA tweakmode enter/exit, need complete refresh */
370                         if (wmn->data == ND_NLA_ACTCHANGE) {
371                                 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
372                                 ED_area_tag_refresh(sa);
373                         }
374                         /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
375                         else if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED))
376                                 ED_area_tag_redraw(sa);
377                         else
378                                 ED_area_tag_refresh(sa);
379                         break;
380                 case NC_SCENE:
381                         switch (wmn->data) {    
382                                 case ND_OB_ACTIVE:  /* selection changed, so force refresh to flush (needs flag set to do syncing) */
383                                 case ND_OB_SELECT:
384                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
385                                         ED_area_tag_refresh(sa);
386                                         break;
387                                         
388                                 default: /* just redrawing the view will do */
389                                         ED_area_tag_redraw(sa);
390                                         break;
391                         }
392                         break;
393                 case NC_OBJECT:
394                         switch (wmn->data) {
395                                 case ND_BONE_SELECT:    /* selection changed, so force refresh to flush (needs flag set to do syncing) */
396                                 case ND_BONE_ACTIVE:
397                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
398                                         ED_area_tag_refresh(sa);
399                                         break;
400                                 case ND_TRANSFORM:
401                                         /* moving object shouldn't need to redraw action */
402                                         break;
403                                 default: /* just redrawing the view will do */
404                                         ED_area_tag_redraw(sa);
405                                         break;
406                         }
407                         break;
408                 case NC_MASK:
409                         if (saction->mode == SACTCONT_MASK) {
410                                 switch (wmn->data) {
411                                         case ND_DATA:
412                                                 ED_area_tag_refresh(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         }
439 }
440
441 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
442 {
443         /* context changes */
444         switch (wmn->category) {
445                 case NC_SCENE:
446                         switch (wmn->data) {
447                                 case ND_OB_ACTIVE:
448                                         ED_region_tag_redraw(ar);
449                                         break;
450                         }
451                         break;
452                 case NC_ID:
453                         if (wmn->action == NA_RENAME)
454                                 ED_region_tag_redraw(ar);
455                         break;
456         }
457 }
458
459 static void action_refresh(const bContext *C, ScrArea *sa)
460 {
461         SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
462         
463         /* update the state of the animchannels in response to changes from the data they represent 
464          * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
465          */
466         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
467                 ANIM_sync_animchannels_to_data(C);
468                 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
469                 ED_area_tag_redraw(sa);
470         }
471         
472         /* region updates? */
473         // XXX re-sizing y-extents of tot should go here?
474 }
475
476 /* only called once, from space/spacetypes.c */
477 void ED_spacetype_action(void)
478 {
479         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype action");
480         ARegionType *art;
481         
482         st->spaceid = SPACE_ACTION;
483         strncpy(st->name, "Action", BKE_ST_MAXNAME);
484         
485         st->new = action_new;
486         st->free = action_free;
487         st->init = action_init;
488         st->duplicate = action_duplicate;
489         st->operatortypes = action_operatortypes;
490         st->keymap = action_keymap;
491         st->listener = action_listener;
492         st->refresh = action_refresh;
493         
494         /* regions: main window */
495         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
496         art->regionid = RGN_TYPE_WINDOW;
497         art->init = action_main_area_init;
498         art->draw = action_main_area_draw;
499         art->listener = action_main_area_listener;
500         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
501
502         BLI_addhead(&st->regiontypes, art);
503         
504         /* regions: header */
505         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
506         art->regionid = RGN_TYPE_HEADER;
507         art->prefsizey = HEADERY;
508         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
509         
510         art->init = action_header_area_init;
511         art->draw = action_header_area_draw;
512         art->listener = action_header_area_listener;
513         
514         BLI_addhead(&st->regiontypes, art);
515         
516         /* regions: channels */
517         art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
518         art->regionid = RGN_TYPE_CHANNELS;
519         art->prefsizex = 200;
520         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
521         
522         art->init = action_channel_area_init;
523         art->draw = action_channel_area_draw;
524         art->listener = action_channel_area_listener;
525         
526         BLI_addhead(&st->regiontypes, art);
527         
528         
529         BKE_spacetype_register(st);
530 }
531