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::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<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=()>{
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;
}
}

View file

@ -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<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,62 +13,46 @@ 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;
if root_ui().button(Vec2::new(screen_width()*0.2, 50.), "Increase"){
length += 10;
}
};
lasttime = get_time();
if root_ui().button(Vec2::new(screen_width()*0.3, 50.), "Decrease"){
length -= 10;
}
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;
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, 95.), "Reset!"){
reset = true;
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
}
algoChoise = GeneratorEnum::StalinSort;
reset = false;
finished = false;
}