2023-11-25 12:43:48 +01:00
|
|
|
#include <hex/providers/undo_redo/stack.hpp>
|
|
|
|
#include <hex/providers/undo_redo/operations/operation_group.hpp>
|
|
|
|
|
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
|
|
|
|
#include <wolv/utils/guards.hpp>
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
#include <atomic>
|
2023-11-25 12:43:48 +01:00
|
|
|
|
|
|
|
namespace hex::prv::undo {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
std::atomic<bool> s_locked;
|
2023-11-25 12:43:48 +01:00
|
|
|
std::mutex s_mutex;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Stack::Stack(Provider *provider) : m_provider(provider) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Stack::undo(u32 count) {
|
|
|
|
std::scoped_lock lock(s_mutex);
|
|
|
|
|
|
|
|
s_locked = true;
|
|
|
|
ON_SCOPE_EXIT { s_locked = false; };
|
|
|
|
|
|
|
|
// If there are no operations, we can't undo anything.
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_undoStack.empty())
|
2023-11-25 12:43:48 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (u32 i = 0; i < count; i += 1) {
|
|
|
|
// If we reached the start of the list, we can't undo anymore.
|
|
|
|
if (!this->canUndo()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move last element from the undo stack to the redo stack
|
2023-12-19 13:10:25 +01:00
|
|
|
m_redoStack.emplace_back(std::move(m_undoStack.back()));
|
|
|
|
m_redoStack.back()->undo(m_provider);
|
|
|
|
m_undoStack.pop_back();
|
2023-11-25 12:43:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stack::redo(u32 count) {
|
|
|
|
std::scoped_lock lock(s_mutex);
|
|
|
|
|
|
|
|
s_locked = true;
|
|
|
|
ON_SCOPE_EXIT { s_locked = false; };
|
|
|
|
|
|
|
|
// If there are no operations, we can't redo anything.
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_redoStack.empty())
|
2023-11-25 12:43:48 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (u32 i = 0; i < count; i += 1) {
|
|
|
|
// If we reached the end of the list, we can't redo anymore.
|
|
|
|
if (!this->canRedo()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move last element from the undo stack to the redo stack
|
2023-12-19 13:10:25 +01:00
|
|
|
m_undoStack.emplace_back(std::move(m_redoStack.back()));
|
|
|
|
m_undoStack.back()->redo(m_provider);
|
|
|
|
m_redoStack.pop_back();
|
2023-11-25 12:43:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void Stack::groupOperations(u32 count, const UnlocalizedString &unlocalizedName) {
|
2023-11-25 12:43:48 +01:00
|
|
|
if (count <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto operation = std::make_unique<OperationGroup>(unlocalizedName);
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
i64 startIndex = std::max<i64>(0, m_undoStack.size() - count);
|
2023-11-25 12:43:48 +01:00
|
|
|
|
|
|
|
// Move operations from our stack to the group in the same order they were added
|
|
|
|
for (u32 i = 0; i < count; i += 1) {
|
|
|
|
i64 index = startIndex + i;
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
operation->addOperation(std::move(m_undoStack[index]));
|
2023-11-25 12:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the empty operations from the stack
|
2023-12-19 13:10:25 +01:00
|
|
|
m_undoStack.resize(startIndex);
|
2023-11-25 12:43:48 +01:00
|
|
|
this->add(std::move(operation));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stack::apply(const Stack &otherStack) {
|
|
|
|
for (const auto &operation : otherStack.m_undoStack) {
|
|
|
|
this->add(operation->clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Stack::add(std::unique_ptr<Operation> &&operation) {
|
|
|
|
// If we're already inside of an undo/redo operation, ignore new operations being added
|
|
|
|
if (s_locked)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
s_locked = true;
|
|
|
|
ON_SCOPE_EXIT { s_locked = false; };
|
|
|
|
|
|
|
|
std::scoped_lock lock(s_mutex);
|
|
|
|
|
|
|
|
// Clear the redo stack
|
2023-12-19 13:10:25 +01:00
|
|
|
m_redoStack.clear();
|
2023-11-25 12:43:48 +01:00
|
|
|
|
|
|
|
// Insert the new operation at the end of the list
|
2023-12-19 13:10:25 +01:00
|
|
|
m_undoStack.emplace_back(std::move(operation));
|
2023-11-25 12:43:48 +01:00
|
|
|
|
|
|
|
// Do the operation
|
2023-12-19 13:10:25 +01:00
|
|
|
this->getLastOperation()->redo(m_provider);
|
2023-11-25 12:43:48 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stack::canUndo() const {
|
2023-12-19 13:10:25 +01:00
|
|
|
return !m_undoStack.empty();
|
2023-11-25 12:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Stack::canRedo() const {
|
2023-12-19 13:10:25 +01:00
|
|
|
return !m_redoStack.empty();
|
2023-11-25 12:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|