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
use std::rc::Rc;
use crate::engine::d2::animation::AnimatedFloat;
use super::{Graphics, Sprite, Texture};
#[derive(Default, Clone, Debug)]
pub struct PatternSprite {
pub inner: Sprite,
pub texture: Option<Rc<dyn Texture>>,
pub width: AnimatedFloat,
pub height: AnimatedFloat,
}
impl PatternSprite {
pub fn new(texture: Option<Rc<dyn Texture>>, width: f32, height: f32) -> Self {
let width = if width < 0.0 {
if let Some(texture) = &texture {
texture.width() as f32
} else {
0.0
}
} else {
width
};
let height = if height < 0.0 {
if let Some(texture) = &texture {
texture.height() as f32
} else {
0.0
}
} else {
height
};
Self {
inner: Sprite::new(),
texture: texture,
width: AnimatedFloat::new(width, None),
height: AnimatedFloat::new(height, None),
}
}
pub fn draw(&self, gfx: &Box<dyn Graphics>) {
if let Some(ref texture) = self.texture {
gfx.draw_pattern(texture, 0.0, 0.0, self.width.get(), self.height.get());
}
}
pub fn natural_width(&self) -> f32 {
self.width.get()
}
pub fn natural_height(&self) -> f32 {
self.height.get()
}
pub fn set_size(&mut self, width: f32, height: f32) -> &Self {
self.width.set(width);
self.height.set(height);
self
}
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 PatternSprite {
fn as_ref(&self) -> &Sprite {
&self.inner
}
}