jugar-input

Unified input handling for touch, mouse, keyboard, and gamepad.

Keyboard

#![allow(unused)]
fn main() {
use jugar_input::prelude::*;

let input = InputState::new();

// Key states
if input.key_pressed(KeyCode::Space) {
    // Just pressed this frame
}

if input.key_held(KeyCode::W) {
    // Currently held down
}

if input.key_released(KeyCode::Escape) {
    // Just released this frame
}
}

Mouse

#![allow(unused)]
fn main() {
// Position
let pos = input.mouse_position();

// Buttons
if input.mouse_pressed(MouseButton::Left) {
    spawn_projectile(pos);
}

if input.mouse_held(MouseButton::Right) {
    aim_at(pos);
}

// Scroll wheel
let scroll = input.scroll_delta();
camera.zoom += scroll.y * 0.1;
}

Touch

#![allow(unused)]
fn main() {
// All active touches
for touch in input.touches() {
    match touch.phase {
        TouchPhase::Started => {
            // New touch
        }
        TouchPhase::Moved => {
            // Touch moved
            let delta = touch.position - touch.previous_position;
        }
        TouchPhase::Ended | TouchPhase::Cancelled => {
            // Touch ended
        }
    }
}

// Gestures
if let Some(pinch) = input.pinch_gesture() {
    camera.zoom *= pinch.scale;
}

if let Some(pan) = input.pan_gesture() {
    camera.position -= pan.delta;
}
}

Gamepad

#![allow(unused)]
fn main() {
// Check if gamepad connected
if let Some(gamepad) = input.gamepad(0) {
    // Buttons
    if gamepad.button_pressed(GamepadButton::South) {
        player.jump();
    }

    // Analog sticks
    let left_stick = gamepad.left_stick();
    player.velocity.x = left_stick.x * max_speed;

    let right_stick = gamepad.right_stick();
    player.aim_direction = right_stick.normalize();

    // Triggers
    let fire_intensity = gamepad.right_trigger();
    if fire_intensity > 0.5 {
        player.fire();
    }
}
}

Virtual Joystick

For touch devices:

#![allow(unused)]
fn main() {
let mut joystick = VirtualJoystick::new()
    .position(100.0, 400.0)
    .radius(60.0)
    .dead_zone(0.1);

// Update with touch input
joystick.update(&input);

// Get direction
let direction = joystick.direction();
player.velocity = direction * max_speed;
}

Input Abstraction

Unify different input methods:

#![allow(unused)]
fn main() {
// Abstract action
let move_input = input.get_axis_2d(
    // Keyboard
    KeyCode::W, KeyCode::S, KeyCode::A, KeyCode::D,
    // Gamepad
    GamepadAxis::LeftStickX, GamepadAxis::LeftStickY,
);

// Returns normalized Vec2 from any input source
player.velocity = move_input * max_speed;
}

Key Codes

Common key codes:

CategoryKeys
LettersA - Z
NumbersKey0 - Key9
ArrowsArrowUp, ArrowDown, ArrowLeft, ArrowRight
SpecialSpace, Enter, Escape, Tab, Backspace
ModifiersShiftLeft, ControlLeft, AltLeft
FunctionF1 - F12