diff --git a/compile_commands.json b/compile_commands.json index bc0dce8..e2be980 120000 --- a/compile_commands.json +++ b/compile_commands.json @@ -1 +1 @@ -/home/anon/sda/Personal/Repos/anm2ed/out/build/linux-debug/compile_commands.json \ No newline at end of file +/home/anon/sda/Personal/Repos/anm2ed/out/build/linux-release/compile_commands.json \ No newline at end of file diff --git a/src/document.cpp b/src/document.cpp index 13493de..6b9c307 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -1,5 +1,6 @@ #include "document.hpp" +#include #include #include @@ -26,6 +27,63 @@ namespace anm2ed return it->second; return 0; } + + void restored_snapshot_sanitize(Document& document) + { + auto& reference = document.reference; + auto& documentAnm2 = document.anm2; + auto& selection = document.frames.selection; + + auto animationCount = (int)documentAnm2.animations.items.size(); + if (animationCount <= 0) + { + reference = {}; + selection.clear(); + document.frameTime = 0.0f; + return; + } + + if (reference.animationIndex < 0 || reference.animationIndex >= animationCount) + reference.animationIndex = std::clamp(reference.animationIndex, 0, animationCount - 1); + + auto item = documentAnm2.item_get(reference.animationIndex, reference.itemType, reference.itemID); + if (!item) + { + reference.itemType = ROOT; + reference.itemID = -1; + item = documentAnm2.item_get(reference.animationIndex, reference.itemType, reference.itemID); + } + + if (!item) + { + reference.frameIndex = -1; + selection.clear(); + document.frameTime = 0.0f; + return; + } + + auto frameCount = (int)item->frames.size(); + for (auto it = selection.begin(); it != selection.end();) + { + if (*it < 0 || *it >= frameCount) + it = selection.erase(it); + else + ++it; + } + + if (frameCount <= 0) + { + reference.frameIndex = -1; + document.frameTime = 0.0f; + return; + } + + if (reference.frameIndex < 0 || reference.frameIndex >= frameCount) + reference.frameIndex = selection.empty() ? std::clamp(reference.frameIndex, 0, frameCount - 1) + : *selection.begin(); + + document.frameTime = item->frame_time_from_index_get(reference.frameIndex); + } } Document::Document(Anm2& anm2, const std::filesystem::path& path) @@ -390,7 +448,8 @@ namespace anm2ed void Document::undo() { - snapshots.undo(); + if (!snapshots.undo()) return; + restored_snapshot_sanitize(*this); toasts.push(std::vformat(localize.get(TOAST_UNDO), std::make_format_args(message))); logger.info(std::vformat(localize.get(TOAST_UNDO, anm2ed::ENGLISH), std::make_format_args(message))); change(Document::ALL); @@ -398,7 +457,8 @@ namespace anm2ed void Document::redo() { - snapshots.redo(); + if (!snapshots.redo()) return; + restored_snapshot_sanitize(*this); toasts.push(std::vformat(localize.get(TOAST_REDO), std::make_format_args(message))); logger.info(std::vformat(localize.get(TOAST_REDO, anm2ed::ENGLISH), std::make_format_args(message))); change(Document::ALL); diff --git a/src/imgui/window/animation_preview.cpp b/src/imgui/window/animation_preview.cpp index cf896fb..0e713ff 100644 --- a/src/imgui/window/animation_preview.cpp +++ b/src/imgui/window/animation_preview.cpp @@ -1132,10 +1132,24 @@ namespace anm2ed::imgui } } - if (isLeftPressed) frame_change_apply({.scaleX = step}, anm2::SUBTRACT); - if (isRightPressed) frame_change_apply({.scaleX = step}, anm2::ADD); - if (isUpPressed) frame_change_apply({.scaleY = step}, anm2::SUBTRACT); - if (isDownPressed) frame_change_apply({.scaleY = step}, anm2::ADD); + if (isSelectedNullRect) + { + if (isLeftPressed) + frame_change_apply({.positionX = step * 0.5f, .scaleX = step}, anm2::SUBTRACT); + if (isRightPressed) + frame_change_apply({.positionX = step * 0.5f, .scaleX = step}, anm2::ADD); + if (isUpPressed) + frame_change_apply({.positionY = step * 0.5f, .scaleY = step}, anm2::SUBTRACT); + if (isDownPressed) + frame_change_apply({.positionY = step * 0.5f, .scaleY = step}, anm2::ADD); + } + else + { + if (isLeftPressed) frame_change_apply({.scaleX = step}, anm2::SUBTRACT); + if (isRightPressed) frame_change_apply({.scaleX = step}, anm2::ADD); + if (isUpPressed) frame_change_apply({.scaleY = step}, anm2::SUBTRACT); + if (isDownPressed) frame_change_apply({.scaleY = step}, anm2::ADD); + } if (isToolDuring) { diff --git a/src/imgui/window/timeline.cpp b/src/imgui/window/timeline.cpp index 71e55a6..6614ab1 100644 --- a/src/imgui/window/timeline.cpp +++ b/src/imgui/window/timeline.cpp @@ -491,7 +491,6 @@ namespace anm2ed::imgui if (!animation) return; if (auto item = animation->item_get(reference.itemType, reference.itemID)) { - document.snapshot(localize.get(EDIT_PASTE_FRAMES)); std::set indices{}; std::string errorString{}; int insertIndex = (int)item->frames.size(); diff --git a/src/snapshots.cpp b/src/snapshots.cpp index 5ffdf6a..098fb73 100644 --- a/src/snapshots.cpp +++ b/src/snapshots.cpp @@ -53,22 +53,26 @@ namespace anm2ed redoStack.clear(); } - void Snapshots::undo() + bool Snapshots::undo() { if (auto snapshot = undoStack.pop()) { redoStack.push(current); current = *snapshot; + return true; } + return false; } - void Snapshots::redo() + bool Snapshots::redo() { if (auto snapshot = redoStack.pop()) { undoStack.push(current); current = *snapshot; + return true; } + return false; } void Snapshots::reset() diff --git a/src/snapshots.hpp b/src/snapshots.hpp index a5fce63..87f5380 100644 --- a/src/snapshots.hpp +++ b/src/snapshots.hpp @@ -63,8 +63,8 @@ namespace anm2ed Snapshot* get(); void push(const Snapshot&); - void undo(); - void redo(); + bool undo(); + bool redo(); void reset(); void apply_limit(); };