Skip to content

Commit

Permalink
Update to Bevy 0.9 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile authored Nov 13, 2022
1 parent 764cc37 commit ff29b3f
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ members = ["./", "tools/ci"]
default = []

[dependencies]
bevy = {git = "https://github.com/alice-i-cecile/bevy", branch="0.8.1withInputSerde", default_features = false, features = ["serialize"]}
bevy = {version ="0.9", default_features = false, features = ["serialize"]}
serde = {version = "1.0", features = ["derive"]}
ron = "0.8"

[dev-dependencies]
bevy = {git = "https://github.com/alice-i-cecile/bevy", branch="0.8.1withInputSerde", default-features = true, features = ["serialize"]}
bevy = {version ="0.9", default_features = true, features = ["serialize"]}

[lib]
name = "leafwing_input_playback"
Expand Down
90 changes: 40 additions & 50 deletions examples/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
.run();
}

#[derive(PartialEq)]
#[derive(Resource, PartialEq)]
enum InputStrategy {
Capture,
Playback,
Expand Down Expand Up @@ -144,10 +144,12 @@ mod gamepad_viewer_example {
#[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 {
let mut materials = world.resource_mut::<Assets<ColorMaterial>>();
Expand All @@ -157,12 +159,14 @@ mod gamepad_viewer_example {
}
}
}
#[derive(Resource)]
struct ButtonMeshes {
circle: Mesh2dHandle,
triangle: Mesh2dHandle,
start_pause: Mesh2dHandle,
trigger: Mesh2dHandle,
}

impl FromWorld for ButtonMeshes {
fn from_world(world: &mut World) -> Self {
let mut meshes = world.resource_mut::<Assets<Mesh>>();
Expand All @@ -176,7 +180,7 @@ mod gamepad_viewer_example {
}
}
}
#[derive(Deref)]
#[derive(Resource, Deref)]
struct FontHandle(Handle<Font>);
impl FromWorld for FontHandle {
fn from_world(world: &mut World) -> Self {
Expand All @@ -186,46 +190,42 @@ mod gamepad_viewer_example {
}

fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<ButtonMaterials>) {
commands.spawn_bundle(Camera2dBundle::default());
commands.spawn(Camera2dBundle::default());

// Buttons

commands
.spawn_bundle(SpatialBundle {
.spawn(SpatialBundle {
transform: Transform::from_xyz(BUTTONS_X, BUTTONS_Y, 0.),
..default()
})
.with_children(|parent| {
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.circle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(0., BUTTON_CLUSTER_RADIUS, 0.),
..default()
})
.insert(ReactTo(GamepadButtonType::North));
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.circle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(0., -BUTTON_CLUSTER_RADIUS, 0.),
..default()
})
.insert(ReactTo(GamepadButtonType::South));
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.circle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(-BUTTON_CLUSTER_RADIUS, 0., 0.),
..default()
})
.insert(ReactTo(GamepadButtonType::West));
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.circle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(BUTTON_CLUSTER_RADIUS, 0., 0.),
Expand All @@ -238,8 +238,7 @@ mod gamepad_viewer_example {
// Start and Pause

commands
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.start_pause.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(-30., BUTTONS_Y, 0.),
Expand All @@ -248,8 +247,7 @@ mod gamepad_viewer_example {
.insert(ReactTo(GamepadButtonType::Select));

commands
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.start_pause.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(30., BUTTONS_Y, 0.),
Expand All @@ -260,24 +258,21 @@ mod gamepad_viewer_example {
// D-Pad

commands
.spawn()
.insert_bundle(SpatialBundle {
.spawn(SpatialBundle {
transform: Transform::from_xyz(-BUTTONS_X, BUTTONS_Y, 0.),
..default()
})
.with_children(|parent| {
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.triangle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(0., BUTTON_CLUSTER_RADIUS, 0.),
..default()
})
.insert(ReactTo(GamepadButtonType::DPadUp));
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.triangle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(0., -BUTTON_CLUSTER_RADIUS, 0.)
Expand All @@ -286,8 +281,7 @@ mod gamepad_viewer_example {
})
.insert(ReactTo(GamepadButtonType::DPadDown));
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.triangle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(-BUTTON_CLUSTER_RADIUS, 0., 0.)
Expand All @@ -296,8 +290,7 @@ mod gamepad_viewer_example {
})
.insert(ReactTo(GamepadButtonType::DPadLeft));
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.triangle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(BUTTON_CLUSTER_RADIUS, 0., 0.)
Expand All @@ -310,8 +303,7 @@ mod gamepad_viewer_example {
// Triggers

commands
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.trigger.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(-BUTTONS_X, BUTTONS_Y + 115., 0.),
Expand All @@ -320,8 +312,7 @@ mod gamepad_viewer_example {
.insert(ReactTo(GamepadButtonType::LeftTrigger));

commands
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.trigger.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(BUTTONS_X, BUTTONS_Y + 115., 0.),
Expand All @@ -337,25 +328,29 @@ mod gamepad_viewer_example {
gamepad_settings: Res<GamepadSettings>,
font: Res<FontHandle>,
) {
let dead_upper = STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.positive_low;
let dead_lower = STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.negative_low;
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.positive_high;
let live_lower = STICK_BOUNDS_SIZE * gamepad_settings.default_axis_settings.negative_high;
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_bundle(SpatialBundle {
.spawn(SpatialBundle {
transform: Transform::from_xyz(x_pos, y_pos, 0.),
..default()
})
.with_children(|parent| {
// full extent
parent.spawn_bundle(SpriteBundle {
parent.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::splat(STICK_BOUNDS_SIZE * 2.)),
color: EXTENT_COLOR,
Expand All @@ -364,7 +359,7 @@ mod gamepad_viewer_example {
..default()
});
// live zone
parent.spawn_bundle(SpriteBundle {
parent.spawn(SpriteBundle {
transform: Transform::from_xyz(live_mid, live_mid, 2.),
sprite: Sprite {
custom_size: Some(Vec2::new(live_size, live_size)),
Expand All @@ -374,7 +369,7 @@ mod gamepad_viewer_example {
..default()
});
// dead zone
parent.spawn_bundle(SpriteBundle {
parent.spawn(SpriteBundle {
transform: Transform::from_xyz(dead_mid, dead_mid, 3.),
sprite: Sprite {
custom_size: Some(Vec2::new(dead_size, dead_size)),
Expand All @@ -390,8 +385,7 @@ mod gamepad_viewer_example {
font: font.clone(),
};
parent
.spawn()
.insert_bundle(Text2dBundle {
.spawn(Text2dBundle {
transform: Transform::from_xyz(0., STICK_BOUNDS_SIZE + 2., 4.),
text: Text::from_sections([
TextSection {
Expand All @@ -413,8 +407,7 @@ mod gamepad_viewer_example {
.insert(TextWithAxes { x_axis, y_axis });
// cursor
parent
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.circle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(0., 0., 5.)
Expand Down Expand Up @@ -454,8 +447,7 @@ mod gamepad_viewer_example {
) {
let mut spawn_trigger = |x, y, button_type| {
commands
.spawn()
.insert_bundle(MaterialMesh2dBundle {
.spawn(MaterialMesh2dBundle {
mesh: meshes.trigger.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(x, y, 0.),
Expand All @@ -464,8 +456,7 @@ mod gamepad_viewer_example {
.insert(ReactTo(button_type))
.with_children(|parent| {
parent
.spawn()
.insert_bundle(Text2dBundle {
.spawn(Text2dBundle {
transform: Transform::from_xyz(0., 0., 1.),
text: Text::from_section(
format!("{:.3}", 0.),
Expand Down Expand Up @@ -501,8 +492,7 @@ mod gamepad_viewer_example {
font: font.clone(),
};
commands
.spawn()
.insert_bundle(TextBundle::from_sections([
.spawn(TextBundle::from_sections([
TextSection {
value: "Connected Gamepads\n".to_string(),
style: style.clone(),
Expand All @@ -523,10 +513,10 @@ mod gamepad_viewer_example {
) {
for gamepad in gamepads.iter() {
for (mut handle, react_to) in query.iter_mut() {
if button_inputs.just_pressed(GamepadButton::new(*gamepad, **react_to)) {
if button_inputs.just_pressed(GamepadButton::new(gamepad, **react_to)) {
*handle = materials.active.clone();
}
if button_inputs.just_released(GamepadButton::new(*gamepad, **react_to)) {
if button_inputs.just_released(GamepadButton::new(gamepad, **react_to)) {
*handle = materials.normal.clone();
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/input_playback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ fn main() {
.run()
}

#[derive(PartialEq)]
#[derive(Resource, PartialEq)]
enum InputStrategy {
Capture,
Playback,
}

fn setup(mut commands: Commands) {
commands.spawn_bundle(Camera2dBundle::default());
commands.spawn(Camera2dBundle::default());
}

pub fn cursor_pos_as_world_pos(
Expand Down Expand Up @@ -67,7 +67,7 @@ fn spawn_boxes(
// Don't break if we leave the window
if let Some(cursor_pos) = cursor_pos_as_world_pos(primary_window, &camera_query) {
commands
.spawn_bundle(SpriteBundle {
.spawn(SpriteBundle {
sprite: Sprite {
color: Color::DARK_GREEN,
..default()
Expand Down
13 changes: 12 additions & 1 deletion src/frame_counting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ use std::ops::{Add, Sub};
///
/// Updated in [`time_tracker`] during [`CoreStage::First`].
#[derive(
Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
Resource,
Clone,
Copy,
Debug,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
)]
pub struct FrameCount(pub u64);

Expand Down
4 changes: 2 additions & 2 deletions src/input_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Plugin for InputCapturePlugin {
/// The input mechanisms captured via the [`InputCapturePlugin`], configured as a resource.
///
/// By default, all supported input modes will be captured.
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Resource, Debug, PartialEq, Eq, Clone)]
pub struct InputModesCaptured {
/// Mouse buttons and mouse wheel inputs
pub mouse_buttons: bool,
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn capture_input(
frame_count: Res<FrameCount>,
time: Res<Time>,
) {
let time_since_startup = time.time_since_startup();
let time_since_startup = time.elapsed();
let frame = *frame_count;

// BLOCKED: these events are arbitrarily ordered within a frame,
Expand Down
Loading

0 comments on commit ff29b3f

Please sign in to comment.