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, Default)]
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 /// Create a new empty button group
97 pub fn new() -> Self {
98 Self(Vec::new())
99 }
100}
101
102// ============================================================================
103// Panel Relationships
104// ============================================================================
105
106/// Marks an entity as content within a panel.
107#[derive(Component)]
108#[relationship(relationship_target = PanelContents)]
109pub struct PanelContent(pub Entity);
110
111/// Contains all content entities within this panel.
112#[derive(Component)]
113#[relationship_target(relationship = PanelContent, linked_spawn)]
114pub struct PanelContents(Vec<Entity>);
115
116impl PanelContents {
117 /// Get an iterator over the panel contents
118 pub fn iter(&self) -> impl Iterator<Item = &Entity> {
119 self.0.iter()
120 }
121}
122
123// ============================================================================
124// Text Input Relationships
125// ============================================================================
126
127/// Marks an entity as part of a text input (cursor, selection, etc.).
128#[derive(Component)]
129#[relationship(relationship_target = TextInputParts)]
130pub struct TextInputPart(pub Entity);
131
132/// Contains all parts that make up this text input.
133#[derive(Component)]
134#[relationship_target(relationship = TextInputPart, linked_spawn)]
135pub struct TextInputParts(Vec<Entity>);
136
137impl TextInputParts {
138 /// Get an iterator over the text input parts
139 pub fn iter(&self) -> impl Iterator<Item = &Entity> {
140 self.0.iter()
141 }
142}
143
144// ============================================================================
145// Progress Bar Relationships
146// ============================================================================
147
148/// Marks an entity as part of a progress bar (fill, label, etc.).
149#[derive(Component)]
150#[relationship(relationship_target = ProgressBarParts)]
151pub struct ProgressBarPart(pub Entity);
152
153/// Contains all parts that make up this progress bar.
154#[derive(Component)]
155#[relationship_target(relationship = ProgressBarPart, linked_spawn)]
156pub struct ProgressBarParts(Vec<Entity>);
157
158impl ProgressBarParts {
159 /// Get an iterator over the progress bar parts
160 pub fn iter(&self) -> impl Iterator<Item = &Entity> {
161 self.0.iter()
162 }
163}
164
165// ============================================================================
166// Dropdown Relationships
167// ============================================================================
168
169/// Marks an entity as belonging to a specific dropdown (menu options).
170/// When the dropdown is closed or despawned, all related entities
171/// are automatically cleaned up thanks to linked_spawn.
172#[derive(Component)]
173#[relationship(relationship_target = DropdownElements)]
174pub struct BelongsToDropdown(pub Entity);
175
176/// Contains all entities that belong to this dropdown (menu, options).
177/// The linked_spawn attribute ensures automatic cleanup when dropdown is despawned.
178#[derive(Component)]
179#[relationship_target(relationship = BelongsToDropdown, linked_spawn)]
180pub struct DropdownElements(Vec<Entity>);
181
182impl DropdownElements {
183 /// Get an iterator over the dropdown elements
184 pub fn iter(&self) -> impl Iterator<Item = &Entity> {
185 self.0.iter()
186 }
187}
188
189// ============================================================================
190// Temporary Components (should be moved to appropriate modules)
191// ============================================================================
192
193/// Component marker for Slider (should be defined in slider module)
194#[derive(Component)]
195pub struct Slider {
196 /// Current value of the slider
197 pub value: f32,
198 /// Minimum value
199 pub min: f32,
200 /// Maximum value
201 pub max: f32,
202}