-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewerStateManager.cpp
68 lines (54 loc) · 1.68 KB
/
ViewerStateManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "ViewerStateManager.h"
#include <stdexcept>
Eng::ViewerStateManager::~ViewerStateManager() { Clear(); }
Eng::ViewerState *Eng::ViewerStateManager::Peek() {
if (states_.empty()) {
return {};
} else {
return states_.back().get();
}
}
void Eng::ViewerStateManager::Push(std::unique_ptr<ViewerState> &&state) {
if (!states_.empty()) {
states_.back()->Exit();
}
states_.emplace_back(std::move(state))->Enter();
}
std::unique_ptr<Eng::ViewerState> Eng::ViewerStateManager::Pop() {
if (states_.empty()) {
throw std::runtime_error("Attempted to pop from an empty game state stack");
}
auto popped = std::move(states_.back());
popped->Exit();
states_.pop_back();
if (!states_.empty()) {
states_.back()->Enter();
}
return popped;
}
void Eng::ViewerStateManager::PopLater() { pop_later_ = true; }
Eng::ViewerState *Eng::ViewerStateManager::Switch(std::unique_ptr<ViewerState> &&state) {
ViewerState *current_state = Peek();
if (current_state) {
Pop();
}
Push(std::move(state));
return current_state;
}
void Eng::ViewerStateManager::Clear() {
while (!states_.empty()) {
Pop();
}
}
void Eng::ViewerStateManager::UpdateFixed(uint64_t dt_us) {
if (pop_later_) {
Pop();
pop_later_ = false;
}
states_.back()->UpdateFixed(dt_us);
}
void Eng::ViewerStateManager::UpdateAnim(uint64_t dt_us) { states_.back()->UpdateAnim(dt_us); }
void Eng::ViewerStateManager::Draw() { states_.back()->Draw(); }
void Eng::ViewerStateManager::HandleInput(const input_event_t &evt, const std::vector<bool> &keys_state) {
states_.back()->HandleInput(evt, keys_state);
}