| 1 |
|
| 2 |
use bevy::{color::palettes::css::*, prelude::*}; |
| 3 |
|
| 4 |
fn main() { |
| 5 |
App::new() |
| 6 |
.add_plugins(DefaultPlugins.set(WindowPlugin { |
| 7 |
primary_window: Some(Window { |
| 8 |
resolution: (800, 600).into(), |
| 9 |
title: "Bevy CSS Grid Layout Example".to_string(), |
| 10 |
..default() |
| 11 |
}), |
| 12 |
..default() |
| 13 |
})) |
| 14 |
.add_systems(Startup, spawn_layout) |
| 15 |
.run(); |
| 16 |
} |
| 17 |
|
| 18 |
fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 19 |
let font = asset_server.load("fonts/FiraSans-Bold.ttf"); |
| 20 |
commands.spawn(Camera2d); |
| 21 |
|
| 22 |
|
| 23 |
commands |
| 24 |
.spawn(( |
| 25 |
Node { |
| 26 |
|
| 27 |
display: Display::Grid, |
| 28 |
|
| 29 |
width: percent(100), |
| 30 |
height: percent(100), |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
grid_template_columns: vec![GridTrack::min_content(), GridTrack::flex(1.0)], |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
grid_template_rows: vec![ |
| 40 |
GridTrack::auto(), |
| 41 |
GridTrack::flex(1.0), |
| 42 |
GridTrack::px(20.), |
| 43 |
], |
| 44 |
..default() |
| 45 |
}, |
| 46 |
BackgroundColor(Color::WHITE), |
| 47 |
)) |
| 48 |
.with_children(|builder| { |
| 49 |
|
| 50 |
builder |
| 51 |
.spawn( |
| 52 |
Node { |
| 53 |
display: Display::Grid, |
| 54 |
|
| 55 |
grid_column: GridPlacement::span(2), |
| 56 |
padding: UiRect::all(px(6)), |
| 57 |
..default() |
| 58 |
}, |
| 59 |
) |
| 60 |
.with_children(|builder| { |
| 61 |
spawn_nested_text_bundle(builder, font.clone(), "Bevy CSS Grid Layout Example"); |
| 62 |
}); |
| 63 |
|
| 64 |
|
| 65 |
builder |
| 66 |
.spawn(( |
| 67 |
Node { |
| 68 |
|
| 69 |
height: percent(100), |
| 70 |
|
| 71 |
|
| 72 |
aspect_ratio: Some(1.0), |
| 73 |
|
| 74 |
display: Display::Grid, |
| 75 |
|
| 76 |
padding: UiRect::all(px(24)), |
| 77 |
|
| 78 |
|
| 79 |
grid_template_columns: RepeatedGridTrack::flex(4, 1.0), |
| 80 |
|
| 81 |
|
| 82 |
grid_template_rows: RepeatedGridTrack::flex(4, 1.0), |
| 83 |
|
| 84 |
row_gap: px(12), |
| 85 |
column_gap: px(12), |
| 86 |
..default() |
| 87 |
}, |
| 88 |
BackgroundColor(Color::srgb(0.25, 0.25, 0.25)), |
| 89 |
)) |
| 90 |
.with_children(|builder| { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
item_rect(builder, ORANGE); |
| 97 |
item_rect(builder, BISQUE); |
| 98 |
item_rect(builder, BLUE); |
| 99 |
item_rect(builder, CRIMSON); |
| 100 |
item_rect(builder, AQUA); |
| 101 |
item_rect(builder, ORANGE_RED); |
| 102 |
item_rect(builder, DARK_GREEN); |
| 103 |
item_rect(builder, FUCHSIA); |
| 104 |
item_rect(builder, TEAL); |
| 105 |
item_rect(builder, ALICE_BLUE); |
| 106 |
item_rect(builder, CRIMSON); |
| 107 |
item_rect(builder, ANTIQUE_WHITE); |
| 108 |
item_rect(builder, YELLOW); |
| 109 |
item_rect(builder, DEEP_PINK); |
| 110 |
item_rect(builder, YELLOW_GREEN); |
| 111 |
item_rect(builder, SALMON); |
| 112 |
}); |
| 113 |
|
| 114 |
|
| 115 |
builder |
| 116 |
.spawn(( |
| 117 |
Node { |
| 118 |
display: Display::Grid, |
| 119 |
|
| 120 |
align_items: AlignItems::Start, |
| 121 |
|
| 122 |
justify_items: JustifyItems::Center, |
| 123 |
|
| 124 |
padding: UiRect::all(px(10)), |
| 125 |
|
| 126 |
|
| 127 |
grid_template_rows: vec![GridTrack::auto(), GridTrack::auto(), GridTrack::fr(1.0)], |
| 128 |
|
| 129 |
row_gap: px(10), |
| 130 |
..default() |
| 131 |
}, |
| 132 |
BackgroundColor(BLACK.into()), |
| 133 |
)) |
| 134 |
.with_children(|builder| { |
| 135 |
builder.spawn((Text::new("Sidebar"), |
| 136 |
TextFont::from(font.clone()), |
| 137 |
)); |
| 138 |
builder.spawn((Text::new("A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely. A paragraph of text which ought to wrap nicely."), |
| 139 |
TextFont { |
| 140 |
font: font.clone().into(), |
| 141 |
font_size: FontSize::Px(13.0), |
| 142 |
..default() |
| 143 |
}, |
| 144 |
)); |
| 145 |
builder.spawn(Node::default()); |
| 146 |
}); |
| 147 |
|
| 148 |
|
| 149 |
builder.spawn(( |
| 150 |
Node { |
| 151 |
|
| 152 |
grid_column: GridPlacement::span(2), |
| 153 |
..default() |
| 154 |
}, |
| 155 |
BackgroundColor(WHITE.into()), |
| 156 |
)); |
| 157 |
|
| 158 |
|
| 159 |
builder.spawn(( |
| 160 |
Node { |
| 161 |
position_type: PositionType::Absolute, |
| 162 |
margin: UiRect { |
| 163 |
top: px(100), |
| 164 |
bottom: auto(), |
| 165 |
left: auto(), |
| 166 |
right: auto(), |
| 167 |
}, |
| 168 |
width: percent(60), |
| 169 |
height: px(300), |
| 170 |
max_width: px(600), |
| 171 |
..default() |
| 172 |
}, |
| 173 |
Visibility::Hidden, |
| 174 |
BackgroundColor(Color::WHITE.with_alpha(0.8)), |
| 175 |
)); |
| 176 |
}); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
fn item_rect(builder: &mut ChildSpawnerCommands, color: Srgba) { |
| 183 |
builder |
| 184 |
.spawn(( |
| 185 |
Node { |
| 186 |
display: Display::Grid, |
| 187 |
padding: UiRect::all(px(3)), |
| 188 |
..default() |
| 189 |
}, |
| 190 |
BackgroundColor(BLACK.into()), |
| 191 |
)) |
| 192 |
.with_children(|builder| { |
| 193 |
builder.spawn((Node::default(), BackgroundColor(color.into()))); |
| 194 |
}); |
| 195 |
} |
| 196 |
|
| 197 |
fn spawn_nested_text_bundle(builder: &mut ChildSpawnerCommands, font: Handle<Font>, text: &str) { |
| 198 |
builder.spawn(( |
| 199 |
Text::new(text), |
| 200 |
TextFont::from(font.clone()), |
| 201 |
TextColor::BLACK, |
| 202 |
)); |
| 203 |
} |