Skip to main content

gizmo_core/
component.rs

1use std::any::Any;
2
3pub trait Component: 'static + Any + Send + Sync + Clone {}
4
5#[macro_export]
6macro_rules! impl_component {
7    ($($t:ty),+ $(,)?) => {
8        $(
9            impl $crate::Component for $t {}
10        )+
11    };
12}
13
14// --- Hiyerarşi (Scene Graph) Bileşenleri ---
15#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
16pub struct Parent(pub u32);
17
18#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
19pub struct Children(pub Vec<u32>);
20
21/// Entity isim bileşeni — Editor, Lua ve Scene Serialization tarafından kullanılır.
22#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
23pub struct EntityName(pub String);
24
25impl EntityName {
26    pub fn new(name: &str) -> Self {
27        Self(name.to_string())
28    }
29}
30
31/// Görünmezlik etiketi: Eğer bu component bir objede varsa render edilmez (veya aktifliği kapatılır).
32#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
33pub struct IsHidden;
34
35/// Geçici silinme etiketi (Undo/Redo ve Çöp kutusu için). Eğer bu component varsa, render, fizik ve picking gibi sistemlerden dışlanır.
36#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
37pub struct IsDeleted;
38
39/// Prefab spawn talebi. Entity'ye eklendiğinde prefab yükleme sistemi tarafından işlenir
40/// ve işlendikten sonra component kaldırılır.
41#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
42pub struct PrefabRequest(pub String);
43
44impl PrefabRequest {
45    pub fn new(name: &str) -> Self {
46        Self(name.to_string())
47    }
48
49    /// Prefab adını döndürür.
50    pub fn name(&self) -> &str {
51        &self.0
52    }
53}
54
55impl std::fmt::Display for EntityName {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        write!(f, "{}", self.0)
58    }
59}
60
61/// A string-based identifier pointing to a mesh asset, used by scene loaders to request mesh loading.
62#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
63pub struct MeshSource(pub String);
64
65/// Pure data description of a material, used by scene loaders.
66#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
67pub struct MaterialSource {
68    pub albedo: [f32; 4],
69    pub roughness: f32,
70    pub metallic: f32,
71    pub unlit: f32,
72    pub texture_source: Option<String>,
73}
74
75impl_component!(Parent, Children, EntityName, IsHidden, PrefabRequest, IsDeleted, MeshSource, MaterialSource);
76
77// ============================================================
78//  Bundle Trait — Birden fazla component'i tek seferde ekleme
79// ============================================================
80
81/// Bevy tarzı Bundle desteği.
82/// Bir struct bu trait'i implemente ederse, `world.spawn_bundle(...)` ile
83/// tüm bileşenler tek seferde eklenebilir.
84///
85/// ```ignore
86/// world.spawn_bundle(CameraBundle {
87///     position: Vec3::new(0.0, 3.0, 10.0),
88///     fov: 60.0_f32.to_radians(),
89///     ..default()
90/// });
91/// ```
92pub trait Bundle {
93    /// Bundle içindeki tüm bileşenleri verilen entity'ye ekler.
94    fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity);
95}
96
97/// Herhangi bir Bundle'a çalışma zamanında veya derleme zamanında dinamik olarak
98/// ekstra component eklemeyi sağlayan zincirlenebilir wrapper.
99pub struct DynamicBundle<B: Bundle, C: Component> {
100    pub bundle: B,
101    pub component: C,
102}
103
104impl<B: Bundle, C: Component> Bundle for DynamicBundle<B, C> {
105    fn apply(self, world: &mut crate::world::World, entity: crate::entity::Entity) {
106        self.bundle.apply(world, entity);
107        world.add_component(entity, self.component);
108    }
109}
110
111/// Tüm Bundle tipleri için otomatik `.with(Component)` zincirleme desteği.
112pub trait BundleExt: Bundle + Sized {
113    /// Bu bundle'ın üzerine ek bir bileşen daha ekler.
114    fn with<C: Component>(self, component: C) -> DynamicBundle<Self, C> {
115        DynamicBundle {
116            bundle: self,
117            component,
118        }
119    }
120}
121
122impl<T: Bundle> BundleExt for T {}