blob: d7799b31cb5d3a3a7c2e95a8323aae4b4e9ab2b8 (
plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#include "states.h"
// StateStack
StateStack::StateStack(State::Context context)
: pendingOps_()
, context_(context)
{
}
void StateStack::update(sf::Time dt){
for(State::Ptr &s: states_) {
s->update(dt);
}
}
void StateStack::render(){
for(State::Ptr &s: states_) {
s->render();
}
}
void StateStack::handleEvent(const sf::Event &event){
for(State::Ptr &s: states_) {
s->handleEvent(event);
}
}
void StateStack::requestOp(StateStack::Op op){
pendingOps_.push_back(op);
}
void StateStack::push(States::Id state){
states_.push_back(std::move(createState(state)));
}
void StateStack::pop(){
states_.pop_back();
}
void StateStack::clear(){
states_.clear();
}
void StateStack::applyPending(){
for(StateStack::Op &op: pendingOps_) {
switch(op.id){
case StateStack::OpId::Push:
push(op.state);
break;
case StateStack::OpId::Pop:
pop();
break;
case StateStack::OpId::Clear:
clear();
break;
}
}
pendingOps_.clear();
}
bool StateStack::isEmpty() const{
return states_.empty();
}
State::Ptr StateStack::createState(States::Id id){
return stateFactory_[id]();
}
// State
State::State(StateStack &stack, State::Context context)
: context_(context)
, stack_(&stack)
{}
void State::requestPush(States::Id state_id){
StateStack::Op op {StateStack::OpId::Push, state_id};
stack_->requestOp(op);
}
void State::requestPop(){
StateStack::Op op {StateStack::OpId::Push};
stack_->requestOp(op);
}
void State::requestClear(){
StateStack::Op op {StateStack::OpId::Clear};
stack_->requestOp(op);
}
|