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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use bevy::prelude::App;

use crate::{egui, Context};
use crate::{utils, Inspectable};

impl<T> Inspectable for Vec<T>
where
    T: Inspectable + Default,
{
    type Attributes = <T as Inspectable>::Attributes;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, context: &mut Context) -> bool {
        let mut changed = false;

        ui.vertical(|ui| {
            let mut to_delete = None;

            let len = self.len();
            for (i, val) in self.iter_mut().enumerate() {
                ui.horizontal(|ui| {
                    if utils::ui::label_button(ui, "✖", egui::Color32::RED) {
                        to_delete = Some(i);
                    }
                    changed |= val.ui(ui, options.clone(), &mut context.with_id(i as u64));
                });

                if i != len - 1 {
                    ui.separator();
                }
            }

            ui.vertical_centered_justified(|ui| {
                if ui.button("+").clicked() {
                    self.push(T::default());
                    changed = true;
                }
            });

            if let Some(i) = to_delete {
                self.remove(i);
                changed = true;
            }
        });

        changed
    }

    fn setup(app: &mut App) {
        T::setup(app);
    }
}

impl<T: Inspectable, const N: usize> Inspectable for [T; N] {
    type Attributes = <T as Inspectable>::Attributes;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, context: &mut Context) -> bool {
        let mut changed = false;
        ui.vertical(|ui| {
            for (i, val) in self.iter_mut().enumerate() {
                ui.horizontal(|ui| {
                    ui.label(i.to_string());
                    changed |= val.ui(ui, options.clone(), &mut context.with_id(i as u64));
                });
            }
        });
        changed
    }

    fn setup(app: &mut App) {
        T::setup(app);
    }
}

macro_rules! impl_for_tuple {
    ( $($ty:ident : $i:tt),* ) => {
        #[allow(unused_variables, non_snake_case)]
        impl<$($ty: Inspectable + 'static),*> Inspectable for ($($ty,)*) {
            type Attributes = ($(<$ty as Inspectable>::Attributes,)*);

            fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, context: &mut Context) -> bool {
                #[allow(unused_mut)]
                let mut inline = true;
                $(inline &= should_display_inline::<$ty>();)*

                #[allow(unused_mut)]
                let mut changed = false;

                if inline {
                    ui.horizontal(|ui| {
                        let ($($ty,)*) = options;
                        ui.label("(");
                        $(changed |= self.$i.ui(ui, $ty, &mut context.with_id($i));)*
                        ui.label(")");
                    });
                } else {
                    let ($($ty,)*) = options;

                    ui.vertical(|ui| {
                        $(
                            if $i != 0 {
                                ui.separator();
                            }
                            changed |= self.$i.ui(ui, $ty, &mut context.with_id($i));
                        )*
                    });
                }

                changed
            }

            fn setup(app: &mut App) {
                $($ty::setup(app);)*
            }
        }
    };
}

macro_rules! matches_ty {
    ($ty:ty, $($types:ty)|+) => {{
        let type_id = std::any::TypeId::of::<$ty>();
        $(
            type_id == std::any::TypeId::of::<$types>()
        )||*
    }};
}
fn should_display_inline<T: 'static>() -> bool {
    matches_ty!(
        T,
        i8 | i16 | i32 | i64 | isize | u8 | u16 | u32 | u64 | usize | char | bool | String | &'static str | f32 | f64 | &'static str
    )
}

impl_for_tuple!();
impl_for_tuple!(A:0);
impl_for_tuple!(A:0, B:1);
impl_for_tuple!(A:0, B:1, C:2);
impl_for_tuple!(A:0, B:1, C:2, D:3);