Add document frame move coverage
This commit is contained in:
@@ -55,6 +55,15 @@ namespace {
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
[[nodiscard]] pp::foundation::Status validate_frame_index(std::size_t index, std::size_t frame_count) noexcept
|
||||
{
|
||||
if (index >= frame_count) {
|
||||
return pp::foundation::Status::out_of_range("frame index is outside the document");
|
||||
}
|
||||
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pp::foundation::Result<CanvasDocument> CanvasDocument::create(DocumentConfig config)
|
||||
@@ -96,6 +105,15 @@ std::size_t CanvasDocument::active_frame_index() const noexcept
|
||||
return active_frame_index_;
|
||||
}
|
||||
|
||||
std::uint64_t CanvasDocument::animation_duration_ms() const noexcept
|
||||
{
|
||||
std::uint64_t duration = 0;
|
||||
for (const auto& frame : frames_) {
|
||||
duration += frame.duration_ms;
|
||||
}
|
||||
return duration;
|
||||
}
|
||||
|
||||
std::span<const Layer> CanvasDocument::layers() const noexcept
|
||||
{
|
||||
return layers_;
|
||||
@@ -265,9 +283,10 @@ pp::foundation::Result<std::size_t> CanvasDocument::add_frame(std::uint32_t dura
|
||||
|
||||
pp::foundation::Result<std::size_t> CanvasDocument::duplicate_frame(std::size_t index)
|
||||
{
|
||||
if (index >= frames_.size()) {
|
||||
const auto index_status = validate_frame_index(index, frames_.size());
|
||||
if (!index_status.ok()) {
|
||||
return pp::foundation::Result<std::size_t>::failure(
|
||||
pp::foundation::Status::out_of_range("frame index is outside the document"));
|
||||
index_status);
|
||||
}
|
||||
|
||||
if (frames_.size() >= max_frame_count) {
|
||||
@@ -283,8 +302,9 @@ pp::foundation::Result<std::size_t> CanvasDocument::duplicate_frame(std::size_t
|
||||
|
||||
pp::foundation::Status CanvasDocument::remove_frame(std::size_t index)
|
||||
{
|
||||
if (index >= frames_.size()) {
|
||||
return pp::foundation::Status::out_of_range("frame index is outside the document");
|
||||
const auto index_status = validate_frame_index(index, frames_.size());
|
||||
if (!index_status.ok()) {
|
||||
return index_status;
|
||||
}
|
||||
|
||||
if (frames_.size() == 1U) {
|
||||
@@ -301,10 +321,36 @@ pp::foundation::Status CanvasDocument::remove_frame(std::size_t index)
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status CanvasDocument::move_frame(std::size_t from, std::size_t to)
|
||||
{
|
||||
if (from >= frames_.size() || to >= frames_.size()) {
|
||||
return pp::foundation::Status::out_of_range("frame index is outside the document");
|
||||
}
|
||||
|
||||
if (from == to) {
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
const auto frame = frames_[from];
|
||||
frames_.erase(frames_.begin() + static_cast<std::ptrdiff_t>(from));
|
||||
frames_.insert(frames_.begin() + static_cast<std::ptrdiff_t>(to), frame);
|
||||
|
||||
if (active_frame_index_ == from) {
|
||||
active_frame_index_ = to;
|
||||
} else if (from < active_frame_index_ && active_frame_index_ <= to) {
|
||||
--active_frame_index_;
|
||||
} else if (to <= active_frame_index_ && active_frame_index_ < from) {
|
||||
++active_frame_index_;
|
||||
}
|
||||
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status CanvasDocument::set_frame_duration(std::size_t index, std::uint32_t duration_ms) noexcept
|
||||
{
|
||||
if (index >= frames_.size()) {
|
||||
return pp::foundation::Status::out_of_range("frame index is outside the document");
|
||||
const auto index_status = validate_frame_index(index, frames_.size());
|
||||
if (!index_status.ok()) {
|
||||
return index_status;
|
||||
}
|
||||
|
||||
if (duration_ms < min_frame_duration_ms) {
|
||||
@@ -317,8 +363,9 @@ pp::foundation::Status CanvasDocument::set_frame_duration(std::size_t index, std
|
||||
|
||||
pp::foundation::Status CanvasDocument::set_active_frame(std::size_t index) noexcept
|
||||
{
|
||||
if (index >= frames_.size()) {
|
||||
return pp::foundation::Status::out_of_range("frame index is outside the document");
|
||||
const auto index_status = validate_frame_index(index, frames_.size());
|
||||
if (!index_status.ok()) {
|
||||
return index_status;
|
||||
}
|
||||
|
||||
active_frame_index_ = index;
|
||||
|
||||
@@ -44,6 +44,7 @@ public:
|
||||
[[nodiscard]] std::uint32_t height() const noexcept;
|
||||
[[nodiscard]] std::size_t active_layer_index() const noexcept;
|
||||
[[nodiscard]] std::size_t active_frame_index() const noexcept;
|
||||
[[nodiscard]] std::uint64_t animation_duration_ms() const noexcept;
|
||||
[[nodiscard]] std::span<const Layer> layers() const noexcept;
|
||||
[[nodiscard]] std::span<const AnimationFrame> frames() const noexcept;
|
||||
|
||||
@@ -59,6 +60,7 @@ public:
|
||||
[[nodiscard]] pp::foundation::Result<std::size_t> add_frame(std::uint32_t duration_ms);
|
||||
[[nodiscard]] pp::foundation::Result<std::size_t> duplicate_frame(std::size_t index);
|
||||
[[nodiscard]] pp::foundation::Status remove_frame(std::size_t index);
|
||||
[[nodiscard]] pp::foundation::Status move_frame(std::size_t from, std::size_t to);
|
||||
[[nodiscard]] pp::foundation::Status set_frame_duration(std::size_t index, std::uint32_t duration_ms) noexcept;
|
||||
[[nodiscard]] pp::foundation::Status set_active_frame(std::size_t index) noexcept;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user