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
20 * Contributor(s): Joshua Leung
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/editors/space_action/action_select.c
35 #include "MEM_guardedalloc.h"
37 #include "BLI_blenlib.h"
39 #include "BLI_dlrbTree.h"
40 #include "BLI_utildefines.h"
42 #include "DNA_anim_types.h"
43 #include "DNA_gpencil_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
47 #include "RNA_access.h"
48 #include "RNA_define.h"
50 #include "BKE_fcurve.h"
52 #include "BKE_context.h"
54 #include "UI_view2d.h"
56 #include "ED_anim_api.h"
57 #include "ED_gpencil.h"
58 #include "ED_keyframes_draw.h"
59 #include "ED_keyframes_edit.h"
60 #include "ED_markers.h"
61 #include "ED_screen.h"
66 #include "action_intern.h"
69 /* ************************************************************************** */
72 /* ******************** Deselect All Operator ***************************** */
73 /* This operator works in one of three ways:
74 * 1) (de)select all (AKEY) - test if select all or deselect all
75 * 2) invert all (CTRL-IKEY) - invert selection of all keyframes
76 * 3) (de)select all - no testing is done; only for use internal tools as normal function...
79 /* Deselects keyframes in the action editor
80 * - This is called by the deselect all operator, as well as other ones!
82 * - test: check if select or deselect all
83 * - sel: how to select keyframes (SELECT_*)
85 static void deselect_action_keys (bAnimContext *ac, short test, short sel)
87 ListBase anim_data = {NULL, NULL};
91 KeyframeEditData ked= {{NULL}};
92 KeyframeEditFunc test_cb, sel_cb;
94 /* determine type-based settings */
95 if (ac->datatype == ANIMCONT_GPENCIL)
96 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_NODUPLIS);
98 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE /*| 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 (ale->type == ANIMTYPE_GPLAYER) {
110 if (is_gplayer_frame_selected(ale->data)) {
111 sel= SELECT_SUBTRACT;
116 if (ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, test_cb, NULL)) {
117 sel= SELECT_SUBTRACT;
124 /* convert sel to selectmode, and use that to get editor */
125 sel_cb= ANIM_editkeyframes_select(sel);
127 /* Now set the flags */
128 for (ale= anim_data.first; ale; ale= ale->next) {
129 if (ale->type == ANIMTYPE_GPLAYER)
130 set_gplayer_frame_selection(ale->data, sel);
132 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, sel_cb, NULL);
136 BLI_freelistN(&anim_data);
139 /* ------------------- */
141 static int actkeys_deselectall_exec(bContext *C, wmOperator *op)
145 /* get editor data */
146 if (ANIM_animdata_get_context(C, &ac) == 0)
147 return OPERATOR_CANCELLED;
149 /* 'standard' behaviour - check if selected, then apply relevant selection */
150 if (RNA_boolean_get(op->ptr, "invert"))
151 deselect_action_keys(&ac, 0, SELECT_INVERT);
153 deselect_action_keys(&ac, 1, SELECT_ADD);
155 /* set notifier that keyframe selection have changed */
156 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
158 return OPERATOR_FINISHED;
161 void ACTION_OT_select_all_toggle (wmOperatorType *ot)
164 ot->name= "Select All";
165 ot->idname= "ACTION_OT_select_all_toggle";
166 ot->description= "Toggle selection of all keyframes";
169 ot->exec= actkeys_deselectall_exec;
170 ot->poll= ED_operator_action_active;
173 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
176 ot->prop= RNA_def_boolean(ot->srna, "invert", 0, "Invert", "");
179 /* ******************** Border Select Operator **************************** */
180 /* This operator currently works in one of three ways:
181 * -> BKEY - 1) all keyframes within region are selected (ACTKEYS_BORDERSEL_ALLKEYS)
182 * -> ALT-BKEY - depending on which axis of the region was larger...
183 * -> 2) x-axis, so select all frames within frame range (ACTKEYS_BORDERSEL_FRAMERANGE)
184 * -> 3) y-axis, so select all frames within channels that region included (ACTKEYS_BORDERSEL_CHANNELS)
187 /* defines for borderselect mode */
189 ACTKEYS_BORDERSEL_ALLKEYS = 0,
190 ACTKEYS_BORDERSEL_FRAMERANGE,
191 ACTKEYS_BORDERSEL_CHANNELS,
192 } /*eActKeys_BorderSelect_Mode*/;
195 static void borderselect_action (bAnimContext *ac, rcti rect, short mode, short selectmode)
197 ListBase anim_data = {NULL, NULL};
201 KeyframeEditData ked;
202 KeyframeEditFunc ok_cb, select_cb;
203 View2D *v2d= &ac->ar->v2d;
205 float ymin=0, ymax=(float)(-ACHANNEL_HEIGHT_HALF);
207 /* convert mouse coordinates to frame ranges and channel coordinates corrected for view pan/zoom */
208 UI_view2d_region_to_view(v2d, rect.xmin, rect.ymin+2, &rectf.xmin, &rectf.ymin);
209 UI_view2d_region_to_view(v2d, rect.xmax, rect.ymax-2, &rectf.xmax, &rectf.ymax);
212 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS | ANIMFILTER_NODUPLIS);
213 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
215 /* get beztriple editing/validation funcs */
216 select_cb= ANIM_editkeyframes_select(selectmode);
218 if (ELEM(mode, ACTKEYS_BORDERSEL_FRAMERANGE, ACTKEYS_BORDERSEL_ALLKEYS))
219 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
223 /* init editing data */
224 memset(&ked, 0, sizeof(KeyframeEditData));
226 /* loop over data, doing border select */
227 for (ale= anim_data.first; ale; ale= ale->next) {
228 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
230 /* get new vertical minimum extent of channel */
231 ymin= ymax - ACHANNEL_STEP;
233 /* set horizontal range (if applicable) */
234 if (ELEM(mode, ACTKEYS_BORDERSEL_FRAMERANGE, ACTKEYS_BORDERSEL_ALLKEYS)) {
235 /* if channel is mapped in NLA, apply correction */
237 ked.f1= BKE_nla_tweakedit_remap(adt, rectf.xmin, NLATIME_CONVERT_UNMAP);
238 ked.f2= BKE_nla_tweakedit_remap(adt, rectf.xmax, NLATIME_CONVERT_UNMAP);
246 /* perform vertical suitability check (if applicable) */
247 if ( (mode == ACTKEYS_BORDERSEL_FRAMERANGE) ||
248 !((ymax < rectf.ymin) || (ymin > rectf.ymax)) )
250 /* loop over data selecting */
251 if (ale->type == ANIMTYPE_GPLAYER)
252 borderselect_gplayer_frames(ale->data, rectf.xmin, rectf.xmax, selectmode);
254 ANIM_animchannel_keyframes_loop(&ked, ac->ads, ale, ok_cb, select_cb, NULL);
257 /* set minimum extent to be the maximum of the next channel */
262 BLI_freelistN(&anim_data);
265 /* ------------------- */
267 static int actkeys_borderselect_exec(bContext *C, wmOperator *op)
271 short mode=0, selectmode=0;
274 /* get editor data */
275 if (ANIM_animdata_get_context(C, &ac) == 0)
276 return OPERATOR_CANCELLED;
278 /* get settings from operator */
279 rect.xmin= RNA_int_get(op->ptr, "xmin");
280 rect.ymin= RNA_int_get(op->ptr, "ymin");
281 rect.xmax= RNA_int_get(op->ptr, "xmax");
282 rect.ymax= RNA_int_get(op->ptr, "ymax");
284 gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
285 if (gesture_mode == GESTURE_MODAL_SELECT)
286 selectmode = SELECT_ADD;
288 selectmode = SELECT_SUBTRACT;
290 /* selection 'mode' depends on whether borderselect region only matters on one axis */
291 if (RNA_boolean_get(op->ptr, "axis_range")) {
292 /* mode depends on which axis of the range is larger to determine which axis to use
293 * - checking this in region-space is fine, as it's fundamentally still going to be a different rect size
294 * - the frame-range select option is favoured over the channel one (x over y), as frame-range one is often
295 * used for tweaking timing when "blocking", while channels is not that useful...
297 if ((rect.xmax - rect.xmin) >= (rect.ymax - rect.ymin))
298 mode= ACTKEYS_BORDERSEL_FRAMERANGE;
300 mode= ACTKEYS_BORDERSEL_CHANNELS;
303 mode= ACTKEYS_BORDERSEL_ALLKEYS;
305 /* apply borderselect action */
306 borderselect_action(&ac, rect, mode, selectmode);
308 /* set notifier that keyframe selection have changed */
309 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
311 return OPERATOR_FINISHED;
314 void ACTION_OT_select_border(wmOperatorType *ot)
317 ot->name= "Border Select";
318 ot->idname= "ACTION_OT_select_border";
319 ot->description= "Select all keyframes within the specified region";
322 ot->invoke= WM_border_select_invoke;
323 ot->exec= actkeys_borderselect_exec;
324 ot->modal= WM_border_select_modal;
325 ot->cancel= WM_border_select_cancel;
327 ot->poll= ED_operator_action_active;
330 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
333 WM_operator_properties_gesture_border(ot, FALSE);
335 ot->prop= RNA_def_boolean(ot->srna, "axis_range", 0, "Axis Range", "");
338 /* ******************** Column Select Operator **************************** */
339 /* This operator works in one of four ways:
340 * - 1) select all keyframes in the same frame as a selected one (KKEY)
341 * - 2) select all keyframes in the same frame as the current frame marker (CTRL-KKEY)
342 * - 3) select all keyframes in the same frame as a selected markers (SHIFT-KKEY)
343 * - 4) select all keyframes that occur between selected markers (ALT-KKEY)
346 /* defines for column-select mode */
347 static EnumPropertyItem prop_column_select_types[] = {
348 {ACTKEYS_COLUMNSEL_KEYS, "KEYS", 0, "On Selected Keyframes", ""},
349 {ACTKEYS_COLUMNSEL_CFRA, "CFRA", 0, "On Current Frame", ""},
350 {ACTKEYS_COLUMNSEL_MARKERS_COLUMN, "MARKERS_COLUMN", 0, "On Selected Markers", ""},
351 {ACTKEYS_COLUMNSEL_MARKERS_BETWEEN, "MARKERS_BETWEEN", 0, "Between Min/Max Selected Markers", ""},
352 {0, NULL, 0, NULL, NULL}
355 /* ------------------- */
357 /* Selects all visible keyframes between the specified markers */
358 static void markers_selectkeys_between (bAnimContext *ac)
360 ListBase anim_data = {NULL, NULL};
364 KeyframeEditFunc ok_cb, select_cb;
365 KeyframeEditData ked= {{NULL}};
368 /* get extreme markers */
369 ED_markers_get_minmax(ac->markers, 1, &min, &max);
373 /* get editing funcs + data */
374 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
375 select_cb= ANIM_editkeyframes_select(SELECT_ADD);
381 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE /*| ANIMFILTER_CURVESONLY */ | ANIMFILTER_NODUPLIS);
382 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
384 /* select keys in-between */
385 for (ale= anim_data.first; ale; ale= ale->next) {
386 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
389 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
390 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
391 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
393 else if (ale->type == ANIMTYPE_GPLAYER) {
394 borderselect_gplayer_frames(ale->data, min, max, SELECT_ADD);
397 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
402 BLI_freelistN(&anim_data);
406 /* Selects all visible keyframes in the same frames as the specified elements */
407 static void columnselect_action_keys (bAnimContext *ac, short mode)
409 ListBase anim_data= {NULL, NULL};
413 Scene *scene= ac->scene;
415 KeyframeEditFunc select_cb, ok_cb;
416 KeyframeEditData ked= {{NULL}};
418 /* initialise keyframe editing data */
420 /* build list of columns */
422 case ACTKEYS_COLUMNSEL_KEYS: /* list of selected keys */
423 if (ac->datatype == ANIMCONT_GPENCIL) {
424 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE);
425 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
427 for (ale= anim_data.first; ale; ale= ale->next)
428 gplayer_make_cfra_list(ale->data, &ked.list, 1);
431 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE /*| ANIMFILTER_CURVESONLY*/);
432 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
434 for (ale= anim_data.first; ale; ale= ale->next)
435 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, bezt_to_cfraelem, NULL);
437 BLI_freelistN(&anim_data);
440 case ACTKEYS_COLUMNSEL_CFRA: /* current frame */
441 /* make a single CfraElem for storing this */
442 ce= MEM_callocN(sizeof(CfraElem), "cfraElem");
443 BLI_addtail(&ked.list, ce);
445 ce->cfra= (float)CFRA;
448 case ACTKEYS_COLUMNSEL_MARKERS_COLUMN: /* list of selected markers */
449 ED_markers_make_cfra_list(ac->markers, &ked.list, SELECT);
452 default: /* invalid option */
456 /* set up BezTriple edit callbacks */
457 select_cb= ANIM_editkeyframes_select(SELECT_ADD);
458 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
460 /* loop through all of the keys and select additional keyframes
461 * based on the keys found to be selected above
463 if (ac->datatype == ANIMCONT_GPENCIL)
464 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE);
466 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE /*| ANIMFILTER_CURVESONLY*/);
467 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
469 for (ale= anim_data.first; ale; ale= ale->next) {
470 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
472 /* loop over cfraelems (stored in the KeyframeEditData->list)
473 * - we need to do this here, as we can apply fewer NLA-mapping conversions
475 for (ce= ked.list.first; ce; ce= ce->next) {
476 /* set frame for validation callback to refer to */
478 ked.f1= BKE_nla_tweakedit_remap(adt, ce->cfra, NLATIME_CONVERT_UNMAP);
482 /* select elements with frame number matching cfraelem */
483 if (ale->type == ANIMTYPE_GPLAYER)
484 select_gpencil_frame(ale->data, ce->cfra, SELECT_ADD);
486 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
491 BLI_freelistN(&ked.list);
492 BLI_freelistN(&anim_data);
495 /* ------------------- */
497 static int actkeys_columnselect_exec(bContext *C, wmOperator *op)
502 /* get editor data */
503 if (ANIM_animdata_get_context(C, &ac) == 0)
504 return OPERATOR_CANCELLED;
506 /* action to take depends on the mode */
507 mode= RNA_enum_get(op->ptr, "mode");
509 if (mode == ACTKEYS_COLUMNSEL_MARKERS_BETWEEN)
510 markers_selectkeys_between(&ac);
512 columnselect_action_keys(&ac, mode);
514 /* set notifier that keyframe selection have changed */
515 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
517 return OPERATOR_FINISHED;
520 void ACTION_OT_select_column (wmOperatorType *ot)
523 ot->name= "Select All";
524 ot->idname= "ACTION_OT_select_column";
525 ot->description= "Select all keyframes on the specified frame(s)";
528 ot->exec= actkeys_columnselect_exec;
529 ot->poll= ED_operator_action_active;
532 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
535 ot->prop= RNA_def_enum(ot->srna, "mode", prop_column_select_types, 0, "Mode", "");
538 /* ******************** Select Linked Operator *********************** */
540 static int actkeys_select_linked_exec (bContext *C, wmOperator *UNUSED(op))
544 ListBase anim_data= {NULL, NULL};
548 KeyframeEditFunc ok_cb = ANIM_editkeyframes_ok(BEZT_OK_SELECTED);
549 KeyframeEditFunc sel_cb = ANIM_editkeyframes_select(SELECT_ADD);
551 /* get editor data */
552 if (ANIM_animdata_get_context(C, &ac) == 0)
553 return OPERATOR_CANCELLED;
555 /* loop through all of the keys and select additional keyframes based on these */
556 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE /*| ANIMFILTER_CURVESONLY*/ | ANIMFILTER_NODUPLIS);
557 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
559 for (ale= anim_data.first; ale; ale= ale->next) {
560 FCurve *fcu= (FCurve *)ale->key_data;
562 /* check if anything selected? */
563 if (ANIM_fcurve_keyframes_loop(NULL, fcu, NULL, ok_cb, NULL)) {
564 /* select every keyframe in this curve then */
565 ANIM_fcurve_keyframes_loop(NULL, fcu, NULL, sel_cb, NULL);
570 BLI_freelistN(&anim_data);
572 /* set notifier that keyframe selection has changed */
573 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
575 return OPERATOR_FINISHED;
578 void ACTION_OT_select_linked (wmOperatorType *ot)
581 ot->name = "Select Linked";
582 ot->idname= "ACTION_OT_select_linked";
583 ot->description = "Select keyframes occurring the same F-Curves as selected ones";
586 ot->exec= actkeys_select_linked_exec;
587 ot->poll= ED_operator_action_active;
590 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
593 /* ******************** Select More/Less Operators *********************** */
595 /* Common code to perform selection */
596 static void select_moreless_action_keys (bAnimContext *ac, short mode)
598 ListBase anim_data= {NULL, NULL};
602 KeyframeEditData ked= {{NULL}};
603 KeyframeEditFunc build_cb;
606 /* init selmap building data */
607 build_cb= ANIM_editkeyframes_buildselmap(mode);
609 /* loop through all of the keys and select additional keyframes based on these */
610 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE /*| ANIMFILTER_CURVESONLY*/ | ANIMFILTER_NODUPLIS);
611 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
613 for (ale= anim_data.first; ale; ale= ale->next) {
614 FCurve *fcu= (FCurve *)ale->key_data;
616 /* only continue if F-Curve has keyframes */
617 if (fcu->bezt == NULL)
620 /* build up map of whether F-Curve's keyframes should be selected or not */
621 ked.data= MEM_callocN(fcu->totvert, "selmap actEdit more");
622 ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, build_cb, NULL);
624 /* based on this map, adjust the selection status of the keyframes */
625 ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, bezt_selmap_flush, NULL);
627 /* free the selmap used here */
633 BLI_freelistN(&anim_data);
636 /* ----------------- */
638 static int actkeys_select_more_exec (bContext *C, wmOperator *UNUSED(op))
642 /* get editor data */
643 if (ANIM_animdata_get_context(C, &ac) == 0)
644 return OPERATOR_CANCELLED;
646 /* perform select changes */
647 select_moreless_action_keys(&ac, SELMAP_MORE);
649 /* set notifier that keyframe selection has changed */
650 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
652 return OPERATOR_FINISHED;
655 void ACTION_OT_select_more (wmOperatorType *ot)
658 ot->name = "Select More";
659 ot->idname= "ACTION_OT_select_more";
660 ot->description = "Select keyframes beside already selected ones";
663 ot->exec= actkeys_select_more_exec;
664 ot->poll= ED_operator_action_active;
667 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
670 /* ----------------- */
672 static int actkeys_select_less_exec (bContext *C, wmOperator *UNUSED(op))
676 /* get editor data */
677 if (ANIM_animdata_get_context(C, &ac) == 0)
678 return OPERATOR_CANCELLED;
680 /* perform select changes */
681 select_moreless_action_keys(&ac, SELMAP_LESS);
683 /* set notifier that keyframe selection has changed */
684 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_SELECTED, NULL);
686 return OPERATOR_FINISHED;
689 void ACTION_OT_select_less (wmOperatorType *ot)
692 ot->name = "Select Less";
693 ot->idname= "ACTION_OT_select_less";
694 ot->description = "Deselect keyframes on ends of selection islands";
697 ot->exec= actkeys_select_less_exec;
698 ot->poll= ED_operator_action_active;
701 ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
704 /* ******************** Select Left/Right Operator ************************* */
705 /* Select keyframes left/right of the current frame indicator */
707 /* defines for left-right select tool */
708 static EnumPropertyItem prop_actkeys_leftright_select_types[] = {
709 {ACTKEYS_LRSEL_TEST, "CHECK", 0, "Check if Select Left or Right", ""},
710 {ACTKEYS_LRSEL_LEFT, "LEFT", 0, "Before current frame", ""},
711 {ACTKEYS_LRSEL_RIGHT, "RIGHT", 0, "After current frame", ""},
712 {0, NULL, 0, NULL, NULL}
715 /* --------------------------------- */
717 static void actkeys_select_leftright (bAnimContext *ac, short leftright, short select_mode)
719 ListBase anim_data = {NULL, NULL};
723 KeyframeEditFunc ok_cb, select_cb;
724 KeyframeEditData ked= {{NULL}};
725 Scene *scene= ac->scene;
727 /* if select mode is replace, deselect all keyframes (and channels) first */
728 if (select_mode==SELECT_REPLACE) {
729 select_mode= SELECT_ADD;
731 /* deselect all other channels and keyframes */
732 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
733 deselect_action_keys(ac, 0, SELECT_SUBTRACT);
736 /* set callbacks and editing data */
737 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
738 select_cb= ANIM_editkeyframes_select(select_mode);
740 if (leftright == ACTKEYS_LRSEL_LEFT) {
742 ked.f2 = (float)(CFRA + 0.1f);
745 ked.f1 = (float)(CFRA - 0.1f);
750 if (ac->datatype == ANIMCONT_GPENCIL)
751 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_NODUPLIS);
753 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE /*| ANIMFILTER_CURVESONLY*/ | ANIMFILTER_NODUPLIS);
754 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
757 for (ale= anim_data.first; ale; ale= ale->next) {
758 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
761 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
762 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
763 ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
765 else if (ale->type == ANIMTYPE_GPLAYER)
766 borderselect_gplayer_frames(ale->data, ked.f1, ked.f2, select_mode);
768 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
771 /* Sync marker support */
772 if (select_mode==SELECT_ADD) {
773 SpaceAction *saction= (SpaceAction *)ac->sl;
775 if ((saction) && (saction->flag & SACTION_MARKERS_MOVE)) {
776 ListBase *markers = ED_animcontext_get_markers(ac);
779 for (marker= markers->first; marker; marker= marker->next) {
780 if( ((leftright == ACTKEYS_LRSEL_LEFT) && (marker->frame < CFRA)) ||
781 ((leftright == ACTKEYS_LRSEL_RIGHT) && (marker->frame >= CFRA)) )
783 marker->flag |= SELECT;
786 marker->flag &= ~SELECT;
793 BLI_freelistN(&anim_data);
796 /* ----------------- */
798 static int actkeys_select_leftright_exec (bContext *C, wmOperator *op)
801 short leftright = RNA_enum_get(op->ptr, "mode");
804 /* get editor data */
805 if (ANIM_animdata_get_context(C, &ac) == 0)
806 return OPERATOR_CANCELLED;
808 /* select mode is either replace (deselect all, then add) or add/extend */
809 if (RNA_boolean_get(op->ptr, "extend"))
810 selectmode= SELECT_INVERT;
812 selectmode= SELECT_REPLACE;
814 /* if "test" mode is set, we don't have any info to set this with */
815 if (leftright == ACTKEYS_LRSEL_TEST)
816 return OPERATOR_CANCELLED;
818 /* do the selecting now */
819 actkeys_select_leftright(&ac, leftright, selectmode);
821 /* set notifier that keyframe selection (and channels too) have changed */
822 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|ND_ANIMCHAN|NA_SELECTED, NULL);
824 return OPERATOR_FINISHED;
827 static int actkeys_select_leftright_invoke (bContext *C, wmOperator *op, wmEvent *event)
830 short leftright = RNA_enum_get(op->ptr, "mode");
832 /* get editor data */
833 if (ANIM_animdata_get_context(C, &ac) == 0)
834 return OPERATOR_CANCELLED;
836 /* handle mode-based testing */
837 if (leftright == ACTKEYS_LRSEL_TEST) {
838 Scene *scene= ac.scene;
840 View2D *v2d= &ar->v2d;
843 /* determine which side of the current frame mouse is on */
844 UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, NULL);
846 RNA_enum_set(op->ptr, "mode", ACTKEYS_LRSEL_LEFT);
848 RNA_enum_set(op->ptr, "mode", ACTKEYS_LRSEL_RIGHT);
851 /* perform selection */
852 return actkeys_select_leftright_exec(C, op);
855 void ACTION_OT_select_leftright (wmOperatorType *ot)
858 ot->name= "Select Left/Right";
859 ot->idname= "ACTION_OT_select_leftright";
860 ot->description= "Select keyframes to the left or the right of the current frame";
863 ot->invoke= actkeys_select_leftright_invoke;
864 ot->exec= actkeys_select_leftright_exec;
865 ot->poll= ED_operator_action_active;
868 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
871 ot->prop= RNA_def_enum(ot->srna, "mode", prop_actkeys_leftright_select_types, ACTKEYS_LRSEL_TEST, "Mode", "");
872 RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
875 /* ******************** Mouse-Click Select Operator *********************** */
876 /* This operator works in one of three ways:
877 * - 1) keyframe under mouse - no special modifiers
878 * - 2) all keyframes on the same side of current frame indicator as mouse - ALT modifier
879 * - 3) column select all keyframes in frame under mouse - CTRL modifier
881 * In addition to these basic options, the SHIFT modifier can be used to toggle the
882 * selection mode between replacing the selection (without) and inverting the selection (with).
885 /* sensitivity factor for frame-selections */
886 #define FRAME_CLICK_THRESH 0.1f
888 /* ------------------- */
890 /* option 1) select keyframe directly under mouse */
891 static void actkeys_mselect_single (bAnimContext *ac, bAnimListElem *ale, short select_mode, float selx)
893 KeyframeEditData ked= {{NULL}};
894 KeyframeEditFunc select_cb, ok_cb;
896 /* get functions for selecting keyframes */
897 select_cb= ANIM_editkeyframes_select(select_mode);
898 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
901 /* select the nominated keyframe on the given frame */
902 if (ale->type == ANIMTYPE_GPLAYER)
903 select_gpencil_frame(ale->data, selx, select_mode);
905 ANIM_animchannel_keyframes_loop(&ked, ac->ads, ale, ok_cb, select_cb, NULL);
908 /* Option 2) Selects all the keyframes on either side of the current frame (depends on which side the mouse is on) */
909 /* (see actkeys_select_leftright) */
911 /* Option 3) Selects all visible keyframes in the same frame as the mouse click */
912 static void actkeys_mselect_column(bAnimContext *ac, short select_mode, float selx)
914 ListBase anim_data= {NULL, NULL};
918 KeyframeEditFunc select_cb, ok_cb;
919 KeyframeEditData ked= {{NULL}};
921 /* initialise keyframe editing data */
923 /* set up BezTriple edit callbacks */
924 select_cb= ANIM_editkeyframes_select(select_mode);
925 ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
927 /* loop through all of the keys and select additional keyframes
928 * based on the keys found to be selected above
930 if (ac->datatype == ANIMCONT_GPENCIL)
931 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE /*| ANIMFILTER_CURVESONLY */ | ANIMFILTER_NODUPLIS);
933 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_NODUPLIS);
934 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
936 for (ale= anim_data.first; ale; ale= ale->next) {
937 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
939 /* set frame for validation callback to refer to */
941 ked.f1= BKE_nla_tweakedit_remap(adt, selx, NLATIME_CONVERT_UNMAP);
945 /* select elements with frame number matching cfra */
946 if (ale->type == ANIMTYPE_GPLAYER)
947 select_gpencil_frame(ale->key_data, selx, select_mode);
949 ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
953 BLI_freelistN(&ked.list);
954 BLI_freelistN(&anim_data);
957 /* ------------------- */
959 static void mouse_action_keys (bAnimContext *ac, const int mval[2], short select_mode, short column)
961 ListBase anim_data = {NULL, NULL};
962 DLRBT_Tree anim_keys;
966 View2D *v2d= &ac->ar->v2d;
967 bDopeSheet *ads = NULL;
974 /* get dopesheet info */
975 if (ac->datatype == ANIMCONT_DOPESHEET)
978 /* use View2D to determine the index of the channel (i.e a row in the list) where keyframe was */
979 UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
980 UI_view2d_listview_view_to_cell(v2d, 0, ACHANNEL_STEP, 0, (float)ACHANNEL_HEIGHT_HALF, x, y, NULL, &channel_index);
982 /* x-range to check is +/- 7 (in screen/region-space) on either side of mouse click (size of keyframe icon) */
983 UI_view2d_region_to_view(v2d, mval[0]-7, mval[1], &rectf.xmin, &rectf.ymin);
984 UI_view2d_region_to_view(v2d, mval[0]+7, mval[1], &rectf.xmax, &rectf.ymax);
987 filter= (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
988 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
990 /* try to get channel */
991 ale= BLI_findlink(&anim_data, channel_index);
993 /* channel not found */
994 printf("Error: animation channel (index = %d) not found in mouse_action_keys() \n", channel_index);
995 BLI_freelistN(&anim_data);
999 /* found match - must return here... */
1000 AnimData *adt= ANIM_nla_mapping_get(ac, ale);
1001 ActKeyColumn *ak, *akn=NULL;
1003 /* make list of keyframes */
1004 BLI_dlrbTree_init(&anim_keys);
1006 if (ale->key_data) {
1007 switch (ale->datatype) {
1010 Scene *scene= (Scene *)ale->key_data;
1011 scene_to_keylist(ads, scene, &anim_keys, NULL);
1016 Object *ob= (Object *)ale->key_data;
1017 ob_to_keylist(ads, ob, &anim_keys, NULL);
1022 bAction *act= (bAction *)ale->key_data;
1023 action_to_keylist(adt, act, &anim_keys, NULL);
1028 FCurve *fcu= (FCurve *)ale->key_data;
1029 fcurve_to_keylist(adt, fcu, &anim_keys, NULL);
1034 else if (ale->type == ANIMTYPE_SUMMARY) {
1035 /* dopesheet summary covers everything */
1036 summary_to_keylist(ac, &anim_keys, NULL);
1038 else if (ale->type == ANIMTYPE_GROUP) {
1039 // TODO: why don't we just give groups key_data too?
1040 bActionGroup *agrp= (bActionGroup *)ale->data;
1041 agroup_to_keylist(adt, agrp, &anim_keys, NULL);
1043 else if (ale->type == ANIMTYPE_GPLAYER) {
1044 // TODO: why don't we just give gplayers key_data too?
1045 bGPDlayer *gpl = (bGPDlayer *)ale->data;
1046 gpl_to_keylist(ads, gpl, &anim_keys);
1049 /* start from keyframe at root of BST, traversing until we find one within the range that was clicked on */
1050 for (ak= anim_keys.root; ak; ak= akn) {
1051 if (IN_RANGE(ak->cfra, rectf.xmin, rectf.xmax)) {
1052 /* set the frame to use, and apply inverse-correction for NLA-mapping
1053 * so that the frame will get selected by the selection functions without
1054 * requiring to map each frame once again...
1056 selx= BKE_nla_tweakedit_remap(adt, ak->cfra, NLATIME_CONVERT_UNMAP);
1060 else if (ak->cfra < rectf.xmin)
1066 /* remove active channel from list of channels for separate treatment (since it's needed later on) */
1067 BLI_remlink(&anim_data, ale);
1069 /* cleanup temporary lists */
1070 BLI_dlrbTree_free(&anim_keys);
1072 /* free list of channels, since it's not used anymore */
1073 BLI_freelistN(&anim_data);
1076 /* for replacing selection, firstly need to clear existing selection */
1077 if (select_mode == SELECT_REPLACE) {
1078 /* reset selection mode for next steps */
1079 select_mode = SELECT_ADD;
1081 /* deselect all keyframes */
1082 deselect_action_keys(ac, 0, SELECT_SUBTRACT);
1084 /* highlight channel clicked on */
1085 if (ELEM(ac->datatype, ANIMCONT_ACTION, ANIMCONT_DOPESHEET)) {
1086 /* deselect all other channels first */
1087 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1089 /* Highlight Action-Group or F-Curve? */
1090 if (ale && ale->data) {
1091 if (ale->type == ANIMTYPE_GROUP) {
1092 bActionGroup *agrp= ale->data;
1094 agrp->flag |= AGRP_SELECTED;
1095 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
1097 else if (ale->type == ANIMTYPE_FCURVE) {
1098 FCurve *fcu= ale->data;
1100 fcu->flag |= FCURVE_SELECTED;
1101 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ANIMTYPE_FCURVE);
1105 else if (ac->datatype == ANIMCONT_GPENCIL) {
1106 /* deselect all other channels first */
1107 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
1109 /* Highlight GPencil Layer */
1110 if ((ale && ale->data) && (ale->type == ANIMTYPE_GPLAYER)) {
1111 bGPDlayer *gpl = ale->data;
1113 gpl->flag |= GP_LAYER_SELECT;
1114 //gpencil_layer_setactive(gpd, gpl);
1119 /* only select keyframes if we clicked on a valid channel and hit something */
1122 /* apply selection to keyframes */
1124 /* select all keyframes in the same frame as the one we hit on the active channel */
1125 actkeys_mselect_column(ac, select_mode, selx);
1128 /* select the nominated keyframe on the given frame */
1129 actkeys_mselect_single(ac, ale, select_mode, selx);
1133 /* free this channel */
1138 /* handle clicking */
1139 static int actkeys_clickselect_invoke(bContext *C, wmOperator *op, wmEvent *event)
1143 short selectmode, column;
1145 /* get editor data */
1146 if (ANIM_animdata_get_context(C, &ac) == 0)
1147 return OPERATOR_CANCELLED;
1149 /* get useful pointers from animation context data */
1152 /* select mode is either replace (deselect all, then add) or add/extend */
1153 if (RNA_boolean_get(op->ptr, "extend"))
1154 selectmode= SELECT_INVERT;
1156 selectmode= SELECT_REPLACE;
1158 /* column selection */
1159 column= RNA_boolean_get(op->ptr, "column");
1161 /* select keyframe(s) based upon mouse position*/
1162 mouse_action_keys(&ac, event->mval, selectmode, column);
1164 /* set notifier that keyframe selection (and channels too) have changed */
1165 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|ND_ANIMCHAN|NA_SELECTED, NULL);
1167 /* for tweak grab to work */
1168 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
1171 void ACTION_OT_clickselect (wmOperatorType *ot)
1174 ot->name= "Mouse Select Keys";
1175 ot->idname= "ACTION_OT_clickselect";
1176 ot->description= "Select keyframes by clicking on them";
1178 /* api callbacks - absolutely no exec() this yet... */
1179 ot->invoke= actkeys_clickselect_invoke;
1180 ot->poll= ED_operator_action_active;
1183 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1186 RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
1187 RNA_def_boolean(ot->srna, "column", 0, "Column Select", ""); // ALTKEY
1190 /* ************************************************************************** */