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,
+25 -6
View File
@@ -1,12 +1,15 @@
use std::sync::mpsc;
use crate::{
app::UserEvent,
emulator::{EmulatorClient, EmulatorCommand, SimId},
};
use egui::{
ecolor::HexColor, menu, Align2, Button, CentralPanel, Color32, Context, Frame, Layout,
Response, Sense, TopBottomPanel, Ui, Vec2, ViewportBuilder, ViewportCommand, ViewportId,
WidgetText, Window,
ecolor::HexColor, menu, Align2, Button, CentralPanel, Color32, Context, Direction, Frame,
Layout, Response, Sense, TopBottomPanel, Ui, Vec2, ViewportBuilder, ViewportCommand,
ViewportId, WidgetText, Window,
};
use egui_toast::{Toast, Toasts};
use winit::event_loop::EventLoopProxy;
use super::{
@@ -36,6 +39,7 @@ pub struct GameWindow {
display_mode: DisplayMode,
colors: [Color32; 2],
screen: Option<GameScreen>,
messages: Option<mpsc::Receiver<Toast>>,
color_picker: Option<ColorPickerState>,
}
@@ -48,6 +52,7 @@ impl GameWindow {
display_mode: DisplayMode::Anaglyph,
colors: COLOR_PRESETS[0],
screen: None,
messages: None,
color_picker: None,
}
}
@@ -257,6 +262,14 @@ impl AppWindow for GameWindow {
}
fn show(&mut self, ctx: &Context) {
let mut toasts = Toasts::new()
.anchor(Align2::LEFT_BOTTOM, (10.0, 10.0))
.direction(Direction::BottomUp);
if let Some(messages) = self.messages.as_mut() {
while let Ok(toast) = messages.try_recv() {
toasts.add(toast);
}
}
TopBottomPanel::top("menubar")
.exact_height(22.0)
.show(ctx, |ui| {
@@ -280,13 +293,19 @@ impl AppWindow for GameWindow {
ui.add(screen);
}
});
toasts.show(ctx);
}
fn on_init(&mut self, render_state: &egui_wgpu::RenderState) {
let (screen, sink) = GameScreen::init(render_state);
self.client
.send_command(EmulatorCommand::SetRenderer(self.sim_id, sink));
self.screen = Some(screen)
let (message_sink, message_source) = mpsc::channel();
self.client.send_command(EmulatorCommand::ConnectToSim(
self.sim_id,
sink,
message_sink,
));
self.screen = Some(screen);
self.messages = Some(message_source);
}
fn on_destroy(&mut self) {