flatland-client-ui 0.2.118

Engine-agnostic play client UI state (world grid, input, presentation)
Documentation
//! Engine-neutral keyboard and pointer events.

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UiKeyEventKind {
    Press,
    Release,
    Repeat,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct UiKeyModifiers {
    pub shift: bool,
    pub control: bool,
    pub alt: bool,
}

impl UiKeyModifiers {
    pub fn contains_shift(self) -> bool {
        self.shift
    }

    pub fn contains_control(self) -> bool {
        self.control
    }

    pub fn contains_alt(self) -> bool {
        self.alt
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UiKeyCode {
    Char(char),
    Up,
    Down,
    Left,
    Right,
    Esc,
    Enter,
    Tab,
    BackTab,
    PageUp,
    PageDown,
    Delete,
    Backspace,
    Space,
    ShiftLeft,
    ShiftRight,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UiKeyEvent {
    pub kind: UiKeyEventKind,
    pub code: UiKeyCode,
    pub modifiers: UiKeyModifiers,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UiMouseButton {
    Left,
    Right,
    Middle,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum UiPointerEvent {
    Move {
        x: f32,
        y: f32,
    },
    Click {
        x: f32,
        y: f32,
        button: UiMouseButton,
        /// True when Shift was held at click time (T2 combat targeting).
        shift: bool,
    },
    Scroll {
        x: f32,
        y: f32,
        delta: f32,
    },
}

/// Number-row digit from a typed char (US QWERTY).
///
/// Shift+digit may arrive as `'1'` (macOS / physical `Key1`) or the shifted
/// glyph `'!'` (Linux X11/Wayland `get_char_pressed`). Use this whenever a
/// shortcut is bound to `1`–`9` / `0` rather than the punctuation itself.
pub fn digit_from_typed_char(c: char) -> Option<u8> {
    Some(match c {
        '0' | ')' => 0,
        '1' | '!' => 1,
        '2' | '@' => 2,
        '3' | '#' => 3,
        '4' | '$' => 4,
        '5' | '%' => 5,
        '6' | '^' => 6,
        '7' | '&' => 7,
        '8' | '*' => 8,
        '9' | '(' => 9,
        _ => return None,
    })
}

/// Shifted US QWERTY glyph for a digit char (`'1'` → `'!'`).
pub fn us_qwerty_shifted_digit(c: char) -> Option<char> {
    Some(match c {
        '0' => ')',
        '1' => '!',
        '2' => '@',
        '3' => '#',
        '4' => '$',
        '5' => '%',
        '6' => '^',
        '7' => '&',
        '8' => '*',
        '9' => '(',
        _ => return None,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn digit_from_typed_char_accepts_shift_glyphs() {
        assert_eq!(digit_from_typed_char('1'), Some(1));
        assert_eq!(digit_from_typed_char('!'), Some(1));
        assert_eq!(digit_from_typed_char('5'), Some(5));
        assert_eq!(digit_from_typed_char('%'), Some(5));
        assert_eq!(digit_from_typed_char('0'), Some(0));
        assert_eq!(digit_from_typed_char(')'), Some(0));
        assert_eq!(digit_from_typed_char('a'), None);
        assert_eq!(digit_from_typed_char('A'), None);
    }
}