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_group_types.h"
37 #include "DNA_scene_types.h"
39 #include "MEM_guardedalloc.h"
41 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
53 #include "UI_resources.h"
54 #include "UI_view2d.h"
56 #include "ED_space_api.h"
57 #include "ED_screen.h"
58 #include "ED_anim_api.h"
59 #include "ED_markers.h"
61 #include "action_intern.h" /* own include */
63 /* ******************** manage regions ********************* */
65 ARegion *action_has_buttons_region(ScrArea *sa)
69 ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
72 /* add subdiv level; after main */
73 ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
76 if (ar == NULL) return NULL;
78 arnew = MEM_callocN(sizeof(ARegion), "buttons for action");
80 BLI_insertlinkafter(&sa->regionbase, ar, arnew);
81 arnew->regiontype = RGN_TYPE_UI;
82 arnew->alignment = RGN_ALIGN_RIGHT;
84 arnew->flag = RGN_FLAG_HIDDEN;
89 /* ******************** default callbacks for action space ***************** */
91 static SpaceLink *action_new(const bContext *C)
93 Scene *scene = CTX_data_scene(C);
94 ScrArea *sa = CTX_wm_area(C);
98 saction = MEM_callocN(sizeof(SpaceAction), "initaction");
99 saction->spacetype = SPACE_ACTION;
101 saction->autosnap = SACTSNAP_FRAME;
102 saction->mode = SACTCONT_DOPESHEET;
104 saction->ads.filterflag |= ADS_FILTER_SUMMARY;
107 ar = MEM_callocN(sizeof(ARegion), "header for action");
109 BLI_addtail(&saction->regionbase, ar);
110 ar->regiontype = RGN_TYPE_HEADER;
111 ar->alignment = RGN_ALIGN_BOTTOM;
113 /* channel list region */
114 ar = MEM_callocN(sizeof(ARegion), "channel region for action");
115 BLI_addtail(&saction->regionbase, ar);
116 ar->regiontype = RGN_TYPE_CHANNELS;
117 ar->alignment = RGN_ALIGN_LEFT;
119 /* only need to set scroll settings, as this will use 'listview' v2d configuration */
120 ar->v2d.scroll = V2D_SCROLL_BOTTOM;
121 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
124 ar = MEM_callocN(sizeof(ARegion), "buttons region for action");
126 BLI_addtail(&saction->regionbase, ar);
127 ar->regiontype = RGN_TYPE_UI;
128 ar->alignment = RGN_ALIGN_RIGHT;
129 ar->flag = RGN_FLAG_HIDDEN;
132 ar = MEM_callocN(sizeof(ARegion), "main region for action");
134 BLI_addtail(&saction->regionbase, ar);
135 ar->regiontype = RGN_TYPE_WINDOW;
137 ar->v2d.tot.xmin = (float)(SFRA - 10);
138 ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
139 ar->v2d.tot.xmax = (float)(EFRA + 10);
140 ar->v2d.tot.ymax = 0.0f;
142 ar->v2d.cur = ar->v2d.tot;
144 ar->v2d.min[0] = 0.0f;
145 ar->v2d.min[1] = 0.0f;
147 ar->v2d.max[0] = MAXFRAMEF;
148 ar->v2d.max[1] = FLT_MAX;
150 ar->v2d.minzoom = 0.01f;
151 ar->v2d.maxzoom = 50;
152 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
153 ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
154 ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
155 ar->v2d.keepofs = V2D_KEEPOFS_Y;
156 ar->v2d.align = V2D_ALIGN_NO_POS_Y;
157 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
159 return (SpaceLink *)saction;
162 /* not spacelink itself */
163 static void action_free(SpaceLink *UNUSED(sl))
165 // SpaceAction *saction = (SpaceAction *) sl;
169 /* spacetype; init callback */
170 static void action_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
172 SpaceAction *saction = sa->spacedata.first;
173 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
176 static SpaceLink *action_duplicate(SpaceLink *sl)
178 SpaceAction *sactionn = MEM_dupallocN(sl);
180 /* clear or remove stuff from old */
182 return (SpaceLink *)sactionn;
187 /* add handlers, stuff you only do once or on area/region changes */
188 static void action_main_region_init(wmWindowManager *wm, ARegion *ar)
192 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
195 keymap = WM_keymap_find(wm->defaultconf, "Dopesheet", SPACE_ACTION, 0);
196 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
197 keymap = WM_keymap_find(wm->defaultconf, "Dopesheet Generic", SPACE_ACTION, 0);
198 WM_event_add_keymap_handler(&ar->handlers, keymap);
201 static void action_main_region_draw(const bContext *C, ARegion *ar)
203 /* draw entirely, view changes should be handled here */
204 SpaceAction *saction = CTX_wm_space_action(C);
206 View2D *v2d = &ar->v2d;
208 View2DScrollers *scrollers;
209 short unit = 0, flag = 0;
211 /* clear and setup matrix */
212 UI_ThemeClearColor(TH_BACK);
213 glClear(GL_COLOR_BUFFER_BIT);
215 UI_view2d_view_ortho(v2d);
218 unit = (saction->flag & SACTION_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
219 grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
220 UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
221 UI_view2d_grid_free(grid);
223 ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
226 if (ANIM_animdata_get_context(C, &ac)) {
227 draw_channel_strips(&ac, saction, ar);
231 if (saction->flag & SACTION_DRAWTIME) flag |= DRAWCFRA_UNIT_SECONDS;
232 if ((saction->flag & SACTION_NODRAWCFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
233 ANIM_draw_cfra(C, v2d, flag);
236 UI_view2d_view_orthoSpecial(ar, v2d, 1);
238 flag = ((ac.markers && (ac.markers != &ac.scene->markers)) ? DRAW_MARKERS_LOCAL : 0) | DRAW_MARKERS_MARGIN;
239 ED_markers_draw(C, flag);
242 UI_view2d_view_ortho(v2d);
243 ANIM_draw_previewrange(C, v2d, 0);
246 UI_view2d_view_ortho(v2d);
247 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
249 /* reset view matrix */
250 UI_view2d_view_restore(C);
253 scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
254 UI_view2d_scrollers_draw(C, v2d, scrollers);
255 UI_view2d_scrollers_free(scrollers);
258 /* add handlers, stuff you only do once or on area/region changes */
259 static void action_channel_region_init(wmWindowManager *wm, ARegion *ar)
263 /* ensure the 2d view sync works - main region has bottom scroller */
264 ar->v2d.scroll = V2D_SCROLL_BOTTOM;
266 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
269 keymap = WM_keymap_find(wm->defaultconf, "Animation Channels", 0, 0);
270 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
272 keymap = WM_keymap_find(wm->defaultconf, "Dopesheet Generic", SPACE_ACTION, 0);
273 WM_event_add_keymap_handler(&ar->handlers, keymap);
276 static void action_channel_region_draw(const bContext *C, ARegion *ar)
278 /* draw entirely, view changes should be handled here */
280 View2D *v2d = &ar->v2d;
282 /* clear and setup matrix */
283 UI_ThemeClearColor(TH_BACK);
284 glClear(GL_COLOR_BUFFER_BIT);
286 UI_view2d_view_ortho(v2d);
289 if (ANIM_animdata_get_context(C, &ac)) {
290 draw_channel_names((bContext *)C, &ac, ar);
293 /* reset view matrix */
294 UI_view2d_view_restore(C);
296 /* no scrollers here */
300 /* add handlers, stuff you only do once or on area/region changes */
301 static void action_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
303 ED_region_header_init(ar);
306 static void action_header_region_draw(const bContext *C, ARegion *ar)
308 ED_region_header(C, ar);
311 static void action_channel_region_listener(
312 bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
313 wmNotifier *wmn, const Scene *UNUSED(scene))
315 /* context changes */
316 switch (wmn->category) {
318 ED_region_tag_redraw(ar);
324 ED_region_tag_redraw(ar);
333 ED_region_tag_redraw(ar);
336 if (wmn->action == NA_RENAME)
337 ED_region_tag_redraw(ar);
342 if (ELEM(wmn->action, NA_RENAME, NA_SELECTED))
343 ED_region_tag_redraw(ar);
346 if (wmn->action == NA_RENAME)
347 ED_region_tag_redraw(ar);
350 if (wmn->data == ND_KEYS)
351 ED_region_tag_redraw(ar);
356 static void action_main_region_listener(
357 bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
358 wmNotifier *wmn, const Scene *UNUSED(scene))
360 /* context changes */
361 switch (wmn->category) {
363 ED_region_tag_redraw(ar);
367 case ND_RENDER_OPTIONS:
371 ED_region_tag_redraw(ar);
378 /* moving object shouldn't need to redraw action */
383 ED_region_tag_redraw(ar);
388 switch (wmn->action) {
390 ED_region_tag_redraw(ar);
395 if (wmn->action == NA_RENAME)
396 ED_region_tag_redraw(ar);
399 if (ELEM(wmn->data, ND_LAYER)) {
400 ED_region_tag_redraw(ar);
404 if (wmn->data == ND_KEYS)
405 ED_region_tag_redraw(ar);
410 /* editor level listener */
411 static void action_listener(
412 bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn, Scene *UNUSED(scene),
413 WorkSpace *UNUSED(workspace))
415 SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
417 /* context changes */
418 switch (wmn->category) {
420 /* only handle these events in GPencil mode for performance considerations */
421 if (saction->mode == SACTCONT_GPENCIL) {
422 if (wmn->action == NA_EDITED) {
423 ED_area_tag_redraw(sa);
425 else if (wmn->action == NA_SELECTED) {
426 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
427 ED_area_tag_refresh(sa);
432 /* for NLA tweakmode enter/exit, need complete refresh */
433 if (wmn->data == ND_NLA_ACTCHANGE) {
434 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
435 ED_area_tag_refresh(sa);
437 /* autocolor only really needs to change when channels are added/removed, or previously hidden stuff appears
438 * (assume for now that if just adding these works, that will be fine)
440 else if (((wmn->data == ND_KEYFRAME) && ELEM(wmn->action, NA_ADDED, NA_REMOVED)) ||
441 ((wmn->data == ND_ANIMCHAN) && (wmn->action != NA_SELECTED)))
443 ED_area_tag_refresh(sa);
445 /* for simple edits to the curve data though (or just plain selections), a simple redraw should work
446 * (see T39851 for an example of how this can go wrong)
449 ED_area_tag_redraw(sa);
454 case ND_OB_ACTIVE: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
456 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
457 ED_area_tag_refresh(sa);
460 default: /* just redrawing the view will do */
461 ED_area_tag_redraw(sa);
467 case ND_BONE_SELECT: /* selection changed, so force refresh to flush (needs flag set to do syncing) */
469 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
470 ED_area_tag_refresh(sa);
473 /* moving object shouldn't need to redraw action */
475 default: /* just redrawing the view will do */
476 ED_area_tag_redraw(sa);
481 if (saction->mode == SACTCONT_MASK) {
484 ED_area_tag_refresh(sa);
485 ED_area_tag_redraw(sa);
487 default: /* just redrawing the view will do */
488 ED_area_tag_redraw(sa);
494 if (wmn->action == NA_SELECTED) {
495 /* selection changed, so force refresh to flush (needs flag set to do syncing) */
496 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
497 ED_area_tag_refresh(sa);
502 case ND_SPACE_DOPESHEET:
503 ED_area_tag_redraw(sa);
505 case ND_SPACE_CHANGED:
506 saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
507 ED_area_tag_refresh(sa);
512 if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
513 /* force redraw/refresh after undo/redo - [#28962] */
514 ED_area_tag_refresh(sa);
520 static void action_header_region_listener(
521 bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
522 wmNotifier *wmn, const Scene *UNUSED(scene))
524 // SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
526 /* context changes */
527 switch (wmn->category) {
531 ED_region_tag_redraw(ar);
536 if (wmn->action == NA_RENAME)
537 ED_region_tag_redraw(ar);
541 case ND_ANIMCHAN: /* set of visible animchannels changed */
542 /* NOTE: for now, this should usually just mean that the filters changed
543 * It may be better if we had a dedicated flag for that though
545 ED_region_tag_redraw(ar);
548 case ND_KEYFRAME: /* new keyframed added -> active action may have changed */
549 //saction->flag |= SACTION_TEMP_NEEDCHANSYNC;
550 ED_region_tag_redraw(ar);
558 /* add handlers, stuff you only do once or on area/region changes */
559 static void action_buttons_area_init(wmWindowManager *wm, ARegion *ar)
563 ED_region_panels_init(wm, ar);
565 keymap = WM_keymap_find(wm->defaultconf, "Dopesheet Generic", SPACE_ACTION, 0);
566 WM_event_add_keymap_handler(&ar->handlers, keymap);
569 static void action_buttons_area_draw(const bContext *C, ARegion *ar)
571 ED_region_panels(C, ar, NULL, -1, true);
574 static void action_region_listener(
575 bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar,
576 wmNotifier *wmn, const Scene *UNUSED(scene))
578 /* context changes */
579 switch (wmn->category) {
581 ED_region_tag_redraw(ar);
588 ED_region_tag_redraw(ar);
597 ED_region_tag_redraw(ar);
602 if (wmn->data == ND_KEYS)
603 ED_region_tag_redraw(ar);
608 static void action_refresh(const bContext *C, ScrArea *sa)
610 SpaceAction *saction = (SpaceAction *)sa->spacedata.first;
612 /* update the state of the animchannels in response to changes from the data they represent
613 * NOTE: the temp flag is used to indicate when this needs to be done, and will be cleared once handled
615 if (saction->flag & SACTION_TEMP_NEEDCHANSYNC) {
618 /* Perform syncing of channel state incl. selection
619 * Active action setting also occurs here (as part of anim channel filtering in anim_filter.c)
621 ANIM_sync_animchannels_to_data(C);
622 saction->flag &= ~SACTION_TEMP_NEEDCHANSYNC;
624 /* Tag everything for redraw
625 * - Regions (such as header) need to be manually tagged for redraw too
626 * or else they don't update [#28962]
628 ED_area_tag_redraw(sa);
629 for (ar = sa->regionbase.first; ar; ar = ar->next)
630 ED_region_tag_redraw(ar);
633 /* region updates? */
634 // XXX re-sizing y-extents of tot should go here?
637 static void action_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
639 SpaceAction *sact = (SpaceAction *)slink;
641 if ((ID *)sact->action == old_id) {
642 sact->action = (bAction *)new_id;
645 if ((ID *)sact->ads.filter_grp == old_id) {
646 sact->ads.filter_grp = (Group *)new_id;
648 if ((ID *)sact->ads.source == old_id) {
649 sact->ads.source = new_id;
654 /* only called once, from space/spacetypes.c */
655 void ED_spacetype_action(void)
657 SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype action");
660 st->spaceid = SPACE_ACTION;
661 strncpy(st->name, "Action", BKE_ST_MAXNAME);
663 st->new = action_new;
664 st->free = action_free;
665 st->init = action_init;
666 st->duplicate = action_duplicate;
667 st->operatortypes = action_operatortypes;
668 st->keymap = action_keymap;
669 st->listener = action_listener;
670 st->refresh = action_refresh;
671 st->id_remap = action_id_remap;
673 /* regions: main window */
674 art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
675 art->regionid = RGN_TYPE_WINDOW;
676 art->init = action_main_region_init;
677 art->draw = action_main_region_draw;
678 art->listener = action_main_region_listener;
679 art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_ANIMATION | ED_KEYMAP_FRAMES;
681 BLI_addhead(&st->regiontypes, art);
683 /* regions: header */
684 art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
685 art->regionid = RGN_TYPE_HEADER;
686 art->prefsizey = HEADERY;
687 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
689 art->init = action_header_region_init;
690 art->draw = action_header_region_draw;
691 art->listener = action_header_region_listener;
693 BLI_addhead(&st->regiontypes, art);
695 /* regions: channels */
696 art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
697 art->regionid = RGN_TYPE_CHANNELS;
698 art->prefsizex = 200;
699 art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES;
701 art->init = action_channel_region_init;
702 art->draw = action_channel_region_draw;
703 art->listener = action_channel_region_listener;
705 BLI_addhead(&st->regiontypes, art);
707 /* regions: UI buttons */
708 art = MEM_callocN(sizeof(ARegionType), "spacetype action region");
709 art->regionid = RGN_TYPE_UI;
710 art->prefsizex = 200;
711 art->keymapflag = ED_KEYMAP_UI;
712 art->listener = action_region_listener;
713 art->init = action_buttons_area_init;
714 art->draw = action_buttons_area_draw;
716 BLI_addhead(&st->regiontypes, art);
718 action_buttons_register(art);
720 BKE_spacetype_register(st);