From 938f984d510971952f761d8cafca067dc98f9a9b Mon Sep 17 00:00:00 2001 From: onsdagens Date: Wed, 3 Sep 2025 01:57:34 +0200 Subject: [PATCH 1/2] add preferences window, make scaling configurable --- src/gui_egui/gui.rs | 50 ++++++++++++++++++++++++++++++++++++++++++ src/gui_egui/keymap.rs | 23 +++++++------------ 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/gui_egui/gui.rs b/src/gui_egui/gui.rs index 4ce9315..86d5c24 100644 --- a/src/gui_egui/gui.rs +++ b/src/gui_egui/gui.rs @@ -34,6 +34,24 @@ pub struct Gui { pub in_built_models: Vec<(String, String)>, pub contexts: HashMap, pub library: Library, + + pub gui_options: GuiOptions, +} + +#[derive(Clone, Debug)] +pub struct GuiOptions { + // This is added/subtracted to/from the view scale when zoomed. + pub view_scaling_val: f32, + pub window_visible: bool, +} + +impl Default for GuiOptions { + fn default() -> GuiOptions { + GuiOptions { + view_scaling_val: 0.03, + window_visible: false, + } + } } #[derive(Clone, Debug)] @@ -67,6 +85,7 @@ pub fn gui(cs: ComponentStore, path: &PathBuf, library: Library) -> Result<(), e contexts, library, in_built_models: Vec::default(), + gui_options: GuiOptions::default(), }; eframe::run_native("SyncRim", options, Box::new(|_cc| Ok(Box::new(gui)))) @@ -95,6 +114,7 @@ impl Gui { contexts, library, in_built_models: Vec::default(), + gui_options: GuiOptions::default(), }) } @@ -116,6 +136,9 @@ impl Gui { impl eframe::App for Gui { fn update(&mut self, ctx: &Context, frame: &mut eframe::Frame) { self.shortcuts.inputs(ctx, self); + if self.gui_options.window_visible { + self.gui_options.render(ctx); + } if self.editor_use { crate::gui_egui::editor::Editor::update(ctx, frame, self); return; @@ -233,3 +256,30 @@ pub fn create_contexts(components: &Components) -> HashMap &mut gui.editor.as_mut().unwrap().scale, false => &mut gui.scale, }; - match *scale { - x if (0.0f32..0.2f32).contains(&x) => *scale = 0.25f32, - x if (0.2f32..0.4f32).contains(&x) => *scale = 0.5f32, - x if (0.4f32..0.6f32).contains(&x) => *scale = 1f32, - x if (0.9f32..1.1f32).contains(&x) => *scale = 1.5f32, - x if (1.4f32..1.6f32).contains(&x) => *scale = 2f32, - _ => *scale = 2f32, + if *scale < 2f32 { + *scale += gui.gui_options.view_scaling_val; } } pub fn view_zoom_out_fn(gui: &mut Gui) { @@ -388,13 +386,8 @@ pub fn view_zoom_out_fn(gui: &mut Gui) { true => &mut gui.editor.as_mut().unwrap().scale, false => &mut gui.scale, }; - match *scale { - x if (0.2f32..0.4f32).contains(&x) => *scale = 0.1f32, - x if (0.4f32..0.6f32).contains(&x) => *scale = 0.25f32, - x if (0.9f32..1.1f32).contains(&x) => *scale = 0.5f32, - x if (1.4f32..1.6f32).contains(&x) => *scale = 1f32, - x if (1.9f32..2.1f32).contains(&x) => *scale = 1.5f32, - _ => *scale = 0.1f32, + if *scale > 0.1 { + *scale -= gui.gui_options.view_scaling_val; } } pub fn view_grid_toggle_fn(gui: &mut Gui) { From b63d48864320cbd2bbfa13f74f5127dc8d096bf4 Mon Sep 17 00:00:00 2001 From: onsdagens Date: Wed, 3 Sep 2025 02:20:43 +0200 Subject: [PATCH 2/2] move gui options somewhere more reasonable --- src/gui_egui/gui.rs | 52 +++++-------------------------------- src/gui_egui/gui_options.rs | 44 +++++++++++++++++++++++++++++++ src/gui_egui/mod.rs | 2 +- 3 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 src/gui_egui/gui_options.rs diff --git a/src/gui_egui/gui.rs b/src/gui_egui/gui.rs index 86d5c24..6055cc5 100644 --- a/src/gui_egui/gui.rs +++ b/src/gui_egui/gui.rs @@ -2,6 +2,7 @@ use crate::common::{ComponentStore, Components, Simulator}; use crate::gui_egui::editor::EditorMode; use crate::gui_egui::{ editor::{Editor, Library}, + gui_options::GuiOptions, keymap, keymap::Shortcuts, menu::Menu, @@ -38,22 +39,6 @@ pub struct Gui { pub gui_options: GuiOptions, } -#[derive(Clone, Debug)] -pub struct GuiOptions { - // This is added/subtracted to/from the view scale when zoomed. - pub view_scaling_val: f32, - pub window_visible: bool, -} - -impl Default for GuiOptions { - fn default() -> GuiOptions { - GuiOptions { - view_scaling_val: 0.03, - window_visible: false, - } - } -} - #[derive(Clone, Debug)] pub struct EguiExtra { pub properties_window: bool, @@ -92,12 +77,16 @@ pub fn gui(cs: ComponentStore, path: &PathBuf, library: Library) -> Result<(), e } impl Gui { - pub fn new(cs: ComponentStore, path: &PathBuf, library: Library) -> Result> { + pub fn new( + cs: ComponentStore, + path: &PathBuf, + library: Library, + ) -> Result> { let contexts = create_contexts(&cs.store); let simulator = Simulator::new(cs)?; let path = path.to_owned(); // simulator.save_dot(&path); - + Ok(Gui { path, simulator: Some(simulator), @@ -256,30 +245,3 @@ pub fn create_contexts(components: &Components) -> HashMap GuiOptions { + GuiOptions { + view_scaling_val: 0.03, + window_visible: false, + } + } +} + + +impl GuiOptions { + // Naming would make more sense if it was a GuiOptionsWindow being rendered, then again i see + // no point in adding more structs for the sake of naming (maybe the point will become apparent + // down the line). + pub fn render(&mut self, ctx: &egui::Context) { + ctx.show_viewport_immediate( + ViewportId::from_hash_of("Preferences"), + ViewportBuilder { + title: Some("Preferences".to_string()), + position: Some(Pos2::new(ctx.screen_rect().max.x/2.0, ctx.screen_rect().max.y/2.0)), + inner_size: Some((500.0, 200.0).into()), + ..ViewportBuilder::default() + }, + |ctx, _class| { + if ctx.input(|i| i.viewport().close_requested()) { + self.window_visible = false + } + egui::CentralPanel::default().show(ctx, |ui| { + ui.label("Zoom Scaling"); + let response = ui.add(egui::Slider::new(&mut self.view_scaling_val, 0.0..=0.1)); + response.on_hover_text("Adjusts the step size by which zooming zooms."); + }); + }); + } +} diff --git a/src/gui_egui/mod.rs b/src/gui_egui/mod.rs index 0d59050..2c855ff 100644 --- a/src/gui_egui/mod.rs +++ b/src/gui_egui/mod.rs @@ -6,7 +6,7 @@ pub mod helper; mod keymap; mod library; mod menu; - +mod gui_options; #[cfg(feature = "components")] pub mod components;