2019-02-13 00:52:52 +01:00
|
|
|
#ifndef FEIS_HISTORY_H
|
|
|
|
#define FEIS_HISTORY_H
|
|
|
|
|
2022-04-01 01:41:47 +02:00
|
|
|
#include <deque>
|
2019-03-28 02:16:29 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <imgui/imgui.h>
|
2021-12-31 14:59:39 +01:00
|
|
|
#include <optional>
|
|
|
|
#include <stack>
|
2019-02-13 00:52:52 +01:00
|
|
|
|
2019-03-02 13:47:26 +01:00
|
|
|
/*
|
|
|
|
* History implemented this way :
|
|
|
|
*
|
|
|
|
* last action -> * <- back of next_actions
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* * <- front of next_actions
|
|
|
|
*
|
2022-04-01 01:41:47 +02:00
|
|
|
* given state of stuff -> o
|
2019-03-02 13:47:26 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
/*
|
2021-12-31 14:59:39 +01:00
|
|
|
* we cannot undo the very first action, which in F.E.I.S corresponds to
|
|
|
|
* opening a chart
|
2019-03-02 13:47:26 +01:00
|
|
|
*/
|
2022-04-01 01:41:47 +02:00
|
|
|
std::optional<T> pop_previous() {
|
2019-03-02 13:47:26 +01:00
|
|
|
if (previous_actions.size() == 1) {
|
2019-02-13 00:52:52 +01:00
|
|
|
return {};
|
|
|
|
} else {
|
2022-04-01 01:41:47 +02:00
|
|
|
auto elt = previous_actions.front();
|
2019-03-02 13:47:26 +01:00
|
|
|
next_actions.push_front(elt);
|
|
|
|
previous_actions.pop_front();
|
|
|
|
return elt;
|
2019-02-13 00:52:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 01:41:47 +02:00
|
|
|
std::optional<T> pop_next() {
|
2019-03-02 13:47:26 +01:00
|
|
|
if (next_actions.empty()) {
|
2019-02-13 00:52:52 +01:00
|
|
|
return {};
|
|
|
|
} else {
|
2022-04-01 01:41:47 +02:00
|
|
|
auto elt = next_actions.front();
|
2019-03-02 13:47:26 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 02:20:07 +01:00
|
|
|
void display() {
|
2019-03-02 13:47:26 +01:00
|
|
|
if (ImGui::Begin("History")) {
|
|
|
|
ImGui::Indent();
|
|
|
|
for (auto it = next_actions.crbegin(); it != next_actions.crend(); ++it) {
|
2022-03-23 02:20:07 +01:00
|
|
|
ImGui::TextUnformatted((*it)->get_message().c_str());
|
2019-03-02 13:47:26 +01:00
|
|
|
}
|
|
|
|
ImGui::Unindent();
|
|
|
|
if (previous_actions.empty()) {
|
2021-12-31 14:59:39 +01:00
|
|
|
ImGui::Bullet();
|
|
|
|
ImGui::TextDisabled("(empty)");
|
2019-03-02 13:47:26 +01:00
|
|
|
} else {
|
|
|
|
auto it = previous_actions.cbegin();
|
2021-12-31 14:59:39 +01:00
|
|
|
ImGui::Bullet();
|
2022-03-23 02:20:07 +01:00
|
|
|
ImGui::TextUnformatted((*it)->get_message().c_str());
|
2019-03-02 13:47:26 +01:00
|
|
|
ImGui::Indent();
|
|
|
|
++it;
|
|
|
|
while (it != previous_actions.cend()) {
|
2022-03-23 02:20:07 +01:00
|
|
|
ImGui::TextUnformatted((*it)->get_message().c_str());
|
2019-03-02 13:47:26 +01:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
ImGui::Unindent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
bool empty() { return previous_actions.size() <= 1; }
|
2019-02-13 00:52:52 +01:00
|
|
|
|
2022-04-01 01:41:47 +02:00
|
|
|
void mark_as_saved() {
|
|
|
|
if (next_actions.empty()) {
|
|
|
|
last_saved_action = nullptr;
|
|
|
|
} else {
|
2022-04-07 00:14:01 +02:00
|
|
|
last_saved_action = &previous_actions.front();
|
2022-04-01 01:41:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:10:09 +02:00
|
|
|
bool current_state_is_saved() const {
|
2022-04-07 00:14:01 +02:00
|
|
|
return last_saved_action == &previous_actions.front();
|
2022-04-01 01:41:47 +02:00
|
|
|
};
|
|
|
|
|
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;
|
2022-04-01 01:41:47 +02:00
|
|
|
T* last_saved_action = nullptr;
|
2019-02-13 00:52:52 +01:00
|
|
|
};
|
|
|
|
|
2021-12-31 14:59:39 +01:00
|
|
|
#endif // FEIS_HISTORY_H
|