Struct bevy_doryen::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)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn process_input(
    input: Res<Input>,
    mut close_requested: ResMut<CloseRequested>,
    mut app_exit: EventWriter<AppExit>,
) {
    if close_requested.0 {
        if input.key("KeyY") {
            app_exit.send(AppExit);
        } else if input.key("KeyN") {
            close_requested.0 = false;
        }
    } else if input.key("Escape") || input.close_requested() {
        close_requested.0 = true;
    }
}
More examples
Hide additional examples
examples/demo/player.rs (line 58)
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
fn move_player(
    input: Res<Input>,
    mut query: Query<(&mut Position, &Speed), With<Player>>,
    walls: Res<Walls>,
) {
    let mut movement = (0, 0);
    if input.key("ArrowLeft") || input.key("KeyA") {
        movement.0 = -2;
    } else if input.key("ArrowRight") || input.key("KeyD") {
        movement.0 = 2;
    }
    if input.key("ArrowUp") || input.key("KeyW") {
        movement.1 = -2;
    } else if input.key("ArrowDown") || input.key("KeyS") {
        movement.1 = 2;
    }
    if movement == (0, 0) {
        return;
    }

    let mut coef = DEFAULT_COEFFICIENT;
    let (mut player_position, player_speed) = query.single_mut();

    let next_x_pos = player_position.next_pos((movement.0 as f32, 0.));

    if walls.is_wall(next_x_pos) {
        movement.0 = 0;
        coef = 1.0;
    }
    let next_y_pos = player_position.next_pos((0., movement.1 as f32));

    if walls.is_wall(next_y_pos) {
        movement.1 = 0;
        coef = 1.0;
    }
    player_position.move_by(movement, *player_speed, coef);
}
examples/basic.rs (line 110)
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
fn input(
    input: Res<Input>,
    entities: Res<Entities>,
    mut player_query: Query<(&mut Position<i32>, &Player)>,
    mut mouse_query: Query<(&mut Position<f32>, &Mouse)>,
    mut screenshot_index: ResMut<ScreenshotIndex>,
    mut capture_events: EventWriter<Capture>,
) {
    let (mut player_position, _) = player_query.get_mut(entities.player).unwrap();

    if input.key("ArrowLeft") {
        player_position.x = (player_position.x - 1).max(1);
    } else if input.key("ArrowRight") {
        player_position.x = (player_position.x + 1).min(CONSOLE_WIDTH as i32 - 2);
    }
    if input.key("ArrowUp") {
        player_position.y = (player_position.y - 1).max(1);
    } else if input.key("ArrowDown") {
        player_position.y = (player_position.y + 1).min(CONSOLE_HEIGHT as i32 - 2);
    }

    let (mut mouse_position, _) = mouse_query.get_mut(entities.mouse).unwrap();

    let new_mouse_position = input.mouse_pos();
    mouse_position.x = new_mouse_position.0;
    mouse_position.y = new_mouse_position.1;

    if input.key("ControlLeft") && input.key_pressed("KeyS") {
        screenshot_index.0 += 1;
        capture_events.send(Capture::new(format!(
            "screenshot_{:03}.png",
            screenshot_index.0
        )));
    }
}
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)
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
fn input(
    input: Res<Input>,
    entities: Res<Entities>,
    mut player_query: Query<(&mut Position<i32>, &Player)>,
    mut mouse_query: Query<(&mut Position<f32>, &Mouse)>,
    mut screenshot_index: ResMut<ScreenshotIndex>,
    mut capture_events: EventWriter<Capture>,
) {
    let (mut player_position, _) = player_query.get_mut(entities.player).unwrap();

    if input.key("ArrowLeft") {
        player_position.x = (player_position.x - 1).max(1);
    } else if input.key("ArrowRight") {
        player_position.x = (player_position.x + 1).min(CONSOLE_WIDTH as i32 - 2);
    }
    if input.key("ArrowUp") {
        player_position.y = (player_position.y - 1).max(1);
    } else if input.key("ArrowDown") {
        player_position.y = (player_position.y + 1).min(CONSOLE_HEIGHT as i32 - 2);
    }

    let (mut mouse_position, _) = mouse_query.get_mut(entities.mouse).unwrap();

    let new_mouse_position = input.mouse_pos();
    mouse_position.x = new_mouse_position.0;
    mouse_position.y = new_mouse_position.1;

    if input.key("ControlLeft") && input.key_pressed("KeyS") {
        screenshot_index.0 += 1;
        capture_events.send(Capture::new(format!(
            "screenshot_{:03}.png",
            screenshot_index.0
        )));
    }
}
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)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
fn update(mut font: ResMut<Font>, input: Res<Input>, mut set_font_path: EventWriter<SetFontPath>) {
    let mut font_path = None;
    if input.key_released("PageDown") {
        font.current_font = (font.current_font + 1) % FONTS.len();
        font_path = Some(FONTS[font.current_font]);
    } else if input.key_released("PageUp") {
        font.current_font = (font.current_font + FONTS.len() - 1) % FONTS.len();
        font_path = Some(FONTS[font.current_font]);
    }

    if let Some(font_path) = font_path {
        font.current_font_name = font_path;
        set_font_path.send(SetFontPath(String::from(font_path).into()));
    }
}
More examples
Hide additional examples
examples/text_input.rs (line 38)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn update(input: Res<Input>, mut text_input: ResMut<TextInput>) {
    // input.text returns the characters typed by the player since last update
    let text = input.text();
    if !text.is_empty() {
        text_input.text.push_str(text);
    }
    // handle backspace
    if input.key_released("Backspace") && !text_input.text.is_empty() {
        // convoluted way to remove the last character of the string
        // in a way that also works with utf-8 graphemes
        // where one character != one byte
        let mut graphemes = text_input.text.graphemes(true).rev();
        graphemes.next();
        text_input.text = graphemes.rev().collect();
    }
    // handle tab
    if input.key_released("Tab") {
        text_input.text.push_str("   ");
    }
    text_input.cursor += 1;
}
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)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fn update(input: Res<Input>, mut text_input: ResMut<TextInput>) {
    // input.text returns the characters typed by the player since last update
    let text = input.text();
    if !text.is_empty() {
        text_input.text.push_str(text);
    }
    // handle backspace
    if input.key_released("Backspace") && !text_input.text.is_empty() {
        // convoluted way to remove the last character of the string
        // in a way that also works with utf-8 graphemes
        // where one character != one byte
        let mut graphemes = text_input.text.graphemes(true).rev();
        graphemes.next();
        text_input.text = graphemes.rev().collect();
    }
    // handle tab
    if input.key_released("Tab") {
        text_input.text.push_str("   ");
    }
    text_input.cursor += 1;
}
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)
37
38
39
fn update_mouse_position(mut resize_data: ResMut<ResizeData>, input: Res<Input>) {
    resize_data.mouse_pos = input.mouse_pos();
}
More examples
Hide additional examples
examples/basic.rs (line 123)
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
fn input(
    input: Res<Input>,
    entities: Res<Entities>,
    mut player_query: Query<(&mut Position<i32>, &Player)>,
    mut mouse_query: Query<(&mut Position<f32>, &Mouse)>,
    mut screenshot_index: ResMut<ScreenshotIndex>,
    mut capture_events: EventWriter<Capture>,
) {
    let (mut player_position, _) = player_query.get_mut(entities.player).unwrap();

    if input.key("ArrowLeft") {
        player_position.x = (player_position.x - 1).max(1);
    } else if input.key("ArrowRight") {
        player_position.x = (player_position.x + 1).min(CONSOLE_WIDTH as i32 - 2);
    }
    if input.key("ArrowUp") {
        player_position.y = (player_position.y - 1).max(1);
    } else if input.key("ArrowDown") {
        player_position.y = (player_position.y + 1).min(CONSOLE_HEIGHT as i32 - 2);
    }

    let (mut mouse_position, _) = mouse_query.get_mut(entities.mouse).unwrap();

    let new_mouse_position = input.mouse_pos();
    mouse_position.x = new_mouse_position.0;
    mouse_position.y = new_mouse_position.1;

    if input.key("ControlLeft") && input.key_pressed("KeyS") {
        screenshot_index.0 += 1;
        capture_events.send(Capture::new(format!(
            "screenshot_{:03}.png",
            screenshot_index.0
        )));
    }
}
source

pub fn close_requested(&self) -> bool

Whether the window close button has been activated.

Examples found in repository?
examples/exit.rs (line 39)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn process_input(
    input: Res<Input>,
    mut close_requested: ResMut<CloseRequested>,
    mut app_exit: EventWriter<AppExit>,
) {
    if close_requested.0 {
        if input.key("KeyY") {
            app_exit.send(AppExit);
        } else if input.key("KeyN") {
            close_requested.0 = false;
        }
    } else if input.key("Escape") || input.close_requested() {
        close_requested.0 = true;
    }
}

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

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

§

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.
§

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.
§

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.
§

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.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

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.
§

impl<T> Instrument for T

§

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

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

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

§

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

§

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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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