forked from marc2332/freya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamepad_focus.rs
115 lines (105 loc) · 3.17 KB
/
gamepad_focus.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use std::thread;
use freya::prelude::*;
use freya_core::prelude::{
EventMessage,
EventName,
FreyaPlugin,
PlatformEvent,
PluginEvent,
PluginHandle,
};
use gilrs::{
EventType,
Gilrs,
};
fn main() {
launch_cfg(
app,
LaunchConfig::<()>::new().with_plugin(GamePadPlugin::default()),
)
}
#[derive(Default)]
pub struct GamePadPlugin;
impl GamePadPlugin {
pub fn listen_gamepad(handle: PluginHandle) {
thread::spawn(move || {
println!("Listening for gamepads");
let mut gilrs_instance = Gilrs::new().unwrap();
loop {
while let Some(ev) = gilrs_instance.next_event() {
match ev.event {
EventType::ButtonReleased(_, code) => {
// NOTE: You might need to tweak these codes
match code.into_u32() {
4 => {
handle.send_event_loop_event(
EventMessage::FocusPrevAccessibilityNode,
);
}
6 => {
handle.send_event_loop_event(
EventMessage::FocusNextAccessibilityNode,
);
}
13 => {
handle.send_platform_event(PlatformEvent::Keyboard {
name: EventName::KeyDown,
key: Key::Enter,
code: Code::Enter,
modifiers: Modifiers::default(),
});
}
_ => {}
}
}
_ => {}
}
}
}
});
}
}
impl FreyaPlugin for GamePadPlugin {
fn on_event(&mut self, event: &PluginEvent, handle: PluginHandle) {
match event {
PluginEvent::WindowCreated(_) => {
Self::listen_gamepad(handle);
}
_ => {}
}
}
}
fn app() -> Element {
let mut count = use_signal(|| 0);
let mut enabled = use_signal(|| true);
rsx!(
rect {
height: "fill",
width: "fill",
main_align: "center",
cross_align: "center",
Button {
onpress: move |_| count += 1,
label {
"Increase -> {count}"
}
}
Switch {
enabled: *enabled.read(),
ontoggled: move |_| {
enabled.toggle();
}
}
Button {
onpress: move |_| count -= 1,
label {
"Decrease -> {count}"
}
}
}
)
}