From 3191915b54b2a5041c656d44e1e750cf2452995e Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 18 May 2022 20:07:59 -0400 Subject: [PATCH] ui changes --- src/consts.rs | 11 ------ src/math_app.rs | 98 +++++++++++++++++++++++++++---------------------- 2 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/consts.rs b/src/consts.rs index c2bbfb0..3fc94eb 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -21,17 +21,6 @@ pub const BUILD_INFO: &str = formatc!( /// Range of acceptable input values for integral_num pub const INTEGRAL_NUM_RANGE: RangeInclusive = 1..=50000; -/// Minimum X value for calculating an Integral -pub const INTEGRAL_X_MIN: f64 = -1000.0; - -/// Maximum X value for calculating an Integral -pub const INTEGRAL_X_MAX: f64 = 1000.0; - -const_assert!(INTEGRAL_X_MAX > INTEGRAL_X_MIN); - -/// Range of acceptable x coordinates for calculating an integral -pub const INTEGRAL_X_RANGE: RangeInclusive = INTEGRAL_X_MIN..=INTEGRAL_X_MAX; - // Default values /// Default minimum X value to display pub const DEFAULT_MIN_X: f64 = -10.0; diff --git a/src/math_app.rs b/src/math_app.rs index ca1921f..d7f2d32 100644 --- a/src/math_app.rs +++ b/src/math_app.rs @@ -8,6 +8,7 @@ use egui::{ plot::Plot, style::Margin, Button, CentralPanel, ComboBox, Context, Frame, Key, Layout, SidePanel, Slider, TopBottomPanel, Vec2, Window, }; +use egui::{DragValue, Ui}; use emath::{Align, Align2}; use epaint::Rounding; use instant::Instant; @@ -271,9 +272,10 @@ impl MathApp { SidePanel::left("side_panel") .resizable(false) .show(ctx, |ui| { + let any_using_integral = self.functions.any_using_integral(); let prev_sum = self.settings.riemann_sum; // ComboBox for selecting what Riemann sum type to use - ui.add_enabled_ui(self.functions.any_using_integral(), |ui| { + ui.add_enabled_ui(any_using_integral, |ui| { ComboBox::from_label("Riemann Sum") .selected_text(self.settings.riemann_sum.to_string()) .show_ui(ui, |ui| { @@ -293,9 +295,57 @@ impl MathApp { "Right", ); }); - }); - let riemann_changed = prev_sum != self.settings.riemann_sum; + let riemann_changed = prev_sum != self.settings.riemann_sum; + + let min_x_old = self.settings.integral_min_x; + let max_x_old = self.settings.integral_max_x; + + let (min_x_changed, max_x_changed) = ui + .horizontal(|ui: &mut Ui| { + let spacing_mut = ui.spacing_mut(); + + spacing_mut.item_spacing = Vec2::new(1.0, 0.0); + spacing_mut.interact_size *= 0.5; + + ui.label("Integral: ["); + let min_x_changed = ui + .add(DragValue::new(&mut self.settings.integral_min_x)) + .changed(); + ui.label(","); + let max_x_changed = ui + .add(DragValue::new(&mut self.settings.integral_max_x)) + .changed(); + ui.label("]"); + (min_x_changed, max_x_changed) + }) + .inner; + + // Checks integral bounds, and if they are invalid, fix them + if self.settings.integral_min_x >= self.settings.integral_max_x { + if max_x_changed { + self.settings.integral_max_x = max_x_old; + } else if min_x_changed { + self.settings.integral_min_x = min_x_old; + } else { + // No clue how this would happen, but just in case + self.settings.integral_min_x = DEFAULT_MIN_X; + self.settings.integral_max_x = DEFAULT_MAX_X; + } + } + + // Number of Rectangles for Riemann sum + let integral_num_changed = ui + .add_enabled( + any_using_integral, + Slider::new(&mut self.settings.integral_num, INTEGRAL_NUM_RANGE) + .text("Interval"), + ) + .changed(); + + self.settings.integral_changed = any_using_integral + && (max_x_changed | min_x_changed | integral_num_changed | riemann_changed); + }); ui.horizontal(|ui| { self.settings.do_extrema.bitxor_assign( @@ -317,50 +367,10 @@ impl MathApp { ); }); - let min_x_old = self.settings.integral_min_x; - let min_x_changed = ui - .add( - Slider::new(&mut self.settings.integral_min_x, INTEGRAL_X_RANGE) - .text("Min X"), - ) - .changed(); - - let max_x_old = self.settings.integral_max_x; - let max_x_changed = ui - .add( - Slider::new(&mut self.settings.integral_max_x, INTEGRAL_X_RANGE) - .text("Max X"), - ) - .changed(); - - // Checks integral bounds, and if they are invalid, fix them - if self.settings.integral_min_x >= self.settings.integral_max_x { - if max_x_changed { - self.settings.integral_max_x = max_x_old; - } else if min_x_changed { - self.settings.integral_min_x = min_x_old; - } else { - // No clue how this would happen, but just in case - self.settings.integral_min_x = DEFAULT_MIN_X; - self.settings.integral_max_x = DEFAULT_MAX_X; - } - } - - // Number of Rectangles for Riemann sum - let integral_num_changed = ui - .add( - Slider::new(&mut self.settings.integral_num, INTEGRAL_NUM_RANGE) - .text("Interval"), - ) - .changed(); - - self.settings.integral_changed = - max_x_changed | min_x_changed | integral_num_changed | riemann_changed; - self.functions.display_entries(ui); // Only render if there's enough space - if ui.available_height() > 0.0 { + if ui.available_height() > 14.0 { ui.with_layout(Layout::bottom_up(Align::Min), |ui| { // Contents put in reverse order from bottom to top due to the 'buttom_up' layout