2022-11-06 21:44:59 +00:00
|
|
|
|
2023-03-04 20:47:37 +00:00
|
|
|
|
2023-03-03 00:19:29 +00:00
|
|
|
use async_trait::async_trait;
|
2023-03-05 00:51:07 +00:00
|
|
|
use macroquad::audio::{play_sound_once, Sound, play_sound, PlaySoundParams};
|
2022-12-06 15:40:58 +00:00
|
|
|
use macroquad::color::{BROWN, WHITE};
|
2023-03-04 22:27:01 +00:00
|
|
|
use macroquad::{hash, time};
|
2023-02-27 01:58:34 +00:00
|
|
|
use macroquad::prelude::{clear_background, Vec2, BLACK};
|
2022-11-06 21:44:59 +00:00
|
|
|
use macroquad::rand::ChooseRandom;
|
|
|
|
use macroquad::shapes::draw_rectangle;
|
2023-02-27 01:58:34 +00:00
|
|
|
use macroquad::text::draw_text;
|
|
|
|
use macroquad::time::{get_frame_time, get_fps};
|
2022-12-06 15:40:58 +00:00
|
|
|
use macroquad::ui::root_ui;
|
|
|
|
use macroquad::window::{next_frame, screen_height, screen_width};
|
2022-11-06 21:44:59 +00:00
|
|
|
use crate::BarPlugin::Bar;
|
2023-03-04 23:21:12 +00:00
|
|
|
use crate::soundGenerator;
|
2023-03-04 20:47:37 +00:00
|
|
|
|
2022-11-06 21:44:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct GuiVec{
|
2023-02-28 22:40:08 +00:00
|
|
|
pub list: Vec<Bar>,
|
2022-11-06 21:44:59 +00:00
|
|
|
initialSize:usize,
|
|
|
|
pub lastTime:f64,
|
2022-11-07 00:21:18 +00:00
|
|
|
pub reads:i32,
|
|
|
|
pub writes:i32,
|
|
|
|
pub comps:i32,
|
2022-12-06 15:40:58 +00:00
|
|
|
isPaused:bool,
|
2023-02-26 22:25:41 +00:00
|
|
|
delay:f32,
|
2023-02-27 01:58:34 +00:00
|
|
|
pub done:bool,
|
|
|
|
renderSkip:i32,
|
2023-03-04 03:00:49 +00:00
|
|
|
skipped:i32,
|
|
|
|
lastTouched:Vec<usize>,
|
2023-03-04 23:21:12 +00:00
|
|
|
lastPlayed:f64,
|
|
|
|
sounds:Vec<Sound>
|
2022-11-06 21:44:59 +00:00
|
|
|
}
|
2023-03-03 00:19:29 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait SortingList{
|
2022-11-06 21:44:59 +00:00
|
|
|
|
2023-03-04 22:11:05 +00:00
|
|
|
async fn new(length:usize, delay:f32) -> Self;
|
2023-03-03 00:19:29 +00:00
|
|
|
|
|
|
|
fn len(&self) -> usize;
|
|
|
|
|
|
|
|
async fn swap(&mut self, index1:usize, index2:usize) -> bool;
|
|
|
|
|
|
|
|
async fn draw(&mut self);
|
|
|
|
|
|
|
|
fn randomize(&mut self);
|
|
|
|
|
|
|
|
fn elements(&mut self) -> std::slice::Iter<'_, Bar>;
|
|
|
|
|
|
|
|
fn get(&mut self, i:usize)-> &Bar;
|
|
|
|
|
|
|
|
fn lessThan(&mut self, a:usize, b:usize) -> bool;
|
|
|
|
|
|
|
|
|
2023-03-04 00:52:39 +00:00
|
|
|
fn lessThanEqual(&mut self, a:usize, b:usize) -> bool;
|
2023-03-03 00:19:29 +00:00
|
|
|
|
|
|
|
fn isSorted(&mut self) -> bool;
|
|
|
|
|
|
|
|
async fn set(&mut self, i:usize, elem:Bar) -> bool;
|
|
|
|
|
|
|
|
async fn show(&mut self);
|
|
|
|
|
|
|
|
fn getListClone(&self) -> Vec<Bar>;
|
|
|
|
}
|
|
|
|
#[async_trait]
|
|
|
|
impl SortingList for GuiVec{
|
2023-02-26 22:25:41 +00:00
|
|
|
|
2023-03-04 22:11:05 +00:00
|
|
|
async fn new(length:usize, delay:f32) -> Self {
|
2022-11-08 05:32:39 +00:00
|
|
|
let colorStep = 360./length as f32;
|
2022-11-06 21:44:59 +00:00
|
|
|
let mut list:Vec<Bar> = vec!();
|
2023-03-04 22:11:05 +00:00
|
|
|
let freqStep = 50. + ((2000.-50.)/length as f32);
|
2023-03-04 22:36:25 +00:00
|
|
|
|
2022-11-06 22:10:16 +00:00
|
|
|
for i in 1..length+1 {
|
2023-03-04 22:11:05 +00:00
|
|
|
let frequency = i as f32 * freqStep;
|
2023-03-04 23:21:12 +00:00
|
|
|
list.push(Bar::new(i, (colorStep*i as f32)/360.));
|
2022-11-06 21:44:59 +00:00
|
|
|
}
|
2023-03-04 23:21:12 +00:00
|
|
|
|
|
|
|
//Generate sounds
|
|
|
|
let mut sounds = Vec::with_capacity(1000);
|
2023-03-05 02:58:25 +00:00
|
|
|
for i in (50..2051).step_by(2){
|
2023-03-05 03:16:59 +00:00
|
|
|
sounds.push(soundGenerator::generateTone(i as f32, 0.1).await);
|
2023-03-04 23:21:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 22:25:41 +00:00
|
|
|
GuiVec{
|
|
|
|
list,
|
|
|
|
initialSize:length as usize,
|
|
|
|
lastTime: 0.0 ,
|
|
|
|
reads:0,
|
|
|
|
writes:0,
|
|
|
|
comps:0,
|
|
|
|
isPaused:false,
|
|
|
|
delay,
|
2023-02-27 01:58:34 +00:00
|
|
|
done:false,
|
|
|
|
renderSkip:1,
|
2023-03-04 03:00:49 +00:00
|
|
|
skipped:0,
|
2023-03-04 22:27:01 +00:00
|
|
|
lastTouched:Vec::with_capacity(2),
|
|
|
|
lastPlayed:0.,
|
2023-03-04 23:21:12 +00:00
|
|
|
sounds,
|
2023-02-26 22:25:41 +00:00
|
|
|
}
|
2022-12-06 15:40:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 00:19:29 +00:00
|
|
|
async fn draw(&mut self){
|
2022-12-06 15:40:58 +00:00
|
|
|
let mut frames = 0.0;
|
|
|
|
let mut delayText = self.delay.to_string();
|
2023-02-27 01:58:34 +00:00
|
|
|
let mut renderSkipText = self.renderSkip.to_string();
|
2022-12-06 15:40:58 +00:00
|
|
|
|
|
|
|
loop {
|
2023-02-26 22:25:41 +00:00
|
|
|
|
2022-12-06 15:40:58 +00:00
|
|
|
|
2023-02-27 01:58:34 +00:00
|
|
|
if self.skipped >= self.renderSkip{
|
|
|
|
clear_background(WHITE);
|
2022-12-06 15:40:58 +00:00
|
|
|
|
2023-02-27 01:58:34 +00:00
|
|
|
for (count,bar) in self.list.iter().enumerate(){
|
2023-03-04 03:00:49 +00:00
|
|
|
let mut color = bar.color;
|
|
|
|
if self.lastTouched.contains(&count){
|
|
|
|
color = BLACK;
|
|
|
|
}
|
|
|
|
draw_rectangle(screen_width() * ((count as f32)/(self.initialSize as f32)),screen_height() - (screen_height()/((self.len()) as f32))*bar.position as f32 , screen_width()/((self.len()) as f32), (screen_height()/((self.len()) as f32))*bar.position as f32, color);
|
2023-02-28 22:40:08 +00:00
|
|
|
|
2023-02-27 01:58:34 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 03:32:52 +00:00
|
|
|
|
2023-02-27 01:58:34 +00:00
|
|
|
root_ui().window(hash!(),Vec2::new(screen_width()*0.01, 5.), Vec2::new(800.0, 50.), |ui|{
|
|
|
|
ui.input_text(hash!(), "Delay (ms)", &mut delayText);
|
|
|
|
ui.input_text(hash!(), "StepsPrFrame (How many steps of the algorithm pr frame)", &mut renderSkipText);
|
2023-03-04 03:32:52 +00:00
|
|
|
draw_text(&format!("FPS: {}", get_fps()), screen_width()*0.01 + 40., 80.0, 20.0, BLACK);
|
|
|
|
draw_text(&format!("Array reads: {}", self.reads), screen_width()*0.01 + 40., 110.0, 20.0, BLACK);
|
|
|
|
draw_text(&format!("Array writes: {}", self.writes), screen_width()*0.01 + 40., 140.0, 20.0, BLACK);
|
|
|
|
draw_text(&format!("Comparisons: {}", self.comps), screen_width()*0.01 + 40., 170.0, 20.0, BLACK);
|
|
|
|
|
|
|
|
|
2023-02-27 01:58:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if root_ui().button(Vec2::new(screen_width()*0.01, 60.), "Exit"){
|
|
|
|
self.done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if root_ui().button(Vec2::new(screen_width()*0.01, 90.), "Pause"){
|
|
|
|
self.isPaused = !self.isPaused;
|
|
|
|
|
|
|
|
}
|
|
|
|
self.renderSkip = match renderSkipText.parse::<i32>(){
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(_) => 1
|
|
|
|
};
|
|
|
|
|
|
|
|
self.delay = match (delayText.parse::<f32>(), self.isPaused){
|
|
|
|
(_, true) => f32::MAX,
|
|
|
|
(Ok(a), false) => a,
|
|
|
|
(Err(_), _)=> {f32::MAX}
|
|
|
|
};
|
|
|
|
next_frame().await;
|
|
|
|
self.skipped = 0;
|
|
|
|
}else{
|
|
|
|
self.skipped += 1;
|
2023-02-26 22:25:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 01:58:34 +00:00
|
|
|
if frames >= self.delay && !self.done{
|
2022-12-06 15:40:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-02-27 02:27:17 +00:00
|
|
|
|
|
|
|
|
2023-02-27 01:58:34 +00:00
|
|
|
frames += get_frame_time()* 1000.;
|
2022-12-06 15:40:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 21:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-03 00:19:29 +00:00
|
|
|
fn len(&self) -> usize{
|
2022-11-06 21:44:59 +00:00
|
|
|
self.list.len()
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:19:29 +00:00
|
|
|
async fn swap(&mut self, index1:usize, index2:usize) -> bool{
|
2022-11-07 00:21:18 +00:00
|
|
|
self.writes += 2;
|
|
|
|
self.reads += 2;
|
2022-11-06 21:44:59 +00:00
|
|
|
self.list.swap(index1, index2);
|
2023-03-04 22:27:01 +00:00
|
|
|
|
|
|
|
|
2023-03-05 03:16:59 +00:00
|
|
|
if time::get_time() + 0.1 >= self.lastPlayed{
|
2023-03-05 00:51:07 +00:00
|
|
|
play_sound(self.sounds[ (self.list[index1].position * 1000 / self.list.len()) ], PlaySoundParams{
|
|
|
|
looped:false,
|
|
|
|
volume:0.5
|
|
|
|
});
|
2023-03-05 03:16:59 +00:00
|
|
|
self.lastPlayed = time::get_time()+0.1;
|
2023-03-04 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 03:00:49 +00:00
|
|
|
self.lastTouched.clear();
|
|
|
|
self.lastTouched.push(index1);
|
|
|
|
self.lastTouched.push(index2);
|
2022-12-06 15:40:58 +00:00
|
|
|
self.draw().await;
|
2023-03-04 03:00:49 +00:00
|
|
|
|
2023-02-26 22:25:41 +00:00
|
|
|
self.done
|
2022-11-06 21:44:59 +00:00
|
|
|
}
|
2023-03-03 00:19:29 +00:00
|
|
|
fn randomize(&mut self){
|
2022-11-06 21:44:59 +00:00
|
|
|
self.list.shuffle();
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:19:29 +00:00
|
|
|
fn elements(&mut self) -> std::slice::Iter<'_, Bar> {
|
2022-11-06 21:44:59 +00:00
|
|
|
self.list.iter()
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:19:29 +00:00
|
|
|
fn get(&mut self, i:usize)-> &Bar{
|
2022-11-07 00:21:18 +00:00
|
|
|
self.reads += 1;
|
2023-03-04 03:00:49 +00:00
|
|
|
self.lastTouched.clear();
|
|
|
|
self.lastTouched.push(i);
|
2022-11-06 21:44:59 +00:00
|
|
|
self.list.get(i).unwrap()
|
2023-03-04 03:00:49 +00:00
|
|
|
|
2022-11-06 21:44:59 +00:00
|
|
|
}
|
2023-03-03 00:19:29 +00:00
|
|
|
fn lessThan(&mut self, a:usize, b:usize) -> bool{
|
2022-11-07 00:21:18 +00:00
|
|
|
self.comps += 1;
|
|
|
|
return self.get(a).position < self.get(b).position
|
|
|
|
}
|
2023-03-04 00:52:39 +00:00
|
|
|
fn lessThanEqual(&mut self, a:usize, b:usize) -> bool{
|
2023-02-28 23:01:34 +00:00
|
|
|
self.comps += 1;
|
|
|
|
return self.get(a).position <= b
|
|
|
|
}
|
2023-03-03 00:19:29 +00:00
|
|
|
fn isSorted(&mut self) -> bool{
|
2022-11-07 00:44:35 +00:00
|
|
|
self.reads += self.len() as i32;
|
|
|
|
let mut prev = 0;
|
|
|
|
for bar in self.list.iter() {
|
|
|
|
if bar.position < prev{
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
prev = bar.position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2023-03-03 00:19:29 +00:00
|
|
|
async fn set(&mut self, i:usize, elem:Bar) -> bool{
|
2023-03-02 23:49:41 +00:00
|
|
|
|
|
|
|
self.writes += 1;
|
|
|
|
self.reads += 1;
|
|
|
|
self.list[i] = elem;
|
|
|
|
self.draw().await;
|
2023-03-05 03:16:59 +00:00
|
|
|
if time::get_time() + 0.1 >= self.lastPlayed{
|
2023-03-05 02:58:25 +00:00
|
|
|
play_sound(self.sounds[ (self.list[i].position * 1000 / self.list.len()) ], PlaySoundParams{
|
|
|
|
looped:false,
|
2023-03-05 03:13:02 +00:00
|
|
|
volume:1.
|
2023-03-05 02:58:25 +00:00
|
|
|
});
|
2023-03-05 03:16:59 +00:00
|
|
|
self.lastPlayed = time::get_time()+0.1;
|
2023-03-05 02:58:25 +00:00
|
|
|
}
|
2023-03-04 03:00:49 +00:00
|
|
|
self.lastTouched.clear();
|
|
|
|
self.lastTouched.push(i);
|
2023-03-02 23:49:41 +00:00
|
|
|
self.done
|
|
|
|
|
|
|
|
}
|
2023-03-03 00:19:29 +00:00
|
|
|
async fn show(&mut self){
|
2023-02-26 22:25:41 +00:00
|
|
|
loop{
|
|
|
|
if !self.done{
|
|
|
|
self.draw().await
|
|
|
|
}else{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:19:29 +00:00
|
|
|
fn getListClone(&self) -> Vec<Bar>{
|
|
|
|
self.list.clone()
|
|
|
|
}
|
|
|
|
|
2022-11-06 21:44:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 00:37:09 +00:00
|
|
|
pub struct NonGuiVec{
|
2023-03-03 00:19:29 +00:00
|
|
|
pub list: Vec<Bar>,
|
|
|
|
|
|
|
|
}
|
|
|
|
#[async_trait]
|
|
|
|
impl SortingList for NonGuiVec{
|
2023-03-04 22:11:05 +00:00
|
|
|
async fn new(length:usize, delay:f32) -> Self{
|
2023-03-03 00:19:29 +00:00
|
|
|
let mut list = Vec::new();
|
|
|
|
for i in 0..(length as usize){
|
2023-03-04 23:21:12 +00:00
|
|
|
list.push(Bar::new(i, i as f32))
|
2023-03-03 00:19:29 +00:00
|
|
|
}
|
|
|
|
NonGuiVec { list: list }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> usize{
|
|
|
|
self.list.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn swap(&mut self, index1:usize, index2:usize) -> bool{
|
|
|
|
self.list.swap(index1, index2);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn draw(&mut self){
|
|
|
|
self.swap(0, 0).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn randomize(&mut self){
|
|
|
|
self.list.shuffle();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn elements(&mut self) -> std::slice::Iter<'_, Bar> {
|
|
|
|
self.list.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&mut self, i:usize)-> &Bar{
|
|
|
|
|
|
|
|
self.list.get(i).unwrap()
|
|
|
|
}
|
|
|
|
fn lessThan(&mut self, a:usize, b:usize) -> bool{
|
|
|
|
|
|
|
|
return self.get(a).position < self.get(b).position
|
|
|
|
}
|
2023-03-04 00:52:39 +00:00
|
|
|
fn lessThanEqual(&mut self, a:usize, b:usize) -> bool{
|
2023-03-03 00:19:29 +00:00
|
|
|
return self.get(a).position <= b
|
|
|
|
}
|
|
|
|
fn isSorted(&mut self) -> bool{
|
|
|
|
let mut prev = 0;
|
|
|
|
for bar in self.list.iter() {
|
|
|
|
if bar.position < prev{
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
prev = bar.position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
async fn set(&mut self, i:usize, elem:Bar) -> bool{
|
|
|
|
|
|
|
|
self.list[i] = elem;
|
|
|
|
self.draw().await;
|
2023-03-04 03:00:49 +00:00
|
|
|
|
2023-03-03 00:19:29 +00:00
|
|
|
false
|
|
|
|
|
|
|
|
}
|
|
|
|
async fn show(&mut self){
|
|
|
|
|
|
|
|
}
|
|
|
|
fn getListClone(&self) -> Vec<Bar>{
|
|
|
|
self.list.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|