Skip to content

Commit

Permalink
Update for Bevy 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
septum committed Mar 7, 2023
1 parent cd052c4 commit 1427b09
Show file tree
Hide file tree
Showing 8 changed files with 589 additions and 282 deletions.
815 changes: 560 additions & 255 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_ui_bits"
version = "0.1.0"
version = "0.2.0"
authors = ["Orlando Valverde <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -14,16 +14,16 @@ categories = ["game-development"]
exclude = ["assets/"]

[dependencies.bevy]
version = "0.9.1"
version = "0.10"
default-features = false
features = ["bevy_ui", "bevy_render", "bevy_asset", "bevy_text"]

[[example]]
name = "main_menu"
path = "examples/main_menu.rs"
required-features = ["bevy/bevy_core_pipeline", "bevy/bevy_sprite", "bevy/bevy_winit"]
required-features = ["bevy/default"]

[[example]]
name = "hud"
path = "examples/hud.rs"
required-features = ["bevy/bevy_core_pipeline", "bevy/bevy_sprite", "bevy/bevy_winit"]
required-features = ["bevy/default"]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn spawn_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
Try it out with:

```
cargo run --example main_menu --features="bevy/bevy_core_pipeline,bevy/bevy_sprite,bevy/bevy_winit"
cargo run --example main_menu --features="bevy/default"
```

### Simple HUD that features a dynamic text component
Expand All @@ -72,13 +72,14 @@ cargo run --example main_menu --features="bevy/bevy_core_pipeline,bevy/bevy_spri
Try it out with:

```
cargo run --example hud --features="bevy/bevy_core_pipeline,bevy/bevy_sprite,bevy/bevy_winit"
cargo run --example hud --features="bevy/default"
```

## Bevy Compatibility

| Bevy | bevy_ui_bits |
| ---- | ------------ |
| 0.10 | 0.2 |
| 0.9 | 0.1 |

## License
Expand Down
4 changes: 2 additions & 2 deletions examples/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const JUMPS_TEXT_ID: usize = 1;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: "HUD Example".to_string(),
..default()
},
}),
..default()
}))
.add_startup_system(spawn_camera)
Expand Down
33 changes: 17 additions & 16 deletions examples/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ const PLAY_BUTTON_ID: usize = 1;
const QUIT_BUTTON_ID: usize = 2;

#[derive(Resource)]
struct SelectedButton(pub usize);
struct SelectedButton {
pub id: usize,
}

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
primary_window: Some(Window {
title: "Main Menu Example".to_string(),
..default()
},
}),
..default()
}))
.insert_resource(SelectedButton(PLAY_BUTTON_ID))
.insert_resource(SelectedButton { id: PLAY_BUTTON_ID })
.add_startup_system(spawn_camera)
.add_startup_system(spawn_main_menu)
.add_system(close_on_esc)
.add_system(handle_keyboard_input)
.add_system(handle_mouse_input)
.add_system(update_buttons)
.add_systems((handle_keyboard_input, handle_mouse_input, update_buttons).chain())
.run();
}

Expand Down Expand Up @@ -56,7 +56,6 @@ fn spawn_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
bottom_wrapper.justify_between();

play.id(PLAY_BUTTON_ID).selected_color(Color::GOLD);

quit.id(QUIT_BUTTON_ID);

root.spawn(&mut commands, |parent| {
Expand All @@ -78,18 +77,20 @@ fn spawn_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
}

fn handle_keyboard_input(
keys: Res<Input<KeyCode>>,
mut keys: ResMut<Input<KeyCode>>,
mut selected_button: ResMut<SelectedButton>,
query: Query<&UiButtonData>,
) {
if keys.just_pressed(KeyCode::Up) || keys.just_pressed(KeyCode::Down) {
for button_data in &query {
if button_data.id == selected_button.0 {
if button_data.id == PLAY_BUTTON_ID {
selected_button.0 = QUIT_BUTTON_ID;
if button_data.id == selected_button.id {
selected_button.id = if button_data.id == PLAY_BUTTON_ID {
QUIT_BUTTON_ID
} else {
selected_button.0 = PLAY_BUTTON_ID;
}
PLAY_BUTTON_ID
};
keys.clear();
break;
}
}
}
Expand All @@ -101,7 +102,7 @@ fn handle_mouse_input(
) {
for (button_data, interaction) in &query {
match *interaction {
Interaction::Hovered => selected_button.0 = button_data.id,
Interaction::Hovered => selected_button.id = button_data.id,
_ => {}
}
}
Expand All @@ -112,7 +113,7 @@ fn update_buttons(
mut query: Query<(&UiButtonData, &mut BackgroundColor)>,
) {
for (button_data, mut background_color) in &mut query {
if button_data.id == selected_button.0 {
if button_data.id == selected_button.id {
*background_color = Color::GOLD.into();
} else {
*background_color = Color::NONE.into();
Expand Down
2 changes: 1 addition & 1 deletion src/components/text/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Default for DynamicText {
DynamicText {
data: DynamicTextData::default(),
bundle: TextBundle::from_sections(vec![section; 2])
.with_text_alignment(TextAlignment::CENTER),
.with_text_alignment(TextAlignment::Center),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/text/embossed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Default for EmbossedText {
};
EmbossedText {
bundle: TextBundle::from_section(String::new(), style)
.with_text_alignment(TextAlignment::CENTER),
.with_text_alignment(TextAlignment::Center),
background_color: Color::BLACK,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/text/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Default for SimpleText {
};
SimpleText {
bundle: TextBundle::from_section(String::new(), style)
.with_text_alignment(TextAlignment::CENTER),
.with_text_alignment(TextAlignment::Center),
}
}
}
Expand Down

0 comments on commit 1427b09

Please sign in to comment.