From 274d072bfeee100d05f6b2d7c166b9c917ab2e3f Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 25 Feb 2022 11:57:31 -0500 Subject: [PATCH] UI improvements --- TODO.md | 1 + src/egui_app.rs | 35 ++++++++++++++++++++++++++++------- src/function.rs | 6 +++++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/TODO.md b/TODO.md index 2e877bf..e896b66 100644 --- a/TODO.md +++ b/TODO.md @@ -7,6 +7,7 @@ - Generation of data - Management - Handle by IDs + - Integrals between functions - UI - Dynamically create inputs - Different colors diff --git a/src/egui_app.rs b/src/egui_app.rs index ce7c0cb..760d913 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -3,8 +3,9 @@ use std::ops::RangeInclusive; use crate::function::Function; use crate::misc::{digits_precision, test_func}; use eframe::{egui, epi}; -use egui::plot::{Line, Plot, Value, Values}; -use egui::widgets::plot::{Bar, BarChart}; +use egui::plot::{Line, Plot, Values}; +use egui::widgets::plot::BarChart; +use egui::widgets::Button; use egui::Color32; use git_version::git_version; @@ -16,6 +17,7 @@ const INTEGRAL_NUM_RANGE: RangeInclusive = 10..=1000000; const MIN_X_TOTAL: f64 = -1000.0; const MAX_X_TOTAL: f64 = 1000.0; const X_RANGE: RangeInclusive = MIN_X_TOTAL..=MAX_X_TOTAL; +const DEFAULT_FUNCION: &str = "x^2"; pub struct MathApp { functions: Vec, @@ -32,13 +34,12 @@ pub struct MathApp { impl Default for MathApp { #[inline] fn default() -> Self { - let def_func_str = "x^2".to_string(); let def_min_x = -10.0; let def_max_x = 10.0; let def_interval: usize = 1000; let def_funcs: Vec = vec![Function::new( - def_func_str, + String::from(DEFAULT_FUNCION), def_min_x, def_max_x, true, @@ -104,17 +105,29 @@ impl epi::App for MathApp { ui.label("- signum, min, max"); }); - let mut func_new_strings: Vec = Vec::new(); + let mut new_func_data: Vec<(String, bool)> = Vec::new(); let mut parse_error: String = "".to_string(); egui::SidePanel::left("side_panel") .resizable(false) .show(ctx, |ui| { ui.heading("Side Panel"); + if ui.add(egui::Button::new("Add function")).clicked() { + functions.push(Function::new(String::from(DEFAULT_FUNCION), *min_x, + *max_x, + true, + Some(*min_x), + Some(*max_x), + Some(*integral_num))); + } for function in functions.iter() { let mut func_str = function.get_string(); + let mut integral_toggle: bool = false; ui.horizontal(|ui| { ui.label("Function: "); + if ui.add(Button::new("Toggle Integrals")).clicked() { + integral_toggle = true; + } ui.text_edit_singleline(&mut func_str); }); @@ -123,7 +136,7 @@ impl epi::App for MathApp { parse_error += &func_test_output; } - func_new_strings.push(func_str); + new_func_data.push((func_str, integral_toggle)); } let min_x_old = *min_x; @@ -178,7 +191,15 @@ impl epi::App for MathApp { let mut i: usize = 0; for function in functions.iter_mut() { - function.update(func_new_strings[i].clone(), *min_x, *max_x, true, Some(*integral_min_x), Some(*integral_max_x), Some(*integral_num)); + let (func_str, integral_toggle) = (new_func_data[i].0.clone(), new_func_data[i].1); + let integral: bool = if integral_toggle { + !function.is_integral() + } else { + function.is_integral() + }; + + + function.update(func_str, *min_x, *max_x, integral, Some(*integral_min_x), Some(*integral_max_x), Some(*integral_num)); i += 1; } }); diff --git a/src/function.rs b/src/function.rs index 15ade42..e4967ce 100644 --- a/src/function.rs +++ b/src/function.rs @@ -32,7 +32,7 @@ impl FunctionOutput { #[inline] pub fn has_integral(&self) -> bool { match &self.front { - Some(x) => true, + Some(_) => true, None => false, } } @@ -123,6 +123,10 @@ impl Function { self.max_x = max_x; } + if integral != self.integral { + self.integral = integral; + } + // Makes sure proper arguments are passed when integral is enabled if integral { if integral_min_x.is_none() {