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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use glium::backend::glutin_backend::GlutinFacade;
use event::Keys;
use traits::HasId;

/// Signalling Enum, meant to tell the SceneManager what should happen next.
pub enum SceneTransition<T : Sized> {
    /// `Nothing` will leave the current Scene on the Stack.
    Nothing,
    /// `Push` will leave the current scene (but not destroy it) and put the new
    /// scene on the stack.
    Push(Box<Scene<State=T>>),
    /// `Pop` will remove the current Scene from the stack returning to the previous
    /// one.
    Pop,
    /// `PopUntil` will remove scenes until the given scene is found, this is useful
    /// to get back to a parent menu for example.
    /// **This panics if the menu does not exist!**
    PopUntil(usize)
}

/// One of the most important traits for a game, the scene is what tells the
/// display what to draw as well as what should happen with the given input.
pub trait Scene : HasId {
    /// What kind of state is carried around?
    type State : Sized + Clone;
    /// Called everytime this scene becomes the top of the stack
    fn enter(&mut self, _state: &mut Self::State) {}
    /// Called everytime this scene stops being the top of the stack (also
    /// before a drop)
    fn leave(&mut self, _state: &mut Self::State) {}
    /// Convenience method where you can handle keyboard input specifically.
    /// This is called _before_ `tick`.
    fn keypress(&mut self, _state: &mut Self::State, _keys: &Keys) {}
    /// Called with a display to draw into something
    fn display(&mut self, _state: &mut Self::State, _display: &GlutinFacade) {}
    /// Called to update the state so as to reflect one advancement in time.
    fn tick(&mut self, _state: &mut Self::State, _dt: f64) -> SceneTransition<Self::State>
    {
        SceneTransition::Pop
    }
}

/// This trait has to be implemented by the SceneManager that will run your game.
/// A sample implementation is `StackSceneManager`
pub trait SceneManager<T : Sized> {
    /// The Associated Scene
    type Scene : ?Sized + HasId = Scene<State=T>;
    /// The Associated SceneTransition
    type SceneTransition = SceneTransition<T>;

    /// Return the scenes as non-mut references
    fn get_scenes(&self) -> &Vec<Box<Self::Scene>>;
    /// Return the scenes as mut references
    fn get_scenes_mut(&mut self) -> &mut Vec<Box<Self::Scene>>;
    /// Make the manager handle a given transition.
    fn handle_transition(&mut self, Self::SceneTransition);
    /// Update the scene/s
    fn update(&mut self, dt: f64, keys: &Keys);
    /// Display the scene/s
    fn display(&mut self, display: &GlutinFacade);
}

/// A sample implementation of `SceneManager` can be used as is for a stack
/// based scene system. The type parameter is the state of the game.
pub struct StackSceneManager<T : Sized + Clone> {
    /// The scenes inside the manager.
    scenes: Vec<Box<Scene<State=T>>>,
    state: T
}

impl<T: Clone> StackSceneManager<T> {
    /// Creates a new StackSceneManager. It has nothing in it,
    /// you probably want to use `with_scene`
    pub fn new(state: T) -> StackSceneManager<T> {
        StackSceneManager {
            scenes: Vec::new(),
            state: state
        }
    }

    /// Creates a StackSceneManager with
    pub fn with_scene(state: T, scene: Box<Scene<State=T>>) -> StackSceneManager<T>
    {
        let mut m = StackSceneManager::new(state);
        m.handle_transition(SceneTransition::Push(scene));
        m
    }
}

impl<T> SceneManager<T> for StackSceneManager<T> where T: Sized + Clone {
    fn get_scenes(&self) -> &Vec<Box<Self::Scene>> {
        return &self.scenes;
    }

    fn get_scenes_mut(&mut self) -> &mut Vec<Box<Self::Scene>> {
        return &mut self.scenes;
    }

    fn handle_transition(&mut self, trans: Self::SceneTransition) {
        use scene::SceneTransition::*;
        match trans {
            Nothing => {},
            Push(boxed_scene) => {
                if let Some(s) = self.scenes.last_mut() {
                    s.leave(&mut self.state);
                }
                self.scenes.push(boxed_scene);
                if let Some(s) = self.scenes.last_mut() {
                    s.enter(&mut self.state);
                }
            },
            Pop => {
                if let Some(mut s) = self.scenes.pop() {
                    s.leave(&mut self.state);
                }
            },
            PopUntil(id) => {
                // If we have just one or zero scenes we can simply panic.
                // If not then we just call leave once and iterate through
                // If we have not panicked at the end we then enter that scene
                let mut length = self.scenes.len();

                if length == 0 {
                    // This should never happen !?
                    panic!("Tried to pop until on an empty stack.");
                }

                if length == 1 {
                    panic!("Tried to pop until a nonexistant stack with 1 element.");
                }

                if let Some(s) = self.scenes.last_mut() {
                    s.leave(&mut self.state);
                }

                while length > 0 {
                    if let Some(k) = self.scenes.last().map(|s| s.get_id()) {
                        if k == id {
                            break;
                        } else {
                            self.scenes.pop();
                        }
                    }

                    length = self.scenes.len();
                }

                if length == 0 {
                    panic!("Emptied the stack in a PopUntil, use Quit instead if this is wanted.");
                } else {
                    if let Some(s) = self.scenes.last_mut() {
                        s.enter(&mut self.state);
                    }
                }
            }
        }
    }

    fn update(&mut self, dt: f64, keys: &Keys) {
        let mut state = self.state.clone();
        self.get_scenes_mut().last_mut()
            .unwrap().keypress(&mut state, keys);
        let answer = self.get_scenes_mut().last_mut()
            .unwrap().tick(&mut state, dt);
        self.handle_transition(answer);
    }

    fn display(&mut self, display: &GlutinFacade) {
        let mut state = self.state.clone();
        self.get_scenes_mut().last_mut()
            .unwrap().display(&mut state, display);
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::rc::Rc;
    use std::cell::RefCell;
    use glium::backend::glutin_backend::GlutinFacade;
    use glium::glutin::HeadlessRendererBuilder;
    use glium::DisplayBuild;

    use traits::HasId;

    struct TestData {
        has_been_modified: usize,
        has_entered:       usize,
        has_left:          usize,
    }

    type State = Rc<RefCell<TestData>>;

    fn create_state() -> State {
        Rc::new(RefCell::new(TestData {
            has_been_modified: 0,
            has_entered:       0,
            has_left:          0,
        }))
    }

    fn create_scene_manager(state: State) -> StackSceneManager<State> {
        StackSceneManager {
            scenes: Vec::new(),
            state: state
        }
    }

    fn create_display() -> GlutinFacade {
        HeadlessRendererBuilder::new(1024, 768).build_glium().unwrap()
    }

    #[test]
    fn enter_leave_scene_manager() {
        struct TestScene;

        impl HasId for TestScene {
            fn get_id(&self) -> usize {
                0
            }
        }

        impl Scene for TestScene {
            type State = State;
            fn enter(&mut self, data: &mut State) {
                data.borrow_mut().has_entered += 1;
            }
            fn leave(&mut self, data: &mut State) {
                data.borrow_mut().has_left += 1;
            }
            fn tick(&mut self, data: &mut State, _dt: f64) -> SceneTransition<State>
            {
                if data.borrow().has_been_modified > 0 {
                    return SceneTransition::Pop
                }

                data.borrow_mut().has_been_modified = 1;
                SceneTransition::Nothing
            }
        }

        let mut state = create_state();
        let mut mgr = create_scene_manager(state.clone());

        mgr.handle_transition(SceneTransition::Push(Box::new(TestScene)));

        assert_eq!(mgr.get_scenes().len(), 1);

        let answer = mgr.get_scenes_mut().last_mut().unwrap().tick(&mut state, 0.0);
        mgr.handle_transition(answer);

        assert_eq!(state.borrow().has_been_modified, 1);

        let answer = mgr.get_scenes_mut().last_mut().unwrap().tick(&mut state, 0.0);
        mgr.handle_transition(answer);

        assert_eq!(mgr.get_scenes().len(), 0);
        assert_eq!(state.borrow().has_entered, 1);
        assert_eq!(state.borrow().has_left, 1);
    }

    #[test]
    fn fake_display() {
        struct TestScene;

        impl HasId for TestScene {
            fn get_id(&self) -> usize {
                0
            }
        }

        impl Scene for TestScene {
            type State = State;
            fn display(&mut self, data: &mut Self::State, display: &GlutinFacade) {
                use glium::Surface;
                let mut frame = display.draw();
                frame.clear_color(0.,1.,0.,1.0);
                data.borrow_mut().has_been_modified = 1;
                frame.finish().unwrap();
            }
        }
        let mut state = create_state();
        let display = create_display();

        let mut scene = TestScene;

        scene.display(&mut state, &display);

        assert_eq!(state.borrow().has_been_modified, 1);
    }

    #[test]
    fn popuntil_manager() {
        struct TestScene;

        impl HasId for TestScene {
            fn get_id(&self) -> usize {
                0
            }
        }

        impl Scene for TestScene {
            type State = State;
            fn enter(&mut self, data: &mut State) {
                data.borrow_mut().has_entered += 1;
            }
            fn leave(&mut self, data: &mut State) {
                data.borrow_mut().has_left += 1;
            }
            fn tick(&mut self, _data: &mut State, _dt: f64) -> SceneTransition<State>
            {
                SceneTransition::Push(Box::new(TestSceneMenu))
            }
        }

        struct TestSceneMenu;

        impl HasId for TestSceneMenu {
            fn get_id(&self) -> usize {
                1
            }
        }

        impl Scene for TestSceneMenu {
            type State = State;
            fn enter(&mut self, data: &mut State) {
                data.borrow_mut().has_entered += 1;
            }
            fn leave(&mut self, data: &mut State) {
                data.borrow_mut().has_left += 1;
            }
            fn tick(&mut self, _data: &mut State, _dt: f64) -> SceneTransition<State>
            {
                SceneTransition::Push(Box::new(TestSceneSubMenu))
            }
        }

        struct TestSceneSubMenu;

        impl HasId for TestSceneSubMenu {
            fn get_id(&self) -> usize {
                2
            }
        }

        impl Scene for TestSceneSubMenu {
            type State = State;
            fn enter(&mut self, data: &mut State) {
                data.borrow_mut().has_entered += 1;
            }
            fn leave(&mut self, data: &mut State) {
                data.borrow_mut().has_left += 1;
            }
            fn tick(&mut self, _data: &mut State, _dt: f64) -> SceneTransition<State>
            {
                SceneTransition::PopUntil(0)
            }
        }


        let mut state = create_state();
        let mut mgr = create_scene_manager(state.clone());

        mgr.handle_transition(SceneTransition::Push(Box::new(TestScene)));

        let answer = mgr.get_scenes_mut().last_mut().unwrap().tick(&mut state, 0.0);
        mgr.handle_transition(answer);

        assert_eq!(mgr.get_scenes().len(), 2);

        let answer = mgr.get_scenes_mut().last_mut().unwrap().tick(&mut state, 0.0);
        mgr.handle_transition(answer);

        assert_eq!(mgr.get_scenes().len(), 3);

        let answer = mgr.get_scenes_mut().last_mut().unwrap().tick(&mut state, 0.0);
        mgr.handle_transition(answer);

        assert_eq!(mgr.get_scenes().len(), 1);

        // TestScene -> Menu -> SubMenu -> TestScene
        assert_eq!(state.borrow().has_entered, 4);

        // TestScene -> Menu -> SubMenu
        assert_eq!(state.borrow().has_left, 3);

    }

}