Animation: enforce that the active keyframe is always selected

Check selection state in `BKE_fcurve_active_keyframe_index()`, and only
return the active keyframe index when that keyframe is actually selected.
This is now also asserted in the `BKE_fcurve_active_keyframe_set()` function,
which is now also used when inserting a keyframe.
This commit is contained in:
Sybren A. Stüvel 2020-10-12 16:55:46 +02:00
parent 0d3e192660
commit 5e6c7de3e9
3 changed files with 13 additions and 3 deletions

View File

@ -839,6 +839,8 @@ bool BKE_fcurve_calc_range(
*/
void BKE_fcurve_active_keyframe_set(FCurve *fcu, const BezTriple *active_bezt)
{
/* The active keyframe should always be selected. */
BLI_assert(active_bezt->f2 & SELECT);
fcu->active_keyframe_index = (active_bezt == NULL) ? FCURVE_ACTIVE_KEYFRAME_NONE :
active_bezt - fcu->bezt;
}
@ -850,12 +852,18 @@ int BKE_fcurve_active_keyframe_index(const FCurve *fcu)
{
const int active_keyframe_index = fcu->active_keyframe_index;
/* Sanity checks. */
/* Array access boundary checks. */
if ((fcu->bezt == NULL) || (active_keyframe_index >= fcu->totvert) ||
(active_keyframe_index < 0)) {
return FCURVE_ACTIVE_KEYFRAME_NONE;
}
const BezTriple *active_bezt = &fcu->bezt[active_keyframe_index];
if ((active_bezt->f2 & SELECT) == 0) {
/* The active keyframe should always be selected. If it's not selected, it can't be active. */
return FCURVE_ACTIVE_KEYFRAME_NONE;
}
return active_keyframe_index;
}

View File

@ -605,8 +605,7 @@ int insert_vert_fcurve(
/* add temp beztriple to keyframes */
a = insert_bezt_fcurve(fcu, &beztr, flag);
fcu->active_keyframe_index = a;
BKE_fcurve_active_keyframe_set(fcu, &fcu->bezt[a]);
/* what if 'a' is a negative index?
* for now, just exit to prevent any segfaults

View File

@ -593,6 +593,9 @@ typedef struct FCurve {
/**
* Index of active keyframe in #bezt for numerical editing in the interface. A value of
* #FCURVE_ACTIVE_KEYFRAME_NONE indicates that the FCurve has no active keyframe.
*
* Do not access directly, use #BKE_fcurve_active_keyframe_index() and
* #BKE_fcurve_active_keyframe_set() instead.
*/
int active_keyframe_index;