Make Document a Snapshot instead of aliasing one
The state undo restores is Document's own members, and Snapshots takes it by reference. The DocumentData base, the reference members and the placement-new move assignment are gone.
This commit is contained in:
1 parent
544ab3cac9
commit
250adcb6e9
5 files changed
+37
-55
No files matched your search
+4
-15
@@ -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<Uids(Model&)>& operation)
|
||||
@@ -1279,7 +1268,7 @@ void Document::libraries_add(const std::vector<std::filesystem::path>& 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);
|
||||
|
||||
+8
-15
@@ -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<ChildStep> 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<ChildStep> childSteps;
|
||||
std::unordered_map<int, uint64_t> spritesheetHashes;
|
||||
std::unordered_map<int, uint64_t> spritesheetSaveHashes;
|
||||
std::unordered_map<int, uint64_t> spritesheetAutosaveHashes;
|
||||
@@ -96,11 +99,6 @@ struct DocumentData
|
||||
mutable std::map<std::pair<std::uint64_t, int>, 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();
|
||||
|
||||
+5
-5
@@ -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();
|
||||
|
||||
+4
-5
@@ -60,14 +60,13 @@ class Snapshots
|
||||
public:
|
||||
SnapshotStack undoStack = {};
|
||||
SnapshotStack redoStack = {};
|
||||
Snapshot current = {};
|
||||
std::optional<Snapshot> pending;
|
||||
std::optional<Reference> 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();
|
||||
};
|
||||
+16
-15
@@ -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<std::uint64_t> hashes{model_hash(snapshots.current.model)};
|
||||
std::vector<std::uint64_t> 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<Reference>{{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});
|
||||
}
|
||||
Reference in new issue
Block a user