Add received character (#805)

* Add ReceivedCharacter window event

* Add ReceivedCharacter window event examples
This commit is contained in:
Oscar 2020-11-06 17:15:56 -08:00 committed by GitHub
parent 9224983897
commit f54788527b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 2 deletions

View file

@ -240,6 +240,10 @@ path = "examples/input/keyboard_input.rs"
name = "keyboard_input_events"
path = "examples/input/keyboard_input_events.rs"
[[example]]
name = "char_input_events"
path = "examples/input/char_input_events.rs"
[[example]]
name = "gamepad_input"
path = "examples/input/gamepad_input.rs"

View file

@ -40,3 +40,10 @@ pub struct CursorMoved {
pub id: WindowId,
pub position: Vec2,
}
/// An event that is sent whenever a window receives a character from the OS or underlying system.
#[derive(Debug, Clone)]
pub struct ReceivedCharacter {
pub id: WindowId,
pub char: char,
}

View file

@ -9,7 +9,7 @@ pub use window::*;
pub use windows::*;
pub mod prelude {
pub use crate::{CursorMoved, Window, WindowDescriptor, Windows};
pub use crate::{CursorMoved, ReceivedCharacter, Window, WindowDescriptor, Windows};
}
use bevy_app::prelude::*;
@ -37,6 +37,7 @@ impl Plugin for WindowPlugin {
.add_event::<WindowCloseRequested>()
.add_event::<CloseWindow>()
.add_event::<CursorMoved>()
.add_event::<ReceivedCharacter>()
.init_resource::<Windows>();
if self.add_primary_window {

View file

@ -13,7 +13,8 @@ use bevy_app::{prelude::*, AppExit};
use bevy_ecs::{IntoThreadLocalSystem, Resources, World};
use bevy_math::Vec2;
use bevy_window::{
CreateWindow, CursorMoved, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows,
CreateWindow, CursorMoved, ReceivedCharacter, Window, WindowCloseRequested, WindowCreated,
WindowResized, Windows,
};
use winit::{
event::{self, DeviceEvent, Event, WindowEvent},
@ -272,6 +273,20 @@ pub fn winit_runner(mut app: App) {
}
touch_input_events.send(converters::convert_touch_input(touch));
}
WindowEvent::ReceivedCharacter(c) => {
let mut char_input_events = app
.resources
.get_mut::<Events<ReceivedCharacter>>()
.unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
char_input_events.send(ReceivedCharacter {
id: window_id,
char: c,
})
}
_ => {}
},
event::Event::DeviceEvent { ref event, .. } => {

View file

@ -0,0 +1,23 @@
use bevy::{prelude::*, window::ReceivedCharacter};
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_system(print_char_event_system.system())
.run();
}
#[derive(Default)]
struct State {
event_reader: EventReader<ReceivedCharacter>,
}
/// This system prints out all char events as they come in
fn print_char_event_system(
mut state: Local<State>,
char_input_events: Res<Events<ReceivedCharacter>>,
) {
for event in state.event_reader.iter(&char_input_events) {
println!("{:?}: '{}'", event, event.char);
}
}