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) 2009 Blender Foundation, Joshua Leung
19 * All rights reserved.
22 * Contributor(s): Joshua Leung (major recode)
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_nla/nla_select.c
35 #include "DNA_anim_types.h"
36 #include "DNA_scene_types.h"
38 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
48 #include "ED_anim_api.h"
49 #include "ED_keyframes_edit.h"
50 #include "ED_screen.h"
52 #include "RNA_access.h"
53 #include "RNA_define.h"
58 #include "UI_view2d.h"
60 #include "nla_intern.h" // own include
62 /* ******************** Utilities ***************************************** */
64 /* Convert SELECT_* flags to ACHANNEL_SETFLAG_* flags */
65 static short selmodes_to_flagmodes(short sel)
67 /* convert selection modes to selection modes */
70 return ACHANNEL_SETFLAG_CLEAR;
74 return ACHANNEL_SETFLAG_INVERT;
79 return ACHANNEL_SETFLAG_ADD;
85 /* ******************** Deselect All Operator ***************************** */
86 /* This operator works in one of three ways:
87 * 1) (de)select all (AKEY) - test if select all or deselect all
88 * 2) invert all (CTRL-IKEY) - invert selection of all keyframes
89 * 3) (de)select all - no testing is done; only for use internal tools as normal function...
93 DESELECT_STRIPS_NOTEST = 0,
95 DESELECT_STRIPS_CLEARACTIVE,
96 } /*eDeselectNlaStrips*/;
98 /* Deselects strips in the NLA Editor
99 * - This is called by the deselect all operator, as well as other ones!
101 * - test: check if select or deselect all (1) or clear all active (2)
102 * - sel: how to select keyframes
107 static void deselect_nla_strips(bAnimContext *ac, short test, short sel)
109 ListBase anim_data = {NULL, NULL};
114 /* determine type-based settings */
115 // FIXME: double check whether ANIMFILTER_LIST_VISIBLE is needed!
116 filter = (ANIMFILTER_DATA_VISIBLE);
119 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
121 /* See if we should be selecting or deselecting */
122 if (test == DESELECT_STRIPS_TEST) {
123 for (ale = anim_data.first; ale; ale = ale->next) {
124 NlaTrack *nlt = (NlaTrack *)ale->data;
127 /* if any strip is selected, break out, since we should now be deselecting */
128 for (strip = nlt->strips.first; strip; strip = strip->next) {
129 if (strip->flag & NLASTRIP_FLAG_SELECT) {
130 sel = SELECT_SUBTRACT;
135 if (sel == SELECT_SUBTRACT)
140 /* convert selection modes to selection modes */
141 smode = selmodes_to_flagmodes(sel);
143 /* Now set the flags */
144 for (ale = anim_data.first; ale; ale = ale->next) {
145 NlaTrack *nlt = (NlaTrack *)ale->data;
148 /* apply same selection to all strips */
149 for (strip = nlt->strips.first; strip; strip = strip->next) {
151 if (test != DESELECT_STRIPS_CLEARACTIVE)
152 ACHANNEL_SET_FLAG(strip, smode, NLASTRIP_FLAG_SELECT);
154 /* clear active flag */
155 // TODO: for clear active, do we want to limit this to only doing this on a certain set of tracks though?
156 strip->flag &= ~NLASTRIP_FLAG_ACTIVE;
161 BLI_freelistN(&anim_data);
164 /* ------------------- */
166 static int nlaedit_deselectall_exec(bContext *C, wmOperator *op)
170 /* get editor data */
171 if (ANIM_animdata_get_context(C, &ac) == 0)
172 return OPERATOR_CANCELLED;
174 /* 'standard' behavior - check if selected, then apply relevant selection */
175 if (RNA_boolean_get(op->ptr, "invert"))
176 deselect_nla_strips(&ac, DESELECT_STRIPS_NOTEST, SELECT_INVERT);
178 deselect_nla_strips(&ac, DESELECT_STRIPS_TEST, SELECT_ADD);
180 /* set notifier that things have changed */
181 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_SELECTED, NULL);
183 return OPERATOR_FINISHED;
186 void NLA_OT_select_all_toggle(wmOperatorType *ot)
189 ot->name = "(De)select All";
190 ot->idname = "NLA_OT_select_all_toggle";
191 ot->description = "Select or deselect all NLA-Strips";
194 ot->exec = nlaedit_deselectall_exec;
195 ot->poll = nlaop_poll_tweakmode_off;
198 ot->flag = OPTYPE_REGISTER /*|OPTYPE_UNDO*/;
201 ot->prop = RNA_def_boolean(ot->srna, "invert", 0, "Invert", "");
202 RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
205 /* ******************** Border Select Operator **************************** */
206 /* This operator currently works in one of three ways:
207 * -> BKEY - 1) all strips within region are selected (NLAEDIT_BORDERSEL_ALLSTRIPS)
208 * -> ALT-BKEY - depending on which axis of the region was larger...
209 * -> 2) x-axis, so select all frames within frame range (NLAEDIT_BORDERSEL_FRAMERANGE)
210 * -> 3) y-axis, so select all frames within channels that region included (NLAEDIT_BORDERSEL_CHANNELS)
213 /* defines for borderselect mode */
215 NLA_BORDERSEL_ALLSTRIPS = 0,
216 NLA_BORDERSEL_FRAMERANGE,
217 NLA_BORDERSEL_CHANNELS,
218 } /* eNLAEDIT_BorderSelect_Mode */;
221 static void borderselect_nla_strips(bAnimContext *ac, rcti rect, short mode, short selectmode)
223 ListBase anim_data = {NULL, NULL};
227 SpaceNla *snla = (SpaceNla *)ac->sl;
228 View2D *v2d = &ac->ar->v2d;
230 float ymin /* =(float)(-NLACHANNEL_HEIGHT(snla)) */ /* UNUSED */, ymax = 0;
232 /* convert border-region to view coordinates */
233 UI_view2d_region_to_view(v2d, rect.xmin, rect.ymin + 2, &rectf.xmin, &rectf.ymin);
234 UI_view2d_region_to_view(v2d, rect.xmax, rect.ymax - 2, &rectf.xmax, &rectf.ymax);
237 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
238 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
240 /* convert selection modes to selection modes */
241 selectmode = selmodes_to_flagmodes(selectmode);
243 /* loop over data, doing border select */
244 for (ale = anim_data.first; ale; ale = ale->next) {
245 ymin = ymax - NLACHANNEL_STEP(snla);
247 /* perform vertical suitability check (if applicable) */
248 if ( (mode == NLA_BORDERSEL_FRAMERANGE) ||
249 !((ymax < rectf.ymin) || (ymin > rectf.ymax)) )
251 /* loop over data selecting (only if NLA-Track) */
252 if (ale->type == ANIMTYPE_NLATRACK) {
253 NlaTrack *nlt = (NlaTrack *)ale->data;
256 /* only select strips if they fall within the required ranges (if applicable) */
257 for (strip = nlt->strips.first; strip; strip = strip->next) {
258 if ( (mode == NLA_BORDERSEL_CHANNELS) ||
259 BKE_nlastrip_within_bounds(strip, rectf.xmin, rectf.xmax))
262 ACHANNEL_SET_FLAG(strip, selectmode, NLASTRIP_FLAG_SELECT);
264 /* clear active flag */
265 strip->flag &= ~NLASTRIP_FLAG_ACTIVE;
271 /* set minimum extent to be the maximum of the next channel */
276 BLI_freelistN(&anim_data);
279 /* ------------------- */
281 static int nlaedit_borderselect_exec(bContext *C, wmOperator *op)
285 short mode = 0, selectmode = 0;
288 /* get editor data */
289 if (ANIM_animdata_get_context(C, &ac) == 0)
290 return OPERATOR_CANCELLED;
292 /* clear all selection if not extending selection */
293 extend = RNA_boolean_get(op->ptr, "extend");
295 deselect_nla_strips(&ac, DESELECT_STRIPS_TEST, SELECT_SUBTRACT);
297 /* get settings from operator */
298 rect.xmin = RNA_int_get(op->ptr, "xmin");
299 rect.ymin = RNA_int_get(op->ptr, "ymin");
300 rect.xmax = RNA_int_get(op->ptr, "xmax");
301 rect.ymax = RNA_int_get(op->ptr, "ymax");
303 if (RNA_int_get(op->ptr, "gesture_mode") == GESTURE_MODAL_SELECT)
304 selectmode = SELECT_ADD;
306 selectmode = SELECT_SUBTRACT;
308 /* selection 'mode' depends on whether borderselect region only matters on one axis */
309 if (RNA_boolean_get(op->ptr, "axis_range")) {
310 /* mode depends on which axis of the range is larger to determine which axis to use
311 * - checking this in region-space is fine, as it's fundamentally still going to be a different rect size
312 * - the frame-range select option is favored over the channel one (x over y), as frame-range one is often
313 * used for tweaking timing when "blocking", while channels is not that useful...
315 if ((rect.xmax - rect.xmin) >= (rect.ymax - rect.ymin))
316 mode = NLA_BORDERSEL_FRAMERANGE;
318 mode = NLA_BORDERSEL_CHANNELS;
321 mode = NLA_BORDERSEL_ALLSTRIPS;
323 /* apply borderselect action */
324 borderselect_nla_strips(&ac, rect, mode, selectmode);
326 /* set notifier that things have changed */
327 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_SELECTED, NULL);
329 return OPERATOR_FINISHED;
332 void NLA_OT_select_border(wmOperatorType *ot)
335 ot->name = "Border Select";
336 ot->idname = "NLA_OT_select_border";
337 ot->description = "Use box selection to grab NLA-Strips";
340 ot->invoke = WM_border_select_invoke;
341 ot->exec = nlaedit_borderselect_exec;
342 ot->modal = WM_border_select_modal;
343 ot->cancel = WM_border_select_cancel;
345 ot->poll = nlaop_poll_tweakmode_off;
348 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
351 WM_operator_properties_gesture_border(ot, 1);
353 RNA_def_boolean(ot->srna, "axis_range", 0, "Axis Range", "");
356 /* ******************** Select Left/Right Operator ************************* */
357 /* Select keyframes left/right of the current frame indicator */
359 /* defines for left-right select tool */
360 static EnumPropertyItem prop_nlaedit_leftright_select_types[] = {
361 {NLAEDIT_LRSEL_TEST, "CHECK", 0, "Check if Select Left or Right", ""},
362 {NLAEDIT_LRSEL_LEFT, "LEFT", 0, "Before current frame", ""},
363 {NLAEDIT_LRSEL_RIGHT, "RIGHT", 0, "After current frame", ""},
364 {0, NULL, 0, NULL, NULL}
367 /* ------------------- */
369 static void nlaedit_select_leftright(bContext *C, bAnimContext *ac, short leftright, short select_mode)
371 ListBase anim_data = {NULL, NULL};
375 Scene *scene = ac->scene;
378 /* if currently in tweakmode, exit tweakmode first */
379 if (scene->flag & SCE_NLA_EDIT_ON)
380 WM_operator_name_call(C, "NLA_OT_tweakmode_exit", WM_OP_EXEC_DEFAULT, NULL);
382 /* if select mode is replace, deselect all keyframes (and channels) first */
383 if (select_mode == SELECT_REPLACE) {
384 select_mode = SELECT_ADD;
386 /* - deselect all other keyframes, so that just the newly selected remain
387 * - channels aren't deselected, since we don't re-select any as a consequence
389 deselect_nla_strips(ac, 0, SELECT_SUBTRACT);
392 /* get range, and get the right flag-setting mode */
393 if (leftright == NLAEDIT_LRSEL_LEFT) {
395 xmax = (float)(CFRA + 0.1f);
398 xmin = (float)(CFRA - 0.1f);
402 select_mode = selmodes_to_flagmodes(select_mode);
406 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE);
407 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
409 /* select strips on the side where most data occurs */
410 for (ale = anim_data.first; ale; ale = ale->next) {
411 NlaTrack *nlt = (NlaTrack *)ale->data;
414 /* check each strip to see if it is appropriate */
415 for (strip = nlt->strips.first; strip; strip = strip->next) {
416 if (BKE_nlastrip_within_bounds(strip, xmin, xmax)) {
417 ACHANNEL_SET_FLAG(strip, select_mode, NLASTRIP_FLAG_SELECT);
423 BLI_freelistN(&anim_data);
426 /* ------------------- */
428 static int nlaedit_select_leftright_exec(bContext *C, wmOperator *op)
431 short leftright = RNA_enum_get(op->ptr, "mode");
434 /* get editor data */
435 if (ANIM_animdata_get_context(C, &ac) == 0)
436 return OPERATOR_CANCELLED;
438 /* select mode is either replace (deselect all, then add) or add/extend */
439 if (RNA_boolean_get(op->ptr, "extend"))
440 selectmode = SELECT_INVERT;
442 selectmode = SELECT_REPLACE;
444 /* if "test" mode is set, we don't have any info to set this with */
445 if (leftright == NLAEDIT_LRSEL_TEST)
446 return OPERATOR_CANCELLED;
448 /* do the selecting now */
449 nlaedit_select_leftright(C, &ac, leftright, selectmode);
451 /* set notifier that keyframe selection (and channels too) have changed */
452 WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | ND_ANIMCHAN | NA_SELECTED, NULL);
454 return OPERATOR_FINISHED;
457 static int nlaedit_select_leftright_invoke(bContext *C, wmOperator *op, wmEvent *event)
460 short leftright = RNA_enum_get(op->ptr, "mode");
462 /* get editor data */
463 if (ANIM_animdata_get_context(C, &ac) == 0)
464 return OPERATOR_CANCELLED;
466 /* handle mode-based testing */
467 if (leftright == NLAEDIT_LRSEL_TEST) {
468 Scene *scene = ac.scene;
470 View2D *v2d = &ar->v2d;
473 /* determine which side of the current frame mouse is on */
474 UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, NULL);
476 RNA_int_set(op->ptr, "mode", NLAEDIT_LRSEL_LEFT);
478 RNA_int_set(op->ptr, "mode", NLAEDIT_LRSEL_RIGHT);
481 /* perform selection */
482 return nlaedit_select_leftright_exec(C, op);
485 void NLA_OT_select_leftright(wmOperatorType *ot)
490 ot->name = "Select Left/Right";
491 ot->idname = "NLA_OT_select_leftright";
492 ot->description = "Select strips to the left or the right of the current frame";
495 ot->invoke = nlaedit_select_leftright_invoke;
496 ot->exec = nlaedit_select_leftright_exec;
497 ot->poll = ED_operator_nla_active;
500 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
503 ot->prop = RNA_def_enum(ot->srna, "mode", prop_nlaedit_leftright_select_types, NLAEDIT_LRSEL_TEST, "Mode", "");
504 RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
506 prop = RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
507 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
511 /* ******************** Mouse-Click Select Operator *********************** */
513 /* select strip directly under mouse */
514 static void mouse_nla_strips(bContext *C, bAnimContext *ac, const int mval[2], short select_mode)
516 ListBase anim_data = {NULL, NULL};
517 bAnimListElem *ale = NULL;
520 SpaceNla *snla = (SpaceNla *)ac->sl;
521 View2D *v2d = &ac->ar->v2d;
522 Scene *scene = ac->scene;
523 NlaStrip *strip = NULL;
525 float xmin, xmax, dummy;
529 /* use View2D to determine the index of the channel (i.e a row in the list) where keyframe was */
530 UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
531 UI_view2d_listview_view_to_cell(v2d, 0, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
533 /* x-range to check is +/- 7 (in screen/region-space) on either side of mouse click
534 * (that is the size of keyframe icons, so user should be expecting similar tolerances)
536 UI_view2d_region_to_view(v2d, mval[0] - 7, mval[1], &xmin, &dummy);
537 UI_view2d_region_to_view(v2d, mval[0] + 7, mval[1], &xmax, &dummy);
540 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
541 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
543 /* try to get channel */
544 ale = BLI_findlink(&anim_data, channel_index);
546 /* channel not found */
547 printf("Error: animation channel (index = %d) not found in mouse_nla_strips()\n", channel_index);
548 BLI_freelistN(&anim_data);
552 /* found some channel - we only really should do somethign when its an Nla-Track */
553 if (ale->type == ANIMTYPE_NLATRACK) {
554 NlaTrack *nlt = (NlaTrack *)ale->data;
556 /* loop over NLA-strips in this track, trying to find one which occurs in the necessary bounds */
557 for (strip = nlt->strips.first; strip; strip = strip->next) {
558 if (BKE_nlastrip_within_bounds(strip, xmin, xmax))
563 /* remove active channel from list of channels for separate treatment (since it's needed later on) */
564 BLI_remlink(&anim_data, ale);
566 /* free list of channels, since it's not used anymore */
567 BLI_freelistN(&anim_data);
570 /* if currently in tweakmode, exit tweakmode before changing selection states
571 * now that we've found our target...
573 if (scene->flag & SCE_NLA_EDIT_ON)
574 WM_operator_name_call(C, "NLA_OT_tweakmode_exit", WM_OP_EXEC_DEFAULT, NULL);
576 /* for replacing selection, firstly need to clear existing selection */
577 if (select_mode == SELECT_REPLACE) {
578 /* reset selection mode for next steps */
579 select_mode = SELECT_ADD;
581 /* deselect all strips */
582 deselect_nla_strips(ac, 0, SELECT_SUBTRACT);
584 /* deselect all other channels first */
585 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
587 /* Highlight NLA-Track */
588 if (ale->type == ANIMTYPE_NLATRACK) {
589 NlaTrack *nlt = (NlaTrack *)ale->data;
591 nlt->flag |= NLATRACK_SELECTED;
592 ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, nlt, ANIMTYPE_NLATRACK);
596 /* only select strip if we clicked on a valid channel and hit something */
598 /* select the strip accordingly (if a matching one was found) */
600 select_mode = selmodes_to_flagmodes(select_mode);
601 ACHANNEL_SET_FLAG(strip, select_mode, NLASTRIP_FLAG_SELECT);
603 /* if we selected it, we can make it active too
604 * - we always need to clear the active strip flag though...
606 deselect_nla_strips(ac, DESELECT_STRIPS_CLEARACTIVE, 0);
607 if (strip->flag & NLASTRIP_FLAG_SELECT)
608 strip->flag |= NLASTRIP_FLAG_ACTIVE;
611 /* free this channel */
616 /* ------------------- */
618 /* handle clicking */
619 static int nlaedit_clickselect_invoke(bContext *C, wmOperator *op, wmEvent *event)
622 /* Scene *scene; */ /* UNUSED */
623 /* ARegion *ar; */ /* UNUSED */
624 // View2D *v2d; /*UNUSED*/
627 /* get editor data */
628 if (ANIM_animdata_get_context(C, &ac) == 0)
629 return OPERATOR_CANCELLED;
631 /* get useful pointers from animation context data */
632 /* scene= ac.scene; */ /* UNUSED */
633 /* ar= ac.ar; */ /* UNUSED */
636 /* select mode is either replace (deselect all, then add) or add/extend */
637 if (RNA_boolean_get(op->ptr, "extend"))
638 selectmode = SELECT_INVERT;
640 selectmode = SELECT_REPLACE;
642 /* select strips based upon mouse position */
643 mouse_nla_strips(C, &ac, event->mval, selectmode);
645 /* set notifier that things have changed */
646 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_SELECTED, NULL);
648 /* for tweak grab to work */
649 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
652 void NLA_OT_click_select(wmOperatorType *ot)
657 ot->name = "Mouse Select";
658 ot->idname = "NLA_OT_click_select";
659 ot->description = "Handle clicks to select NLA Strips";
661 /* api callbacks - absolutely no exec() this yet... */
662 ot->invoke = nlaedit_clickselect_invoke;
663 ot->poll = ED_operator_nla_active;
666 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
669 prop = RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
670 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
673 /* *********************************************** */