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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use ggez;
pub enum SceneSwitch<C, Ev> {
None,
Push(Box<Scene<C, Ev>>),
Replace(Box<Scene<C, Ev>>),
Pop,
}
pub trait Scene<C, Ev> {
fn update(&mut self, gameworld: &mut C) -> SceneSwitch<C, Ev>;
fn draw(&mut self, gameworld: &mut C, ctx: &mut ggez::Context) -> ggez::GameResult<()>;
fn input(&mut self, gameworld: &mut C, event: Ev, started: bool);
fn name(&self) -> &str;
fn draw_previous(&self) -> bool {
false
}
}
impl<C, Ev> SceneSwitch<C, Ev> {
pub fn replace<S>(scene: S) -> Self
where
S: Scene<C, Ev> + 'static,
{
SceneSwitch::Replace(Box::new(scene))
}
pub fn push<S>(scene: S) -> Self
where
S: Scene<C, Ev> + 'static,
{
SceneSwitch::Push(Box::new(scene))
}
}
pub struct SceneStack<C, Ev> {
pub world: C,
scenes: Vec<Box<Scene<C, Ev>>>,
}
impl<C, Ev> SceneStack<C, Ev> {
pub fn new(ctx: &mut ggez::Context, global_state: C) -> Self {
Self {
world: global_state,
scenes: Vec::new(),
}
}
pub fn push(&mut self, scene: Box<Scene<C, Ev>>) {
self.scenes.push(scene)
}
pub fn pop(&mut self) -> Box<Scene<C, Ev>> {
self.scenes
.pop()
.expect("ERROR: Popped an empty scene stack.")
}
pub fn current(&self) -> &Scene<C, Ev> {
&**self.scenes
.last()
.expect("ERROR: Tried to get current scene of an empty scene stack.")
}
pub fn switch(&mut self, next_scene: SceneSwitch<C, Ev>) -> Option<Box<Scene<C, Ev>>> {
match next_scene {
SceneSwitch::None => None,
SceneSwitch::Pop => {
let s = self.pop();
Some(s)
}
SceneSwitch::Push(s) => {
self.push(s);
None
}
SceneSwitch::Replace(s) => {
let old_scene = self.pop();
self.push(s);
Some(old_scene)
}
}
}
pub fn update(&mut self) {
let next_scene = {
let current_scene = &mut **self.scenes
.last_mut()
.expect("Tried to update empty scene stack");
current_scene.update(&mut self.world)
};
self.switch(next_scene);
}
fn draw_scenes(scenes: &mut [Box<Scene<C, Ev>>], world: &mut C, ctx: &mut ggez::Context) {
assert!(scenes.len() > 0);
if let Some((current, rest)) = scenes.split_last_mut() {
if current.draw_previous() {
SceneStack::draw_scenes(rest, world, ctx);
}
current
.draw(world, ctx)
.expect("I would hope drawing a scene never fails!");
}
}
pub fn draw(&mut self, ctx: &mut ggez::Context) {
SceneStack::draw_scenes(&mut self.scenes, &mut self.world, ctx)
}
pub fn input(&mut self, event: Ev, started: bool) {
let current_scene = &mut **self.scenes
.last_mut()
.expect("Tried to do input for empty scene stack");
current_scene.input(&mut self.world, event, started);
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Thing {
scenes: Vec<SceneStack<u32, u32>>,
}
#[test]
fn test1() {
let x = Thing { scenes: vec![] };
}
}