logo
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
use crate::engine::d2::animation::AnimatedFloat;

use super::{Graphics, Sprite};

/// A sprite that displays a rectangle filled with a given color.
#[derive(Default, Clone, Debug)]
pub struct FillSprite {
    pub inner: Sprite,
    pub color: i32,
    pub width: AnimatedFloat,
    pub height: AnimatedFloat,
}

impl FillSprite {
    pub fn new(color: i32, width: f32, height: f32) -> Self {
        Self {
            inner: Sprite::new(),
            color,
            width: AnimatedFloat::new(width, None),
            height: AnimatedFloat::new(height, None),
        }
    }

    // override
    pub fn draw(&self, gfx: &Box<dyn Graphics>) {
        gfx.fill_rect(self.color, 0.0, 0.0, self.width.get(), self.height.get());
    }

    // override
    pub fn natural_width(&self) -> f32 {
        self.width.get()
    }

    // override
    pub fn natural_height(&self) -> f32 {
        self.height.get()
    }

    /// Chainable convenience method to set the width and height.
    /// @returns This instance, for chaining.
    pub fn set_size(&mut self, width: f32, height: f32) -> &Self {
        self.width.set(width);
        self.height.set(height);

        self
    }

    // override
    pub fn on_update(&mut self, dt: f32) {
        self.inner.on_update(dt);
        self.width.update(dt);
        self.height.update(dt);
    }
}

impl AsRef<Sprite> for FillSprite {
    fn as_ref(&self) -> &Sprite {
        &self.inner
    }
}