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
use std::{any::TypeId, borrow::Cow};

use bevy_asset::UntypedAssetId;
use bevy_ecs::entity::Entity;
use bevy_reflect::TypeRegistry;
use egui::FontId;

use crate::{egui_utils::layout_job, restricted_world_view::Error};

pub fn show_error(error: Error, ui: &mut egui::Ui, name_of_type: &str) {
    match error {
        Error::NoAccessToResource(_) => no_access_resource(ui, name_of_type),
        Error::NoAccessToComponent((entity, _)) => no_access_component(ui, entity, name_of_type),
        Error::ComponentDoesNotExist((entity, _)) => {
            component_does_not_exist(ui, entity, name_of_type)
        }
        Error::ResourceDoesNotExist(_) => resource_does_not_exist(ui, name_of_type),
        Error::NoComponentId(_) => no_component_id(ui, name_of_type),
        Error::NoTypeRegistration(_) => {
            crate::reflect_inspector::errors::not_in_type_registry(ui, name_of_type)
        }
        Error::NoTypeData(_, data) => no_type_data(ui, name_of_type, data),
    }
}

pub fn no_access_resource(ui: &mut egui::Ui, type_name: &str) {
    let job = layout_job(&[
        (FontId::proportional(13.0), "No access to resource "),
        (FontId::monospace(12.0), type_name),
        (FontId::proportional(13.0), "."),
    ]);

    ui.label(job);
}
pub fn no_access_component(ui: &mut egui::Ui, entity: Entity, type_name: &str) {
    let job = layout_job(&[
        (FontId::proportional(13.0), "No access to component "),
        (FontId::monospace(12.0), type_name),
        (FontId::proportional(13.0), " on entity "),
        (FontId::monospace(12.0), &format!("{entity:?}")),
        (FontId::proportional(13.0), "."),
    ]);

    ui.label(job);
}

pub fn resource_does_not_exist(ui: &mut egui::Ui, name: &str) {
    let job = layout_job(&[
        (FontId::proportional(13.0), "Resource "),
        (FontId::monospace(12.0), name),
        (FontId::proportional(13.0), " does not exist in the world."),
    ]);

    ui.label(job);
}

pub fn component_does_not_exist(ui: &mut egui::Ui, entity: Entity, name: &str) {
    let job = layout_job(&[
        (FontId::proportional(13.0), "Component "),
        (FontId::monospace(12.0), name),
        (FontId::proportional(13.0), " does not exist on entity "),
        (FontId::monospace(12.0), &format!("{entity:?}")),
        (FontId::proportional(13.0), "."),
    ]);

    ui.label(job);
}

pub fn no_component_id(ui: &mut egui::Ui, type_name: &str) {
    let job = layout_job(&[
        (FontId::monospace(12.0), type_name),
        (FontId::proportional(13.0), " has no associated "),
        (FontId::monospace(12.0), "ComponentId"),
        (FontId::proportional(13.0), "."),
    ]);

    ui.label(job);
}

pub fn no_type_data(ui: &mut egui::Ui, type_name: &str, type_data: &str) {
    let job = layout_job(&[
        (FontId::monospace(12.0), type_name),
        (FontId::proportional(13.0), " has no "),
        (FontId::monospace(12.0), type_data),
        (
            FontId::proportional(13.0),
            " type data, so it cannot be displayed",
        ),
    ]);

    ui.label(job);
}

pub fn entity_does_not_exist(ui: &mut egui::Ui, entity: Entity) {
    let job = layout_job(&[
        (FontId::proportional(13.0), "Entity "),
        (FontId::monospace(12.0), &format!("{entity:?}")),
        (FontId::proportional(13.0), " does not exist."),
    ]);

    ui.label(job);
}

pub fn no_world_in_context(ui: &mut egui::Ui, type_name: &str) {
    let job = layout_job(&[
        (FontId::monospace(12.0), type_name),
        (FontId::proportional(13.0), " needs the bevy world in the "),
        (FontId::monospace(12.0), "InspectorUi"),
        (
            FontId::proportional(13.0),
            " context to provide meaningful information.",
        ),
    ]);

    ui.label(job);
}

pub fn dead_asset_handle(ui: &mut egui::Ui, handle: UntypedAssetId) {
    let job = layout_job(&[
        (FontId::proportional(13.0), "Handle "),
        (FontId::monospace(12.0), &format!("{handle:?}")),
        (FontId::proportional(13.0), " points to no asset."),
    ]);

    ui.label(job);
}

pub fn state_does_not_exist(ui: &mut egui::Ui, name: &str) {
    let job = layout_job(&[
        (FontId::proportional(13.0), "State "),
        (FontId::monospace(12.0), name),
        (
            FontId::proportional(13.0),
            " does not exist. Did you forget to call ",
        ),
        (
            FontId::monospace(12.0),
            &format!(".add_state::<{name}>(..)"),
        ),
        (FontId::proportional(13.0), "?"),
    ]);

    ui.label(job);
}

pub fn no_type_id(ui: &mut egui::Ui, component_name: &str) {
    let job = layout_job(&[
        (FontId::monospace(12.0), component_name),
        (
            FontId::proportional(13.0),
            " is not backed by a rust type, so it cannot be displayed.",
        ),
    ]);

    ui.label(job);
}

pub fn name_of_type(type_id: TypeId, type_registry: &TypeRegistry) -> Cow<str> {
    type_registry
        .get(type_id)
        .map(|registration| Cow::Borrowed(registration.type_info().type_path_table().short_path()))
        .unwrap_or_else(|| Cow::Owned(format!("{type_id:?}")))
}