dear_imgui/widget/
image.rs

1use crate::sys;
2use crate::texture::TextureRef;
3use crate::ui::Ui;
4
5/// # Image Widgets
6///
7/// Examples
8/// - Using a plain texture id:
9/// ```no_run
10/// # use dear_imgui::*;
11/// # fn demo(ui: &Ui) {
12/// let tex_id = texture::TextureId::new(0xDEAD_BEEF);
13/// ui.image(tex_id, [128.0, 128.0]);
14/// # }
15/// ```
16/// - Using an ImGui-managed texture:
17/// ```no_run
18/// # use dear_imgui::*;
19/// # fn demo(ui: &Ui) {
20/// let mut tex = texture::TextureData::new();
21/// tex.create(texture::TextureFormat::RGBA32, 64, 64);
22/// ui.image(&mut tex, [64.0, 64.0]);
23/// # }
24/// ```
25impl Ui {
26    /// Creates an image widget
27    #[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    /// Creates an image button widget
33    #[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    /// Creates an image builder
44    pub fn image_config(&self, texture: impl Into<TextureRef>, size: [f32; 2]) -> Image<'_> {
45        Image::new(self, texture, size)
46    }
47
48    /// Creates an image button builder
49    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/// Builder for an image widget
60#[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    /// Creates a new image builder
74    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    /// Sets the UV coordinates for the top-left corner (default: [0.0, 0.0])
87    pub fn uv0(mut self, uv0: [f32; 2]) -> Self {
88        self.uv0 = uv0;
89        self
90    }
91
92    /// Sets the UV coordinates for the bottom-right corner (default: [1.0, 1.0])
93    pub fn uv1(mut self, uv1: [f32; 2]) -> Self {
94        self.uv1 = uv1;
95        self
96    }
97
98    /// Sets the tint color (default: white, no tint)
99    pub fn tint_color(mut self, tint_color: [f32; 4]) -> Self {
100        self.tint_color = tint_color;
101        self
102    }
103
104    /// Sets the border color (default: transparent, no border)
105    pub fn border_color(mut self, border_color: [f32; 4]) -> Self {
106        self.border_color = border_color;
107        self
108    }
109
110    /// Builds the image widget
111    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    /// Builds the image widget with background color and tint (v1.92+)
132    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/// Builder for an image button widget
163#[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    /// Creates a new image button builder
178    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    /// Sets the UV coordinates for the top-left corner (default: [0.0, 0.0])
197    pub fn uv0(mut self, uv0: [f32; 2]) -> Self {
198        self.uv0 = uv0;
199        self
200    }
201
202    /// Sets the UV coordinates for the bottom-right corner (default: [1.0, 1.0])
203    pub fn uv1(mut self, uv1: [f32; 2]) -> Self {
204        self.uv1 = uv1;
205        self
206    }
207
208    /// Sets the background color (default: transparent)
209    pub fn bg_color(mut self, bg_color: [f32; 4]) -> Self {
210        self.bg_color = bg_color;
211        self
212    }
213
214    /// Sets the tint color (default: white, no tint)
215    pub fn tint_color(mut self, tint_color: [f32; 4]) -> Self {
216        self.tint_color = tint_color;
217        self
218    }
219
220    /// Builds the image button widget
221    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}