add ngon and change sound on polygon
This commit is contained in:
65
src/main.rs
65
src/main.rs
@@ -1,5 +1,7 @@
|
|||||||
mod polygon_draw;
|
mod polygon_draw;
|
||||||
|
|
||||||
|
use std::{fs, io};
|
||||||
|
|
||||||
use iced::{
|
use iced::{
|
||||||
Color, Element, Task, Theme,
|
Color, Element, Task, Theme,
|
||||||
time::{self, Duration},
|
time::{self, Duration},
|
||||||
@@ -30,6 +32,7 @@ enum Message {
|
|||||||
ChangeTeta(usize, f32),
|
ChangeTeta(usize, f32),
|
||||||
Remove(usize),
|
Remove(usize),
|
||||||
ChangeColor(usize, String),
|
ChangeColor(usize, String),
|
||||||
|
ChangeSound(usize, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MyApp {
|
struct MyApp {
|
||||||
@@ -72,6 +75,14 @@ impl MyApp {
|
|||||||
}
|
}
|
||||||
Message::AddPolygon(s) => {
|
Message::AddPolygon(s) => {
|
||||||
let mut poly: Polygon;
|
let mut poly: Polygon;
|
||||||
|
if s.starts_with("Ngon") {
|
||||||
|
if let Ok(sides) = s.trim_start_matches("Ngon").parse::<u8>() {
|
||||||
|
poly = Polygon::n_gon(0.0, sides, self.default_sound.clone());
|
||||||
|
poly.name = format!("Ngon_{}", sides);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
match s.as_str() {
|
match s.as_str() {
|
||||||
"Segment" => {
|
"Segment" => {
|
||||||
poly = Polygon::segment(0.0, self.default_sound.clone());
|
poly = Polygon::segment(0.0, self.default_sound.clone());
|
||||||
@@ -119,6 +130,7 @@ impl MyApp {
|
|||||||
}
|
}
|
||||||
_ => poly = Polygon::n_gon(0.0, 0, self.default_sound.clone()),
|
_ => poly = Polygon::n_gon(0.0, 0, self.default_sound.clone()),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
self.poly_frame.polygons.push(poly);
|
self.poly_frame.polygons.push(poly);
|
||||||
}
|
}
|
||||||
Message::Tick => {
|
Message::Tick => {
|
||||||
@@ -157,11 +169,16 @@ impl MyApp {
|
|||||||
}
|
}
|
||||||
self.poly_frame.polygons[i].color = c;
|
self.poly_frame.polygons[i].color = c;
|
||||||
}
|
}
|
||||||
|
Message::ChangeSound(i, s) => {
|
||||||
|
self.poly_frame.polygons[i].sound =
|
||||||
|
StaticSoundData::from_file(s).expect("Fail to load audio")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> iced::Element<Message> {
|
fn view(&self) -> iced::Element<Message> {
|
||||||
let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev);
|
let txt_nb_rev = format!("Revolution per second : {}", self.nb_sec_for_rev);
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
//Create all polygon options
|
//Create all polygon options
|
||||||
let polygon_rows: Vec<Element<Message>> = self
|
let polygon_rows: Vec<Element<Message>> = self
|
||||||
@@ -171,6 +188,11 @@ impl MyApp {
|
|||||||
.map(|polygon| {
|
.map(|polygon| {
|
||||||
let current_index = i;
|
let current_index = i;
|
||||||
i += 1;
|
i += 1;
|
||||||
|
let entries = fs::read_dir("./assets/")
|
||||||
|
.unwrap()
|
||||||
|
.map(|res| res.map(|e| e.path().to_str().unwrap().to_string()))
|
||||||
|
.collect::<Result<Vec<_>, io::Error>>()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
row![
|
row![
|
||||||
text(&polygon.name),
|
text(&polygon.name),
|
||||||
@@ -186,10 +208,31 @@ impl MyApp {
|
|||||||
Some("Color".to_string()),
|
Some("Color".to_string()),
|
||||||
move |s| { Message::ChangeColor(current_index, s) }
|
move |s| { Message::ChangeColor(current_index, s) }
|
||||||
),
|
),
|
||||||
|
pick_list(entries, Some("Sound".to_string()), move |s| {
|
||||||
|
Message::ChangeSound(current_index, s)
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
.into()
|
.into()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
let ngon_options: Vec<String> = (5..=42).map(|sides| format!("Ngon{}", sides)).collect();
|
||||||
|
let all_options: Vec<String> = [
|
||||||
|
"Segment",
|
||||||
|
"Triangle",
|
||||||
|
"Square",
|
||||||
|
"Nr6In30",
|
||||||
|
"Nr7In30",
|
||||||
|
"Nr8In30",
|
||||||
|
"Nr9In30",
|
||||||
|
"Nr8In42",
|
||||||
|
"Nr9In42",
|
||||||
|
"Nr10aIn42",
|
||||||
|
"Nr10bIn42",
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.chain(ngon_options.into_iter())
|
||||||
|
.collect();
|
||||||
|
|
||||||
let polygon_column = Column::with_children(polygon_rows);
|
let polygon_column = Column::with_children(polygon_rows);
|
||||||
column![
|
column![
|
||||||
@@ -203,25 +246,9 @@ impl MyApp {
|
|||||||
button("Decrement").on_press(Message::ButtonPressedDecrement),
|
button("Decrement").on_press(Message::ButtonPressedDecrement),
|
||||||
],
|
],
|
||||||
text("Polygon options"),
|
text("Polygon options"),
|
||||||
pick_list(
|
pick_list(all_options, Some("Choose polygon".to_string()), |s| {
|
||||||
[
|
Message::AddPolygon(s)
|
||||||
"Segment",
|
}),
|
||||||
"Triangle",
|
|
||||||
"Square",
|
|
||||||
"Nr6In30",
|
|
||||||
"Nr7In30",
|
|
||||||
"Nr8In30",
|
|
||||||
"Nr9In30",
|
|
||||||
"Nr8In42",
|
|
||||||
"Nr9In42",
|
|
||||||
"Nr10aIn42",
|
|
||||||
"Nr10bIn42"
|
|
||||||
]
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.to_vec(),
|
|
||||||
Some("Chose polygon".to_string()),
|
|
||||||
|s| { Message::AddPolygon(s) }
|
|
||||||
),
|
|
||||||
polygon_column,
|
polygon_column,
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -6,11 +6,7 @@ use iced::widget::canvas;
|
|||||||
use iced::widget::canvas::Stroke;
|
use iced::widget::canvas::Stroke;
|
||||||
use iced::widget::canvas::Style;
|
use iced::widget::canvas::Style;
|
||||||
use iced::{Color, Rectangle, Renderer, Theme};
|
use iced::{Color, Rectangle, Renderer, Theme};
|
||||||
use kira::PlaySoundError;
|
use kira::sound::static_sound::StaticSoundData;
|
||||||
use kira::{
|
|
||||||
AudioManager, AudioManagerSettings, DefaultBackend, sound::static_sound::StaticSoundData,
|
|
||||||
};
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
pub trait RotationExt {
|
pub trait RotationExt {
|
||||||
fn rotate(&mut self, teta: f32) -> Self;
|
fn rotate(&mut self, teta: f32) -> Self;
|
||||||
@@ -55,6 +51,16 @@ impl<Message> canvas::Program<Message> for PolygonFrame {
|
|||||||
let radius = frame.size().width / 2.0 - 32.0;
|
let radius = frame.size().width / 2.0 - 32.0;
|
||||||
let mut vec = Vector::new(0.0, -radius);
|
let mut vec = Vector::new(0.0, -radius);
|
||||||
let c = frame.center();
|
let c = frame.center();
|
||||||
|
// Draw Circle
|
||||||
|
let circle = canvas::Path::circle(frame.center(), radius);
|
||||||
|
frame.stroke(
|
||||||
|
&circle,
|
||||||
|
Stroke {
|
||||||
|
width: 6.0,
|
||||||
|
style: Style::Solid(Color::from_rgba(0.0, 0.0, 0.0, 1.0)),
|
||||||
|
..Stroke::default()
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// Draw all polygons by there points
|
// Draw all polygons by there points
|
||||||
for poly in &self.polygons {
|
for poly in &self.polygons {
|
||||||
@@ -77,16 +83,6 @@ impl<Message> canvas::Program<Message> for PolygonFrame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw Circle
|
|
||||||
let circle = canvas::Path::circle(frame.center(), radius);
|
|
||||||
frame.stroke(
|
|
||||||
&circle,
|
|
||||||
Stroke {
|
|
||||||
width: 6.0,
|
|
||||||
style: Style::Solid(Color::from_rgba(0.0, 0.0, 0.0, 1.0)),
|
|
||||||
..Stroke::default()
|
|
||||||
},
|
|
||||||
);
|
|
||||||
// Draw the red dot on the current position
|
// Draw the red dot on the current position
|
||||||
let dot = canvas::Path::circle(frame.center() + vec.rotate(self.teta), 5.0);
|
let dot = canvas::Path::circle(frame.center() + vec.rotate(self.teta), 5.0);
|
||||||
frame.fill(&dot, Color::from_rgb(1.0, 0.0, 0.0));
|
frame.fill(&dot, Color::from_rgb(1.0, 0.0, 0.0));
|
||||||
|
|||||||
Reference in New Issue
Block a user