#[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,
shift: bool,
},
Scroll {
x: f32,
y: f32,
delta: f32,
},
}
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,
})
}
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);
}
}