imgui-inspect 0.1.0

Traits and default implementations for inspecting values with imgui.
Documentation

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>,
}

/// 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)
}