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
//! ## Optional fields
//!
//! * `label` override widget label.
//! * `catch`
//! * `map` Applies a mapping function to `&mut Self` (works the same as in the
//!   [input example](../input/index.html#mapping))
//!
//! ## Example
//!
//! ```
//! #[derive(imgui_ext::Gui)]
//! struct Checkboxes {
//!     // All parameters are optional.
//!     #[imgui(checkbox)]
//!     turbo: bool,
//!
//!     // Optionally, you can override the label:
//!     #[imgui(checkbox(label = "Checkbox!"))]
//!     check: bool,
//! }
//! ```
//!
//! ### Result
//!
//! ![][result]
//!
//! [result]: https://i.imgur.com/1hTR89V.png
use imgui::{ImStr, Ui};

/// Structure generated by the annoration.
pub struct CheckboxParams<'a> {
    pub label: &'a ImStr,
}

/// Trait for types that can be represented with a checkbox.
pub trait Checkbox {
    fn build(ui: &Ui, elem: &mut Self, params: CheckboxParams) -> bool;
}

impl<C: Checkbox> Checkbox for Option<C> {
    fn build(ui: &Ui, elem: &mut Self, params: CheckboxParams) -> bool {
        if let Some(ref mut elem) = elem {
            C::build(ui, elem, params)
        } else {
            false
        }
    }
}

impl Checkbox for bool {
    fn build(ui: &Ui, elem: &mut Self, params: CheckboxParams) -> bool {
        ui.checkbox(params.label, elem)
    }
}

impl<T: Checkbox> Checkbox for Box<T> {
    #[inline]
    fn build(ui: &Ui, elem: &mut Self, params: CheckboxParams) -> bool {
        T::build(ui, elem, params)
    }
}