imgui_ext/checkbox.rs
1//! ## Optional fields
2//!
3//! * `label` override widget label.
4//! * `catch`
5//! * `map` Applies a mapping function to `&mut Self` (works the same as in the
6//! [input example](../input/index.html#mapping))
7//!
8//! ## Example
9//!
10//! ```
11//! #[derive(imgui_ext::Gui)]
12//! struct Checkboxes {
13//! // All parameters are optional.
14//! #[imgui(checkbox)]
15//! turbo: bool,
16//!
17//! // Optionally, you can override the label:
18//! #[imgui(checkbox(label = "Checkbox!"))]
19//! check: bool,
20//! }
21//! ```
22//!
23//! ### Result
24//!
25//! ![][result]
26//!
27//! [result]: https://i.imgur.com/1hTR89V.png
28use imgui::{ImStr, Ui};
29
30/// Structure generated by the annoration.
31pub struct CheckboxParams<'a> {
32 pub label: &'a ImStr,
33}
34
35/// Trait for types that can be represented with a checkbox.
36pub trait Checkbox {
37 fn build(ui: &Ui, elem: &mut Self, params: CheckboxParams) -> bool;
38}
39
40impl<C: Checkbox> Checkbox for Option<C> {
41 fn build(ui: &Ui, elem: &mut Self, params: CheckboxParams) -> bool {
42 if let Some(ref mut elem) = elem {
43 C::build(ui, elem, params)
44 } else {
45 false
46 }
47 }
48}
49
50impl Checkbox for bool {
51 fn build(ui: &Ui, elem: &mut Self, params: CheckboxParams) -> bool {
52 ui.checkbox(params.label, elem)
53 }
54}
55
56impl<T: Checkbox> Checkbox for Box<T> {
57 #[inline]
58 fn build(ui: &Ui, elem: &mut Self, params: CheckboxParams) -> bool {
59 T::build(ui, elem, params)
60 }
61}