Save/load more config

This commit is contained in:
2024-12-14 23:14:13 -05:00
parent 6e2d70abd7
commit e31269368d
5 changed files with 148 additions and 52 deletions
+103 -47
View File
@@ -3,6 +3,7 @@ use std::sync::mpsc;
use crate::{
app::UserEvent,
emulator::{EmulatorClient, EmulatorCommand, SimId},
persistence::Persistence,
};
use egui::{
ecolor::HexColor, menu, Align2, Button, CentralPanel, Color32, Context, Direction, Frame,
@@ -10,6 +11,7 @@ use egui::{
ViewportId, WidgetText, Window,
};
use egui_toast::{Toast, Toasts};
use serde::{Deserialize, Serialize};
use winit::event_loop::EventLoopProxy;
use super::{
@@ -35,22 +37,28 @@ const COLOR_PRESETS: [[Color32; 2]; 3] = [
pub struct GameWindow {
client: EmulatorClient,
proxy: EventLoopProxy<UserEvent>,
persistence: Persistence,
sim_id: SimId,
display_mode: DisplayMode,
colors: [Color32; 2],
config: GameConfig,
screen: Option<GameScreen>,
messages: Option<mpsc::Receiver<Toast>>,
color_picker: Option<ColorPickerState>,
}
impl GameWindow {
pub fn new(client: EmulatorClient, proxy: EventLoopProxy<UserEvent>, sim_id: SimId) -> Self {
pub fn new(
client: EmulatorClient,
proxy: EventLoopProxy<UserEvent>,
persistence: Persistence,
sim_id: SimId,
) -> Self {
let config = load_config(&persistence, sim_id);
Self {
client,
proxy,
persistence,
sim_id,
display_mode: DisplayMode::Anaglyph,
colors: COLOR_PRESETS[0],
config,
screen: None,
messages: None,
color_picker: None,
@@ -99,7 +107,7 @@ impl GameWindow {
let label = format!("x{scale}");
let scale = scale as f32;
let dims = {
let Vec2 { x, y } = self.display_mode.proportions();
let Vec2 { x, y } = self.config.display_mode.proportions();
Vec2::new(x * scale, y * scale + 22.0)
};
if ui
@@ -113,49 +121,46 @@ impl GameWindow {
});
ui.menu_button("Display Mode", |ui| {
let old_proportions = self.display_mode.proportions();
if ui
.selectable_option(&mut self.display_mode, DisplayMode::Anaglyph, "Anaglyph")
.clicked()
{
ui.close_menu();
}
if ui
.selectable_option(&mut self.display_mode, DisplayMode::LeftEye, "Left Eye")
.clicked()
{
ui.close_menu();
}
if ui
.selectable_option(&mut self.display_mode, DisplayMode::RightEye, "Right Eye")
.clicked()
{
ui.close_menu();
}
if ui
.selectable_option(
&mut self.display_mode,
DisplayMode::SideBySide,
"Side by Side",
)
.clicked()
{
ui.close_menu();
let old_proportions = self.config.display_mode.proportions();
let mut changed = false;
let mut display_mode = self.config.display_mode;
changed |= ui
.selectable_option(&mut display_mode, DisplayMode::Anaglyph, "Anaglyph")
.clicked();
changed |= ui
.selectable_option(&mut display_mode, DisplayMode::LeftEye, "Left Eye")
.clicked();
changed |= ui
.selectable_option(&mut display_mode, DisplayMode::RightEye, "Right Eye")
.clicked();
changed |= ui
.selectable_option(&mut display_mode, DisplayMode::SideBySide, "Side by Side")
.clicked();
if !changed {
return;
}
let new_proportions = self.display_mode.proportions();
let current_dims = {
let viewport = ctx.input(|i| i.viewport().inner_rect.unwrap());
viewport.max - viewport.min
};
let new_proportions = display_mode.proportions();
let scale = new_proportions / old_proportions;
if scale != Vec2::new(1.0, 1.0) {
let current_dims = ctx.input(|i| i.viewport().inner_rect.unwrap());
let current_dims = current_dims.max - current_dims.min;
ctx.send_viewport_cmd(ViewportCommand::InnerSize(current_dims * scale));
}
self.update_config(|c| {
c.display_mode = display_mode;
c.dimensions = current_dims * scale;
});
ui.close_menu();
});
ui.menu_button("Colors", |ui| {
for preset in COLOR_PRESETS {
if ui.color_pair_button(preset[0], preset[1]).clicked() {
self.colors = preset;
self.update_config(|c| c.colors = preset);
ui.close_menu();
}
}
@@ -168,8 +173,12 @@ impl GameWindow {
if is_running {
self.client.send_command(EmulatorCommand::Pause);
}
let color_codes = [
color_str(self.config.colors[0]),
color_str(self.config.colors[1]),
];
self.color_picker = Some(ColorPickerState {
color_codes: [color_str(self.colors[0]), color_str(self.colors[1])],
color_codes,
just_opened: true,
unpause_on_close: is_running,
});
@@ -223,18 +232,21 @@ impl GameWindow {
}
fn show_color_picker(&mut self, ui: &mut Ui) {
let mut colors = self.config.colors;
let Some(state) = self.color_picker.as_mut() else {
return;
};
let open = ui
let (open, updated) = ui
.horizontal(|ui| {
let left_color = ui.color_picker(&mut self.colors[0], &mut state.color_codes[0]);
let left_color = ui.color_picker(&mut colors[0], &mut state.color_codes[0]);
if state.just_opened {
left_color.request_focus();
state.just_opened = false;
}
let right_color = ui.color_picker(&mut self.colors[1], &mut state.color_codes[1]);
left_color.has_focus() || right_color.has_focus()
let right_color = ui.color_picker(&mut colors[1], &mut state.color_codes[1]);
let open = left_color.has_focus() || right_color.has_focus();
let updated = left_color.changed() || right_color.changed();
(open, updated)
})
.inner;
if !open {
@@ -243,6 +255,38 @@ impl GameWindow {
}
self.color_picker = None;
}
if updated {
self.update_config(|c| c.colors = colors);
}
}
fn update_config(&mut self, update: impl FnOnce(&mut GameConfig)) {
let mut new_config = self.config.clone();
update(&mut new_config);
if self.config != new_config {
let _ = self
.persistence
.save_config(config_filename(self.sim_id), &new_config);
}
self.config = new_config;
}
}
fn config_filename(sim_id: SimId) -> &'static str {
match sim_id {
SimId::Player1 => "config_p1",
SimId::Player2 => "config_p2",
}
}
fn load_config(persistence: &Persistence, sim_id: SimId) -> GameConfig {
if let Ok(config) = persistence.load_config(config_filename(sim_id)) {
return config;
}
GameConfig {
display_mode: DisplayMode::Anaglyph,
colors: COLOR_PRESETS[0],
dimensions: DisplayMode::Anaglyph.proportions() + Vec2::new(0.0, 22.0),
}
}
@@ -255,13 +299,18 @@ impl AppWindow for GameWindow {
}
fn initial_viewport(&self) -> ViewportBuilder {
let dimensions = self.display_mode.proportions() + Vec2::new(0.0, 22.0);
ViewportBuilder::default()
.with_title("Lemur")
.with_inner_size(dimensions)
.with_inner_size(self.config.dimensions)
}
fn show(&mut self, ctx: &Context) {
let dimensions = {
let bounds = ctx.input(|i| i.viewport().inner_rect.unwrap());
bounds.max - bounds.min
};
self.update_config(|c| c.dimensions = dimensions);
let mut toasts = Toasts::new()
.anchor(Align2::LEFT_BOTTOM, (10.0, 10.0))
.direction(Direction::BottomUp);
@@ -289,7 +338,7 @@ impl AppWindow for GameWindow {
let frame = Frame::central_panel(&ctx.style()).fill(Color32::BLACK);
CentralPanel::default().frame(frame).show(ctx, |ui| {
if let Some(screen) = self.screen.as_mut() {
screen.update(self.display_mode, self.colors);
screen.update(self.config.display_mode, self.config.colors);
ui.add(screen);
}
});
@@ -383,3 +432,10 @@ struct ColorPickerState {
just_opened: bool,
unpause_on_close: bool,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
struct GameConfig {
display_mode: DisplayMode,
colors: [Color32; 2],
dimensions: Vec2,
}
+2 -1
View File
@@ -1,6 +1,7 @@
use std::{collections::HashMap, sync::Arc};
use egui::{Color32, Rgba, Vec2, Widget};
use serde::{Deserialize, Serialize};
use wgpu::{util::DeviceExt as _, BindGroup, BindGroupLayout, Buffer, RenderPipeline};
use crate::graphics::TextureSink;
@@ -260,7 +261,7 @@ impl Colors {
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub enum DisplayMode {
Anaglyph,
LeftEye,