Skip to main content

imgui_inspect/
lib.rs

1mod default;
2mod slider;
3
4pub use default::*;
5pub use slider::*;
6
7/// Options for rendering a value as a struct (i.e. draw all of its subfields)
8#[derive(Debug, Default)]
9pub struct InspectArgsStruct {
10    pub header: Option<bool>,
11    pub indent_children: Option<bool>,
12}
13
14impl From<InspectArgsDefault> for InspectArgsStruct {
15    fn from(default_args: InspectArgsDefault) -> Self {
16        Self {
17            header: default_args.header,
18            indent_children: default_args.indent_children,
19        }
20    }
21}
22
23/// Renders a struct (i.e. draw all of its subfields). Most traits are implemented by hand-written code, but this trait
24/// is normally generated by putting `#[derive(Inspect)]` on a struct
25pub trait InspectRenderStruct<T> {
26    fn render(
27        data: &[&T],
28        label: &'static str,
29        ui: &imgui::Ui,
30        args: &InspectArgsStruct,
31    );
32    fn render_mut(
33        data: &mut [&mut T],
34        label: &'static str,
35        ui: &imgui::Ui,
36        args: &InspectArgsStruct,
37    ) -> bool;
38}
39
40/// Utility function that, given a list of references, returns Some(T) if they are the same, otherwise None
41pub fn get_same_or_none<T: PartialEq + Clone>(data: &[&T]) -> Option<T> {
42    if data.is_empty() {
43        return None;
44    }
45
46    let first = data[0].clone();
47    for d in data {
48        if **d != first {
49            return None;
50        }
51    }
52
53    Some(first)
54}
55
56/// Utility function that, given a list of references, returns Some(T) if they are the same, otherwise None
57fn get_same_or_none_mut<T: PartialEq + Clone>(data: &mut [&mut T]) -> Option<T> {
58    if data.is_empty() {
59        return None;
60    }
61
62    let first = data[0].clone();
63    for d in data {
64        if **d != first {
65            return None;
66        }
67    }
68
69    Some(first)
70}