switched to using tauri for gamepad input

This commit is contained in:
Nickiel12 2024-09-21 03:43:04 +00:00
parent bac744d506
commit ee7d0b96c6
6 changed files with 81 additions and 6 deletions

View file

@ -53,6 +53,7 @@ pub async fn joystick_loop(tx: Sender<ApplicationEvent>, is_alive: Arc<AtomicBoo
// get the next event, and if it is an axis we are interested in, update the
// corresponding variable
while let Some(evt) = gilrs.next_event().filter_ev(&UnknownSlayer {}, &mut gilrs) {
info!("got a new joystick event");
match evt.event {
gilrs::EventType::AxisChanged(gilrs::Axis::LeftStickY, val, _) => {
curr_y = (val * 100.0) as i32;

View file

@ -21,6 +21,7 @@ use satellite_connection::SatelliteConnection;
pub enum ApplicationEvent {
SupportsWebRTC(bool),
WebRTCMessage((String, vcs_common::ApplicationMessage)),
ChangeTargetSatellite(String),
JoystickMove(Point),
RetryDisconnectedSatellites,
Close,
@ -90,7 +91,10 @@ impl AppState {
let new_names = self
.camera_satellites
.iter()
.map(|x| SatelliteName { name: x.name.clone(), is_connected: x.is_connected() })
.map(|x| SatelliteName {
name: x.name.clone(),
is_connected: x.is_connected(),
})
.collect::<Vec<SatelliteName>>();
send_ui_message(
"satellite_names".to_owned(),
@ -99,6 +103,7 @@ impl AppState {
}
pub async fn check_alive_things(&mut self) {
/*
if !self
.joystick_task_is_alive
.load(std::sync::atomic::Ordering::SeqCst)
@ -108,6 +113,7 @@ impl AppState {
Arc::clone(&self.joystick_task_is_alive),
));
}
*/
let mut resend_names = false;
for i in self
@ -169,6 +175,16 @@ pub async fn run_main_event_loop(
ApplicationEvent::Close => {
state.mec.close(); // cleanup is handled on reading from a closed mec
}
ApplicationEvent::ChangeTargetSatellite(new_target_name) => {
state.target_satellite = state
.camera_satellites
.iter()
.position(|x| x.name == new_target_name);
info!(
"Changed active satellite index to: {:?}",
state.target_satellite
);
}
ApplicationEvent::RetryDisconnectedSatellites => {
state.camera_satellites.iter_mut().for_each(|x| {
info!("Resetting connections");
@ -205,9 +221,15 @@ pub async fn run_main_event_loop(
}
}
}
ApplicationEvent::JoystickMove(coord) => {
ApplicationEvent::JoystickMove(mut coord) => {
if coord.x < 10 && coord.x > -10 {
coord.x = 0;
}
if coord.y < 10 && coord.y > -10 {
coord.y = 0;
}
if let Some(target) = state.target_satellite {
if state.camera_satellites.len() < target {
if state.camera_satellites.len() >= target {
if let Err(e) = state.camera_satellites[target]
.send(ApplicationMessage::ManualMovementOverride((
coord.x, coord.y,
@ -218,6 +240,7 @@ pub async fn run_main_event_loop(
}
} else {
error!("That was not a valid target! Need to notifiy the UI about this");
debug!("Count of camera satellites: {}, target: {:?}", state.camera_satellites.len(), target);
}
}
}

View file

@ -76,7 +76,9 @@ fn main() {
})
.invoke_handler(tauri::generate_handler![
tauri_functions::connect_to_camera,
tauri_functions::supports_webrtc
tauri_functions::supports_webrtc,
tauri_functions::change_target_satellite,
tauri_functions::send_joystick_event,
])
.setup(|app| {
*APP_HANDLE.lock().unwrap() = Some(app.handle());

View file

@ -3,7 +3,7 @@ use tauri::State;
use tokio::runtime::Handle;
use tracing::info;
use crate::coordinator::ApplicationEvent;
use crate::coordinator::{ApplicationEvent, Point};
pub struct TauriState {
pub to_mec: Sender<ApplicationEvent>,
@ -25,3 +25,22 @@ pub fn supports_webrtc(has_support: bool, state: State<'_, TauriState>) {
let _ = mec.send_blocking(ApplicationEvent::SupportsWebRTC(has_support));
});
}
#[tauri::command(rename_all = "snake_case")]
pub fn change_target_satellite(new_target_name: String, state: State<'_, TauriState>) {
let mec = state.to_mec.clone();
state.rt.spawn(async move {
let _ = mec.send_blocking(ApplicationEvent::ChangeTargetSatellite(new_target_name));
});
}
#[tauri::command(rename_all = "snake_case")]
pub fn send_joystick_event(x: f32, y: f32, state: State<'_, TauriState>) {
let mec = state.to_mec.clone();
state.rt.spawn(async move {
let _ = mec.send_blocking(ApplicationEvent::JoystickMove(Point {
x: (x * 100.0) as i32,
y: (y * 100.0) as i32,
}));
});
}

View file

@ -2,6 +2,7 @@ import { invoke, event } from "./node_modules/@tauri-apps/api/index.js";
async function setup_listeners() {
await listen_for_new_satellites();
start_joystick_callback();
}
async function listen_for_new_satellites() {
@ -33,6 +34,36 @@ async function listen_for_new_satellites() {
out_ul.appendChild(li);
feather.replace();
})
register_list_elements();
});
}
function register_list_elements() {
Array.from(document.getElementById("connections_list").children).forEach((li) => {
const target_name = li.querySelector("span").innerText;
li.addEventListener("click", () => {
invoke("change_target_satellite", { new_target_name: target_name })
.catch((e) => console.log(e));
});
});
}
const denoise_level = 0.01;
function start_joystick_callback() {
window.addEventListener("gamepadconnected", (e) => {
setInterval(() => {
var cur_obj = {};
const gp = navigator.getGamepads()[e.gamepad.index];
if (Math.abs(gp.axes[0]) > denoise_level || Math.abs(gp.axes[1]) > denoise_level) {
invoke("send_joystick_event", {x: gp.axes[0], y: gp.axes[1]});
}
// THIS DISABLES THE RIGHT STICK
// if (Math.abs(gp.axes[2]) > denoise_level || Math.abs(gp.axes[3]) > denoise_level) {
// invoke("send_joystick_event", {x: gp.axes[2], y: gp.axes[3]});
//}
}, 100);
});
}

View file

@ -43,5 +43,4 @@ async function init() {
}
}
export { init };