Fix for key map python file export and load
[blender.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
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         float col[3];
175         short unit=0, flag=0;
176         
177         /* clear and setup matrix */
178         UI_GetThemeColor3fv(TH_BACK, col);
179         glClearColor(col[0], col[1], col[2], 0.0);
180         glClear(GL_COLOR_BUFFER_BIT);
181         
182         UI_view2d_view_ortho(C, v2d);
183         
184         /* time grid */
185         unit= (saction->flag & SACTION_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
186         grid= UI_view2d_grid_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
187         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
188         UI_view2d_grid_free(grid);
189         
190         /* data */
191         if (ANIM_animdata_get_context(C, &ac)) {
192                 draw_channel_strips(&ac, saction, ar);
193         }
194         
195         /* current frame */
196         if (saction->flag & SACTION_DRAWTIME)   flag |= DRAWCFRA_UNIT_SECONDS;
197         if ((saction->flag & SACTION_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
198         ANIM_draw_cfra(C, v2d, flag);
199         
200         /* markers */
201         UI_view2d_view_orthoSpecial(C, v2d, 1);
202         draw_markers_time(C, 0);
203         
204         /* preview range */
205         UI_view2d_view_ortho(C, 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         SpaceAction *saction= CTX_wm_space_action(C);
233         bAnimContext ac;
234         View2D *v2d= &ar->v2d;
235         View2DScrollers *scrollers;
236         float col[3];
237         
238         /* clear and setup matrix */
239         UI_GetThemeColor3fv(TH_BACK, col);
240         glClearColor(col[0], col[1], col[2], 0.0);
241         glClear(GL_COLOR_BUFFER_BIT);
242         
243         UI_view2d_view_ortho(C, v2d);
244         
245         /* data */
246         if (ANIM_animdata_get_context(C, &ac)) {
247                 draw_channel_names((bContext *)C, &ac, saction, ar);
248         }
249         
250         /* reset view matrix */
251         UI_view2d_view_restore(C);
252         
253         /* scrollers */
254         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
255         UI_view2d_scrollers_draw(C, v2d, scrollers);
256         UI_view2d_scrollers_free(scrollers);
257 }
258
259
260 /* add handlers, stuff you only do once or on area/region changes */
261 static void action_header_area_init(wmWindowManager *wm, ARegion *ar)
262 {
263         ED_region_header_init(ar);
264 }
265
266 static void action_header_area_draw(const bContext *C, ARegion *ar)
267 {
268         ED_region_header(C, ar);
269 }
270
271 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
272 {
273         /* context changes */
274         switch(wmn->category) {
275                 case NC_ANIMATION:
276                         ED_region_tag_redraw(ar);
277                         break;
278                 case NC_SCENE:
279                         switch(wmn->data) {
280                                 case ND_OB_ACTIVE:
281                                 case ND_FRAME:
282                                         ED_region_tag_redraw(ar);
283                                         break;
284                         }
285                         break;
286                 case NC_OBJECT:
287                         switch(wmn->data) {
288                                 case ND_BONE_ACTIVE:
289                                 case ND_BONE_SELECT:
290                                 case ND_KEYS:
291                                         ED_region_tag_redraw(ar);
292                                         break;
293                                 case ND_MODIFIER:
294                                         if(wmn->action == NA_RENAME)
295                                                 ED_region_tag_redraw(ar);
296                                         break;
297                         }
298                         break;
299                 case NC_ID:
300                         if(wmn->action == NA_RENAME)
301                                 ED_region_tag_redraw(ar);
302                         break;
303                 default:
304                         if(wmn->data==ND_KEYS)
305                                 ED_region_tag_redraw(ar);
306         }
307 }
308
309 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
310 {
311         /* context changes */
312         switch(wmn->category) {
313                 case NC_ANIMATION:
314                         ED_region_tag_redraw(ar);
315                         break;
316                 case NC_SCENE:
317                         switch(wmn->data) {
318                                 case ND_RENDER_OPTIONS:
319                                 case ND_OB_ACTIVE:
320                                 case ND_FRAME:
321                                 case ND_MARKERS:
322                                         ED_region_tag_redraw(ar);
323                                         break;
324                         }
325                         break;
326                 case NC_OBJECT:
327                         switch(wmn->data) {
328                                 case ND_BONE_ACTIVE:
329                                 case ND_BONE_SELECT:
330                                 case ND_KEYS:
331                                 case ND_TRANSFORM:
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_ANIMATION:
362                         /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
363                         if (ELEM(wmn->data, ND_KEYFRAME_SELECT, ND_ANIMCHAN_SELECT))
364                                 ED_area_tag_redraw(sa);
365                         else
366                                 ED_area_tag_refresh(sa);
367                         break;
368                 case NC_SCENE:
369                         switch (wmn->data) {    
370                                 case ND_OB_ACTIVE:      /* selection changed, so force refresh to flush (needs flag set to do syncing) */
371                                 case ND_OB_SELECT:
372                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
373                                         ED_area_tag_refresh(sa);
374                                         break;
375                                         
376                                 default: /* just redrawing the view will do */
377                                         ED_area_tag_redraw(sa);
378                                         break;
379                         }
380                         break;
381                 case NC_OBJECT:
382                         switch (wmn->data) {
383                                 case ND_BONE_SELECT:    /* selection changed, so force refresh to flush (needs flag set to do syncing) */
384                                 case ND_BONE_ACTIVE:
385                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
386                                         ED_area_tag_refresh(sa);
387                                         break;
388                                         
389                                 default: /* just redrawing the view will do */
390                                         ED_area_tag_redraw(sa);
391                                         break;
392                         }
393                         break;
394                 case NC_SPACE:
395                         if(wmn->data == ND_SPACE_DOPESHEET)
396                                 ED_area_tag_redraw(sa);
397                         break;
398         }
399 }
400
401 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
402 {
403         /* context changes */
404         switch(wmn->category) {
405                 case NC_SCENE:
406                         switch(wmn->data) {
407                                 case ND_OB_ACTIVE:
408                                         ED_region_tag_redraw(ar);
409                                         break;
410                         }
411                         break;
412                 case NC_ID:
413                         if(wmn->action == NA_RENAME)
414                                 ED_region_tag_redraw(ar);
415                         break;
416         }
417 }
418
419 static void action_refresh(const bContext *C, ScrArea *sa)
420 {
421         SpaceAction *saction= CTX_wm_space_action(C);
422         
423         /* update the state of the animchannels in response to changes from the data they represent 
424          * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
425          */
426         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
427                 ANIM_sync_animchannels_to_data(C);
428                 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
429         }
430         
431         /* region updates? */
432         // XXX resizing y-extents of tot should go here?
433 }
434
435 /* only called once, from space/spacetypes.c */
436 void ED_spacetype_action(void)
437 {
438         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
439         ARegionType *art;
440         
441         st->spaceid= SPACE_ACTION;
442         strncpy(st->name, "Action", BKE_ST_MAXNAME);
443         
444         st->new= action_new;
445         st->free= action_free;
446         st->init= action_init;
447         st->duplicate= action_duplicate;
448         st->operatortypes= action_operatortypes;
449         st->keymap= action_keymap;
450         st->listener= action_listener;
451         st->refresh= action_refresh;
452         
453         /* regions: main window */
454         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
455         art->regionid = RGN_TYPE_WINDOW;
456         art->init= action_main_area_init;
457         art->draw= action_main_area_draw;
458         art->listener= action_main_area_listener;
459         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
460
461         BLI_addhead(&st->regiontypes, art);
462         
463         /* regions: header */
464         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
465         art->regionid = RGN_TYPE_HEADER;
466         art->minsizey= HEADERY;
467         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
468         
469         art->init= action_header_area_init;
470         art->draw= action_header_area_draw;
471         art->listener= action_header_area_listener;
472         
473         BLI_addhead(&st->regiontypes, art);
474         
475         /* regions: channels */
476         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
477         art->regionid = RGN_TYPE_CHANNELS;
478         art->minsizex= 200;
479         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
480         
481         art->init= action_channel_area_init;
482         art->draw= action_channel_area_draw;
483         art->listener= action_channel_area_listener;
484         
485         BLI_addhead(&st->regiontypes, art);
486         
487         
488         BKE_spacetype_register(st);
489 }
490