diff --git a/src/escher.rs b/src/escher.rs index 91cb370..6e229bd 100644 --- a/src/escher.rs +++ b/src/escher.rs @@ -3,7 +3,6 @@ use crate::funcplot::draw_arrow; use crate::geom::{GeomHist, HistTag, Side, Xaxis}; use crate::info::Info; -use crate::scale::DefaultFontSize; use bevy::prelude::*; use bevy::reflect::TypePath; use bevy_prototype_lyon::prelude::*; @@ -359,6 +358,11 @@ pub struct MapDimensions { pub y: f32, } +#[derive(Component)] +pub struct DefaultFontSize { + pub size: f32, +} + /// Load escher map once the asset is available. /// The colors correspond to the default escher colors. pub fn load_map( diff --git a/src/gui.rs b/src/gui.rs index f5b384b..28e93a0 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,6 +1,7 @@ //! Gui (windows and panels) to upload data and hover. use crate::data::{Data, ReactionState}; +use crate::escher::DefaultFontSize; use crate::escher::{EscherMap, MapState}; use crate::geom::{AnyTag, Xaxis}; use crate::info::Info; @@ -25,7 +26,8 @@ impl Plugin for GuiPlugin { .insert_resource(ActiveData::default()) .add_event::() .add_systems(Update, ui_settings) - .add_systems(Update, scale_ui); + .add_systems(Update, scale_ui) + .add_systems(Update, update_text_sizes); // file drop and file system does not work in WASM #[cfg(not(target_arch = "wasm32"))] @@ -85,8 +87,7 @@ pub struct UiState { pub data_path: String, pub screen_path: String, pub hide: bool, - // since this type and field are private, Self has to be initialized - // with Default::default(), ensuring that the fallbacks for colors (empty string) are set. + pub font_scale: f32, _init: Init, } @@ -107,6 +108,7 @@ impl Default for UiState { max_left: 100., max_right: 100., max_top: 100., + font_scale: 1.0, color_left: { let mut color = HashMap::new(); color.insert( @@ -224,6 +226,11 @@ pub fn ui_settings( } egui::Window::new("Settings").show(egui_context.ctx_mut(), |ui| { ui.visuals_mut().override_text_color = Some(egui::Color32::WHITE); + + ui.label("Text Scale"); + ui.add(egui::Slider::new(&mut state.font_scale, 0.5..=2.0).text("Scale")); + ui.separator(); + for (geom, ext) in ["Reaction", "Metabolite"] .into_iter() .cartesian_product(["min", "max"]) @@ -371,6 +378,16 @@ fn scale_ui( } } +/// Update all text components when the font scale changes in UI settings +fn update_text_sizes( + state: Res, + mut text_query: Query<(&mut TextFont, &DefaultFontSize)>, +) { + for (mut text_font, default_size) in text_query.iter_mut() { + text_font.font_size = default_size.size * state.font_scale; + } +} + /// Save map to arbitrary place, including (non-hover) hist transforms. fn save_file( mut assets: ResMut>, diff --git a/src/main.rs b/src/main.rs index be26ab9..650da71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,6 @@ mod gui; mod info; mod legend; mod picking; -mod scale; mod screenshot; #[cfg(test)] mod tests; @@ -48,7 +47,6 @@ fn main() { .add_plugins(data::DataPlugin) .add_systems(Startup, setup_system) .add_plugins(aesthetics::AesPlugin) - .add_plugins(scale::ZoomPlugin) .add_plugins(legend::LegendPlugin) .run(); } diff --git a/src/scale.rs b/src/scale.rs deleted file mode 100644 index bc42113..0000000 --- a/src/scale.rs +++ /dev/null @@ -1,34 +0,0 @@ -//! Module to handle dynamic scaling on zoom. -use crate::funcplot::lerp; -use bevy::prelude::*; - -/// Constant that matches bevy_pancman Line pixel increment -pub struct ZoomPlugin; - -impl Plugin for ZoomPlugin { - fn build(&self, app: &mut App) { - app.add_systems(Update, zoom_fonts); - } -} - -#[derive(Component)] -pub struct DefaultFontSize { - pub size: f32, -} - -/// Rerender fonts on zoom to achieve a constantly-readable size. -fn zoom_fonts( - mut text_query: Query<(&mut TextFont, &DefaultFontSize)>, - proj_query: Query<&OrthographicProjection, (Changed, Without)>, -) { - let Ok(proj) = proj_query.get_single() else { - return; - }; - for (mut text_font, def) in text_query.iter_mut() { - let new_font_size = lerp(proj.scale, 1., 40., def.size, def.size * 10.); - // step update to enhance perfomance - if (new_font_size - text_font.font_size).abs() > 1.0 { - text_font.font_size = new_font_size; - } - } -}