Skip to content

Commit 32f7997

Browse files
Troels JessenSheepyhead
Troels Jessen
andcommitted
Partially document bevy_ui (#3526)
# Objective Updated the docs for bevy_ui as requested by #3492 ## Solution I have documented the parts I understand. anchors.rs is not in use and should be removed, thus I haven't documented that, and some of the more renderer-heavy code is beyond me and needs input from either cart or someone familiar with bevy rendering Co-authored-by: Troels Jessen <[email protected]>
1 parent d34ecd7 commit 32f7997

File tree

11 files changed

+186
-4
lines changed

11 files changed

+186
-4
lines changed

crates/bevy_ui/src/entity.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! This module contains the bundles used in Bevy's UI
2+
13
use crate::{
24
widget::{Button, ImageMode},
35
CalculatedSize, FocusPolicy, Interaction, Node, Style, UiColor, UiImage, CAMERA_UI,
@@ -10,39 +12,66 @@ use bevy_render::{
1012
use bevy_text::Text;
1113
use bevy_transform::prelude::{GlobalTransform, Transform};
1214

15+
/// The basic UI node
1316
#[derive(Bundle, Clone, Debug, Default)]
1417
pub struct NodeBundle {
18+
/// Describes the size of the node
1519
pub node: Node,
20+
/// Describes the style including flexbox settings
1621
pub style: Style,
22+
/// Describes the color of the node
1723
pub color: UiColor,
24+
/// Describes the image of the node
1825
pub image: UiImage,
26+
/// The transform of the node
1927
pub transform: Transform,
28+
/// The global transform of the node
2029
pub global_transform: GlobalTransform,
30+
/// Describes the visibility properties of the node
2131
pub visibility: Visibility,
2232
}
2333

34+
/// A UI node that is an image
2435
#[derive(Bundle, Clone, Debug, Default)]
2536
pub struct ImageBundle {
37+
/// Describes the size of the node
2638
pub node: Node,
39+
/// Describes the style including flexbox settings
2740
pub style: Style,
41+
/// Configures how the image should scale
2842
pub image_mode: ImageMode,
43+
/// The calculated size based on the given image
2944
pub calculated_size: CalculatedSize,
45+
/// The color of the node
3046
pub color: UiColor,
47+
/// The image of the node
3148
pub image: UiImage,
49+
/// The transform of the node
3250
pub transform: Transform,
51+
/// The global transform of the node
3352
pub global_transform: GlobalTransform,
53+
/// Describes the visibility properties of the node
3454
pub visibility: Visibility,
3555
}
3656

57+
/// A UI node that is text
3758
#[derive(Bundle, Clone, Debug)]
3859
pub struct TextBundle {
60+
/// Describes the size of the node
3961
pub node: Node,
62+
/// Describes the style including flexbox settings
4063
pub style: Style,
64+
/// Contains the text of the node
4165
pub text: Text,
66+
/// The calculated size based on the given image
4267
pub calculated_size: CalculatedSize,
68+
/// Whether this node should block interaction with lower nodes
4369
pub focus_policy: FocusPolicy,
70+
/// The transform of the node
4471
pub transform: Transform,
72+
/// The global transform of the node
4573
pub global_transform: GlobalTransform,
74+
/// Describes the visibility properties of the node
4675
pub visibility: Visibility,
4776
}
4877

@@ -61,17 +90,28 @@ impl Default for TextBundle {
6190
}
6291
}
6392

93+
/// A UI node that is a button
6494
#[derive(Bundle, Clone, Debug)]
6595
pub struct ButtonBundle {
96+
/// Describes the size of the node
6697
pub node: Node,
98+
/// Marker component that signals this node is a button
6799
pub button: Button,
100+
/// Describes the style including flexbox settings
68101
pub style: Style,
102+
/// Describes whether and how the button has been interacted with by the input
69103
pub interaction: Interaction,
104+
/// Whether this node should block interaction with lower nodes
70105
pub focus_policy: FocusPolicy,
106+
/// The color of the node
71107
pub color: UiColor,
108+
/// The image of the node
72109
pub image: UiImage,
110+
/// The transform of the node
73111
pub transform: Transform,
112+
/// The global transform of the node
74113
pub global_transform: GlobalTransform,
114+
/// Describes the visibility properties of the node
75115
pub visibility: Visibility,
76116
}
77117

@@ -92,12 +132,18 @@ impl Default for ButtonBundle {
92132
}
93133
}
94134

135+
/// The camera that is needed to see UI elements
95136
#[derive(Bundle, Debug)]
96137
pub struct UiCameraBundle {
138+
/// The camera component
97139
pub camera: Camera,
140+
/// The orthographic projection settings
98141
pub orthographic_projection: OrthographicProjection,
142+
/// The transform of the camera
99143
pub transform: Transform,
144+
/// The global transform of the camera
100145
pub global_transform: GlobalTransform,
146+
/// Contains visible entities
101147
// FIXME there is no frustrum culling for UI
102148
pub visible_entities: VisibleEntities,
103149
}

crates/bevy_ui/src/focus.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ use bevy_window::Windows;
1414
use serde::{Deserialize, Serialize};
1515
use smallvec::SmallVec;
1616

17+
/// Describes what type of input interaction has occurred for a UI node.
18+
///
19+
/// This is commonly queried with a `Changed<Interaction>` filter.
1720
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
1821
#[reflect_value(Component, Serialize, Deserialize, PartialEq)]
1922
pub enum Interaction {
23+
/// The node has been clicked
2024
Clicked,
25+
/// The node has been hovered over
2126
Hovered,
27+
/// Nothing has happened
2228
None,
2329
}
2430

@@ -28,10 +34,13 @@ impl Default for Interaction {
2834
}
2935
}
3036

37+
/// Describes whether the node should block interactions with lower nodes
3138
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
3239
#[reflect_value(Component, Serialize, Deserialize, PartialEq)]
3340
pub enum FocusPolicy {
41+
/// Blocks interaction
3442
Block,
43+
/// Lets interaction pass through
3544
Pass,
3645
}
3746

@@ -41,11 +50,13 @@ impl Default for FocusPolicy {
4150
}
4251
}
4352

53+
/// Contains entities whose Interaction should be set to None
4454
#[derive(Default)]
4555
pub struct State {
4656
entities_to_reset: SmallVec<[Entity; 1]>,
4757
}
4858

59+
/// The system that sets Interaction for all UI elements based on the mouse cursor activity
4960
#[allow(clippy::type_complexity)]
5061
pub fn ui_focus_system(
5162
mut state: Local<State>,

crates/bevy_ui/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! This crate contains Bevy's UI system, which can be used to create UI for both 2D and 3D games
2+
//! # Basic usage
3+
//! Spawn [`entity::UiCameraBundle`] and spawn UI elements with [`entity::ButtonBundle`], [`entity::ImageBundle`], [`entity::TextBundle`] and [`entity::NodeBundle`]
4+
//! This UI is laid out with the Flexbox paradigm (see <https://cssreference.io/flexbox/> ) except the vertical axis is inverted
15
mod flex;
26
mod focus;
37
mod margins;
@@ -14,6 +18,7 @@ pub use margins::*;
1418
pub use render::*;
1519
pub use ui_node::*;
1620

21+
#[doc(hidden)]
1722
pub mod prelude {
1823
#[doc(hidden)]
1924
pub use crate::{entity::*, ui_node::*, widget::Button, Interaction, Margins};
@@ -26,13 +31,16 @@ use bevy_math::{Rect, Size};
2631
use bevy_transform::TransformSystem;
2732
use update::{ui_z_system, update_clipping_system};
2833

34+
/// The basic plugin for Bevy UI
2935
#[derive(Default)]
3036
pub struct UiPlugin;
3137

38+
/// The label enum labeling the types of systems in the Bevy UI
3239
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
3340
pub enum UiSystem {
3441
/// After this label, the ui flex state has been updated
3542
Flex,
43+
/// After this label, input interactions with UI entities have been updated for this frame
3644
Focus,
3745
}
3846

crates/bevy_ui/src/margins.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
/// Defines the margins of a UI node
12
#[derive(Debug, Clone)]
23
pub struct Margins {
4+
/// Left margin size in pixels
35
pub left: f32,
6+
/// Right margin size in pixels
47
pub right: f32,
8+
/// Bottom margin size in pixels
59
pub bottom: f32,
10+
/// Top margin size in pixels
611
pub top: f32,
712
}
813

914
impl Margins {
15+
/// Creates a new Margins based on the input
1016
pub fn new(left: f32, right: f32, bottom: f32, top: f32) -> Self {
1117
Margins {
1218
left,

crates/bevy_ui/src/render/camera.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use bevy_ecs::prelude::*;
22
use bevy_render::{camera::ActiveCameras, render_phase::RenderPhase};
33

4+
/// The name of the UI camera
45
pub const CAMERA_UI: &str = "camera_ui";
56

7+
/// Inserts the [`RenderPhase`] into the UI camera
68
pub fn extract_ui_camera_phases(mut commands: Commands, active_cameras: Res<ActiveCameras>) {
79
if let Some(camera_ui) = active_cameras.get(CAMERA_UI) {
810
if let Some(entity) = camera_ui.entity {

0 commit comments

Comments
 (0)