i am retarded, fixed it now by adding a pre-menu

This commit is contained in:
Rolf Martin Glomsrud 2022-11-07 23:21:10 +01:00
parent 643ae4a77f
commit 0e5ab853a6
3 changed files with 95 additions and 54 deletions

View file

@ -2,6 +2,7 @@
use crate::BarPlugin::Bar; use crate::BarPlugin::Bar;
use crate::GuiHookVec::GuiVec; use crate::GuiHookVec::GuiVec;
use std::ops::{Generator, GeneratorState}; use std::ops::{Generator, GeneratorState};
use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
use std::thread::yield_now; use std::thread::yield_now;
use macroquad::prelude::screen_width; use macroquad::prelude::screen_width;
@ -12,7 +13,26 @@ pub struct Algorithm{
} }
impl Algorithm { pub enum AlgoEnum{
InsertSort(Box<dyn Generator<Yield=GuiVec, Return=()>>),
}
impl Algorithm{
pub fn start(length:i32, algorithm:u32) -> impl Generator<Yield=GuiVec, Return=()>{
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<Yield=GuiVec, Return=()>{ pub fn insertSort(length:i32) -> impl Generator<Yield=GuiVec, Return=()>{
let mut list = GuiVec::new(screen_width(), screen_height(), length); let mut list = GuiVec::new(screen_width(), screen_height(), length);
@ -23,7 +43,6 @@ impl Algorithm {
let mut j = index; let mut j = index;
while j>0 && list.lessThan(j, j-1){ while j>0 && list.lessThan(j, j-1){
yield list.swap(j, j-1); yield list.swap(j, j-1);
j = j-1; j = j-1;
} }
} }

View file

@ -1,13 +1,51 @@
use std::ops::Generator; use std::ops::{Generator, GeneratorState};
use macroquad::prelude::screen_width; 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 macroquad::window::screen_height;
use crate::Algorithm::Algorithm; use crate::Algorithm::Algorithm;
use crate::GuiHookVec::GuiVec; use crate::GuiHookVec::GuiVec;
#[derive(PartialEq)] pub struct State{
pub enum GeneratorEnum{
InsertSort,
BogoSort,
StalinSort,
BubbleSort,
} }
impl State{
pub async fn runInsertSort(length:i32, mut generator:impl Generator<Yield=GuiVec, Return=()>+ 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
}
}
}

View file

@ -13,61 +13,45 @@ use crate::GuiHookVec::GuiVec;
use std::ops::{Deref, Generator, GeneratorState}; use std::ops::{Deref, Generator, GeneratorState};
use macroquad::ui::root_ui; use macroquad::ui::root_ui;
use crate::State::GeneratorEnum; use crate::Algorithm::AlgoEnum;
#[macroquad::main("BeepSort")] #[macroquad::main("BeepSort")]
async fn main() { 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 mut finished = false;
let timeout = 0.0;
let mut paused = false; let mut length = 100;
let mut reset = false;
let mut algoChoise = GeneratorEnum::InsertSort;
loop{ loop{
let mut generator = Algorithm::Algorithm::insertSort(length); clear_background(WHITE);
if root_ui().button(Vec2::new(screen_width()*0.2, 50.), "Increase"){
while !reset { length += 10;
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
} }
algoChoise = GeneratorEnum::StalinSort; if root_ui().button(Vec2::new(screen_width()*0.3, 50.), "Decrease"){
reset = false; length -= 10;
finished = false; }
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
} }