Refactor: GPv3: Use insert_frame API in duplicate layer operator

This is part of #121565.

Uses the newer `GreasePencil::insert_frame` API to insert
a key and create a drawing. This means we no longer need
to deal with the drawing index, as this is handled by
`insert_frame` internally.
This commit is contained in:
Falk David
2024-05-13 17:02:58 +02:00
parent 9bc8e438ef
commit 10b5a401dc
@@ -541,23 +541,23 @@ 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 = active_layer.get_frame_duration_at(key);
for (auto [frame_number, frame] : active_layer.frames().items()) {
const int duration = active_layer.get_frame_duration_at(frame_number);
GreasePencilFrame *new_frame = new_layer.add_frame(key, duration);
new_frame->drawing_index = grease_pencil.drawings().size();
new_frame->type = frame.type;
if (empty_keyframes) {
grease_pencil.add_empty_drawings(1);
}
else {
const Drawing &drawing = *grease_pencil.get_drawing_at(active_layer, key);
grease_pencil.add_duplicate_drawings(1, drawing);
Drawing *dst_drawing = grease_pencil.insert_frame(
new_layer, frame_number, duration, eBezTriple_KeyframeType(frame.type));
if (!empty_keyframes) {
BLI_assert(dst_drawing != nullptr);
/* TODO: This can fail (return `nullptr`) if the drawing is a drawing reference! */
const Drawing &src_drawing = *grease_pencil.get_drawing_at(active_layer, frame_number);
/* Duplicate the drawing. */
*dst_drawing = src_drawing;
}
}
grease_pencil.move_node_after(new_layer.as_node(), active_layer.as_node());
grease_pencil.set_active_layer(&new_layer);
DEG_id_tag_update(&grease_pencil.id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_SELECTED, nullptr);
return OPERATOR_FINISHED;