1use crate::sys;
2use crate::texture::TextureRef;
3use crate::ui::Ui;
4
5impl Ui {
26 #[doc(alias = "Image")]
28 pub fn image(&self, texture: impl Into<TextureRef>, size: [f32; 2]) {
29 self.image_config(texture, size).build()
30 }
31
32 #[doc(alias = "ImageButton")]
34 pub fn image_button(
35 &self,
36 str_id: impl AsRef<str>,
37 texture: impl Into<TextureRef>,
38 size: [f32; 2],
39 ) -> bool {
40 self.image_button_config(str_id, texture, size).build()
41 }
42
43 pub fn image_config(&self, texture: impl Into<TextureRef>, size: [f32; 2]) -> Image<'_> {
45 Image::new(self, texture, size)
46 }
47
48 pub fn image_button_config(
50 &self,
51 str_id: impl AsRef<str>,
52 texture: impl Into<TextureRef>,
53 size: [f32; 2],
54 ) -> ImageButton<'_> {
55 ImageButton::new(self, str_id, texture, size)
56 }
57}
58
59#[derive(Debug)]
61#[must_use]
62pub struct Image<'ui> {
63 ui: &'ui Ui,
64 texture: TextureRef,
65 size: [f32; 2],
66 uv0: [f32; 2],
67 uv1: [f32; 2],
68 tint_color: [f32; 4],
69 border_color: [f32; 4],
70}
71
72impl<'ui> Image<'ui> {
73 pub fn new(ui: &'ui Ui, texture: impl Into<TextureRef>, size: [f32; 2]) -> Self {
75 Self {
76 ui,
77 texture: texture.into(),
78 size,
79 uv0: [0.0, 0.0],
80 uv1: [1.0, 1.0],
81 tint_color: [1.0, 1.0, 1.0, 1.0],
82 border_color: [0.0, 0.0, 0.0, 0.0],
83 }
84 }
85
86 pub fn uv0(mut self, uv0: [f32; 2]) -> Self {
88 self.uv0 = uv0;
89 self
90 }
91
92 pub fn uv1(mut self, uv1: [f32; 2]) -> Self {
94 self.uv1 = uv1;
95 self
96 }
97
98 pub fn tint_color(mut self, tint_color: [f32; 4]) -> Self {
100 self.tint_color = tint_color;
101 self
102 }
103
104 pub fn border_color(mut self, border_color: [f32; 4]) -> Self {
106 self.border_color = border_color;
107 self
108 }
109
110 pub fn build(self) {
112 let size_vec: sys::ImVec2 = self.size.into();
113 let uv0_vec: sys::ImVec2 = self.uv0.into();
114 let uv1_vec: sys::ImVec2 = self.uv1.into();
115 let _tint_vec: sys::ImVec4 = sys::ImVec4 {
116 x: self.tint_color[0],
117 y: self.tint_color[1],
118 z: self.tint_color[2],
119 w: self.tint_color[3],
120 };
121 let _border_vec: sys::ImVec4 = sys::ImVec4 {
122 x: self.border_color[0],
123 y: self.border_color[1],
124 z: self.border_color[2],
125 w: self.border_color[3],
126 };
127
128 unsafe { sys::igImage(self.texture.raw(), size_vec, uv0_vec, uv1_vec) }
129 }
130
131 pub fn build_with_bg(self, bg_color: [f32; 4], tint_color: [f32; 4]) {
133 let size_vec: sys::ImVec2 = self.size.into();
134 let uv0_vec: sys::ImVec2 = self.uv0.into();
135 let uv1_vec: sys::ImVec2 = self.uv1.into();
136 let bg_vec = sys::ImVec4 {
137 x: bg_color[0],
138 y: bg_color[1],
139 z: bg_color[2],
140 w: bg_color[3],
141 };
142 let tint_vec = sys::ImVec4 {
143 x: tint_color[0],
144 y: tint_color[1],
145 z: tint_color[2],
146 w: tint_color[3],
147 };
148
149 unsafe {
150 sys::igImageWithBg(
151 self.texture.raw(),
152 size_vec,
153 uv0_vec,
154 uv1_vec,
155 bg_vec,
156 tint_vec,
157 )
158 }
159 }
160}
161
162#[derive(Debug)]
164#[must_use]
165pub struct ImageButton<'ui> {
166 ui: &'ui Ui,
167 str_id: String,
168 texture: TextureRef,
169 size: [f32; 2],
170 uv0: [f32; 2],
171 uv1: [f32; 2],
172 bg_color: [f32; 4],
173 tint_color: [f32; 4],
174}
175
176impl<'ui> ImageButton<'ui> {
177 pub fn new(
179 ui: &'ui Ui,
180 str_id: impl AsRef<str>,
181 texture: impl Into<TextureRef>,
182 size: [f32; 2],
183 ) -> Self {
184 Self {
185 ui,
186 str_id: str_id.as_ref().to_string(),
187 texture: texture.into(),
188 size,
189 uv0: [0.0, 0.0],
190 uv1: [1.0, 1.0],
191 bg_color: [0.0, 0.0, 0.0, 0.0],
192 tint_color: [1.0, 1.0, 1.0, 1.0],
193 }
194 }
195
196 pub fn uv0(mut self, uv0: [f32; 2]) -> Self {
198 self.uv0 = uv0;
199 self
200 }
201
202 pub fn uv1(mut self, uv1: [f32; 2]) -> Self {
204 self.uv1 = uv1;
205 self
206 }
207
208 pub fn bg_color(mut self, bg_color: [f32; 4]) -> Self {
210 self.bg_color = bg_color;
211 self
212 }
213
214 pub fn tint_color(mut self, tint_color: [f32; 4]) -> Self {
216 self.tint_color = tint_color;
217 self
218 }
219
220 pub fn build(self) -> bool {
222 let str_id_ptr = self.ui.scratch_txt(&self.str_id);
223 let size_vec: sys::ImVec2 = self.size.into();
224 let uv0_vec: sys::ImVec2 = self.uv0.into();
225 let uv1_vec: sys::ImVec2 = self.uv1.into();
226 let bg_vec: sys::ImVec4 = sys::ImVec4 {
227 x: self.bg_color[0],
228 y: self.bg_color[1],
229 z: self.bg_color[2],
230 w: self.bg_color[3],
231 };
232 let tint_vec: sys::ImVec4 = sys::ImVec4 {
233 x: self.tint_color[0],
234 y: self.tint_color[1],
235 z: self.tint_color[2],
236 w: self.tint_color[3],
237 };
238
239 unsafe {
240 sys::igImageButton(
241 str_id_ptr,
242 self.texture.raw(),
243 size_vec,
244 uv0_vec,
245 uv1_vec,
246 bg_vec,
247 tint_vec,
248 )
249 }
250 }
251}