Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/gui_egui/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,6 +35,8 @@ pub struct Gui {
pub in_built_models: Vec<(String, String)>,
pub contexts: HashMap<crate::common::Id, EguiExtra>,
pub library: Library,

pub gui_options: GuiOptions,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -67,18 +70,23 @@ 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))))
}

impl Gui {
pub fn new(cs: ComponentStore, path: &PathBuf, library: Library) -> Result<Self, Box<dyn Error>> {
pub fn new(
cs: ComponentStore,
path: &PathBuf,
library: Library,
) -> Result<Self, Box<dyn Error>> {
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),
Expand All @@ -95,6 +103,7 @@ impl Gui {
contexts,
library,
in_built_models: Vec::default(),
gui_options: GuiOptions::default(),
})
}

Expand All @@ -116,6 +125,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;
Expand Down
44 changes: 44 additions & 0 deletions src/gui_egui/gui_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use egui::{ViewportBuilder, ViewportId, Pos2};

#[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,
}
}
}


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.");
});
});
}
}
23 changes: 8 additions & 15 deletions src/gui_egui/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ pub fn file_editor_toggle_fn(gui: &mut Gui) {
}
}
}
pub fn file_preferences_fn(_gui: &mut Gui) {}

pub fn file_preferences_fn(gui: &mut Gui) {
gui.gui_options.window_visible = true;
}
pub fn file_quit_fn(_gui: &mut Gui) {}
pub fn edit_cut_fn(_gui: &mut Gui) {}
pub fn edit_copy_fn(_gui: &mut Gui) {}
Expand All @@ -374,27 +377,17 @@ pub fn view_zoom_in_fn(gui: &mut Gui) {
true => &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) {
let scale: &mut f32 = match gui.editor_use {
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/gui_egui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod helper;
mod keymap;
mod library;
mod menu;

mod gui_options;
#[cfg(feature = "components")]
pub mod components;

Expand Down