diff --git a/global-input-reading/src/lib.rs b/global-input-reading/src/lib.rs index a926f47..ede5514 100644 --- a/global-input-reading/src/lib.rs +++ b/global-input-reading/src/lib.rs @@ -1,144 +1,169 @@ +use std::collections::HashSet; + use device_query::{DeviceQuery, DeviceState, Keycode}; -use godot::{global::Key, prelude::*}; +use godot::{classes::Engine, global::Key, prelude::*}; struct MyExtension; +const SINGLETON_NAME: &str = "BackgroundInputCapture"; + #[gdextension] -unsafe impl ExtensionLibrary for MyExtension {} +unsafe impl ExtensionLibrary for MyExtension { + fn on_level_init(_level: InitLevel) { + Engine::singleton() + .register_singleton(SINGLETON_NAME, &BackgroundInputCapture::new_alloc()); + } + + fn on_level_deinit(_level: InitLevel) { + Engine::singleton().unregister_singleton(SINGLETON_NAME); + } +} #[derive(GodotClass)] -#[class(init, base = Node)] +#[class(init, base = Object)] pub struct BackgroundInputCapture { device_state: DeviceState, - base: Base, + pressed_keys: HashSet, + base: Base, } #[godot_api] impl BackgroundInputCapture { #[func] - pub fn get_keys(&mut self) -> Vec { - self.device_state - .get_keys() + pub fn get_keys(&mut self) -> Dictionary { + let keys: HashSet = self.device_state.get_keys().into_iter().collect(); + let just_pressed = keys.difference(&self.pressed_keys); + let just_released = self.pressed_keys.difference(&keys); + let output = just_pressed .into_iter() - .filter_map(|k| { - Some( - match k { - Keycode::Key0 => Key::KEY_0, - Keycode::Key1 => Key::KEY_1, - Keycode::Key2 => Key::KEY_2, - Keycode::Key3 => Key::KEY_3, - Keycode::Key4 => Key::KEY_4, - Keycode::Key5 => Key::KEY_5, - Keycode::Key6 => Key::KEY_6, - Keycode::Key7 => Key::KEY_7, - Keycode::Key8 => Key::KEY_8, - Keycode::Key9 => Key::KEY_9, - Keycode::A => Key::A, - Keycode::B => Key::B, - Keycode::C => Key::C, - Keycode::D => Key::D, - Keycode::E => Key::E, - Keycode::F => Key::F, - Keycode::G => Key::G, - Keycode::H => Key::H, - Keycode::I => Key::I, - Keycode::J => Key::J, - Keycode::K => Key::K, - Keycode::L => Key::L, - Keycode::M => Key::M, - Keycode::N => Key::N, - Keycode::O => Key::O, - Keycode::P => Key::P, - Keycode::Q => Key::Q, - Keycode::R => Key::R, - Keycode::S => Key::S, - Keycode::T => Key::T, - Keycode::U => Key::U, - Keycode::V => Key::V, - Keycode::W => Key::W, - Keycode::X => Key::X, - Keycode::Y => Key::Y, - Keycode::Z => Key::Z, - Keycode::F1 => Key::F1, - Keycode::F2 => Key::F2, - Keycode::F3 => Key::F3, - Keycode::F4 => Key::F4, - Keycode::F5 => Key::F5, - Keycode::F6 => Key::F6, - Keycode::F7 => Key::F7, - Keycode::F8 => Key::F8, - Keycode::F9 => Key::F9, - Keycode::F10 => Key::F10, - Keycode::F11 => Key::F11, - Keycode::F12 => Key::F12, - Keycode::F13 => Key::F13, - Keycode::F14 => Key::F14, - Keycode::F15 => Key::F15, - Keycode::F16 => Key::F16, - Keycode::F17 => Key::F17, - Keycode::F18 => Key::F18, - Keycode::F19 => Key::F19, - Keycode::F20 => Key::F20, - Keycode::Escape => Key::ESCAPE, - Keycode::Space => Key::SPACE, - Keycode::LControl => return None, - Keycode::RControl => return None, - Keycode::LShift => return None, - Keycode::RShift => return None, - Keycode::LAlt => return None, - Keycode::RAlt => return None, - Keycode::Command => return None, - Keycode::RCommand => return None, - Keycode::LOption => return None, - Keycode::ROption => return None, - Keycode::LMeta => return None, - Keycode::RMeta => return None, - Keycode::Enter => Key::ENTER, - Keycode::Up => Key::UP, - Keycode::Down => Key::DOWN, - Keycode::Left => Key::LEFT, - Keycode::Right => Key::RIGHT, - Keycode::Backspace => Key::BACKSPACE, - Keycode::CapsLock => Key::CAPSLOCK, - Keycode::Tab => Key::TAB, - Keycode::Home => Key::HOME, - Keycode::End => Key::END, - Keycode::PageUp => Key::PAGEUP, - Keycode::PageDown => Key::PAGEDOWN, - Keycode::Insert => Key::INSERT, - Keycode::Delete => Key::DELETE, - Keycode::Numpad0 => Key::KP_0, - Keycode::Numpad1 => Key::KP_1, - Keycode::Numpad2 => Key::KP_2, - Keycode::Numpad3 => Key::KP_3, - Keycode::Numpad4 => Key::KP_4, - Keycode::Numpad5 => Key::KP_5, - Keycode::Numpad6 => Key::KP_6, - Keycode::Numpad7 => Key::KP_7, - Keycode::Numpad8 => Key::KP_8, - Keycode::Numpad9 => Key::KP_9, - Keycode::NumpadSubtract => Key::KP_SUBTRACT, - Keycode::NumpadAdd => Key::KP_ADD, - Keycode::NumpadDivide => Key::KP_DIVIDE, - Keycode::NumpadMultiply => Key::KP_MULTIPLY, - Keycode::NumpadEquals => Key::EQUAL, - Keycode::NumpadEnter => Key::KP_ENTER, - Keycode::NumpadDecimal => Key::KP_PERIOD, - Keycode::Grave => Key::QUOTELEFT, - Keycode::Minus => Key::MINUS, - Keycode::Equal => Key::EQUAL, - Keycode::LeftBracket => Key::BRACKETLEFT, - Keycode::RightBracket => Key::BRACKETRIGHT, - Keycode::BackSlash => Key::BACKSLASH, - Keycode::Semicolon => Key::SEMICOLON, - Keycode::Apostrophe => Key::APOSTROPHE, - Keycode::Comma => Key::COMMA, - Keycode::Dot => Key::PERIOD, - Keycode::Slash => Key::SLASH, - } - .to_godot(), - ) - }) - .collect() + .filter_map(|k| Some((keycode_to_key(k)?, true))) + .chain( + just_released + .into_iter() + .filter_map(|k| Some((keycode_to_key(k)?, false))), + ) + .collect(); + self.pressed_keys = keys; + output } } + +fn keycode_to_key(key: &Keycode) -> Option { + Some( + match key { + Keycode::Key0 => Key::KEY_0, + Keycode::Key1 => Key::KEY_1, + Keycode::Key2 => Key::KEY_2, + Keycode::Key3 => Key::KEY_3, + Keycode::Key4 => Key::KEY_4, + Keycode::Key5 => Key::KEY_5, + Keycode::Key6 => Key::KEY_6, + Keycode::Key7 => Key::KEY_7, + Keycode::Key8 => Key::KEY_8, + Keycode::Key9 => Key::KEY_9, + Keycode::A => Key::A, + Keycode::B => Key::B, + Keycode::C => Key::C, + Keycode::D => Key::D, + Keycode::E => Key::E, + Keycode::F => Key::F, + Keycode::G => Key::G, + Keycode::H => Key::H, + Keycode::I => Key::I, + Keycode::J => Key::J, + Keycode::K => Key::K, + Keycode::L => Key::L, + Keycode::M => Key::M, + Keycode::N => Key::N, + Keycode::O => Key::O, + Keycode::P => Key::P, + Keycode::Q => Key::Q, + Keycode::R => Key::R, + Keycode::S => Key::S, + Keycode::T => Key::T, + Keycode::U => Key::U, + Keycode::V => Key::V, + Keycode::W => Key::W, + Keycode::X => Key::X, + Keycode::Y => Key::Y, + Keycode::Z => Key::Z, + Keycode::F1 => Key::F1, + Keycode::F2 => Key::F2, + Keycode::F3 => Key::F3, + Keycode::F4 => Key::F4, + Keycode::F5 => Key::F5, + Keycode::F6 => Key::F6, + Keycode::F7 => Key::F7, + Keycode::F8 => Key::F8, + Keycode::F9 => Key::F9, + Keycode::F10 => Key::F10, + Keycode::F11 => Key::F11, + Keycode::F12 => Key::F12, + Keycode::F13 => Key::F13, + Keycode::F14 => Key::F14, + Keycode::F15 => Key::F15, + Keycode::F16 => Key::F16, + Keycode::F17 => Key::F17, + Keycode::F18 => Key::F18, + Keycode::F19 => Key::F19, + Keycode::F20 => Key::F20, + Keycode::Escape => Key::ESCAPE, + Keycode::Space => Key::SPACE, + Keycode::LControl => return None, + Keycode::RControl => return None, + Keycode::LShift => return None, + Keycode::RShift => return None, + Keycode::LAlt => return None, + Keycode::RAlt => return None, + Keycode::Command => return None, + Keycode::RCommand => return None, + Keycode::LOption => return None, + Keycode::ROption => return None, + Keycode::LMeta => return None, + Keycode::RMeta => return None, + Keycode::Enter => Key::ENTER, + Keycode::Up => Key::UP, + Keycode::Down => Key::DOWN, + Keycode::Left => Key::LEFT, + Keycode::Right => Key::RIGHT, + Keycode::Backspace => Key::BACKSPACE, + Keycode::CapsLock => Key::CAPSLOCK, + Keycode::Tab => Key::TAB, + Keycode::Home => Key::HOME, + Keycode::End => Key::END, + Keycode::PageUp => Key::PAGEUP, + Keycode::PageDown => Key::PAGEDOWN, + Keycode::Insert => Key::INSERT, + Keycode::Delete => Key::DELETE, + Keycode::Numpad0 => Key::KP_0, + Keycode::Numpad1 => Key::KP_1, + Keycode::Numpad2 => Key::KP_2, + Keycode::Numpad3 => Key::KP_3, + Keycode::Numpad4 => Key::KP_4, + Keycode::Numpad5 => Key::KP_5, + Keycode::Numpad6 => Key::KP_6, + Keycode::Numpad7 => Key::KP_7, + Keycode::Numpad8 => Key::KP_8, + Keycode::Numpad9 => Key::KP_9, + Keycode::NumpadSubtract => Key::KP_SUBTRACT, + Keycode::NumpadAdd => Key::KP_ADD, + Keycode::NumpadDivide => Key::KP_DIVIDE, + Keycode::NumpadMultiply => Key::KP_MULTIPLY, + Keycode::NumpadEquals => Key::EQUAL, + Keycode::NumpadEnter => Key::KP_ENTER, + Keycode::NumpadDecimal => Key::KP_PERIOD, + Keycode::Grave => Key::QUOTELEFT, + Keycode::Minus => Key::MINUS, + Keycode::Equal => Key::EQUAL, + Keycode::LeftBracket => Key::BRACKETLEFT, + Keycode::RightBracket => Key::BRACKETRIGHT, + Keycode::BackSlash => Key::BACKSLASH, + Keycode::Semicolon => Key::SEMICOLON, + Keycode::Apostrophe => Key::APOSTROPHE, + Keycode::Comma => Key::COMMA, + Keycode::Dot => Key::PERIOD, + Keycode::Slash => Key::SLASH, + } + .to_godot(), + ) +} diff --git a/main_scenes/main.gd b/main_scenes/main.gd index 1ceb270..21c66f2 100644 --- a/main_scenes/main.gd +++ b/main_scenes/main.gd @@ -133,7 +133,10 @@ func _ready(): camera.position = origin.position func _process(delta): - process_key_presses($BackgroundInputCapture.get_keys()) + var keys = BackgroundInputCapture.get_keys() + bgInputSprite(keys) + process_key_presses(keys) + var hold = origin.get_parent().position.y origin.get_parent().position.y += yVel * 0.0166 @@ -178,6 +181,8 @@ func isFileSystemOpen(): #Displays control panel whether or not application is focused func _notification(what): + if !is_node_ready(): + return match what: SceneTree.NOTIFICATION_APPLICATION_FOCUS_OUT: controlPanel.visible = false @@ -589,7 +594,8 @@ func process_key_presses(keys_pressed): var keyStrings = [] for i in keys_pressed: - keyStrings.append(OS.get_keycode_string(i) if !OS.get_keycode_string(i).strip_edges().is_empty() else "Keycode" + str(i)) + if keys_pressed[i]: + keyStrings.append(OS.get_keycode_string(i) if !OS.get_keycode_string(i).strip_edges().is_empty() else "Keycode" + str(i)) if fileSystemOpen: return @@ -618,7 +624,7 @@ func process_key_presses(keys_pressed): -func bgInputSprite(node, keys_pressed): +func bgInputSprite(keys_pressed): if fileSystemOpen: return var keyStrings = [] diff --git a/main_scenes/main.tscn b/main_scenes/main.tscn index c12e7d5..ba989ee 100644 --- a/main_scenes/main.tscn +++ b/main_scenes/main.tscn @@ -280,12 +280,19 @@ offset_bottom = -89.0 text = "kaiakairos'" label_settings = SubResource("LabelSettings_qg0do") -[node name="versionNo" type="Label" parent="ControlPanel/VersionLabels/Label2"] -layout_mode = 0 -offset_left = 144.0 -offset_top = 40.0 -offset_right = 282.0 -offset_bottom = 74.0 +[node name="Label3" type="Label" parent="ControlPanel/VersionLabels"] +offset_left = -516.0 +offset_top = -92.0 +offset_right = -314.0 +offset_bottom = -58.0 +text = "(fixed by Charlotte)" +label_settings = SubResource("LabelSettings_qg0do") + +[node name="versionNo" type="Label" parent="ControlPanel/VersionLabels"] +offset_left = -569.0 +offset_top = -83.0 +offset_right = -431.0 +offset_bottom = -49.0 text = "v1.4.5" label_settings = SubResource("LabelSettings_xvf50") @@ -619,8 +626,6 @@ texture = ExtResource("24_joqmn") [node name="Camera2D" type="Camera2D" parent="."] position = Vector2(360, 360) -[node name="BackgroundInputCapture" type="BackgroundInputCapture" parent="."] - [node name="PushUpdates" parent="." instance=ExtResource("30_q43vl")] z_index = 4090 position = Vector2(0, 720)