1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 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
use crate::WindowId;

// Represents an input event
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PixEvent {
    None,
    Quit,
    AppTerminating,
    GamepadBtn(u32, Button, bool),     // Id, Button, pressed
    GamepadAxis(u32, Axis, i16),       // Id, Axis, value
    KeyPress(Key, bool, bool),         // Key, pressed, repeat
    MousePress(Mouse, i32, i32, bool), // Mouse, x, y, pressed
    MouseWheel(i32),                   // Wheel delta
    MouseMotion(i32, i32),             // x, y
    WinClose(WindowId),
    Resized,
    Focus(u32, bool), // Window ID, focused
    Background(bool),
}

/// Represents a user key/button input
#[derive(Debug, Copy, Clone)]
pub struct Input {
    pub pressed: bool,  // Set once during the frame in which it occurs
    pub released: bool, // Set once during the frame in which it occurs
    pub held: bool,     // Set for all frames between pressed and released
}

impl Input {
    pub(super) fn new() -> Self {
        Self {
            pressed: false,
            released: false,
            held: false,
        }
    }
}

/// Represents a mouse button
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Mouse {
    Left,
    Middle,
    Right,
    X1,
    X2,
    Unknown,
}

/// A non-exhaustive list of useful keys to detect
#[rustfmt::skip]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Key {
    A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
    Num0, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9,
    Kp0, Kp1, Kp2, Kp3, Kp4, Kp5, Kp6, Kp7, Kp8, Kp9,
    F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
    Left, Up, Down, Right,
    Tab, Insert, Delete, Home, End, PageUp, PageDown,
    Escape, Backspace, Return, KpEnter, Pause, ScrollLock,
    Plus, Minus, Period, Underscore, Equals,
    KpMultiply, KpDivide, KpPlus, KpMinus, KpPeriod,
    Backquote, Exclaim, At, Hash, Dollar, Percent,
    Caret, Ampersand, Asterisk, LeftParen, RightParen,
    LeftBracket, RightBracket, Backslash,
    CapsLock, Semicolon, Colon, Quotedbl, Quote,
    Less, Comma, Greater, Question, Slash,
    LShift, RShift, Space, Ctrl, Alt, Meta,
    Unknown,
}

/// Controller buttons
#[rustfmt::skip]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Button {
    A, B, X, Y, Back, Start, Guide, DPadUp, DPadDown, DPadLeft, DPadRight,
    LeftStick, RightStick, LeftShoulder, RightShoulder,
}

#[rustfmt::skip]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Axis {
    LeftX, RightX, LeftY, RightY, TriggerLeft, TriggerRight,
}

impl Default for PixEvent {
    fn default() -> Self {
        Self::None
    }
}

impl Default for Mouse {
    fn default() -> Self {
        Self::Left
    }
}

impl Default for Key {
    fn default() -> Self {
        Self::A
    }
}

impl Default for Button {
    fn default() -> Self {
        Self::A
    }
}

impl Default for Axis {
    fn default() -> Self {
        Self::LeftX
    }
}

impl Default for Input {
    fn default() -> Self {
        Self::new()
    }
}