Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,682 changes: 1,959 additions & 723 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_quickmenu"
version = "0.5.0"
version = "0.6.0"
edition = "2021"
authors = ["Benedikt Terhechte"]
description = "A simple way of quickly creating nested menus in bevy that can be navigated with keys, gamepads and pointers"
Expand All @@ -14,7 +14,7 @@ exclude = ["data", "assets", ".vscode", "icons.sketch", ".DS_Store"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "0.14.0", default-features = false, features = [
bevy = { version = "0.17.2", default-features = false, features = [
"bevy_ui",
"bevy_render",
"bevy_asset",
Expand All @@ -23,4 +23,4 @@ bevy = { version = "0.14.0", default-features = false, features = [
] }

[dev-dependencies]
bevy = "0.14"
bevy = "0.17.2"
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bevy_quickmenu = "0.1.5"

| Bevy Version | Crates Version |
| ------------ | -------------- |
| 0.17.2 | 0.6.0 |
| 0.14.0 | 0.5.0 |
| 0.13.0 | 0.4.0 |
| 0.12.0 | 0.3.0 |
Expand Down
12 changes: 6 additions & 6 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {

/// This custom event can be emitted by the action handler (below) in order to
/// process actions with access to the bevy ECS
#[derive(Debug, Event)]
#[derive(Debug, Message)]
enum BasicEvent {
Close,
}
Expand All @@ -36,7 +36,7 @@ impl Plugin for BasicPlugin {
fn build(&self, app: &mut App) {
app
// Register a event that can be called from your action handler
.add_event::<BasicEvent>()
.add_message::<BasicEvent>()
// The plugin
.add_plugins(QuickMenuPlugin::<Screens>::new())
// Some systems
Expand All @@ -46,7 +46,7 @@ impl Plugin for BasicPlugin {
}

fn setup(mut commands: Commands) {
commands.spawn(Camera3dBundle::default());
commands.spawn(Camera3d::default());
// Create a default stylesheet. You can customize these as you wish
let sheet = Stylesheet::default();

Expand All @@ -69,10 +69,10 @@ enum Actions {
impl ActionTrait for Actions {
type State = BasicState;
type Event = BasicEvent;
fn handle(&self, state: &mut BasicState, event_writer: &mut EventWriter<BasicEvent>) {
fn handle(&self, state: &mut BasicState, event_writer: &mut MessageWriter<BasicEvent>) {
match self {
Actions::Close => {
event_writer.send(BasicEvent::Close);
event_writer.write(BasicEvent::Close);
}
Actions::Toggle1 => {
state.boolean1 = !state.boolean1;
Expand Down Expand Up @@ -130,7 +130,7 @@ fn boolean_menu(state: &BasicState) -> Menu<Screens> {

/// This allows to react to actions with custom bevy resources or eventwriters or queries.
/// In this example we use it to close the menu
fn event_reader(mut commands: Commands, mut event_reader: EventReader<BasicEvent>) {
fn event_reader(mut commands: Commands, mut event_reader: MessageReader<BasicEvent>) {
for event in event_reader.read() {
match event {
BasicEvent::Close => bevy_quickmenu::cleanup(&mut commands),
Expand Down
18 changes: 9 additions & 9 deletions examples/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {

/// This custom event can be emitted by the action handler (below) in order to
/// process actions with access to the bevy ECS
#[derive(Debug, Event)]
#[derive(Debug, Message)]
enum BasicEvent {
Close,
}
Expand All @@ -51,7 +51,7 @@ impl Plugin for BasicPlugin {

app
// Register a event that can be called from your action handler
.add_event::<BasicEvent>()
.add_message::<BasicEvent>()
// The plugin
.add_plugins(QuickMenuPlugin::<Screens>::with_options(options))
// Some systems
Expand All @@ -61,7 +61,7 @@ impl Plugin for BasicPlugin {
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera3dBundle::default());
commands.spawn(Camera3d::default());
// Create a customized stylesheet
let mut button_style = StyleEntry::button();
button_style.size = 25.0;
Expand Down Expand Up @@ -97,10 +97,10 @@ enum Actions {
impl ActionTrait for Actions {
type State = BasicState;
type Event = BasicEvent;
fn handle(&self, state: &mut BasicState, event_writer: &mut EventWriter<BasicEvent>) {
fn handle(&self, state: &mut BasicState, event_writer: &mut MessageWriter<BasicEvent>) {
match self {
Actions::Close => {
event_writer.send(BasicEvent::Close);
event_writer.write(BasicEvent::Close);
}
Actions::Toggle1 => {
state.boolean1 = !state.boolean1;
Expand Down Expand Up @@ -138,7 +138,7 @@ fn root_menu(state: &BasicState) -> Menu<Screens> {
vec![
MenuItem::headline([
RichTextEntry::new("Rich "),
RichTextEntry::new_color("Text ", Color::srgb(1.0,0.0,0.0)),
RichTextEntry::new_color("Text ", Color::srgb(1.0, 0.0, 0.0)),
RichTextEntry::new_color("!", Color::srgb(1.0, 1.0, 0.0)),
]),
MenuItem::action("Close", Actions::Close).with_icon(MenuIcon::Back),
Expand All @@ -160,8 +160,8 @@ fn boolean_menu(state: &BasicState) -> Menu<Screens> {
MenuItem::action("Toggle Boolean 2", Actions::Toggle2).checked(state.boolean2),
],
)
.with_background(BackgroundColor(Color::srgb(0.0,0.0,0.5)))
.with_style(Style {
.with_background(BackgroundColor(Color::srgb(0.0, 0.0, 0.5)))
.with_style(Node {
align_items: AlignItems::FlexEnd,
flex_direction: FlexDirection::Column,
..Default::default()
Expand All @@ -170,7 +170,7 @@ fn boolean_menu(state: &BasicState) -> Menu<Screens> {

/// This allows to react to actions with custom bevy resources or eventwriters or queries.
/// In this example we use it to close the menu
fn event_reader(mut commands: Commands, mut event_reader: EventReader<BasicEvent>) {
fn event_reader(mut commands: Commands, mut event_reader: MessageReader<BasicEvent>) {
for event in event_reader.read() {
match event {
BasicEvent::Close => bevy_quickmenu::cleanup(&mut commands),
Expand Down
67 changes: 36 additions & 31 deletions examples/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//! It also shows how to get from the Settings to the game and back.
//! Due to the way Bevy handles GameStates (which will soon be rewritten),
//! composing menus and games looks a bit convoluted.
use bevy::{prelude::*, utils::HashMap};
use bevy::prelude::*;

use std::collections::HashMap;

use bevy_quickmenu::{
style::Stylesheet, ActionTrait, Menu, MenuIcon, MenuItem, MenuState, QuickMenuPlugin,
Expand Down Expand Up @@ -39,15 +41,15 @@ fn main() {
}

fn setup(mut commands: Commands) {
commands.spawn(Camera3dBundle::default());
commands.spawn(Camera3d::default());
}

mod settings {
use super::*;

/// This custom event can be emitted by the action handler (below) in order to
/// process actions with access to the bevy ECS
#[derive(Debug, Event)]
#[derive(Debug, Message)]
enum MyEvent {
CloseSettings,
}
Expand All @@ -57,7 +59,7 @@ mod settings {
#[derive(Debug, Clone)]
struct CustomState {
sound_on: bool,
gamepads: Vec<(Gamepad, String)>,
gamepads: Vec<usize>,
controls: HashMap<usize, ControlDevice>,
logo: Handle<Image>,
}
Expand All @@ -68,7 +70,7 @@ mod settings {
fn build(&self, app: &mut App) {
app
// Register a event that can be called from your action handler
.add_event::<MyEvent>()
.add_message::<MyEvent>()
// The plugin
.add_plugins(QuickMenuPlugin::<Screens>::new())
// Some systems
Expand Down Expand Up @@ -105,19 +107,19 @@ mod settings {
/// Whenever a new gamepad connects, get the known gamepads and their names
/// into our state
fn update_gamepads_system(
gamepads: Res<Gamepads>,
gamepads: Query<&Gamepad>,
menu_state: Option<ResMut<MenuState<Screens>>>,
) {
let Some(mut menu_state) = menu_state else {
return;
};
let gamepads = gamepads
.iter()
.map(|p| {
(
p,
gamepads.name(p).map(|s| s.to_owned()).unwrap_or_default(),
)
.map(|gamepad| {
gamepad
.product_id()
.map(|v| v as usize)
.unwrap_or(usize::MAX)
})
.collect();
if menu_state.state().gamepads != gamepads {
Expand All @@ -138,10 +140,10 @@ mod settings {
impl ActionTrait for Actions {
type State = CustomState;
type Event = MyEvent;
fn handle(&self, state: &mut CustomState, event_writer: &mut EventWriter<MyEvent>) {
fn handle(&self, state: &mut CustomState, event_writer: &mut MessageWriter<MyEvent>) {
match self {
Actions::Close => {
event_writer.send(MyEvent::CloseSettings);
event_writer.write(MyEvent::CloseSettings);
}
Actions::SoundOn => {
state.sound_on = true;
Expand Down Expand Up @@ -235,11 +237,14 @@ mod settings {
.collect();

// Get the GamePads
for (pad, title) in &state.gamepads {
let device = ControlDevice::Gamepad { gamepad_id: pad.id };
for title in &state.gamepads {
let device = ControlDevice::Gamepad { gamepad_id: *title };
entries.push(
MenuItem::action(title, Actions::Control(player, device))
.checked(device.id() == selected_control.id()),
MenuItem::action(
format!("Controller id: {:}", title),
Actions::Control(player, device),
)
.checked(device.id() == selected_control.id()),
)
}

Expand All @@ -250,7 +255,7 @@ mod settings {
/// In this example we use it to close the menu
fn event_reader(
mut commands: Commands,
mut event_reader: EventReader<MyEvent>,
mut event_reader: MessageReader<MyEvent>,
mut next_state: ResMut<NextState<GameState>>,
) {
for event in event_reader.read() {
Expand Down Expand Up @@ -362,22 +367,22 @@ mod game {
struct GameComponent;

fn setup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
commands
.spawn((TextBundle::from_section(
"Return Key to go back to menu",
TextStyle {
font: asset_server.load("font.otf"),
font_size: 30.0,
color: Color::WHITE,
},
)
.with_style(Style {
commands.spawn((
Text("Return Key to go back to menu".to_string()),
TextFont {
font: asset_server.load("font.otf"),
font_size: 30.0,
..default()
},
TextColor(Color::WHITE),
Node {
position_type: PositionType::Absolute,
top: Val::Px(60.0),
left: Val::Px(50.0),
..default()
}),))
.insert(GameComponent);
},
GameComponent,
));
}

fn detect_close_system(
Expand All @@ -388,7 +393,7 @@ mod game {
) {
if keyboard_input.just_pressed(KeyCode::Enter) {
for entity in game_items.iter() {
commands.entity(entity).despawn_recursive();
commands.entity(entity).despawn();
}
next_state.set(GameState::Settings);
}
Expand Down
23 changes: 14 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub use types::{
PrimaryMenu, RedrawEvent, RichTextEntry, Selections, VerticalMenuComponent,
};

use crate::types::GamepadActivation;

/// The quickmenu plugin.
/// It requires multiple generic parameters in order to setup. A minimal example.
/// For a full explanation refer to the examples or the README.
Expand Down Expand Up @@ -193,20 +195,23 @@ where
app.insert_resource(self.options.unwrap_or_default())
.init_resource::<MenuAssets>()
.insert_resource(Selections::default())
.add_event::<NavigationEvent>()
.add_event::<RedrawEvent>()
.add_message::<NavigationEvent>()
.add_message::<RedrawEvent>()
.add_systems(
Update,
systems::cleanup_system::<S>.run_if(resource_exists::<CleanUpUI>),
)
.add_systems(
Update,
(
systems::mouse_system::<S>.run_if(resource_exists::<MenuState<S>>),
systems::input_system::<S>.run_if(resource_exists::<MenuState<S>>),
systems::redraw_system::<S>.run_if(resource_exists::<MenuState<S>>),
systems::keyboard_input_system.run_if(resource_exists::<MenuState<S>>),
),
systems::mouse_system::<S>,
systems::input_system::<S>,
systems::redraw_system::<S>,
systems::keyboard_input_system,
systems::insert_gamepad_activation_system
.run_if(any_match_filter::<(With<Gamepad>, Without<GamepadActivation>)>),
)
.run_if(resource_exists::<MenuState<S>>),
);
}
}
Expand All @@ -220,8 +225,8 @@ pub fn cleanup(commands: &mut Commands) {
/// are generated as the user interacts with the menu
pub trait ActionTrait: Debug + PartialEq + Eq + Clone + Copy + Hash + Send + Sync {
type State;
type Event: Event + Send + Sync + 'static;
fn handle(&self, state: &mut Self::State, event_writer: &mut EventWriter<Self::Event>);
type Event: Message + Send + Sync + 'static;
fn handle(&self, state: &mut Self::State, event_writer: &mut MessageWriter<Self::Event>);
}

/// Each Menu / Screen uses this trait to define which menu items lead
Expand Down
Loading