
# Objective - Upgrade winit to v0.30 - Fixes https://github.com/bevyengine/bevy/issues/13331 ## Solution This is a rewrite/adaptation of the new trait system described and implemented in `winit` v0.30. ## Migration Guide The custom UserEvent is now renamed as WakeUp, used to wake up the loop if anything happens outside the app (a new [custom_user_event](https://github.com/bevyengine/bevy/pull/13366/files#diff-2de8c0a8d3028d0059a3d80ae31b2bbc1cde2595ce2d317ea378fe3e0cf6ef2d) shows this behavior. The internal `UpdateState` has been removed and replaced internally by the AppLifecycle. When changed, the AppLifecycle is sent as an event. The `UpdateMode` now accepts only two values: `Continuous` and `Reactive`, but the latter exposes 3 new properties to enable reactive to device, user or window events. The previous `UpdateMode::Reactive` is now equivalent to `UpdateMode::reactive()`, while `UpdateMode::ReactiveLowPower` to `UpdateMode::reactive_low_power()`. The `ApplicationLifecycle` has been renamed as `AppLifecycle`, and now contains the possible values of the application state inside the event loop: * `Idle`: the loop has not started yet * `Running` (previously called `Started`): the loop is running * `WillSuspend`: the loop is going to be suspended * `Suspended`: the loop is suspended * `WillResume`: the loop is going to be resumed Note: the `Resumed` state has been removed since the resumed app is just running. Finally, now that `winit` enables this, it extends the `WinitPlugin` to support custom events. ## Test platforms - [x] Windows - [x] MacOs - [x] Linux (x11) - [x] Linux (Wayland) - [x] Android - [x] iOS - [x] WASM/WebGPU - [x] WASM/WebGL2 ## Outstanding issues / regressions - [ ] iOS: build failed in CI - blocking, but may just be flakiness - [x] Cross-platform: when the window is maximised, changes in the scale factor don't apply, to make them apply one has to make the window smaller again. (Re-maximising keeps the updated scale factor) - non-blocking, but good to fix - [ ] Android: it's pretty easy to quickly open and close the app and then the music keeps playing when suspended. - non-blocking but worrying - [ ] Web: the application will hang when switching tabs - Not new, duplicate of https://github.com/bevyengine/bevy/issues/13486 - [ ] Cross-platform?: Screenshot failure, `ERROR present_frames: wgpu_core::present: No work has been submitted for this frame before` taking the first screenshot, but after pressing space - non-blocking, but good to fix --------- Co-authored-by: François <francois.mockers@vleue.com>
184 lines
5.6 KiB
Rust
184 lines
5.6 KiB
Rust
//! A 3d Scene with a button and playing sound.
|
|
|
|
use bevy::{
|
|
color::palettes::basic::*,
|
|
input::touch::TouchPhase,
|
|
prelude::*,
|
|
window::{AppLifecycle, WindowMode},
|
|
};
|
|
|
|
// the `bevy_main` proc_macro generates the required boilerplate for iOS and Android
|
|
#[bevy_main]
|
|
fn main() {
|
|
let mut app = App::new();
|
|
app.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
resizable: false,
|
|
mode: WindowMode::BorderlessFullscreen,
|
|
..default()
|
|
}),
|
|
..default()
|
|
}))
|
|
.add_systems(Startup, (setup_scene, setup_music))
|
|
.add_systems(Update, (touch_camera, button_handler, handle_lifetime));
|
|
|
|
// MSAA makes some Android devices panic, this is under investigation
|
|
// https://github.com/bevyengine/bevy/issues/8229
|
|
#[cfg(target_os = "android")]
|
|
app.insert_resource(Msaa::Off);
|
|
|
|
app.run();
|
|
}
|
|
|
|
fn touch_camera(
|
|
windows: Query<&Window>,
|
|
mut touches: EventReader<TouchInput>,
|
|
mut camera: Query<&mut Transform, With<Camera3d>>,
|
|
mut last_position: Local<Option<Vec2>>,
|
|
) {
|
|
let window = windows.single();
|
|
|
|
for touch in touches.read() {
|
|
if touch.phase == TouchPhase::Started {
|
|
*last_position = None;
|
|
}
|
|
if let Some(last_position) = *last_position {
|
|
let mut transform = camera.single_mut();
|
|
*transform = Transform::from_xyz(
|
|
transform.translation.x
|
|
+ (touch.position.x - last_position.x) / window.width() * 5.0,
|
|
transform.translation.y,
|
|
transform.translation.z
|
|
+ (touch.position.y - last_position.y) / window.height() * 5.0,
|
|
)
|
|
.looking_at(Vec3::ZERO, Vec3::Y);
|
|
}
|
|
*last_position = Some(touch.position);
|
|
}
|
|
}
|
|
|
|
/// set up a simple 3D scene
|
|
fn setup_scene(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
// plane
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Plane3d::default().mesh().size(5.0, 5.0)),
|
|
material: materials.add(Color::srgb(0.1, 0.2, 0.1)),
|
|
..default()
|
|
});
|
|
// cube
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Cuboid::default()),
|
|
material: materials.add(Color::srgb(0.5, 0.4, 0.3)),
|
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
|
..default()
|
|
});
|
|
// sphere
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Sphere::new(0.5).mesh().ico(4).unwrap()),
|
|
material: materials.add(Color::srgb(0.1, 0.4, 0.8)),
|
|
transform: Transform::from_xyz(1.5, 1.5, 1.5),
|
|
..default()
|
|
});
|
|
// light
|
|
commands.spawn(PointLightBundle {
|
|
transform: Transform::from_xyz(4.0, 8.0, 4.0),
|
|
point_light: PointLight {
|
|
intensity: 1_000_000.0,
|
|
// Shadows makes some Android devices segfault, this is under investigation
|
|
// https://github.com/bevyengine/bevy/issues/8214
|
|
#[cfg(not(target_os = "android"))]
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
// camera
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
|
|
// Test ui
|
|
commands
|
|
.spawn((
|
|
ButtonBundle {
|
|
style: Style {
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(50.0),
|
|
right: Val::Px(50.0),
|
|
bottom: Val::Px(50.0),
|
|
..default()
|
|
},
|
|
image: UiImage::default().with_color(Color::NONE),
|
|
..default()
|
|
},
|
|
BackgroundColor(Color::WHITE),
|
|
))
|
|
.with_children(|b| {
|
|
b.spawn(
|
|
TextBundle::from_section(
|
|
"Test Button",
|
|
TextStyle {
|
|
font_size: 30.0,
|
|
color: Color::BLACK,
|
|
..default()
|
|
},
|
|
)
|
|
.with_text_justify(JustifyText::Center),
|
|
);
|
|
});
|
|
}
|
|
|
|
fn button_handler(
|
|
mut interaction_query: Query<
|
|
(&Interaction, &mut BackgroundColor),
|
|
(Changed<Interaction>, With<Button>),
|
|
>,
|
|
) {
|
|
for (interaction, mut color) in &mut interaction_query {
|
|
match *interaction {
|
|
Interaction::Pressed => {
|
|
*color = BLUE.into();
|
|
}
|
|
Interaction::Hovered => {
|
|
*color = GRAY.into();
|
|
}
|
|
Interaction::None => {
|
|
*color = WHITE.into();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn setup_music(asset_server: Res<AssetServer>, mut commands: Commands) {
|
|
commands.spawn(AudioBundle {
|
|
source: asset_server.load("sounds/Windless Slopes.ogg"),
|
|
settings: PlaybackSettings::LOOP,
|
|
});
|
|
}
|
|
|
|
// Pause audio when app goes into background and resume when it returns.
|
|
// This is handled by the OS on iOS, but not on Android.
|
|
fn handle_lifetime(
|
|
mut lifecycle_events: EventReader<AppLifecycle>,
|
|
music_controller: Query<&AudioSink>,
|
|
) {
|
|
let Ok(music_controller) = music_controller.get_single() else {
|
|
return;
|
|
};
|
|
|
|
for event in lifecycle_events.read() {
|
|
match event {
|
|
AppLifecycle::Idle | AppLifecycle::WillSuspend | AppLifecycle::WillResume => {}
|
|
AppLifecycle::Suspended => music_controller.pause(),
|
|
AppLifecycle::Running => music_controller.play(),
|
|
}
|
|
}
|
|
}
|