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
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?
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
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);
}
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
)));
}
}
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?
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
)));
}
}
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?
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
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;
}
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?
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;
}
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
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
)));
}
}
sourcepub fn close_requested(&self) -> bool
pub fn close_requested(&self) -> bool
Whether the window close button has been activated.
Examples found in repository?
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§
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> 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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§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
.§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
.§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.§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.§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
.