khora_editor/panels/asset_browser/
handlers.rs1use khora_sdk::editor_ui::Icon;
29
30use crate::widgets::tile::AssetTileKind;
31
32#[derive(Debug, Clone)]
34pub enum ActivationKind {
35 LoadScene { abs_path: String },
39 SpawnPrefab {
49 #[allow(dead_code)]
50 abs_path: String,
51 },
52 OpenExternal { abs_path: String },
54}
55
56#[allow(dead_code)]
62pub trait AssetTypeHandler: Send + Sync + 'static {
63 fn matches_type_name(&self, type_name: &str) -> bool;
65 fn tile_kind(&self) -> AssetTileKind;
66 fn icon(&self) -> Icon;
67 fn category_label(&self) -> &'static str;
68 fn activate(&self, abs_path: String) -> ActivationKind {
70 ActivationKind::OpenExternal { abs_path }
71 }
72}
73
74pub struct AssetTypeHandlerRegistration {
76 pub handler: &'static dyn AssetTypeHandler,
77}
78
79inventory::collect!(AssetTypeHandlerRegistration);
80
81pub fn handler_for(type_name: &str) -> Option<&'static dyn AssetTypeHandler> {
85 inventory::iter::<AssetTypeHandlerRegistration>
86 .into_iter()
87 .map(|r| r.handler)
88 .find(|h| h.matches_type_name(type_name))
89}
90
91pub fn tile_kind_for(type_name: &str) -> AssetTileKind {
93 handler_for(type_name)
94 .map(|h| h.tile_kind())
95 .unwrap_or(AssetTileKind::Unknown)
96}
97
98pub mod builtins {
101 use super::*;
102
103 pub struct SceneHandler;
104 impl AssetTypeHandler for SceneHandler {
105 fn matches_type_name(&self, n: &str) -> bool {
106 n == "scene"
107 }
108 fn tile_kind(&self) -> AssetTileKind {
109 AssetTileKind::Scene
110 }
111 fn icon(&self) -> Icon {
112 Icon::Film
113 }
114 fn category_label(&self) -> &'static str {
115 "Scenes"
116 }
117 fn activate(&self, abs_path: String) -> ActivationKind {
118 ActivationKind::LoadScene { abs_path }
119 }
120 }
121
122 pub struct MeshHandler;
123 impl AssetTypeHandler for MeshHandler {
124 fn matches_type_name(&self, n: &str) -> bool {
125 n == "mesh"
126 }
127 fn tile_kind(&self) -> AssetTileKind {
128 AssetTileKind::Mesh
129 }
130 fn icon(&self) -> Icon {
131 Icon::Cube
132 }
133 fn category_label(&self) -> &'static str {
134 "Meshes"
135 }
136 }
137
138 pub struct TextureHandler;
139 impl AssetTypeHandler for TextureHandler {
140 fn matches_type_name(&self, n: &str) -> bool {
141 n == "texture"
142 }
143 fn tile_kind(&self) -> AssetTileKind {
144 AssetTileKind::Texture
145 }
146 fn icon(&self) -> Icon {
147 Icon::Image
148 }
149 fn category_label(&self) -> &'static str {
150 "Textures"
151 }
152 }
153
154 pub struct AudioHandler;
155 impl AssetTypeHandler for AudioHandler {
156 fn matches_type_name(&self, n: &str) -> bool {
157 n == "audio"
158 }
159 fn tile_kind(&self) -> AssetTileKind {
160 AssetTileKind::Audio
161 }
162 fn icon(&self) -> Icon {
163 Icon::Music
164 }
165 fn category_label(&self) -> &'static str {
166 "Audio"
167 }
168 }
169
170 pub struct ShaderHandler;
171 impl AssetTypeHandler for ShaderHandler {
172 fn matches_type_name(&self, n: &str) -> bool {
173 n == "shader"
174 }
175 fn tile_kind(&self) -> AssetTileKind {
176 AssetTileKind::Shader
177 }
178 fn icon(&self) -> Icon {
179 Icon::Zap
180 }
181 fn category_label(&self) -> &'static str {
182 "Shaders"
183 }
184 }
185
186 pub struct PrefabHandler;
187 impl AssetTypeHandler for PrefabHandler {
188 fn matches_type_name(&self, n: &str) -> bool {
189 n == "prefab"
190 }
191 fn tile_kind(&self) -> AssetTileKind {
192 AssetTileKind::Scene
193 }
194 fn icon(&self) -> Icon {
195 Icon::Cube
196 }
197 fn category_label(&self) -> &'static str {
198 "Prefabs"
199 }
200 fn activate(&self, abs_path: String) -> ActivationKind {
201 ActivationKind::SpawnPrefab { abs_path }
202 }
203 }
204
205 pub struct MaterialHandler;
206 impl AssetTypeHandler for MaterialHandler {
207 fn matches_type_name(&self, n: &str) -> bool {
208 n == "material"
209 }
210 fn tile_kind(&self) -> AssetTileKind {
211 AssetTileKind::Material
212 }
213 fn icon(&self) -> Icon {
214 Icon::Circle
215 }
216 fn category_label(&self) -> &'static str {
217 "Materials"
218 }
219 }
220
221 pub struct ScriptHandler;
222 impl AssetTypeHandler for ScriptHandler {
223 fn matches_type_name(&self, n: &str) -> bool {
224 n == "script"
225 }
226 fn tile_kind(&self) -> AssetTileKind {
227 AssetTileKind::Script
228 }
229 fn icon(&self) -> Icon {
230 Icon::Code
231 }
232 fn category_label(&self) -> &'static str {
233 "Scripts"
234 }
235 }
236
237 inventory::submit! {
238 super::AssetTypeHandlerRegistration { handler: &SceneHandler }
239 }
240 inventory::submit! {
241 super::AssetTypeHandlerRegistration { handler: &MeshHandler }
242 }
243 inventory::submit! {
244 super::AssetTypeHandlerRegistration { handler: &TextureHandler }
245 }
246 inventory::submit! {
247 super::AssetTypeHandlerRegistration { handler: &AudioHandler }
248 }
249 inventory::submit! {
250 super::AssetTypeHandlerRegistration { handler: &ShaderHandler }
251 }
252 inventory::submit! {
253 super::AssetTypeHandlerRegistration { handler: &ScriptHandler }
254 }
255 inventory::submit! {
256 super::AssetTypeHandlerRegistration { handler: &PrefabHandler }
257 }
258 inventory::submit! {
259 super::AssetTypeHandlerRegistration { handler: &MaterialHandler }
260 }
261}