From caf515b14f7bbe1f849843b1f44b17bed3c57a92 Mon Sep 17 00:00:00 2001 From: polsevev Date: Sat, 4 Mar 2023 03:14:03 +0100 Subject: [PATCH] Added dropdown for algorithm choice --- src/algorithm.rs | 18 ++++++++-- src/algorithm/radixSort.rs | 22 +++--------- src/dropdown.rs | 68 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 46 +++++--------------------- 4 files changed, 98 insertions(+), 56 deletions(-) create mode 100644 src/dropdown.rs diff --git a/src/algorithm.rs b/src/algorithm.rs index 36367b1..a17cc46 100644 --- a/src/algorithm.rs +++ b/src/algorithm.rs @@ -14,11 +14,21 @@ use crate::GuiHookVec::SortingList; #[derive(Debug, Clone)] pub struct Algorithm{ - + algorithms:Vec } impl Algorithm{ - + pub fn new() -> Self{ + Algorithm { algorithms: vec![ + "insertSort".to_string(), + "bubbleSort".to_string(), + "bogoSort".to_string(), + "cocktailShaker".to_string(), + "binaryHeap".to_string(), + "quickSort".to_string(), + "radixSort".to_string(), + ] } + } pub async fn run(length:usize, delay:f32, functionName:String){ let mut list:GuiVec = SortingList::new(length, delay); list.randomize(); @@ -42,6 +52,10 @@ impl Algorithm{ } } + pub fn getAlgorithms(&self) -> &Vec{ + &self.algorithms + } + } diff --git a/src/algorithm/radixSort.rs b/src/algorithm/radixSort.rs index 252727f..6c4ffc1 100644 --- a/src/algorithm/radixSort.rs +++ b/src/algorithm/radixSort.rs @@ -20,14 +20,13 @@ pub async fn radixSort(list:&mut impl SortingList) { let mut stack2:Vec<(usize,usize)> = Vec::new(); stack.push((0, list.len())); for i in (0..(max)).rev(){ - println!("Sorting by bit {}", i); + while stack.len() > 0{ let cur = stack.pop().unwrap(); - println!("{:?}", cur); - println!("{:?}", stack2); + match radix(list, i, cur.0, cur.1).await{ Some((initial, switch, max)) => { - println!("{}, {}, {}", initial, switch, max); + if initial < switch{ stack2.push((initial, switch)); } @@ -39,13 +38,7 @@ pub async fn radixSort(list:&mut impl SortingList) { }, None => return } - // let mut bitVec:Vec = Vec::new(); - // for y in 0..list.len(){ - // let currentBit = get_bit_at(list.get(y).position, i); - // bitVec.push(if currentBit {1} else {0}); - // } - // println!("{:?}", bitVec); - + } stack = stack2.clone(); @@ -75,12 +68,7 @@ async fn radix(list:&mut impl SortingList, radix:usize, mut minBoundry:usize, mu } } - // let mut bitVec:Vec = Vec::new(); - // for i in 0..list.len(){ - // let currentBit = get_bit_at(list.get(i).position, radix); - // bitVec.push(if currentBit {1} else {0}); - // } - + Some((initialMin, minBoundry, initialMax)) } diff --git a/src/dropdown.rs b/src/dropdown.rs new file mode 100644 index 0000000..faf0983 --- /dev/null +++ b/src/dropdown.rs @@ -0,0 +1,68 @@ +use macroquad::hash; + +use macroquad::{ui::root_ui, window::screen_width, prelude::Vec2}; + + + + +enum Status{ + Open, + Closed +} + +pub struct ButtonDropDown{ + selected:String, + elements:Vec, + status:Status +} + +impl ButtonDropDown{ + pub fn new(elements:&Vec) -> Self{ + ButtonDropDown{ + selected:elements.get(0).unwrap().clone(), + elements:elements.clone(), + status:Status::Closed + } + } + + pub fn render(&mut self, location:Vec2) -> String{ + let mut algo = ""; + + match self.status{ + + Status::Open => { + let size = Vec2::new(250., (self.elements.len() as f32*25.0) + 20.0); + root_ui().window(hash!(), location, size, |ui|{ + let mut position = Vec2::new(10.0, 10.); + + for i in self.elements.iter(){ + let label = format!("{}{}", i[0..1].to_string().to_uppercase(), i[1..i.len()].to_string()); + if ui.button(position, label.as_str()){ + self.selected = i.clone(); + self.status = Status::Closed; + } + position.y += 25.0; + } + + + + }); + } + Status::Closed => { + root_ui().window(hash!(), Vec2::new(screen_width()*0.01, 45.), Vec2::new(300., 50.), |ui|{ + let uppercasedSelected = format!("{}{}", self.selected[0..1].to_string().to_uppercase(), self.selected[1..self.selected.len()].to_string()); + ui.label(Vec2::new(10.0, 0.0), format!("Curent chosen algorithm: {}", uppercasedSelected).as_str()); + if ui.button(Vec2::new(10.0, 20.0), "Open Menu!"){ + self.status = Status::Open; + } + if ui.button(Vec2::new(200.0, 20.0), "RUN!"){ + algo = self.selected.as_str(); + } + }); + }, + } + + algo.to_string() + } +} + diff --git a/src/main.rs b/src/main.rs index 686d7cc..2eb7728 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,10 @@ mod BarPlugin; mod GuiHookVec; mod algorithm; +mod dropdown; +use dropdown::ButtonDropDown; use macroquad::prelude::*; -use macroquad::prelude::scene::clear; - -use crate::BarPlugin::Bar; -use crate::GuiHookVec::GuiVec; use macroquad::hash; use macroquad::ui::root_ui; @@ -17,21 +15,17 @@ use macroquad::ui::root_ui; async fn main() { let mut length = 1_usize; let mut lengthString = "100".to_owned(); - let mut delay = 0.; + let mut delayText = "1".to_owned(); + + let mut algorithm = algorithm::Algorithm::new(); + let mut buttonDropDown = ButtonDropDown::new(&algorithm.getAlgorithms()); loop{ clear_background(WHITE); - - - - delay = match delayText.parse::(){ - Ok(a) => a/1000., - Err(error)=> {0.1} - }; length = match lengthString.parse::(){ Ok(a) => a, - Err(error)=> {100} + Err(_)=> {100} }; draw_text("Sorting!", screen_width()*0.3, screen_height()*0.1, 100.0, GREEN); @@ -41,30 +35,8 @@ async fn main() { ui.input_text(hash!(), "Delay (ms)", &mut delayText); ui.input_text(hash!(), "Length Of Array!", &mut lengthString); }); - let mut algo = ""; - if root_ui().button(Vec2::new(screen_width()*0.01, 100.), "Run InsertSort!"){ - algo = "insertSort"; - } - if root_ui().button(Vec2::new(screen_width()*0.01, 130.), "Run BubbleSort!"){ - algo = "bubbleSort"; - } - if root_ui().button(Vec2::new(screen_width()*0.01, 160.), "Run BinaryHeapSort!"){ - algo = "binaryHeap"; - } - if root_ui().button(Vec2::new(screen_width()*0.01, 190.), "Run CoctailShakerSort!"){ - algo = "cocktailShaker"; - } - if root_ui().button(Vec2::new(screen_width()*0.01, 220.), "Run BogoSort!"){ - algo = "bogoSort"; - } - if root_ui().button(Vec2::new(screen_width()*0.01, 250.), "Run QuickSort!"){ - algo = "quickSort"; - } - if root_ui().button(Vec2::new(screen_width()*0.01, 280.), "Run RadixSort!"){ - algo = "radixSort"; - } - - + let mut algo = buttonDropDown.render(Vec2::new(screen_width()*0.01, 45.)); + if algo != ""{ algorithm::Algorithm::run(length, 1.0, algo.to_string()).await; }