Skip to content

Commit

Permalink
add "start scanning on startup" + make "enable" button larger
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadlock0133 committed Dec 11, 2022
1 parent 3692178 commit 6d69e13
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 61 deletions.
154 changes: 93 additions & 61 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use buttplug::{
use clap::Parser;
use eframe::{
egui::{
self, Button, Color32, ProgressBar, RichText, Slider, TextFormat, Ui,
Visuals, Window,
self, Button, Color32, ProgressBar, RichText, SelectableLabel, Slider,
TextFormat, Ui, Visuals, Window,
},
epaint::text::LayoutJob,
CreationContext, Storage,
Expand Down Expand Up @@ -60,7 +60,6 @@ struct DeviceProps {
multiplier: f32,
min: f32,
max: f32,
show_vibrators: bool,
vibrators: Vec<VibratorProps>,
}

Expand Down Expand Up @@ -125,7 +124,6 @@ impl DeviceProps {
multiplier: 1.0,
min: 0.0,
max: 1.0,
show_vibrators: false,
vibrators,
}
}
Expand Down Expand Up @@ -202,13 +200,18 @@ impl GuiApp {
capture_thread(current_sound_power2, low_pass_freq)
});

let is_scanning = settings.start_scanning_on_startup;
if is_scanning {
runtime.spawn(client.start_scanning());
}

GuiApp {
runtime,
client,
devices,
current_sound_power,
_capture_thread,
is_scanning: false,
is_scanning,
show_settings: false,
settings,
}
Expand Down Expand Up @@ -333,17 +336,33 @@ impl eframe::App for GuiApp {
device_widget(ui, device, props, sound_power, &self.runtime);
}
});
Window::new("Settings")
.open(&mut self.show_settings)
.resizable(false)
.collapsible(false)
.show(ctx, |ui| {
ui.checkbox(&mut self.settings.use_dark_mode, "Use dark mode");
});
settings_window_widget(
ctx,
&mut self.show_settings,
&mut self.settings,
);
ctx.request_repaint();
}
}

fn settings_window_widget(
ctx: &egui::Context,
show_settings: &mut bool,
settings: &mut Settings,
) {
Window::new("Settings")
.open(show_settings)
.resizable(false)
.collapsible(false)
.show(ctx, |ui| {
ui.checkbox(&mut settings.use_dark_mode, "Use dark mode");
ui.checkbox(
&mut settings.start_scanning_on_startup,
"Start scanning on startup",
);
});
}

struct VibratorProps {
is_enabled: bool,
multiplier: f32,
Expand All @@ -369,7 +388,6 @@ fn device_widget(
sound_power: f32,
runtime: &Runtime,
) {
// TODO: Add per-vibrator settings
ui.group(|ui| {
if cfg!(debug_assertions) {
ui.label(format!("({}) {}", device.index(), device.name()));
Expand All @@ -384,66 +402,80 @@ fn device_widget(
let (speed, cutoff) = props.calculate_visual_output(sound_power);

ui.horizontal(|ui| {
ui.label(format!("{:.2}%", speed * 100.0));
if cutoff {
ui.visuals_mut().selection.bg_fill = Color32::RED;
}
if !props.is_enabled {
ui.visuals_mut().selection.bg_fill = Color32::GRAY;
}
ui.add(ProgressBar::new(speed));
});
ui.horizontal_wrapped(|ui| {
if ui.selectable_label(props.is_enabled, "Enable").clicked() {
props.is_enabled = !props.is_enabled;
if !props.is_enabled {
runtime.spawn(device.stop());
}
}
ui.label("Multiplier: ");
ui.add(Slider::new(&mut props.multiplier, 0.0..=20.0));
ui.label("Minimum (cut-off): ");
ui.add(Slider::new(&mut props.min, 0.0..=1.0));
ui.label("Maximum: ");
ui.add(Slider::new(&mut props.max, 0.0..=1.0));
});
ui.toggle_value(&mut props.show_vibrators, "Vibrators");
if props.show_vibrators {
let label = if props.is_enabled {
"Enabled"
} else {
"Enable"
};
let enable_button = SelectableLabel::new(props.is_enabled, label);
ui.group(|ui| {
for (i, vibe) in props.vibrators.iter_mut().enumerate() {
vibrator_widget(ui, i, vibe);
if ui.add_sized([60.0, 60.0], enable_button).clicked() {
props.is_enabled = !props.is_enabled;
if !props.is_enabled {
runtime.spawn(device.stop());
}
}
});
}
if props.is_enabled {
let speed = props.calculate_output(sound_power);
let speed_cmd = VibrateCommand::SpeedVec(
props
.vibrators
.iter()
.map(|v| {
if v.is_enabled {
(speed * v.multiplier)
.clamp(0.0, v.max)
.min_cutoff(v.min)
as f64
} else {
0.0
ui.vertical(|ui| {
ui.horizontal(|ui| {
ui.label(format!("{:.2}%", speed * 100.0));
if cutoff {
ui.visuals_mut().selection.bg_fill = Color32::RED;
}
if !props.is_enabled {
ui.visuals_mut().selection.bg_fill = Color32::GRAY;
}
ui.add(ProgressBar::new(speed));
});
ui.horizontal_wrapped(|ui| {
ui.label("Multiplier: ");
ui.add(Slider::new(&mut props.multiplier, 0.0..=20.0));
ui.label("Minimum (cut-off): ");
ui.add(Slider::new(&mut props.min, 0.0..=1.0));
ui.label("Maximum: ");
ui.add(Slider::new(&mut props.max, 0.0..=1.0));
});
ui.collapsing("Vibrators", |ui| {
ui.group(|ui| {
for (i, vibe) in props.vibrators.iter_mut().enumerate()
{
vibrator_widget(ui, i, vibe);
}
})
.collect(),
);
runtime.spawn(device.vibrate(&speed_cmd));
}
});
});
if props.is_enabled {
let speed = props.calculate_output(sound_power);
let speed_cmd = VibrateCommand::SpeedVec(
props
.vibrators
.iter()
.map(|v| {
if v.is_enabled {
(speed * v.multiplier)
.clamp(0.0, v.max)
.min_cutoff(v.min)
as f64
} else {
0.0
}
})
.collect(),
);
runtime.spawn(device.vibrate(&speed_cmd));
}
})
})
});
}

fn vibrator_widget(ui: &mut Ui, index: usize, vibe: &mut VibratorProps) {
ui.horizontal_wrapped(|ui| {
ui.label(format!("Vibe {index}: "));
if ui.selectable_label(vibe.is_enabled, "Enable").clicked() {
let label = if vibe.is_enabled { "Enabled" } else { "Enable" };
if ui.selectable_label(vibe.is_enabled, label).clicked() {
vibe.is_enabled = !vibe.is_enabled;
}

ui.label("Multiplier: ");
ui.add(Slider::new(&mut vibe.multiplier, 0.0..=5.0));
ui.label("Minimum (cut-off): ");
Expand Down
13 changes: 13 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Settings {
pub main_volume: f32,
pub low_pass_freq: SharedF32,
pub use_dark_mode: bool,
pub start_scanning_on_startup: bool,
}

impl Default for Settings {
Expand All @@ -15,6 +16,7 @@ impl Default for Settings {
main_volume: defaults::MAIN_VOLUME,
low_pass_freq: SharedF32::new(defaults::LOW_PASS_FREQ),
use_dark_mode: defaults::DARK_MODE,
start_scanning_on_startup: defaults::START_SCANNING_ON_STARTUP,
}
}
}
Expand All @@ -23,11 +25,13 @@ mod names {
pub const MAIN_VOLUME: &str = "main_volume";
pub const LOW_PASS_FREQ: &str = "low_pass_freq";
pub const DARK_MODE: &str = "dark_mode";
pub const START_SCANNING_ON_STARTUP: &str = "start_scanning_on_startup";
}
mod defaults {
pub const MAIN_VOLUME: f32 = 1.0;
pub const LOW_PASS_FREQ: f32 = 20_000.0;
pub const DARK_MODE: bool = true;
pub const START_SCANNING_ON_STARTUP: bool = false;
}

impl Settings {
Expand All @@ -38,16 +42,25 @@ impl Settings {
.unwrap_or(defaults::LOW_PASS_FREQ);
let use_dark_mode =
get_value(storage, names::DARK_MODE).unwrap_or(defaults::DARK_MODE);
let start_scanning_on_startup =
get_value(storage, names::START_SCANNING_ON_STARTUP)
.unwrap_or(defaults::START_SCANNING_ON_STARTUP);
Self {
main_volume,
low_pass_freq: SharedF32::new(low_pass_freq),
use_dark_mode,
start_scanning_on_startup,
}
}

pub fn save(&self, storage: &mut dyn Storage) {
set_value(storage, names::MAIN_VOLUME, &self.main_volume);
set_value(storage, names::LOW_PASS_FREQ, &self.low_pass_freq.load());
set_value(storage, names::DARK_MODE, &self.use_dark_mode);
set_value(
storage,
names::START_SCANNING_ON_STARTUP,
&self.start_scanning_on_startup,
);
}
}

0 comments on commit 6d69e13

Please sign in to comment.