Added dropdown for algorithm choice

This commit is contained in:
Rolf Martin Glomsrud 2023-03-04 03:14:03 +01:00
parent 8ebad48659
commit caf515b14f
4 changed files with 98 additions and 56 deletions

View file

@ -14,11 +14,21 @@ use crate::GuiHookVec::SortingList;
#[derive(Debug, Clone)]
pub struct Algorithm{
algorithms:Vec<String>
}
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<String>{
&self.algorithms
}
}

View file

@ -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<usize> = 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<usize> = 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))
}

68
src/dropdown.rs Normal file
View file

@ -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<String>,
status:Status
}
impl ButtonDropDown{
pub fn new(elements:&Vec<String>) -> 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()
}
}

View file

@ -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::<f64>(){
Ok(a) => a/1000.,
Err(error)=> {0.1}
};
length = match lengthString.parse::<usize>(){
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;
}