gizmo-scene 0.1.0

A custom ECS and physics engine aimed for realistic simulations.
Documentation
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use gizmo_core::{EntityName, World};
use gizmo_math::Vec4;
use gizmo_renderer::asset::AssetManager;
use gizmo_renderer::components::{Material, Mesh, MeshRenderer};
use serde::{Deserialize, Serialize};
use std::fs;
use std::sync::Arc;

use gizmo_core::component::{Children, Parent};
use std::collections::HashMap;

/// Tam sahne verisi — tüm entity'ler ve bileşenleri
#[derive(Serialize, Deserialize, Clone)]
pub struct SceneData {
    pub entities: Vec<EntityData>,
}

/// Prefab verisi — Tıpkı SceneData gibi ama kök entity'si var
#[derive(Serialize, Deserialize, Clone)]
pub struct PrefabData {
    pub root_id: u32,
    pub entities: Vec<EntityData>,
}

/// Tek bir entity'nin serileştirilebilir verisi
#[derive(Serialize, Deserialize, Clone)]
pub struct EntityData {
    pub original_id: u32,
    pub name: Option<String>,
    pub mesh_source: Option<String>,
    pub material_source: Option<MaterialData>,
    #[serde(default)]
    pub parent_id: Option<u32>,
    #[serde(default)]
    pub components: std::collections::BTreeMap<String, ron::Value>,
}

/// Material serileştirme verisi (GPU bind group'u diske yazılamaz)
#[derive(Serialize, Deserialize, Clone)]
pub struct MaterialData {
    pub albedo: Vec4,
    pub roughness: f32,
    pub metallic: f32,
    pub unlit: f32,
    pub texture_source: Option<String>,
}

impl SceneData {
    /// Mevcut World durumunu JSON dosyası olarak diske kaydeder
    pub fn save(
        world: &World,
        file_path: &str,
        registry: &crate::registry::SceneRegistry,
    ) -> Result<(), String> {
        if let Some(parent) = std::path::Path::new(file_path).parent() {
            let _ = fs::create_dir_all(parent);
        }
        let entities_data = Self::serialize_entities(
            world,
            world
                .iter_alive_entities()
                .into_iter()
                .map(|e| e.id())
                .collect(),
            registry,
        );

        let scene = SceneData {
            entities: entities_data,
        };

        let string_data = ron::ser::to_string_pretty(&scene, ron::ser::PrettyConfig::default())
            .map_err(|e| format!("[SceneData::save] Serileştirme hatası: {}", e))?;

        fs::write(file_path, string_data)
            .map_err(|e| format!("[SceneData::save] Dosya yazma hatası: {}", e))?;

        println!("✅ Sahne kaydedildi → {}", file_path);
        Ok(())
    }

    /// Belirtilen entity ID'lerini serileştirir
    pub fn serialize_entities(
        world: &World,
        entity_ids: Vec<u32>,
        registry: &crate::registry::SceneRegistry,
    ) -> Vec<EntityData> {
        let mut entities_data = Vec::new();
        let names = world.borrow::<EntityName>();
        let meshes = world.borrow::<Mesh>();
        let materials = world.borrow::<Material>();
        let parents = world.borrow::<Parent>();

        for &id in &entity_ids {
            let name = names.get(id).map(|n| n.0.clone());

            // Gizmo Studio'nun içsel araçlarını kaydetme (Gizmo kurguları, Highlight box, grid vs.)
            if let Some(ref n) = name {
                if n.starts_with("Editor ") || n == "Highlight Box" {
                    continue;
                }
            }

            let mesh_source = meshes.get(id).map(|m| m.source.clone());
            let material_source = materials.get(id).map(|m| MaterialData {
                albedo: m.albedo,
                roughness: m.roughness,
                metallic: m.metallic,
                unlit: match m.material_type {
                    gizmo_renderer::components::MaterialType::Skybox => 2.0,
                    gizmo_renderer::components::MaterialType::Unlit => 1.0,
                    _ => 0.0,
                },
                texture_source: m.texture_source.clone(),
            });
            let parent_id = parents.get(id).map(|p| p.0);

            // Dinamik bileşenleri Registry üzerinden tarayarak JSON AST'sine (ron::Value) dönüştür
            let mut dynamic_components = std::collections::BTreeMap::new();
            for comp_name in registry.all_components() {
                if let Some(serializer) = registry.get_serializer(comp_name) {
                    if let Some(comp_value) = serializer(world, id) {
                        dynamic_components.insert(comp_name.clone(), comp_value);
                    }
                }
            }

            if name.is_some()
                || mesh_source.is_some()
                || material_source.is_some()
                || parent_id.is_some()
                || !dynamic_components.is_empty()
            {
                entities_data.push(EntityData {
                    original_id: id,
                    name,
                    mesh_source,
                    material_source,
                    parent_id,
                    components: dynamic_components,
                });
            }
        }
        entities_data
    }

    /// JSON sahne dosyasını okuyup World'e entity olarak yükler
    pub fn load_into(
        file_path: &str,
        world: &mut World,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        texture_bind_group_layout: &wgpu::BindGroupLayout,
        asset_manager: &mut AssetManager,
        default_texture_bind_group: Arc<wgpu::BindGroup>,
        registry: &crate::registry::SceneRegistry,
    ) -> bool {
        let string_data = match fs::read_to_string(file_path) {
            Ok(content) => content,
            Err(_) => return false,
        };

        let scene: SceneData = match ron::from_str(&string_data) {
            Ok(s) => s,
            Err(e) => {
                println!("❌ Sahne dosyası geçersiz ({}): {}", file_path, e);
                return false;
            }
        };

        Self::instantiate_entities(
            scene.entities,
            None,
            world,
            device,
            queue,
            texture_bind_group_layout,
            asset_manager,
            &default_texture_bind_group,
            registry,
        );

        println!("✅ Sahne yüklendi ← {}", file_path);
        true
    }

    /// Verilen entity listesini instantiate eder, id eşleştirmelerini yapar ve gerekirse root bir parent'a bağlar
    pub fn instantiate_entities(
        entities: Vec<EntityData>,
        root_parent: Option<u32>,
        world: &mut World,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        texture_bind_group_layout: &wgpu::BindGroupLayout,
        asset_manager: &mut AssetManager,
        default_texture_bind_group: &Arc<wgpu::BindGroup>,
        registry: &crate::registry::SceneRegistry,
    ) -> HashMap<u32, u32> {
        let mut id_map = HashMap::new(); // original_id -> new_entity_id
        let mut entity_structs = HashMap::new();

        // Entity'leri oluştur ve id haritasını çıkar
        for data in &entities {
            let root_ent = world.spawn();
            id_map.insert(data.original_id, root_ent.id());
            entity_structs.insert(root_ent.id(), root_ent);
        }

        // Parent-Child ilişkilerini toplayacağımız geçici yapı (new_parent -> [new_children])
        let mut children_map: HashMap<u32, Vec<u32>> = HashMap::new();

        for data in entities {
            let new_id = id_map[&data.original_id];
            let entity = entity_structs[&new_id];

            if let Some(n) = data.name {
                world.add_component(entity, EntityName::new(&n));
            }
            // Dinamik bileşenleri Registry üzerinden yükle
            for (comp_name, comp_val) in &data.components {
                if let Some(deserializer) = registry.get_deserializer(comp_name) {
                    deserializer(world, new_id, comp_val);
                }
            }

            if let Some(mesh_src) = data.mesh_source {
                let mesh = if mesh_src == "inverted_cube" {
                    AssetManager::create_inverted_cube(device)
                } else if mesh_src == "plane" {
                    AssetManager::create_plane(device, 200.0)
                } else if mesh_src == "standard_cube" {
                    AssetManager::create_cube(device)
                } else if mesh_src == "sphere" {
                    AssetManager::create_sphere(device, 1.0, 16, 16)
                } else if mesh_src == "sprite_quad" {
                    AssetManager::create_sprite_quad(device, 1.0, 1.0)
                } else if mesh_src.starts_with("gltf_mesh_") {
                    if let Some(cached) = asset_manager.get_cached_mesh(&mesh_src) {
                        cached
                    } else {
                        // Eğer GLTF RAM'de yoksa, path'i çıkar ve gizlice parse edip Cache'e yükle.
                        let file_path = if let Some(idx) = mesh_src.find(".glb") {
                            Some(&mesh_src["gltf_mesh_".len()..idx + 4])
                        } else {
                            mesh_src
                                .find(".gltf")
                                .map(|idx| &mesh_src["gltf_mesh_".len()..idx + 5])
                        };

                        if let Some(path) = file_path {
                            let _ = asset_manager.load_gltf_scene(
                                device,
                                queue,
                                texture_bind_group_layout,
                                default_texture_bind_group.clone(),
                                path,
                            );
                            if let Some(cached) = asset_manager.get_cached_mesh(&mesh_src) {
                                cached
                            } else {
                                asset_manager.loading_placeholder_mesh(device)
                            }
                        } else {
                            asset_manager.loading_placeholder_mesh(device)
                        }
                    }
                } else if mesh_src.starts_with("obj:") {
                    let path = mesh_src.trim_start_matches("obj:");
                    asset_manager.load_obj(device, path)
                } else {
                    // Fail-safe obj loading
                    asset_manager.load_obj(device, &mesh_src)
                };
                world.add_component(entity, mesh);
            }

            if let Some(mat_data) = data.material_source {
                let bind_group = if let Some(tex_path) = &mat_data.texture_source {
                    asset_manager
                        .load_material_texture(device, queue, texture_bind_group_layout, tex_path)
                        .unwrap_or_else(|e| {
                            println!("Scene Texture error: {}", e);
                            default_texture_bind_group.clone()
                        })
                } else {
                    default_texture_bind_group.clone()
                };

                let mut mat = Material::new(bind_group);
                mat.albedo = mat_data.albedo;
                mat.roughness = mat_data.roughness;
                mat.metallic = mat_data.metallic;
                mat.material_type = if mat_data.unlit > 1.5 {
                    gizmo_renderer::components::MaterialType::Skybox
                } else if mat_data.unlit > 0.5 {
                    gizmo_renderer::components::MaterialType::Unlit
                } else {
                    gizmo_renderer::components::MaterialType::Pbr
                };
                mat.texture_source = mat_data.texture_source;
                world.add_component(entity, mat);
                world.add_component(entity, MeshRenderer::new());
            }

            // Hiyerarşi Bağlantıları
            let mut resolved_parent = None;
            if let Some(orig_parent) = data.parent_id {
                // Kendi içerisinde (bu sahnede/prefabda) kaydedilmiş bir parent varsa ona bağla
                if let Some(&p_id) = id_map.get(&orig_parent) {
                    resolved_parent = Some(p_id);
                }
            } else {
                // Eğer entity'nin kendi parent'ı yoksa, root_parent verilmişse ona tak
                resolved_parent = root_parent;
            }

            if let Some(p_id) = resolved_parent {
                world.add_component(entity, Parent(p_id));
                children_map.entry(p_id).or_default().push(new_id);
            }
        }

        // Tüm Children bileşenlerini ilgili parent'lara ekle
        for (p_id, mut c_list) in children_map {
            if let Some(&p_ent) = entity_structs.get(&p_id) {
                world.add_component(p_ent, Children(c_list));
            } else if let Some(p_ent) = world.get_entity(p_id) {
                // External parent (e.g. root_parent passed in) — merge with existing children.
                let existing: Vec<u32> = world
                    .borrow::<Children>()
                    .get(p_id)
                    .map(|c| c.0.clone())
                    .unwrap_or_default();
                let mut merged = existing;
                merged.append(&mut c_list);
                world.add_component(p_ent, Children(merged));
            }
        }

        id_map
    }

    /// Prefab kaydet (Verilen entity ve tüm alt çocukları)
    pub fn save_prefab(
        world: &World,
        root_entity_id: u32,
        file_path: &str,
        registry: &crate::registry::SceneRegistry,
    ) -> Result<(), String> {
        if let Some(parent) = std::path::Path::new(file_path).parent() {
            let _ = fs::create_dir_all(parent);
        }

        let mut ids_to_save = vec![root_entity_id];
        let children_storage = world.borrow::<Children>();

        let mut i = 0;
        while i < ids_to_save.len() {
            let current = ids_to_save[i];
            if let Some(children_comp) = children_storage.get(current) {
                for &child_id in &children_comp.0 {
                    ids_to_save.push(child_id);
                }
            }
            i += 1;
        }

        let mut entities_data = Self::serialize_entities(world, ids_to_save, registry);

        // Prefab'ın root entity'sinin parent'ını kopar ki bağımsız yüklensin
        if let Some(root_data) = entities_data
            .iter_mut()
            .find(|d| d.original_id == root_entity_id)
        {
            root_data.parent_id = None;
        }

        let prefab = PrefabData {
            root_id: root_entity_id,
            entities: entities_data,
        };

        let string_data = ron::ser::to_string_pretty(&prefab, ron::ser::PrettyConfig::default())
            .map_err(|e| format!("[SceneData::save_prefab] Serileştirme hatası: {}", e))?;

        fs::write(file_path, string_data)
            .map_err(|e| format!("[SceneData::save_prefab] Dosya yazma hatası: {}", e))?;

        println!("✅ Prefab kaydedildi → {}", file_path);
        Ok(())
    }

    /// Prefab yükle
    pub fn load_prefab(
        file_path: &str,
        parent_entity: Option<u32>,
        world: &mut World,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        texture_bind_group_layout: &wgpu::BindGroupLayout,
        asset_manager: &mut AssetManager,
        default_texture_bind_group: Arc<wgpu::BindGroup>,
        registry: &crate::registry::SceneRegistry,
    ) -> Option<u32> {
        let string_data = match fs::read_to_string(file_path) {
            Ok(content) => content,
            Err(_) => return None,
        };

        let prefab: PrefabData = match ron::from_str(&string_data) {
            Ok(p) => p,
            Err(e) => {
                println!("❌ Prefab dosyası geçersiz ({}): {}", file_path, e);
                return None;
            }
        };

        let id_map = Self::instantiate_entities(
            prefab.entities,
            parent_entity,
            world,
            device,
            queue,
            texture_bind_group_layout,
            asset_manager,
            &default_texture_bind_group,
            registry,
        );

        let new_root_id = id_map.get(&prefab.root_id).copied();

        if let (Some(new_r), Some(p_id)) = (new_root_id, parent_entity) {
            if let Some(p_ent) = world.get_entity(p_id) {
                let mut children_list = world
                    .borrow::<Children>()
                    .get(p_id)
                    .map(|c| c.0.clone())
                    .unwrap_or_default();
                children_list.push(new_r);
                world.add_component(p_ent, Children(children_list));
            }
        }

        println!("✅ Prefab yüklendi ← {}", file_path);
        new_root_id
    }

    /// Entity listesini döndürür (Lua API'si için)
    pub fn get_entity_names(world: &World) -> Vec<(u32, String)> {
        let mut result = Vec::new();
        let names = world.borrow::<EntityName>();
        for (entity_id, _) in names.iter() {
            if let Some(name) = names.get(entity_id) {
                result.push((entity_id, name.0.clone()));
            }
        }
        result
    }

    /// İsme göre entity bul
    pub fn find_entity_by_name(world: &World, target_name: &str) -> Option<u32> {
        let names = world.borrow::<EntityName>();
        for (entity_id, _) in names.iter() {
            if let Some(name) = names.get(entity_id) {
                if name.0 == target_name {
                    return Some(entity_id);
                }
            }
        }
        None
    }
}

#[cfg(test)]
mod tests {

    // Test removed because EntityData relies dynamically on SceneRegistry serialization
}