add audio but better with kira
This commit is contained in:
39
src/main.rs
39
src/main.rs
@@ -1,15 +1,20 @@
|
||||
mod polygon_draw;
|
||||
|
||||
use iced::{
|
||||
Settings, Task, Theme,
|
||||
Task, Theme,
|
||||
time::{self, Duration},
|
||||
widget::{button, canvas, column, row, text},
|
||||
};
|
||||
use polygon_draw::{Polygon, PolygonFrame};
|
||||
|
||||
use std::f32::consts::PI;
|
||||
use std::io::BufReader;
|
||||
use std::time::Instant;
|
||||
|
||||
use kira::{
|
||||
AudioManager, AudioManagerSettings, DefaultBackend, sound::static_sound::StaticSoundData,
|
||||
};
|
||||
|
||||
fn main() -> iced::Result {
|
||||
iced::application("My App", MyApp::update, MyApp::view)
|
||||
.theme(|_| Theme::Dark)
|
||||
@@ -28,20 +33,27 @@ struct MyApp {
|
||||
polys: PolygonFrame,
|
||||
time_last_frame: Instant,
|
||||
nb_sec_for_rev: u32,
|
||||
audio_manager: AudioManager,
|
||||
default_sound: StaticSoundData,
|
||||
}
|
||||
|
||||
impl MyApp {
|
||||
fn new() -> (Self, Task<Message>) {
|
||||
let manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())
|
||||
.expect("Error to load AudioManager");
|
||||
let sound_data = StaticSoundData::from_file("assets/tick.ogg").expect("Fail to load audio");
|
||||
(
|
||||
Self {
|
||||
nb_sec_for_rev: 4,
|
||||
time_last_frame: Instant::now(),
|
||||
audio_manager: manager,
|
||||
default_sound: sound_data,
|
||||
polys: PolygonFrame {
|
||||
teta: 0.0,
|
||||
polygons: vec![
|
||||
//Polygon::n_gon(0.0, 12),
|
||||
Polygon::nr_10a_in_42(0.0),
|
||||
Polygon::nr_10b_in_42(0.0),
|
||||
Polygon::triangle(0.1),
|
||||
Polygon::square(1.0),
|
||||
],
|
||||
},
|
||||
},
|
||||
@@ -53,17 +65,27 @@ impl MyApp {
|
||||
Message::ButtonPressedIncrement => self.nb_sec_for_rev += 1,
|
||||
Message::ButtonPressedDecrement => {
|
||||
if self.nb_sec_for_rev > 1 {
|
||||
self.nb_sec_for_rev -= 1
|
||||
self.nb_sec_for_rev -= 1;
|
||||
}
|
||||
}
|
||||
Message::Tick => {
|
||||
let time_btw = Instant::now().duration_since(self.time_last_frame);
|
||||
let teta_temp = self.polys.teta;
|
||||
self.polys.teta += 2.0
|
||||
* PI
|
||||
* (1.0 / self.nb_sec_for_rev as f32)
|
||||
* (time_btw.as_millis() as f32 / 1_000.0);
|
||||
self.polys.teta %= 2.0 * PI;
|
||||
//println!("Teta : {}", self.polys.teta);
|
||||
if self
|
||||
.polys
|
||||
.have_point_polygon_btw(teta_temp, self.polys.teta)
|
||||
{
|
||||
self.audio_manager
|
||||
.play(self.default_sound.clone())
|
||||
.expect("Error to play sound");
|
||||
//do_sound("assets/tick.mp3");
|
||||
}
|
||||
self.time_last_frame = Instant::now();
|
||||
}
|
||||
}
|
||||
@@ -90,3 +112,12 @@ impl MyApp {
|
||||
time::every(Duration::from_millis(1)).map(|_| Message::Tick)
|
||||
}
|
||||
}
|
||||
/*
|
||||
fn do_sound(file_name: &str) {
|
||||
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
|
||||
let sink = rodio::Sink::try_new(&handle).unwrap();
|
||||
|
||||
let file = std::fs::File::open(file_name).unwrap();
|
||||
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
|
||||
sink.sleep_until_end();
|
||||
}*/
|
||||
|
||||
@@ -6,6 +6,7 @@ use iced::widget::canvas;
|
||||
use iced::widget::canvas::Stroke;
|
||||
use iced::widget::canvas::Style;
|
||||
use iced::{Color, Rectangle, Renderer, Theme};
|
||||
use std::time::Instant;
|
||||
|
||||
pub trait RotationExt {
|
||||
fn rotate(&mut self, teta: f32) -> Self;
|
||||
@@ -24,6 +25,17 @@ pub struct PolygonFrame {
|
||||
pub polygons: Vec<Polygon>,
|
||||
}
|
||||
|
||||
impl PolygonFrame {
|
||||
pub fn have_point_polygon_btw(&self, before: f32, after: f32) -> bool {
|
||||
for poly in &self.polygons {
|
||||
if poly.have_points_btw(before, after) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message> canvas::Program<Message> for PolygonFrame {
|
||||
// No internal state
|
||||
type State = ();
|
||||
@@ -97,6 +109,19 @@ pub struct Polygon {
|
||||
}
|
||||
#[warn(dead_code)]
|
||||
impl Polygon {
|
||||
pub fn have_points_btw(&self, before: f32, after: f32) -> bool {
|
||||
let mut p_g;
|
||||
for p in self.points_teta.clone() {
|
||||
p_g = p + self.global_teta;
|
||||
if before < p_g && p_g < after {
|
||||
return true;
|
||||
}
|
||||
if p_g > after {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
false
|
||||
}
|
||||
pub fn n_gon(teta: f32, n_side: u8) -> Self {
|
||||
let mut v: Vec<f32> = Vec::with_capacity(n_side as usize);
|
||||
for i in 0..n_side {
|
||||
@@ -112,11 +137,11 @@ impl Polygon {
|
||||
}
|
||||
|
||||
pub fn triangle(teta: f32) -> Self {
|
||||
Polygon::n_gon(teta, 2)
|
||||
Polygon::n_gon(teta, 3)
|
||||
}
|
||||
|
||||
pub fn square(teta: f32) -> Self {
|
||||
Polygon::n_gon(teta, 2)
|
||||
Polygon::n_gon(teta, 4)
|
||||
}
|
||||
|
||||
pub fn nr_6_in_30(teta: f32) -> Self {
|
||||
|
||||
Reference in New Issue
Block a user