Struct blue_engine::imports::InputHelper
source · pub struct InputHelper { /* private fields */ }Expand description
Input helper The main struct of the API.
Create with WinitInputHelper::new.
Call WinitInputHelper::update for every winit::event::Event you receive from winit.
WinitInputHelper::update returning true indicates a step has occured.
You should now run your application logic, calling any of the accessor methods you need.
An alternative API is provided via WinitInputHelper::step_with_window_events,
call this method instead of WinitInputHelper::update if you need to manually control when a new step begins.
A step occurs every time this method is called.
Do not mix usages of WinitInputHelper::update and WinitInputHelper::step_with_window_events.
You should stick to one or the other.
Implementations§
source§impl WinitInputHelper
impl WinitInputHelper
pub fn new() -> WinitInputHelper
sourcepub fn update<T>(&mut self, event: &Event<T>) -> bool
pub fn update<T>(&mut self, event: &Event<T>) -> bool
Pass every winit event to this function and run your application logic when it returns true.
The following winit events are handled:
Event::NewEventsclears all internal state.Event::MainEventsClearedcauses this function to return true, signifying a “step” has completed.Event::WindowEventupdates internal state, this will affect the result of accessor methods immediately.Event::DeviceEventupdates value ofmouse_diff()
sourcepub fn step_with_window_events(&mut self, events: &[WindowEvent])
pub fn step_with_window_events(&mut self, events: &[WindowEvent])
Pass a slice containing every winit event that occured within the step to this function.
Ensure this method is only called once per application main loop.
Ensure every event since the last WinitInputHelper::step_with_window_events call is included in the events argument.
WinitInputHelper::Update is easier to use.
But this method is useful when your application logic steps dont line up with winit’s event loop.
e.g. you have a seperate thread for application logic using WinitInputHelper that constantly
runs regardless of winit’s event loop and you need to send events to it directly.
sourcepub fn key_pressed(&self, keycode: KeyCode) -> bool
pub fn key_pressed(&self, keycode: KeyCode) -> bool
Returns true when the key with the specified keycode goes from “not pressed” to “pressed”. Otherwise returns false.
Uses physical keys in the US layout, so for example the W key will be in the same physical key on both US and french keyboards.
This is suitable for game controls.
sourcepub fn key_pressed_os(&self, keycode: KeyCode) -> bool
pub fn key_pressed_os(&self, keycode: KeyCode) -> bool
Returns true when the key with the specified keycode goes from “not pressed” to “pressed”. Otherwise returns false.
Uses physical keys in the US layout, so for example the W key will be in the same physical key on both US and french keyboards.
Will repeat key presses while held down according to the OS’s key repeat configuration This is suitable for UI.
sourcepub fn key_released(&self, keycode: KeyCode) -> bool
pub fn key_released(&self, keycode: KeyCode) -> bool
Returns true when the key with the specified KeyCode goes from “pressed” to “not pressed”. Otherwise returns false.
Uses physical keys in the US layout, so for example the W key will be in the same physical key on both US and french keyboards.
sourcepub fn key_held(&self, keycode: KeyCode) -> bool
pub fn key_held(&self, keycode: KeyCode) -> bool
Returns true when the key with the specified keycode remains “pressed”. Otherwise returns false.
Uses physical keys in the US layout, so for example the W key will be in the same physical key on both US and french keyboards.
Examples found in repository?
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 89 90 91 92 93 94 95 96 97 98 99 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
fn main() {
let mut engine = Engine::new().expect("win");
//let test_instance = Instance::default();
//println!("{:?}", test_instance.to_raw());
let texture = engine
.renderer
.build_texture(
"background",
TextureData::Path("resources/BlueLogoDiscord.png".to_string()),
blue_engine::TextureMode::Clamp,
)
.unwrap();
let texture2 = engine
.renderer
.build_texture(
"background",
TextureData::Path("resources/player.png".to_string()),
blue_engine::TextureMode::Clamp,
)
.unwrap();
let texture3 = engine
.renderer
.build_texture(
"background",
TextureData::Path("resources/image.png".to_string()),
blue_engine::TextureMode::Clamp,
)
.unwrap();
square(
"main",
ObjectSettings::default(),
&mut engine.renderer,
&mut engine.objects,
);
engine.objects.get_mut("main").unwrap().set_texture(texture);
engine
.objects
.get_mut("main")
.unwrap()
.set_position(-1f32, 0f32, 0f32);
square(
"alt",
ObjectSettings::default(),
&mut engine.renderer,
&mut engine.objects,
);
engine.objects.get_mut("alt").unwrap().set_texture(texture2);
engine
.objects
.get_mut("alt")
.unwrap()
.set_position(0.2f32, 0f32, 0.001f32);
square(
"alt2",
ObjectSettings::default(),
&mut engine.renderer,
&mut engine.objects,
);
engine
.objects
.get_mut("alt2")
.unwrap()
.set_texture(texture3);
engine
.objects
.get_mut("alt2")
.unwrap()
.set_position(-0.2f32, 0f32, 0.001f32);
let speed = -0.05;
let mut last_time = std::time::Instant::now();
let mut frames = 0;
engine
.update_loop(move |renderer, _window, objects, input, camera, plugins| {
// calculate FPS
let current_time = std::time::Instant::now();
frames += 1;
if current_time - last_time >= std::time::Duration::from_secs(1) {
println!("{}ms/frame", 1000f32 / frames as f32);
frames = 0;
last_time = current_time;
}
let sprite = objects.get_mut("alt").unwrap();
if input.key_held(blue_engine::KeyCode::ArrowUp) {
sprite.set_position(
sprite.position.x,
sprite.position.y - speed,
sprite.position.z,
);
//lm.ambient_color.data = [1f32, 1f32, 1f32, 1f32];
}
if input.key_held(blue_engine::KeyCode::ArrowDown) {
sprite.set_position(
sprite.position.x,
sprite.position.y + speed,
sprite.position.z,
);
//lm.ambient_color.data = [0.1f32, 0.1f32, 0.1f32, 1f32];
}
if input.key_held(blue_engine::KeyCode::ArrowLeft) {
sprite.set_position(
sprite.position.x + speed,
sprite.position.y,
sprite.position.z,
);
}
if input.key_held(blue_engine::KeyCode::ArrowRight) {
sprite.set_position(
sprite.position.x - speed,
sprite.position.y,
sprite.position.z,
);
}
if input.key_held(blue_engine::KeyCode::KeyE) {
sprite.set_position(
sprite.position.x,
sprite.position.y,
sprite.position.z + speed,
);
}
if input.key_held(blue_engine::KeyCode::KeyQ) {
sprite.set_position(
sprite.position.x,
sprite.position.y,
sprite.position.z - speed,
);
}
})
.expect("Error during update loop");
}sourcepub fn held_shift(&self) -> bool
pub fn held_shift(&self) -> bool
Returns true while any shift key is held on the keyboard. Otherwise returns false.
Uses physical keys.
sourcepub fn held_control(&self) -> bool
pub fn held_control(&self) -> bool
Returns true while any control key is held on the keyboard. Otherwise returns false.
Uses physical keys.
sourcepub fn held_alt(&self) -> bool
pub fn held_alt(&self) -> bool
Returns true while any alt key is held on the keyboard. Otherwise returns false.
Uses physical keys.
sourcepub fn key_pressed_logical(&self, check_key: Key<&str>) -> bool
pub fn key_pressed_logical(&self, check_key: Key<&str>) -> bool
Returns true when the specified keyboard key goes from “not pressed” to “pressed”. Otherwise returns false.
Uses logical keypresses, so for example W is changed between a US and french keyboard.
Will never repeat keypresses while held.
sourcepub fn key_pressed_os_logical(&self, check_key: Key<&str>) -> bool
pub fn key_pressed_os_logical(&self, check_key: Key<&str>) -> bool
Returns true when the specified keyboard key goes from “not pressed” to “pressed”. Otherwise returns false.
Uses logical keypresses, so for example W is changed between a US and french keyboard.
Will repeat key presses while held down according to the OS’s key repeat configuration This is suitable for UI.
sourcepub fn key_released_logical(&self, check_key: Key<&str>) -> bool
pub fn key_released_logical(&self, check_key: Key<&str>) -> bool
Returns true when the specified keyboard key goes from “pressed” to “not pressed”. Otherwise returns false.
Uses logical keypresses, so for example W is changed between a US and french keyboard.
sourcepub fn key_held_logical(&self, check_key: Key<&str>) -> bool
pub fn key_held_logical(&self, check_key: Key<&str>) -> bool
Returns true while the specified keyboard key remains “pressed”. Otherwise returns false.
Uses logical keypresses, so for example W is changed between a US and french keyboard.
sourcepub fn mouse_pressed(&self, check_mouse_button: usize) -> bool
pub fn mouse_pressed(&self, check_mouse_button: usize) -> bool
Returns true when the specified mouse button goes from “not pressed” to “pressed”. Otherwise returns false.
Left => 0 Right => 1 Middle => 2 Other => 3..255
sourcepub fn mouse_released(&self, check_mouse_button: usize) -> bool
pub fn mouse_released(&self, check_mouse_button: usize) -> bool
Returns true when the specified mouse button goes from “pressed” to “not pressed”. Otherwise returns false.
Left => 0 Right => 1 Middle => 2 Other => 3..255
sourcepub fn mouse_held(&self, mouse_button: usize) -> bool
pub fn mouse_held(&self, mouse_button: usize) -> bool
Returns true while the specified mouse button remains “pressed”. Otherwise returns false.
Left => 0 Right => 1 Middle => 2 Other => 3..255
sourcepub fn scroll_diff(&self) -> (f32, f32)
pub fn scroll_diff(&self) -> (f32, f32)
Returns (0.0, 0.0) when the window is not focused.
Otherwise returns the amount scrolled by the mouse during the last step.
Returns (horizontally, vertically)
sourcepub fn cursor(&self) -> Option<(f32, f32)>
pub fn cursor(&self) -> Option<(f32, f32)>
Returns the cursor coordinates in pixels, when window is focused AND (cursor is on window OR any mouse button remains held while cursor moved off window)
Otherwise returns None
sourcepub fn cursor_diff(&self) -> (f32, f32)
pub fn cursor_diff(&self) -> (f32, f32)
Returns the change in cursor coordinates that occured during the last step, when window is focused AND (cursor is on window OR any mouse button remains held while cursor moved off window)
Otherwise returns (0.0, 0.0).
sourcepub fn mouse_diff(&self) -> (f32, f32)
pub fn mouse_diff(&self) -> (f32, f32)
Returns the change in mouse coordinates that occured during the last step.
This is useful when implementing first person controls with a captured mouse.
Because this uses DeviceEvents, the step_with_windows_events
function won’t update this as it is not a WindowEvent.
sourcepub fn text(&self) -> Vec<TextChar>
pub fn text(&self) -> Vec<TextChar>
Returns the characters pressed during the last step. The earlier the character was pressed, the lower the index in the Vec.
sourcepub fn dropped_file(&self) -> Option<PathBuf>
pub fn dropped_file(&self) -> Option<PathBuf>
Returns the path to a file that has been drag-and-dropped onto the window.
sourcepub fn window_resized(&self) -> Option<PhysicalSize<u32>>
pub fn window_resized(&self) -> Option<PhysicalSize<u32>>
Returns the current window size if it was resized during the last step.
Otherwise returns None.
sourcepub fn resolution(&self) -> Option<(u32, u32)>
pub fn resolution(&self) -> Option<(u32, u32)>
Returns None when no WindowEvent::Resized have been received yet.
After one has been received it returns the current resolution of the window.
sourcepub fn scale_factor_changed(&self) -> Option<f64>
pub fn scale_factor_changed(&self) -> Option<f64>
Returns the current scale factor if it was changed during the last step.
Otherwise returns None.
sourcepub fn scale_factor(&self) -> Option<f64>
pub fn scale_factor(&self) -> Option<f64>
Returns None when no WindowEvent::ScaleFactorChanged have been received yet.
After one has been received it returns the current scale_factor of the window.
sourcepub fn destroyed(&self) -> bool
pub fn destroyed(&self) -> bool
Returns true if the window has been destroyed Otherwise returns false. Once this method has returned true once all following calls to this method will also return true.
sourcepub fn close_requested(&self) -> bool
pub fn close_requested(&self) -> bool
Returns true if the OS has requested the application to close during this step. Otherwise returns false.
sourcepub fn quit(&self) -> bool
👎Deprecated: Instead use input.close_requested() || input.destroyed()
pub fn quit(&self) -> bool
input.close_requested() || input.destroyed()Deprecated
sourcepub fn delta_time(&self) -> Option<Duration>
pub fn delta_time(&self) -> Option<Duration>
Returns the std::time::Duration elapsed since the last step.
Returns None if the step is still in progress.
Trait Implementations§
source§impl Clone for WinitInputHelper
impl Clone for WinitInputHelper
source§fn clone(&self) -> WinitInputHelper
fn clone(&self) -> WinitInputHelper
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Default for WinitInputHelper
impl Default for WinitInputHelper
source§fn default() -> WinitInputHelper
fn default() -> WinitInputHelper
Auto Trait Implementations§
impl RefUnwindSafe for WinitInputHelper
impl Send for WinitInputHelper
impl Sync for WinitInputHelper
impl Unpin for WinitInputHelper
impl UnwindSafe for WinitInputHelper
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
§fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
§fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
§fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
§fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
§fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
§fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
§fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
§fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
§fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
§fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
§fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
§fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
§fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
§fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
§fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
§fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
§fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
§fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
§fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
§fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
§fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
§fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
§fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
§fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
§fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more§fn fg_rgb<const R: u8, const G: u8, const B: u8>(
&self
) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
§fn bg_rgb<const R: u8, const G: u8, const B: u8>(
&self
) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
§fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
§fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.