imgui_ext/
image.rs

1//! ## Params
2//!
3//! * `size` path to a function that returns the size.
4//!
5//! ## Optional params
6//!
7//! * `border` path to a function that returns the border color.
8//! * `tint` path to a function tht returns a color to tint the image with.
9//! * `uv0` path to a function that returns the first uv coordinate. The default
10//!   value is `[0.0, 0.0]`.
11//! * `uv0` path to a function that returns the second uv coordinate. The
12//!   default value is `[1.0, 1.0]`.
13//! * `map` Applies a mapping function to `&mut Self`.
14//!
15//! ## Limitations
16//!
17//! * Parameters cannot be set at runtime (including `uv`s). This may be a deal
18//!   breaker for most applications that deal with texture atlases.
19//!
20//! ## Example
21//!
22//! ```
23//! #[derive(imgui_ext::Gui)]
24//! struct Image {
25//!     #[imgui(image(size = "size", uv0 = "uv0", uv1 = "uv1"))]
26//!     texture: usize,
27//!     #[imgui(image(size = "size", tint = "tint", border = "border"))]
28//!     texture_tint: usize,
29//! }
30//!
31//! fn size() -> [f32; 2] {
32//!     [512.0, 64.0]
33//! }
34//!
35//! fn tint() -> [f32; 4] {
36//!     [1.0, 0.0, 1.0, 1.0]
37//! }
38//!
39//! fn border() -> [f32; 4] {
40//!     [1.0, 1.0, 1.0, 1.0]
41//! }
42//!
43//! fn uv0() -> [f32; 2] {
44//!     [0.0, 0.0]
45//! }
46//!
47//! fn uv1() -> [f32; 2] {
48//!     [1.0, 1.0]
49//! }
50//! ```
51//!
52//! ### Result
53//!
54//! ![][result]
55//!
56//! [result]: https://i.imgur.com/RoJdyGR.png
57//!
58use imgui::{TextureId, Ui};
59
60pub struct ImageParams {
61    pub size: [f32; 2],
62    pub border: Option<[f32; 4]>,
63    pub tint: Option<[f32; 4]>,
64    pub uv0: Option<[f32; 2]>,
65    pub uv1: Option<[f32; 2]>,
66}
67
68pub trait Image {
69    fn build(ui: &Ui, elem: Self, params: ImageParams);
70}
71
72impl<T> Image for T
73where
74    T: Copy + Into<TextureId>,
75{
76    fn build(ui: &Ui, elem: Self, params: ImageParams) {
77        let mut image = ui.image(elem.into(), params.size);
78        if let Some(tint) = params.tint {
79            image = image.tint_col(tint);
80        }
81        if let Some(border) = params.border {
82            image = image.border_col(border);
83        }
84        if let Some(uv0) = params.uv0 {
85            image = image.uv0(uv0);
86        }
87        if let Some(uv1) = params.uv1 {
88            image = image.uv1(uv1);
89        }
90        image.build();
91    }
92}