Skip to content

Commit

Permalink
feat: add UI persistence. closes #257 (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukexor authored Jun 2, 2024
1 parent 8c7f6df commit 4c861f7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
23 changes: 21 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion tetanes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ crossbeam = "0.8"
# TODO: Remove once https://github.com/emilk/egui/pull/4372 is released
color-hex = "0.2"
dirs.workspace = true
egui = { version = "0.27", features = ["extra_debug_asserts", "log"] }
egui = { version = "0.27", features = [
"extra_debug_asserts",
"log",
"persistence",
] }
egui-wgpu = { version = "0.27", features = ["winit", "wayland", "x11"] }
egui_extras = { version = "0.27", default-features = false, features = [
"image",
Expand Down
3 changes: 3 additions & 0 deletions tetanes/src/nes/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ impl Running {
#[cfg(feature = "profiling")]
puffin::set_scopes_on(false);

if let Err(err) = self.renderer.save() {
error!("failed to save rendererer state: {err:?}");
}
self.renderer.destroy();

if let Err(err) = self.cfg.save() {
Expand Down
33 changes: 32 additions & 1 deletion tetanes/src/nes/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
platform::{self, BuilderExt},
thread,
};
use anyhow::Context;
use egui::{
ahash::HashMap, DeferredViewportUiCallback, ImmediateViewport, SystemTheme, Vec2,
ViewportBuilder, ViewportClass, ViewportCommand, ViewportId, ViewportIdMap, ViewportIdPair,
Expand All @@ -21,6 +22,7 @@ use egui_winit::EventResponse;
use parking_lot::Mutex;
use std::{cell::RefCell, collections::hash_map::Entry, rc::Rc, sync::Arc};
use tetanes_core::{
fs,
ppu::Ppu,
time::{Duration, Instant},
video::Frame,
Expand All @@ -29,7 +31,7 @@ use thingbuf::{
mpsc::{blocking::Receiver as BufReceiver, errors::TryRecvError},
Recycle,
};
use tracing::{debug, error, warn};
use tracing::{debug, error, info, warn};
use winit::{
event::WindowEvent,
event_loop::{EventLoopProxy, EventLoopWindowTarget},
Expand Down Expand Up @@ -257,6 +259,10 @@ impl Renderer {
});
}

if let Err(err) = Self::load(&ctx) {
tracing::error!("{err:?}");
}

Ok(Self {
state,
frame_rx,
Expand Down Expand Up @@ -632,6 +638,31 @@ impl Renderer {
self.gui.error = Some(err.to_string());
}

pub fn load(ctx: &egui::Context) -> anyhow::Result<()> {
let path = Config::default_config_dir().join("gui.dat");
if fs::exists(&path) {
let data = fs::load_raw(path).context("failed to load gui memory")?;
let memory = bincode::deserialize(&data).context("failed to deserialize gui memory")?;
ctx.memory_mut(|mem| {
*mem = memory;
});
info!("Loaded UI state");
}
Ok(())
}

pub fn save(&self) -> anyhow::Result<()> {
let path = Config::default_config_dir().join("gui.dat");
self.ctx.memory(|mem| {
let data = bincode::serialize(&mem).context("failed to serialize gui memory")?;
fs::save_raw(path, &data).context("failed to save gui memory")
})?;

info!("Saved UI state");

Ok(())
}

pub fn create_window(
event_loop: &EventLoopWindowTarget<NesEvent>,
ctx: &egui::Context,
Expand Down

0 comments on commit 4c861f7

Please sign in to comment.