diff --git a/src/document.cpp b/src/document.cpp index ee706ed..6b74e6b 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -206,17 +206,6 @@ Document::Document(const std::filesystem::path& path, bool isNew, std::string* e change(); } -Document::Document(Document&& other) noexcept : DocumentData(std::move(other)), editTarget(other.editTarget) -{} - -Document& Document::operator=(Document&& other) noexcept -{ - if (this != &other) - this->~Document(); - new (this) Document(std::move(other)); - return *this; -} - bool Document::save(const std::filesystem::path& path, std::string* errorString, Options options) { auto absolutePath = !path.empty() ? path : this->path; @@ -715,13 +704,13 @@ void Document::change() assets_sync(); spritesheet_hashes_sync(); - snapshots.commit(); + snapshots.commit(*this); index = UidIndex(model); } void Document::edit_begin(StringType label) { - snapshots.push(localize.get(label)); + snapshots.push(*this, localize.get(label)); } Uids Document::edit_run(StringType label, const std::function& operation) @@ -1279,7 +1268,7 @@ void Document::libraries_add(const std::vector& paths) void Document::undo() { - if (!snapshots.undo()) + if (!snapshots.undo(*this)) return; frame_time_sync(*this); toast_log(Level::INFO, TOAST_UNDO, message); @@ -1288,7 +1277,7 @@ void Document::undo() void Document::redo() { - if (!snapshots.redo()) + if (!snapshots.redo(*this)) return; frame_time_sync(*this); toast_log(Level::INFO, TOAST_REDO, message); diff --git a/src/document.h b/src/document.h index 778172c..c251df6 100644 --- a/src/document.h +++ b/src/document.h @@ -52,8 +52,10 @@ struct ChildStep int childAnimationIndex = -1; }; -struct DocumentData +// An open document: the state undo restores (as a Snapshot), its history, assets, libraries and view. +class Document : public Snapshot { +public: std::filesystem::path path; uint64_t tabId = 0; Snapshots snapshots = {}; @@ -76,9 +78,10 @@ struct DocumentData bool isValid = true; bool isOpen = true; bool isForceDirty = false; + // Asked this session to keep SVG spritesheets in a Rebirth document. bool isVectorKept = false; - std::vector childSteps{}; // the steps that led to the animation being edited (the breadcrumb) // asked - // this session to keep SVG spritesheets in a Rebirth document + // The steps that led to the animation being edited (the breadcrumb). + std::vector childSteps; std::unordered_map spritesheetHashes; std::unordered_map spritesheetSaveHashes; std::unordered_map spritesheetAutosaveHashes; @@ -96,11 +99,6 @@ struct DocumentData mutable std::map, Image> vectorImages; bool isAnimationPreviewSet = false; bool isSpritesheetEditorSet = false; -}; - -class Document : public DocumentData -{ -public: enum class FrameReferenceFallback { NONE, @@ -115,19 +113,14 @@ public: SPRITESHEET }; - Snapshot& current = snapshots.current; -#define X(type, name) type& name = current.name; - SNAPSHOT_STEP_STATE_FIELDS -#undef X - std::string& message = current.message; EditTarget editTarget = EditTarget::NONE; UidIndex index = {}; Document(const std::filesystem::path&, bool = false, std::string* = nullptr); Document(const Document&) = delete; Document& operator=(const Document&) = delete; - Document(Document&&) noexcept; - Document& operator=(Document&&) noexcept; + Document(Document&&) = default; + Document& operator=(Document&&) = default; bool save(const std::filesystem::path& = {}, std::string* = nullptr, Options = {}); bool file_write(const std::filesystem::path&, std::string* = nullptr, Options = {}); void assets_sync(); diff --git a/src/snapshots.cpp b/src/snapshots.cpp index 0c6722f..de7e3d5 100644 --- a/src/snapshots.cpp +++ b/src/snapshots.cpp @@ -93,16 +93,16 @@ void SnapshotStack::max_size_set(int value) } // Starts an undoable edit: remembers the state before it (cheap. Animations are shared). -void Snapshots::push(const std::string& message) +void Snapshots::push(Snapshot& current, const std::string& message) { if (pending || pendingFocus) - commit(); + commit(current); pendingFocus = focus_get(current); current.message = message; pending = current; } -void Snapshots::commit() +void Snapshots::commit(Snapshot& current) { if (pendingFocus) selection_follow(current, *std::exchange(pendingFocus, std::nullopt)); @@ -115,7 +115,7 @@ void Snapshots::commit() redoStack.clear(); } -bool Snapshots::undo() +bool Snapshots::undo(Snapshot& current) { pending.reset(); pendingFocus.reset(); @@ -129,7 +129,7 @@ bool Snapshots::undo() return true; } -bool Snapshots::redo() +bool Snapshots::redo(Snapshot& current) { pending.reset(); pendingFocus.reset(); diff --git a/src/snapshots.h b/src/snapshots.h index e02c885..d91a15c 100644 --- a/src/snapshots.h +++ b/src/snapshots.h @@ -60,14 +60,13 @@ class Snapshots public: SnapshotStack undoStack = {}; SnapshotStack redoStack = {}; - Snapshot current = {}; std::optional pending; std::optional pendingFocus; - void push(const std::string&); - void commit(); - bool undo(); - bool redo(); + void push(Snapshot&, const std::string&); + void commit(Snapshot&); + bool undo(Snapshot&); + bool redo(Snapshot&); void reset(); void apply_limit(); }; diff --git a/tests/undo.cpp b/tests/undo.cpp index 67493e7..e98298e 100644 --- a/tests/undo.cpp +++ b/tests/undo.cpp @@ -85,33 +85,34 @@ TEST_CASE("undo and redo restore every state of a random edit walk") { INFO("fixture: ", name); Snapshots snapshots = {}; - snapshots.current.model = model_load(file_load(CORPUS_DIR / name)); + Snapshot current = {}; + current.model = model_load(file_load(CORPUS_DIR / name)); std::mt19937 random(WALK_SEED); - std::vector hashes{model_hash(snapshots.current.model)}; + std::vector hashes{model_hash(current.model)}; for (int step = 0; step < WALK_STEPS; ++step) { - snapshots.push("Edit"); - if (!edit_random_apply(snapshots.current.model, random)) + snapshots.push(current, "Edit"); + if (!edit_random_apply(current.model, random)) break; auto count = snapshots.undoStack.size(); - snapshots.commit(); + snapshots.commit(current); if (snapshots.undoStack.size() != count) - hashes.push_back(model_hash(snapshots.current.model)); + hashes.push_back(model_hash(current.model)); } REQUIRE(hashes.size() > 1); for (auto it = hashes.rbegin() + 1; it != hashes.rend(); ++it) { - REQUIRE(snapshots.undo()); - CHECK(model_hash(snapshots.current.model) == *it); + REQUIRE(snapshots.undo(current)); + CHECK(model_hash(current.model) == *it); } - CHECK_FALSE(snapshots.undo()); + CHECK_FALSE(snapshots.undo(current)); for (auto it = hashes.begin() + 1; it != hashes.end(); ++it) { - REQUIRE(snapshots.redo()); - CHECK(model_hash(snapshots.current.model) == *it); + REQUIRE(snapshots.redo(current)); + CHECK(model_hash(current.model) == *it); } } } @@ -119,21 +120,21 @@ TEST_CASE("undo and redo restore every state of a random edit walk") TEST_CASE("selection follows its frame through an edit and its undo") { Snapshots snapshots = {}; - auto& current = snapshots.current; + Snapshot current = {}; current.model = model_load(file_load(CORPUS_DIR / "02_items.anm2")); selection_focus_set(current.selection, current.model, {0, LAYER, 0, 1}); selection_references_set(current.selection, current.model, SelectionKind::FRAMES, {{0, LAYER, 0, 1}}); - snapshots.push("Insert"); + snapshots.push(current, "Insert"); frame_insert(current.model, {0, LAYER, 0, -1}, 0); auto& track = *current.model.track_edit(Reference{0, LAYER, 0}); std::rotate(track.frames.begin(), track.frames.end() - 1, track.frames.end()); - snapshots.commit(); + snapshots.commit(current); UidIndex index(current.model); CHECK(selection_focus_get(current.selection, index) == Reference{0, LAYER, 0, 2}); CHECK(selection_references_get(current.selection, index, SelectionKind::FRAMES) == std::set{{0, LAYER, 0, 2}}); - REQUIRE(snapshots.undo()); + REQUIRE(snapshots.undo(current)); CHECK(selection_focus_get(current.selection, UidIndex(current.model)) == Reference{0, LAYER, 0, 1}); }