diff --git a/src/Algorithm.rs b/src/Algorithm.rs index c37c871..7d40508 100644 --- a/src/Algorithm.rs +++ b/src/Algorithm.rs @@ -2,6 +2,7 @@ use crate::BarPlugin::Bar; use crate::GuiHookVec::GuiVec; use std::ops::{Generator, GeneratorState}; +use std::pin::Pin; use std::rc::Rc; use std::thread::yield_now; use macroquad::prelude::screen_width; @@ -12,7 +13,26 @@ pub struct Algorithm{ } -impl Algorithm { +pub enum AlgoEnum{ + InsertSort(Box>), +} + +impl Algorithm{ + + pub fn start(length:i32, algorithm:u32) -> impl Generator{ + move ||{ + let mut generator = Algorithm::insertSort(length); + + match Pin::new(&mut generator).resume(()) { + GeneratorState::Yielded(x) => { + yield x + }, + GeneratorState::Complete(x) => { + } + } + } + + } pub fn insertSort(length:i32) -> impl Generator{ let mut list = GuiVec::new(screen_width(), screen_height(), length); @@ -23,7 +43,6 @@ impl Algorithm { let mut j = index; while j>0 && list.lessThan(j, j-1){ yield list.swap(j, j-1); - j = j-1; } } diff --git a/src/State.rs b/src/State.rs index 5d96939..3f9cb16 100644 --- a/src/State.rs +++ b/src/State.rs @@ -1,13 +1,51 @@ -use std::ops::Generator; -use macroquad::prelude::screen_width; +use std::ops::{Generator, GeneratorState}; +use std::pin::Pin; +use macroquad::color::{BLACK, WHITE}; +use macroquad::math::Vec2; +use macroquad::prelude::{clear_background, draw_text, get_fps, get_time, next_frame, screen_width}; +use macroquad::ui::root_ui; use macroquad::window::screen_height; use crate::Algorithm::Algorithm; use crate::GuiHookVec::GuiVec; -#[derive(PartialEq)] -pub enum GeneratorEnum{ - InsertSort, - BogoSort, - StalinSort, - BubbleSort, +pub struct State{ + } +impl State{ + pub async fn runInsertSort(length:i32, mut generator:impl Generator+ std::marker::Unpin){ + let mut finished = false; + + let mut paused = false; + let timeout = 0.0; + let mut lasttime:f64 = 0.; + let mut holder = GuiVec::new(screen_width(), screen_height(), length); + while !finished{ + clear_background(WHITE); + if get_time()-lasttime > timeout && !finished && !paused{ + match Pin::new(& mut generator).resume(()){ + GeneratorState::Yielded(x) => { + holder = x; + }, + GeneratorState::Complete(x) => { + finished = true; + } + }; + lasttime = get_time(); + } + holder.draw(); + draw_text(format!("Read: {}", holder.reads).as_str(), screen_width()*0.01, 20.0, 20.0, BLACK); + draw_text(format!("Write: {}", holder.writes).as_str(), screen_width()*0.01, 40.0, 20.0, BLACK); + draw_text(format!("Comparisons: {}", holder.comps).as_str(), screen_width()*0.01, 60.0, 20.0, BLACK); + draw_text(format!("FPS: {}", get_fps()).as_str(), screen_width()*0.01, 80., 20.0, BLACK); + + if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "Pause"){ + if paused { + paused = false; + }else{ + paused = true; + } + } + next_frame().await + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index dab92a4..4a45016 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,61 +13,45 @@ use crate::GuiHookVec::GuiVec; use std::ops::{Deref, Generator, GeneratorState}; use macroquad::ui::root_ui; -use crate::State::GeneratorEnum; +use crate::Algorithm::AlgoEnum; + #[macroquad::main("BeepSort")] async fn main() { - let mut length = 100; - let mut lasttime:f64 = 0.; - let mut holder = GuiVec::new(screen_width(), screen_height(), length); + + + let mut finished = false; - let timeout = 0.0; - let mut paused = false; - let mut reset = false; + + let mut length = 100; + - let mut algoChoise = GeneratorEnum::InsertSort; loop{ - let mut generator = Algorithm::Algorithm::insertSort(length); - - while !reset { - clear_background(WHITE); - if get_time()-lasttime > timeout && !finished && !paused{ - match Pin::new(& mut generator).resume(()){ - GeneratorState::Yielded(x) => { - - holder = x.clone(); - }, - GeneratorState::Complete(x) => { - finished = true; - } - }; - lasttime = get_time(); - } - holder.draw(); - draw_text(format!("Read: {}", holder.reads).as_str(), screen_width()*0.01, 20.0, 20.0, BLACK); - draw_text(format!("Write: {}", holder.writes).as_str(), screen_width()*0.01, 40.0, 20.0, BLACK); - draw_text(format!("Comparisons: {}", holder.comps).as_str(), screen_width()*0.01, 60.0, 20.0, BLACK); - draw_text(format!("FPS: {}", get_fps()).as_str(), screen_width()*0.01, 80., 20.0, BLACK); - - if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "Pause"){ - if paused { - paused = false; - }else{ - paused = true; - } - } - if root_ui().button(Vec2::new(screen_width()*0.01, 95.), "Reset!"){ - reset = true; - } - next_frame().await + clear_background(WHITE); + if root_ui().button(Vec2::new(screen_width()*0.2, 50.), "Increase"){ + length += 10; } - algoChoise = GeneratorEnum::StalinSort; - reset = false; - finished = false; + if root_ui().button(Vec2::new(screen_width()*0.3, 50.), "Decrease"){ + length -= 10; + } + draw_text(format!("Length: {}", length.to_string()).as_str(), screen_width()*0.01, 50., 20.0, BLACK); + if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "InsertSort"){ + State::State::runInsertSort(length, Algorithm::Algorithm::insertSort(length)).await; + } + if root_ui().button(Vec2::new(screen_width()*0.01, 100.), "BogoSort"){ + State::State::runInsertSort(length, Algorithm::Algorithm::bogoSort(length)).await; + } + if root_ui().button(Vec2::new(screen_width()*0.01, 130.), "BubbleSort"){ + State::State::runInsertSort(length, Algorithm::Algorithm::bogoSort(length)).await; + } + if root_ui().button(Vec2::new(screen_width()*0.01, 160.), "StalinSort"){ + State::State::runInsertSort(length, Algorithm::Algorithm::bogoSort(length)).await; + } + next_frame().await }