Remember whether game is muted

This commit is contained in:
2025-11-23 13:37:35 -05:00
parent 8dd64598ed
commit e1d08672fb
6 changed files with 92 additions and 66 deletions
+14 -56
View File
@@ -5,6 +5,7 @@ use std::{
use crate::{
app::UserEvent,
config::{COLOR_PRESETS, SimConfig},
emulator::{EmulatorClient, EmulatorCommand, EmulatorState, SimId, SimState},
input::{Command, ShortcutProvider},
persistence::Persistence,
@@ -16,7 +17,6 @@ use egui::{
ViewportBuilder, ViewportCommand, ViewportId, Window,
};
use egui_notify::{Anchor, Toast, Toasts};
use serde::{Deserialize, Serialize};
use winit::event_loop::EventLoopProxy;
use super::{
@@ -25,28 +25,13 @@ use super::{
utils::UiExt as _,
};
const COLOR_PRESETS: [[Color32; 2]; 3] = [
[
Color32::from_rgb(0xff, 0x00, 0x00),
Color32::from_rgb(0x00, 0xc6, 0xf0),
],
[
Color32::from_rgb(0x00, 0xb4, 0x00),
Color32::from_rgb(0xc8, 0x00, 0xff),
],
[
Color32::from_rgb(0xb4, 0x9b, 0x00),
Color32::from_rgb(0x00, 0x00, 0xff),
],
];
pub struct GameWindow {
client: EmulatorClient,
proxy: EventLoopProxy<UserEvent>,
persistence: Persistence,
shortcuts: ShortcutProvider,
sim_id: SimId,
config: GameConfig,
config: SimConfig,
toasts: Toasts,
screen: Option<GameScreen>,
messages: Option<mpsc::Receiver<Toast>>,
@@ -62,7 +47,7 @@ impl GameWindow {
shortcuts: ShortcutProvider,
sim_id: SimId,
) -> Self {
let config = load_config(&persistence, sim_id);
let config = SimConfig::load(&persistence, sim_id);
let toasts = Toasts::new()
.with_anchor(Anchor::BottomLeft)
.with_margin((10.0, 10.0).into())
@@ -410,15 +395,15 @@ impl GameWindow {
});
});
ui.menu_button("Audio", |ui| {
let p1_enabled = self.client.is_audio_enabled(SimId::Player1);
let p2_enabled = self.client.is_audio_enabled(SimId::Player2);
if ui.selectable_button(p1_enabled, "Player 1").clicked() {
self.client
.send_command(EmulatorCommand::SetAudioEnabled(!p1_enabled, p2_enabled));
}
if ui.selectable_button(p2_enabled, "Player 2").clicked() {
self.client
.send_command(EmulatorCommand::SetAudioEnabled(p1_enabled, !p2_enabled));
if ui
.selectable_button(self.config.audio_enabled, "Enabled")
.clicked()
{
self.update_config(|c| c.audio_enabled = !c.audio_enabled);
self.client.send_command(EmulatorCommand::SetAudioEnabled(
self.sim_id,
self.config.audio_enabled,
));
}
});
ui.menu_button("Input", |ui| {
@@ -460,13 +445,11 @@ impl GameWindow {
}
}
fn update_config(&mut self, update: impl FnOnce(&mut GameConfig)) {
fn update_config(&mut self, update: impl FnOnce(&mut SimConfig)) {
let mut new_config = self.config.clone();
update(&mut new_config);
if self.config != new_config {
let _ = self
.persistence
.save_config(config_filename(self.sim_id), &new_config);
let _ = new_config.save(&self.persistence, self.sim_id);
}
self.config = new_config;
}
@@ -480,24 +463,6 @@ impl GameWindow {
}
}
fn config_filename(sim_id: SimId) -> &'static str {
match sim_id {
SimId::Player1 => "config_p1",
SimId::Player2 => "config_p2",
}
}
fn load_config(persistence: &Persistence, sim_id: SimId) -> GameConfig {
if let Ok(config) = persistence.load_config(config_filename(sim_id)) {
return config;
}
GameConfig {
display_mode: DisplayMode::Anaglyph,
colors: COLOR_PRESETS[0],
dimensions: DisplayMode::Anaglyph.proportions() + Vec2::new(0.0, 22.0),
}
}
impl AppWindow for GameWindow {
fn viewport_id(&self) -> ViewportId {
match self.sim_id {
@@ -580,10 +545,3 @@ struct ColorPickerState {
just_opened: bool,
unpause_on_close: bool,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
struct GameConfig {
display_mode: DisplayMode,
colors: [Color32; 2],
dimensions: Vec2,
}