Surface error notifications

This commit is contained in:
2024-12-09 23:18:42 -05:00
parent 25b88c622c
commit ae04f9f73b
4 changed files with 76 additions and 19 deletions
+35 -8
View File
@@ -11,6 +11,7 @@ use std::{
};
use anyhow::Result;
use egui_toast::{Toast, ToastKind, ToastOptions};
use crate::{audio::Audio, graphics::TextureSink};
pub use shrooms_vb_core::VBKey;
@@ -139,6 +140,7 @@ pub struct Emulator {
audio_on: Arc<[AtomicBool; 2]>,
linked: Arc<AtomicBool>,
renderers: HashMap<SimId, TextureSink>,
messages: HashMap<SimId, mpsc::Sender<Toast>>,
}
impl Emulator {
@@ -161,6 +163,7 @@ impl Emulator {
audio_on,
linked,
renderers: HashMap::new(),
messages: HashMap::new(),
})
}
@@ -330,28 +333,35 @@ impl Emulator {
fn handle_command(&mut self, command: EmulatorCommand) {
match command {
EmulatorCommand::SetRenderer(sim_id, renderer) => {
EmulatorCommand::ConnectToSim(sim_id, renderer, messages) => {
self.renderers.insert(sim_id, renderer);
self.messages.insert(sim_id, messages);
}
EmulatorCommand::LoadGame(sim_id, path) => {
if let Err(error) = self.load_cart(sim_id, &path) {
eprintln!("error loading rom: {}", error);
self.report_error(sim_id, format!("Error loading rom: {error}"));
}
}
EmulatorCommand::StartSecondSim(path) => {
if let Err(error) = self.start_second_sim(path) {
eprintln!("error starting second sim: {}", error);
self.report_error(
SimId::Player2,
format!("Error starting second sim: {error}"),
);
}
}
EmulatorCommand::StopSecondSim => {
if let Err(error) = self.stop_second_sim() {
eprintln!("error stopping second sim: {}", error);
self.report_error(
SimId::Player2,
format!("Error stopping second sim: {error}"),
);
}
}
EmulatorCommand::Pause => {
for sim_id in SimId::values() {
if let Err(error) = self.pause_sim(sim_id) {
eprintln!("error pausing: {}", error);
self.report_error(sim_id, format!("Error pausing: {error}"));
}
}
}
@@ -375,7 +385,7 @@ impl Emulator {
}
EmulatorCommand::Reset(sim_id) => {
if let Err(error) = self.reset_sim(sim_id, None) {
eprintln!("error resetting sim: {}", error);
self.report_error(sim_id, format!("Error resetting sim: {error}"));
}
}
EmulatorCommand::SetKeys(sim_id, keys) => {
@@ -386,18 +396,35 @@ impl Emulator {
EmulatorCommand::Exit(done) => {
for sim_id in SimId::values() {
if let Err(error) = self.save_sram(sim_id) {
eprintln!("error saving sram on exit: {}", error);
self.report_error(sim_id, format!("Error saving sram on exit: {error}"));
}
}
let _ = done.send(());
}
}
}
fn report_error(&self, sim_id: SimId, message: String) {
let messages = self
.messages
.get(&sim_id)
.or_else(|| self.messages.get(&SimId::Player1));
if let Some(msg) = messages {
let toast = Toast::new()
.kind(ToastKind::Error)
.options(ToastOptions::default().duration_in_seconds(5.0))
.text(&message);
if msg.send(toast).is_ok() {
return;
}
}
eprintln!("{}", message);
}
}
#[derive(Debug)]
pub enum EmulatorCommand {
SetRenderer(SimId, TextureSink),
ConnectToSim(SimId, TextureSink, mpsc::Sender<Toast>),
LoadGame(SimId, PathBuf),
StartSecondSim(Option<PathBuf>),
StopSecondSim,