Backport input fixes and stuff
This commit is contained in:
parent
b1b2b6d803
commit
dc69ce81e0
3 changed files with 174 additions and 138 deletions
|
@ -1,28 +1,56 @@
|
|||
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<Node>,
|
||||
pressed_keys: HashSet<Keycode>,
|
||||
base: Base<Object>,
|
||||
}
|
||||
|
||||
#[godot_api]
|
||||
impl BackgroundInputCapture {
|
||||
#[func]
|
||||
pub fn get_keys(&mut self) -> Vec<i32> {
|
||||
self.device_state
|
||||
.get_keys()
|
||||
pub fn get_keys(&mut self) -> Dictionary {
|
||||
let keys: HashSet<Keycode> = 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| {
|
||||
.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(
|
||||
match k {
|
||||
match key {
|
||||
Keycode::Key0 => Key::KEY_0,
|
||||
Keycode::Key1 => Key::KEY_1,
|
||||
Keycode::Key2 => Key::KEY_2,
|
||||
|
@ -138,7 +166,4 @@ impl BackgroundInputCapture {
|
|||
}
|
||||
.to_godot(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,6 +594,7 @@ func process_key_presses(keys_pressed):
|
|||
var keyStrings = []
|
||||
|
||||
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))
|
||||
|
||||
if fileSystemOpen:
|
||||
|
@ -618,7 +624,7 @@ func process_key_presses(keys_pressed):
|
|||
|
||||
|
||||
|
||||
func bgInputSprite(node, keys_pressed):
|
||||
func bgInputSprite(keys_pressed):
|
||||
if fileSystemOpen:
|
||||
return
|
||||
var keyStrings = []
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue