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;
118 ar->v2d.maxzoom = 50;
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;
136 /* spacetype; init callback */
137 static void action_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
139 SpaceAction *saction = sa->spacedata.first;
140 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
143 static SpaceLink *action_duplicate(SpaceLink *sl)
145 SpaceAction *sactionn = MEM_dupallocN(sl);
147 /* clear or remove stuff from old */
149 return (SpaceLink *)sactionn;
154 /* add handlers, stuff you only do once or on area/region changes */
155 static void action_main_area_init(wmWindowManager *wm, ARegion *ar)
159 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
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);
166 static void action_main_area_draw(const bContext *C, ARegion *ar)
168 /* draw entirely, view changes should be handled here */
169 SpaceAction *saction = CTX_wm_space_action(C);
171 View2D *v2d = &ar->v2d;
173 View2DScrollers *scrollers;
174 short unit = 0, flag = 0;
176 /* clear and setup matrix */
177 UI_ThemeClearColor(TH_BACK);
178 glClear(GL_COLOR_BUFFER_BIT);
180 UI_view2d_view_ortho(v2d);
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);
188 ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
191 if (ANIM_animdata_get_context(C, &ac)) {
192 draw_channel_strips(&ac, saction, ar);
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);
201 UI_view2d_view_orthoSpecial(ar, v2d, 1);
203 flag = (ac.markers && (ac.markers != &ac.scene->markers)) ? DRAW_MARKERS_LOCAL : 0;
204 draw_markers_time(C, flag);
207 UI_view2d_view_ortho(v2d);
208 ANIM_draw_previewrange(C, v2d);
211 UI_view2d_view_ortho(v2d);
212 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
214 /* reset view matrix */
215 UI_view2d_view_restore(C);
218 scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
219 UI_view2d_scrollers_draw(C, v2d, scrollers);
220 UI_view2d_scrollers_free(scrollers);
223 /* add handlers, stuff you only do once or on area/region changes */
224 static void action_channel_area_init(wmWindowManager *wm, ARegion *ar)
228 /* ensure the 2d view sync works - main region has bottom scroller */
229 ar->v2d.scroll = V2D_SCROLL_BOTTOM;
231 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
234 keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
235 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
238 static void action_channel_area_draw(const bContext *C, ARegion *ar)
240 /* draw entirely, view changes should be handled here */
242 View2D *v2d = &ar->v2d;
244 /* clear and setup matrix */
245 UI_ThemeClearColor(TH_BACK);
246 glClear(GL_COLOR_BUFFER_BIT);
248 UI_view2d_view_ortho(v2d);
251 if (ANIM_animdata_get_context(C, &ac)) {
252 draw_channel_names((bContext *)C, &ac, ar);
255 /* reset view matrix */
256 UI_view2d_view_restore(C);
258 /* no scrollers here */
262 /* add handlers, stuff you only do once or on area/region changes */
263 static void action_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
265 ED_region_header_init(ar);
268 static void action_header_area_draw(const bContext *C, ARegion *ar)
270 ED_region_header(C, ar);
273 static void action_channel_area_listener(ARegion *ar, wmNotifier *wmn)
275 /* context changes */
276 switch (wmn->category) {
278 ED_region_tag_redraw(ar);
284 ED_region_tag_redraw(ar);
293 ED_region_tag_redraw(ar);
296 if (wmn->action == NA_RENAME)
297 ED_region_tag_redraw(ar);
302 if (wmn->action == NA_RENAME)
303 ED_region_tag_redraw(ar);
306 if (wmn->data == ND_KEYS)
307 ED_region_tag_redraw(ar);
311 static void action_main_area_listener(ARegion *ar, wmNotifier *wmn)
313 /* context changes */
314 switch (wmn->category) {
316 ED_region_tag_redraw(ar);
320 case ND_RENDER_OPTIONS:
324 ED_region_tag_redraw(ar);
331 /* moving object shouldn't need to redraw action */
336 ED_region_tag_redraw(ar);
341 switch (wmn->action) {
343 ED_region_tag_redraw(ar);
348 if (wmn->action == NA_RENAME)
349 ED_region_tag_redraw(ar);
353 if (wmn->data == ND_KEYS)
354 ED_region_tag_redraw(ar);
358 /* editor level listener */
359 static void action_listener(ScrArea *sa, wmNotifier *wmn)
361 SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
363 /* context changes */
364 switch (wmn->category) {
366 if (wmn->action == NA_EDITED) {
367 /* only handle this event in GPencil mode for performance considerations */
368 if (saction->mode == SACTCONT_GPENCIL)
369 ED_area_tag_redraw(sa);
373 /* for NLA tweakmode enter/exit, need complete refresh */
374 if (wmn->data == ND_NLA_ACTCHANGE) {
375 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
376 ED_area_tag_refresh(sa);
378 /* for selection changes of animation data, we can just redraw... otherwise autocolor might need to be done again */
379 else if (ELEM(wmn->data, ND_KEYFRAME, ND_ANIMCHAN) && (wmn->action == NA_SELECTED))
380 ED_area_tag_redraw(sa);
382 ED_area_tag_refresh(sa);
386 case ND_OB_ACTIVE: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
388 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
389 ED_area_tag_refresh(sa);
392 default: /* just redrawing the view will do */
393 ED_area_tag_redraw(sa);
399 case ND_BONE_SELECT: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
401 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
402 ED_area_tag_refresh(sa);
405 /* moving object shouldn't need to redraw action */
407 default: /* just redrawing the view will do */
408 ED_area_tag_redraw(sa);
413 if (saction->mode == SACTCONT_MASK) {
416 ED_area_tag_refresh(sa);
417 ED_area_tag_redraw(sa);
419 default: /* just redrawing the view will do */
420 ED_area_tag_redraw(sa);
426 if (wmn->action == NA_SELECTED) {
427 /* selection changed, so force refresh to flush (needs flag set to do syncing) */
428 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
429 ED_area_tag_refresh(sa);
434 case ND_SPACE_DOPESHEET:
435 ED_area_tag_redraw(sa);
437 case ND_SPACE_CHANGED:
438 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
439 ED_area_tag_refresh(sa);
444 if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
445 /* force redraw/refresh after undo/redo - [#28962] */
446 ED_area_tag_refresh(sa);
452 static void action_header_area_listener(ARegion *ar, wmNotifier *wmn)
454 /* context changes */
455 switch (wmn->category) {
459 ED_region_tag_redraw(ar);
464 if (wmn->action == NA_RENAME)
465 ED_region_tag_redraw(ar);
470 static void action_refresh(const bContext *C, ScrArea *sa)
472 SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
474 /* update the state of the animchannels in response to changes from the data they represent
475 * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
477 if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
480 /* Perform syncing of channel state incl. selection
481 * Active action setting also occurs here (as part of anim channel filtering in anim_filter.c)
483 ANIM_sync_animchannels_to_data(C);
484 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
486 /* Tag everything for redraw
487 * - Regions (such as header) need to be manually tagged for redraw too
488 * or else they don't update [#28962]
490 ED_area_tag_redraw(sa);
491 for (ar = sa->regionbase.first; ar; ar = ar->next)
492 ED_region_tag_redraw(ar);
495 /* region updates? */
496 // XXX re-sizing y-extents of tot should go here?
499 /* only called once, from space/spacetypes.c */
500 void ED_spacetype_action(void)
502 SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype action");
505 st->spaceid = SPACE_ACTION;
506 strncpy(st->name, "Action", BKE_ST_MAXNAME);
508 st->new = action_new;
509 st->free = action_free;
510 st->init = action_init;
511 st->duplicate = action_duplicate;
512 st->operatortypes = action_operatortypes;
513 st->keymap = action_keymap;
514 st->listener = action_listener;
515 st->refresh = action_refresh;
517 /* regions: main window */
518 art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
519 art->regionid = RGN_TYPE_WINDOW;
520 art->init = action_main_area_init;
521 art->draw = action_main_area_draw;
522 art->listener = action_main_area_listener;
523 art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
525 BLI_addhead(&st->regiontypes, art);
527 /* regions: header */
528 art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
529 art->regionid = RGN_TYPE_HEADER;
530 art->prefsizey = HEADERY;
531 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
533 art->init = action_header_area_init;
534 art->draw = action_header_area_draw;
535 art->listener = action_header_area_listener;
537 BLI_addhead(&st->regiontypes, art);
539 /* regions: channels */
540 art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
541 art->regionid = RGN_TYPE_CHANNELS;
542 art->prefsizex = 200;
543 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
545 art->init = action_channel_area_init;
546 art->draw = action_channel_area_draw;
547 art->listener = action_channel_area_listener;
549 BLI_addhead(&st->regiontypes, art);
552 BKE_spacetype_register(st);