Refactor: GPv3: Return 0 from get_frame_duration_at for implicitly held frames

This function returned the duration in frames for a keyframe, but
for keyframes that are implicitly held until the next keyframe,
it makes more sense to return 0.
For `insert_frame` a duration of 0 also creates an implicit hold
so this is more consistent with this API and removes a few checks
elsewhere.
This commit is contained in:
Falk David
2024-05-10 10:24:25 +02:00
parent 8056a31a7e
commit 2fb752b168
5 changed files with 15 additions and 14 deletions

View File

@@ -462,8 +462,10 @@ class Layer : public ::GreasePencilLayer {
GreasePencilFrame *frame_at(const int frame_number);
/**
* \returns the frame duration of the active frame at \a frame_number or -1 if there is no active
* frame or the active frame is the last frame.
* \returns the frame duration of the keyframe at \a frame_number.
* If there is no keyframe at \a frame_number it \returns -1.
* If the keyframe is an implicit hold, \returns 0.
* if the keyframe is the last one, \returns -1.
*/
int get_frame_duration_at(const int frame_number) const;

View File

@@ -1165,10 +1165,16 @@ int Layer::get_frame_duration_at(const int frame_number) const
if (!frame_key) {
return -1;
}
/* For frames that are implicitly held, we return a duration of 0. */
if (this->frames().lookup_ptr(*frame_key)->is_implicit_hold()) {
return 0;
}
SortedKeysIterator frame_number_it = std::next(this->sorted_keys().begin(), *frame_key);
/* The last key has no duration. */
if (*frame_number_it == this->sorted_keys().last()) {
return -1;
}
/* Compute the difference in frames between this key and the next key. */
const int next_frame_number = *(std::next(frame_number_it));
return next_frame_number - frame_number;
}
@@ -2173,9 +2179,7 @@ bool GreasePencil::insert_duplicate_frame(blender::bke::greasepencil::Layer &lay
* If we want to make an instance of the source frame, the drawing index gets copied from the
* source frame. Otherwise, we set the drawing index to the size of the drawings array, since we
* are going to add a new drawing copied from the source drawing. */
const int duration = src_frame.is_implicit_hold() ?
0 :
layer.get_frame_duration_at(src_frame_number);
const int duration = layer.get_frame_duration_at(src_frame_number);
GreasePencilFrame *dst_frame = layer.add_frame(dst_frame_number, duration);
if (dst_frame == nullptr) {
return false;
@@ -2375,9 +2379,7 @@ void GreasePencil::move_duplicate_frames(
/* Copy frames durations. */
Map<int, int> src_layer_frames_durations;
for (const auto [frame_number, frame] : layer.frames().items()) {
if (!frame.is_implicit_hold()) {
src_layer_frames_durations.add(frame_number, layer.get_frame_duration_at(frame_number));
}
src_layer_frames_durations.add(frame_number, layer.get_frame_duration_at(frame_number));
}
/* Remove original frames for duplicates before inserting any frames.

View File

@@ -465,8 +465,7 @@ bool grease_pencil_copy_keyframes(bAnimContext *ac, KeyframeClipboard &clipboard
for (auto [frame_number, frame] : layer->frames().items()) {
if (frame.is_selected()) {
const Drawing *drawing = grease_pencil->get_drawing_at(*layer, frame_number);
const int duration = frame.is_implicit_hold() ? 0 :
layer->get_frame_duration_at(frame_number);
const int duration = layer->get_frame_duration_at(frame_number);
buf.append(
{frame_number, Drawing(*drawing), duration, eBezTriple_KeyframeType(frame.type)});

View File

@@ -504,7 +504,7 @@ static int grease_pencil_layer_duplicate_exec(bContext *C, wmOperator *op)
/* Clear source keyframes and recreate them with duplicated drawings. */
new_layer.frames_for_write().clear();
for (auto [key, frame] : active_layer.frames().items()) {
const int duration = frame.is_implicit_hold() ? 0 : active_layer.get_frame_duration_at(key);
const int duration = active_layer.get_frame_duration_at(key);
GreasePencilFrame *new_frame = new_layer.add_frame(key, duration);
new_frame->drawing_index = grease_pencil.drawings().size();

View File

@@ -96,9 +96,7 @@ static bool grease_pencil_layer_initialize_trans_data(blender::bke::greasepencil
}
/* Store frames' duration to keep them visually correct while moving the frames. */
if (!frame.is_implicit_hold()) {
trans_data.frames_duration.add(frame_number, layer.get_frame_duration_at(frame_number));
}
trans_data.frames_duration.add(frame_number, layer.get_frame_duration_at(frame_number));
}
trans_data.status = LayerTransformData::TRANS_INIT;