F.E.I.S/src/history.hpp

117 lines
3.0 KiB
C++
Raw Normal View History

#ifndef FEIS_HISTORY_H
#define FEIS_HISTORY_H
2022-04-09 00:54:06 +02:00
#include <imgui/imgui.h>
#include <deque>
#include <functional>
2022-04-09 00:54:06 +02:00
#include <memory>
2021-12-31 14:59:39 +01:00
#include <optional>
#include <stack>
2022-04-09 00:54:06 +02:00
#include "history_item.hpp"
/*
* History implemented this way :
*
* last action -> * <- back of next_actions
* *
* *
* * <- front of next_actions
*
* given state of stuff -> o
*
* cause of current state -> * <- front of previous_actions
* *
* *
* first action ever done -> * <- back of previous_actions
*
*/
class History {
2022-04-09 00:54:06 +02:00
using item = std::shared_ptr<HistoryItem>;
public:
/*
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
*/
2022-04-09 00:54:06 +02:00
std::optional<item> pop_previous() {
if (previous_actions.size() == 1) {
return {};
} else {
auto elt = previous_actions.front();
next_actions.push_front(elt);
previous_actions.pop_front();
return elt;
}
}
2022-04-09 00:54:06 +02:00
std::optional<item> pop_next() {
if (next_actions.empty()) {
return {};
} else {
auto elt = next_actions.front();
previous_actions.push_front(elt);
next_actions.pop_front();
return elt;
}
}
2022-04-09 00:54:06 +02:00
void push(const item& elt) {
previous_actions.push_front(elt);
if (not next_actions.empty()) {
next_actions.clear();
}
}
2022-03-23 02:20:07 +01:00
void display() {
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());
}
ImGui::Unindent();
if (previous_actions.empty()) {
2021-12-31 14:59:39 +01:00
ImGui::Bullet();
ImGui::TextDisabled("(empty)");
} 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());
ImGui::Indent();
++it;
while (it != previous_actions.cend()) {
2022-03-23 02:20:07 +01:00
ImGui::TextUnformatted((*it)->get_message().c_str());
++it;
}
ImGui::Unindent();
}
}
ImGui::End();
}
2021-12-31 14:59:39 +01:00
bool empty() { return previous_actions.size() <= 1; }
void mark_as_saved() {
if (next_actions.empty()) {
2022-04-09 00:54:06 +02:00
last_saved_action.reset();
} else {
2022-04-09 00:54:06 +02:00
last_saved_action = previous_actions.front();
}
}
2022-04-02 04:10:09 +02:00
bool current_state_is_saved() const {
2022-04-09 00:54:06 +02:00
if (not last_saved_action) {
return false;
} else {
return *last_saved_action == previous_actions.front();
}
};
private:
2022-04-09 00:54:06 +02:00
std::deque<item> previous_actions;
std::deque<item> next_actions;
std::optional<item> last_saved_action;
};
2021-12-31 14:59:39 +01:00
#endif // FEIS_HISTORY_H