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,28 +1,56 @@
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)))
.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<i32> {
Some( Some(
match k { match key {
Keycode::Key0 => Key::KEY_0, Keycode::Key0 => Key::KEY_0,
Keycode::Key1 => Key::KEY_1, Keycode::Key1 => Key::KEY_1,
Keycode::Key2 => Key::KEY_2, Keycode::Key2 => Key::KEY_2,
@ -138,7 +166,4 @@ impl BackgroundInputCapture {
} }
.to_godot(), .to_godot(),
) )
})
.collect()
}
} }

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,6 +594,7 @@ func process_key_presses(keys_pressed):
var keyStrings = [] var keyStrings = []
for i in keys_pressed: for i in keys_pressed:
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)) 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:
@ -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)