Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate ReceivedCharacter #12868

Merged
merged 8 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(deprecated)]
use std::path::PathBuf;

use bevy_ecs::entity::Entity;
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<WindowResized>()
.add_event::<WindowCreated>()
.add_event::<WindowClosed>()
Expand Down Expand Up @@ -132,6 +134,7 @@ impl Plugin for WindowPlugin {
}

// Register event types
#[allow(deprecated)]
app.register_type::<WindowResized>()
.register_type::<RequestRedraw>()
.register_type::<WindowCreated>()
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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,
Expand Down Expand Up @@ -462,6 +463,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 });
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_winit/src/winit_event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(deprecated)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be localized to where they're supposed to be used.

Copy link
Contributor Author

@chompaa chompaa Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this achieved for the WinitEvent enum in winit_event.rs? I've tried the following placements:

#[allow(deprecated)]
pub enum WinitEvent {
  // ..
  #[allow(deprecated)]
  ReceivedCharacter(#[allow(deprecated)] ReceivedCharacter),
  // ..
}

but clippy still seems to complain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs to go on the functions that use the WinitEvent

#![allow(missing_docs)]

use bevy_app::App;
Expand Down
18 changes: 15 additions & 3 deletions examples/input/char_input_events.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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<ReceivedCharacter>) {
fn print_char_event_system(mut char_input_events: EventReader<KeyboardInput>) {
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);
}
}
}
37 changes: 21 additions & 16 deletions examples/input/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -17,7 +23,6 @@ fn main() {
(
toggle_ime,
listen_ime_events,
listen_received_character_events,
listen_keyboard_input_events,
bubbling_text,
),
Expand Down Expand Up @@ -163,25 +168,19 @@ fn listen_ime_events(
}
}

fn listen_received_character_events(
mut events: EventReader<ReceivedCharacter>,
mut edit_text: Query<&mut Text, (Without<Node>, Without<Bubble>)>,
) {
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<KeyboardInput>,
mut edit_text: Query<&mut Text, (Without<Node>, Without<Bubble>)>,
) {
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;
Expand All @@ -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,
}
}
Expand Down