Backport input fixes and stuff

This commit is contained in:
CenTdemeern1 2025-05-01 16:39:17 +02:00
parent b1b2b6d803
commit dc69ce81e0
3 changed files with 174 additions and 138 deletions

View file

@ -1,144 +1,169 @@
use std::collections::HashSet;
use device_query::{DeviceQuery, DeviceState, Keycode}; use device_query::{DeviceQuery, DeviceState, Keycode};
use godot::{global::Key, prelude::*}; use godot::{classes::Engine, global::Key, prelude::*};
struct MyExtension; struct MyExtension;
const SINGLETON_NAME: &str = "BackgroundInputCapture";
#[gdextension] #[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)] #[derive(GodotClass)]
#[class(init, base = Node)] #[class(init, base = Object)]
pub struct BackgroundInputCapture { pub struct BackgroundInputCapture {
device_state: DeviceState, device_state: DeviceState,
base: Base<Node>, pressed_keys: HashSet<Keycode>,
base: Base<Object>,
} }
#[godot_api] #[godot_api]
impl BackgroundInputCapture { impl BackgroundInputCapture {
#[func] #[func]
pub fn get_keys(&mut self) -> Vec<i32> { pub fn get_keys(&mut self) -> Dictionary {
self.device_state let keys: HashSet<Keycode> = self.device_state.get_keys().into_iter().collect();
.get_keys() let just_pressed = keys.difference(&self.pressed_keys);
let just_released = self.pressed_keys.difference(&keys);
let output = just_pressed
.into_iter() .into_iter()
.filter_map(|k| { .filter_map(|k| Some((keycode_to_key(k)?, true)))
Some( .chain(
match k { just_released
Keycode::Key0 => Key::KEY_0, .into_iter()
Keycode::Key1 => Key::KEY_1, .filter_map(|k| Some((keycode_to_key(k)?, false))),
Keycode::Key2 => Key::KEY_2, )
Keycode::Key3 => Key::KEY_3, .collect();
Keycode::Key4 => Key::KEY_4, self.pressed_keys = keys;
Keycode::Key5 => Key::KEY_5, output
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()
} }
} }
fn keycode_to_key(key: &Keycode) -> Option<i32> {
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(),
)
}

View file

@ -133,7 +133,10 @@ func _ready():
camera.position = origin.position camera.position = origin.position
func _process(delta): 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 var hold = origin.get_parent().position.y
origin.get_parent().position.y += yVel * 0.0166 origin.get_parent().position.y += yVel * 0.0166
@ -178,6 +181,8 @@ func isFileSystemOpen():
#Displays control panel whether or not application is focused #Displays control panel whether or not application is focused
func _notification(what): func _notification(what):
if !is_node_ready():
return
match what: match what:
SceneTree.NOTIFICATION_APPLICATION_FOCUS_OUT: SceneTree.NOTIFICATION_APPLICATION_FOCUS_OUT:
controlPanel.visible = false controlPanel.visible = false
@ -589,7 +594,8 @@ func process_key_presses(keys_pressed):
var keyStrings = [] var keyStrings = []
for i in keys_pressed: 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: if fileSystemOpen:
return return
@ -618,7 +624,7 @@ func process_key_presses(keys_pressed):
func bgInputSprite(node, keys_pressed): func bgInputSprite(keys_pressed):
if fileSystemOpen: if fileSystemOpen:
return return
var keyStrings = [] var keyStrings = []

View file

@ -280,12 +280,19 @@ offset_bottom = -89.0
text = "kaiakairos'" text = "kaiakairos'"
label_settings = SubResource("LabelSettings_qg0do") label_settings = SubResource("LabelSettings_qg0do")
[node name="versionNo" type="Label" parent="ControlPanel/VersionLabels/Label2"] [node name="Label3" type="Label" parent="ControlPanel/VersionLabels"]
layout_mode = 0 offset_left = -516.0
offset_left = 144.0 offset_top = -92.0
offset_top = 40.0 offset_right = -314.0
offset_right = 282.0 offset_bottom = -58.0
offset_bottom = 74.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" text = "v1.4.5"
label_settings = SubResource("LabelSettings_xvf50") label_settings = SubResource("LabelSettings_xvf50")
@ -619,8 +626,6 @@ texture = ExtResource("24_joqmn")
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(360, 360) position = Vector2(360, 360)
[node name="BackgroundInputCapture" type="BackgroundInputCapture" parent="."]
[node name="PushUpdates" parent="." instance=ExtResource("30_q43vl")] [node name="PushUpdates" parent="." instance=ExtResource("30_q43vl")]
z_index = 4090 z_index = 4090
position = Vector2(0, 720) position = Vector2(0, 720)