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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//! Custom UI implementations for specific types. Check [`InspectorPrimitive`] for an example.

use crate::reflect_inspector::{errors::no_multiedit, InspectorUi};
use bevy_reflect::{FromType, Reflect, TypePath, TypeRegistry};
use bevy_utils::Instant;
use std::{
    any::{Any, TypeId},
    borrow::Cow,
    path::PathBuf,
};

mod bevy_impls;
mod glam_impls;
mod image;
mod std_impls;

type InspectorEguiImplFn =
    fn(&mut dyn Any, &mut egui::Ui, &dyn Any, egui::Id, InspectorUi<'_, '_>) -> bool;
type InspectorEguiImplFnReadonly =
    fn(&dyn Any, &mut egui::Ui, &dyn Any, egui::Id, InspectorUi<'_, '_>);
type InspectorEguiImplFnMany = for<'a> fn(
    &mut egui::Ui,
    &dyn Any,
    egui::Id,
    InspectorUi<'_, '_>,
    &mut [&mut dyn Reflect],
    &dyn Fn(&mut dyn Reflect) -> &mut dyn Reflect,
) -> bool;

/// Custom UI implementation for a concrete type.
///
/// # Example Usage
/// ```rust,no_run
/// use bevy::prelude::*;
/// use bevy_inspector_egui::inspector_egui_impls::{InspectorEguiImpl, InspectorPrimitive};
/// use bevy_inspector_egui::quick::ResourceInspectorPlugin;
/// use bevy_inspector_egui::reflect_inspector::InspectorUi;
///
/// #[derive(Reflect, Default)]
/// struct ToggleOption(bool);
///
/// impl InspectorPrimitive for ToggleOption {
///     fn ui(&mut self, ui: &mut egui::Ui, _: &dyn std::any::Any, _: egui::Id, _: InspectorUi<'_, '_>) -> bool {
///         let mut changed = ui.radio_value(&mut self.0, false, "Disabled").changed();
///         changed |= ui.radio_value(&mut self.0, true, "Enabled").changed();
///         changed
///     }
///
///     fn ui_readonly(&self, ui: &mut egui::Ui, _: &dyn std::any::Any, _: egui::Id, _: InspectorUi<'_, '_>) {
///         let mut copy = self.0;
///         ui.add_enabled_ui(false, |ui| {
///             ui.radio_value(&mut copy, false, "Disabled").changed();
///             ui.radio_value(&mut copy, true, "Enabled").changed();
///         });
///     }
/// }
///
/// fn main() {
///     App::new()
///         .add_plugins(DefaultPlugins)
///         // ...
///         .register_type_data::<ToggleOption, InspectorEguiImpl>()
///         .run();
/// }
/// ```
pub trait InspectorPrimitive: Reflect {
    fn ui(
        &mut self,
        ui: &mut egui::Ui,
        options: &dyn Any,
        id: egui::Id,
        env: InspectorUi<'_, '_>,
    ) -> bool;
    fn ui_readonly(
        &self,
        ui: &mut egui::Ui,
        options: &dyn Any,
        id: egui::Id,
        env: InspectorUi<'_, '_>,
    );
}

fn ui_many_vtable<T: Reflect + PartialEq + Clone + Default + InspectorPrimitive>(
    ui: &mut egui::Ui,
    options: &dyn Any,
    id: egui::Id,
    env: InspectorUi<'_, '_>,
    values: &mut [&mut dyn bevy_reflect::Reflect],
    projector: &dyn Fn(&mut dyn bevy_reflect::Reflect) -> &mut dyn bevy_reflect::Reflect,
) -> bool {
    let same = crate::inspector_egui_impls::iter_all_eq(
        values
            .iter_mut()
            .map(|value| projector(*value).downcast_ref::<T>().unwrap()),
    );

    let mut temp = same.cloned().unwrap_or_default();
    if T::ui(&mut temp, ui, options, id, env) {
        for value in values.iter_mut() {
            let value = projector(*value).downcast_mut::<T>().unwrap();
            *value = temp.clone();
        }

        return true;
    }
    false
}

fn ui_vtable<T: InspectorPrimitive>(
    val: &mut dyn Any,
    ui: &mut egui::Ui,
    options: &dyn Any,
    id: egui::Id,
    env: InspectorUi<'_, '_>,
) -> bool {
    let val = val.downcast_mut::<T>().unwrap();
    T::ui(val, ui, options, id, env)
}
fn ui_readonly_vtable<T: InspectorPrimitive>(
    val: &dyn Any,
    ui: &mut egui::Ui,
    options: &dyn Any,
    id: egui::Id,
    env: InspectorUi<'_, '_>,
) {
    let val = val.downcast_ref::<T>().unwrap();
    T::ui_readonly(val, ui, options, id, env)
}

/// Function pointers for displaying a concrete type, to be registered in the [`TypeRegistry`].
///
/// This can used for leaf types like `u8` or `String`, as well as people who want to completely customize the way
/// to display a certain type. You can use [`InspectorPrimitive`] to avoid manually writing the function pointers with correct downcasting.
#[derive(Clone)]
pub struct InspectorEguiImpl {
    fn_mut: InspectorEguiImplFn,
    fn_readonly: InspectorEguiImplFnReadonly,
    fn_many: InspectorEguiImplFnMany,
}

impl<T: InspectorPrimitive> FromType<T> for InspectorEguiImpl {
    fn from_type() -> Self {
        InspectorEguiImpl::of_with_many::<T>(many_unimplemented::<T>)
    }
}

impl InspectorEguiImpl {
    pub fn of<T: InspectorPrimitive + PartialEq + Clone + Default>() -> Self {
        InspectorEguiImpl {
            fn_mut: ui_vtable::<T>,
            fn_readonly: ui_readonly_vtable::<T>,
            fn_many: ui_many_vtable::<T>,
        }
    }
    pub fn of_with_many<T: InspectorPrimitive>(fn_many: InspectorEguiImplFnMany) -> Self {
        InspectorEguiImpl {
            fn_mut: ui_vtable::<T>,
            fn_readonly: ui_readonly_vtable::<T>,
            fn_many,
        }
    }

    /// Create a new [`InspectorEguiImpl`] from functions displaying a type
    pub fn new(
        fn_mut: InspectorEguiImplFn,
        fn_readonly: InspectorEguiImplFnReadonly,
        fn_many: InspectorEguiImplFnMany,
    ) -> Self {
        InspectorEguiImpl {
            fn_mut,
            fn_readonly,
            fn_many,
        }
    }

    pub fn execute<'a, 'c: 'a>(
        &'a self,
        value: &mut dyn Any,
        ui: &mut egui::Ui,
        options: &dyn Any,
        id: egui::Id,
        env: InspectorUi<'_, '_>,
    ) -> bool {
        (self.fn_mut)(value, ui, options, id, env)
    }
    pub fn execute_readonly<'a, 'c: 'a>(
        &'a self,
        value: &dyn Any,
        ui: &mut egui::Ui,
        options: &dyn Any,
        id: egui::Id,
        env: InspectorUi<'_, '_>,
    ) {
        (self.fn_readonly)(value, ui, options, id, env)
    }
    pub fn execute_many<'a, 'c: 'a, 'e>(
        &'a self,
        ui: &mut egui::Ui,
        options: &dyn Any,
        id: egui::Id,
        env: InspectorUi<'_, '_>,
        values: &mut [&mut dyn Reflect],
        projector: &dyn Fn(&mut dyn Reflect) -> &mut dyn Reflect,
    ) -> bool {
        (self.fn_many)(ui, options, id, env, values, projector)
    }
}

fn many_unimplemented<T: Any>(
    ui: &mut egui::Ui,
    _options: &dyn Any,
    _id: egui::Id,
    _env: InspectorUi<'_, '_>,
    _values: &mut [&mut dyn Reflect],
    _projector: &dyn Fn(&mut dyn Reflect) -> &mut dyn Reflect,
) -> bool {
    no_multiedit(ui, &pretty_type_name::pretty_type_name::<T>());
    false
}

fn add<T: InspectorPrimitive + TypePath + PartialEq + Clone + Default>(
    type_registry: &mut TypeRegistry,
) {
    type_registry.register_type_data::<T, InspectorEguiImpl>();
}
fn add_of_with_many<T: InspectorPrimitive>(
    type_registry: &mut TypeRegistry,
    fn_many: InspectorEguiImplFnMany,
) {
    type_registry
        .get_mut(TypeId::of::<T>())
        .unwrap_or_else(|| panic!("{} not registered", std::any::type_name::<T>()))
        .insert(InspectorEguiImpl::of_with_many::<T>(fn_many));
}

fn add_raw<T: 'static>(
    type_registry: &mut TypeRegistry,
    fn_mut: InspectorEguiImplFn,
    fn_readonly: InspectorEguiImplFnReadonly,
    fn_many: InspectorEguiImplFnMany,
) {
    type_registry
        .get_mut(TypeId::of::<T>())
        .unwrap_or_else(|| panic!("{} not registered", std::any::type_name::<T>()))
        .insert(InspectorEguiImpl::new(fn_mut, fn_readonly, fn_many));
}

/// Register [`InspectorEguiImpl`]s for primitive rust types as well as standard library types
#[rustfmt::skip]
pub fn register_std_impls(type_registry: &mut TypeRegistry) {
    add_of_with_many::<f32>(type_registry, std_impls::number_ui_many::<f32>);
    add_of_with_many::<f64>(type_registry, std_impls::number_ui_many::<f64>);
    add_of_with_many::<i8>(type_registry, std_impls::number_ui_many::<i8>);
    add_of_with_many::<i16>(type_registry, std_impls::number_ui_many::<i16>);
    add_of_with_many::<i32>(type_registry, std_impls::number_ui_many::<i32>);
    add_of_with_many::<i64>(type_registry, std_impls::number_ui_many::<i64>);
    add_of_with_many::<isize>(type_registry, std_impls::number_ui_many::<isize>);
    add_of_with_many::<u8>(type_registry, std_impls::number_ui_many::<u8>);
    add_of_with_many::<u16>(type_registry, std_impls::number_ui_many::<u16>);
    add_of_with_many::<u32>(type_registry, std_impls::number_ui_many::<u32>);
    add_of_with_many::<u64>(type_registry, std_impls::number_ui_many::<u64>);
    add_of_with_many::<usize>(type_registry, std_impls::number_ui_many::<usize>);
    add::<bool>(type_registry);
    add::<String>(type_registry);
    add::<Cow<str>>(type_registry);
    add::<PathBuf>(type_registry);

    type_registry.register::<std::ops::Range<f64>>();
    add::<std::ops::Range<f32>>(type_registry);
    add::<std::ops::Range<f64>>(type_registry);

    add::<std::time::Duration>(type_registry);
    add_of_with_many::<Instant>(type_registry, many_unimplemented::<Instant>);
}

/// Register [`InspectorEguiImpl`]s for [`bevy_math`]/`glam` types
#[rustfmt::skip]
pub fn register_glam_impls(type_registry: &mut TypeRegistry) {
    add_raw::<bevy_math::Vec2>(type_registry, glam_impls::vec2_ui, glam_impls::vec2_ui_readonly, glam_impls::vec2_ui_many);
    add_raw::<bevy_math::Vec3>(type_registry, glam_impls::vec3_ui, glam_impls::vec3_ui_readonly, glam_impls::vec3_ui_many);
    add_raw::<bevy_math::Vec3A>(type_registry, glam_impls::vec3a_ui, glam_impls::vec3a_ui_readonly, glam_impls::vec3a_ui_many);
    add_raw::<bevy_math::Vec4>(type_registry, glam_impls::vec4_ui, glam_impls::vec4_ui_readonly, glam_impls::vec4_ui_many);
    add_raw::<bevy_math::UVec2>(type_registry, glam_impls::uvec2_ui, glam_impls::uvec2_ui_readonly, glam_impls::uvec2_ui_many);
    add_raw::<bevy_math::UVec3>(type_registry, glam_impls::uvec3_ui, glam_impls::uvec3_ui_readonly, glam_impls::uvec3_ui_many);
    add_raw::<bevy_math::UVec4>(type_registry, glam_impls::uvec4_ui, glam_impls::uvec4_ui_readonly, glam_impls::uvec4_ui_many);
    add_raw::<bevy_math::IVec2>(type_registry, glam_impls::ivec2_ui, glam_impls::ivec2_ui_readonly, glam_impls::ivec2_ui_many);
    add_raw::<bevy_math::IVec3>(type_registry, glam_impls::ivec3_ui, glam_impls::ivec3_ui_readonly, glam_impls::ivec3_ui_many);
    add_raw::<bevy_math::IVec4>(type_registry, glam_impls::ivec4_ui, glam_impls::ivec4_ui_readonly, glam_impls::ivec4_ui_many);
    add_raw::<bevy_math::DVec2>(type_registry, glam_impls::dvec2_ui, glam_impls::dvec2_ui_readonly, glam_impls::dvec2_ui_many);
    add_raw::<bevy_math::DVec3>(type_registry, glam_impls::dvec3_ui, glam_impls::dvec3_ui_readonly, glam_impls::dvec3_ui_many);
    add_raw::<bevy_math::DVec4>(type_registry, glam_impls::dvec4_ui, glam_impls::dvec4_ui_readonly, glam_impls::dvec4_ui_many);
    add_raw::<bevy_math::BVec2>(type_registry, glam_impls::bvec2_ui, glam_impls::bvec2_ui_readonly, many_unimplemented::<bevy_math::BVec2>);
    add_raw::<bevy_math::BVec3>(type_registry, glam_impls::bvec3_ui, glam_impls::bvec3_ui_readonly, many_unimplemented::<bevy_math::BVec3>);
    add_raw::<bevy_math::BVec4>(type_registry, glam_impls::bvec4_ui, glam_impls::bvec4_ui_readonly, many_unimplemented::<bevy_math::BVec4>);
    add_raw::<bevy_math::Mat2>(type_registry, glam_impls::mat2_ui, glam_impls::mat2_ui_readonly, many_unimplemented::<bevy_math::Mat2>);
    add_raw::<bevy_math::Mat3>(type_registry, glam_impls::mat3_ui, glam_impls::mat3_ui_readonly, many_unimplemented::<bevy_math::Mat3>);
    add_raw::<bevy_math::Mat3A>(type_registry, glam_impls::mat3a_ui, glam_impls::mat3a_ui_readonly, many_unimplemented::<bevy_math::Mat3A>);
    add_raw::<bevy_math::Mat4>(type_registry, glam_impls::mat4_ui, glam_impls::mat4_ui_readonly, many_unimplemented::<bevy_math::Mat4>);
    add_raw::<bevy_math::DMat2>(type_registry, glam_impls::dmat2_ui, glam_impls::dmat2_ui_readonly, many_unimplemented::<bevy_math::DMat2>);
    add_raw::<bevy_math::DMat3>(type_registry, glam_impls::dmat3_ui, glam_impls::dmat3_ui_readonly, many_unimplemented::<bevy_math::DMat3>);
    add_raw::<bevy_math::DMat4>(type_registry, glam_impls::dmat4_ui, glam_impls::dmat4_ui_readonly, many_unimplemented::<bevy_math::DMat4>);

    add_raw::<bevy_math::Quat>(type_registry, glam_impls::quat::quat_ui, glam_impls::quat::quat_ui_readonly, glam_impls::quat::quat_ui_many);
}

/// Register [`InspectorEguiImpl`]s for `bevy` types
#[rustfmt::skip]
pub fn register_bevy_impls(type_registry: &mut TypeRegistry) {
    add_of_with_many::<bevy_asset::Handle<bevy_render::texture::Image>>(type_registry, many_unimplemented::<bevy_asset::Handle<bevy_render::texture::Image>>);
    add_of_with_many::<bevy_asset::Handle<bevy_render::mesh::Mesh>>(type_registry, many_unimplemented::<bevy_asset::Handle<bevy_render::mesh::Mesh>>);
    add_of_with_many::<bevy_ecs::entity::Entity>(type_registry, many_unimplemented::<bevy_ecs::entity::Entity>);
    add::<bevy_render::color::Color>(type_registry);
    add::<bevy_render::view::RenderLayers>(type_registry);
}

pub(crate) fn change_slider<T>(
    ui: &mut egui::Ui,
    id: egui::Id,
    same: Option<T>,
    f: impl FnOnce(T, bool),
) -> bool
where
    T: egui::emath::Numeric + std::ops::Sub<Output = T> + Default + Send + Sync + 'static,
{
    let speed = if T::INTEGRAL { 1.0 } else { 0.1 };

    match same {
        Some(mut same) => {
            let widget = egui::DragValue::new(&mut same).speed(speed);

            let changed = ui.add(widget).changed();
            if changed {
                f(same, true);
            }

            changed
        }
        None => {
            let old_change = ui.memory_mut(|memory| *memory.data.get_temp_mut_or_default::<T>(id));
            let mut change = old_change;

            let widget = egui::DragValue::new(&mut change)
                .speed(speed)
                .custom_formatter(|_, _| "-".to_string());

            let changed = ui.add(widget).changed();
            if changed {
                f(change - old_change, false);
            }

            ui.memory_mut(|memory| *memory.data.get_temp_mut_or_default(id) = change);
            changed
        }
    }
}

pub(crate) fn iter_all_eq<T: PartialEq>(mut iter: impl Iterator<Item = T>) -> Option<T> {
    let first = iter.next()?;
    iter.all(|elem| elem == first).then_some(first)
}

#[macro_export]
#[doc(hidden)]
macro_rules! many_ui {
    ($name:ident $inner:ident $ty:ty) => {
        pub fn $name(
            ui: &mut egui::Ui,
            options: &dyn Any,
            id: egui::Id,
            env: InspectorUi<'_, '_>,
            values: &mut [&mut dyn bevy_reflect::Reflect],
            projector: &dyn Fn(&mut dyn bevy_reflect::Reflect) -> &mut dyn bevy_reflect::Reflect,
        ) -> bool {
            let same = $crate::inspector_egui_impls::iter_all_eq(
                values
                    .iter_mut()
                    .map(|value| projector(*value).downcast_ref::<$ty>().unwrap()),
            );

            let mut temp = same.cloned().unwrap_or_default();
            if $inner(&mut temp, ui, options, id, env) {
                for value in values.iter_mut() {
                    let value = projector(*value).downcast_mut::<$ty>().unwrap();
                    *value = temp.clone();
                }

                return true;
            }
            false
        }
    };
}