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_graph/graph_select.c
37 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
41 #include "BLI_utildefines.h"
43 #include "DNA_anim_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_space_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_keyframes_edit.h"
60 #include "ED_markers.h"
65 #include "graph_intern.h"
68 /* ************************************************************************** */
71 /* ******************** Deselect All Operator ***************************** */
72 /* This operator works in one of three ways:
73 * 1) (de)select all (AKEY) - test if select all or deselect all
74 * 2) invert all (CTRL-IKEY) - invert selection of all keyframes
75 * 3) (de)select all - no testing is done; only for use internal tools as normal function...
78 /* Deselects keyframes in the Graph Editor
79 * - This is called by the deselect all operator, as well as other ones!
81 * - test: check if select or deselect all
82 * - sel: how to select keyframes
87 static void deselect_graph_keys (bAnimContext *ac, short test, short sel)
89 ListBase anim_data = {NULL, NULL};
93 SpaceIpo *sipo= (SpaceIpo *)ac->sa->spacedata.first;
94 KeyframeEditData ked= {{NULL}};
95 KeyframeEditFunc test_cb, sel_cb;
97 /* determine type-based settings */
98 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
101 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
103 /* init BezTriple looping data */
104 test_cb= ANIM_editkeyframes_ok(BEZT_OK_SELECTED);
106 /* See if we should be selecting or deselecting */
108 for (ale= anim_data.first; ale; ale= ale->next) {
109 if (ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, test_cb, NULL)) {
110 sel= SELECT_SUBTRACT;
116 /* convert sel to selectmode, and use that to get editor */
117 sel_cb= ANIM_editkeyframes_select(sel);
119 /* Now set the flags */
120 for (ale= anim_data.first; ale; ale= ale->next) {
121 FCurve *fcu= (FCurve *)ale->key_data;
123 /* Keyframes First */
124 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, sel_cb, NULL);
126 /* only change selection of channel when the visibility of keyframes doesn't depend on this */
127 if ((sipo->flag & SIPO_SELCUVERTSONLY) == 0) {
128 /* deactivate the F-Curve, and deselect if deselecting keyframes.
129 * otherwise select the F-Curve too since we've selected all the keyframes
131 if (sel == SELECT_SUBTRACT)
132 fcu->flag &= ~FCURVE_SELECTED;
134 fcu->flag |= FCURVE_SELECTED;
137 /* always deactivate all F-Curves if we perform batch ops for selection */
138 fcu->flag &= ~FCURVE_ACTIVE;
142 BLI_freelistN(&anim_data);
145 /* ------------------- */
147 static int graphkeys_deselectall_exec(bContext *C, wmOperator *op)
151 /* get editor data */
152 if (ANIM_animdata_get_context(C, &ac) == 0)
153 return OPERATOR_CANCELLED;
155 /* 'standard' behaviour - check if selected, then apply relevant selection */
156 if (RNA_boolean_get(op->ptr, "invert"))
157 deselect_graph_keys(&ac, 0, SELECT_INVERT);
159 deselect_graph_keys(&ac, 1, SELECT_ADD);
161 /* set notifier that things have changed */
162 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
164 return OPERATOR_FINISHED;
167 void GRAPH_OT_select_all_toggle (wmOperatorType *ot)
170 ot->name= "Select All";
171 ot->idname= "GRAPH_OT_select_all_toggle";
172 ot->description= "Toggle selection of all keyframes";
175 ot->exec= graphkeys_deselectall_exec;
176 ot->poll= graphop_visible_keyframes_poll;
179 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
182 ot->prop= RNA_def_boolean(ot->srna, "invert", 0, "Invert", "");
185 /* ******************** Border Select Operator **************************** */
186 /* This operator currently works in one of three ways:
187 * -> BKEY - 1) all keyframes within region are selected (validation with BEZT_OK_REGION)
188 * -> ALT-BKEY - depending on which axis of the region was larger...
189 * -> 2) x-axis, so select all frames within frame range (validation with BEZT_OK_FRAMERANGE)
190 * -> 3) y-axis, so select all frames within channels that region included (validation with BEZT_OK_VALUERANGE)
193 /* Borderselect only selects keyframes now, as overshooting handles often get caught too,
194 * which means that they may be inadvertantly moved as well. However, incl_handles overrides
195 * this, and allow handles to be considered independently too.
196 * Also, for convenience, handles should get same status as keyframe (if it was within bounds).
198 static void borderselect_graphkeys (bAnimContext *ac, rcti rect, short mode, short selectmode, short incl_handles)
200 ListBase anim_data = {NULL, NULL};
204 SpaceIpo *sipo= (SpaceIpo *)ac->sa->spacedata.first;
205 KeyframeEditData ked;
206 KeyframeEditFunc ok_cb, select_cb;
207 View2D *v2d= &ac->ar->v2d;
210 /* convert mouse coordinates to frame ranges and channel coordinates corrected for view pan/zoom */
211 UI_view2d_region_to_view(v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
212 UI_view2d_region_to_view(v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
215 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_NODUPLIS);
216 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
218 /* get beztriple editing/validation funcs */
219 select_cb= ANIM_editkeyframes_select(selectmode);
220 ok_cb= ANIM_editkeyframes_ok(mode);
222 /* init editing data */
223 memset(&ked, 0, sizeof(KeyframeEditData));
226 /* treat handles separately? */
228 ked.iterflags |= KEYFRAME_ITER_INCL_HANDLES;
230 /* loop over data, doing border select */
231 for (ale= anim_data.first; ale; ale= ale->next) {
232 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
233 FCurve *fcu= (FCurve *)ale->key_data;
235 /* apply unit corrections */
236 ANIM_unit_mapping_apply_fcurve(ac->scene, ale->id, ale->key_data, ANIM_UNITCONV_ONLYKEYS);
238 /* apply NLA mapping to all the keyframes, since it's easier than trying to
239 * guess when a callback might use something different
242 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, incl_handles==0);
244 /* set horizontal range (if applicable)
245 * NOTE: these values are only used for x-range and y-range but not region
246 * (which uses ked.data, i.e. rectf)
248 if (mode != BEZT_OK_VALUERANGE) {
257 /* firstly, check if any keyframes will be hit by this */
258 if (ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, ok_cb, NULL)) {
259 /* select keyframes that are in the appropriate places */
260 ANIM_fcurve_keyframes_loop(&ked, fcu, ok_cb, select_cb, NULL);
262 /* only change selection of channel when the visibility of keyframes doesn't depend on this */
263 if ((sipo->flag & SIPO_SELCUVERTSONLY) == 0) {
264 /* select the curve too now that curve will be touched */
265 if (selectmode == SELECT_ADD)
266 fcu->flag |= FCURVE_SELECTED;
270 /* un-apply NLA mapping from all the keyframes */
272 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, incl_handles==0);
274 /* unapply unit corrections */
275 ANIM_unit_mapping_apply_fcurve(ac->scene, ale->id, ale->key_data, ANIM_UNITCONV_RESTORE|ANIM_UNITCONV_ONLYKEYS);
279 BLI_freelistN(&anim_data);
282 /* ------------------- */
284 static int graphkeys_borderselect_exec(bContext *C, wmOperator *op)
288 short mode=0, selectmode=0;
291 /* get editor data */
292 if (ANIM_animdata_get_context(C, &ac) == 0)
293 return OPERATOR_CANCELLED;
296 * - 'gesture_mode' from the operator specifies how to select
297 * - 'include_handles' from the operator specifies whether to include handles in the selection
299 if (RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_SELECT)
300 selectmode= SELECT_ADD;
302 selectmode= SELECT_SUBTRACT;
304 incl_handles = RNA_boolean_get(op->ptr, "include_handles");
306 /* get settings from operator */
307 rect.xmin= RNA_int_get(op->ptr, "xmin");
308 rect.ymin= RNA_int_get(op->ptr, "ymin");
309 rect.xmax= RNA_int_get(op->ptr, "xmax");
310 rect.ymax= RNA_int_get(op->ptr, "ymax");
312 /* selection 'mode' depends on whether borderselect region only matters on one axis */
313 if (RNA_boolean_get(op->ptr, "axis_range")) {
314 /* mode depends on which axis of the range is larger to determine which axis to use
315 * - checking this in region-space is fine, as it's fundamentally still going to be a different rect size
316 * - the frame-range select option is favoured over the channel one (x over y), as frame-range one is often
317 * used for tweaking timing when "blocking", while channels is not that useful...
319 if ((rect.xmax - rect.xmin) >= (rect.ymax - rect.ymin))
320 mode= BEZT_OK_FRAMERANGE;
322 mode= BEZT_OK_VALUERANGE;
325 mode= BEZT_OK_REGION;
327 /* apply borderselect action */
328 borderselect_graphkeys(&ac, rect, mode, selectmode, incl_handles);
330 /* send notifier that keyframe selection has changed */
331 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
333 return OPERATOR_FINISHED;
336 void GRAPH_OT_select_border(wmOperatorType *ot)
339 ot->name= "Border Select";
340 ot->idname= "GRAPH_OT_select_border";
341 ot->description= "Select all keyframes within the specified region";
344 ot->invoke= WM_border_select_invoke;
345 ot->exec= graphkeys_borderselect_exec;
346 ot->modal= WM_border_select_modal;
348 ot->poll= graphop_visible_keyframes_poll;
351 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
354 WM_operator_properties_gesture_border(ot, FALSE);
356 ot->prop= RNA_def_boolean(ot->srna, "axis_range", 0, "Axis Range", "");
357 RNA_def_boolean(ot->srna, "include_handles", 0, "Include Handles", "Are handles tested individually against the selection criteria");
360 /* ******************** Column Select Operator **************************** */
361 /* This operator works in one of four ways:
362 * - 1) select all keyframes in the same frame as a selected one (KKEY)
363 * - 2) select all keyframes in the same frame as the current frame marker (CTRL-KKEY)
364 * - 3) select all keyframes in the same frame as a selected markers (SHIFT-KKEY)
365 * - 4) select all keyframes that occur between selected markers (ALT-KKEY)
368 /* defines for column-select mode */
369 static EnumPropertyItem prop_column_select_types[] = {
370 {GRAPHKEYS_COLUMNSEL_KEYS, "KEYS", 0, "On Selected Keyframes", ""},
371 {GRAPHKEYS_COLUMNSEL_CFRA, "CFRA", 0, "On Current Frame", ""},
372 {GRAPHKEYS_COLUMNSEL_MARKERS_COLUMN, "MARKERS_COLUMN", 0, "On Selected Markers", ""},
373 {GRAPHKEYS_COLUMNSEL_MARKERS_BETWEEN, "MARKERS_BETWEEN", 0, "Between Min/Max Selected Markers", ""},
374 {0, NULL, 0, NULL, NULL}
377 /* ------------------- */
379 /* Selects all visible keyframes between the specified markers */
380 static void markers_selectkeys_between (bAnimContext *ac)
382 ListBase anim_data = {NULL, NULL};
386 KeyframeEditFunc ok_cb, select_cb;
387 KeyframeEditData ked;
390 /* get extreme markers */
391 ED_markers_get_minmax(ac->markers, 1, &min, &max);
395 /* get editing funcs + data */
396 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
397 select_cb= ANIM_editkeyframes_select(SELECT_ADD);
399 memset(&ked, 0, sizeof(KeyframeEditData));
404 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
405 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
407 /* select keys in-between */
408 for (ale= anim_data.first; ale; ale= ale->next) {
409 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
412 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
413 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
414 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
417 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
422 BLI_freelistN(&anim_data);
426 /* Selects all visible keyframes in the same frames as the specified elements */
427 static void columnselect_graph_keys (bAnimContext *ac, short mode)
429 ListBase anim_data= {NULL, NULL};
433 Scene *scene= ac->scene;
435 KeyframeEditFunc select_cb, ok_cb;
436 KeyframeEditData ked;
438 /* initialise keyframe editing data */
439 memset(&ked, 0, sizeof(KeyframeEditData));
441 /* build list of columns */
443 case GRAPHKEYS_COLUMNSEL_KEYS: /* list of selected keys */
444 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
445 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
447 for (ale= anim_data.first; ale; ale= ale->next)
448 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, bezt_to_cfraelem, NULL);
450 BLI_freelistN(&anim_data);
453 case GRAPHKEYS_COLUMNSEL_CFRA: /* current frame */
454 /* make a single CfraElem for storing this */
455 ce= MEM_callocN(sizeof(CfraElem), "cfraElem");
456 BLI_addtail(&ked.list, ce);
458 ce->cfra= (float)CFRA;
461 case GRAPHKEYS_COLUMNSEL_MARKERS_COLUMN: /* list of selected markers */
462 ED_markers_make_cfra_list(ac->markers, &ked.list, SELECT);
465 default: /* invalid option */
469 /* set up BezTriple edit callbacks */
470 select_cb= ANIM_editkeyframes_select(SELECT_ADD);
471 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
473 /* loop through all of the keys and select additional keyframes
474 * based on the keys found to be selected above
476 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
477 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
479 for (ale= anim_data.first; ale; ale= ale->next) {
480 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
482 /* loop over cfraelems (stored in the KeyframeEditData->list)
483 * - we need to do this here, as we can apply fewer NLA-mapping conversions
485 for (ce= ked.list.first; ce; ce= ce->next) {
486 /* set frame for validation callback to refer to */
487 ked.f1= BKE_nla_tweakedit_remap(adt, ce->cfra, NLATIME_CONVERT_UNMAP);
489 /* select elements with frame number matching cfraelem */
490 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
495 BLI_freelistN(&ked.list);
496 BLI_freelistN(&anim_data);
499 /* ------------------- */
501 static int graphkeys_columnselect_exec(bContext *C, wmOperator *op)
506 /* get editor data */
507 if (ANIM_animdata_get_context(C, &ac) == 0)
508 return OPERATOR_CANCELLED;
510 /* action to take depends on the mode */
511 mode= RNA_enum_get(op->ptr, "mode");
513 if (mode == GRAPHKEYS_COLUMNSEL_MARKERS_BETWEEN)
514 markers_selectkeys_between(&ac);
516 columnselect_graph_keys(&ac, mode);
518 /* set notifier that keyframe selection has changed */
519 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
521 return OPERATOR_FINISHED;
524 void GRAPH_OT_select_column (wmOperatorType *ot)
527 ot->name= "Select All";
528 ot->idname= "GRAPH_OT_select_column";
529 ot->description= "Select all keyframes on the specified frame(s)";
532 ot->exec= graphkeys_columnselect_exec;
533 ot->poll= graphop_visible_keyframes_poll;
536 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
539 ot->prop= RNA_def_enum(ot->srna, "mode", prop_column_select_types, 0, "Mode", "");
542 /* ******************** Select Linked Operator *********************** */
544 static int graphkeys_select_linked_exec (bContext *C, wmOperator *UNUSED(op))
548 ListBase anim_data= {NULL, NULL};
552 KeyframeEditFunc ok_cb = ANIM_editkeyframes_ok(BEZT_OK_SELECTED);
553 KeyframeEditFunc sel_cb = ANIM_editkeyframes_select(SELECT_ADD);
555 /* get editor data */
556 if (ANIM_animdata_get_context(C, &ac) == 0)
557 return OPERATOR_CANCELLED;
559 /* loop through all of the keys and select additional keyframes based on these */
560 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
561 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
563 for (ale= anim_data.first; ale; ale= ale->next) {
564 FCurve *fcu= (FCurve *)ale->key_data;
566 /* check if anything selected? */
567 if (ANIM_fcurve_keyframes_loop(NULL, fcu, NULL, ok_cb, NULL)) {
568 /* select every keyframe in this curve then */
569 ANIM_fcurve_keyframes_loop(NULL, fcu, NULL, sel_cb, NULL);
574 BLI_freelistN(&anim_data);
576 /* set notifier that keyframe selection has changed */
577 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
579 return OPERATOR_FINISHED;
582 void GRAPH_OT_select_linked (wmOperatorType *ot)
585 ot->name = "Select Linked";
586 ot->idname= "GRAPH_OT_select_linked";
587 ot->description = "Select keyframes occurring the same F-Curves as selected ones";
590 ot->exec= graphkeys_select_linked_exec;
591 ot->poll= graphop_visible_keyframes_poll;
594 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
597 /* ******************** Select More/Less Operators *********************** */
599 /* Common code to perform selection */
600 static void select_moreless_graph_keys (bAnimContext *ac, short mode)
602 ListBase anim_data= {NULL, NULL};
606 KeyframeEditData ked;
607 KeyframeEditFunc build_cb;
610 /* init selmap building data */
611 build_cb= ANIM_editkeyframes_buildselmap(mode);
612 memset(&ked, 0, sizeof(KeyframeEditData));
614 /* loop through all of the keys and select additional keyframes based on these */
615 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
616 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
618 for (ale= anim_data.first; ale; ale= ale->next) {
619 FCurve *fcu= (FCurve *)ale->key_data;
621 /* only continue if F-Curve has keyframes */
622 if (fcu->bezt == NULL)
625 /* build up map of whether F-Curve's keyframes should be selected or not */
626 ked.data= MEM_callocN(fcu->totvert, "selmap graphEdit");
627 ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, build_cb, NULL);
629 /* based on this map, adjust the selection status of the keyframes */
630 ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, bezt_selmap_flush, NULL);
632 /* free the selmap used here */
638 BLI_freelistN(&anim_data);
641 /* ----------------- */
643 static int graphkeys_select_more_exec (bContext *C, wmOperator *UNUSED(op))
647 /* get editor data */
648 if (ANIM_animdata_get_context(C, &ac) == 0)
649 return OPERATOR_CANCELLED;
651 /* perform select changes */
652 select_moreless_graph_keys(&ac, SELMAP_MORE);
654 /* set notifier that keyframe selection has changed */
655 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
657 return OPERATOR_FINISHED;
660 void GRAPH_OT_select_more (wmOperatorType *ot)
663 ot->name = "Select More";
664 ot->idname= "GRAPH_OT_select_more";
665 ot->description = "Select keyframes beside already selected ones";
668 ot->exec= graphkeys_select_more_exec;
669 ot->poll= graphop_visible_keyframes_poll;
672 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
675 /* ----------------- */
677 static int graphkeys_select_less_exec (bContext *C, wmOperator *UNUSED(op))
681 /* get editor data */
682 if (ANIM_animdata_get_context(C, &ac) == 0)
683 return OPERATOR_CANCELLED;
685 /* perform select changes */
686 select_moreless_graph_keys(&ac, SELMAP_LESS);
688 /* set notifier that keyframe selection has changed */
689 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
691 return OPERATOR_FINISHED;
694 void GRAPH_OT_select_less (wmOperatorType *ot)
697 ot->name = "Select Less";
698 ot->idname= "GRAPH_OT_select_less";
699 ot->description = "Deselect keyframes on ends of selection islands";
702 ot->exec= graphkeys_select_less_exec;
703 ot->poll= graphop_visible_keyframes_poll;
706 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
709 /* ******************** Select Left/Right Operator ************************* */
710 /* Select keyframes left/right of the current frame indicator */
712 /* defines for left-right select tool */
713 static EnumPropertyItem prop_graphkeys_leftright_select_types[] = {
714 {GRAPHKEYS_LRSEL_TEST, "CHECK", 0, "Check if Select Left or Right", ""},
715 {GRAPHKEYS_LRSEL_LEFT, "LEFT", 0, "Before current frame", ""},
716 {GRAPHKEYS_LRSEL_RIGHT, "RIGHT", 0, "After current frame", ""},
717 {0, NULL, 0, NULL, NULL}
720 /* --------------------------------- */
722 static void graphkeys_select_leftright (bAnimContext *ac, short leftright, short select_mode)
724 ListBase anim_data = {NULL, NULL};
728 KeyframeEditFunc ok_cb, select_cb;
729 KeyframeEditData ked= {{NULL}};
730 Scene *scene= ac->scene;
732 /* if select mode is replace, deselect all keyframes (and channels) first */
733 if (select_mode==SELECT_REPLACE) {
734 select_mode= SELECT_ADD;
736 /* deselect all other channels and keyframes */
737 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
738 deselect_graph_keys(ac, 0, SELECT_SUBTRACT);
741 /* set callbacks and editing data */
742 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
743 select_cb= ANIM_editkeyframes_select(select_mode);
745 if (leftright == GRAPHKEYS_LRSEL_LEFT) {
747 ked.f2 = (float)(CFRA + 0.1f);
750 ked.f1 = (float)(CFRA - 0.1f);
755 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
756 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
759 for (ale= anim_data.first; ale; ale= ale->next) {
760 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
763 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
764 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
765 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
768 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
772 BLI_freelistN(&anim_data);
775 /* ----------------- */
777 static int graphkeys_select_leftright_exec (bContext *C, wmOperator *op)
780 short leftright = RNA_enum_get(op->ptr, "mode");
783 /* get editor data */
784 if (ANIM_animdata_get_context(C, &ac) == 0)
785 return OPERATOR_CANCELLED;
787 /* select mode is either replace (deselect all, then add) or add/extend */
788 if (RNA_boolean_get(op->ptr, "extend"))
789 selectmode= SELECT_INVERT;
791 selectmode= SELECT_REPLACE;
793 /* if "test" mode is set, we don't have any info to set this with */
794 if (leftright == GRAPHKEYS_LRSEL_TEST)
795 return OPERATOR_CANCELLED;
797 /* do the selecting now */
798 graphkeys_select_leftright(&ac, leftright, selectmode);
800 /* set notifier that keyframe selection (and channels too) have changed */
801 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|ND_ANIMCHAN|NA_SELECTED, NULL);
803 return OPERATOR_FINISHED;
806 static int graphkeys_select_leftright_invoke (bContext *C, wmOperator *op, wmEvent *event)
809 short leftright = RNA_enum_get(op->ptr, "mode");
811 /* get editor data */
812 if (ANIM_animdata_get_context(C, &ac) == 0)
813 return OPERATOR_CANCELLED;
815 /* handle mode-based testing */
816 if (leftright == GRAPHKEYS_LRSEL_TEST) {
817 Scene *scene= ac.scene;
819 View2D *v2d= &ar->v2d;
822 /* determine which side of the current frame mouse is on */
823 UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, NULL);
825 RNA_enum_set(op->ptr, "mode", GRAPHKEYS_LRSEL_LEFT);
827 RNA_enum_set(op->ptr, "mode", GRAPHKEYS_LRSEL_RIGHT);
830 /* perform selection */
831 return graphkeys_select_leftright_exec(C, op);
834 void GRAPH_OT_select_leftright (wmOperatorType *ot)
837 ot->name= "Select Left/Right";
838 ot->idname= "GRAPH_OT_select_leftright";
839 ot->description= "Select keyframes to the left or the right of the current frame";
842 ot->invoke= graphkeys_select_leftright_invoke;
843 ot->exec= graphkeys_select_leftright_exec;
844 ot->poll= graphop_visible_keyframes_poll;
847 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
850 ot->prop= RNA_def_enum(ot->srna, "mode", prop_graphkeys_leftright_select_types, GRAPHKEYS_LRSEL_TEST, "Mode", "");
851 RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
854 /* ******************** Mouse-Click Select Operator *********************** */
855 /* This operator works in one of three ways:
856 * - 1) keyframe under mouse - no special modifiers
857 * - 2) all keyframes on the same side of current frame indicator as mouse - ALT modifier
858 * - 3) column select all keyframes in frame under mouse - CTRL modifier
860 * In addition to these basic options, the SHIFT modifier can be used to toggle the
861 * selection mode between replacing the selection (without) and inverting the selection (with).
864 /* temp info for caching handle vertices close */
865 typedef struct tNearestVertInfo {
866 struct tNearestVertInfo *next, *prev;
868 FCurve *fcu; /* F-Curve that keyframe comes from */
870 BezTriple *bezt; /* keyframe to consider */
871 FPoint *fpt; /* sample point to consider */
873 short hpoint; /* the handle index that we hit (eHandleIndex) */
874 short sel; /* whether the handle is selected or not */
875 int dist; /* distance from mouse to vert */
878 /* Tags for the type of graph vert that we have */
879 typedef enum eGraphVertIndex {
880 NEAREST_HANDLE_LEFT = -1,
885 /* Tolerance for absolute radius (in pixels) of the vert from the cursor to use */
886 // TODO: perhaps this should depend a bit on the size that the user set the vertices to be?
887 #define GVERTSEL_TOL 10
891 /* check if its ok to select a handle */
892 // XXX also need to check for int-values only?
893 static int fcurve_handle_sel_check(SpaceIpo *sipo, BezTriple *bezt)
895 if (sipo->flag & SIPO_NOHANDLES) return 0;
896 if ((sipo->flag & SIPO_SELVHANDLESONLY) && BEZSELECTED(bezt)==0) return 0;
900 /* check if the given vertex is within bounds or not */
901 // TODO: should we return if we hit something?
902 static void nearest_fcurve_vert_store (ListBase *matches, View2D *v2d, FCurve *fcu, BezTriple *bezt, FPoint *fpt, short hpoint, const int mval[2])
904 /* Keyframes or Samples? */
906 int screen_co[2], dist;
908 /* convert from data-space to screen coordinates
909 * NOTE: hpoint+1 gives us 0,1,2 respectively for each handle,
910 * needed to access the relevant vertex coordinates in the 3x3
913 UI_view2d_view_to_region(v2d, bezt->vec[hpoint+1][0], bezt->vec[hpoint+1][1], &screen_co[0], &screen_co[1]);
915 /* check if distance from mouse cursor to vert in screen space is within tolerance */
916 // XXX: inlined distance calculation, since we cannot do this on ints using the math lib...
917 //dist = len_v2v2(mval, screen_co);
918 dist = sqrt((mval[0] - screen_co[0])*(mval[0] - screen_co[0]) +
919 (mval[1] - screen_co[1])*(mval[1] - screen_co[1]));
921 if (dist <= GVERTSEL_TOL) {
922 tNearestVertInfo *nvi = (tNearestVertInfo *)matches->last;
925 /* if there is already a point for the F-Curve, check if this point is closer than that was */
926 if ((nvi) && (nvi->fcu == fcu)) {
927 /* replace if we are closer, or if equal and that one wasn't selected but we are... */
928 if ( (nvi->dist > dist) || ((nvi->sel == 0) && BEZSELECTED(bezt)) )
931 /* add new if not replacing... */
933 nvi = MEM_callocN(sizeof(tNearestVertInfo), "Nearest Graph Vert Info - Bezt");
938 nvi->hpoint = hpoint;
941 nvi->sel= BEZSELECTED(bezt); // XXX... should this use the individual verts instead?
943 /* add to list of matches if appropriate... */
945 BLI_addtail(matches, nvi);
953 /* helper for find_nearest_fcurve_vert() - build the list of nearest matches */
954 static void get_nearest_fcurve_verts_list (bAnimContext *ac, const int mval[2], ListBase *matches)
956 ListBase anim_data = {NULL, NULL};
960 SpaceIpo *sipo= (SpaceIpo *)ac->sa->spacedata.first;
961 View2D *v2d= &ac->ar->v2d;
963 /* get curves to search through
964 * - if the option to only show keyframes that belong to selected F-Curves is enabled,
965 * include the 'only selected' flag...
967 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
968 if (sipo->flag & SIPO_SELCUVERTSONLY) // FIXME: this should really be check for by the filtering code...
969 filter |= ANIMFILTER_SEL;
970 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
972 for (ale= anim_data.first; ale; ale= ale->next) {
973 FCurve *fcu= (FCurve *)ale->key_data;
974 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
976 /* apply unit corrections */
977 ANIM_unit_mapping_apply_fcurve(ac->scene, ale->id, ale->key_data, 0);
979 /* apply NLA mapping to all the keyframes */
981 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 0);
984 BezTriple *bezt1=fcu->bezt, *prevbezt=NULL;
987 for (i=0; i < fcu->totvert; i++, prevbezt=bezt1, bezt1++) {
989 nearest_fcurve_vert_store(matches, v2d, fcu, bezt1, NULL, NEAREST_HANDLE_KEY, mval);
991 /* handles - only do them if they're visible */
992 if (fcurve_handle_sel_check(sipo, bezt1) && (fcu->totvert > 1)) {
993 /* first handle only visible if previous segment had handles */
994 if ( (!prevbezt && (bezt1->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) )
996 nearest_fcurve_vert_store(matches, v2d, fcu, bezt1, NULL, NEAREST_HANDLE_LEFT, mval);
999 /* second handle only visible if this segment is bezier */
1000 if (bezt1->ipo == BEZT_IPO_BEZ)
1002 nearest_fcurve_vert_store(matches, v2d, fcu, bezt1, NULL, NEAREST_HANDLE_RIGHT, mval);
1007 else if (fcu->fpt) {
1008 // TODO; do this for samples too
1012 /* un-apply NLA mapping from all the keyframes */
1014 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 0);
1016 /* unapply unit corrections */
1017 ANIM_unit_mapping_apply_fcurve(ac->scene, ale->id, ale->key_data, ANIM_UNITCONV_RESTORE);
1021 BLI_freelistN(&anim_data);
1024 /* helper for find_nearest_fcurve_vert() - get the best match to use */
1025 static tNearestVertInfo *get_best_nearest_fcurve_vert (ListBase *matches)
1027 tNearestVertInfo *nvi = NULL;
1030 /* abort if list is empty */
1031 if (matches->first == NULL)
1034 /* if list only has 1 item, remove it from the list and return */
1035 if (matches->first == matches->last) {
1036 /* need to remove from the list, otherwise it gets freed and then we can't return it */
1037 nvi= matches->first;
1038 BLI_remlink(matches, nvi);
1043 /* try to find the first selected F-Curve vert, then take the one after it */
1044 for (nvi = matches->first; nvi; nvi = nvi->next) {
1045 /* which mode of search are we in: find first selected, or find vert? */
1047 /* just take this vert now that we've found the selected one
1048 * - we'll need to remove this from the list so that it can be returned to the original caller
1050 BLI_remlink(matches, nvi);
1054 /* if vert is selected, we've got what we want... */
1060 /* if we're still here, this means that we failed to find anything appropriate in the first pass,
1061 * so just take the first item now...
1063 nvi = matches->first;
1064 BLI_remlink(matches, nvi);
1068 /* Find the nearest vertices (either a handle or the keyframe) that are nearest to the mouse cursor (in area coordinates)
1069 * NOTE: the match info found must still be freed
1071 static tNearestVertInfo *find_nearest_fcurve_vert (bAnimContext *ac, const int mval[2])
1073 ListBase matches = {NULL, NULL};
1074 tNearestVertInfo *nvi;
1076 /* step 1: get the nearest verts */
1077 get_nearest_fcurve_verts_list(ac, mval, &matches);
1079 /* step 2: find the best vert */
1080 nvi= get_best_nearest_fcurve_vert(&matches);
1082 BLI_freelistN(&matches);
1084 /* return the best vert found */
1088 /* ------------------- */
1090 /* option 1) select keyframe directly under mouse */
1091 static void mouse_graph_keys (bAnimContext *ac, const int mval[2], short select_mode, short curves_only)
1093 SpaceIpo *sipo= (SpaceIpo *)ac->sa->spacedata.first;
1094 tNearestVertInfo *nvi;
1095 BezTriple *bezt= NULL;
1097 /* find the beztriple that we're selecting, and the handle that was clicked on */
1098 nvi = find_nearest_fcurve_vert(ac, mval);
1100 /* check if anything to select */
1104 /* deselect all other curves? */
1105 if (select_mode == SELECT_REPLACE) {
1106 /* reset selection mode */
1107 select_mode= SELECT_ADD;
1109 /* deselect all other keyframes */
1110 deselect_graph_keys(ac, 0, SELECT_SUBTRACT);
1112 /* deselect other channels too, but only only do this if
1113 * selection of channel when the visibility of keyframes
1114 * doesn't depend on this
1116 if ((sipo->flag & SIPO_SELCUVERTSONLY) == 0)
1117 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1120 /* if points can be selected on this F-Curve */
1121 // TODO: what about those with no keyframes?
1122 if ((curves_only == 0) && ((nvi->fcu->flag & FCURVE_PROTECTED)==0)) {
1123 /* only if there's keyframe */
1125 bezt= nvi->bezt; /* used to check bezt seletion is set */
1126 /* depends on selection mode */
1127 if (select_mode == SELECT_INVERT) {
1128 /* keyframe - invert select of all */
1129 if (nvi->hpoint == NEAREST_HANDLE_KEY) {
1130 if (BEZSELECTED(bezt)) {
1138 /* handles - toggle selection of relevant handle */
1139 else if (nvi->hpoint == NEAREST_HANDLE_LEFT) {
1140 /* toggle selection */
1144 /* toggle selection */
1149 /* if the keyframe was clicked on, select all verts of given beztriple */
1150 if (nvi->hpoint == NEAREST_HANDLE_KEY) {
1153 /* otherwise, select the handle that applied */
1154 else if (nvi->hpoint == NEAREST_HANDLE_LEFT)
1160 else if (nvi->fpt) {
1161 // TODO: need to handle sample points
1165 KeyframeEditFunc select_cb;
1166 KeyframeEditData ked;
1168 /* initialise keyframe editing data */
1169 memset(&ked, 0, sizeof(KeyframeEditData));
1171 /* set up BezTriple edit callbacks */
1172 select_cb= ANIM_editkeyframes_select(select_mode);
1174 /* select all keyframes */
1175 ANIM_fcurve_keyframes_loop(&ked, nvi->fcu, NULL, select_cb, NULL);
1178 /* only change selection of channel when the visibility of keyframes doesn't depend on this */
1179 if ((sipo->flag & SIPO_SELCUVERTSONLY) == 0) {
1180 /* select or deselect curve? */
1182 /* take selection status from item that got hit, to prevent flip/flop on channel
1183 * selection status when shift-selecting (i.e. "SELECT_INVERT") points
1185 if (BEZSELECTED(bezt))
1186 nvi->fcu->flag |= FCURVE_SELECTED;
1188 nvi->fcu->flag &= ~FCURVE_SELECTED;
1191 /* didn't hit any channel, so just apply that selection mode to the curve's selection status */
1192 if (select_mode == SELECT_INVERT)
1193 nvi->fcu->flag ^= FCURVE_SELECTED;
1194 else if (select_mode == SELECT_ADD)
1195 nvi->fcu->flag |= FCURVE_SELECTED;
1199 /* set active F-Curve (NOTE: sync the filter flags with findnearest_fcurve_vert) */
1200 /* needs to be called with (sipo->flag & SIPO_SELCUVERTSONLY) otherwise the active flag won't be set [#26452] */
1201 if (nvi->fcu->flag & FCURVE_SELECTED) {
1202 int filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
1203 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, nvi->fcu, ANIMTYPE_FCURVE);
1206 /* free temp sample data for filtering */
1210 /* Option 2) Selects all the keyframes on either side of the current frame (depends on which side the mouse is on) */
1211 /* (see graphkeys_select_leftright) */
1213 /* Option 3) Selects all visible keyframes in the same frame as the mouse click */
1214 static void graphkeys_mselect_column (bAnimContext *ac, const int mval[2], short select_mode)
1216 ListBase anim_data= {NULL, NULL};
1220 SpaceIpo *sipo= (SpaceIpo *)ac->sa->spacedata.first;
1221 KeyframeEditFunc select_cb, ok_cb;
1222 KeyframeEditData ked;
1223 tNearestVertInfo *nvi;
1224 float selx = (float)ac->scene->r.cfra;
1226 /* find the beztriple that we're selecting, and the handle that was clicked on */
1227 nvi = find_nearest_fcurve_vert(ac, mval);
1229 /* check if anything to select */
1233 /* get frame number on which elements should be selected */
1234 // TODO: should we restrict to integer frames only?
1236 selx= nvi->bezt->vec[1][0];
1238 selx= nvi->fpt->vec[0];
1240 /* if select mode is replace, deselect all keyframes (and channels) first */
1241 if (select_mode==SELECT_REPLACE) {
1242 /* reset selection mode to add to selection */
1243 select_mode= SELECT_ADD;
1245 /* deselect all other keyframes */
1246 deselect_graph_keys(ac, 0, SELECT_SUBTRACT);
1248 /* deselect other channels too, but only only do this if
1249 * selection of channel when the visibility of keyframes
1250 * doesn't depend on this
1252 if ((sipo->flag & SIPO_SELCUVERTSONLY) == 0)
1253 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1256 /* initialise keyframe editing data */
1257 memset(&ked, 0, sizeof(KeyframeEditData));
1259 /* set up BezTriple edit callbacks */
1260 select_cb= ANIM_editkeyframes_select(select_mode);
1261 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
1263 /* loop through all of the keys and select additional keyframes
1264 * based on the keys found to be selected above
1266 filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY | ANIMFILTER_NODUPLIS);
1267 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
1269 for (ale= anim_data.first; ale; ale= ale->next) {
1270 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
1272 /* set frame for validation callback to refer to */
1274 ked.f1= BKE_nla_tweakedit_remap(adt, selx, NLATIME_CONVERT_UNMAP);
1278 /* select elements with frame number matching cfra */
1279 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
1284 BLI_freelistN(&ked.list);
1285 BLI_freelistN(&anim_data);
1288 /* ------------------- */
1290 /* handle clicking */
1291 static int graphkeys_clickselect_invoke(bContext *C, wmOperator *op, wmEvent *event)
1297 /* get editor data */
1298 if (ANIM_animdata_get_context(C, &ac) == 0)
1299 return OPERATOR_CANCELLED;
1301 /* get useful pointers from animation context data */
1304 /* select mode is either replace (deselect all, then add) or add/extend */
1305 if (RNA_boolean_get(op->ptr, "extend"))
1306 selectmode= SELECT_INVERT;
1308 selectmode= SELECT_REPLACE;
1310 /* figure out action to take */
1311 if (RNA_boolean_get(op->ptr, "column")) {
1312 /* select all keyframes in the same frame as the one that was under the mouse */
1313 graphkeys_mselect_column(&ac, event->mval, selectmode);
1315 else if (RNA_boolean_get(op->ptr, "curves")) {
1316 /* select all keyframes in the same F-Curve as the one under the mouse */
1317 mouse_graph_keys(&ac, event->mval, selectmode, 1);
1320 /* select keyframe under mouse */
1321 mouse_graph_keys(&ac, event->mval, selectmode, 0);
1324 /* set notifier that keyframe selection (and also channel selection in some cases) has changed */
1325 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|ND_ANIMCHAN|NA_SELECTED, NULL);
1327 /* for tweak grab to work */
1328 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
1331 void GRAPH_OT_clickselect (wmOperatorType *ot)
1334 ot->name= "Mouse Select Keys";
1335 ot->idname= "GRAPH_OT_clickselect";
1336 ot->description= "Select keyframes by clicking on them";
1339 ot->invoke= graphkeys_clickselect_invoke;
1340 ot->poll= graphop_visible_keyframes_poll;
1343 RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
1344 RNA_def_boolean(ot->srna, "column", 0, "Column Select", "Select all keyframes that occur on the same frame as the one under the mouse"); // ALTKEY
1345 RNA_def_boolean(ot->srna, "curves", 0, "Only Curves", "Select all the keyframes in the curve"); // CTRLKEY + ALTKEY
1348 /* ************************************************************************** */