1use crate::{
4 math::Matrix2d,
5 triangulation,
6 types::{Color, Rectangle, SourceRectangle},
7 DrawState, Graphics, ImageSize,
8};
9
10#[derive(Copy, Clone)]
57pub struct Image {
58 pub color: Option<Color>,
60 pub rectangle: Option<Rectangle>,
62 pub source_rectangle: Option<SourceRectangle>,
64}
65
66impl Image {
67 pub fn new() -> Image {
69 Image {
70 color: None,
71 source_rectangle: None,
72 rectangle: None,
73 }
74 }
75
76 pub fn new_color(color: Color) -> Image {
78 Image {
79 color: Some(color),
80 source_rectangle: None,
81 rectangle: None,
82 }
83 }
84
85 pub fn color(mut self, value: Color) -> Self {
87 self.color = Some(value);
88 self
89 }
90
91 pub fn maybe_color(mut self, value: Option<Color>) -> Self {
93 self.color = value;
94 self
95 }
96
97 pub fn rect<R: Into<Rectangle>>(mut self, value: R) -> Self {
99 self.rectangle = Some(value.into());
100 self
101 }
102
103 pub fn maybe_rect<R: Into<Rectangle>>(mut self, value: Option<R>) -> Self {
105 self.rectangle = value.map(|v| v.into());
106 self
107 }
108
109 pub fn src_rect(mut self, value: SourceRectangle) -> Self {
111 self.source_rectangle = Some(value);
112 self
113 }
114
115 pub fn maybe_src_rect(mut self, value: Option<SourceRectangle>) -> Self {
117 self.source_rectangle = value;
118 self
119 }
120
121 #[inline(always)]
123 pub fn draw<G>(
124 &self,
125 texture: &<G as Graphics>::Texture,
126 draw_state: &DrawState,
127 transform: Matrix2d,
128 g: &mut G,
129 ) where
130 G: Graphics,
131 {
132 g.image(self, texture, draw_state, transform);
133 }
134
135 pub fn draw_tri<G>(
137 &self,
138 texture: &<G as Graphics>::Texture,
139 draw_state: &DrawState,
140 transform: Matrix2d,
141 g: &mut G,
142 ) where
143 G: Graphics,
144 {
145 use crate::math::Scalar;
146
147 let color = self.color.unwrap_or([1.0; 4]);
148 let source_rectangle = self.source_rectangle.unwrap_or({
149 let (w, h) = texture.get_size();
150 [0.0, 0.0, w as Scalar, h as Scalar]
151 });
152 let rectangle = self.rectangle.unwrap_or([
153 0.0,
154 0.0,
155 source_rectangle[2] as Scalar,
156 source_rectangle[3] as Scalar,
157 ]);
158 g.tri_list_uv(draw_state, &color, texture, |f| {
159 f(
160 &triangulation::rect_tri_list_xy(transform, rectangle),
161 &triangulation::rect_tri_list_uv(texture, source_rectangle),
162 )
163 });
164 }
165}
166
167impl Default for Image {
168 fn default() -> Self {
169 Image::new()
170 }
171}
172
173pub fn draw_many<G>(
175 rects: &[(Rectangle, SourceRectangle)],
176 color: Color,
177 texture: &<G as Graphics>::Texture,
178 draw_state: &DrawState,
179 transform: Matrix2d,
180 g: &mut G,
181) where
182 G: Graphics,
183{
184 g.tri_list_uv(draw_state, &color, texture, |f| {
185 for r in rects {
186 f(
187 &triangulation::rect_tri_list_xy(transform, r.0),
188 &triangulation::rect_tri_list_uv(texture, r.1),
189 )
190 }
191 });
192}
193
194#[cfg(test)]
195mod test {
196 use super::Image;
197
198 #[test]
199 fn test_image() {
200 let _img = Image::new()
201 .color([1.0; 4])
202 .rect([0.0, 0.0, 100.0, 100.0])
203 .src_rect([0.0, 0.0, 32.0, 32.0]);
204 }
205}