Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
prism/tools/common/include/undostack.hpp

53 lines
1.1 KiB
C++
Raw Normal View History

2020-08-11 12:07:21 -04:00
#pragma once
2022-08-15 11:10:06 -04:00
#include <memory>
2020-08-11 12:07:21 -04:00
#include <string>
#include <vector>
class Command {
public:
std::string stored_name;
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
std::string get_name() {
2022-08-15 11:10:06 -04:00
if (stored_name.empty())
2020-08-11 12:07:21 -04:00
stored_name = fetch_name();
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
return stored_name;
}
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
virtual std::string fetch_name() = 0;
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
virtual void undo() = 0;
virtual void execute() = 0;
};
class UndoStack {
public:
2022-08-15 11:10:06 -04:00
template<class T> T& new_command() {
2020-08-11 12:07:21 -04:00
// if we are in the middle of the stack currently, wipe out everything and start fresh
// TODO: only do it up to the point we're modifing
2022-08-15 11:10:06 -04:00
if (stack_position != (command_stack.size() - 1)) {
2020-08-11 12:07:21 -04:00
command_stack.clear();
stack_position = -1;
}
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
stack_position++;
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
return static_cast<T&>(*command_stack.emplace_back(std::make_unique<T>()));
}
2022-08-15 11:10:06 -04:00
// void push_command(Command* command);
2020-08-11 12:07:21 -04:00
void undo();
void redo();
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
// useful for undo
Command* get_last_command();
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
// useful for redo
Command* get_next_command();
2022-08-15 11:10:06 -04:00
2020-08-11 12:07:21 -04:00
std::vector<std::unique_ptr<Command>> command_stack;
int stack_position = -1;
};