4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation
22 * Contributor(s): Joshua Leung
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_action/action_select.c
37 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
41 #include "BLI_dlrbTree.h"
42 #include "BLI_utildefines.h"
44 #include "DNA_anim_types.h"
45 #include "DNA_gpencil_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
49 #include "RNA_access.h"
50 #include "RNA_define.h"
52 #include "BKE_fcurve.h"
54 #include "BKE_context.h"
56 #include "UI_view2d.h"
58 #include "ED_anim_api.h"
59 #include "ED_gpencil.h"
60 #include "ED_keyframes_draw.h"
61 #include "ED_keyframes_edit.h"
62 #include "ED_markers.h"
63 #include "ED_screen.h"
68 #include "action_intern.h"
71 /* ************************************************************************** */
74 /* ******************** Deselect All Operator ***************************** */
75 /* This operator works in one of three ways:
76 * 1) (de)select all (AKEY) - test if select all or deselect all
77 * 2) invert all (CTRL-IKEY) - invert selection of all keyframes
78 * 3) (de)select all - no testing is done; only for use internal tools as normal function...
81 /* Deselects keyframes in the action editor
82 * - This is called by the deselect all operator, as well as other ones!
84 * - test: check if select or deselect all
85 * - sel: how to select keyframes (SELECT_*)
87 static void deselect_action_keys (bAnimContext *ac, short test, short sel)
89 ListBase anim_data = {NULL, NULL};
93 KeyframeEditData ked= {{NULL}};
94 KeyframeEditFunc test_cb, sel_cb;
96 /* determine type-based settings */
97 if (ac->datatype == ANIMCONT_GPENCIL)
98 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_NODUPLIS);
100 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
103 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
105 /* init BezTriple looping data */
106 test_cb= ANIM_editkeyframes_ok(BEZT_OK_SELECTED);
108 /* See if we should be selecting or deselecting */
110 for (ale= anim_data.first; ale; ale= ale->next) {
111 if (ale->type == ANIMTYPE_GPLAYER) {
112 if (is_gplayer_frame_selected(ale->data)) {
113 sel= SELECT_SUBTRACT;
118 if (ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, test_cb, NULL)) {
119 sel= SELECT_SUBTRACT;
126 /* convert sel to selectmode, and use that to get editor */
127 sel_cb= ANIM_editkeyframes_select(sel);
129 /* Now set the flags */
130 for (ale= anim_data.first; ale; ale= ale->next) {
131 if (ale->type == ANIMTYPE_GPLAYER)
132 set_gplayer_frame_selection(ale->data, sel);
134 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, sel_cb, NULL);
138 BLI_freelistN(&anim_data);
141 /* ------------------- */
143 static int actkeys_deselectall_exec(bContext *C, wmOperator *op)
147 /* get editor data */
148 if (ANIM_animdata_get_context(C, &ac) == 0)
149 return OPERATOR_CANCELLED;
151 /* 'standard' behaviour - check if selected, then apply relevant selection */
152 if (RNA_boolean_get(op->ptr, "invert"))
153 deselect_action_keys(&ac, 0, SELECT_INVERT);
155 deselect_action_keys(&ac, 1, SELECT_ADD);
157 /* set notifier that keyframe selection have changed */
158 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
160 return OPERATOR_FINISHED;
163 void ACTION_OT_select_all_toggle (wmOperatorType *ot)
166 ot->name= "Select All";
167 ot->idname= "ACTION_OT_select_all_toggle";
168 ot->description= "Toggle selection of all keyframes";
171 ot->exec= actkeys_deselectall_exec;
172 ot->poll= ED_operator_action_active;
175 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
178 ot->prop= RNA_def_boolean(ot->srna, "invert", 0, "Invert", "");
181 /* ******************** Border Select Operator **************************** */
182 /* This operator currently works in one of three ways:
183 * -> BKEY - 1) all keyframes within region are selected (ACTKEYS_BORDERSEL_ALLKEYS)
184 * -> ALT-BKEY - depending on which axis of the region was larger...
185 * -> 2) x-axis, so select all frames within frame range (ACTKEYS_BORDERSEL_FRAMERANGE)
186 * -> 3) y-axis, so select all frames within channels that region included (ACTKEYS_BORDERSEL_CHANNELS)
189 /* defines for borderselect mode */
191 ACTKEYS_BORDERSEL_ALLKEYS = 0,
192 ACTKEYS_BORDERSEL_FRAMERANGE,
193 ACTKEYS_BORDERSEL_CHANNELS,
194 } /*eActKeys_BorderSelect_Mode*/;
197 static void borderselect_action (bAnimContext *ac, rcti rect, short mode, short selectmode)
199 ListBase anim_data = {NULL, NULL};
201 int filter, filterflag;
203 KeyframeEditData ked;
204 KeyframeEditFunc ok_cb, select_cb;
205 View2D *v2d= &ac->ar->v2d;
207 float ymin=0, ymax=(float)(-ACHANNEL_HEIGHT_HALF);
209 /* convert mouse coordinates to frame ranges and channel coordinates corrected for view pan/zoom */
210 UI_view2d_region_to_view(v2d, rect.xmin, rect.ymin+2, &rectf.xmin, &rectf.ymin);
211 UI_view2d_region_to_view(v2d, rect.xmax, rect.ymax-2, &rectf.xmax, &rectf.ymax);
214 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CHANNELS | ANIMFILTER_NODUPLIS);
215 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
217 /* get filtering flag for dopesheet data (if applicable) */
218 if (ac->datatype == ANIMCONT_DOPESHEET) {
219 bDopeSheet *ads= (bDopeSheet *)ac->data;
220 filterflag= ads->filterflag;
225 /* get beztriple editing/validation funcs */
226 select_cb= ANIM_editkeyframes_select(selectmode);
228 if (ELEM(mode, ACTKEYS_BORDERSEL_FRAMERANGE, ACTKEYS_BORDERSEL_ALLKEYS))
229 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
233 /* init editing data */
234 memset(&ked, 0, sizeof(KeyframeEditData));
236 /* loop over data, doing border select */
237 for (ale= anim_data.first; ale; ale= ale->next) {
238 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
240 /* get new vertical minimum extent of channel */
241 ymin= ymax - ACHANNEL_STEP;
243 /* set horizontal range (if applicable) */
244 if (ELEM(mode, ACTKEYS_BORDERSEL_FRAMERANGE, ACTKEYS_BORDERSEL_ALLKEYS)) {
245 /* if channel is mapped in NLA, apply correction */
247 ked.f1= BKE_nla_tweakedit_remap(adt, rectf.xmin, NLATIME_CONVERT_UNMAP);
248 ked.f2= BKE_nla_tweakedit_remap(adt, rectf.xmax, NLATIME_CONVERT_UNMAP);
256 /* perform vertical suitability check (if applicable) */
257 if ( (mode == ACTKEYS_BORDERSEL_FRAMERANGE) ||
258 !((ymax < rectf.ymin) || (ymin > rectf.ymax)) )
260 /* loop over data selecting */
261 if (ale->type == ANIMTYPE_GPLAYER)
262 borderselect_gplayer_frames(ale->data, rectf.xmin, rectf.xmax, selectmode);
264 ANIM_animchannel_keyframes_loop(&ked, ale, ok_cb, select_cb, NULL, filterflag);
267 /* set minimum extent to be the maximum of the next channel */
272 BLI_freelistN(&anim_data);
275 /* ------------------- */
277 static int actkeys_borderselect_exec(bContext *C, wmOperator *op)
281 short mode=0, selectmode=0;
284 /* get editor data */
285 if (ANIM_animdata_get_context(C, &ac) == 0)
286 return OPERATOR_CANCELLED;
288 /* get settings from operator */
289 rect.xmin= RNA_int_get(op->ptr, "xmin");
290 rect.ymin= RNA_int_get(op->ptr, "ymin");
291 rect.xmax= RNA_int_get(op->ptr, "xmax");
292 rect.ymax= RNA_int_get(op->ptr, "ymax");
294 gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
295 if (gesture_mode == GESTURE_MODAL_SELECT)
296 selectmode = SELECT_ADD;
298 selectmode = SELECT_SUBTRACT;
300 /* selection 'mode' depends on whether borderselect region only matters on one axis */
301 if (RNA_boolean_get(op->ptr, "axis_range")) {
302 /* mode depends on which axis of the range is larger to determine which axis to use
303 * - checking this in region-space is fine, as it's fundamentally still going to be a different rect size
304 * - the frame-range select option is favoured over the channel one (x over y), as frame-range one is often
305 * used for tweaking timing when "blocking", while channels is not that useful...
307 if ((rect.xmax - rect.xmin) >= (rect.ymax - rect.ymin))
308 mode= ACTKEYS_BORDERSEL_FRAMERANGE;
310 mode= ACTKEYS_BORDERSEL_CHANNELS;
313 mode= ACTKEYS_BORDERSEL_ALLKEYS;
315 /* apply borderselect action */
316 borderselect_action(&ac, rect, mode, selectmode);
318 /* set notifier that keyframe selection have changed */
319 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
321 return OPERATOR_FINISHED;
324 void ACTION_OT_select_border(wmOperatorType *ot)
327 ot->name= "Border Select";
328 ot->idname= "ACTION_OT_select_border";
329 ot->description= "Select all keyframes within the specified region";
332 ot->invoke= WM_border_select_invoke;
333 ot->exec= actkeys_borderselect_exec;
334 ot->modal= WM_border_select_modal;
336 ot->poll= ED_operator_action_active;
339 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
342 WM_operator_properties_gesture_border(ot, FALSE);
344 ot->prop= RNA_def_boolean(ot->srna, "axis_range", 0, "Axis Range", "");
347 /* ******************** Column Select Operator **************************** */
348 /* This operator works in one of four ways:
349 * - 1) select all keyframes in the same frame as a selected one (KKEY)
350 * - 2) select all keyframes in the same frame as the current frame marker (CTRL-KKEY)
351 * - 3) select all keyframes in the same frame as a selected markers (SHIFT-KKEY)
352 * - 4) select all keyframes that occur between selected markers (ALT-KKEY)
355 /* defines for column-select mode */
356 static EnumPropertyItem prop_column_select_types[] = {
357 {ACTKEYS_COLUMNSEL_KEYS, "KEYS", 0, "On Selected Keyframes", ""},
358 {ACTKEYS_COLUMNSEL_CFRA, "CFRA", 0, "On Current Frame", ""},
359 {ACTKEYS_COLUMNSEL_MARKERS_COLUMN, "MARKERS_COLUMN", 0, "On Selected Markers", ""},
360 {ACTKEYS_COLUMNSEL_MARKERS_BETWEEN, "MARKERS_BETWEEN", 0, "Between Min/Max Selected Markers", ""},
361 {0, NULL, 0, NULL, NULL}
364 /* ------------------- */
366 /* Selects all visible keyframes between the specified markers */
367 static void markers_selectkeys_between (bAnimContext *ac)
369 ListBase anim_data = {NULL, NULL};
373 KeyframeEditFunc ok_cb, select_cb;
374 KeyframeEditData ked= {{NULL}};
377 /* get extreme markers */
378 ED_markers_get_minmax(ac->markers, 1, &min, &max);
382 /* get editing funcs + data */
383 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
384 select_cb= ANIM_editkeyframes_select(SELECT_ADD);
390 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
391 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
393 /* select keys in-between */
394 for (ale= anim_data.first; ale; ale= ale->next) {
395 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
398 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
399 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
400 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
402 else if (ale->type == ANIMTYPE_GPLAYER) {
403 borderselect_gplayer_frames(ale->data, min, max, SELECT_ADD);
406 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
411 BLI_freelistN(&anim_data);
415 /* Selects all visible keyframes in the same frames as the specified elements */
416 static void columnselect_action_keys (bAnimContext *ac, short mode)
418 ListBase anim_data= {NULL, NULL};
422 Scene *scene= ac->scene;
424 KeyframeEditFunc select_cb, ok_cb;
425 KeyframeEditData ked= {{NULL}};
427 /* initialise keyframe editing data */
429 /* build list of columns */
431 case ACTKEYS_COLUMNSEL_KEYS: /* list of selected keys */
432 if (ac->datatype == ANIMCONT_GPENCIL) {
433 filter= (ANIMFILTER_VISIBLE);
434 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
436 for (ale= anim_data.first; ale; ale= ale->next)
437 gplayer_make_cfra_list(ale->data, &ked.list, 1);
440 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY);
441 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
443 for (ale= anim_data.first; ale; ale= ale->next)
444 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, bezt_to_cfraelem, NULL);
446 BLI_freelistN(&anim_data);
449 case ACTKEYS_COLUMNSEL_CFRA: /* current frame */
450 /* make a single CfraElem for storing this */
451 ce= MEM_callocN(sizeof(CfraElem), "cfraElem");
452 BLI_addtail(&ked.list, ce);
454 ce->cfra= (float)CFRA;
457 case ACTKEYS_COLUMNSEL_MARKERS_COLUMN: /* list of selected markers */
458 ED_markers_make_cfra_list(ac->markers, &ked.list, SELECT);
461 default: /* invalid option */
465 /* set up BezTriple edit callbacks */
466 select_cb= ANIM_editkeyframes_select(SELECT_ADD);
467 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
469 /* loop through all of the keys and select additional keyframes
470 * based on the keys found to be selected above
472 if (ac->datatype == ANIMCONT_GPENCIL)
473 filter= (ANIMFILTER_VISIBLE);
475 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY);
476 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
478 for (ale= anim_data.first; ale; ale= ale->next) {
479 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
481 /* loop over cfraelems (stored in the KeyframeEditData->list)
482 * - we need to do this here, as we can apply fewer NLA-mapping conversions
484 for (ce= ked.list.first; ce; ce= ce->next) {
485 /* set frame for validation callback to refer to */
487 ked.f1= BKE_nla_tweakedit_remap(adt, ce->cfra, NLATIME_CONVERT_UNMAP);
491 /* select elements with frame number matching cfraelem */
492 if (ale->type == ANIMTYPE_GPLAYER)
493 select_gpencil_frame(ale->data, ce->cfra, SELECT_ADD);
495 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
500 BLI_freelistN(&ked.list);
501 BLI_freelistN(&anim_data);
504 /* ------------------- */
506 static int actkeys_columnselect_exec(bContext *C, wmOperator *op)
511 /* get editor data */
512 if (ANIM_animdata_get_context(C, &ac) == 0)
513 return OPERATOR_CANCELLED;
515 /* action to take depends on the mode */
516 mode= RNA_enum_get(op->ptr, "mode");
518 if (mode == ACTKEYS_COLUMNSEL_MARKERS_BETWEEN)
519 markers_selectkeys_between(&ac);
521 columnselect_action_keys(&ac, mode);
523 /* set notifier that keyframe selection have changed */
524 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
526 return OPERATOR_FINISHED;
529 void ACTION_OT_select_column (wmOperatorType *ot)
532 ot->name= "Select All";
533 ot->idname= "ACTION_OT_select_column";
534 ot->description= "Select all keyframes on the specified frame(s)";
537 ot->exec= actkeys_columnselect_exec;
538 ot->poll= ED_operator_action_active;
541 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
544 ot->prop= RNA_def_enum(ot->srna, "mode", prop_column_select_types, 0, "Mode", "");
547 /* ******************** Select Linked Operator *********************** */
549 static int actkeys_select_linked_exec (bContext *C, wmOperator *UNUSED(op))
553 ListBase anim_data= {NULL, NULL};
557 KeyframeEditFunc ok_cb = ANIM_editkeyframes_ok(BEZT_OK_SELECTED);
558 KeyframeEditFunc sel_cb = ANIM_editkeyframes_select(SELECT_ADD);
560 /* get editor data */
561 if (ANIM_animdata_get_context(C, &ac) == 0)
562 return OPERATOR_CANCELLED;
564 /* loop through all of the keys and select additional keyframes based on these */
565 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
566 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
568 for (ale= anim_data.first; ale; ale= ale->next) {
569 FCurve *fcu= (FCurve *)ale->key_data;
571 /* check if anything selected? */
572 if (ANIM_fcurve_keyframes_loop(NULL, fcu, NULL, ok_cb, NULL)) {
573 /* select every keyframe in this curve then */
574 ANIM_fcurve_keyframes_loop(NULL, fcu, NULL, sel_cb, NULL);
579 BLI_freelistN(&anim_data);
581 /* set notifier that keyframe selection has changed */
582 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
584 return OPERATOR_FINISHED;
587 void ACTION_OT_select_linked (wmOperatorType *ot)
590 ot->name = "Select Linked";
591 ot->idname= "ACTION_OT_select_linked";
592 ot->description = "Select keyframes occurring the same F-Curves as selected ones";
595 ot->exec= actkeys_select_linked_exec;
596 ot->poll= ED_operator_action_active;
599 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
602 /* ******************** Select More/Less Operators *********************** */
604 /* Common code to perform selection */
605 static void select_moreless_action_keys (bAnimContext *ac, short mode)
607 ListBase anim_data= {NULL, NULL};
611 KeyframeEditData ked= {{NULL}};
612 KeyframeEditFunc build_cb;
615 /* init selmap building data */
616 build_cb= ANIM_editkeyframes_buildselmap(mode);
618 /* loop through all of the keys and select additional keyframes based on these */
619 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
620 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
622 for (ale= anim_data.first; ale; ale= ale->next) {
623 FCurve *fcu= (FCurve *)ale->key_data;
625 /* only continue if F-Curve has keyframes */
626 if (fcu->bezt == NULL)
629 /* build up map of whether F-Curve's keyframes should be selected or not */
630 ked.data= MEM_callocN(fcu->totvert, "selmap actEdit more");
631 ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, build_cb, NULL);
633 /* based on this map, adjust the selection status of the keyframes */
634 ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, bezt_selmap_flush, NULL);
636 /* free the selmap used here */
642 BLI_freelistN(&anim_data);
645 /* ----------------- */
647 static int actkeys_select_more_exec (bContext *C, wmOperator *UNUSED(op))
651 /* get editor data */
652 if (ANIM_animdata_get_context(C, &ac) == 0)
653 return OPERATOR_CANCELLED;
655 /* perform select changes */
656 select_moreless_action_keys(&ac, SELMAP_MORE);
658 /* set notifier that keyframe selection has changed */
659 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
661 return OPERATOR_FINISHED;
664 void ACTION_OT_select_more (wmOperatorType *ot)
667 ot->name = "Select More";
668 ot->idname= "ACTION_OT_select_more";
669 ot->description = "Select keyframes beside already selected ones";
672 ot->exec= actkeys_select_more_exec;
673 ot->poll= ED_operator_action_active;
676 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
679 /* ----------------- */
681 static int actkeys_select_less_exec (bContext *C, wmOperator *UNUSED(op))
685 /* get editor data */
686 if (ANIM_animdata_get_context(C, &ac) == 0)
687 return OPERATOR_CANCELLED;
689 /* perform select changes */
690 select_moreless_action_keys(&ac, SELMAP_LESS);
692 /* set notifier that keyframe selection has changed */
693 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
695 return OPERATOR_FINISHED;
698 void ACTION_OT_select_less (wmOperatorType *ot)
701 ot->name = "Select Less";
702 ot->idname= "ACTION_OT_select_less";
703 ot->description = "Deselect keyframes on ends of selection islands";
706 ot->exec= actkeys_select_less_exec;
707 ot->poll= ED_operator_action_active;
710 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
713 /* ******************** Select Left/Right Operator ************************* */
714 /* Select keyframes left/right of the current frame indicator */
716 /* defines for left-right select tool */
717 static EnumPropertyItem prop_actkeys_leftright_select_types[] = {
718 {ACTKEYS_LRSEL_TEST, "CHECK", 0, "Check if Select Left or Right", ""},
719 {ACTKEYS_LRSEL_LEFT, "LEFT", 0, "Before current frame", ""},
720 {ACTKEYS_LRSEL_RIGHT, "RIGHT", 0, "After current frame", ""},
721 {0, NULL, 0, NULL, NULL}
724 /* --------------------------------- */
726 static void actkeys_select_leftright (bAnimContext *ac, short leftright, short select_mode)
728 ListBase anim_data = {NULL, NULL};
732 KeyframeEditFunc ok_cb, select_cb;
733 KeyframeEditData ked= {{NULL}};
734 Scene *scene= ac->scene;
736 /* if select mode is replace, deselect all keyframes (and channels) first */
737 if (select_mode==SELECT_REPLACE) {
738 select_mode= SELECT_ADD;
740 /* deselect all other channels and keyframes */
741 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
742 deselect_action_keys(ac, 0, SELECT_SUBTRACT);
745 /* set callbacks and editing data */
746 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
747 select_cb= ANIM_editkeyframes_select(select_mode);
749 if (leftright == ACTKEYS_LRSEL_LEFT) {
751 ked.f2 = (float)(CFRA + 0.1f);
754 ked.f1 = (float)(CFRA - 0.1f);
759 if (ac->datatype == ANIMCONT_GPENCIL)
760 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_NODUPLIS);
762 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
763 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
766 for (ale= anim_data.first; ale; ale= ale->next) {
767 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
770 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
771 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
772 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
774 else if (ale->type == ANIMTYPE_GPLAYER)
775 borderselect_gplayer_frames(ale->data, ked.f1, ked.f2, select_mode);
777 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
780 /* Sync marker support */
781 if (select_mode==SELECT_ADD) {
782 SpaceAction *saction= ac->sa->spacedata.first;
784 if ((saction) && (saction->flag & SACTION_MARKERS_MOVE)) {
785 ListBase *markers = ED_animcontext_get_markers(ac);
788 for (marker= markers->first; marker; marker= marker->next) {
789 if( ((leftright == ACTKEYS_LRSEL_LEFT) && (marker->frame < CFRA)) ||
790 ((leftright == ACTKEYS_LRSEL_RIGHT) && (marker->frame >= CFRA)) )
792 marker->flag |= SELECT;
795 marker->flag &= ~SELECT;
802 BLI_freelistN(&anim_data);
805 /* ----------------- */
807 static int actkeys_select_leftright_exec (bContext *C, wmOperator *op)
810 short leftright = RNA_enum_get(op->ptr, "mode");
813 /* get editor data */
814 if (ANIM_animdata_get_context(C, &ac) == 0)
815 return OPERATOR_CANCELLED;
817 /* select mode is either replace (deselect all, then add) or add/extend */
818 if (RNA_boolean_get(op->ptr, "extend"))
819 selectmode= SELECT_INVERT;
821 selectmode= SELECT_REPLACE;
823 /* if "test" mode is set, we don't have any info to set this with */
824 if (leftright == ACTKEYS_LRSEL_TEST)
825 return OPERATOR_CANCELLED;
827 /* do the selecting now */
828 actkeys_select_leftright(&ac, leftright, selectmode);
830 /* set notifier that keyframe selection (and channels too) have changed */
831 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|ND_ANIMCHAN|NA_SELECTED, NULL);
833 return OPERATOR_FINISHED;
836 static int actkeys_select_leftright_invoke (bContext *C, wmOperator *op, wmEvent *event)
839 short leftright = RNA_enum_get(op->ptr, "mode");
841 /* get editor data */
842 if (ANIM_animdata_get_context(C, &ac) == 0)
843 return OPERATOR_CANCELLED;
845 /* handle mode-based testing */
846 if (leftright == ACTKEYS_LRSEL_TEST) {
847 Scene *scene= ac.scene;
849 View2D *v2d= &ar->v2d;
854 /* get mouse coordinates (in region coordinates) */
855 mval[0]= (event->x - ar->winrct.xmin);
856 mval[1]= (event->y - ar->winrct.ymin);
858 /* determine which side of the current frame mouse is on */
859 UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, NULL);
861 RNA_int_set(op->ptr, "mode", ACTKEYS_LRSEL_LEFT);
863 RNA_int_set(op->ptr, "mode", ACTKEYS_LRSEL_RIGHT);
866 /* perform selection */
867 return actkeys_select_leftright_exec(C, op);
870 void ACTION_OT_select_leftright (wmOperatorType *ot)
873 ot->name= "Select Left/Right";
874 ot->idname= "ACTION_OT_select_leftright";
875 ot->description= "Select keyframes to the left or the right of the current frame";
878 ot->invoke= actkeys_select_leftright_invoke;
879 ot->exec= actkeys_select_leftright_exec;
880 ot->poll= ED_operator_action_active;
883 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
886 ot->prop= RNA_def_enum(ot->srna, "mode", prop_actkeys_leftright_select_types, ACTKEYS_LRSEL_TEST, "Mode", "");
887 RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
890 /* ******************** Mouse-Click Select Operator *********************** */
891 /* This operator works in one of three ways:
892 * - 1) keyframe under mouse - no special modifiers
893 * - 2) all keyframes on the same side of current frame indicator as mouse - ALT modifier
894 * - 3) column select all keyframes in frame under mouse - CTRL modifier
896 * In addition to these basic options, the SHIFT modifier can be used to toggle the
897 * selection mode between replacing the selection (without) and inverting the selection (with).
900 /* sensitivity factor for frame-selections */
901 #define FRAME_CLICK_THRESH 0.1f
903 /* ------------------- */
905 /* option 1) select keyframe directly under mouse */
906 static void actkeys_mselect_single (bAnimContext *ac, bAnimListElem *ale, short select_mode, float selx)
908 bDopeSheet *ads= (ac->datatype == ANIMCONT_DOPESHEET) ? ac->data : NULL;
909 int ds_filter = ((ads) ? (ads->filterflag) : (0));
911 KeyframeEditData ked= {{NULL}};
912 KeyframeEditFunc select_cb, ok_cb;
914 /* get functions for selecting keyframes */
915 select_cb= ANIM_editkeyframes_select(select_mode);
916 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
919 /* select the nominated keyframe on the given frame */
920 if (ale->type == ANIMTYPE_GPLAYER)
921 select_gpencil_frame(ale->data, selx, select_mode);
923 ANIM_animchannel_keyframes_loop(&ked, ale, ok_cb, select_cb, NULL, ds_filter);
926 /* Option 2) Selects all the keyframes on either side of the current frame (depends on which side the mouse is on) */
927 /* (see actkeys_select_leftright) */
929 /* Option 3) Selects all visible keyframes in the same frame as the mouse click */
930 static void actkeys_mselect_column(bAnimContext *ac, short select_mode, float selx)
932 ListBase anim_data= {NULL, NULL};
936 KeyframeEditFunc select_cb, ok_cb;
937 KeyframeEditData ked= {{NULL}};
939 /* initialise keyframe editing data */
941 /* set up BezTriple edit callbacks */
942 select_cb= ANIM_editkeyframes_select(select_mode);
943 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
945 /* loop through all of the keys and select additional keyframes
946 * based on the keys found to be selected above
948 if (ac->datatype == ANIMCONT_GPENCIL)
949 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_NODUPLIS);
951 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
952 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
954 for (ale= anim_data.first; ale; ale= ale->next) {
955 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
957 /* set frame for validation callback to refer to */
959 ked.f1= BKE_nla_tweakedit_remap(adt, selx, NLATIME_CONVERT_UNMAP);
963 /* select elements with frame number matching cfra */
964 if (ale->type == ANIMTYPE_GPLAYER)
965 select_gpencil_frame(ale->key_data, selx, select_mode);
967 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
971 BLI_freelistN(&ked.list);
972 BLI_freelistN(&anim_data);
975 /* ------------------- */
977 static void mouse_action_keys (bAnimContext *ac, int mval[2], short select_mode, short column)
979 ListBase anim_data = {NULL, NULL};
980 DLRBT_Tree anim_keys;
984 View2D *v2d= &ac->ar->v2d;
985 bDopeSheet *ads = NULL;
992 /* get dopesheet info */
993 if (ac->datatype == ANIMCONT_DOPESHEET)
996 /* use View2D to determine the index of the channel (i.e a row in the list) where keyframe was */
997 UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
998 UI_view2d_listview_view_to_cell(v2d, 0, ACHANNEL_STEP, 0, (float)ACHANNEL_HEIGHT_HALF, x, y, NULL, &channel_index);
1000 /* x-range to check is +/- 7 (in screen/region-space) on either side of mouse click (size of keyframe icon) */
1001 UI_view2d_region_to_view(v2d, mval[0]-7, mval[1], &rectf.xmin, &rectf.ymin);
1002 UI_view2d_region_to_view(v2d, mval[0]+7, mval[1], &rectf.xmax, &rectf.ymax);
1005 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CHANNELS);
1006 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1008 /* try to get channel */
1009 ale= BLI_findlink(&anim_data, channel_index);
1011 /* channel not found */
1012 printf("Error: animation channel (index = %d) not found in mouse_action_keys() \n", channel_index);
1013 BLI_freelistN(&anim_data);
1017 /* found match - must return here... */
1018 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
1019 ActKeyColumn *ak, *akn=NULL;
1021 /* make list of keyframes */
1022 BLI_dlrbTree_init(&anim_keys);
1024 if (ale->key_data) {
1025 switch (ale->datatype) {
1028 Scene *scene= (Scene *)ale->key_data;
1029 scene_to_keylist(ads, scene, &anim_keys, NULL);
1034 Object *ob= (Object *)ale->key_data;
1035 ob_to_keylist(ads, ob, &anim_keys, NULL);
1040 bAction *act= (bAction *)ale->key_data;
1041 action_to_keylist(adt, act, &anim_keys, NULL);
1046 FCurve *fcu= (FCurve *)ale->key_data;
1047 fcurve_to_keylist(adt, fcu, &anim_keys, NULL);
1052 else if (ale->type == ANIMTYPE_SUMMARY) {
1053 /* dopesheet summary covers everything */
1054 summary_to_keylist(ac, &anim_keys, NULL);
1056 else if (ale->type == ANIMTYPE_GROUP) {
1057 // TODO: why don't we just give groups key_data too?
1058 bActionGroup *agrp= (bActionGroup *)ale->data;
1059 agroup_to_keylist(adt, agrp, &anim_keys, NULL);
1061 else if (ale->type == ANIMTYPE_GPLAYER) {
1062 // TODO: why don't we just give gplayers key_data too?
1063 bGPDlayer *gpl = (bGPDlayer *)ale->data;
1064 gpl_to_keylist(ads, gpl, &anim_keys);
1067 /* start from keyframe at root of BST, traversing until we find one within the range that was clicked on */
1068 for (ak= anim_keys.root; ak; ak= akn) {
1069 if (IN_RANGE(ak->cfra, rectf.xmin, rectf.xmax)) {
1070 /* set the frame to use, and apply inverse-correction for NLA-mapping
1071 * so that the frame will get selected by the selection functions without
1072 * requiring to map each frame once again...
1074 selx= BKE_nla_tweakedit_remap(adt, ak->cfra, NLATIME_CONVERT_UNMAP);
1078 else if (ak->cfra < rectf.xmin)
1084 /* remove active channel from list of channels for separate treatment (since it's needed later on) */
1085 BLI_remlink(&anim_data, ale);
1087 /* cleanup temporary lists */
1088 BLI_dlrbTree_free(&anim_keys);
1090 /* free list of channels, since it's not used anymore */
1091 BLI_freelistN(&anim_data);
1094 /* for replacing selection, firstly need to clear existing selection */
1095 if (select_mode == SELECT_REPLACE) {
1096 /* reset selection mode for next steps */
1097 select_mode = SELECT_ADD;
1099 /* deselect all keyframes */
1100 deselect_action_keys(ac, 0, SELECT_SUBTRACT);
1102 /* highlight channel clicked on */
1103 if (ELEM(ac->datatype, ANIMCONT_ACTION, ANIMCONT_DOPESHEET)) {
1104 /* deselect all other channels first */
1105 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1107 /* Highlight Action-Group or F-Curve? */
1108 if (ale && ale->data) {
1109 if (ale->type == ANIMTYPE_GROUP) {
1110 bActionGroup *agrp= ale->data;
1112 agrp->flag |= AGRP_SELECTED;
1113 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
1115 else if (ale->type == ANIMTYPE_FCURVE) {
1116 FCurve *fcu= ale->data;
1118 fcu->flag |= FCURVE_SELECTED;
1119 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ANIMTYPE_FCURVE);
1123 else if (ac->datatype == ANIMCONT_GPENCIL) {
1124 /* deselect all other channels first */
1125 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1127 /* Highlight GPencil Layer */
1128 if ((ale && ale->data) && (ale->type == ANIMTYPE_GPLAYER)) {
1129 bGPDlayer *gpl = ale->data;
1131 gpl->flag |= GP_LAYER_SELECT;
1132 //gpencil_layer_setactive(gpd, gpl);
1137 /* only select keyframes if we clicked on a valid channel and hit something */
1140 /* apply selection to keyframes */
1142 /* select all keyframes in the same frame as the one we hit on the active channel */
1143 actkeys_mselect_column(ac, select_mode, selx);
1146 /* select the nominated keyframe on the given frame */
1147 actkeys_mselect_single(ac, ale, select_mode, selx);
1151 /* free this channel */
1156 /* handle clicking */
1157 static int actkeys_clickselect_invoke(bContext *C, wmOperator *op, wmEvent *event)
1161 short selectmode, column;
1164 /* get editor data */
1165 if (ANIM_animdata_get_context(C, &ac) == 0)
1166 return OPERATOR_CANCELLED;
1168 /* get useful pointers from animation context data */
1171 /* get mouse coordinates (in region coordinates) */
1172 mval[0]= (event->x - ar->winrct.xmin);
1173 mval[1]= (event->y - ar->winrct.ymin);
1175 /* select mode is either replace (deselect all, then add) or add/extend */
1176 if (RNA_boolean_get(op->ptr, "extend"))
1177 selectmode= SELECT_INVERT;
1179 selectmode= SELECT_REPLACE;
1181 /* column selection */
1182 column= RNA_boolean_get(op->ptr, "column");
1184 /* select keyframe(s) based upon mouse position*/
1185 mouse_action_keys(&ac, mval, selectmode, column);
1187 /* set notifier that keyframe selection (and channels too) have changed */
1188 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|ND_ANIMCHAN|NA_SELECTED, NULL);
1190 /* for tweak grab to work */
1191 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
1194 void ACTION_OT_clickselect (wmOperatorType *ot)
1197 ot->name= "Mouse Select Keys";
1198 ot->idname= "ACTION_OT_clickselect";
1199 ot->description= "Select keyframes by clicking on them";
1201 /* api callbacks - absolutely no exec() this yet... */
1202 ot->invoke= actkeys_clickselect_invoke;
1203 ot->poll= ED_operator_action_active;
1206 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1209 RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
1210 RNA_def_boolean(ot->srna, "column", 0, "Column Select", ""); // ALTKEY
1213 /* ************************************************************************** */