Struct Input

Source
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

Source

pub fn key(&self, key: &str) -> bool

Returns the current status of the given key (true if currently pressed).

Examples found in repository?
examples/exit.rs (line 34)
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
Hide additional examples
examples/demo/player.rs (line 59)
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}
examples/basic.rs (line 110)
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}
Source

pub fn key_pressed(&self, key: &str) -> bool

Returns true if the given key was pressed since the last update.

Examples found in repository?
examples/basic.rs (line 127)
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}
Source

pub fn keys_pressed(&self) -> Keys<'_>

Returns an iterator over all the keys that were pressed since the last update in no particular order.

Source

pub fn key_released(&self, key: &str) -> bool

Returns true if the given key was released since the last update.

Examples found in repository?
examples/fonts.rs (line 70)
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
Hide additional examples
examples/text_input.rs (line 38)
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}
Source

pub fn keys_released(&self) -> Keys<'_>

Returns an iterator over all the keys that were released since the last update in no particular order.

Source

pub fn text(&self) -> &str

Characters typed since last update.

Examples found in repository?
examples/text_input.rs (line 33)
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}
Source

pub fn mouse_button(&self, mouse_button: MouseButton) -> bool

Returns the current status of the given mouse button (true if currently pressed).

Source

pub fn mouse_button_pressed(&self, mouse_button: MouseButton) -> bool

Returns true if the given mouse button was pressed since the last update.

Source

pub fn mouse_button_released(&self, mouse_button: MouseButton) -> bool

Returns true if the given mouse button was released since the last update.

Source

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?
examples/resize.rs (line 38)
37fn update_mouse_position(mut resize_data: ResMut<ResizeData>, input: Res<Input>) {
38    resize_data.mouse_pos = input.mouse_pos();
39}
More examples
Hide additional examples
examples/basic.rs (line 123)
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}
Source

pub fn close_requested(&self) -> bool

Whether the window close button has been activated.

Examples found in repository?
examples/exit.rs (line 39)
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§

Source§

impl Debug for Input

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Input

Source§

fn default() -> Input

Returns the “default value” for a type. Read more
Source§

impl Resource for Input
where Self: Send + Sync + 'static,

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given World.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,