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
use crate::*;
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
#[derive(Clone, Copy, Debug)]
pub struct Image {
texture_id: TextureId,
uv: Rect,
desired_size: Vec2,
bg_fill: Color32,
tint: Color32,
}
impl Image {
pub fn new(texture_id: TextureId, desired_size: impl Into<Vec2>) -> Self {
Self {
texture_id,
uv: Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)),
desired_size: desired_size.into(),
bg_fill: Default::default(),
tint: Color32::WHITE,
}
}
pub fn uv(mut self, uv: impl Into<Rect>) -> Self {
self.uv = uv.into();
self
}
pub fn bg_fill(mut self, bg_fill: impl Into<Color32>) -> Self {
self.bg_fill = bg_fill.into();
self
}
pub fn tint(mut self, tint: impl Into<Color32>) -> Self {
self.tint = tint.into();
self
}
}
impl Image {
pub fn desired_size(&self) -> Vec2 {
self.desired_size
}
pub fn paint_at(&self, ui: &mut Ui, rect: Rect) {
use paint::*;
let Self {
texture_id,
uv,
desired_size: _,
bg_fill,
tint,
} = self;
if *bg_fill != Default::default() {
let mut triangles = Triangles::default();
triangles.add_colored_rect(rect, *bg_fill);
ui.painter().add(PaintCmd::triangles(triangles));
}
{
let mut triangles = Triangles::with_texture(*texture_id);
triangles.add_rect_with_uv(rect, *uv, *tint);
ui.painter().add(PaintCmd::triangles(triangles));
}
}
}
impl Widget for Image {
fn ui(self, ui: &mut Ui) -> Response {
let response = ui.allocate_response(self.desired_size, Sense::hover());
self.paint_at(ui, response.rect);
response
}
}