Refactor mouse and keyboard input handling
This commit is contained in:
1 parent
c297eeaca6
commit
4b344d15ae
5 files changed
+199
-261
No files matched your search
+20
-185
@@ -3,37 +3,40 @@
|
||||
use crate::dpi::LogicalPoint;
|
||||
use ahash::{HashMap, HashSet};
|
||||
use std::time::{Duration, Instant};
|
||||
use winit::{
|
||||
event::MouseButton,
|
||||
keyboard::{Key, ModifiersState, NamedKey, SmolStr},
|
||||
};
|
||||
|
||||
pub type Key = winit::keyboard::Key;
|
||||
pub type MouseButton = winit::event::MouseButton;
|
||||
pub type ModifiersState = winit::keyboard::ModifiersState;
|
||||
|
||||
/// Max gap between two clicks for them to count as a double-click.
|
||||
const DOUBLE_CLICK_DURATION: Duration = Duration::from_millis(500);
|
||||
/// Max cursor movement between two clicks for them to count as a double-click.
|
||||
const DOUBLE_CLICK_MOVE_THRESHOLD: f32 = 5.0;
|
||||
|
||||
/// When and where a mouse button was last pressed, for double-click detection.
|
||||
#[derive(Debug, Clone)]
|
||||
struct MousePressState {
|
||||
time: Instant,
|
||||
pos: LogicalPoint<f32>,
|
||||
}
|
||||
|
||||
/// Current state of various keyboard and mouse inputs for the application.
|
||||
/// Tracks currently-pressed keys/buttons and cursor position.
|
||||
#[derive(Default)]
|
||||
pub struct InputState {
|
||||
/// Current state(s) of modifier keys. (Shift, Control, Alt, Super)
|
||||
/// Currently held modifier keys (Shift/Ctrl/Alt/Super).
|
||||
pub key_modifiers: ModifiersState,
|
||||
/// `HashSet` of currently pressed keyboard keys.
|
||||
/// Keys currently held down.
|
||||
keys_pressed: HashSet<Key>,
|
||||
/// `HashSet` of currently pressed mouse buttons.
|
||||
/// Mouse buttons currently held down.
|
||||
mouse_pressed: HashSet<MouseButton>,
|
||||
/// `HashMap` of mouse buttons (keys) with the last time they were pressed.
|
||||
/// Last press time/position per mouse button, for double-click detection.
|
||||
mouse_last_pressed: HashMap<MouseButton, MousePressState>,
|
||||
/// Current cursor position relative to the top-left corner of the window.
|
||||
/// Current mouse cursor position.
|
||||
cursor_pos: LogicalPoint<f32>,
|
||||
}
|
||||
|
||||
impl InputState {
|
||||
/// Updates [`Self::keys_pressed`] based on `is_pressed`.
|
||||
/// Marks 'key' as pressed or released.
|
||||
pub fn set_key_pressed(&mut self, key: &Key, is_pressed: bool) {
|
||||
if is_pressed {
|
||||
self.keys_pressed.insert(key.clone());
|
||||
@@ -42,13 +45,13 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns if the key is pressed (`true`) or not pressed (`false`).
|
||||
/// Returns whether `key` is currently pressed.
|
||||
pub fn key_pressed(&self, key: &Key) -> bool {
|
||||
self.keys_pressed.contains(key)
|
||||
}
|
||||
|
||||
/// Updates [`Self::mouse_pressed`](field@Self::mouse_pressed) based on provided
|
||||
/// `button` and `is_pressed`.
|
||||
/// Marks `button` as pressed or released.
|
||||
/// Returns `true` if this press counts as a double-click (always `false` on release).
|
||||
pub fn set_mouse_pressed(&mut self, button: MouseButton, is_pressed: bool) -> bool {
|
||||
if !is_pressed {
|
||||
self.mouse_pressed.remove(&button);
|
||||
@@ -80,7 +83,7 @@ impl InputState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the `button` was pressed and no release has been seen.
|
||||
/// Returns whether `button` is currently pressed.
|
||||
pub fn mouse_pressed(&self, button: MouseButton) -> bool {
|
||||
self.mouse_pressed.contains(&button)
|
||||
}
|
||||
@@ -90,13 +93,12 @@ impl InputState {
|
||||
self.cursor_pos
|
||||
}
|
||||
|
||||
/// Sets [`Self::cursor_pos`] to the provided `pos`.
|
||||
/// Updates the cursor position.
|
||||
pub fn set_mouse_pos(&mut self, pos: LogicalPoint<f32>) {
|
||||
self.cursor_pos = pos;
|
||||
}
|
||||
|
||||
/// Clears all pressed keys, buttons, and modifier states. Used when the
|
||||
/// application loses focus to avoid keys being "stuck" on/pressed.
|
||||
/// Clears all pressed keys, buttons, and modifier states.
|
||||
pub fn clear_pressed(&mut self) {
|
||||
self.keys_pressed.clear();
|
||||
self.mouse_pressed.clear();
|
||||
@@ -104,170 +106,3 @@ impl InputState {
|
||||
self.key_modifiers = ModifiersState::empty();
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to convert the provided string `s` to a [`winit::keyboard::Key`].
|
||||
/// Returns [None] if no matching Key found.
|
||||
pub fn str_as_key(s: &str) -> Option<Key> {
|
||||
Some(match s.to_uppercase().as_str() {
|
||||
"A" => Key::Character(SmolStr::new_static("A")),
|
||||
"B" => Key::Character(SmolStr::new_static("B")),
|
||||
"C" => Key::Character(SmolStr::new_static("C")),
|
||||
"D" => Key::Character(SmolStr::new_static("D")),
|
||||
"E" => Key::Character(SmolStr::new_static("E")),
|
||||
"F" => Key::Character(SmolStr::new_static("F")),
|
||||
"G" => Key::Character(SmolStr::new_static("G")),
|
||||
"H" => Key::Character(SmolStr::new_static("H")),
|
||||
"I" => Key::Character(SmolStr::new_static("I")),
|
||||
"J" => Key::Character(SmolStr::new_static("J")),
|
||||
"K" => Key::Character(SmolStr::new_static("K")),
|
||||
"L" => Key::Character(SmolStr::new_static("L")),
|
||||
"M" => Key::Character(SmolStr::new_static("M")),
|
||||
"N" => Key::Character(SmolStr::new_static("N")),
|
||||
"O" => Key::Character(SmolStr::new_static("O")),
|
||||
"P" => Key::Character(SmolStr::new_static("P")),
|
||||
"Q" => Key::Character(SmolStr::new_static("Q")),
|
||||
"R" => Key::Character(SmolStr::new_static("R")),
|
||||
"S" => Key::Character(SmolStr::new_static("S")),
|
||||
"T" => Key::Character(SmolStr::new_static("T")),
|
||||
"U" => Key::Character(SmolStr::new_static("U")),
|
||||
"V" => Key::Character(SmolStr::new_static("V")),
|
||||
"W" => Key::Character(SmolStr::new_static("W")),
|
||||
"X" => Key::Character(SmolStr::new_static("X")),
|
||||
"Y" => Key::Character(SmolStr::new_static("Y")),
|
||||
"Z" => Key::Character(SmolStr::new_static("Z")),
|
||||
|
||||
"0" => Key::Character(SmolStr::new_static("0")),
|
||||
"1" => Key::Character(SmolStr::new_static("1")),
|
||||
"2" => Key::Character(SmolStr::new_static("2")),
|
||||
"3" => Key::Character(SmolStr::new_static("3")),
|
||||
"4" => Key::Character(SmolStr::new_static("4")),
|
||||
"5" => Key::Character(SmolStr::new_static("5")),
|
||||
"6" => Key::Character(SmolStr::new_static("6")),
|
||||
"7" => Key::Character(SmolStr::new_static("7")),
|
||||
"8" => Key::Character(SmolStr::new_static("8")),
|
||||
"9" => Key::Character(SmolStr::new_static("9")),
|
||||
|
||||
"SHIFT" => Key::Named(NamedKey::Shift),
|
||||
"CTRL" => Key::Named(NamedKey::Control),
|
||||
"ALT" => Key::Named(NamedKey::Alt),
|
||||
|
||||
"F1" => Key::Named(NamedKey::F1),
|
||||
"F2" => Key::Named(NamedKey::F2),
|
||||
"F3" => Key::Named(NamedKey::F3),
|
||||
"F4" => Key::Named(NamedKey::F4),
|
||||
"F5" => Key::Named(NamedKey::F5),
|
||||
"F6" => Key::Named(NamedKey::F6),
|
||||
"F7" => Key::Named(NamedKey::F7),
|
||||
"F8" => Key::Named(NamedKey::F8),
|
||||
"F9" => Key::Named(NamedKey::F9),
|
||||
"F10" => Key::Named(NamedKey::F10),
|
||||
"F11" => Key::Named(NamedKey::F11),
|
||||
"F12" => Key::Named(NamedKey::F12),
|
||||
|
||||
" " => Key::Named(NamedKey::Space),
|
||||
"BACK" => Key::Named(NamedKey::Backspace),
|
||||
"TAB" => Key::Named(NamedKey::Tab),
|
||||
"RETURN" => Key::Named(NamedKey::Enter),
|
||||
"ESCAPE" => Key::Named(NamedKey::Escape),
|
||||
"PAUSE" => Key::Named(NamedKey::Pause),
|
||||
"PAGEUP" => Key::Named(NamedKey::PageUp),
|
||||
"PAGEDOWN" => Key::Named(NamedKey::PageDown),
|
||||
"END" => Key::Named(NamedKey::End),
|
||||
"HOME" => Key::Named(NamedKey::Home),
|
||||
"PRINTSCREEN" => Key::Named(NamedKey::PrintScreen),
|
||||
"INSERT" => Key::Named(NamedKey::Insert),
|
||||
"DELETE" => Key::Named(NamedKey::Delete),
|
||||
"UP" => Key::Named(NamedKey::ArrowUp),
|
||||
"DOWN" => Key::Named(NamedKey::ArrowDown),
|
||||
"LEFT" => Key::Named(NamedKey::ArrowLeft),
|
||||
"RIGHT" => Key::Named(NamedKey::ArrowRight),
|
||||
"NUMLOCK" => Key::Named(NamedKey::NumLock),
|
||||
"SCROLL" => Key::Named(NamedKey::ScrollLock),
|
||||
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Attempts to convert the provided [`winit::keyboard::Key`] `key` to a string
|
||||
/// representation that PoB recognizes.
|
||||
///
|
||||
/// Returns [None] if no matching string found.
|
||||
pub fn key_as_str(key: Key) -> Option<SmolStr> {
|
||||
Some(match key {
|
||||
Key::Character(ch) => {
|
||||
if ch == "=" {
|
||||
SmolStr::new("+") // This is what PoB does
|
||||
} else {
|
||||
ch
|
||||
}
|
||||
}
|
||||
Key::Named(named) => SmolStr::new(match named {
|
||||
NamedKey::Shift => "SHIFT",
|
||||
NamedKey::Control => "CTRL",
|
||||
NamedKey::Alt => "ALT",
|
||||
NamedKey::F1 => "F1",
|
||||
NamedKey::F2 => "F2",
|
||||
NamedKey::F3 => "F3",
|
||||
NamedKey::F4 => "F4",
|
||||
NamedKey::F5 => "F5",
|
||||
NamedKey::F6 => "F6",
|
||||
NamedKey::F7 => "F7",
|
||||
NamedKey::F8 => "F8",
|
||||
NamedKey::F9 => "F9",
|
||||
NamedKey::F10 => "F10",
|
||||
NamedKey::F11 => "F11",
|
||||
NamedKey::F12 => "F12",
|
||||
NamedKey::Space => " ",
|
||||
NamedKey::Backspace => "BACK",
|
||||
NamedKey::Tab => "TAB",
|
||||
NamedKey::Enter => "RETURN",
|
||||
NamedKey::Escape => "ESCAPE",
|
||||
NamedKey::Pause => "PAUSE",
|
||||
NamedKey::PageUp => "PAGEUP",
|
||||
NamedKey::PageDown => "PAGEDOWN",
|
||||
NamedKey::End => "END",
|
||||
NamedKey::Home => "HOME",
|
||||
NamedKey::PrintScreen => "PRINTSCREEN",
|
||||
NamedKey::Insert => "INSERT",
|
||||
NamedKey::Delete => "DELETE",
|
||||
NamedKey::ArrowUp => "UP",
|
||||
NamedKey::ArrowDown => "DOWN",
|
||||
NamedKey::ArrowLeft => "LEFT",
|
||||
NamedKey::ArrowRight => "RIGHT",
|
||||
NamedKey::NumLock => "NUMLOCK",
|
||||
NamedKey::ScrollLock => "SCROLL",
|
||||
_ => return None,
|
||||
}),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Attempts to convert the provided [&str] `s` from the PoB Lua Backend to a
|
||||
/// [MouseButton].
|
||||
///
|
||||
/// Returns [None] if no matching string found.
|
||||
pub fn str_as_mousebutton(s: &str) -> Option<MouseButton> {
|
||||
Some(match s.to_uppercase().as_str() {
|
||||
"LEFTBUTTON" => MouseButton::Left,
|
||||
"RIGHTBUTTON" => MouseButton::Right,
|
||||
"MIDDLEBUTTON" => MouseButton::Middle,
|
||||
"MOUSE4" => MouseButton::Back,
|
||||
"MOUSE5" => MouseButton::Forward,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Attempts to convert the provided [`MouseButton`] to a [`SmolStr`] that the PoB Lua
|
||||
/// backend recognizes.
|
||||
///
|
||||
/// Returns [None] if no matching [`MouseButton`] was found.
|
||||
pub fn mousebutton_as_str(button: MouseButton) -> Option<SmolStr> {
|
||||
Some(match button {
|
||||
MouseButton::Left => SmolStr::new("LEFTBUTTON"),
|
||||
MouseButton::Right => SmolStr::new("RIGHTBUTTON"),
|
||||
MouseButton::Middle => SmolStr::new("MIDDLEBUTTON"),
|
||||
MouseButton::Back => SmolStr::new("MOUSE4"),
|
||||
MouseButton::Forward => SmolStr::new("MOUSE5"),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
+9
-23
@@ -9,7 +9,7 @@ use crate::{
|
||||
dpi::{PhysicalPoint, ScaleFactor},
|
||||
draw_commands::DrawCommandRecorder,
|
||||
fonts::Fonts,
|
||||
input::{InputState, key_as_str, mousebutton_as_str},
|
||||
input::InputState,
|
||||
pob::subscript::{SubscriptManager, SubscriptResult},
|
||||
renderer::textures::TextureManager,
|
||||
stage::{StageEvent, StageFrameOutput, StageTransition},
|
||||
@@ -170,17 +170,11 @@ impl PathOfBuilding {
|
||||
match event {
|
||||
StageEvent::KeyDown { key } => {
|
||||
self.ctx_mut().input_state.set_key_pressed(&key, true);
|
||||
|
||||
if let Some(key) = key_as_str(key) {
|
||||
api::on_key_down(&self.lua, key.as_str(), false)?;
|
||||
}
|
||||
api::on_key_down(&self.lua, &api::Input::Keyboard(key), false)?;
|
||||
}
|
||||
StageEvent::KeyUp { key } => {
|
||||
self.ctx_mut().input_state.set_key_pressed(&key, false);
|
||||
|
||||
if let Some(key) = key_as_str(key) {
|
||||
api::on_key_up(&self.lua, key.as_str())?;
|
||||
}
|
||||
api::on_key_up(&self.lua, &api::Input::Keyboard(key))?;
|
||||
}
|
||||
StageEvent::ModifiersChanged { state } => {
|
||||
self.ctx_mut().input_state.key_modifiers = state;
|
||||
@@ -191,27 +185,19 @@ impl PathOfBuilding {
|
||||
}
|
||||
StageEvent::MouseDown { button } => {
|
||||
let is_double_click = self.ctx_mut().input_state.set_mouse_pressed(button, true);
|
||||
|
||||
if let Some(button) = mousebutton_as_str(button) {
|
||||
// PoB treats mouse buttons as keys
|
||||
api::on_key_down(&self.lua, button.as_str(), is_double_click)?;
|
||||
}
|
||||
api::on_key_down(&self.lua, &api::Input::Mouse(button), is_double_click)?;
|
||||
}
|
||||
StageEvent::MouseUp { button } => {
|
||||
self.ctx_mut().input_state.set_mouse_pressed(button, false);
|
||||
|
||||
if let Some(button) = mousebutton_as_str(button) {
|
||||
// PoB treats mouse buttons as keys
|
||||
api::on_key_up(&self.lua, button.as_str())?;
|
||||
}
|
||||
api::on_key_up(&self.lua, &api::Input::Mouse(button))?;
|
||||
}
|
||||
StageEvent::MouseWheel { delta } => {
|
||||
if delta > 0.0 {
|
||||
api::on_key_down(&self.lua, "WHEELUP", false)?;
|
||||
api::on_key_up(&self.lua, "WHEELUP")?;
|
||||
api::on_key_down(&self.lua, &api::Input::WheelUp, false)?;
|
||||
api::on_key_up(&self.lua, &api::Input::WheelUp)?;
|
||||
} else if delta < 0.0 {
|
||||
api::on_key_down(&self.lua, "WHEELDOWN", false)?;
|
||||
api::on_key_up(&self.lua, "WHEELDOWN")?;
|
||||
api::on_key_down(&self.lua, &api::Input::WheelDown, false)?;
|
||||
api::on_key_up(&self.lua, &api::Input::WheelDown)?;
|
||||
}
|
||||
}
|
||||
StageEvent::Exit => api::on_exit(&self.lua)?,
|
||||
|
||||
+64
-23
@@ -1,27 +1,36 @@
|
||||
use crate::pob::{
|
||||
api::{
|
||||
callback::{call_callback, get_custom_callback, set_custom_callback, set_main_object},
|
||||
clipboard::{copy, paste},
|
||||
compression::{deflate, inflate},
|
||||
console::{console_clear, console_execute, console_print_table, console_printf},
|
||||
general::{exit, get_time, open_url, render_init, restart, strip_escapes, take_screenshot},
|
||||
image_handle::register_image_handle_api,
|
||||
input::{get_cursor_pos, is_key_down, set_cursor_pos, show_cursor},
|
||||
lua::{load_module, protected_call, protected_load_module},
|
||||
paths::{
|
||||
get_runtime_path, get_script_path, get_user_path, get_work_dir, make_dir, remove_dir,
|
||||
set_work_dir,
|
||||
},
|
||||
search_handle::new_search_handle,
|
||||
subscript::{abort_subscript, is_subscript_running, launch_subscript},
|
||||
window::{
|
||||
get_dpi_scale_override, get_screen_scale, get_screen_size, set_dpi_scale_override,
|
||||
set_foreground, set_window_title,
|
||||
use crate::{
|
||||
input::{Key, MouseButton},
|
||||
pob::{
|
||||
api::{
|
||||
callback::{call_callback, get_custom_callback, set_custom_callback, set_main_object},
|
||||
clipboard::{copy, paste},
|
||||
compression::{deflate, inflate},
|
||||
console::{console_clear, console_execute, console_print_table, console_printf},
|
||||
general::{
|
||||
exit, get_time, open_url, render_init, restart, strip_escapes, take_screenshot,
|
||||
},
|
||||
image_handle::register_image_handle_api,
|
||||
input::{
|
||||
get_cursor_pos, is_key_down, key_as_str, mousebutton_as_str, set_cursor_pos,
|
||||
show_cursor,
|
||||
},
|
||||
lua::{load_module, protected_call, protected_load_module},
|
||||
paths::{
|
||||
get_runtime_path, get_script_path, get_user_path, get_work_dir, make_dir,
|
||||
remove_dir, set_work_dir,
|
||||
},
|
||||
search_handle::new_search_handle,
|
||||
subscript::{abort_subscript, is_subscript_running, launch_subscript},
|
||||
window::{
|
||||
get_dpi_scale_override, get_screen_scale, get_screen_size, set_dpi_scale_override,
|
||||
set_foreground, set_window_title,
|
||||
},
|
||||
},
|
||||
subscript::NativeMultiValue,
|
||||
},
|
||||
subscript::NativeMultiValue,
|
||||
};
|
||||
use mlua::{Lua, MultiValue, Result as LuaResult};
|
||||
use winit::keyboard::SmolStr;
|
||||
|
||||
mod callback;
|
||||
mod clipboard;
|
||||
@@ -37,7 +46,7 @@ mod search_handle;
|
||||
mod subscript;
|
||||
mod window;
|
||||
|
||||
/// Register functions that can be called from lua
|
||||
/// Register functions that can be called from Lua
|
||||
pub fn register_globals(lua: &Lua) -> LuaResult<()> {
|
||||
let globals = lua.globals();
|
||||
|
||||
@@ -142,10 +151,42 @@ define_callbacks! {
|
||||
on_exit => "OnExit"() -> ();
|
||||
on_frame => "OnFrame"() -> ();
|
||||
can_exit => "CanExit"() -> bool;
|
||||
on_key_down => "OnKeyDown"(key: &str, double_click: bool) -> ();
|
||||
on_key_up => "OnKeyUp"(key: &str) -> ();
|
||||
on_char => "OnChar"(ch: char) -> ();
|
||||
on_sub_call => "OnSubCall"(name: String, args: NativeMultiValue) -> MultiValue;
|
||||
on_sub_finished => "OnSubFinished"(id: u64, return_values: NativeMultiValue) -> ();
|
||||
on_sub_error => "OnSubError"(id: u64, error_msg: String) -> ();
|
||||
}
|
||||
|
||||
// `on_key_up` and `on_key_down` are manually defined because they require additional conversions
|
||||
// between key representations.
|
||||
pub enum Input {
|
||||
Keyboard(Key),
|
||||
Mouse(MouseButton),
|
||||
WheelUp,
|
||||
WheelDown,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
fn as_pob_key_str(&self) -> Option<SmolStr> {
|
||||
match self {
|
||||
Input::Keyboard(key) => key_as_str(key.clone()),
|
||||
Input::Mouse(button) => mousebutton_as_str(*button),
|
||||
Input::WheelUp => Some("WHEELUP".into()),
|
||||
Input::WheelDown => Some("WHEELDOWN".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_key_down(lua: &Lua, key: &Input, double_click: bool) -> LuaResult<()> {
|
||||
let Some(key_str) = key.as_pob_key_str() else {
|
||||
return Ok(());
|
||||
};
|
||||
call_callback(lua, "OnKeyDown", (key_str.as_str(), double_click))
|
||||
}
|
||||
|
||||
pub fn on_key_up(lua: &Lua, key: &Input) -> LuaResult<()> {
|
||||
let Some(key_str) = key.as_pob_key_str() else {
|
||||
return Ok(());
|
||||
};
|
||||
call_callback(lua, "OnKeyUp", (key_str.as_str(),))
|
||||
}
|
||||
+97
-1
@@ -1,8 +1,9 @@
|
||||
use crate::{
|
||||
input::{str_as_key, str_as_mousebutton},
|
||||
input::{Key, MouseButton},
|
||||
pob::Context,
|
||||
};
|
||||
use mlua::{Lua, Result as LuaResult};
|
||||
use winit::keyboard::{NamedKey, SmolStr};
|
||||
|
||||
pub fn get_cursor_pos(l: &Lua, _: ()) -> LuaResult<(u32, u32)> {
|
||||
let ctx = l.app_data_ref::<Context>().unwrap();
|
||||
@@ -29,3 +30,98 @@ pub fn is_key_down(l: &Lua, key_name: String) -> LuaResult<bool> {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
const NAMED_KEYS: &[(&str, NamedKey)] = &[
|
||||
("BACK", NamedKey::Backspace),
|
||||
("TAB", NamedKey::Tab),
|
||||
("RETURN", NamedKey::Enter),
|
||||
("ESCAPE", NamedKey::Escape),
|
||||
("SHIFT", NamedKey::Shift),
|
||||
("CTRL", NamedKey::Control),
|
||||
("ALT", NamedKey::Alt),
|
||||
("PAUSE", NamedKey::Pause),
|
||||
("PAGEUP", NamedKey::PageUp),
|
||||
("PAGEDOWN", NamedKey::PageDown),
|
||||
("END", NamedKey::End),
|
||||
("HOME", NamedKey::Home),
|
||||
("PRINTSCREEN", NamedKey::PrintScreen),
|
||||
("INSERT", NamedKey::Insert),
|
||||
("DELETE", NamedKey::Delete),
|
||||
("UP", NamedKey::ArrowUp),
|
||||
("DOWN", NamedKey::ArrowDown),
|
||||
("LEFT", NamedKey::ArrowLeft),
|
||||
("RIGHT", NamedKey::ArrowRight),
|
||||
("F1", NamedKey::F1),
|
||||
("F2", NamedKey::F2),
|
||||
("F3", NamedKey::F3),
|
||||
("F4", NamedKey::F4),
|
||||
("F5", NamedKey::F5),
|
||||
("F6", NamedKey::F6),
|
||||
("F7", NamedKey::F7),
|
||||
("F8", NamedKey::F8),
|
||||
("F9", NamedKey::F9),
|
||||
("F10", NamedKey::F10),
|
||||
("F11", NamedKey::F11),
|
||||
("F12", NamedKey::F12),
|
||||
("F13", NamedKey::F13),
|
||||
("F14", NamedKey::F14),
|
||||
("F15", NamedKey::F15),
|
||||
(" ", NamedKey::Space),
|
||||
("NUMLOCK", NamedKey::NumLock),
|
||||
("SCROLL", NamedKey::ScrollLock),
|
||||
];
|
||||
|
||||
/// Atempts to convert PoB's key representation into a `Key`.
|
||||
fn str_as_key(s: &str) -> Option<Key> {
|
||||
let upper = s.to_uppercase();
|
||||
|
||||
// named keys
|
||||
if let Some((_, named)) = NAMED_KEYS.iter().find(|(name, _)| *name == upper) {
|
||||
return Some(Key::Named(*named));
|
||||
}
|
||||
|
||||
// single-character keys
|
||||
let mut chars = upper.chars();
|
||||
let first = chars.next()?;
|
||||
chars
|
||||
.next()
|
||||
.is_none()
|
||||
.then(|| Key::Character(SmolStr::new(first.to_string())))
|
||||
}
|
||||
|
||||
/// Atempts to convert a `Key` into PoB's key representation
|
||||
pub fn key_as_str(key: Key) -> Option<SmolStr> {
|
||||
match key {
|
||||
Key::Character(ch) if ch == "=" => Some(SmolStr::new("+")), // matches PoB's behavior
|
||||
Key::Character(ch) => Some(ch),
|
||||
Key::Named(named) => NAMED_KEYS
|
||||
.iter()
|
||||
.find(|(_, n)| *n == named)
|
||||
.map(|(name, _)| SmolStr::new(*name)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
const MOUSE_BUTTONS: &[(&str, MouseButton)] = &[
|
||||
("LEFTBUTTON", MouseButton::Left),
|
||||
("RIGHTBUTTON", MouseButton::Right),
|
||||
("MIDDLEBUTTON", MouseButton::Middle),
|
||||
("MOUSE4", MouseButton::Back),
|
||||
("MOUSE5", MouseButton::Forward),
|
||||
];
|
||||
|
||||
/// Atempts to convert PoB's key representation into a `MouseButton`.
|
||||
fn str_as_mousebutton(s: &str) -> Option<MouseButton> {
|
||||
MOUSE_BUTTONS
|
||||
.iter()
|
||||
.find(|(name, _)| *name == s.to_uppercase())
|
||||
.map(|(_, button)| *button)
|
||||
}
|
||||
|
||||
/// Atempts to convert a `MouseButton` into PoB's key representation
|
||||
pub fn mousebutton_as_str(button: MouseButton) -> Option<SmolStr> {
|
||||
MOUSE_BUTTONS
|
||||
.iter()
|
||||
.find(|(_, b)| *b == button)
|
||||
.map(|(name, _)| SmolStr::new(*name))
|
||||
}
|
||||
+9
-29
@@ -5,40 +5,20 @@ use winit::window::Window;
|
||||
use crate::{
|
||||
dpi::{PhysicalPoint, PhysicalSize, ScaleFactor},
|
||||
draw_commands::DrawCommand,
|
||||
input::{Key, ModifiersState, MouseButton},
|
||||
installer::Installer,
|
||||
pob::PathOfBuilding,
|
||||
};
|
||||
|
||||
pub type Key = winit::keyboard::Key;
|
||||
pub type MouseButton = winit::event::MouseButton;
|
||||
pub type ModifiersState = winit::keyboard::ModifiersState;
|
||||
|
||||
pub enum StageEvent {
|
||||
KeyDown {
|
||||
key: Key,
|
||||
},
|
||||
KeyUp {
|
||||
key: Key,
|
||||
},
|
||||
CharacterInput {
|
||||
ch: char,
|
||||
},
|
||||
ModifiersChanged {
|
||||
state: ModifiersState,
|
||||
},
|
||||
MouseDown {
|
||||
button: MouseButton,
|
||||
//is_double_click: bool,
|
||||
},
|
||||
MouseUp {
|
||||
button: MouseButton,
|
||||
},
|
||||
MouseWheel {
|
||||
delta: f32,
|
||||
},
|
||||
MouseMoved {
|
||||
pos: PhysicalPoint<f32>,
|
||||
},
|
||||
KeyDown { key: Key },
|
||||
KeyUp { key: Key },
|
||||
CharacterInput { ch: char },
|
||||
ModifiersChanged { state: ModifiersState },
|
||||
MouseDown { button: MouseButton },
|
||||
MouseUp { button: MouseButton },
|
||||
MouseWheel { delta: f32 },
|
||||
MouseMoved { pos: PhysicalPoint<f32> },
|
||||
Exit,
|
||||
Resized(PhysicalSize<u32>),
|
||||
ScaleFactorChanged(f64),
|
||||
|
||||
Reference in new issue
Block a user