2cf21186233d6b5a3c2d3a97a34c2ca23cfa67d6
[blender-staging.git] / source / blender / editors / space_action / space_action.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_listBase.h"
33 #include "DNA_action_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_rand.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48 #include "BKE_utildefines.h"
49
50 #include "ED_space_api.h"
51 #include "ED_screen.h"
52
53 #include "BIF_gl.h"
54
55 #include "WM_api.h"
56 #include "WM_types.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #include "ED_anim_api.h"
63 #include "ED_keyframes_draw.h"
64 #include "ED_markers.h"
65
66 #include "action_intern.h"      // own include
67
68 /* ******************** default callbacks for action space ***************** */
69
70 static SpaceLink *action_new(const bContext *C)
71 {
72         ScrArea *sa= CTX_wm_area(C);
73         SpaceAction *saction;
74         ARegion *ar;
75         
76         saction= MEM_callocN(sizeof(SpaceAction), "initaction");
77         saction->spacetype= SPACE_ACTION;
78         
79         saction->autosnap = SACTSNAP_FRAME;
80         saction->mode= SACTCONT_DOPESHEET;
81         
82         /* header */
83         ar= MEM_callocN(sizeof(ARegion), "header for action");
84         
85         BLI_addtail(&saction->regionbase, ar);
86         ar->regiontype= RGN_TYPE_HEADER;
87         ar->alignment= RGN_ALIGN_BOTTOM;
88         
89         /* channel list region */
90         ar= MEM_callocN(sizeof(ARegion), "channel area for action");
91         BLI_addtail(&saction->regionbase, ar);
92         ar->regiontype= RGN_TYPE_CHANNELS;
93         ar->alignment= RGN_ALIGN_LEFT;
94         
95                 /* only need to set scroll settings, as this will use 'listview' v2d configuration */
96         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
97         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
98         
99         /* main area */
100         ar= MEM_callocN(sizeof(ARegion), "main area for action");
101         
102         BLI_addtail(&saction->regionbase, ar);
103         ar->regiontype= RGN_TYPE_WINDOW;
104         
105         ar->v2d.tot.xmin= -10.0f;
106         ar->v2d.tot.ymin= (float)(-sa->winy);
107         ar->v2d.tot.xmax= (float)(sa->winx);
108         ar->v2d.tot.ymax= 0.0f;
109         
110         ar->v2d.cur = ar->v2d.tot;
111         
112         ar->v2d.min[0]= 0.0f;
113         ar->v2d.min[1]= 0.0f;
114         
115         ar->v2d.max[0]= MAXFRAMEF;
116         ar->v2d.max[1]= 10000.0f;
117         
118         ar->v2d.minzoom= 0.01f;
119         ar->v2d.maxzoom= 50;
120         ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
121         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
122         ar->v2d.keepzoom= V2D_LOCKZOOM_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 *sl)
131 {       
132 //      SpaceAction *saction= (SpaceAction*) sl;
133         
134 }
135
136
137 /* spacetype; init callback */
138 static void action_init(struct wmWindowManager *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         float col[3];
176         short unit=0, flag=0;
177         
178         /* clear and setup matrix */
179         UI_GetThemeColor3fv(TH_BACK, col);
180         glClearColor(col[0], col[1], col[2], 0.0);
181         glClear(GL_COLOR_BUFFER_BIT);
182         
183         UI_view2d_view_ortho(C, v2d);
184         
185         /* time grid */
186         unit= (saction->flag & SACTION_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
187         grid= UI_view2d_grid_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
188         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
189         UI_view2d_grid_free(grid);
190         
191         /* data */
192         if (ANIM_animdata_get_context(C, &ac)) {
193                 draw_channel_strips(&ac, saction, ar);
194         }
195         
196         /* current frame */
197         if (saction->flag & SACTION_DRAWTIME)   flag |= DRAWCFRA_UNIT_SECONDS;
198         if ((saction->flag & SACTION_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
199         ANIM_draw_cfra(C, v2d, flag);
200         
201         /* markers */
202         UI_view2d_view_orthoSpecial(C, v2d, 1);
203         draw_markers_time(C, 0);
204         
205         /* preview range */
206         UI_view2d_view_ortho(C, 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         SpaceAction *saction= CTX_wm_space_action(C);
234         bAnimContext ac;
235         View2D *v2d= &ar->v2d;
236         View2DScrollers *scrollers;
237         float col[3];
238         
239         /* clear and setup matrix */
240         UI_GetThemeColor3fv(TH_BACK, col);
241         glClearColor(col[0], col[1], col[2], 0.0);
242         glClear(GL_COLOR_BUFFER_BIT);
243         
244         UI_view2d_view_ortho(C, v2d);
245         
246         /* data */
247         if (ANIM_animdata_get_context(C, &ac)) {
248                 draw_channel_names((bContext *)C, &ac, saction, ar);
249         }
250         
251         /* reset view matrix */
252         UI_view2d_view_restore(C);
253         
254         /* scrollers */
255         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
256         UI_view2d_scrollers_draw(C, v2d, scrollers);
257         UI_view2d_scrollers_free(scrollers);
258 }
259
260
261 /* add handlers, stuff you only do once or on area/region changes */
262 static void action_header_area_init(wmWindowManager *wm, ARegion *ar)
263 {
264         ED_region_header_init(ar);
265 }
266
267 static void action_header_area_draw(const bContext *C, ARegion *ar)
268 {
269         ED_region_header(C, ar);
270 }
271
272 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
273 {
274         /* context changes */
275         switch(wmn->category) {
276                 case NC_ANIMATION:
277                         ED_region_tag_redraw(ar);
278                         break;
279                 case NC_SCENE:
280                         switch(wmn->data) {
281                                 case ND_OB_ACTIVE:
282                                 case ND_FRAME:
283                                         ED_region_tag_redraw(ar);
284                                         break;
285                         }
286                         break;
287                 case NC_OBJECT:
288                         switch(wmn->data) {
289                                 case ND_BONE_ACTIVE:
290                                 case ND_BONE_SELECT:
291                                 case ND_KEYS:
292                                         ED_region_tag_redraw(ar);
293                                         break;
294                                 case ND_MODIFIER:
295                                         if(wmn->action == NA_RENAME)
296                                                 ED_region_tag_redraw(ar);
297                                         break;
298                         }
299                         break;
300                 case NC_ID:
301                         if(wmn->action == NA_RENAME)
302                                 ED_region_tag_redraw(ar);
303                         break;
304                 default:
305                         if(wmn->data==ND_KEYS)
306                                 ED_region_tag_redraw(ar);
307         }
308 }
309
310 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
311 {
312         /* context changes */
313         switch(wmn->category) {
314                 case NC_ANIMATION:
315                         ED_region_tag_redraw(ar);
316                         break;
317                 case NC_SCENE:
318                         switch(wmn->data) {
319                                 case ND_RENDER_OPTIONS:
320                                 case ND_OB_ACTIVE:
321                                 case ND_FRAME:
322                                 case ND_MARKERS:
323                                         ED_region_tag_redraw(ar);
324                                         break;
325                         }
326                         break;
327                 case NC_OBJECT:
328                         switch(wmn->data) {
329                                 case ND_BONE_ACTIVE:
330                                 case ND_BONE_SELECT:
331                                 case ND_KEYS:
332                                 case ND_TRANSFORM:
333                                         ED_region_tag_redraw(ar);
334                                         break;
335                         }
336                         break;
337                 case NC_NODE:
338                         switch(wmn->action) {
339                                 case NA_EDITED:
340                                         ED_region_tag_redraw(ar);
341                                         break;
342                         }
343                         break;
344                 case NC_ID:
345                         if(wmn->action == NA_RENAME)
346                                 ED_region_tag_redraw(ar);
347                         break;
348                                 
349                 default:
350                         if(wmn->data==ND_KEYS)
351                                 ED_region_tag_redraw(ar);
352         }
353 }
354
355 /* editor level listener */
356 static void action_listener(ScrArea *sa, wmNotifier *wmn)
357 {
358         SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
359         
360         /* context changes */
361         switch (wmn->category) {
362                 case NC_ANIMATION:
363                         /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
364                         if (ELEM(wmn->data, ND_KEYFRAME_SELECT, ND_ANIMCHAN_SELECT))
365                                 ED_area_tag_redraw(sa);
366                         else
367                                 ED_area_tag_refresh(sa);
368                         break;
369                 case NC_SCENE:
370                         switch (wmn->data) {    
371                                 case ND_OB_ACTIVE:      /* selection changed, so force refresh to flush (needs flag set to do syncing) */
372                                 case ND_OB_SELECT:
373                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
374                                         ED_area_tag_refresh(sa);
375                                         break;
376                                         
377                                 default: /* just redrawing the view will do */
378                                         ED_area_tag_redraw(sa);
379                                         break;
380                         }
381                         break;
382                 case NC_OBJECT:
383                         switch (wmn->data) {
384                                 case ND_BONE_SELECT:    /* selection changed, so force refresh to flush (needs flag set to do syncing) */
385                                 case ND_BONE_ACTIVE:
386                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
387                                         ED_area_tag_refresh(sa);
388                                         break;
389                                 case ND_VIEW3D_TRANSFORM:
390                                         break; /*do nothing*/                                   
391                                 default: /* just redrawing the view will do */
392                                         ED_area_tag_redraw(sa);
393                                         break;
394                         }
395                         break;
396                 case NC_SPACE:
397                         switch (wmn->data) {
398                                 case ND_SPACE_DOPESHEET:
399                                         ED_area_tag_redraw(sa);
400                                         break;
401                                 case ND_SPACE_CHANGED:
402                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
403                                         ED_area_tag_refresh(sa);
404                                         break;
405                         }                       
406                         break;
407         }
408 }
409
410 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
411 {
412         /* context changes */
413         switch(wmn->category) {
414                 case NC_SCENE:
415                         switch(wmn->data) {
416                                 case ND_OB_ACTIVE:
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         }
426 }
427
428 static void action_refresh(const bContext *C, ScrArea *sa)
429 {
430         SpaceAction *saction= CTX_wm_space_action(C);
431         
432         /* update the state of the animchannels in response to changes from the data they represent 
433          * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
434          */
435         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
436                 ANIM_sync_animchannels_to_data(C);
437                 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
438         }
439         
440         /* region updates? */
441         // XXX resizing y-extents of tot should go here?
442 }
443
444 /* only called once, from space/spacetypes.c */
445 void ED_spacetype_action(void)
446 {
447         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
448         ARegionType *art;
449         
450         st->spaceid= SPACE_ACTION;
451         strncpy(st->name, "Action", BKE_ST_MAXNAME);
452         
453         st->new= action_new;
454         st->free= action_free;
455         st->init= action_init;
456         st->duplicate= action_duplicate;
457         st->operatortypes= action_operatortypes;
458         st->keymap= action_keymap;
459         st->listener= action_listener;
460         st->refresh= action_refresh;
461         
462         /* regions: main window */
463         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
464         art->regionid = RGN_TYPE_WINDOW;
465         art->init= action_main_area_init;
466         art->draw= action_main_area_draw;
467         art->listener= action_main_area_listener;
468         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
469
470         BLI_addhead(&st->regiontypes, art);
471         
472         /* regions: header */
473         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
474         art->regionid = RGN_TYPE_HEADER;
475         art->minsizey= HEADERY;
476         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
477         
478         art->init= action_header_area_init;
479         art->draw= action_header_area_draw;
480         art->listener= action_header_area_listener;
481         
482         BLI_addhead(&st->regiontypes, art);
483         
484         /* regions: channels */
485         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
486         art->regionid = RGN_TYPE_CHANNELS;
487         art->minsizex= 200;
488         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
489         
490         art->init= action_channel_area_init;
491         art->draw= action_channel_area_draw;
492         art->listener= action_channel_area_listener;
493         
494         BLI_addhead(&st->regiontypes, art);
495         
496         
497         BKE_spacetype_register(st);
498 }
499