Skip to main content

Events

Struct Events 

Source
pub struct Events {
Show 13 fields pub keys: [ButtonState; 256], pub mouse_buttons: [ButtonState; 8], pub mouse_scroll_line: Option<(f32, f32)>, pub mouse_scroll_pixel: Option<(f64, f64)>, pub modifiers: ModifiersState, pub gamepad_connected: [bool; 4], pub gamepad_buttons: [[ButtonState; 20]; 4], pub gamepad_axes: [[f32; 6]; 4], pub resize_event: Option<Size2D>, pub close_requested: bool, pub focused: bool, pub frame: u64, pub network: NetworkEvents,
}
Expand description

Per-frame input state snapshot.

Events holds all input state for a single frame. It is populated by calling process_window_event and process_gilrs_event as events arrive, then frozen for the frame. At the end of the frame, end_frame advances the frame counter and clears transient state.

§Keyboard

use optic_window::*;

if events.key(KeyCode::Space, Is::Pressed) {
    // space was just pressed this frame
}
if events.key(KeyCode::ControlLeft, Is::Held) {
    // ctrl is held down
}

§Mouse

use optic_window::*;

if events.mouse(Mouse::Left, Is::Pressed) {
    // left click this frame
}

§Gamepad

use optic_window::*;

if events.gamepad_button(0, GamepadButton::A, Is::Pressed) {
    // gamepad 0 pressed A this frame
}
let axis = events.gamepad_axis(0, GamepadAxis::LeftX);

Fields§

§keys: [ButtonState; 256]§mouse_buttons: [ButtonState; 8]§mouse_scroll_line: Option<(f32, f32)>§mouse_scroll_pixel: Option<(f64, f64)>§modifiers: ModifiersState§gamepad_connected: [bool; 4]§gamepad_buttons: [[ButtonState; 20]; 4]§gamepad_axes: [[f32; 6]; 4]§resize_event: Option<Size2D>§close_requested: bool§focused: bool§frame: u64§network: NetworkEvents

Implementations§

Source§

impl Events

Source

pub const GAMEPAD_AXIS_DEADZONE: f32 = 0.15

Default deadzone for gamepad analog axes.

Source

pub fn new() -> Self

Create a new, empty event state (frame = 1, focused = true).

Source

pub fn clear(&mut self)

Reset all state to defaults. Does not advance the frame counter.

Source

pub fn process_window_event(&mut self, event: &WindowEvent, _window: &Window)

Process a single winit WindowEvent, updating internal state.

Called by the game loop for each event in the winit event queue.

Source

pub fn process_gilrs_event(&mut self, event: &Event)

Process a single gilrs gamepad event.

Source

pub fn end_frame(&mut self)

Advance the frame counter and clear per-frame transient state.

Must be called at the end of every frame. Resets scroll deltas, resize events, and network events. The close_requested flag persists across frames (the user must manually clear it).

Source

pub fn key(&self, kc: KeyCode, action: Is) -> bool

Query a single key by KeyCode and Is action.

Source

pub fn key_combo(&self, primary: KeyCode, modifier: KeyCode, action: Is) -> bool

Query a key combo: primary must match action while modifier is held.

use optic_window::*;

if events.key_combo(KeyCode::KeyC, KeyCode::ControlLeft, Is::Pressed) {
    // Ctrl+C was pressed
}
Source

pub fn key_combo_n(&self, keys: &[(KeyCode, Is)]) -> bool

Query multiple keys simultaneously — all must match their respective actions.

Source

pub fn any_key(&self, action: Is) -> bool

True if any key matches the given action.

Source

pub fn mouse(&self, m: Mouse, action: Is) -> bool

Query a mouse button by Mouse and Is action.

Source

pub fn any_mouse(&self, action: Is) -> bool

True if any mouse button matches the given action.

Source

pub fn gamepad_connected(&self, id: usize) -> bool

True if a gamepad with the given id is currently connected.

Source

pub fn gamepad_count(&self) -> usize

Number of currently connected gamepads.

Source

pub fn gamepad_button( &self, id: usize, button: GamepadButton, action: Is, ) -> bool

Query a gamepad button by GamepadButton and Is action.

Source

pub fn any_gamepad_button(&self, id: usize, action: Is) -> bool

True if any button on the given gamepad matches the action.

Source

pub fn any_gamepad(&self, action: Is) -> bool

True if any button on any connected gamepad matches the action.

Source

pub fn gamepad_axis_raw(&self, id: usize, axis: GamepadAxis) -> f32

Raw gamepad axis value (no deadzone applied).

Source

pub fn gamepad_axis(&self, id: usize, axis: GamepadAxis) -> f32

Gamepad axis value with the default deadzone ([GAMEPAD_AXIS_DEADZONE]).

Values below the deadzone are snapped to 0.0.

Source

pub fn gamepad_axis_deadzoned( &self, id: usize, axis: GamepadAxis, deadzone: f32, ) -> f32

Gamepad axis value with a custom deadzone.

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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 + Send + Sync>

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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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