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
//! ## Params
//!
//! * `size` local function returning image size
//!
//! ## Optional params
//!
//! * `border` local function returning the border color
//! * `tint` local function returning tint color
//!
//! ## Limitations
//!
//! * `border`, `tint` and `size` cannot be set at runtime.
//! * No support to edit UVs yet.
//!
//! ## Example
//!
//! ```
//! use imgui_ext::ImGuiExt;
//!
//! #[derive(ImGuiExt)]
//! struct Image {
//!     #[imgui(image(size = "img_size"))]
//!     texture: usize,
//!     #[imgui(image(size = "img_size", tint = "img_tint", border = "img_border"))]
//!     texture_tint: usize,
//! }
//!
//! const fn img_size() -> (f32, f32) {
//!     (512.0, 64.0)
//! }
//!
//! const fn img_tint() -> (f32, f32, f32, f32) {
//!     (1.0, 0.0, 1.0, 1.0)
//! }
//!
//! const fn img_border() -> (f32, f32, f32, f32) {
//!     (1.0, 1.0, 1.0, 1.0)
//! }
//! ```
//!
//! ### Result
//!
//! ![][result]
//!
//! [result]: https://i.imgur.com/RoJdyGR.png
//!
use imgui::{ImTexture, ImVec2, ImVec4, Ui};

#[derive(Clone, Copy)]
pub struct ImageParams {
    pub size: ImVec2,
    pub border: Option<ImVec4>,
    pub tint: Option<ImVec4>,
}

pub trait Image {
    fn build(ui: &Ui, elem: Self, params: ImageParams);
}

impl<T> Image for T
where
    T: Copy + Into<ImTexture>,
{
    fn build(ui: &Ui, elem: Self, params: ImageParams) {
        let mut image = ui.image(elem.into(), params.size);
        if let Some(tint) = params.tint {
            image = image.tint_col(tint);
        }
        if let Some(border) = params.border {
            image = image.border_col(border);
        }
        image.build();
    }
}