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
use std::marker::PhantomData;

use bevy::prelude::Mut;

use crate::Inspectable;

/// The resource inspector can be used to edit resources in the inspector.
///
/// # Example
///
/// ```rust,no_run
/// use bevy::{prelude::*, pbr::AmbientLight};
/// use bevy_inspector_egui::{Inspectable, InspectorPlugin, widgets::ResourceInspector};
///
/// #[derive(Inspectable, Default)]
/// struct Data {
///     clear_color: ResourceInspector<ClearColor>,
///     ambient_light: ResourceInspector<AmbientLight>,
/// }
///
/// fn main() {
///     App::new()
///         .add_plugins(DefaultPlugins)
///         .add_plugin(InspectorPlugin::<Data>::new())
///         .run();
/// }
/// ```
pub struct ResourceInspector<T>(PhantomData<fn() -> T>);
impl<T: Inspectable + Send + Sync + 'static> Inspectable for ResourceInspector<T> {
    type Attributes = T::Attributes;

    fn ui(
        &mut self,
        ui: &mut bevy_egui::egui::Ui,
        options: Self::Attributes,
        context: &mut crate::Context,
    ) -> bool {
        context.resource_scope(ui, "ResourceInspector", |ui, context, mut res: Mut<T>| {
            res.ui(ui, options, context)
        })
    }
}

impl<T> std::fmt::Debug for ResourceInspector<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResourceInspector").finish()
    }
}
impl<T> Default for ResourceInspector<T> {
    fn default() -> Self {
        ResourceInspector(PhantomData)
    }
}