converted more mixed tab/space indentations to tabs. only whitespace changes.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file blender/editors/space_action/space_action.c
30  *  \ingroup spaction
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "DNA_action_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_rand.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
49
50 #include "ED_screen.h"
51
52 #include "BIF_gl.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "UI_resources.h"
58 #include "UI_view2d.h"
59
60 #include "ED_space_api.h"
61 #include "ED_anim_api.h"
62 #include "ED_markers.h"
63
64 #include "action_intern.h"      // own include
65
66 /* ******************** default callbacks for action space ***************** */
67
68 static SpaceLink *action_new(const bContext *C)
69 {
70         ScrArea *sa= CTX_wm_area(C);
71         SpaceAction *saction;
72         ARegion *ar;
73         
74         saction= MEM_callocN(sizeof(SpaceAction), "initaction");
75         saction->spacetype= SPACE_ACTION;
76         
77         saction->autosnap = SACTSNAP_FRAME;
78         saction->mode= SACTCONT_DOPESHEET;
79         
80         /* header */
81         ar= MEM_callocN(sizeof(ARegion), "header for action");
82         
83         BLI_addtail(&saction->regionbase, ar);
84         ar->regiontype= RGN_TYPE_HEADER;
85         ar->alignment= RGN_ALIGN_BOTTOM;
86         
87         /* channel list region */
88         ar= MEM_callocN(sizeof(ARegion), "channel area for action");
89         BLI_addtail(&saction->regionbase, ar);
90         ar->regiontype= RGN_TYPE_CHANNELS;
91         ar->alignment= RGN_ALIGN_LEFT;
92         
93                 /* only need to set scroll settings, as this will use 'listview' v2d configuration */
94         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
95         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
96         
97         /* main area */
98         ar= MEM_callocN(sizeof(ARegion), "main area for action");
99         
100         BLI_addtail(&saction->regionbase, ar);
101         ar->regiontype= RGN_TYPE_WINDOW;
102         
103         ar->v2d.tot.xmin= -10.0f;
104         ar->v2d.tot.ymin= (float)(-sa->winy)/3.0f;
105         ar->v2d.tot.xmax= (float)(sa->winx);
106         ar->v2d.tot.ymax= 0.0f;
107         
108         ar->v2d.cur = ar->v2d.tot;
109         
110         ar->v2d.min[0]= 0.0f;
111         ar->v2d.min[1]= 0.0f;
112         
113         ar->v2d.max[0]= MAXFRAMEF;
114         ar->v2d.max[1]= FLT_MAX;
115
116         ar->v2d.minzoom= 0.01f;
117         ar->v2d.maxzoom= 50;
118         ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
119         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
120         ar->v2d.keepzoom= V2D_LOCKZOOM_Y;
121         ar->v2d.keepofs= V2D_KEEPOFS_Y;
122         ar->v2d.align= V2D_ALIGN_NO_POS_Y;
123         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
124         
125         return (SpaceLink *)saction;
126 }
127
128 /* not spacelink itself */
129 static void action_free(SpaceLink *UNUSED(sl))
130 {       
131 //      SpaceAction *saction= (SpaceAction*) sl;
132         
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         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         bAnimContext ac;
233         View2D *v2d= &ar->v2d;
234         View2DScrollers *scrollers;
235         
236         /* clear and setup matrix */
237         UI_ThemeClearColor(TH_BACK);
238         glClear(GL_COLOR_BUFFER_BIT);
239         
240         UI_view2d_view_ortho(v2d);
241         
242         /* data */
243         if (ANIM_animdata_get_context(C, &ac)) {
244                 draw_channel_names((bContext *)C, &ac, ar);
245         }
246         
247         /* reset view matrix */
248         UI_view2d_view_restore(C);
249         
250         /* scrollers */
251         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
252         UI_view2d_scrollers_draw(C, v2d, scrollers);
253         UI_view2d_scrollers_free(scrollers);
254 }
255
256
257 /* add handlers, stuff you only do once or on area/region changes */
258 static void action_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
259 {
260         ED_region_header_init(ar);
261 }
262
263 static void action_header_area_draw(const bContext *C, ARegion *ar)
264 {
265         ED_region_header(C, ar);
266 }
267
268 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
269 {
270         /* context changes */
271         switch(wmn->category) {
272                 case NC_ANIMATION:
273                         ED_region_tag_redraw(ar);
274                         break;
275                 case NC_SCENE:
276                         switch(wmn->data) {
277                                 case ND_OB_ACTIVE:
278                                 case ND_FRAME:
279                                         ED_region_tag_redraw(ar);
280                                         break;
281                         }
282                         break;
283                 case NC_OBJECT:
284                         switch(wmn->data) {
285                                 case ND_BONE_ACTIVE:
286                                 case ND_BONE_SELECT:
287                                 case ND_KEYS:
288                                         ED_region_tag_redraw(ar);
289                                         break;
290                                 case ND_MODIFIER:
291                                         if(wmn->action == NA_RENAME)
292                                                 ED_region_tag_redraw(ar);
293                                         break;
294                         }
295                         break;
296                 case NC_ID:
297                         if(wmn->action == NA_RENAME)
298                                 ED_region_tag_redraw(ar);
299                         break;
300                 default:
301                         if(wmn->data==ND_KEYS)
302                                 ED_region_tag_redraw(ar);
303         }
304 }
305
306 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
307 {
308         /* context changes */
309         switch(wmn->category) {
310                 case NC_ANIMATION:
311                         ED_region_tag_redraw(ar);
312                         break;
313                 case NC_SCENE:
314                         switch(wmn->data) {
315                                 case ND_RENDER_OPTIONS:
316                                 case ND_OB_ACTIVE:
317                                 case ND_FRAME:
318                                 case ND_MARKERS:
319                                         ED_region_tag_redraw(ar);
320                                         break;
321                         }
322                         break;
323                 case NC_OBJECT:
324                         switch(wmn->data) {
325                                 case ND_TRANSFORM:
326                                         /* moving object shouldn't need to redraw action */
327                                         break;
328                                 case ND_BONE_ACTIVE:
329                                 case ND_BONE_SELECT:
330                                 case ND_KEYS:
331                                         ED_region_tag_redraw(ar);
332                                         break;
333                         }
334                         break;
335                 case NC_NODE:
336                         switch(wmn->action) {
337                                 case NA_EDITED:
338                                         ED_region_tag_redraw(ar);
339                                         break;
340                         }
341                         break;
342                 case NC_ID:
343                         if(wmn->action == NA_RENAME)
344                                 ED_region_tag_redraw(ar);
345                         break;
346                                 
347                 default:
348                         if(wmn->data==ND_KEYS)
349                                 ED_region_tag_redraw(ar);
350         }
351 }
352
353 /* editor level listener */
354 static void action_listener(ScrArea *sa, wmNotifier *wmn)
355 {
356         SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
357         
358         /* context changes */
359         switch (wmn->category) {
360                 case NC_SCREEN:
361                         if (wmn->data == ND_GPENCIL) {
362                                 /* only handle this event in GPencil mode for performance considerations */
363                                 if (saction->mode == SACTCONT_GPENCIL)  
364                                         ED_area_tag_redraw(sa);
365                         }
366                         break;
367                 case NC_ANIMATION:
368                         /* for NLA tweakmode enter/exit, need complete refresh */
369                         if (wmn->data == ND_NLA_ACTCHANGE) {
370                                 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
371                                 ED_area_tag_refresh(sa);
372                         }
373                         /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
374                         else if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED))
375                                 ED_area_tag_redraw(sa);
376                         else
377                                 ED_area_tag_refresh(sa);
378                         break;
379                 case NC_SCENE:
380                         switch (wmn->data) {    
381                                 case ND_OB_ACTIVE:      /* selection changed, so force refresh to flush (needs flag set to do syncing) */
382                                 case ND_OB_SELECT:
383                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
384                                         ED_area_tag_refresh(sa);
385                                         break;
386                                         
387                                 default: /* just redrawing the view will do */
388                                         ED_area_tag_redraw(sa);
389                                         break;
390                         }
391                         break;
392                 case NC_OBJECT:
393                         switch (wmn->data) {
394                                 case ND_BONE_SELECT:    /* selection changed, so force refresh to flush (needs flag set to do syncing) */
395                                 case ND_BONE_ACTIVE:
396                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
397                                         ED_area_tag_refresh(sa);
398                                         break;
399                                 case ND_TRANSFORM:
400                                         /* moving object shouldn't need to redraw action */
401                                         break;
402                                 default: /* just redrawing the view will do */
403                                         ED_area_tag_redraw(sa);
404                                         break;
405                         }
406                         break;
407                 case NC_SPACE:
408                         switch (wmn->data) {
409                                 case ND_SPACE_DOPESHEET:
410                                         ED_area_tag_redraw(sa);
411                                         break;
412                                 case ND_SPACE_CHANGED:
413                                         saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
414                                         ED_area_tag_refresh(sa);
415                                         break;
416                         }                       
417                         break;
418         }
419 }
420
421 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
422 {
423         /* context changes */
424         switch (wmn->category) {
425                 case NC_SCENE:
426                         switch (wmn->data) {
427                                 case ND_OB_ACTIVE:
428                                         ED_region_tag_redraw(ar);
429                                         break;
430                         }
431                         break;
432                 case NC_ID:
433                         if(wmn->action == NA_RENAME)
434                                 ED_region_tag_redraw(ar);
435                         break;
436         }
437 }
438
439 static void action_refresh(const bContext *C, ScrArea *sa)
440 {
441         SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
442         
443         /* update the state of the animchannels in response to changes from the data they represent 
444          * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
445          */
446         if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
447                 ANIM_sync_animchannels_to_data(C);
448                 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
449                 ED_area_tag_redraw(sa);
450         }
451         
452         /* region updates? */
453         // XXX resizing y-extents of tot should go here?
454 }
455
456 /* only called once, from space/spacetypes.c */
457 void ED_spacetype_action(void)
458 {
459         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
460         ARegionType *art;
461         
462         st->spaceid= SPACE_ACTION;
463         strncpy(st->name, "Action", BKE_ST_MAXNAME);
464         
465         st->new= action_new;
466         st->free= action_free;
467         st->init= action_init;
468         st->duplicate= action_duplicate;
469         st->operatortypes= action_operatortypes;
470         st->keymap= action_keymap;
471         st->listener= action_listener;
472         st->refresh= action_refresh;
473         
474         /* regions: main window */
475         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
476         art->regionid = RGN_TYPE_WINDOW;
477         art->init= action_main_area_init;
478         art->draw= action_main_area_draw;
479         art->listener= action_main_area_listener;
480         art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_MARKERS|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
481
482         BLI_addhead(&st->regiontypes, art);
483         
484         /* regions: header */
485         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
486         art->regionid = RGN_TYPE_HEADER;
487         art->prefsizey= HEADERY;
488         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
489         
490         art->init= action_header_area_init;
491         art->draw= action_header_area_draw;
492         art->listener= action_header_area_listener;
493         
494         BLI_addhead(&st->regiontypes, art);
495         
496         /* regions: channels */
497         art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
498         art->regionid = RGN_TYPE_CHANNELS;
499         art->prefsizex= 200;
500         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
501         
502         art->init= action_channel_area_init;
503         art->draw= action_channel_area_draw;
504         art->listener= action_channel_area_listener;
505         
506         BLI_addhead(&st->regiontypes, art);
507         
508         
509         BKE_spacetype_register(st);
510 }
511