Change to storing windows in a hash map

This commit is contained in:
pjht 2024-01-31 10:18:39 -06:00
parent e6872655f9
commit 708bc2ba87
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
2 changed files with 28 additions and 17 deletions

View File

@ -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<OptionWindow>,
state: EmuState,
windows: HashMap<&'static str, Box<dyn Window>>,
}
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| {

7
src/window.rs Normal file
View File

@ -0,0 +1,7 @@
use eframe::egui;
use crate::EmuState;
pub trait Window {
fn draw(&mut self, ctx: &egui::Context, state: &mut EmuState) -> bool;
}