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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use bevy_ecs::entity::Entity;
use std::collections::VecDeque;

use crate::InspectorOptions;

use super::{InspectorOptionsType, Target};

macro_rules! impl_options {
    ($ty:ty => $options:ty) => {
        impl InspectorOptionsType for $ty {
            type DeriveOptions = $options;
            type Options = $options;

            fn options_from_derive(options: Self::DeriveOptions) -> Self::Options {
                options
            }
        }
    };
}
macro_rules! impl_options_defer_generic {
    ($name:ident < $generic:ident >) => {
        impl<$generic: InspectorOptionsType> InspectorOptionsType for $name<$generic> {
            type DeriveOptions = <$generic as InspectorOptionsType>::DeriveOptions;
            type Options = <$generic as InspectorOptionsType>::Options;

            fn options_from_derive(options: Self::DeriveOptions) -> Self::Options {
                $generic::options_from_derive(options)
            }
        }
    };
}

#[derive(Clone)]
#[non_exhaustive]
pub struct NumberOptions<T> {
    pub min: Option<T>,
    pub max: Option<T>,
    pub speed: f32,
    pub prefix: String,
    pub suffix: String,
    pub display: NumberDisplay,
}

impl<T> Default for NumberOptions<T> {
    fn default() -> Self {
        Self {
            min: None,
            max: None,
            speed: 0.0,
            prefix: String::new(),
            suffix: String::new(),
            display: NumberDisplay::default(),
        }
    }
}

#[derive(Clone, Copy, Default)]
#[non_exhaustive]
pub enum NumberDisplay {
    #[default]
    Drag,
    Slider,
}

impl<T> NumberOptions<T> {
    pub fn between(min: T, max: T) -> NumberOptions<T> {
        NumberOptions {
            min: Some(min),
            max: Some(max),
            speed: 0.0,
            prefix: String::new(),
            suffix: String::new(),
            display: NumberDisplay::default(),
        }
    }
    pub fn at_least(min: T) -> NumberOptions<T> {
        NumberOptions {
            min: Some(min),
            max: None,
            speed: 0.0,
            prefix: String::new(),
            suffix: String::new(),
            display: NumberDisplay::default(),
        }
    }

    pub fn with_speed(self, speed: f32) -> NumberOptions<T> {
        NumberOptions { speed, ..self }
    }

    pub fn map<U>(&self, f: impl Fn(&T) -> U) -> NumberOptions<U> {
        NumberOptions {
            #[allow(clippy::redundant_closure)] // false positive
            min: self.min.as_ref().map(|min| f(min)),
            max: self.max.as_ref().map(f),
            speed: self.speed,
            prefix: self.prefix.clone(),
            suffix: self.suffix.clone(),
            display: NumberDisplay::default(),
        }
    }
}
impl<T: egui::emath::Numeric> NumberOptions<T> {
    pub fn positive() -> NumberOptions<T> {
        NumberOptions {
            min: Some(T::from_f64(0.0)),
            max: None,
            speed: 0.0,
            prefix: String::new(),
            suffix: String::new(),
            display: NumberDisplay::default(),
        }
    }

    pub fn normalized() -> Self {
        NumberOptions {
            min: Some(T::from_f64(0.0)),
            max: Some(T::from_f64(1.0)),
            speed: 0.01,
            prefix: String::new(),
            suffix: String::new(),
            display: NumberDisplay::default(),
        }
    }
}

impl_options!(f32 => NumberOptions<f32>);
impl_options!(f64 => NumberOptions<f64>);
impl_options!(i8 => NumberOptions<i8>);
impl_options!(i16 => NumberOptions<i16>);
impl_options!(i32 => NumberOptions<i32>);
impl_options!(i64 => NumberOptions<i64>);
impl_options!(i128 => NumberOptions<i128>);
impl_options!(isize => NumberOptions<isize>);
impl_options!(u8 => NumberOptions<u8>);
impl_options!(u16 => NumberOptions<u16>);
impl_options!(u32 => NumberOptions<u32>);
impl_options!(u64 => NumberOptions<u64>);
impl_options!(u128 => NumberOptions<u128>);
impl_options!(usize => NumberOptions<usize>);

#[non_exhaustive]
pub struct RangeOptions<T: InspectorOptionsType> {
    pub start: T::Options,
    pub end: T::Options,
}

impl<T: InspectorOptionsType> Clone for RangeOptions<T> {
    fn clone(&self) -> Self {
        Self {
            start: self.start.clone(),
            end: self.end.clone(),
        }
    }
}

impl<T: InspectorOptionsType> Default for RangeOptions<T> {
    fn default() -> Self {
        Self {
            start: T::options_from_derive(T::DeriveOptions::default()),
            end: T::options_from_derive(T::DeriveOptions::default()),
        }
    }
}

impl<T: InspectorOptionsType + 'static> InspectorOptionsType for std::ops::Range<T> {
    type DeriveOptions = RangeOptions<T>;
    type Options = RangeOptions<T>;

    fn options_from_derive(options: Self::DeriveOptions) -> Self::Options {
        options
    }
}

#[derive(Default, Clone)]
#[non_exhaustive]
pub struct QuatOptions {
    pub display: QuatDisplay,
}

#[derive(Copy, Clone, Default)]
pub enum QuatDisplay {
    Raw,
    #[default]
    Euler,
    YawPitchRoll,
    AxisAngle,
}

impl_options!(bevy_math::Quat => QuatOptions);

#[derive(Clone)]
#[non_exhaustive]
pub struct EntityOptions {
    pub display: EntityDisplay,
    pub despawnable: bool,
}

impl Default for EntityOptions {
    fn default() -> Self {
        Self {
            display: EntityDisplay::default(),
            despawnable: true,
        }
    }
}

#[derive(Copy, Clone, Default)]
#[non_exhaustive]
pub enum EntityDisplay {
    Id,
    #[default]
    Components,
}

impl_options!(Entity => EntityOptions);

impl<T: InspectorOptionsType> InspectorOptionsType for Option<T> {
    type DeriveOptions = T::DeriveOptions;
    type Options = InspectorOptions;

    fn options_from_derive(options: Self::DeriveOptions) -> Self::Options {
        let inner_options = T::options_from_derive(options);

        let mut inspector_options = InspectorOptions::new();
        inspector_options.insert(
            Target::VariantField {
                variant_index: 1, // Some
                field_index: 0,
            },
            inner_options,
        );

        inspector_options
    }
}

impl_options_defer_generic!(Vec<T>);
impl_options_defer_generic!(VecDeque<T>);

impl<T: InspectorOptionsType, const N: usize> InspectorOptionsType for [T; N] {
    type DeriveOptions = T::DeriveOptions;
    type Options = T::Options;

    fn options_from_derive(options: Self::DeriveOptions) -> Self::Options {
        T::options_from_derive(options)
    }
}