Refactor mouse and keyboard input handling

This commit is contained in:
meehl committed 2026-08-29 16:12:45 +02:00
1 parent c297eeaca6
commit 4b344d15ae
5 files changed
+199 -261

No files matched your search

+64 -23
View File
@@ -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
View File
@@ -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))
}