2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_action/space_action.c
35 #include "DNA_action_types.h"
36 #include "DNA_scene_types.h"
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
48 #include "ED_screen.h"
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
58 #include "ED_space_api.h"
59 #include "ED_screen.h"
60 #include "ED_anim_api.h"
61 #include "ED_markers.h"
63 #include "action_intern.h" // own include
65 /* ******************** default callbacks for action space ***************** */
67 static SpaceLink *action_new(const bContext *C)
69 ScrArea *sa= CTX_wm_area(C);
73 saction= MEM_callocN(sizeof(SpaceAction), "initaction");
74 saction->spacetype= SPACE_ACTION;
76 saction->autosnap = SACTSNAP_FRAME;
77 saction->mode= SACTCONT_DOPESHEET;
79 saction->ads.filterflag |= ADS_FILTER_SUMMARY;
82 ar= MEM_callocN(sizeof(ARegion), "header for action");
84 BLI_addtail(&saction->regionbase, ar);
85 ar->regiontype= RGN_TYPE_HEADER;
86 ar->alignment= RGN_ALIGN_BOTTOM;
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;
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;
99 ar= MEM_callocN(sizeof(ARegion), "main area for action");
101 BLI_addtail(&saction->regionbase, ar);
102 ar->regiontype= RGN_TYPE_WINDOW;
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;
109 ar->v2d.cur = ar->v2d.tot;
111 ar->v2d.min[0]= 0.0f;
112 ar->v2d.min[1]= 0.0f;
114 ar->v2d.max[0]= MAXFRAMEF;
115 ar->v2d.max[1]= FLT_MAX;
117 ar->v2d.minzoom= 0.01f;
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;
126 return (SpaceLink *)saction;
129 /* not spacelink itself */
130 static void action_free(SpaceLink *UNUSED(sl))
132 // SpaceAction *saction= (SpaceAction*) sl;
137 /* spacetype; init callback */
138 static void action_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
140 SpaceAction *saction = sa->spacedata.first;
141 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
144 static SpaceLink *action_duplicate(SpaceLink *sl)
146 SpaceAction *sactionn= MEM_dupallocN(sl);
148 /* clear or remove stuff from old */
150 return (SpaceLink *)sactionn;
155 /* add handlers, stuff you only do once or on area/region changes */
156 static void action_main_area_init(wmWindowManager *wm, ARegion *ar)
160 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
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);
167 static void action_main_area_draw(const bContext *C, ARegion *ar)
169 /* draw entirely, view changes should be handled here */
170 SpaceAction *saction= CTX_wm_space_action(C);
172 View2D *v2d= &ar->v2d;
174 View2DScrollers *scrollers;
175 short unit=0, flag=0;
177 /* clear and setup matrix */
178 UI_ThemeClearColor(TH_BACK);
179 glClear(GL_COLOR_BUFFER_BIT);
181 UI_view2d_view_ortho(v2d);
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);
190 if (ANIM_animdata_get_context(C, &ac)) {
191 draw_channel_strips(&ac, saction, ar);
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);
200 UI_view2d_view_orthoSpecial(ar, v2d, 1);
202 flag = (ac.markers && (ac.markers != &ac.scene->markers))? DRAW_MARKERS_LOCAL : 0;
203 draw_markers_time(C, flag);
206 UI_view2d_view_ortho(v2d);
207 ANIM_draw_previewrange(C, v2d);
209 /* reset view matrix */
210 UI_view2d_view_restore(C);
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);
218 /* add handlers, stuff you only do once or on area/region changes */
219 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
223 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
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);
230 static void action_channel_area_draw(const bContext *C, ARegion *ar)
232 /* draw entirely, view changes should be handled here */
234 View2D *v2d= &ar->v2d;
235 View2DScrollers *scrollers;
237 /* clear and setup matrix */
238 UI_ThemeClearColor(TH_BACK);
239 glClear(GL_COLOR_BUFFER_BIT);
241 UI_view2d_view_ortho(v2d);
244 if (ANIM_animdata_get_context(C, &ac)) {
245 draw_channel_names((bContext *)C, &ac, ar);
248 /* reset view matrix */
249 UI_view2d_view_restore(C);
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);
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)
261 ED_region_header_init(ar);
264 static void action_header_area_draw(const bContext *C, ARegion *ar)
266 ED_region_header(C, ar);
269 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
271 /* context changes */
272 switch(wmn->category) {
274 ED_region_tag_redraw(ar);
280 ED_region_tag_redraw(ar);
289 ED_region_tag_redraw(ar);
292 if(wmn->action == NA_RENAME)
293 ED_region_tag_redraw(ar);
298 if(wmn->action == NA_RENAME)
299 ED_region_tag_redraw(ar);
302 if(wmn->data==ND_KEYS)
303 ED_region_tag_redraw(ar);
307 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
309 /* context changes */
310 switch(wmn->category) {
312 ED_region_tag_redraw(ar);
316 case ND_RENDER_OPTIONS:
320 ED_region_tag_redraw(ar);
327 /* moving object shouldn't need to redraw action */
332 ED_region_tag_redraw(ar);
337 switch(wmn->action) {
339 ED_region_tag_redraw(ar);
344 if(wmn->action == NA_RENAME)
345 ED_region_tag_redraw(ar);
349 if(wmn->data==ND_KEYS)
350 ED_region_tag_redraw(ar);
354 /* editor level listener */
355 static void action_listener(ScrArea *sa, wmNotifier *wmn)
357 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
359 /* context changes */
360 switch (wmn->category) {
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);
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);
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);
378 ED_area_tag_refresh(sa);
382 case ND_OB_ACTIVE: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
384 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
385 ED_area_tag_refresh(sa);
388 default: /* just redrawing the view will do */
389 ED_area_tag_redraw(sa);
395 case ND_BONE_SELECT: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
397 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
398 ED_area_tag_refresh(sa);
401 /* moving object shouldn't need to redraw action */
403 default: /* just redrawing the view will do */
404 ED_area_tag_redraw(sa);
409 if (wmn->action == NA_SELECTED) {
410 /* selection changed, so force refresh to flush (needs flag set to do syncing) */
411 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
412 ED_area_tag_refresh(sa);
417 case ND_SPACE_DOPESHEET:
418 ED_area_tag_redraw(sa);
420 case ND_SPACE_CHANGED:
421 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
422 ED_area_tag_refresh(sa);
429 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
431 /* context changes */
432 switch (wmn->category) {
436 ED_region_tag_redraw(ar);
441 if(wmn->action == NA_RENAME)
442 ED_region_tag_redraw(ar);
447 static void action_refresh(const bContext *C, ScrArea *sa)
449 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
451 /* update the state of the animchannels in response to changes from the data they represent
452 * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
454 if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
455 ANIM_sync_animchannels_to_data(C);
456 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
457 ED_area_tag_redraw(sa);
460 /* region updates? */
461 // XXX resizing y-extents of tot should go here?
464 /* only called once, from space/spacetypes.c */
465 void ED_spacetype_action(void)
467 SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype action");
470 st->spaceid= SPACE_ACTION;
471 strncpy(st->name, "Action", BKE_ST_MAXNAME);
474 st->free= action_free;
475 st->init= action_init;
476 st->duplicate= action_duplicate;
477 st->operatortypes= action_operatortypes;
478 st->keymap= action_keymap;
479 st->listener= action_listener;
480 st->refresh= action_refresh;
482 /* regions: main window */
483 art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
484 art->regionid = RGN_TYPE_WINDOW;
485 art->init= action_main_area_init;
486 art->draw= action_main_area_draw;
487 art->listener= action_main_area_listener;
488 art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_MARKERS|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
490 BLI_addhead(&st->regiontypes, art);
492 /* regions: header */
493 art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
494 art->regionid = RGN_TYPE_HEADER;
495 art->prefsizey= HEADERY;
496 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
498 art->init= action_header_area_init;
499 art->draw= action_header_area_draw;
500 art->listener= action_header_area_listener;
502 BLI_addhead(&st->regiontypes, art);
504 /* regions: channels */
505 art= MEM_callocN(sizeof(ARegionType), "spacetype action region");
506 art->regionid = RGN_TYPE_CHANNELS;
508 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
510 art->init= action_channel_area_init;
511 art->draw= action_channel_area_draw;
512 art->listener= action_channel_area_listener;
514 BLI_addhead(&st->regiontypes, art);
517 BKE_spacetype_register(st);