use crate::event::{Event, Ime, Key, NamedKey};
pub const ACTION_DOWN: i32 = 0;
pub const ACTION_UP: i32 = 1;
pub const ACTION_MOVE: i32 = 2;
pub const ACTION_CANCEL: i32 = 3;
pub fn translate_touch(action: i32, x: f32, y: f32) -> (usize, [Event; 2]) {
use crate::event::{ElementState, MouseButton};
let empty = [Event::CursorLeft, Event::CursorLeft];
let press_at = |x: f32, y: f32| Event::CursorMoved { x, y };
let press_button = Event::MouseInput {
state: ElementState::Pressed,
button: MouseButton::Left,
};
let release_button = Event::MouseInput {
state: ElementState::Released,
button: MouseButton::Left,
};
match action {
ACTION_DOWN => (2, [press_at(x, y), press_button]),
ACTION_MOVE => (1, [press_at(x, y), empty[1].clone()]),
ACTION_UP => (1, [release_button, empty[1].clone()]),
ACTION_CANCEL => (2, [Event::CursorLeft, release_button]),
_ => (0, empty),
}
}
const KEYCODE_DPAD_UP: i32 = 19;
const KEYCODE_DPAD_DOWN: i32 = 20;
const KEYCODE_DPAD_LEFT: i32 = 21;
const KEYCODE_DPAD_RIGHT: i32 = 22;
const KEYCODE_DEL: i32 = 67; const KEYCODE_ENTER: i32 = 66;
const KEYCODE_TAB: i32 = 61;
const KEYCODE_ESCAPE: i32 = 111;
const KEYCODE_MOVE_HOME: i32 = 122;
const KEYCODE_MOVE_END: i32 = 123;
const KEYCODE_FORWARD_DEL: i32 = 112; const KEYCODE_SPACE: i32 = 62;
pub fn translate_keycode(key_code: i32) -> Key {
match key_code {
KEYCODE_DEL => Key::Named(NamedKey::Backspace),
KEYCODE_ENTER => Key::Named(NamedKey::Enter),
KEYCODE_TAB => Key::Named(NamedKey::Tab),
KEYCODE_ESCAPE => Key::Named(NamedKey::Escape),
KEYCODE_DPAD_LEFT => Key::Named(NamedKey::ArrowLeft),
KEYCODE_DPAD_RIGHT => Key::Named(NamedKey::ArrowRight),
KEYCODE_DPAD_UP => Key::Named(NamedKey::ArrowUp),
KEYCODE_DPAD_DOWN => Key::Named(NamedKey::ArrowDown),
KEYCODE_MOVE_HOME => Key::Named(NamedKey::Home),
KEYCODE_MOVE_END => Key::Named(NamedKey::End),
KEYCODE_FORWARD_DEL => Key::Named(NamedKey::Delete),
KEYCODE_SPACE => Key::Named(NamedKey::Space),
_ => Key::Unidentified,
}
}
pub fn key_press_from_keycode(key_code: i32) -> Option<Event> {
use crate::event::{ElementState, KeyEvent as FKeyEvent};
let logical_key = translate_keycode(key_code);
if matches!(logical_key, Key::Unidentified) {
return None;
}
Some(Event::KeyboardInput {
event: FKeyEvent {
logical_key,
state: ElementState::Pressed,
repeat: false,
text: None,
},
})
}
pub fn ime_commit(text: alloc::string::String) -> Event {
Event::Ime(Ime::Commit(text))
}