Compare commits

...
7 Commits
Author SHA1 Message Date
meehl 1c99e9fabc chore: Release rusty-path-of-building version 0.2.11
Release / create-release (push) Successful in 4s
2026-03-05 08:50:53 +01:00
meehl 12fc773958 Update CHANGELOG 2026-03-05 08:50:34 +01:00
meehl ef4f86eac8 Track state and forward events for logical keys.
Using physical key codes leads to unexpected bevahior on different
keyboard layouts. Use logical keys like SimpleGraphic.

Closes #27
2026-03-05 08:30:56 +01:00
Ac5000 966363a859 Added method to InputState to clear pressed keys, mods, and mouse. Call method on window focus loss. 2026-02-28 10:23:56 +01:00
Ac5000 9d33a7704c Added documentation to src/input.rs 2026-02-28 10:23:56 +01:00
Ac5000 3a66f15b49 Added documentation. 2026-02-28 10:23:56 +01:00
meehl 7d02ca098c Use repology to display packaging status 2025-12-17 12:19:40 +01:00
11 changed files with 233 additions and 229 deletions

No files matched your search

+9 -1
View File
@@ -2,6 +2,13 @@
## [Unreleased] - ReleaseDate ## [Unreleased] - ReleaseDate
## [0.2.11] - 2026-03-05
### Fixed
- Clear pressed keys on focus loss to avoid stuck keys
- Use logical keys for input handling to fix unexpected behavior with different keyboard layouts
## [0.2.10] - 2025-12-15 ## [0.2.10] - 2025-12-15
### Added ### Added
@@ -143,8 +150,9 @@
- First release - First release
<!-- next-url --> <!-- next-url -->
[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.10...HEAD [Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.11...HEAD
[0.2.11]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.10...v0.2.11
[0.2.10]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.9...v0.2.10 [0.2.10]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.9...v0.2.10
[0.2.9]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.8...v0.2.9 [0.2.9]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.8...v0.2.9
[0.2.8]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.7...v0.2.8 [0.2.8]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.7...v0.2.8
Generated
+1 -1
View File
@@ -2506,7 +2506,7 @@ checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]] [[package]]
name = "rusty-path-of-building" name = "rusty-path-of-building"
version = "0.2.10" version = "0.2.11"
dependencies = [ dependencies = [
"ahash", "ahash",
"anyhow", "anyhow",
+1 -1
View File
@@ -4,7 +4,7 @@ authors = ["meehl"]
description = "Cross-platform runtime for Path of Building" description = "Cross-platform runtime for Path of Building"
repository = "https://github.com/meehl/rusty-path-of-building" repository = "https://github.com/meehl/rusty-path-of-building"
license = "MIT" license = "MIT"
version = "0.2.10" version = "0.2.11"
edition = "2024" edition = "2024"
publish = false publish = false
+1 -9
View File
@@ -12,20 +12,12 @@ rusty-path-of-building [poe1|poe2]
## Installation ## Installation
### Arch (AUR) [![Packaging status](https://repology.org/badge/vertical-allrepos/rusty-path-of-building.svg)](https://repology.org/project/rusty-path-of-building/versions)
[![Stable version badge](https://img.shields.io/aur/version/rusty-path-of-building)](https://aur.archlinux.org/packages/rusty-path-of-building)
Please check [the Arch Wiki](https://wiki.archlinux.org/title/Arch_User_Repository) on how to install packages from the AUR.
### Flathub ### Flathub
[![Stable version badge](https://img.shields.io/flathub/v/community.pathofbuilding.PathOfBuilding)](https://flathub.org/en/apps/community.pathofbuilding.PathOfBuilding) [![Stable version badge](https://img.shields.io/flathub/v/community.pathofbuilding.PathOfBuilding)](https://flathub.org/en/apps/community.pathofbuilding.PathOfBuilding)
### Nix
[nixpkgs](https://search.nixos.org/packages?type=packages&query=rusty-path-of-building)
### Building from source ### Building from source
`LuaJIT` needs to be installed on your system for the `mlua` crate to compile. `LuaJIT` needs to be installed on your system for the `mlua` crate to compile.
+3 -3
View File
@@ -1,5 +1,5 @@
use crate::{ use crate::{
input::{str_as_keycode, str_as_mousebutton}, input::{str_as_key, str_as_mousebutton},
lua::Context, lua::Context,
}; };
use mlua::{Lua, Result as LuaResult}; use mlua::{Lua, Result as LuaResult};
@@ -13,8 +13,8 @@ pub fn get_cursor_pos(l: &Lua, _: ()) -> LuaResult<(u32, u32)> {
pub fn is_key_down(l: &Lua, key_name: String) -> LuaResult<bool> { pub fn is_key_down(l: &Lua, key_name: String) -> LuaResult<bool> {
let ctx = l.app_data_ref::<&'static Context>().unwrap(); let ctx = l.app_data_ref::<&'static Context>().unwrap();
if let Some(code) = str_as_keycode(&key_name) { if let Some(key) = str_as_key(&key_name) {
Ok(ctx.input().key_pressed(code)) Ok(ctx.input().key_pressed(key))
} else if let Some(button) = str_as_mousebutton(&key_name) { } else if let Some(button) = str_as_mousebutton(&key_name) {
Ok(ctx.input().mouse_pressed(button)) Ok(ctx.input().mouse_pressed(button))
} else { } else {
+20 -16
View File
@@ -14,7 +14,7 @@ use anyhow::Result;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use winit::{ use winit::{
application::ApplicationHandler, event::*, event_loop::ActiveEventLoop, keyboard::PhysicalKey, application::ApplicationHandler, event::*, event_loop::ActiveEventLoop,
platform::modifier_supplement::KeyEventExtModifierSupplement, window::Window, platform::modifier_supplement::KeyEventExtModifierSupplement, window::Window,
}; };
@@ -258,32 +258,36 @@ impl ApplicationHandler<GraphicsContext> for App {
self.state.window.is_focused = focused; self.state.window.is_focused = focused;
if focused { if focused {
self.state.window.request_redraw(); self.state.window.request_redraw();
} else {
// Clear inputs on lost focus to avoid "stuck" keys on Wayland
// systems.
self.state.input.clear_pressed();
} }
} }
WindowEvent::ScaleFactorChanged { scale_factor, .. } => { WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
self.state.window.set_scale_factor(scale_factor as f32); self.state.window.set_scale_factor(scale_factor as f32);
} }
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput { event, .. } => {
event: let state = event.state;
key_event @ KeyEvent {
physical_key: PhysicalKey::Code(code),
state,
..
},
..
} => {
// update input state // update input state
self.state.input.set_key_pressed(code, state.is_pressed()); self.state
.input
.set_key_pressed(event.logical_key.clone(), state.is_pressed());
// forward KeyUp/KeyDown events // forward KeyUp/KeyDown events
let event = match state { let app_event = match state {
ElementState::Pressed => AppEvent::KeyDown { code }, ElementState::Pressed => AppEvent::KeyDown {
ElementState::Released => AppEvent::KeyUp { code }, key: event.logical_key.clone(),
},
ElementState::Released => AppEvent::KeyUp {
key: event.logical_key.clone(),
},
}; };
self.handle_event(event); self.handle_event(app_event);
// handle text input // handle text input
if let Some(text) = key_event.text_with_all_modifiers() if let Some(text) = event.text_with_all_modifiers()
&& state.is_pressed() && state.is_pressed()
{ {
for ch in text.chars() { for ch in text.chars() {
+14
View File
@@ -1,29 +1,42 @@
//! `args` is used to parse the arguments passed to the program on launch.
//!
//! Normally these are the arguments passed after the `rusty-path-of-building`
//! command from a CLI.
use clap::Parser; use clap::Parser;
use clap::ValueEnum; use clap::ValueEnum;
use directories::BaseDirs; use directories::BaseDirs;
use std::path::PathBuf; use std::path::PathBuf;
/// CLI arguments passed to the application on launch.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
pub struct Args { pub struct Args {
/// Used to determine which PoB to start. (PoE1 or PoE2)
#[arg(value_enum)] #[arg(value_enum)]
pub game: Game, pub game: Game,
/// Specify a build to load on start using a URL. (Optional)
#[arg( #[arg(
help = "URL of build to import on startup. Needs to use custom protocol schema, e.g. `pob://pobbin/<id>`" help = "URL of build to import on startup. Needs to use custom protocol schema, e.g. `pob://pobbin/<id>`"
)] )]
pub import_url: Option<String>, pub import_url: Option<String>,
} }
/// Enum representing which game (PoE1 or PoE2) the application needs to launch.
#[derive(Debug, Clone, Copy, ValueEnum)] #[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Game { pub enum Game {
/// Path of Exile 1
#[value(name = "poe1")] #[value(name = "poe1")]
Poe1, Poe1,
/// Path of Exile 2
#[value(name = "poe2")] #[value(name = "poe2")]
Poe2, Poe2,
} }
impl Game { impl Game {
/// Returns the path to the user’s data directory based on which `Game` option
/// was used to start the application.
pub fn data_dir(&self) -> PathBuf { pub fn data_dir(&self) -> PathBuf {
let directory_name = match self { let directory_name = match self {
Game::Poe1 => "RustyPathOfBuilding1", Game::Poe1 => "RustyPathOfBuilding1",
@@ -32,6 +45,7 @@ impl Game {
BaseDirs::new().unwrap().data_dir().join(directory_name) BaseDirs::new().unwrap().data_dir().join(directory_name)
} }
/// Returns the path to the user's data directory. Calls [`Self::data_dir`].
pub fn script_dir(&self) -> PathBuf { pub fn script_dir(&self) -> PathBuf {
self.data_dir() self.data_dir()
} }
+167 -182
View File
@@ -1,33 +1,45 @@
//! Module to handle user inputs like keyboard keys and mouse buttons.
use crate::dpi::LogicalPoint; use crate::dpi::LogicalPoint;
use ahash::{HashMap, HashSet}; use ahash::{HashMap, HashSet};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use winit::{ use winit::{
event::MouseButton, event::MouseButton,
keyboard::{KeyCode, ModifiersState}, keyboard::{Key, ModifiersState, NamedKey, SmolStr},
}; };
/// Current state of various keyboard and mouse inputs for the application.
#[derive(Default)] #[derive(Default)]
pub struct InputState { pub struct InputState {
/// Current state(s) of modifier keys. (Shift, Control, Alt, Super)
pub key_modifiers: ModifiersState, pub key_modifiers: ModifiersState,
keys_pressed: HashSet<KeyCode>, /// HashSet of currently pressed keyboard keys.
keys_pressed: HashSet<Key>,
/// HashSet of currently pressed mouse buttons.
mouse_pressed: HashSet<MouseButton>, mouse_pressed: HashSet<MouseButton>,
/// HashMap of mouse buttons (keys) with the last time they were pressed.
mouse_last_pressed: HashMap<MouseButton, Instant>, mouse_last_pressed: HashMap<MouseButton, Instant>,
/// Current cursor position relative to the top-left corner of the window.
cursor_pos: LogicalPoint<f32>, cursor_pos: LogicalPoint<f32>,
} }
impl InputState { impl InputState {
pub fn set_key_pressed(&mut self, code: KeyCode, is_pressed: bool) { /// Updates [`Self::keys_pressed`] based on `is_pressed`.
pub fn set_key_pressed(&mut self, key: Key, is_pressed: bool) {
if is_pressed { if is_pressed {
self.keys_pressed.insert(code); self.keys_pressed.insert(key);
} else { } else {
self.keys_pressed.remove(&code); self.keys_pressed.remove(&key);
} }
} }
pub fn key_pressed(&self, code: KeyCode) -> bool { /// Returns if the key is pressed (`true`) or not pressed (`false`).
self.keys_pressed.contains(&code) 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`.
pub fn set_mouse_pressed(&mut self, button: MouseButton, is_pressed: bool) -> bool { pub fn set_mouse_pressed(&mut self, button: MouseButton, is_pressed: bool) -> bool {
if is_pressed { if is_pressed {
self.mouse_pressed.insert(button); self.mouse_pressed.insert(button);
@@ -50,202 +62,171 @@ impl InputState {
} }
} }
/// Returns `true` if the `button` was pressed and no release has been seen.
pub fn mouse_pressed(&self, button: MouseButton) -> bool { pub fn mouse_pressed(&self, button: MouseButton) -> bool {
self.mouse_pressed.contains(&button) self.mouse_pressed.contains(&button)
} }
/// Returns the last known cursor position.
pub fn mouse_pos(&self) -> LogicalPoint<f32> { pub fn mouse_pos(&self) -> LogicalPoint<f32> {
self.cursor_pos self.cursor_pos
} }
/// Sets [`Self::cursor_pos`] to the provided `pos`.
pub fn set_mouse_pos(&mut self, pos: LogicalPoint<f32>) { pub fn set_mouse_pos(&mut self, pos: LogicalPoint<f32>) {
self.cursor_pos = pos; 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.
pub fn clear_pressed(&mut self) {
self.keys_pressed.clear();
self.mouse_pressed.clear();
self.key_modifiers = ModifiersState::empty();
}
} }
pub fn str_as_keycode(s: &str) -> Option<KeyCode> { /// 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() { Some(match s.to_uppercase().as_str() {
// Letters "A" => Key::Character(SmolStr::new_static("A")),
"A" => KeyCode::KeyA, "B" => Key::Character(SmolStr::new_static("B")),
"B" => KeyCode::KeyB, "C" => Key::Character(SmolStr::new_static("C")),
"C" => KeyCode::KeyC, "D" => Key::Character(SmolStr::new_static("D")),
"D" => KeyCode::KeyD, "E" => Key::Character(SmolStr::new_static("E")),
"E" => KeyCode::KeyE, "F" => Key::Character(SmolStr::new_static("F")),
"F" => KeyCode::KeyF, "G" => Key::Character(SmolStr::new_static("G")),
"G" => KeyCode::KeyG, "H" => Key::Character(SmolStr::new_static("H")),
"H" => KeyCode::KeyH, "I" => Key::Character(SmolStr::new_static("I")),
"I" => KeyCode::KeyI, "J" => Key::Character(SmolStr::new_static("J")),
"J" => KeyCode::KeyJ, "K" => Key::Character(SmolStr::new_static("K")),
"K" => KeyCode::KeyK, "L" => Key::Character(SmolStr::new_static("L")),
"L" => KeyCode::KeyL, "M" => Key::Character(SmolStr::new_static("M")),
"M" => KeyCode::KeyM, "N" => Key::Character(SmolStr::new_static("N")),
"N" => KeyCode::KeyN, "O" => Key::Character(SmolStr::new_static("O")),
"O" => KeyCode::KeyO, "P" => Key::Character(SmolStr::new_static("P")),
"P" => KeyCode::KeyP, "Q" => Key::Character(SmolStr::new_static("Q")),
"Q" => KeyCode::KeyQ, "R" => Key::Character(SmolStr::new_static("R")),
"R" => KeyCode::KeyR, "S" => Key::Character(SmolStr::new_static("S")),
"S" => KeyCode::KeyS, "T" => Key::Character(SmolStr::new_static("T")),
"T" => KeyCode::KeyT, "U" => Key::Character(SmolStr::new_static("U")),
"U" => KeyCode::KeyU, "V" => Key::Character(SmolStr::new_static("V")),
"V" => KeyCode::KeyV, "W" => Key::Character(SmolStr::new_static("W")),
"W" => KeyCode::KeyW, "X" => Key::Character(SmolStr::new_static("X")),
"X" => KeyCode::KeyX, "Y" => Key::Character(SmolStr::new_static("Y")),
"Y" => KeyCode::KeyY, "Z" => Key::Character(SmolStr::new_static("Z")),
"Z" => KeyCode::KeyZ,
// Digits "0" => Key::Character(SmolStr::new_static("0")),
"0" => KeyCode::Digit0, "1" => Key::Character(SmolStr::new_static("1")),
"1" => KeyCode::Digit1, "2" => Key::Character(SmolStr::new_static("2")),
"2" => KeyCode::Digit2, "3" => Key::Character(SmolStr::new_static("3")),
"3" => KeyCode::Digit3, "4" => Key::Character(SmolStr::new_static("4")),
"4" => KeyCode::Digit4, "5" => Key::Character(SmolStr::new_static("5")),
"5" => KeyCode::Digit5, "6" => Key::Character(SmolStr::new_static("6")),
"6" => KeyCode::Digit6, "7" => Key::Character(SmolStr::new_static("7")),
"7" => KeyCode::Digit7, "8" => Key::Character(SmolStr::new_static("8")),
"8" => KeyCode::Digit8, "9" => Key::Character(SmolStr::new_static("9")),
"9" => KeyCode::Digit9,
// Modifiers "SHIFT" => Key::Named(NamedKey::Shift),
"SHIFT" => KeyCode::ShiftLeft, "CTRL" => Key::Named(NamedKey::Control),
"CTRL" => KeyCode::ControlLeft, "ALT" => Key::Named(NamedKey::Alt),
"ALT" => KeyCode::AltLeft,
// F Keys "F1" => Key::Named(NamedKey::F1),
"F1" => KeyCode::F1, "F2" => Key::Named(NamedKey::F2),
"F2" => KeyCode::F2, "F3" => Key::Named(NamedKey::F3),
"F3" => KeyCode::F3, "F4" => Key::Named(NamedKey::F4),
"F4" => KeyCode::F4, "F5" => Key::Named(NamedKey::F5),
"F5" => KeyCode::F5, "F6" => Key::Named(NamedKey::F6),
"F6" => KeyCode::F6, "F7" => Key::Named(NamedKey::F7),
"F7" => KeyCode::F7, "F8" => Key::Named(NamedKey::F8),
"F8" => KeyCode::F8, "F9" => Key::Named(NamedKey::F9),
"F9" => KeyCode::F9, "F10" => Key::Named(NamedKey::F10),
"F10" => KeyCode::F10, "F11" => Key::Named(NamedKey::F11),
"F11" => KeyCode::F11, "F12" => Key::Named(NamedKey::F12),
"F12" => KeyCode::F12,
// Rest " " => Key::Named(NamedKey::Space),
" " => KeyCode::Space, "BACK" => Key::Named(NamedKey::Backspace),
"BACK" => KeyCode::Backspace, "TAB" => Key::Named(NamedKey::Tab),
"TAB" => KeyCode::Tab, "RETURN" => Key::Named(NamedKey::Enter),
"RETURN" => KeyCode::Enter, "ESCAPE" => Key::Named(NamedKey::Escape),
"ESCAPE" => KeyCode::Escape, "PAUSE" => Key::Named(NamedKey::Pause),
"PAUSE" => KeyCode::Pause, "PAGEUP" => Key::Named(NamedKey::PageUp),
"PAGEUP" => KeyCode::PageUp, "PAGEDOWN" => Key::Named(NamedKey::PageDown),
"PAGEDOWN" => KeyCode::PageDown, "END" => Key::Named(NamedKey::End),
"END" => KeyCode::End, "HOME" => Key::Named(NamedKey::Home),
"HOME" => KeyCode::Home, "PRINTSCREEN" => Key::Named(NamedKey::PrintScreen),
"PRINTSCREEN" => KeyCode::PrintScreen, "INSERT" => Key::Named(NamedKey::Insert),
"INSERT" => KeyCode::Insert, "DELETE" => Key::Named(NamedKey::Delete),
"DELETE" => KeyCode::Delete, "UP" => Key::Named(NamedKey::ArrowUp),
"UP" => KeyCode::ArrowUp, "DOWN" => Key::Named(NamedKey::ArrowDown),
"DOWN" => KeyCode::ArrowDown, "LEFT" => Key::Named(NamedKey::ArrowLeft),
"LEFT" => KeyCode::ArrowLeft, "RIGHT" => Key::Named(NamedKey::ArrowRight),
"RIGHT" => KeyCode::ArrowRight, "NUMLOCK" => Key::Named(NamedKey::NumLock),
"NUMLOCK" => KeyCode::NumLock, "SCROLL" => Key::Named(NamedKey::ScrollLock),
"SCROLL" => KeyCode::ScrollLock,
_ => return None, _ => return None,
}) })
} }
pub fn keycode_as_str(code: KeyCode) -> Option<&'static str> { /// Attempts to convert the provided [winit::keyboard::Key] `key` to a string
Some(match code { /// representation that PoB recognizes.
// Letters ///
KeyCode::KeyA => "a", /// Returns [None] if no matching string found.
KeyCode::KeyB => "b", pub fn key_as_str(key: Key) -> Option<SmolStr> {
KeyCode::KeyC => "c", Some(match key {
KeyCode::KeyD => "d", Key::Character(ch) => {
KeyCode::KeyE => "e", if ch == "=" {
KeyCode::KeyF => "f", SmolStr::new("+") // This is what PoB does
KeyCode::KeyG => "g", } else {
KeyCode::KeyH => "h", ch
KeyCode::KeyI => "i", }
KeyCode::KeyJ => "j", }
KeyCode::KeyK => "k", Key::Named(named) => SmolStr::new(match named {
KeyCode::KeyL => "l", NamedKey::Shift => "SHIFT",
KeyCode::KeyM => "m", NamedKey::Control => "CTRL",
KeyCode::KeyN => "n", NamedKey::Alt => "ALT",
KeyCode::KeyO => "o", NamedKey::F1 => "F1",
KeyCode::KeyP => "p", NamedKey::F2 => "F2",
KeyCode::KeyQ => "q", NamedKey::F3 => "F3",
KeyCode::KeyR => "r", NamedKey::F4 => "F4",
KeyCode::KeyS => "s", NamedKey::F5 => "F5",
KeyCode::KeyT => "t", NamedKey::F6 => "F6",
KeyCode::KeyU => "u", NamedKey::F7 => "F7",
KeyCode::KeyV => "v", NamedKey::F8 => "F8",
KeyCode::KeyW => "w", NamedKey::F9 => "F9",
KeyCode::KeyX => "x", NamedKey::F10 => "F10",
KeyCode::KeyY => "y", NamedKey::F11 => "F11",
KeyCode::KeyZ => "z", NamedKey::F12 => "F12",
NamedKey::Space => " ",
// Digits NamedKey::Backspace => "BACK",
KeyCode::Digit0 => "0", NamedKey::Tab => "TAB",
KeyCode::Digit1 => "1", NamedKey::Enter => "RETURN",
KeyCode::Digit2 => "2", NamedKey::Escape => "ESCAPE",
KeyCode::Digit3 => "3", NamedKey::Pause => "PAUSE",
KeyCode::Digit4 => "4", NamedKey::PageUp => "PAGEUP",
KeyCode::Digit5 => "5", NamedKey::PageDown => "PAGEDOWN",
KeyCode::Digit6 => "6", NamedKey::End => "END",
KeyCode::Digit7 => "7", NamedKey::Home => "HOME",
KeyCode::Digit8 => "8", NamedKey::PrintScreen => "PRINTSCREEN",
KeyCode::Digit9 => "9", NamedKey::Insert => "INSERT",
NamedKey::Delete => "DELETE",
// Modifiers NamedKey::ArrowUp => "UP",
KeyCode::ShiftLeft => "SHIFT", NamedKey::ArrowDown => "DOWN",
KeyCode::ControlLeft => "CTRL", NamedKey::ArrowLeft => "LEFT",
KeyCode::AltLeft => "ALT", NamedKey::ArrowRight => "RIGHT",
NamedKey::NumLock => "NUMLOCK",
// F Keys NamedKey::ScrollLock => "SCROLL",
KeyCode::F1 => "F1", _ => return None,
KeyCode::F2 => "F2", }),
KeyCode::F3 => "F3",
KeyCode::F4 => "F4",
KeyCode::F5 => "F5",
KeyCode::F6 => "F6",
KeyCode::F7 => "F7",
KeyCode::F8 => "F8",
KeyCode::F9 => "F9",
KeyCode::F10 => "F10",
KeyCode::F11 => "F11",
KeyCode::F12 => "F12",
// Rest
KeyCode::Space => " ",
KeyCode::Backspace => "BACK",
KeyCode::Tab => "TAB",
KeyCode::Enter => "RETURN",
KeyCode::Escape => "ESCAPE",
KeyCode::Pause => "PAUSE",
KeyCode::PageUp => "PAGEUP",
KeyCode::PageDown => "PAGEDOWN",
KeyCode::End => "END",
KeyCode::Home => "HOME",
KeyCode::PrintScreen => "PRINTSCREEN",
KeyCode::Insert => "INSERT",
KeyCode::Delete => "DELETE",
KeyCode::ArrowUp => "UP",
KeyCode::ArrowDown => "DOWN",
KeyCode::ArrowLeft => "LEFT",
KeyCode::ArrowRight => "RIGHT",
KeyCode::NumLock => "NUMLOCK",
KeyCode::ScrollLock => "SCROLL",
KeyCode::Equal => "+", // This is what PoB does
KeyCode::Minus => "-",
KeyCode::Comma => ",",
KeyCode::Period => ".",
KeyCode::Slash => "/",
KeyCode::NumpadAdd => "+",
KeyCode::NumpadSubtract => "-",
KeyCode::NumpadEnter => "RETURN",
KeyCode::Numpad0 => "0",
_ => 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> { pub fn str_as_mousebutton(s: &str) -> Option<MouseButton> {
Some(match s.to_uppercase().as_str() { Some(match s.to_uppercase().as_str() {
"LEFTBUTTON" => MouseButton::Left, "LEFTBUTTON" => MouseButton::Left,
@@ -257,13 +238,17 @@ pub fn str_as_mousebutton(s: &str) -> Option<MouseButton> {
}) })
} }
pub fn mousebutton_as_str(button: MouseButton) -> Option<&'static str> { /// 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 { Some(match button {
MouseButton::Left => "LEFTBUTTON", MouseButton::Left => SmolStr::new("LEFTBUTTON"),
MouseButton::Right => "RIGHTBUTTON", MouseButton::Right => SmolStr::new("RIGHTBUTTON"),
MouseButton::Middle => "MIDDLEBUTTON", MouseButton::Middle => SmolStr::new("MIDDLEBUTTON"),
MouseButton::Back => "MOUSE4", MouseButton::Back => SmolStr::new("MOUSE4"),
MouseButton::Forward => "MOUSE5", MouseButton::Forward => SmolStr::new("MOUSE5"),
_ => return None, _ => return None,
}) })
} }
+5 -4
View File
@@ -18,6 +18,7 @@ use std::{
path::PathBuf, path::PathBuf,
rc::Rc, rc::Rc,
}; };
use winit::keyboard::SmolStr;
macro_rules! ctx_accessor { macro_rules! ctx_accessor {
($field:ident: & $ty:ty) => { ($field:ident: & $ty:ty) => {
@@ -109,8 +110,8 @@ pub enum PoBEvent {
Init, Init,
Exit, Exit,
Frame, Frame,
KeyDown(&'static str, bool), KeyDown(SmolStr, bool),
KeyUp(&'static str), KeyUp(SmolStr),
Char(char), Char(char),
SubFinished { SubFinished {
id: u64, id: u64,
@@ -271,9 +272,9 @@ impl LuaInstance {
PoBEvent::Exit => get_callback(&self.lua, "OnExit")?.call::<()>(()), PoBEvent::Exit => get_callback(&self.lua, "OnExit")?.call::<()>(()),
PoBEvent::Frame => get_callback(&self.lua, "OnFrame")?.call::<()>(()), PoBEvent::Frame => get_callback(&self.lua, "OnFrame")?.call::<()>(()),
PoBEvent::KeyDown(key, double_click) => { PoBEvent::KeyDown(key, double_click) => {
get_callback(&self.lua, "OnKeyDown")?.call::<()>((key, double_click)) get_callback(&self.lua, "OnKeyDown")?.call::<()>((key.as_str(), double_click))
} }
PoBEvent::KeyUp(key) => get_callback(&self.lua, "OnKeyUp")?.call::<()>(key), PoBEvent::KeyUp(key) => get_callback(&self.lua, "OnKeyUp")?.call::<()>(key.as_str()),
PoBEvent::Char(ch) => get_callback(&self.lua, "OnChar")?.call::<()>(ch), PoBEvent::Char(ch) => get_callback(&self.lua, "OnChar")?.call::<()>(ch),
PoBEvent::SubFinished { id, return_values } => { PoBEvent::SubFinished { id, return_values } => {
get_callback(&self.lua, "OnSubFinished")?.call::<()>((id, return_values)) get_callback(&self.lua, "OnSubFinished")?.call::<()>((id, return_values))
+3 -3
View File
@@ -1,14 +1,14 @@
use crate::{ use crate::{
app::AppState, installer::InstallMode, pob::PoBMode, renderer::primitives::ClippedPrimitive, app::AppState, installer::InstallMode, pob::PoBMode, renderer::primitives::ClippedPrimitive,
}; };
use winit::{event::MouseButton, keyboard::KeyCode}; use winit::{event::MouseButton, keyboard::Key};
pub enum AppEvent { pub enum AppEvent {
KeyDown { KeyDown {
code: KeyCode, key: Key,
}, },
KeyUp { KeyUp {
code: KeyCode, key: Key,
}, },
MouseDown { MouseDown {
button: MouseButton, button: MouseButton,
+9 -9
View File
@@ -1,7 +1,7 @@
use crate::{ use crate::{
app::AppState, app::AppState,
dpi::{LogicalRect, LogicalSize}, dpi::{LogicalRect, LogicalSize},
input::{keycode_as_str, mousebutton_as_str}, input::{key_as_str, mousebutton_as_str},
layers::Layers, layers::Layers,
lua::{LuaInstance, PoBContext, PoBEvent}, lua::{LuaInstance, PoBContext, PoBEvent},
mode::{AppEvent, ModeFrameOutput, ModeTransition}, mode::{AppEvent, ModeFrameOutput, ModeTransition},
@@ -97,14 +97,14 @@ impl PoBMode {
let mut ctx = PoBContext::new(app_state, &mut self.state); let mut ctx = PoBContext::new(app_state, &mut self.state);
match event { match event {
AppEvent::KeyDown { code } => { AppEvent::KeyDown { key } => {
if let Some(key_string) = keycode_as_str(code) { if let Some(key_string) = key_as_str(key) {
let pob_event = PoBEvent::KeyDown(key_string, false); let pob_event = PoBEvent::KeyDown(key_string, false);
self.lua_instance.handle_event(pob_event, &mut ctx)?; self.lua_instance.handle_event(pob_event, &mut ctx)?;
} }
} }
AppEvent::KeyUp { code } => { AppEvent::KeyUp { key } => {
if let Some(key_string) = keycode_as_str(code) { if let Some(key_string) = key_as_str(key) {
let pob_event = PoBEvent::KeyUp(key_string); let pob_event = PoBEvent::KeyUp(key_string);
self.lua_instance.handle_event(pob_event, &mut ctx)?; self.lua_instance.handle_event(pob_event, &mut ctx)?;
} }
@@ -127,14 +127,14 @@ impl PoBMode {
AppEvent::MouseWheel { delta } => { AppEvent::MouseWheel { delta } => {
if delta > 0.0 { if delta > 0.0 {
self.lua_instance self.lua_instance
.handle_event(PoBEvent::KeyDown("WHEELUP", false), &mut ctx)?; .handle_event(PoBEvent::KeyDown("WHEELUP".into(), false), &mut ctx)?;
self.lua_instance self.lua_instance
.handle_event(PoBEvent::KeyUp("WHEELUP"), &mut ctx)?; .handle_event(PoBEvent::KeyUp("WHEELUP".into()), &mut ctx)?;
} else if delta < 0.0 { } else if delta < 0.0 {
self.lua_instance self.lua_instance
.handle_event(PoBEvent::KeyDown("WHEELDOWN", false), &mut ctx)?; .handle_event(PoBEvent::KeyDown("WHEELDOWN".into(), false), &mut ctx)?;
self.lua_instance self.lua_instance
.handle_event(PoBEvent::KeyUp("WHEELDOWN"), &mut ctx)?; .handle_event(PoBEvent::KeyUp("WHEELDOWN".into()), &mut ctx)?;
} }
} }
AppEvent::CharacterInput { ch } => { AppEvent::CharacterInput { ch } => {