bevy/examples/tools/gamepad_viewer.rs

525 lines
16 KiB
Rust
Raw Normal View History

//! Shows a visualization of gamepad buttons, sticks, and triggers
use std::f32::consts::PI;
use bevy::{
input::gamepad::{GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadSettings},
prelude::*,
Remove VerticalAlign from TextAlignment (#6807) # Objective Remove the `VerticalAlign` enum. Text's alignment field should only affect the text's internal text alignment, not its position. The only way to control a `TextBundle`'s position and bounds should be through the manipulation of the constraints in the `Style` components of the nodes in the Bevy UI's layout tree. `Text2dBundle` should have a separate `Anchor` component that sets its position relative to its transform. Related issues: #676, #1490, #5502, #5513, #5834, #6717, #6724, #6741, #6748 ## Changelog * Changed `TextAlignment` into an enum with `Left`, `Center`, and `Right` variants. * Removed the `HorizontalAlign` and `VerticalAlign` types. * Added an `Anchor` component to `Text2dBundle` * Added `Component` derive to `Anchor` * Use `f32::INFINITY` instead of `f32::MAX` to represent unbounded text in Text2dBounds ## Migration Guide The `alignment` field of `Text` now only affects the text's internal alignment. ### Change `TextAlignment` to TextAlignment` which is now an enum. Replace: * `TextAlignment::TOP_LEFT`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_LEFT` with `TextAlignment::Left` * `TextAlignment::TOP_CENTER`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_CENTER` with `TextAlignment::Center` * `TextAlignment::TOP_RIGHT`, `TextAlignment::CENTER_RIGHT`, `TextAlignment::BOTTOM_RIGHT` with `TextAlignment::Right` ### Changes for `Text2dBundle` `Text2dBundle` has a new field 'text_anchor' that takes an `Anchor` component that controls its position relative to its transform.
2023-01-18 02:19:17 +00:00
sprite::{Anchor, MaterialMesh2dBundle, Mesh2dHandle},
};
const BUTTON_RADIUS: f32 = 25.;
const BUTTON_CLUSTER_RADIUS: f32 = 50.;
const START_SIZE: Vec2 = Vec2::new(30., 15.);
const TRIGGER_SIZE: Vec2 = Vec2::new(70., 20.);
const STICK_BOUNDS_SIZE: f32 = 100.;
const BUTTONS_X: f32 = 150.;
const BUTTONS_Y: f32 = 80.;
const STICKS_X: f32 = 150.;
const STICKS_Y: f32 = -135.;
Migrate from `LegacyColor` to `bevy_color::Color` (#12163) # Objective - As part of the migration process we need to a) see the end effect of the migration on user ergonomics b) check for serious perf regressions c) actually migrate the code - To accomplish this, I'm going to attempt to migrate all of the remaining user-facing usages of `LegacyColor` in one PR, being careful to keep a clean commit history. - Fixes #12056. ## Solution I've chosen to use the polymorphic `Color` type as our standard user-facing API. - [x] Migrate `bevy_gizmos`. - [x] Take `impl Into<Color>` in all `bevy_gizmos` APIs - [x] Migrate sprites - [x] Migrate UI - [x] Migrate `ColorMaterial` - [x] Migrate `MaterialMesh2D` - [x] Migrate fog - [x] Migrate lights - [x] Migrate StandardMaterial - [x] Migrate wireframes - [x] Migrate clear color - [x] Migrate text - [x] Migrate gltf loader - [x] Register color types for reflection - [x] Remove `LegacyColor` - [x] Make sure CI passes Incidental improvements to ease migration: - added `Color::srgba_u8`, `Color::srgba_from_array` and friends - added `set_alpha`, `is_fully_transparent` and `is_fully_opaque` to the `Alpha` trait - add and immediately deprecate (lol) `Color::rgb` and friends in favor of more explicit and consistent `Color::srgb` - standardized on white and black for most example text colors - added vector field traits to `LinearRgba`: ~~`Add`, `Sub`, `AddAssign`, `SubAssign`,~~ `Mul<f32>` and `Div<f32>`. Multiplications and divisions do not scale alpha. `Add` and `Sub` have been cut from this PR. - added `LinearRgba` and `Srgba` `RED/GREEN/BLUE` - added `LinearRgba_to_f32_array` and `LinearRgba::to_u32` ## Migration Guide Bevy's color types have changed! Wherever you used a `bevy::render::Color`, a `bevy::color::Color` is used instead. These are quite similar! Both are enums storing a color in a specific color space (or to be more precise, using a specific color model). However, each of the different color models now has its own type. TODO... - `Color::rgba`, `Color::rgb`, `Color::rbga_u8`, `Color::rgb_u8`, `Color::rgb_from_array` are now `Color::srgba`, `Color::srgb`, `Color::srgba_u8`, `Color::srgb_u8` and `Color::srgb_from_array`. - `Color::set_a` and `Color::a` is now `Color::set_alpha` and `Color::alpha`. These are part of the `Alpha` trait in `bevy_color`. - `Color::is_fully_transparent` is now part of the `Alpha` trait in `bevy_color` - `Color::r`, `Color::set_r`, `Color::with_r` and the equivalents for `g`, `b` `h`, `s` and `l` have been removed due to causing silent relatively expensive conversions. Convert your `Color` into the desired color space, perform your operations there, and then convert it back into a polymorphic `Color` enum. - `Color::hex` is now `Srgba::hex`. Call `.into` or construct a `Color::Srgba` variant manually to convert it. - `WireframeMaterial`, `ExtractedUiNode`, `ExtractedDirectionalLight`, `ExtractedPointLight`, `ExtractedSpotLight` and `ExtractedSprite` now store a `LinearRgba`, rather than a polymorphic `Color` - `Color::rgb_linear` and `Color::rgba_linear` are now `Color::linear_rgb` and `Color::linear_rgba` - The various CSS color constants are no longer stored directly on `Color`. Instead, they're defined in the `Srgba` color space, and accessed via `bevy::color::palettes::css`. Call `.into()` on them to convert them into a `Color` for quick debugging use, and consider using the much prettier `tailwind` palette for prototyping. - The `LIME_GREEN` color has been renamed to `LIMEGREEN` to comply with the standard naming. - Vector field arithmetic operations on `Color` (add, subtract, multiply and divide by a f32) have been removed. Instead, convert your colors into `LinearRgba` space, and perform your operations explicitly there. This is particularly relevant when working with emissive or HDR colors, whose color channel values are routinely outside of the ordinary 0 to 1 range. - `Color::as_linear_rgba_f32` has been removed. Call `LinearRgba::to_f32_array` instead, converting if needed. - `Color::as_linear_rgba_u32` has been removed. Call `LinearRgba::to_u32` instead, converting if needed. - Several other color conversion methods to transform LCH or HSL colors into float arrays or `Vec` types have been removed. Please reimplement these externally or open a PR to re-add them if you found them particularly useful. - Various methods on `Color` such as `rgb` or `hsl` to convert the color into a specific color space have been removed. Convert into `LinearRgba`, then to the color space of your choice. - Various implicitly-converting color value methods on `Color` such as `r`, `g`, `b` or `h` have been removed. Please convert it into the color space of your choice, then check these properties. - `Color` no longer implements `AsBindGroup`. Store a `LinearRgba` internally instead to avoid conversion costs. --------- Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com> Co-authored-by: Afonso Lage <lage.afonso@gmail.com> Co-authored-by: Rob Parrett <robparrett@gmail.com> Co-authored-by: Zachary Harrold <zac@harrold.com.au>
2024-02-29 19:35:12 +00:00
const NORMAL_BUTTON_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
const ACTIVE_BUTTON_COLOR: Color = Color::srgb(0.5, 0., 0.5);
const LIVE_COLOR: Color = Color::srgb(0.4, 0.4, 0.4);
const DEAD_COLOR: Color = Color::srgb(0.13, 0.13, 0.13);
#[derive(Component, Deref)]
struct ReactTo(GamepadButtonType);
#[derive(Component)]
struct MoveWithAxes {
x_axis: GamepadAxisType,
y_axis: GamepadAxisType,
scale: f32,
}
#[derive(Component)]
struct TextWithAxes {
x_axis: GamepadAxisType,
y_axis: GamepadAxisType,
}
#[derive(Component, Deref)]
struct TextWithButtonValue(GamepadButtonType);
#[derive(Component)]
struct ConnectedGamepadsText;
#[derive(Resource)]
struct ButtonMaterials {
normal: Handle<ColorMaterial>,
active: Handle<ColorMaterial>,
}
impl FromWorld for ButtonMaterials {
fn from_world(world: &mut World) -> Self {
Self {
normal: world.add_asset(NORMAL_BUTTON_COLOR),
active: world.add_asset(ACTIVE_BUTTON_COLOR),
}
}
}
#[derive(Resource)]
struct ButtonMeshes {
circle: Mesh2dHandle,
triangle: Mesh2dHandle,
start_pause: Mesh2dHandle,
trigger: Mesh2dHandle,
}
impl FromWorld for ButtonMeshes {
fn from_world(world: &mut World) -> Self {
Self {
circle: world.add_asset(Circle::new(BUTTON_RADIUS)).into(),
triangle: world
.add_asset(RegularPolygon::new(BUTTON_RADIUS, 3))
.into(),
start_pause: world.add_asset(Rectangle::from_size(START_SIZE)).into(),
trigger: world.add_asset(Rectangle::from_size(TRIGGER_SIZE)).into(),
}
}
}
#[derive(Bundle)]
struct GamepadButtonBundle {
mesh_bundle: MaterialMesh2dBundle<ColorMaterial>,
react_to: ReactTo,
}
impl GamepadButtonBundle {
pub fn new(
button_type: GamepadButtonType,
mesh: Mesh2dHandle,
material: Handle<ColorMaterial>,
x: f32,
y: f32,
) -> Self {
Self {
mesh_bundle: MaterialMesh2dBundle {
mesh,
material,
transform: Transform::from_xyz(x, y, 0.),
..default()
},
react_to: ReactTo(button_type),
}
}
pub fn with_rotation(mut self, angle: f32) -> Self {
self.mesh_bundle.transform.rotation = Quat::from_rotation_z(angle);
self
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_resource::<ButtonMaterials>()
.init_resource::<ButtonMeshes>()
.add_systems(
Startup,
(setup, setup_sticks, setup_triggers, setup_connected),
)
.add_systems(
Update,
(
update_buttons,
update_button_values,
update_axes,
update_connected,
),
)
.run();
}
fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<ButtonMaterials>) {
commands.spawn(Camera2dBundle::default());
// Buttons
commands
.spawn(SpatialBundle {
transform: Transform::from_xyz(BUTTONS_X, BUTTONS_Y, 0.),
..default()
})
.with_children(|parent| {
parent.spawn(GamepadButtonBundle::new(
GamepadButtonType::North,
meshes.circle.clone(),
materials.normal.clone(),
0.,
BUTTON_CLUSTER_RADIUS,
));
parent.spawn(GamepadButtonBundle::new(
GamepadButtonType::South,
meshes.circle.clone(),
materials.normal.clone(),
0.,
-BUTTON_CLUSTER_RADIUS,
));
parent.spawn(GamepadButtonBundle::new(
GamepadButtonType::West,
meshes.circle.clone(),
materials.normal.clone(),
-BUTTON_CLUSTER_RADIUS,
0.,
));
parent.spawn(GamepadButtonBundle::new(
GamepadButtonType::East,
meshes.circle.clone(),
materials.normal.clone(),
BUTTON_CLUSTER_RADIUS,
0.,
));
});
// Start and Pause
commands.spawn(GamepadButtonBundle::new(
GamepadButtonType::Select,
meshes.start_pause.clone(),
materials.normal.clone(),
-30.,
BUTTONS_Y,
));
commands.spawn(GamepadButtonBundle::new(
GamepadButtonType::Start,
meshes.start_pause.clone(),
materials.normal.clone(),
30.,
BUTTONS_Y,
));
// D-Pad
commands
.spawn(SpatialBundle {
transform: Transform::from_xyz(-BUTTONS_X, BUTTONS_Y, 0.),
..default()
})
.with_children(|parent| {
parent.spawn(GamepadButtonBundle::new(
GamepadButtonType::DPadUp,
meshes.triangle.clone(),
materials.normal.clone(),
0.,
BUTTON_CLUSTER_RADIUS,
));
parent.spawn(
GamepadButtonBundle::new(
GamepadButtonType::DPadDown,
meshes.triangle.clone(),
materials.normal.clone(),
0.,
-BUTTON_CLUSTER_RADIUS,
)
.with_rotation(PI),
);
parent.spawn(
GamepadButtonBundle::new(
GamepadButtonType::DPadLeft,
meshes.triangle.clone(),
materials.normal.clone(),
-BUTTON_CLUSTER_RADIUS,
0.,
)
.with_rotation(PI / 2.),
);
parent.spawn(
GamepadButtonBundle::new(
GamepadButtonType::DPadRight,
meshes.triangle.clone(),
materials.normal.clone(),
BUTTON_CLUSTER_RADIUS,
0.,
)
.with_rotation(-PI / 2.),
);
});
// Triggers
commands.spawn(GamepadButtonBundle::new(
GamepadButtonType::LeftTrigger,
meshes.trigger.clone(),
materials.normal.clone(),
-BUTTONS_X,
BUTTONS_Y + 115.,
));
commands.spawn(GamepadButtonBundle::new(
GamepadButtonType::RightTrigger,
meshes.trigger.clone(),
materials.normal.clone(),
BUTTONS_X,
BUTTONS_Y + 115.,
));
}
fn setup_sticks(
mut commands: Commands,
meshes: Res<ButtonMeshes>,
materials: Res<ButtonMaterials>,
gamepad_settings: Res<GamepadSettings>,
) {
let dead_upper =
STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.deadzone_upperbound();
let dead_lower =
STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.deadzone_lowerbound();
let dead_size = dead_lower.abs() + dead_upper.abs();
let dead_mid = (dead_lower + dead_upper) / 2.0;
let live_upper =
STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.livezone_upperbound();
let live_lower =
STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.livezone_lowerbound();
let live_size = live_lower.abs() + live_upper.abs();
let live_mid = (live_lower + live_upper) / 2.0;
let mut spawn_stick = |x_pos, y_pos, x_axis, y_axis, button| {
commands
.spawn(SpatialBundle {
transform: Transform::from_xyz(x_pos, y_pos, 0.),
..default()
})
.with_children(|parent| {
// full extent
parent.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::splat(STICK_BOUNDS_SIZE * 2.)),
color: DEAD_COLOR,
..default()
},
..default()
});
// live zone
parent.spawn(SpriteBundle {
transform: Transform::from_xyz(live_mid, live_mid, 2.),
sprite: Sprite {
custom_size: Some(Vec2::new(live_size, live_size)),
color: LIVE_COLOR,
..default()
},
..default()
});
// dead zone
parent.spawn(SpriteBundle {
transform: Transform::from_xyz(dead_mid, dead_mid, 3.),
sprite: Sprite {
custom_size: Some(Vec2::new(dead_size, dead_size)),
color: DEAD_COLOR,
..default()
},
..default()
});
// text
let style = TextStyle {
font_size: 16.,
..default()
};
parent.spawn((
Text2dBundle {
transform: Transform::from_xyz(0., STICK_BOUNDS_SIZE + 2., 4.),
text: Text::from_sections([
TextSection {
value: format!("{:.3}", 0.),
style: style.clone(),
},
TextSection {
value: ", ".to_string(),
style: style.clone(),
},
TextSection {
value: format!("{:.3}", 0.),
style,
},
Remove VerticalAlign from TextAlignment (#6807) # Objective Remove the `VerticalAlign` enum. Text's alignment field should only affect the text's internal text alignment, not its position. The only way to control a `TextBundle`'s position and bounds should be through the manipulation of the constraints in the `Style` components of the nodes in the Bevy UI's layout tree. `Text2dBundle` should have a separate `Anchor` component that sets its position relative to its transform. Related issues: #676, #1490, #5502, #5513, #5834, #6717, #6724, #6741, #6748 ## Changelog * Changed `TextAlignment` into an enum with `Left`, `Center`, and `Right` variants. * Removed the `HorizontalAlign` and `VerticalAlign` types. * Added an `Anchor` component to `Text2dBundle` * Added `Component` derive to `Anchor` * Use `f32::INFINITY` instead of `f32::MAX` to represent unbounded text in Text2dBounds ## Migration Guide The `alignment` field of `Text` now only affects the text's internal alignment. ### Change `TextAlignment` to TextAlignment` which is now an enum. Replace: * `TextAlignment::TOP_LEFT`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_LEFT` with `TextAlignment::Left` * `TextAlignment::TOP_CENTER`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_CENTER` with `TextAlignment::Center` * `TextAlignment::TOP_RIGHT`, `TextAlignment::CENTER_RIGHT`, `TextAlignment::BOTTOM_RIGHT` with `TextAlignment::Right` ### Changes for `Text2dBundle` `Text2dBundle` has a new field 'text_anchor' that takes an `Anchor` component that controls its position relative to its transform.
2023-01-18 02:19:17 +00:00
]),
text_anchor: Anchor::BottomCenter,
..default()
},
TextWithAxes { x_axis, y_axis },
));
// cursor
parent.spawn((
MaterialMesh2dBundle {
mesh: meshes.circle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(0., 0., 5.)
.with_scale(Vec2::splat(0.15).extend(1.)),
..default()
},
MoveWithAxes {
x_axis,
y_axis,
scale: STICK_BOUNDS_SIZE,
},
ReactTo(button),
));
});
};
spawn_stick(
-STICKS_X,
STICKS_Y,
GamepadAxisType::LeftStickX,
GamepadAxisType::LeftStickY,
GamepadButtonType::LeftThumb,
);
spawn_stick(
STICKS_X,
STICKS_Y,
GamepadAxisType::RightStickX,
GamepadAxisType::RightStickY,
GamepadButtonType::RightThumb,
);
}
fn setup_triggers(
mut commands: Commands,
meshes: Res<ButtonMeshes>,
materials: Res<ButtonMaterials>,
) {
let mut spawn_trigger = |x, y, button_type| {
commands
.spawn(GamepadButtonBundle::new(
button_type,
meshes.trigger.clone(),
materials.normal.clone(),
x,
y,
))
.with_children(|parent| {
parent.spawn((
Text2dBundle {
transform: Transform::from_xyz(0., 0., 1.),
text: Text::from_section(
format!("{:.3}", 0.),
TextStyle {
font_size: 16.,
..default()
},
Remove VerticalAlign from TextAlignment (#6807) # Objective Remove the `VerticalAlign` enum. Text's alignment field should only affect the text's internal text alignment, not its position. The only way to control a `TextBundle`'s position and bounds should be through the manipulation of the constraints in the `Style` components of the nodes in the Bevy UI's layout tree. `Text2dBundle` should have a separate `Anchor` component that sets its position relative to its transform. Related issues: #676, #1490, #5502, #5513, #5834, #6717, #6724, #6741, #6748 ## Changelog * Changed `TextAlignment` into an enum with `Left`, `Center`, and `Right` variants. * Removed the `HorizontalAlign` and `VerticalAlign` types. * Added an `Anchor` component to `Text2dBundle` * Added `Component` derive to `Anchor` * Use `f32::INFINITY` instead of `f32::MAX` to represent unbounded text in Text2dBounds ## Migration Guide The `alignment` field of `Text` now only affects the text's internal alignment. ### Change `TextAlignment` to TextAlignment` which is now an enum. Replace: * `TextAlignment::TOP_LEFT`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_LEFT` with `TextAlignment::Left` * `TextAlignment::TOP_CENTER`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_CENTER` with `TextAlignment::Center` * `TextAlignment::TOP_RIGHT`, `TextAlignment::CENTER_RIGHT`, `TextAlignment::BOTTOM_RIGHT` with `TextAlignment::Right` ### Changes for `Text2dBundle` `Text2dBundle` has a new field 'text_anchor' that takes an `Anchor` component that controls its position relative to its transform.
2023-01-18 02:19:17 +00:00
),
..default()
},
TextWithButtonValue(button_type),
));
});
};
spawn_trigger(
-BUTTONS_X,
BUTTONS_Y + 145.,
GamepadButtonType::LeftTrigger2,
);
spawn_trigger(
BUTTONS_X,
BUTTONS_Y + 145.,
GamepadButtonType::RightTrigger2,
);
}
fn setup_connected(mut commands: Commands) {
let text_style = TextStyle::default();
commands.spawn((
TextBundle {
text: Text::from_sections([
TextSection {
value: "Connected Gamepads:\n".to_string(),
style: text_style.clone(),
},
TextSection {
value: "None".to_string(),
style: text_style,
},
]),
style: Style {
position_type: PositionType::Absolute,
top: Val::Px(12.),
left: Val::Px(12.),
..default()
},
..default()
},
ConnectedGamepadsText,
));
}
fn update_buttons(
gamepads: Res<Gamepads>,
button_inputs: Res<ButtonInput<GamepadButton>>,
materials: Res<ButtonMaterials>,
mut query: Query<(&mut Handle<ColorMaterial>, &ReactTo)>,
) {
for gamepad in gamepads.iter() {
for (mut handle, react_to) in query.iter_mut() {
if button_inputs.just_pressed(GamepadButton::new(gamepad, **react_to)) {
*handle = materials.active.clone();
}
if button_inputs.just_released(GamepadButton::new(gamepad, **react_to)) {
*handle = materials.normal.clone();
}
}
}
}
fn update_button_values(
mut events: EventReader<GamepadButtonChangedEvent>,
mut query: Query<(&mut Text, &TextWithButtonValue)>,
) {
for button_event in events.read() {
for (mut text, text_with_button_value) in query.iter_mut() {
if button_event.button_type == **text_with_button_value {
text.sections[0].value = format!("{:.3}", button_event.value);
}
}
}
}
fn update_axes(
mut axis_events: EventReader<GamepadAxisChangedEvent>,
mut query: Query<(&mut Transform, &MoveWithAxes)>,
mut text_query: Query<(&mut Text, &TextWithAxes)>,
) {
for axis_event in axis_events.read() {
let axis_type = axis_event.axis_type;
let value = axis_event.value;
for (mut transform, move_with) in query.iter_mut() {
if axis_type == move_with.x_axis {
transform.translation.x = value * move_with.scale;
}
if axis_type == move_with.y_axis {
transform.translation.y = value * move_with.scale;
}
}
for (mut text, text_with_axes) in text_query.iter_mut() {
if axis_type == text_with_axes.x_axis {
text.sections[0].value = format!("{value:.3}");
}
if axis_type == text_with_axes.y_axis {
text.sections[2].value = format!("{value:.3}");
}
}
}
}
fn update_connected(
gamepads: Res<Gamepads>,
mut query: Query<&mut Text, With<ConnectedGamepadsText>>,
) {
if !gamepads.is_changed() {
return;
}
let mut text = query.single_mut();
let formatted = gamepads
.iter()
.map(|g| format!("- {}", gamepads.name(g).unwrap()))
.collect::<Vec<_>>()
.join("\n");
text.sections[1].value = if !formatted.is_empty() {
formatted
} else {
"None".to_string()
}
}