imgui_ext/
image_button.rs

1//! ## Params
2//!
3//! * `size` path to a function that returns the size of the image.
4//!
5//! ## Optional params
6//!
7//! * `background` path to a function that returns the background color to be
8//!   used.
9//! * `tint` path to a function that returns a color to tint the image with.
10//! * `frame_padding` an `i32`.
11//! * `uv0` path to a function that returns the first uv coordinate to be used.
12//!   The default value is `[0.0, 0.0]`.
13//! * `uv0` path to a function that returns the second uv coordinate. The
14//!   default value is `[1.0, 1.0]`.
15//!
16//! ## Example
17//!
18//! ### Result
19//!
20use imgui::{TextureId, Ui};
21
22pub struct ImageButtonParams {
23    pub size: [f32; 2],
24    pub background: Option<[f32; 4]>,
25    pub tint: Option<[f32; 4]>,
26    pub uv0: Option<[f32; 2]>,
27    pub uv1: Option<[f32; 2]>,
28    pub frame_padding: Option<i32>,
29}
30
31pub trait ImageButton {
32    fn build(ui: &Ui, elem: Self, params: ImageButtonParams);
33}
34
35impl<T> ImageButton for T
36where
37    T: Copy + Into<TextureId>,
38{
39    fn build(ui: &Ui, elem: Self, params: ImageButtonParams) {
40        let mut image = ui.image_button(elem.into(), params.size);
41        if let Some(tint) = params.tint {
42            image = image.tint_col(tint);
43        }
44        if let Some(padding) = params.frame_padding {
45            image = image.frame_padding(padding);
46        }
47        if let Some(background) = params.background {
48            image = image.background_col(background);
49        }
50        if let Some(uv0) = params.uv0 {
51            image = image.uv0(uv0);
52        }
53        if let Some(uv1) = params.uv1 {
54            image = image.uv1(uv1);
55        }
56        image.build();
57    }
58}