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
//! Make an object have a static size

use sdl2::render::Canvas;
use sdl2::video::Window;

use drawable::{DrawSettings, Drawable, KnownSize, Position, State};

/// A wrapper that makes an object have a known size
pub struct WithSize<T: Drawable> {
    /// The size of the object
    pub size: (u32, u32),
    /// The inner object
    pub inner: T,
}

impl<T: Drawable> WithSize<T> {
    /// Create a new WithSize wrapper
    pub fn new(size: (u32, u32), inner: T) -> WithSize<T> {
        WithSize { size, inner }
    }
}

impl<T: Drawable> Drawable for WithSize<T> {
    fn content(&self) -> Vec<&dyn Drawable> {
        vec![&self.inner]
    }

    fn content_mut(&mut self) -> Vec<&mut dyn Drawable> {
        vec![&mut self.inner]
    }

    fn load(&mut self) {
        self.inner.load();
    }

    fn update(&mut self, dt: f64) {
        self.inner.update(dt);
    }

    fn step(&mut self) {
        self.inner.step()
    }

    fn state(&self) -> State {
        self.inner.state()
    }

    fn draw(&self, canvas: &mut Canvas<Window>, pos: &Position, settings: DrawSettings) {
        self.inner.draw(canvas, pos, settings);
    }
}

impl<T: Drawable> KnownSize for WithSize<T> {
    fn width(&self) -> usize {
        self.size.0 as usize
    }

    fn height(&self) -> usize {
        self.size.1 as usize
    }
}