use storage manager in gui
This commit is contained in:
parent
9f4b2e57a2
commit
c0daf3f64c
3 changed files with 106 additions and 60 deletions
|
|
@ -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<Context> = OnceCell::new();
|
||||
|
||||
pub fn gui(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, executor: Runtime) -> eframe::Result {
|
||||
pub fn gui(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, 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<GUIData>, toasts: mpsc::Receiver<Toast>, 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<ControlPacket>,
|
||||
}
|
||||
|
||||
|
||||
#[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<GUIData>,
|
||||
toasts: mpsc::Receiver<Toast>,
|
||||
executor: Option<Runtime>,
|
||||
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<GUIData>, toasts: mpsc::Receiver<Toast>, 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<GUIData>, toasts: mpsc::Receiver<Toast>, 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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>) -> 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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue