pub struct Input { /* private fields */ }
Expand description
Provides access to the input events handled by the Doryen engine. See the
documentation for the InputApi
type for details on what values should
be used with the various key
methods.
Implementations§
Source§impl Input
impl Input
Sourcepub fn key(&self, key: &str) -> bool
pub fn key(&self, key: &str) -> bool
Returns the current status of the given key (true if currently pressed).
Examples found in repository?
28fn process_input(
29 input: Res<Input>,
30 mut close_requested: ResMut<CloseRequested>,
31 mut app_exit: EventWriter<AppExit>,
32) {
33 if close_requested.0 {
34 if input.key("KeyY") {
35 app_exit.send(AppExit::Success);
36 } else if input.key("KeyN") {
37 close_requested.0 = false;
38 }
39 } else if input.key("Escape") || input.close_requested() {
40 close_requested.0 = true;
41 }
42}
More examples
53fn move_player(
54 input: Res<Input>,
55 mut query: Query<(&mut Position, &Speed), With<Player>>,
56 walls: Res<Walls>,
57) {
58 let mut movement = (0, 0);
59 if input.key("ArrowLeft") || input.key("KeyA") {
60 movement.0 = -2;
61 } else if input.key("ArrowRight") || input.key("KeyD") {
62 movement.0 = 2;
63 }
64 if input.key("ArrowUp") || input.key("KeyW") {
65 movement.1 = -2;
66 } else if input.key("ArrowDown") || input.key("KeyS") {
67 movement.1 = 2;
68 }
69 if movement == (0, 0) {
70 return;
71 }
72
73 let mut coef = DEFAULT_COEFFICIENT;
74 let (mut player_position, player_speed) = query.single_mut();
75
76 let next_x_pos = player_position.next_pos((movement.0 as f32, 0.));
77
78 if walls.is_wall(next_x_pos) {
79 movement.0 = 0;
80 coef = 1.0;
81 }
82 let next_y_pos = player_position.next_pos((0., movement.1 as f32));
83
84 if walls.is_wall(next_y_pos) {
85 movement.1 = 0;
86 coef = 1.0;
87 }
88 player_position.move_by(movement, *player_speed, coef);
89}
100fn input(
101 input: Res<Input>,
102 entities: Res<Entities>,
103 mut player_query: Query<(&mut Position<i32>, &Player)>,
104 mut mouse_query: Query<(&mut Position<f32>, &Mouse)>,
105 mut screenshot_index: ResMut<ScreenshotIndex>,
106 mut capture_events: EventWriter<Capture>,
107) {
108 let (mut player_position, _) = player_query.get_mut(entities.player).unwrap();
109
110 if input.key("ArrowLeft") {
111 player_position.x = (player_position.x - 1).max(1);
112 } else if input.key("ArrowRight") {
113 player_position.x = (player_position.x + 1).min(CONSOLE_WIDTH as i32 - 2);
114 }
115 if input.key("ArrowUp") {
116 player_position.y = (player_position.y - 1).max(1);
117 } else if input.key("ArrowDown") {
118 player_position.y = (player_position.y + 1).min(CONSOLE_HEIGHT as i32 - 2);
119 }
120
121 let (mut mouse_position, _) = mouse_query.get_mut(entities.mouse).unwrap();
122
123 let new_mouse_position = input.mouse_pos();
124 mouse_position.x = new_mouse_position.0;
125 mouse_position.y = new_mouse_position.1;
126
127 if input.key("ControlLeft") && input.key_pressed("KeyS") {
128 screenshot_index.0 += 1;
129 capture_events.send(Capture::new(format!(
130 "screenshot_{:03}.png",
131 screenshot_index.0
132 )));
133 }
134}
Sourcepub fn key_pressed(&self, key: &str) -> bool
pub fn key_pressed(&self, key: &str) -> bool
Returns true if the given key was pressed since the last update.
Examples found in repository?
100fn input(
101 input: Res<Input>,
102 entities: Res<Entities>,
103 mut player_query: Query<(&mut Position<i32>, &Player)>,
104 mut mouse_query: Query<(&mut Position<f32>, &Mouse)>,
105 mut screenshot_index: ResMut<ScreenshotIndex>,
106 mut capture_events: EventWriter<Capture>,
107) {
108 let (mut player_position, _) = player_query.get_mut(entities.player).unwrap();
109
110 if input.key("ArrowLeft") {
111 player_position.x = (player_position.x - 1).max(1);
112 } else if input.key("ArrowRight") {
113 player_position.x = (player_position.x + 1).min(CONSOLE_WIDTH as i32 - 2);
114 }
115 if input.key("ArrowUp") {
116 player_position.y = (player_position.y - 1).max(1);
117 } else if input.key("ArrowDown") {
118 player_position.y = (player_position.y + 1).min(CONSOLE_HEIGHT as i32 - 2);
119 }
120
121 let (mut mouse_position, _) = mouse_query.get_mut(entities.mouse).unwrap();
122
123 let new_mouse_position = input.mouse_pos();
124 mouse_position.x = new_mouse_position.0;
125 mouse_position.y = new_mouse_position.1;
126
127 if input.key("ControlLeft") && input.key_pressed("KeyS") {
128 screenshot_index.0 += 1;
129 capture_events.send(Capture::new(format!(
130 "screenshot_{:03}.png",
131 screenshot_index.0
132 )));
133 }
134}
Sourcepub fn keys_pressed(&self) -> Keys<'_> ⓘ
pub fn keys_pressed(&self) -> Keys<'_> ⓘ
Returns an iterator over all the keys that were pressed since the last update in no particular order.
Sourcepub fn key_released(&self, key: &str) -> bool
pub fn key_released(&self, key: &str) -> bool
Returns true if the given key was released since the last update.
Examples found in repository?
68fn update(mut font: ResMut<Font>, input: Res<Input>, mut set_font_path: EventWriter<SetFontPath>) {
69 let mut font_path = None;
70 if input.key_released("PageDown") {
71 font.current_font = (font.current_font + 1) % FONTS.len();
72 font_path = Some(FONTS[font.current_font]);
73 } else if input.key_released("PageUp") {
74 font.current_font = (font.current_font + FONTS.len() - 1) % FONTS.len();
75 font_path = Some(FONTS[font.current_font]);
76 }
77
78 if let Some(font_path) = font_path {
79 font.current_font_name = font_path;
80 set_font_path.send(SetFontPath(String::from(font_path).into()));
81 }
82}
More examples
31fn update(input: Res<Input>, mut text_input: ResMut<TextInput>) {
32 // input.text returns the characters typed by the player since last update
33 let text = input.text();
34 if !text.is_empty() {
35 text_input.text.push_str(text);
36 }
37 // handle backspace
38 if input.key_released("Backspace") && !text_input.text.is_empty() {
39 // convoluted way to remove the last character of the string
40 // in a way that also works with utf-8 graphemes
41 // where one character != one byte
42 let mut graphemes = text_input.text.graphemes(true).rev();
43 graphemes.next();
44 text_input.text = graphemes.rev().collect();
45 }
46 // handle tab
47 if input.key_released("Tab") {
48 text_input.text.push_str(" ");
49 }
50 text_input.cursor += 1;
51}
Sourcepub fn keys_released(&self) -> Keys<'_> ⓘ
pub fn keys_released(&self) -> Keys<'_> ⓘ
Returns an iterator over all the keys that were released since the last update in no particular order.
Sourcepub fn text(&self) -> &str
pub fn text(&self) -> &str
Characters typed since last update.
Examples found in repository?
31fn update(input: Res<Input>, mut text_input: ResMut<TextInput>) {
32 // input.text returns the characters typed by the player since last update
33 let text = input.text();
34 if !text.is_empty() {
35 text_input.text.push_str(text);
36 }
37 // handle backspace
38 if input.key_released("Backspace") && !text_input.text.is_empty() {
39 // convoluted way to remove the last character of the string
40 // in a way that also works with utf-8 graphemes
41 // where one character != one byte
42 let mut graphemes = text_input.text.graphemes(true).rev();
43 graphemes.next();
44 text_input.text = graphemes.rev().collect();
45 }
46 // handle tab
47 if input.key_released("Tab") {
48 text_input.text.push_str(" ");
49 }
50 text_input.cursor += 1;
51}
Returns the current status of the given mouse button (true if currently pressed).
Returns true if the given mouse button was pressed since the last update.
Returns true if the given mouse button was released since the last update.
Sourcepub fn mouse_pos(&self) -> (f32, f32)
pub fn mouse_pos(&self) -> (f32, f32)
Returns the current mouse position in console cells coordinates. The decimal part of the value indicates sub-cell location.
Examples found in repository?
More examples
100fn input(
101 input: Res<Input>,
102 entities: Res<Entities>,
103 mut player_query: Query<(&mut Position<i32>, &Player)>,
104 mut mouse_query: Query<(&mut Position<f32>, &Mouse)>,
105 mut screenshot_index: ResMut<ScreenshotIndex>,
106 mut capture_events: EventWriter<Capture>,
107) {
108 let (mut player_position, _) = player_query.get_mut(entities.player).unwrap();
109
110 if input.key("ArrowLeft") {
111 player_position.x = (player_position.x - 1).max(1);
112 } else if input.key("ArrowRight") {
113 player_position.x = (player_position.x + 1).min(CONSOLE_WIDTH as i32 - 2);
114 }
115 if input.key("ArrowUp") {
116 player_position.y = (player_position.y - 1).max(1);
117 } else if input.key("ArrowDown") {
118 player_position.y = (player_position.y + 1).min(CONSOLE_HEIGHT as i32 - 2);
119 }
120
121 let (mut mouse_position, _) = mouse_query.get_mut(entities.mouse).unwrap();
122
123 let new_mouse_position = input.mouse_pos();
124 mouse_position.x = new_mouse_position.0;
125 mouse_position.y = new_mouse_position.1;
126
127 if input.key("ControlLeft") && input.key_pressed("KeyS") {
128 screenshot_index.0 += 1;
129 capture_events.send(Capture::new(format!(
130 "screenshot_{:03}.png",
131 screenshot_index.0
132 )));
133 }
134}
Sourcepub fn close_requested(&self) -> bool
pub fn close_requested(&self) -> bool
Whether the window close button has been activated.
Examples found in repository?
28fn process_input(
29 input: Res<Input>,
30 mut close_requested: ResMut<CloseRequested>,
31 mut app_exit: EventWriter<AppExit>,
32) {
33 if close_requested.0 {
34 if input.key("KeyY") {
35 app_exit.send(AppExit::Success);
36 } else if input.key("KeyN") {
37 close_requested.0 = false;
38 }
39 } else if input.key("Escape") || input.close_requested() {
40 close_requested.0 = true;
41 }
42}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Input
impl RefUnwindSafe for Input
impl Send for Input
impl Sync for Input
impl Unpin for Input
impl UnwindSafe for Input
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.