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

mod default;
mod slider;

pub use default::*;
pub use slider::*;

/// Options for rendering a value as a struct (i.e. draw all of its subfields)
#[derive(Debug, Default)]
pub struct InspectArgsStruct {
    pub header: Option<bool>,
    pub indent_children: Option<bool>,
}

impl From<InspectArgsDefault> for InspectArgsStruct {
    fn from(default_args: InspectArgsDefault) -> Self {
        Self {
            header: default_args.header,
            indent_children: default_args.indent_children,
        }
    }
}

/// Renders a struct (i.e. draw all of its subfields). Most traits are implemented by hand-written code, but this trait
/// is normally generated by putting `#[derive(Inspect)]` on a struct
pub trait InspectRenderStruct<T> {
    fn render(data: &[&T], label: &'static str, ui: &imgui::Ui, args: &InspectArgsStruct);
    fn render_mut(
        data: &mut [&mut T],
        label: &'static str,
        ui: &imgui::Ui,
        args: &InspectArgsStruct,
    ) -> bool;
}

/// Utility function that, given a list of references, returns Some(T) if they are the same, otherwise None
pub fn get_same_or_none<T: PartialEq + Clone>(data: &[&T]) -> Option<T> {
    if data.len() == 0 {
        return None;
    }

    let first = data[0].clone();
    for d in data {
        if **d != first {
            return None;
        }
    }

    Some(first)
}

/// Utility function that, given a list of references, returns Some(T) if they are the same, otherwise None
fn get_same_or_none_mut<T: PartialEq + Clone>(data: &mut [&mut T]) -> Option<T> {
    if data.len() == 0 {
        return None;
    }

    let first = data[0].clone();
    for d in data {
        if **d != first {
            return None;
        }
    }

    Some(first)
}