Drag and drop 2.5 integration! Finally, slashdot regulars can use
[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_TRANSFORM:
330                                         /* moving object shouldn't need to redraw action */
331                                         break;
332                                 case ND_BONE_ACTIVE:
333                                 case ND_BONE_SELECT:
334                                 case ND_KEYS:
335                                         ED_region_tag_redraw(ar);
336                                         break;
337                         }
338                         break;
339                 case NC_NODE:
340                         switch(wmn->action) {
341                                 case NA_EDITED:
342                                         ED_region_tag_redraw(ar);
343                                         break;
344                         }
345                         break;
346                 case NC_ID:
347                         if(wmn->action == NA_RENAME)
348                                 ED_region_tag_redraw(ar);
349                         break;
350                                 
351                 default:
352                         if(wmn->data==ND_KEYS)
353                                 ED_region_tag_redraw(ar);
354         }
355 }
356
357 /* editor level listener */
358 static void action_listener(ScrArea *sa, wmNotifier *wmn)
359 {
360         SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
361         
362         /* context changes */
363         switch (wmn->category) {
364                 case NC_ANIMATION:
365                         /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
366                         if (ELEM(wmn->data, ND_KEYFRAME_SELECT, ND_ANIMCHAN_SELECT))
367                                 ED_area_tag_redraw(sa);
368                         else
369                                 ED_area_tag_refresh(sa);
370                         break;
371                 case NC_SCENE:
372                         switch (wmn->data) {    
373                                 case ND_OB_ACTIVE:      /* selection changed, so force refresh to flush (needs flag set to do syncing) */
374                                 case ND_OB_SELECT:
375                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
376                                         ED_area_tag_refresh(sa);
377                                         break;
378                                         
379                                 default: /* just redrawing the view will do */
380                                         ED_area_tag_redraw(sa);
381                                         break;
382                         }
383                         break;
384                 case NC_OBJECT:
385                         switch (wmn->data) {
386                                 case ND_BONE_SELECT:    /* selection changed, so force refresh to flush (needs flag set to do syncing) */
387                                 case ND_BONE_ACTIVE:
388                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
389                                         ED_area_tag_refresh(sa);
390                                         break;
391                                 case ND_TRANSFORM:
392                                         /* moving object shouldn't need to redraw action */
393                                         break;
394                                 default: /* just redrawing the view will do */
395                                         ED_area_tag_redraw(sa);
396                                         break;
397                         }
398                         break;
399                 case NC_SPACE:
400                         switch (wmn->data) {
401                                 case ND_SPACE_DOPESHEET:
402                                         ED_area_tag_redraw(sa);
403                                         break;
404                                 case ND_SPACE_CHANGED:
405                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
406                                         ED_area_tag_refresh(sa);
407                                         break;
408                         }                       
409                         break;
410         }
411 }
412
413 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
414 {
415         /* context changes */
416         switch(wmn->category) {
417                 case NC_SCENE:
418                         switch(wmn->data) {
419                                 case ND_OB_ACTIVE:
420                                         ED_region_tag_redraw(ar);
421                                         break;
422                         }
423                         break;
424                 case NC_ID:
425                         if(wmn->action == NA_RENAME)
426                                 ED_region_tag_redraw(ar);
427                         break;
428         }
429 }
430
431 static void action_refresh(const bContext *C, ScrArea *sa)
432 {
433         SpaceAction *saction= CTX_wm_space_action(C);
434         
435         /* update the state of the animchannels in response to changes from the data they represent 
436          * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
437          */
438         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
439                 ANIM_sync_animchannels_to_data(C);
440                 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
441         }
442         
443         /* region updates? */
444         // XXX resizing y-extents of tot should go here?
445 }
446
447 /* only called once, from space/spacetypes.c */
448 void ED_spacetype_action(void)
449 {
450         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
451         ARegionType *art;
452         
453         st->spaceid= SPACE_ACTION;
454         strncpy(st->name, "Action", BKE_ST_MAXNAME);
455         
456         st->new= action_new;
457         st->free= action_free;
458         st->init= action_init;
459         st->duplicate= action_duplicate;
460         st->operatortypes= action_operatortypes;
461         st->keymap= action_keymap;
462         st->listener= action_listener;
463         st->refresh= action_refresh;
464         
465         /* regions: main window */
466         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
467         art->regionid = RGN_TYPE_WINDOW;
468         art->init= action_main_area_init;
469         art->draw= action_main_area_draw;
470         art->listener= action_main_area_listener;
471         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
472
473         BLI_addhead(&st->regiontypes, art);
474         
475         /* regions: header */
476         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
477         art->regionid = RGN_TYPE_HEADER;
478         art->prefsizey= HEADERY;
479         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
480         
481         art->init= action_header_area_init;
482         art->draw= action_header_area_draw;
483         art->listener= action_header_area_listener;
484         
485         BLI_addhead(&st->regiontypes, art);
486         
487         /* regions: channels */
488         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
489         art->regionid = RGN_TYPE_CHANNELS;
490         art->prefsizex= 200;
491         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
492         
493         art->init= action_channel_area_init;
494         art->draw= action_channel_area_draw;
495         art->listener= action_channel_area_listener;
496         
497         BLI_addhead(&st->regiontypes, art);
498         
499         
500         BKE_spacetype_register(st);
501 }
502