2019-02-13 00:52:52 +01:00
|
|
|
//
|
|
|
|
// Created by Syméon on 12/02/2019.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef FEIS_HISTORY_H
|
|
|
|
#define FEIS_HISTORY_H
|
|
|
|
|
|
|
|
#include <stack>
|
|
|
|
#include <optional>
|
|
|
|
|
2019-03-02 13:47:26 +01:00
|
|
|
/*
|
|
|
|
* History implemented this way :
|
|
|
|
*
|
|
|
|
* last action -> * <- back of next_actions
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* * <- front of next_actions
|
|
|
|
*
|
|
|
|
* given state of stuff -> x
|
|
|
|
*
|
|
|
|
* cause of current state -> * <- front of previous_actions
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* first action ever done -> * <- back of previous_actions
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2019-02-13 00:52:52 +01:00
|
|
|
template<typename T>
|
|
|
|
class History {
|
|
|
|
public:
|
|
|
|
|
2019-03-02 13:47:26 +01:00
|
|
|
/*
|
|
|
|
* we cannot undo the very first action, which in my case corresponds to opening a chart
|
|
|
|
*/
|
2019-02-13 00:52:52 +01:00
|
|
|
std::optional<T> get_previous() {
|
2019-03-02 13:47:26 +01:00
|
|
|
if (previous_actions.size() == 1) {
|
2019-02-13 00:52:52 +01:00
|
|
|
return {};
|
|
|
|
} else {
|
2019-03-02 13:47:26 +01:00
|
|
|
T elt = previous_actions.front();
|
|
|
|
next_actions.push_front(elt);
|
|
|
|
previous_actions.pop_front();
|
|
|
|
return elt;
|
2019-02-13 00:52:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<T> get_next() {
|
2019-03-02 13:47:26 +01:00
|
|
|
if (next_actions.empty()) {
|
2019-02-13 00:52:52 +01:00
|
|
|
return {};
|
|
|
|
} else {
|
2019-03-02 13:47:26 +01:00
|
|
|
T elt = next_actions.front();
|
|
|
|
previous_actions.push_front(elt);
|
|
|
|
next_actions.pop_front();
|
|
|
|
return elt;
|
2019-02-13 00:52:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void push(const T& elt) {
|
2019-03-02 13:47:26 +01:00
|
|
|
previous_actions.push_front(elt);
|
|
|
|
if (not next_actions.empty()) {
|
|
|
|
next_actions.clear();
|
2019-02-13 00:52:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 13:47:26 +01:00
|
|
|
void display(std::function<std::string(T)> printer) {
|
|
|
|
if (ImGui::Begin("History")) {
|
|
|
|
ImGui::Indent();
|
|
|
|
for (auto it = next_actions.crbegin(); it != next_actions.crend(); ++it) {
|
|
|
|
ImGui::TextUnformatted(printer(*it).c_str());
|
|
|
|
}
|
|
|
|
ImGui::Unindent();
|
|
|
|
if (previous_actions.empty()) {
|
|
|
|
ImGui::Bullet(); ImGui::TextDisabled("(empty)");
|
|
|
|
} else {
|
|
|
|
auto it = previous_actions.cbegin();
|
|
|
|
ImGui::Bullet(); ImGui::TextUnformatted(printer(*it).c_str());
|
|
|
|
ImGui::Indent();
|
|
|
|
++it;
|
|
|
|
while (it != previous_actions.cend()) {
|
|
|
|
ImGui::TextUnformatted(printer(*it).c_str());
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
ImGui::Unindent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
2019-02-13 00:52:52 +01:00
|
|
|
|
|
|
|
private:
|
2019-03-02 13:47:26 +01:00
|
|
|
std::deque<T> previous_actions;
|
|
|
|
std::deque<T> next_actions;
|
2019-02-13 00:52:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif //FEIS_HISTORY_H
|