From c0daf3f64c1a12e86298685756b34ffdff06a90b Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 27 Mar 2025 18:35:31 -0400 Subject: [PATCH] use storage manager in gui --- auto/Cargo.lock | 85 ++++++++++++++++++++++++++++++++++------ interface/src/gui.rs | 64 +++++++++--------------------- interface/src/main.rs | 5 ++- interface/src/storage.rs | 12 ++++++ 4 files changed, 106 insertions(+), 60 deletions(-) diff --git a/auto/Cargo.lock b/auto/Cargo.lock index b34493f..60128b4 100644 --- a/auto/Cargo.lock +++ b/auto/Cargo.lock @@ -38,7 +38,7 @@ dependencies = [ "accesskit_consumer", "atspi-common", "serde", - "thiserror", + "thiserror 1.0.69", "zvariant", ] @@ -198,7 +198,7 @@ dependencies = [ "ndk-context", "ndk-sys 0.6.0+11769913", "num_enum", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -747,7 +747,7 @@ dependencies = [ "polling", "rustix 0.38.44", "slab", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -1119,6 +1119,27 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "directories" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f5094c54661b38d03bd7e50df373292118db60b585c08a411c6d840017fe7d" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.59.0", +] + [[package]] name = "dispatch" version = "0.2.0" @@ -1255,7 +1276,7 @@ dependencies = [ "epaint", "log", "profiling", - "thiserror", + "thiserror 1.0.69", "type-map", "web-time", "wgpu", @@ -2157,6 +2178,7 @@ dependencies = [ "common", "cpal", "dashmap", + "directories", "eframe", "egui-toast", "egui_extras", @@ -2212,7 +2234,7 @@ dependencies = [ "combine", "jni-sys", "log", - "thiserror", + "thiserror 1.0.69", "walkdir", "windows-sys 0.45.0", ] @@ -2490,7 +2512,7 @@ dependencies = [ "rustc-hash", "spirv", "termcolor", - "thiserror", + "thiserror 1.0.69", "unicode-xid", ] @@ -2520,7 +2542,7 @@ dependencies = [ "log", "ndk-sys 0.5.0+25.2.9519653", "num_enum", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -2535,7 +2557,7 @@ dependencies = [ "ndk-sys 0.6.0+11769913", "num_enum", "raw-window-handle", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -2934,6 +2956,12 @@ version = "1.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "orbclient" version = "0.3.48" @@ -3260,7 +3288,7 @@ dependencies = [ "rand_chacha", "simd_helpers", "system-deps", - "thiserror", + "thiserror 1.0.69", "v_frame", "wasm-bindgen", ] @@ -3324,6 +3352,17 @@ dependencies = [ "bitflags 2.9.0", ] +[[package]] +name = "redox_users" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b" +dependencies = [ + "getrandom 0.2.15", + "libredox", + "thiserror 2.0.12", +] + [[package]] name = "regex" version = "1.11.1" @@ -3628,7 +3667,7 @@ dependencies = [ "log", "memmap2", "rustix 0.38.44", - "thiserror", + "thiserror 1.0.69", "wayland-backend", "wayland-client", "wayland-csd-frame", @@ -3825,7 +3864,16 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl 2.0.12", ] [[package]] @@ -3839,6 +3887,17 @@ dependencies = [ "syn 2.0.99", ] +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.99", +] + [[package]] name = "tiff" version = "0.9.1" @@ -4396,7 +4455,7 @@ dependencies = [ "raw-window-handle", "rustc-hash", "smallvec", - "thiserror", + "thiserror 1.0.69", "wgpu-hal", "wgpu-types", ] @@ -4434,7 +4493,7 @@ dependencies = [ "renderdoc-sys", "rustc-hash", "smallvec", - "thiserror", + "thiserror 1.0.69", "wasm-bindgen", "web-sys", "wgpu-types", diff --git a/interface/src/gui.rs b/interface/src/gui.rs index 3b09761..38baf4f 100644 --- a/interface/src/gui.rs +++ b/interface/src/gui.rs @@ -3,14 +3,14 @@ use std::{cell::OnceCell, collections::HashMap, path::PathBuf, sync::{atomic::Or use common::{ControlPacket, TelemetryPacket}; use eframe::{egui::{self, containers, Align2, Checkbox, Context, IconData, Id, ImageSource, Label, Ui}, Storage}; use image::ImageFormat; -use tokio::{runtime::Runtime, sync::{mpsc, watch::Receiver}}; +use tokio::{runtime::Runtime, sync::{mpsc, watch::{self, Receiver}}}; use egui_toast::{Toast, Toasts}; -use crate::{auto::Configurable, storage_dir::storage_dir, POWER_THRESHOLD}; +use crate::{auto::Configurable, storage::StorageManager, storage_dir::storage_dir, POWER_THRESHOLD}; pub const GUI: OnceCell = OnceCell::new(); -pub fn gui(data: Receiver, toasts: mpsc::Receiver, executor: Runtime) -> eframe::Result { +pub fn gui(data: Receiver, toasts: mpsc::Receiver, executor: Runtime, autoconf: watch::Receiver<&'static [Configurable]>, storage: StorageManager) -> eframe::Result { let icon = egui::include_image!("../assets/lizard.png"); let icon = image::load_from_memory_with_format(include_bytes!("../assets/lizard.png"), ImageFormat::Png).unwrap(); @@ -35,7 +35,7 @@ pub fn gui(data: Receiver, toasts: mpsc::Receiver, executor: Run // This gives us image support: egui_extras::install_image_loaders(&cc.egui_ctx); - Ok(Box::new(GUI::with_receivers(data, toasts, executor, cc.storage.unwrap()))) + Ok(Box::new(GUI::with_receivers(data, toasts, executor, storage, autoconf))) }), ) } @@ -46,10 +46,13 @@ pub struct GUIData { pub last_command: Option, } + #[cfg(target_os = "linux")] pub const DEFAULT_VOLUME_THRESHOLD: f32 = 5.0; #[cfg(target_os = "windows")] pub const DEFAULT_VOLUME_THRESHOLD: f32 = 1.0; +const VOLUME_THRESHOLD: Configurable = Configurable::new("Volume").range(0.0..10.).default(DEFAULT_VOLUME_THRESHOLD) + .description("higher accepts less noise (better)"); const DEFAULT_TURN_GAIN: f32 = 0.3; const DEFAULT_FIRE_DISTANCE: f32 = 55.0; @@ -57,32 +60,20 @@ struct GUI { data: Receiver, toasts: mpsc::Receiver, executor: Option, - volume_threshold: f32, - auto_turn_gain: f32, - /// mm - auto_fire_distance: f32, selected_auto: usize, + autoconf: watch::Receiver<&'static [Configurable]>, + storage: StorageManager, } impl GUI { - fn with_receivers(data: Receiver, toasts: mpsc::Receiver, executor: Runtime, storage: &dyn Storage) -> Self { - let volume_threshold: f32 = storage.get_string("volume_threshold").map(|s| s.parse().ok()).flatten().unwrap_or(DEFAULT_VOLUME_THRESHOLD); - - let auto_turn_gain: f32 = storage.get_string("auto_turn_gain").map(|s| s.parse().ok()).flatten().unwrap_or(DEFAULT_TURN_GAIN); - - let auto_fire_distance: f32 = storage.get_string("auto_fire_distance").map(|s| s.parse().ok()).flatten().unwrap_or(DEFAULT_FIRE_DISTANCE); - - let selected_auto: usize = storage.get_string("selected_auto").map(|s| s.parse().ok()).flatten().unwrap_or(0); - - + fn with_receivers(data: Receiver, toasts: mpsc::Receiver, executor: Runtime, storage: StorageManager, autoconf: watch::Receiver<&'static [Configurable]>) -> Self { Self { data, toasts, executor: Some(executor), - volume_threshold, - auto_turn_gain, - auto_fire_distance, - selected_auto, + selected_auto: 0, + autoconf, + storage, } } } @@ -103,26 +94,18 @@ impl eframe::App for GUI { let _ = GUI.set(ctx.clone()); egui::SidePanel::right(Id::new("config")).resizable(false).show(ctx, |ui| { ui.heading("configuration"); - ui.add(egui::Slider::new(&mut self.volume_threshold, 0.0..=10.0).text("volume threshold")); - ui.label("higher accepts less noise (better)"); - ui.add(egui::Slider::new(&mut self.auto_turn_gain, 0.0..=1.0).text("auto turn kP")); - ui.add(egui::Slider::new(&mut self.auto_fire_distance, 30.0..=100.0).text("auto fire distance (mm)")); + + configurator(ui, &VOLUME_THRESHOLD, &self.storage); ui.horizontal(|ui| { egui::ComboBox::new("auto selector", "select auto") .show_index(ui, &mut self.selected_auto, 3, |n| ["a","b","c"][n]); ui.button("refresh").clicked(); }); - let storage = _frame.storage_mut().unwrap(); - storage.set_string("volume_threshold", format!("{}",self.volume_threshold)); - storage.set_string("auto_turn_gain", format!("{}",self.auto_turn_gain)); - storage.set_string("auto_fire_distance", format!("{}",self.auto_fire_distance)); - storage.set_string("selected_auto", format!("{}",self.selected_auto)); - - POWER_THRESHOLD.store(self.volume_threshold, Ordering::Relaxed); + POWER_THRESHOLD.store(self.storage.load(&VOLUME_THRESHOLD), Ordering::Relaxed); for conf in CONFIGS { - configurator(ui, conf, storage); + configurator(ui, conf, &self.storage); } }); @@ -159,17 +142,8 @@ impl eframe::App for GUI { } } -fn configurator(ui: &mut Ui, config: &Configurable, map: &mut dyn Storage) { - ui.add(egui::Slider::from_get_set(config.min as f64 ..= config.max as f64, |value| { - match value { - Some(value) => { - map.set_string(&config.name,format!("{value}")); - value - }, - None => map.get_string(&config.name).map(|s| s.parse().ok()).flatten().unwrap_or(config.default as f64), - } - - }).text(config.name)); +fn configurator(ui: &mut Ui, config: &'static Configurable, map: &StorageManager) { + ui.add(egui::Slider::from_get_set(config.min as f64 ..= config.max as f64, map.get_set(config)).text(config.name)); if let Some(description) = config.description { ui.label(description); } diff --git a/interface/src/main.rs b/interface/src/main.rs index 66e7c96..470ca9f 100644 --- a/interface/src/main.rs +++ b/interface/src/main.rs @@ -3,7 +3,7 @@ use std::{fmt::format, ops::ControlFlow, result, sync::{atomic::Ordering, Arc}, use anyhow::{Context, Ok, Result}; use atomic_float::AtomicF32; use directories::ProjectDirs; -use interface::{auto::{get_confs, Auto}, combatlog::{combat_logger, CombatData}, storage::StorageManager, POWER_THRESHOLD}; +use interface::{auto::{get_confs, Auto}, combatlog::{combat_logger, CombatData}, gui::CONFIGS, storage::StorageManager, POWER_THRESHOLD}; use common::{ControlPacket, TelemetryPacket}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use egui_toast::{Toast, ToastKind}; @@ -115,7 +115,8 @@ fn main() -> Result<()> { }); println!("launching gui"); - gui(gui_data, toasts, executor).unwrap(); + let (_conf_sender, conf) = watch::channel(CONFIGS); + gui(gui_data, toasts, executor, conf, storage).unwrap(); drop(stream); diff --git a/interface/src/storage.rs b/interface/src/storage.rs index 9f35dd4..0ed09e4 100644 --- a/interface/src/storage.rs +++ b/interface/src/storage.rs @@ -41,4 +41,16 @@ impl StorageManager { pub fn store(&self, setting: &'static Configurable, value: f32) { self.map.insert(setting.name.to_owned(), value); } + + pub fn get_set(&self, setting: &'static Configurable) -> impl 'static + FnMut(Option) -> f64 { + let self_ = self.clone(); + let setting_ = setting; + move |new| { + if let Some(new) = new { + self_.store(setting_, new as f32); + } + self_.load(setting_) as f64 + } + } + }