
# Objective - Update winit dependency to 0.29 ## Changelog ### KeyCode changes - Removed `ScanCode`, as it was [replaced by KeyCode](https://github.com/rust-windowing/winit/blob/master/CHANGELOG.md#0292). - `ReceivedCharacter.char` is now a `SmolStr`, [relevant doc](https://docs.rs/winit/latest/winit/event/struct.KeyEvent.html#structfield.text). - Changed most `KeyCode` values, and added more. KeyCode has changed meaning. With this PR, it refers to physical position on keyboard rather than the printed letter on keyboard keys. In practice this means: - On QWERTY keyboard layouts, nothing changes - On any other keyboard layout, `KeyCode` no longer reflects the label on key. - This is "good". In bevy 0.12, when you used WASD for movement, users with non-QWERTY keyboards couldn't play your game! This was especially bad for non-latin keyboards. Now, WASD represents the physical keys. A French player will press the ZQSD keys, which are near each other, Kyrgyz players will use "Цфыв". - This is "bad" as well. You can't know in advance what the label of the key for input is. Your UI says "press WASD to move", even if in reality, they should be pressing "ZQSD" or "Цфыв". You also no longer can use `KeyCode` for text inputs. In any case, it was a pretty bad API for text input. You should use `ReceivedCharacter` now instead. ### Other changes - Use `web-time` rather than `instant` crate. (https://github.com/rust-windowing/winit/pull/2836) - winit did split `run_return` in `run_onDemand` and `pump_events`, I did the same change in bevy_winit and used `pump_events`. - Removed `return_from_run` from `WinitSettings` as `winit::run` now returns on supported platforms. - I left the example "return_after_run" as I think it's still useful. - This winit change is done partly to allow to create a new window after quitting all windows: https://github.com/emilk/egui/issues/1918 ; this PR doesn't address. - added `width` and `height` properties in the `canvas` from wasm example (https://github.com/bevyengine/bevy/pull/10702#discussion_r1420567168) ## Known regressions (important follow ups?) - Provide an API for reacting when a specific key from current layout was released. - possible solutions: use winit::Key from winit::KeyEvent ; mapping between KeyCode and Key ; or . - We don't receive characters through alt+numpad (e.g. alt + 151 = "ù") anymore ; reproduced on winit example "ime". maybe related to https://github.com/rust-windowing/winit/issues/2945 - (windows) Window content doesn't refresh at all when resizing. By reading https://github.com/rust-windowing/winit/issues/2900 ; I suspect we should just fire a `window.request_redraw();` from `AboutToWait`, and handle actual redrawing within `RedrawRequested`. I'm not sure how to move all that code so I'd appreciate it to be a follow up. - (windows) unreleased winit fix for using set_control_flow in AboutToWait https://github.com/rust-windowing/winit/issues/3215 ; ⚠️ I'm not sure what the implications are, but that feels bad 🤔 ## Follow up I'd like to avoid bloating this PR, here are a few follow up tasks worthy of a separate PR, or new issue to track them once this PR is closed, as they would either complicate reviews, or at risk of being controversial: - remove CanvasParentResizePlugin (https://github.com/bevyengine/bevy/pull/10702#discussion_r1417068856) - avoid mentionning explicitly winit in docs from bevy_window ? - NamedKey integration on bevy_input: https://github.com/rust-windowing/winit/pull/3143 introduced a new NamedKey variant. I implemented it only on the converters but we'd benefit making the same changes to bevy_input. - Add more info in KeyboardInput https://github.com/bevyengine/bevy/pull/10702#pullrequestreview-1748336313 - https://github.com/bevyengine/bevy/pull/9905 added a workaround on a bug allegedly fixed by winit 0.29. We should check if it's still necessary. - update to raw_window_handle 0.6 - blocked by wgpu - Rename `KeyCode` to `PhysicalKeyCode` https://github.com/bevyengine/bevy/pull/10702#discussion_r1404595015 - remove `instant` dependency, [replaced by](https://github.com/rust-windowing/winit/pull/2836) `web_time`), we'd need to update to : - fastrand >= 2.0 - [`async-executor`](https://github.com/smol-rs/async-executor) >= 1.7 - [`futures-lite`](https://github.com/smol-rs/futures-lite) >= 2.0 - Verify license, see [discussion](https://github.com/bevyengine/bevy/pull/8745#discussion_r1402439800) - we might be missing a short notice or description of changes made - Consider using https://github.com/rust-windowing/cursor-icon directly rather than vendoring it in bevy. - investigate [this unwrap](https://github.com/bevyengine/bevy/pull/8745#discussion_r1387044986) (`winit_window.canvas().unwrap();`) - Use more good things about winit's update - https://github.com/bevyengine/bevy/pull/10689#issuecomment-1823560428 ## Migration Guide This PR should have one.
189 lines
6.8 KiB
Rust
189 lines
6.8 KiB
Rust
//! Illustrates how to change window settings and shows how to affect
|
|
//! the mouse pointer in various ways.
|
|
|
|
use bevy::{
|
|
core::FrameCount,
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
prelude::*,
|
|
window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((
|
|
DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
title: "I am a window!".into(),
|
|
resolution: (500., 300.).into(),
|
|
present_mode: PresentMode::AutoVsync,
|
|
// Tells wasm to resize the window according to the available canvas
|
|
fit_canvas_to_parent: true,
|
|
// Tells wasm not to override default event handling, like F5, Ctrl+R etc.
|
|
prevent_default_event_handling: false,
|
|
window_theme: Some(WindowTheme::Dark),
|
|
enabled_buttons: bevy::window::EnabledButtons {
|
|
maximize: false,
|
|
..Default::default()
|
|
},
|
|
// This will spawn an invisible window
|
|
// The window will be made visible in the make_visible() system after 3 frames.
|
|
// This is useful when you want to avoid the white window that shows up before the GPU is ready to render the app.
|
|
visible: false,
|
|
..default()
|
|
}),
|
|
..default()
|
|
}),
|
|
LogDiagnosticsPlugin::default(),
|
|
FrameTimeDiagnosticsPlugin,
|
|
))
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
change_title,
|
|
toggle_theme,
|
|
toggle_cursor,
|
|
toggle_vsync,
|
|
toggle_window_controls,
|
|
cycle_cursor_icon,
|
|
switch_level,
|
|
make_visible,
|
|
),
|
|
)
|
|
.run();
|
|
}
|
|
|
|
fn make_visible(mut window: Query<&mut Window>, frames: Res<FrameCount>) {
|
|
// The delay may be different for your app or system.
|
|
if frames.0 == 3 {
|
|
// At this point the gpu is ready to show the app so we can make the window visible.
|
|
// Alternatively, you could toggle the visibility in Startup.
|
|
// It will work, but it will have one white frame before it starts rendering
|
|
window.single_mut().visible = true;
|
|
}
|
|
}
|
|
|
|
/// This system toggles the vsync mode when pressing the button V.
|
|
/// You'll see fps increase displayed in the console.
|
|
fn toggle_vsync(input: Res<ButtonInput<KeyCode>>, mut windows: Query<&mut Window>) {
|
|
if input.just_pressed(KeyCode::KeyV) {
|
|
let mut window = windows.single_mut();
|
|
|
|
window.present_mode = if matches!(window.present_mode, PresentMode::AutoVsync) {
|
|
PresentMode::AutoNoVsync
|
|
} else {
|
|
PresentMode::AutoVsync
|
|
};
|
|
info!("PRESENT_MODE: {:?}", window.present_mode);
|
|
}
|
|
}
|
|
|
|
/// This system switches the window level when pressing the T button
|
|
/// You'll notice it won't be covered by other windows, or will be covered by all the other
|
|
/// windows depending on the level.
|
|
///
|
|
/// This feature only works on some platforms. Please check the
|
|
/// [documentation](https://docs.rs/bevy/latest/bevy/prelude/struct.Window.html#structfield.window_level)
|
|
/// for more details.
|
|
|
|
fn switch_level(input: Res<ButtonInput<KeyCode>>, mut windows: Query<&mut Window>) {
|
|
if input.just_pressed(KeyCode::KeyT) {
|
|
let mut window = windows.single_mut();
|
|
|
|
window.window_level = match window.window_level {
|
|
WindowLevel::AlwaysOnBottom => WindowLevel::Normal,
|
|
WindowLevel::Normal => WindowLevel::AlwaysOnTop,
|
|
WindowLevel::AlwaysOnTop => WindowLevel::AlwaysOnBottom,
|
|
};
|
|
info!("WINDOW_LEVEL: {:?}", window.window_level);
|
|
}
|
|
}
|
|
|
|
/// This system toggles the window controls when pressing buttons 1, 2 and 3
|
|
///
|
|
/// This feature only works on some platforms. Please check the
|
|
/// [documentation](https://docs.rs/bevy/latest/bevy/prelude/struct.Window.html#structfield.enabled_buttons)
|
|
/// for more details.
|
|
fn toggle_window_controls(input: Res<ButtonInput<KeyCode>>, mut windows: Query<&mut Window>) {
|
|
let toggle_minimize = input.just_pressed(KeyCode::Digit1);
|
|
let toggle_maximize = input.just_pressed(KeyCode::Digit2);
|
|
let toggle_close = input.just_pressed(KeyCode::Digit3);
|
|
|
|
if toggle_minimize || toggle_maximize || toggle_close {
|
|
let mut window = windows.single_mut();
|
|
|
|
if toggle_minimize {
|
|
window.enabled_buttons.minimize = !window.enabled_buttons.minimize;
|
|
}
|
|
if toggle_maximize {
|
|
window.enabled_buttons.maximize = !window.enabled_buttons.maximize;
|
|
}
|
|
if toggle_close {
|
|
window.enabled_buttons.close = !window.enabled_buttons.close;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// This system will then change the title during execution
|
|
fn change_title(mut windows: Query<&mut Window>, time: Res<Time>) {
|
|
let mut window = windows.single_mut();
|
|
window.title = format!(
|
|
"Seconds since startup: {}",
|
|
time.elapsed().as_secs_f32().round()
|
|
);
|
|
}
|
|
|
|
fn toggle_cursor(mut windows: Query<&mut Window>, input: Res<ButtonInput<KeyCode>>) {
|
|
if input.just_pressed(KeyCode::Space) {
|
|
let mut window = windows.single_mut();
|
|
|
|
window.cursor.visible = !window.cursor.visible;
|
|
window.cursor.grab_mode = match window.cursor.grab_mode {
|
|
CursorGrabMode::None => CursorGrabMode::Locked,
|
|
CursorGrabMode::Locked | CursorGrabMode::Confined => CursorGrabMode::None,
|
|
};
|
|
}
|
|
}
|
|
|
|
// This system will toggle the color theme used by the window
|
|
fn toggle_theme(mut windows: Query<&mut Window>, input: Res<ButtonInput<KeyCode>>) {
|
|
if input.just_pressed(KeyCode::KeyF) {
|
|
let mut window = windows.single_mut();
|
|
|
|
if let Some(current_theme) = window.window_theme {
|
|
window.window_theme = match current_theme {
|
|
WindowTheme::Light => Some(WindowTheme::Dark),
|
|
WindowTheme::Dark => Some(WindowTheme::Light),
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
/// This system cycles the cursor's icon through a small set of icons when clicking
|
|
fn cycle_cursor_icon(
|
|
mut windows: Query<&mut Window>,
|
|
input: Res<ButtonInput<MouseButton>>,
|
|
mut index: Local<usize>,
|
|
) {
|
|
let mut window = windows.single_mut();
|
|
|
|
const ICONS: &[CursorIcon] = &[
|
|
CursorIcon::Default,
|
|
CursorIcon::Pointer,
|
|
CursorIcon::Wait,
|
|
CursorIcon::Text,
|
|
CursorIcon::Copy,
|
|
];
|
|
|
|
if input.just_pressed(MouseButton::Left) {
|
|
*index = (*index + 1) % ICONS.len();
|
|
} else if input.just_pressed(MouseButton::Right) {
|
|
*index = if *index == 0 {
|
|
ICONS.len() - 1
|
|
} else {
|
|
*index - 1
|
|
};
|
|
}
|
|
|
|
window.cursor.icon = ICONS[*index];
|
|
}
|