bevy_ui_builders/relationships/
types.rs

1//! Component types for UI relationships
2//!
3//! Defines all custom relationship components using Bevy 0.16's relationship system.
4//! These components provide automatic cleanup, type-safe entity connections,
5//! and efficient querying of related UI elements.
6
7use bevy::prelude::*;
8
9// ============================================================================
10// Dialog Relationships
11// ============================================================================
12
13/// Marks an entity as belonging to a specific dialog.
14/// When the dialog is despawned, all entities with this component
15/// are automatically cleaned up thanks to linked_spawn.
16#[derive(Component)]
17#[relationship(relationship_target = DialogElements)]
18pub struct BelongsToDialog(pub Entity);
19
20/// Contains all entities that belong to this dialog.
21/// The linked_spawn attribute ensures automatic cleanup.
22#[derive(Component)]
23#[relationship_target(relationship = BelongsToDialog, linked_spawn)]
24pub struct DialogElements(Vec<Entity>);
25
26impl DialogElements {
27    /// Get an iterator over the dialog elements
28    pub fn iter(&self) -> impl Iterator<Item = &Entity> {
29        self.0.iter()
30    }
31}
32
33// ============================================================================
34// Slider Relationships
35// ============================================================================
36
37/// Marks an entity as part of a slider (track, handle, label, etc.).
38#[derive(Component)]
39#[relationship(relationship_target = SliderParts)]
40pub struct SliderPart(pub Entity);
41
42/// Contains all parts that make up this slider.
43#[derive(Component)]
44#[relationship_target(relationship = SliderPart, linked_spawn)]
45pub struct SliderParts(Vec<Entity>);
46
47impl SliderParts {
48    /// Get an iterator over the slider parts
49    pub fn iter(&self) -> impl Iterator<Item = &Entity> {
50        self.0.iter()
51    }
52}
53
54// ============================================================================
55// Form Relationships
56// ============================================================================
57
58/// Marks an entity as a field belonging to a form.
59#[derive(Component)]
60#[relationship(relationship_target = FormFields)]
61pub struct BelongsToForm(pub Entity);
62
63/// Contains all fields that belong to this form.
64#[derive(Component)]
65#[relationship_target(relationship = BelongsToForm, linked_spawn)]
66pub struct FormFields(Vec<Entity>);
67
68impl FormFields {
69    /// Get an iterator over the form fields
70    pub fn iter(&self) -> impl Iterator<Item = &Entity> {
71        self.0.iter()
72    }
73}
74
75// ============================================================================
76// Button Group Relationships
77// ============================================================================
78
79/// Marks a button as part of a button group (e.g., radio buttons).
80#[derive(Component)]
81#[relationship(relationship_target = ButtonGroupMembers)]
82pub struct InButtonGroup(pub Entity);
83
84/// Contains all buttons in this button group.
85/// Note: No linked_spawn here as button groups may outlive individual buttons.
86#[derive(Component)]
87#[relationship_target(relationship = InButtonGroup)]
88pub struct ButtonGroupMembers(Vec<Entity>);
89
90impl ButtonGroupMembers {
91    /// Get an iterator over the button group members
92    pub fn iter(&self) -> impl Iterator<Item = &Entity> {
93        self.0.iter()
94    }
95}
96
97// ============================================================================
98// Panel Relationships
99// ============================================================================
100
101/// Marks an entity as content within a panel.
102#[derive(Component)]
103#[relationship(relationship_target = PanelContents)]
104pub struct PanelContent(pub Entity);
105
106/// Contains all content entities within this panel.
107#[derive(Component)]
108#[relationship_target(relationship = PanelContent, linked_spawn)]
109pub struct PanelContents(Vec<Entity>);
110
111impl PanelContents {
112    /// Get an iterator over the panel contents
113    pub fn iter(&self) -> impl Iterator<Item = &Entity> {
114        self.0.iter()
115    }
116}
117
118// ============================================================================
119// Text Input Relationships
120// ============================================================================
121
122/// Marks an entity as part of a text input (cursor, selection, etc.).
123#[derive(Component)]
124#[relationship(relationship_target = TextInputParts)]
125pub struct TextInputPart(pub Entity);
126
127/// Contains all parts that make up this text input.
128#[derive(Component)]
129#[relationship_target(relationship = TextInputPart, linked_spawn)]
130pub struct TextInputParts(Vec<Entity>);
131
132impl TextInputParts {
133    /// Get an iterator over the text input parts
134    pub fn iter(&self) -> impl Iterator<Item = &Entity> {
135        self.0.iter()
136    }
137}
138
139// ============================================================================
140// Progress Bar Relationships
141// ============================================================================
142
143/// Marks an entity as part of a progress bar (fill, label, etc.).
144#[derive(Component)]
145#[relationship(relationship_target = ProgressBarParts)]
146pub struct ProgressBarPart(pub Entity);
147
148/// Contains all parts that make up this progress bar.
149#[derive(Component)]
150#[relationship_target(relationship = ProgressBarPart, linked_spawn)]
151pub struct ProgressBarParts(Vec<Entity>);
152
153impl ProgressBarParts {
154    /// Get an iterator over the progress bar parts
155    pub fn iter(&self) -> impl Iterator<Item = &Entity> {
156        self.0.iter()
157    }
158}
159
160// ============================================================================
161// Temporary Components (should be moved to appropriate modules)
162// ============================================================================
163
164/// Component marker for Slider (should be defined in slider module)
165#[derive(Component)]
166pub struct Slider {
167    /// Current value of the slider
168    pub value: f32,
169    /// Minimum value
170    pub min: f32,
171    /// Maximum value
172    pub max: f32,
173}