From 7b4b5966d91d7519898260cc06f4da1bcbdb147e Mon Sep 17 00:00:00 2001 From: Antony Date: Mon, 29 Apr 2024 20:49:41 -0400 Subject: [PATCH] Deprecate `ReceivedCharacter` (#12868) # Objective - Partially resolves #12639. ## Solution - Deprecate `ReceivedCharacter`. - Replace `ReceivedCharacter` with `KeyboardInput` in the relevant examples. ## Migration Guide - `ReceivedCharacter` is now deprecated, use `KeyboardInput` instead. - Before: ```rust fn listen_characters(events: EventReader) { for event in events.read() { info!("{}", event.char); } } ``` After: ```rust fn listen_characters(events: EventReader) { for event in events.read() { // Only check for characters when the key is pressed. if event.state == ButtonState::Released { continue; } // Note that some keys such as `Space` and `Tab` won't be detected as before. // Instead, check for them with `Key::Space` and `Key::Tab`. if let Key::Character(character) = &event.logical_key { info!("{}", character); } } } ``` --------- Co-authored-by: Mike --- crates/bevy_window/src/event.rs | 2 ++ crates/bevy_window/src/lib.rs | 3 +++ crates/bevy_winit/src/lib.rs | 2 ++ crates/bevy_winit/src/winit_event.rs | 1 + examples/input/char_input_events.rs | 18 +++++++++++--- examples/input/text_input.rs | 37 ++++++++++++++++------------ 6 files changed, 44 insertions(+), 19 deletions(-) diff --git a/crates/bevy_window/src/event.rs b/crates/bevy_window/src/event.rs index 385e2d5549..039b646b8f 100644 --- a/crates/bevy_window/src/event.rs +++ b/crates/bevy_window/src/event.rs @@ -1,3 +1,4 @@ +#![allow(deprecated)] use std::path::PathBuf; use bevy_ecs::entity::Entity; @@ -171,6 +172,7 @@ pub struct CursorLeft { } /// An event that is sent whenever a window receives a character from the OS or underlying system. +#[deprecated(since = "0.14.0", note = "Use `KeyboardInput` instead.")] #[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)] #[reflect(Debug, PartialEq)] #[cfg_attr( diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index c9ae32dca5..cabdb04d18 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -28,6 +28,7 @@ pub use window::*; #[allow(missing_docs)] pub mod prelude { + #[allow(deprecated)] #[doc(hidden)] pub use crate::{ CursorEntered, CursorIcon, CursorLeft, CursorMoved, FileDragAndDrop, Ime, MonitorSelection, @@ -85,6 +86,7 @@ pub struct WindowPlugin { impl Plugin for WindowPlugin { fn build(&self, app: &mut App) { // User convenience events + #[allow(deprecated)] app.add_event::() .add_event::() .add_event::() @@ -132,6 +134,7 @@ impl Plugin for WindowPlugin { } // Register event types + #[allow(deprecated)] app.register_type::() .register_type::() .register_type::() diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index f566d72e1e..9507eabaeb 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -43,6 +43,7 @@ use bevy_math::{ivec2, DVec2, Vec2}; #[cfg(not(target_arch = "wasm32"))] use bevy_tasks::tick_global_task_pools_on_main_thread; use bevy_utils::tracing::{error, trace, warn}; +#[allow(deprecated)] use bevy_window::{ exit_on_all_closed, ApplicationLifetime, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, Ime, ReceivedCharacter, RequestRedraw, Window, @@ -474,6 +475,7 @@ fn handle_winit_event( if event.state.is_pressed() { if let Some(char) = &event.text { let char = char.clone(); + #[allow(deprecated)] winit_events.send(ReceivedCharacter { window, char }); } } diff --git a/crates/bevy_winit/src/winit_event.rs b/crates/bevy_winit/src/winit_event.rs index 3e47ff17df..a314ea2896 100644 --- a/crates/bevy_winit/src/winit_event.rs +++ b/crates/bevy_winit/src/winit_event.rs @@ -1,3 +1,4 @@ +#![allow(deprecated)] #![allow(missing_docs)] use bevy_app::App; diff --git a/examples/input/char_input_events.rs b/examples/input/char_input_events.rs index f2e52dd733..365bd7c92c 100644 --- a/examples/input/char_input_events.rs +++ b/examples/input/char_input_events.rs @@ -1,6 +1,12 @@ //! Prints out all chars as they are inputted. -use bevy::prelude::*; +use bevy::{ + input::{ + keyboard::{Key, KeyboardInput}, + ButtonState, + }, + prelude::*, +}; fn main() { App::new() @@ -10,8 +16,14 @@ fn main() { } /// This system prints out all char events as they come in -fn print_char_event_system(mut char_input_events: EventReader) { +fn print_char_event_system(mut char_input_events: EventReader) { for event in char_input_events.read() { - info!("{:?}: '{}'", event, event.char); + // Only check for characters when the key is pressed + if event.state == ButtonState::Released { + continue; + } + if let Key::Character(character) = &event.logical_key { + info!("{:?}: '{}'", event, character); + } } } diff --git a/examples/input/text_input.rs b/examples/input/text_input.rs index ffe03de151..6952dc8e5a 100644 --- a/examples/input/text_input.rs +++ b/examples/input/text_input.rs @@ -6,7 +6,13 @@ use std::mem; -use bevy::{input::keyboard::KeyboardInput, prelude::*}; +use bevy::{ + input::{ + keyboard::{Key, KeyboardInput}, + ButtonState, + }, + prelude::*, +}; fn main() { App::new() @@ -17,7 +23,6 @@ fn main() { ( toggle_ime, listen_ime_events, - listen_received_character_events, listen_keyboard_input_events, bubbling_text, ), @@ -163,25 +168,19 @@ fn listen_ime_events( } } -fn listen_received_character_events( - mut events: EventReader, - mut edit_text: Query<&mut Text, (Without, Without)>, -) { - for event in events.read() { - edit_text.single_mut().sections[0] - .value - .push_str(&event.char); - } -} - fn listen_keyboard_input_events( mut commands: Commands, mut events: EventReader, mut edit_text: Query<&mut Text, (Without, Without)>, ) { for event in events.read() { - match event.key_code { - KeyCode::Enter => { + // Only trigger changes when the key is first pressed. + if event.state == ButtonState::Released { + continue; + } + + match &event.logical_key { + Key::Enter => { let mut text = edit_text.single_mut(); if text.sections[0].value.is_empty() { continue; @@ -198,9 +197,15 @@ fn listen_keyboard_input_events( }, )); } - KeyCode::Backspace => { + Key::Space => { + edit_text.single_mut().sections[0].value.push(' '); + } + Key::Backspace => { edit_text.single_mut().sections[0].value.pop(); } + Key::Character(character) => { + edit_text.single_mut().sections[0].value.push_str(character); + } _ => continue, } }