|
| 1 | +use bevy::{ |
| 2 | + prelude::{App, AssetServer, BuildChildren, Commands, Rect, Res, Size}, |
| 3 | + render2::{camera::OrthographicCameraBundle, color::Color}, |
| 4 | + ui2::{ |
| 5 | + entity::ImageBundle, entity::NodeBundle, entity::UiCameraBundle, AlignItems, |
| 6 | + JustifyContent, PositionType, Style, Val, |
| 7 | + }, |
| 8 | + PipelinedDefaultPlugins, |
| 9 | +}; |
| 10 | + |
| 11 | +/// This example illustrates the various features of Bevy UI. |
| 12 | +fn main() { |
| 13 | + App::new() |
| 14 | + .add_plugins(PipelinedDefaultPlugins) |
| 15 | + .add_startup_system(setup) |
| 16 | + .run(); |
| 17 | +} |
| 18 | + |
| 19 | +fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 20 | + // ui camera |
| 21 | + commands.spawn_bundle(UiCameraBundle::default()); |
| 22 | + // FIXME: this is needed to clear the background root node |
| 23 | + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); |
| 24 | + commands |
| 25 | + .spawn_bundle(NodeBundle { |
| 26 | + style: Style { |
| 27 | + size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), |
| 28 | + justify_content: JustifyContent::SpaceBetween, |
| 29 | + ..Default::default() |
| 30 | + }, |
| 31 | + color: Color::NONE, |
| 32 | + ..Default::default() |
| 33 | + }) |
| 34 | + .with_children(|parent| { |
| 35 | + // left vertical fill (border) |
| 36 | + parent |
| 37 | + .spawn_bundle(NodeBundle { |
| 38 | + style: Style { |
| 39 | + size: Size::new(Val::Px(200.0), Val::Percent(100.0)), |
| 40 | + border: Rect::all(Val::Px(2.0)), |
| 41 | + ..Default::default() |
| 42 | + }, |
| 43 | + color: Color::rgb(0.65, 0.65, 0.65), |
| 44 | + ..Default::default() |
| 45 | + }) |
| 46 | + .with_children(|parent| { |
| 47 | + // left vertical fill (content) |
| 48 | + parent |
| 49 | + .spawn_bundle(NodeBundle { |
| 50 | + style: Style { |
| 51 | + size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), |
| 52 | + align_items: AlignItems::FlexEnd, |
| 53 | + ..Default::default() |
| 54 | + }, |
| 55 | + color: Color::rgb(0.15, 0.15, 0.15), |
| 56 | + ..Default::default() |
| 57 | + }) |
| 58 | + .with_children(|_parent| { |
| 59 | + // text |
| 60 | + // parent.spawn_bundle(TextBundle { |
| 61 | + // style: Style { |
| 62 | + // margin: Rect::all(Val::Px(5.0)), |
| 63 | + // ..Default::default() |
| 64 | + // }, |
| 65 | + // text: Text::with_section( |
| 66 | + // "Text Example", |
| 67 | + // TextStyle { |
| 68 | + // font: asset_server.load("fonts/FiraSans-Bold.ttf"), |
| 69 | + // font_size: 30.0, |
| 70 | + // color: Color::WHITE, |
| 71 | + // }, |
| 72 | + // Default::default(), |
| 73 | + // ), |
| 74 | + // ..Default::default() |
| 75 | + // }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + // right vertical fill |
| 79 | + parent.spawn_bundle(NodeBundle { |
| 80 | + style: Style { |
| 81 | + size: Size::new(Val::Px(200.0), Val::Percent(100.0)), |
| 82 | + ..Default::default() |
| 83 | + }, |
| 84 | + color: Color::rgb(0.15, 0.15, 0.15), |
| 85 | + ..Default::default() |
| 86 | + }); |
| 87 | + // absolute positioning |
| 88 | + parent |
| 89 | + .spawn_bundle(NodeBundle { |
| 90 | + style: Style { |
| 91 | + size: Size::new(Val::Px(200.0), Val::Px(200.0)), |
| 92 | + position_type: PositionType::Absolute, |
| 93 | + position: Rect { |
| 94 | + left: Val::Px(210.0), |
| 95 | + bottom: Val::Px(10.0), |
| 96 | + ..Default::default() |
| 97 | + }, |
| 98 | + border: Rect::all(Val::Px(20.0)), |
| 99 | + ..Default::default() |
| 100 | + }, |
| 101 | + color: Color::rgb(0.4, 0.4, 1.0), |
| 102 | + ..Default::default() |
| 103 | + }) |
| 104 | + .with_children(|parent| { |
| 105 | + parent.spawn_bundle(NodeBundle { |
| 106 | + style: Style { |
| 107 | + size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), |
| 108 | + ..Default::default() |
| 109 | + }, |
| 110 | + color: Color::rgb(0.8, 0.8, 1.0), |
| 111 | + ..Default::default() |
| 112 | + }); |
| 113 | + }); |
| 114 | + // render order test: reddest in the back, whitest in the front (flex center) |
| 115 | + parent |
| 116 | + .spawn_bundle(NodeBundle { |
| 117 | + style: Style { |
| 118 | + size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), |
| 119 | + position_type: PositionType::Absolute, |
| 120 | + align_items: AlignItems::Center, |
| 121 | + justify_content: JustifyContent::Center, |
| 122 | + ..Default::default() |
| 123 | + }, |
| 124 | + color: Color::NONE, |
| 125 | + ..Default::default() |
| 126 | + }) |
| 127 | + .with_children(|parent| { |
| 128 | + parent |
| 129 | + .spawn_bundle(NodeBundle { |
| 130 | + style: Style { |
| 131 | + size: Size::new(Val::Px(100.0), Val::Px(100.0)), |
| 132 | + ..Default::default() |
| 133 | + }, |
| 134 | + color: Color::rgb(1.0, 0.0, 0.0), |
| 135 | + ..Default::default() |
| 136 | + }) |
| 137 | + .with_children(|parent| { |
| 138 | + parent.spawn_bundle(NodeBundle { |
| 139 | + style: Style { |
| 140 | + size: Size::new(Val::Px(100.0), Val::Px(100.0)), |
| 141 | + position_type: PositionType::Absolute, |
| 142 | + position: Rect { |
| 143 | + left: Val::Px(20.0), |
| 144 | + bottom: Val::Px(20.0), |
| 145 | + ..Default::default() |
| 146 | + }, |
| 147 | + ..Default::default() |
| 148 | + }, |
| 149 | + color: Color::rgb(1.0, 0.3, 0.3), |
| 150 | + ..Default::default() |
| 151 | + }); |
| 152 | + parent.spawn_bundle(NodeBundle { |
| 153 | + style: Style { |
| 154 | + size: Size::new(Val::Px(100.0), Val::Px(100.0)), |
| 155 | + position_type: PositionType::Absolute, |
| 156 | + position: Rect { |
| 157 | + left: Val::Px(40.0), |
| 158 | + bottom: Val::Px(40.0), |
| 159 | + ..Default::default() |
| 160 | + }, |
| 161 | + ..Default::default() |
| 162 | + }, |
| 163 | + color: Color::rgb(1.0, 0.5, 0.5), |
| 164 | + ..Default::default() |
| 165 | + }); |
| 166 | + parent.spawn_bundle(NodeBundle { |
| 167 | + style: Style { |
| 168 | + size: Size::new(Val::Px(100.0), Val::Px(100.0)), |
| 169 | + position_type: PositionType::Absolute, |
| 170 | + position: Rect { |
| 171 | + left: Val::Px(60.0), |
| 172 | + bottom: Val::Px(60.0), |
| 173 | + ..Default::default() |
| 174 | + }, |
| 175 | + ..Default::default() |
| 176 | + }, |
| 177 | + color: Color::rgb(1.0, 0.7, 0.7), |
| 178 | + ..Default::default() |
| 179 | + }); |
| 180 | + // alpha test |
| 181 | + parent.spawn_bundle(NodeBundle { |
| 182 | + style: Style { |
| 183 | + size: Size::new(Val::Px(100.0), Val::Px(100.0)), |
| 184 | + position_type: PositionType::Absolute, |
| 185 | + position: Rect { |
| 186 | + left: Val::Px(80.0), |
| 187 | + bottom: Val::Px(80.0), |
| 188 | + ..Default::default() |
| 189 | + }, |
| 190 | + ..Default::default() |
| 191 | + }, |
| 192 | + color: Color::rgba(1.0, 0.9, 0.9, 0.4), |
| 193 | + ..Default::default() |
| 194 | + }); |
| 195 | + }); |
| 196 | + }); |
| 197 | + // bevy logo (flex center) |
| 198 | + parent |
| 199 | + .spawn_bundle(NodeBundle { |
| 200 | + style: Style { |
| 201 | + size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), |
| 202 | + position_type: PositionType::Absolute, |
| 203 | + justify_content: JustifyContent::Center, |
| 204 | + align_items: AlignItems::FlexEnd, |
| 205 | + ..Default::default() |
| 206 | + }, |
| 207 | + color: Color::NONE, |
| 208 | + ..Default::default() |
| 209 | + }) |
| 210 | + .with_children(|parent| { |
| 211 | + // bevy logo (image) |
| 212 | + parent.spawn_bundle(ImageBundle { |
| 213 | + style: Style { |
| 214 | + size: Size::new(Val::Px(500.0), Val::Auto), |
| 215 | + ..Default::default() |
| 216 | + }, |
| 217 | + texture: Some(asset_server.load("branding/bevy_logo_dark_big.png")), |
| 218 | + ..Default::default() |
| 219 | + }); |
| 220 | + }); |
| 221 | + }); |
| 222 | +} |
0 commit comments