From 708bc2ba87f1bc78db1c451bb5ff4a308bd2ca27 Mon Sep 17 00:00:00 2001 From: pjht Date: Wed, 31 Jan 2024 10:18:39 -0600 Subject: [PATCH] Change to storing windows in a hash map --- src/main.rs | 38 +++++++++++++++++++++----------------- src/window.rs | 7 +++++++ 2 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 src/window.rs diff --git a/src/main.rs b/src/main.rs index 3ed9f20..6c5354d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,9 @@ mod card; mod cpu; mod frontpanel; mod ram; +mod window; -use std::sync::mpsc::Sender; +use std::{collections::HashMap, sync::mpsc::Sender}; use audio::{AudioMessage, AudioThread}; use cpu::{MemCycle, Status, I8080}; @@ -17,6 +18,7 @@ use frontpanel::Textures; use rand::RngCore; use rfd::FileDialog; use serde::{Deserialize, Serialize}; +use window::Window; use crate::frontpanel::{switch::SwitchState, Frontpanel, FrontpanelInteraction, FrontpanelState}; @@ -54,8 +56,8 @@ impl Options { struct AltairEmulator { textures: Textures, - option_window: Option, state: EmuState, + windows: HashMap<&'static str, Box>, } struct EmuState { @@ -71,7 +73,7 @@ impl EmuState { fn handle_fp_interaction(&mut self, interaction: FrontpanelInteraction) { self.audio_tx.send(AudioMessage::PlaySwitchClick).unwrap(); match interaction { - FrontpanelInteraction::ActionSwChanged(sw, state) => { + FrontpanelInteraction::ActionSwChanged(sw, state) => { if self.fp_state.power() { match sw { frontpanel::ActionSwitch::RunStop => { @@ -191,13 +193,9 @@ impl EmuState { fn update_options(&mut self, new_opts: Options) { self.options = new_opts; if self.options.mute { - self - .audio_tx - .send(AudioMessage::SetVolume(0.0)) - .unwrap(); + self.audio_tx.send(AudioMessage::SetVolume(0.0)).unwrap(); } else { - self - .audio_tx + self.audio_tx .send(AudioMessage::SetVolume(self.options.volume)) .unwrap(); } @@ -232,7 +230,6 @@ impl AltairEmulator { rand::thread_rng().fill_bytes(&mut mem); Self { textures: Textures::new(&cc.egui_ctx), - option_window: None, state: EmuState { mem, cpu, @@ -241,6 +238,7 @@ impl AltairEmulator { options, fp_state: FrontpanelState::new(), }, + windows: HashMap::new(), } } @@ -248,10 +246,16 @@ impl AltairEmulator { menu::bar(ui, |ui| { menu::menu_button(ui, "Edit", |ui| { if ui - .add_enabled(self.option_window.is_none(), Button::new("Options")) + .add_enabled( + !self.windows.contains_key("options"), + Button::new("Options"), + ) .clicked() { - self.option_window = Some(OptionWindow::new(ui.ctx(), &self.state)); + self.windows.insert( + "options", + Box::new(OptionWindow::new(ui.ctx(), &self.state)), + ); ui.close_menu(); } if ui.button("Load binary file").clicked() { @@ -315,11 +319,8 @@ impl eframe::App for AltairEmulator { self.state.run_cpu_cycle(); self.state.update_fp(); } - if let Some(option_window) = self.option_window.as_mut() { - if option_window.draw(ctx, &mut self.state) { - self.option_window = None; - } - } + self.windows + .retain(|_, window| !window.draw(ctx, &mut self.state)); if self.state.running { ctx.request_repaint(); } @@ -345,6 +346,9 @@ impl OptionWindow { category: OptionsCategory::General, } } +} + +impl Window for OptionWindow { fn draw(&mut self, ctx: &egui::Context, state: &mut EmuState) -> bool { let modal = Modal::new(ctx, "options_modal"); modal.show(|ui| { diff --git a/src/window.rs b/src/window.rs new file mode 100644 index 0000000..3aa1d83 --- /dev/null +++ b/src/window.rs @@ -0,0 +1,7 @@ +use eframe::egui; + +use crate::EmuState; + +pub trait Window { + fn draw(&mut self, ctx: &egui::Context, state: &mut EmuState) -> bool; +}