|
| 1 | +//! Demonstrates how the `AlignItems` and `JustifyContent` properties can be composed to layout text. |
| 2 | +use bevy::prelude::*; |
| 3 | + |
| 4 | +const ALIGN_ITEMS_COLOR: Color = Color::rgb(1., 0.066, 0.349); |
| 5 | +const JUSTIFY_CONTENT_COLOR: Color = Color::rgb(0.102, 0.522, 1.); |
| 6 | +const MARGIN: Val = Val::Px(5.); |
| 7 | + |
| 8 | +fn main() { |
| 9 | + App::new() |
| 10 | + .add_plugins(DefaultPlugins.set(WindowPlugin { |
| 11 | + primary_window: Some(Window { |
| 12 | + resolution: [870., 1066.].into(), |
| 13 | + title: "Bevy Text Layout Example".to_string(), |
| 14 | + ..Default::default() |
| 15 | + }), |
| 16 | + ..Default::default() |
| 17 | + })) |
| 18 | + .add_startup_system(spawn_layout) |
| 19 | + .run(); |
| 20 | +} |
| 21 | + |
| 22 | +fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 23 | + let font = asset_server.load("fonts/FiraSans-Bold.ttf"); |
| 24 | + commands.spawn(Camera2dBundle::default()); |
| 25 | + commands |
| 26 | + .spawn(NodeBundle { |
| 27 | + style: Style { |
| 28 | + // fill the entire window |
| 29 | + size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), |
| 30 | + flex_direction: FlexDirection::Column, |
| 31 | + align_items: AlignItems::Center, |
| 32 | + ..Default::default() |
| 33 | + }, |
| 34 | + background_color: BackgroundColor(Color::BLACK), |
| 35 | + ..Default::default() |
| 36 | + }) |
| 37 | + .with_children(|builder| { |
| 38 | + // spawn the key |
| 39 | + builder |
| 40 | + .spawn(NodeBundle { |
| 41 | + style: Style { |
| 42 | + flex_direction: FlexDirection::Row, |
| 43 | + margin: UiRect::top(MARGIN), |
| 44 | + ..Default::default() |
| 45 | + }, |
| 46 | + ..Default::default() |
| 47 | + }) |
| 48 | + .with_children(|builder| { |
| 49 | + spawn_nested_text_bundle( |
| 50 | + builder, |
| 51 | + font.clone(), |
| 52 | + ALIGN_ITEMS_COLOR, |
| 53 | + UiRect::right(MARGIN), |
| 54 | + "AlignItems", |
| 55 | + ); |
| 56 | + spawn_nested_text_bundle( |
| 57 | + builder, |
| 58 | + font.clone(), |
| 59 | + JUSTIFY_CONTENT_COLOR, |
| 60 | + UiRect::default(), |
| 61 | + "JustifyContent", |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + builder |
| 66 | + .spawn(NodeBundle { |
| 67 | + style: Style { |
| 68 | + min_size: Size::new(Val::Px(850.), Val::Px(1020.)), |
| 69 | + flex_direction: FlexDirection::Column, |
| 70 | + ..Default::default() |
| 71 | + }, |
| 72 | + ..Default::default() |
| 73 | + }) |
| 74 | + .with_children(|builder| { |
| 75 | + // spawn one child node for each combination of `AlignItems` and `JustifyContent` |
| 76 | + let justifications = [ |
| 77 | + JustifyContent::FlexStart, |
| 78 | + JustifyContent::Center, |
| 79 | + JustifyContent::FlexEnd, |
| 80 | + JustifyContent::SpaceEvenly, |
| 81 | + JustifyContent::SpaceAround, |
| 82 | + JustifyContent::SpaceBetween, |
| 83 | + ]; |
| 84 | + let alignments = [ |
| 85 | + AlignItems::Baseline, |
| 86 | + AlignItems::FlexStart, |
| 87 | + AlignItems::Center, |
| 88 | + AlignItems::FlexEnd, |
| 89 | + AlignItems::Stretch, |
| 90 | + ]; |
| 91 | + for justify_content in justifications { |
| 92 | + builder |
| 93 | + .spawn(NodeBundle { |
| 94 | + style: Style { |
| 95 | + flex_direction: FlexDirection::Row, |
| 96 | + ..Default::default() |
| 97 | + }, |
| 98 | + ..Default::default() |
| 99 | + }) |
| 100 | + .with_children(|builder| { |
| 101 | + for align_items in alignments { |
| 102 | + spawn_child_node( |
| 103 | + builder, |
| 104 | + font.clone(), |
| 105 | + align_items, |
| 106 | + justify_content, |
| 107 | + ); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + }); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +fn spawn_child_node( |
| 116 | + builder: &mut ChildBuilder, |
| 117 | + font: Handle<Font>, |
| 118 | + align_items: AlignItems, |
| 119 | + justify_content: JustifyContent, |
| 120 | +) { |
| 121 | + builder |
| 122 | + .spawn(NodeBundle { |
| 123 | + style: Style { |
| 124 | + flex_direction: FlexDirection::Column, |
| 125 | + align_items, |
| 126 | + justify_content, |
| 127 | + size: Size::new(Val::Px(160.), Val::Px(160.)), |
| 128 | + margin: UiRect::all(MARGIN), |
| 129 | + ..Default::default() |
| 130 | + }, |
| 131 | + background_color: BackgroundColor(Color::DARK_GRAY), |
| 132 | + ..Default::default() |
| 133 | + }) |
| 134 | + .with_children(|builder| { |
| 135 | + let labels = [ |
| 136 | + (format!("{align_items:?}"), ALIGN_ITEMS_COLOR, 0.), |
| 137 | + (format!("{justify_content:?}"), JUSTIFY_CONTENT_COLOR, 3.), |
| 138 | + ]; |
| 139 | + for (text, color, top_margin) in labels { |
| 140 | + // We nest the text within a parent node because margins and padding can't be directly applied to text nodes currently. |
| 141 | + spawn_nested_text_bundle( |
| 142 | + builder, |
| 143 | + font.clone(), |
| 144 | + color, |
| 145 | + UiRect::top(Val::Px(top_margin)), |
| 146 | + &text, |
| 147 | + ); |
| 148 | + } |
| 149 | + }); |
| 150 | +} |
| 151 | + |
| 152 | +fn spawn_nested_text_bundle( |
| 153 | + builder: &mut ChildBuilder, |
| 154 | + font: Handle<Font>, |
| 155 | + background_color: Color, |
| 156 | + margin: UiRect, |
| 157 | + text: &str, |
| 158 | +) { |
| 159 | + builder |
| 160 | + .spawn(NodeBundle { |
| 161 | + style: Style { |
| 162 | + margin, |
| 163 | + padding: UiRect { |
| 164 | + top: Val::Px(1.), |
| 165 | + left: Val::Px(5.), |
| 166 | + right: Val::Px(5.), |
| 167 | + bottom: Val::Px(1.), |
| 168 | + }, |
| 169 | + ..Default::default() |
| 170 | + }, |
| 171 | + background_color: BackgroundColor(background_color), |
| 172 | + ..Default::default() |
| 173 | + }) |
| 174 | + .with_children(|builder| { |
| 175 | + builder.spawn(TextBundle::from_section( |
| 176 | + text, |
| 177 | + TextStyle { |
| 178 | + font, |
| 179 | + font_size: 24.0, |
| 180 | + color: Color::BLACK, |
| 181 | + }, |
| 182 | + )); |
| 183 | + }); |
| 184 | +} |
0 commit comments