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();
|
||||
};
|
||||
Reference in new issue
Block a user