Skip to content

Commit

Permalink
Upgrade to bevy 0.14 (#37)
Browse files Browse the repository at this point in the history
* upgrade to bevy 0.14 -- some tests failing

* don't anticipate double-buffering in tests

* clippy

* Revert "don't anticipate double-buffering in tests"

This reverts commit 0252fc0.

* Configure playback systems to run after event updates
  • Loading branch information
snendev authored Aug 22, 2024
1 parent 40e27e1 commit e779860
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 77 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 = { version = "0.13", default_features = false, features = ["serialize"] }
bevy = { version = "0.14", default-features = false, features = ["serialize"] }
serde = { version = "1.0", features = ["derive"] }
ron = "0.8"

[dev-dependencies]
bevy = { version = "0.13", default_features = true, features = ["serialize"] }
bevy = { version = "0.14", default-features = true, features = ["serialize"] }
smol_str = "0.2"

[lib]
Expand Down
11 changes: 6 additions & 5 deletions examples/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ mod gamepad_viewer_example {
use std::f32::consts::PI;

use bevy::{
color::palettes,
input::gamepad::{GamepadButton, GamepadButtonChangedEvent, GamepadEvent, GamepadSettings},
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
Expand All @@ -125,11 +126,11 @@ mod gamepad_viewer_example {
const STICKS_X: f32 = 150.;
const STICKS_Y: f32 = -135.;

const NORMAL_BUTTON_COLOR: Color = Color::rgb(0.2, 0.2, 0.2);
const ACTIVE_BUTTON_COLOR: Color = Color::PURPLE;
const LIVE_COLOR: Color = Color::rgb(0.4, 0.4, 0.4);
const DEAD_COLOR: Color = Color::rgb(0.3, 0.3, 0.3);
const EXTENT_COLOR: Color = Color::rgb(0.3, 0.3, 0.3);
const NORMAL_BUTTON_COLOR: Color = Color::srgb(0.2, 0.2, 0.2);
const ACTIVE_BUTTON_COLOR: Color = Color::Srgba(palettes::css::PURPLE);
const LIVE_COLOR: Color = Color::srgb(0.4, 0.4, 0.4);
const DEAD_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
const EXTENT_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
const TEXT_COLOR: Color = Color::WHITE;

#[derive(Component, Deref)]
Expand Down
2 changes: 1 addition & 1 deletion examples/input_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use leafwing_input_playback::{
input_capture::InputCapturePlugin, timestamped_input::TimestampedInputs,
};

fn main() {
fn main() -> AppExit {
App::new()
.add_plugins((DefaultPlugins, InputCapturePlugin))
.add_systems(Update, debug_input_capture)
Expand Down
10 changes: 5 additions & 5 deletions examples/input_playback.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use bevy::{prelude::*, window::PrimaryWindow};
use bevy::{color::palettes, prelude::*, window::PrimaryWindow};

use leafwing_input_playback::{
input_capture::{InputCapturePlugin, InputModesCaptured},
input_playback::{InputPlaybackPlugin, PlaybackStrategy},
timestamped_input::TimestampedInputs,
};

fn main() {
fn main() -> AppExit {
App::new()
.add_plugins((DefaultPlugins, InputCapturePlugin, InputPlaybackPlugin))
// Disable all input capture and playback to start
.insert_resource(InputModesCaptured::DISABLE_ALL)
.insert_resource(PlaybackStrategy::Paused)
// Creates a little game that spawns decaying boxes where the player clicks
.insert_resource(ClearColor(Color::rgb(0.9, 0.9, 0.9)))
.insert_resource(ClearColor(Color::srgb(0.9, 0.9, 0.9)))
.add_systems(Startup, setup)
.add_systems(
Update,
Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn cursor_pos_as_world_pos(
let window_size = Vec2::new(current_window.width(), current_window.height());

// Convert screen position [0..resolution] to ndc [-1..1]
let ndc_to_world = cam_t.compute_matrix() * cam.projection_matrix().inverse();
let ndc_to_world = cam_t.compute_matrix() * cam.clip_from_view().inverse();
let ndc = (Vec2::new(cursor_pos.x, cursor_pos.y) / window_size) * 2.0 - Vec2::ONE;
let world_pos = ndc_to_world.project_point3(ndc.extend(-1.0));
world_pos.truncate()
Expand All @@ -68,7 +68,7 @@ fn spawn_boxes(
commands
.spawn(SpriteBundle {
sprite: Sprite {
color: Color::DARK_GREEN,
color: Color::Srgba(palettes::css::DARK_GREEN),
..default()
},
transform: Transform {
Expand Down
3 changes: 2 additions & 1 deletion src/input_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct InputCapturePlugin;
impl Plugin for InputCapturePlugin {
fn build(&self, app: &mut App) {
// Avoid double-adding frame_counter
if !app.world.contains_resource::<FrameCount>() {
if !app.world().contains_resource::<FrameCount>() {
app.init_resource::<FrameCount>()
.add_systems(First, frame_counter);
}
Expand Down Expand Up @@ -178,6 +178,7 @@ pub fn serialize_timestamped_inputs(
if let Some(file_path) = playback_file.path() {
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(file_path)
.expect("Could not open file.");
Expand Down
4 changes: 2 additions & 2 deletions src/input_playback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ pub struct InputPlaybackPlugin;
impl Plugin for InputPlaybackPlugin {
fn build(&self, app: &mut App) {
// Avoid double-adding frame_counter
if !app.world.contains_resource::<FrameCount>() {
if !app.world().contains_resource::<FrameCount>() {
app.init_resource::<FrameCount>()
.add_systems(First, frame_counter);
.add_systems(First, frame_counter.after(bevy::ecs::event::EventUpdates));
}

app.init_resource::<TimestampedInputs>()
Expand Down
34 changes: 17 additions & 17 deletions tests/input_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,30 @@ fn capture_app() -> App {
fn capture_sent_events() {
let mut app = capture_app();

let mut keyboard_events = app.world.resource_mut::<Events<KeyboardInput>>();
let mut keyboard_events = app.world_mut().resource_mut::<Events<KeyboardInput>>();
keyboard_events.send(TEST_PRESS);
keyboard_events.send(TEST_RELEASE);

app.update();
let timestamped_input = app.world.resource::<TimestampedInputs>();
let timestamped_input = app.world().resource::<TimestampedInputs>();
assert_eq!(timestamped_input.len(), 2);
}

#[test]
fn identity_of_sent_events() {
let mut app = capture_app();

let mut keyboard_events = app.world.resource_mut::<Events<KeyboardInput>>();
let mut keyboard_events = app.world_mut().resource_mut::<Events<KeyboardInput>>();
keyboard_events.send(TEST_PRESS);

// Events within the same frame are not ordered reliably
app.update();

let mut mouse_events = app.world.resource_mut::<Events<MouseButtonInput>>();
let mut mouse_events = app.world_mut().resource_mut::<Events<MouseButtonInput>>();
mouse_events.send(TEST_MOUSE);

app.update();
let mut timestamped_input = app.world.resource_mut::<TimestampedInputs>();
let mut timestamped_input = app.world_mut().resource_mut::<TimestampedInputs>();
let mut iterator = timestamped_input.iter_all().into_iter();

let first_event: TimestampedInputEvent = iterator.next().unwrap();
Expand All @@ -90,16 +90,16 @@ fn identity_of_sent_events() {
fn framecount_of_sent_events() {
let mut app = capture_app();

let mut keyboard_events = app.world.resource_mut::<Events<KeyboardInput>>();
let mut keyboard_events = app.world_mut().resource_mut::<Events<KeyboardInput>>();
keyboard_events.send(TEST_PRESS);

app.update();

let mut mouse_events = app.world.resource_mut::<Events<MouseButtonInput>>();
let mut mouse_events = app.world_mut().resource_mut::<Events<MouseButtonInput>>();
mouse_events.send(TEST_MOUSE);

app.update();
let mut timestamped_input = app.world.resource_mut::<TimestampedInputs>();
let mut timestamped_input = app.world_mut().resource_mut::<TimestampedInputs>();
let mut iterator = timestamped_input.iter_all().into_iter();

let first_event: TimestampedInputEvent = iterator.next().expect("Keyboard event failed.");
Expand All @@ -115,47 +115,47 @@ fn framecount_of_sent_events() {
fn toggle_input_capture() {
let mut app = capture_app();

let mut keyboard_events = app.world.resource_mut::<Events<KeyboardInput>>();
let mut keyboard_events = app.world_mut().resource_mut::<Events<KeyboardInput>>();
keyboard_events.send(TEST_PRESS);
keyboard_events.send(TEST_RELEASE);

app.update();

// Inputs are captured while input capturing is enabled by default
let timestamped_input = app.world.resource::<TimestampedInputs>();
let timestamped_input = app.world().resource::<TimestampedInputs>();
assert_eq!(timestamped_input.len(), 2);

// Disabling input capture
let mut input_modes_captured = app.world.resource_mut::<InputModesCaptured>();
let mut input_modes_captured = app.world_mut().resource_mut::<InputModesCaptured>();
*input_modes_captured = InputModesCaptured::DISABLE_ALL;

let mut keyboard_events = app.world.resource_mut::<Events<KeyboardInput>>();
let mut keyboard_events = app.world_mut().resource_mut::<Events<KeyboardInput>>();
keyboard_events.send(TEST_PRESS);
keyboard_events.send(TEST_RELEASE);

app.update();

// Inputs are not captured while input capturing is disabled
let timestamped_input = app.world.resource::<TimestampedInputs>();
let timestamped_input = app.world().resource::<TimestampedInputs>();
assert_eq!(timestamped_input.len(), 2);

// Partially re-enabling input capture
let mut input_modes_captured = app.world.resource_mut::<InputModesCaptured>();
let mut input_modes_captured = app.world_mut().resource_mut::<InputModesCaptured>();
*input_modes_captured = InputModesCaptured {
mouse_buttons: false,
keyboard: true,
..Default::default()
};

let mut keyboard_events = app.world.resource_mut::<Events<KeyboardInput>>();
let mut keyboard_events = app.world_mut().resource_mut::<Events<KeyboardInput>>();
keyboard_events.send(TEST_PRESS);

let mut mouse_events = app.world.resource_mut::<Events<MouseButtonInput>>();
let mut mouse_events = app.world_mut().resource_mut::<Events<MouseButtonInput>>();
mouse_events.send(TEST_MOUSE);

app.update();

// Only the keyboard events (and app exit events) were captured
let timestamped_input = app.world.resource::<TimestampedInputs>();
let timestamped_input = app.world().resource::<TimestampedInputs>();
assert_eq!(timestamped_input.len(), 3);
}
Loading

0 comments on commit e779860

Please sign in to comment.