vizualizer of different polygon frame
This commit is contained in:
103
src/main.rs
103
src/main.rs
@@ -12,12 +12,17 @@ use std::fs;
|
||||
|
||||
use iced::{Element, window};
|
||||
use iced::{
|
||||
Length, Task, Theme,
|
||||
Font,
|
||||
font::Family,
|
||||
widget::{
|
||||
Column, TextInput, button, canvas, column, container, pick_list, row, scrollable, slider,
|
||||
text,
|
||||
TextInput, column, text, text_input,
|
||||
text_input::{Icon, Side},
|
||||
},
|
||||
};
|
||||
use iced::{
|
||||
Length, Task, Theme,
|
||||
widget::{Column, button, canvas, container, pick_list, row, scrollable, slider},
|
||||
};
|
||||
|
||||
use regex::Regex;
|
||||
use std::f32::consts::PI;
|
||||
@@ -37,7 +42,6 @@ fn main() -> iced::Result {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
WindowEvent(window::Event),
|
||||
ButtonPressedIncrement,
|
||||
ButtonPressedDecrement,
|
||||
Tick,
|
||||
@@ -89,9 +93,6 @@ impl MyApp {
|
||||
}
|
||||
fn update(&mut self, message: Message) {
|
||||
match message {
|
||||
Message::WindowEvent(window::Event::Resized(size)) => {
|
||||
println!("Resize detected: {}x{}", size.width, size.height);
|
||||
}
|
||||
Message::ButtonPressedIncrement => self.music.nb_sec_for_rev += 0.5,
|
||||
Message::ButtonPressedDecrement => {
|
||||
if self.music.nb_sec_for_rev > 0.5 {
|
||||
@@ -102,7 +103,11 @@ impl MyApp {
|
||||
self.music.add_polygon(self.current_delta, s);
|
||||
}
|
||||
Message::Tick => {
|
||||
if self.current_delta >= self.music.length {
|
||||
self.paused = true
|
||||
}
|
||||
if !self.paused {
|
||||
self.music.current_delta = self.current_delta;
|
||||
let time_btw = Instant::now().duration_since(self.time_last_frame);
|
||||
self.current_delta += time_btw.as_millis() as f32 / 1000.0;
|
||||
self.music
|
||||
@@ -126,10 +131,11 @@ impl MyApp {
|
||||
}
|
||||
Message::Save => {
|
||||
let json = serde_json::to_string_pretty(&self.music).unwrap();
|
||||
fs::write(format!("./saves/{0}", &self.music.file_name), json).unwrap();
|
||||
fs::write(format!("./saves/{0}.pmx", &self.music.file_name), json).unwrap();
|
||||
self.all_saves = load_path_saves();
|
||||
}
|
||||
Message::Load => {
|
||||
let json = fs::read_to_string(format!("./saves/{0}", &self.music.file_name));
|
||||
let json = fs::read_to_string(format!("./saves/{0}.pmx", &self.music.file_name));
|
||||
match json {
|
||||
Ok(j) => {
|
||||
let decoded: Music = serde_json::from_str(&j).unwrap();
|
||||
@@ -158,16 +164,22 @@ impl MyApp {
|
||||
Message::ChangeDelta(f) => {
|
||||
self.current_delta = f;
|
||||
self.music.fix_teta(self.current_delta);
|
||||
// update the red dot on canvas
|
||||
if self.paused {
|
||||
self.update(Message::TogglePaused);
|
||||
self.update(Message::Tick);
|
||||
self.update(Message::TogglePaused);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self) -> iced::Element<Message> {
|
||||
let txt_nb_rev = format!(
|
||||
"Number of second for revolution : {}",
|
||||
self.music.nb_sec_for_rev
|
||||
);
|
||||
let txt_nb_rev = if self.music.nb_sec_for_rev % 1. != 0.0 {
|
||||
format!("{} sec/revolution", self.music.nb_sec_for_rev)
|
||||
} else {
|
||||
format!("{}.0 sec/revolution", self.music.nb_sec_for_rev)
|
||||
};
|
||||
|
||||
let mut i = 0;
|
||||
let entries = self.all_sounds.clone();
|
||||
@@ -250,8 +262,29 @@ impl MyApp {
|
||||
save_panel.push(button("Load").on_press(Message::Load).into());
|
||||
}
|
||||
column![
|
||||
text("Polymusic").size(32.0),
|
||||
text(&self.music.file_name).size(32.0),
|
||||
row(save_panel).spacing(20),
|
||||
row![
|
||||
text("Music Length").size(20),
|
||||
TextInput::new("MM:SS:CS", &self.str_music_length)
|
||||
.on_input(|new_value| Message::LengthChange(new_value))
|
||||
.icon(Icon {
|
||||
font: Font::DEFAULT,
|
||||
code_point: if self.is_length_valid() {
|
||||
'\u{2705}'
|
||||
} else {
|
||||
'\u{274C}'
|
||||
},
|
||||
size: None,
|
||||
spacing: 10.,
|
||||
side: Side::Left,
|
||||
}),
|
||||
button("Valid").on_press(Message::SetMusicLength),
|
||||
text(txt_nb_rev).size(20),
|
||||
button("Increment").on_press(Message::ButtonPressedIncrement),
|
||||
button("Decrement").on_press(Message::ButtonPressedDecrement),
|
||||
]
|
||||
.spacing(20),
|
||||
row![
|
||||
container(
|
||||
canvas(self.music.current_frame(self.current_delta))
|
||||
@@ -259,11 +292,6 @@ impl MyApp {
|
||||
.width(Length::FillPortion(1))
|
||||
),
|
||||
column![
|
||||
text(txt_nb_rev),
|
||||
row![
|
||||
button("Increment").on_press(Message::ButtonPressedIncrement),
|
||||
button("Decrement").on_press(Message::ButtonPressedDecrement),
|
||||
],
|
||||
text("Polygon options"),
|
||||
pick_list(all_options, Some("Choose polygon".to_string()), |s| {
|
||||
Message::AddPolygon(s)
|
||||
@@ -277,7 +305,6 @@ impl MyApp {
|
||||
.height(Length::FillPortion(2))
|
||||
.spacing(20),
|
||||
column![
|
||||
text("TimeLine"),
|
||||
row![
|
||||
button("Toggle Play").on_press(Message::TogglePaused),
|
||||
text(format!(
|
||||
@@ -285,17 +312,19 @@ impl MyApp {
|
||||
delta_to_string(self.current_delta),
|
||||
delta_to_string(self.music.length)
|
||||
))
|
||||
],
|
||||
row![
|
||||
text("Music Length"),
|
||||
TextInput::new("MM:SS:CS", &self.str_music_length)
|
||||
.on_input(|new_value| Message::LengthChange(new_value)),
|
||||
button("Valid").on_press(Message::SetMusicLength),
|
||||
],
|
||||
slider(0.0..=self.music.length, self.current_delta, move |f| {
|
||||
Message::ChangeDelta(f)
|
||||
})
|
||||
.step(&self.music.length / 10_000.),
|
||||
.size(20.0)
|
||||
]
|
||||
.spacing(20),
|
||||
column![
|
||||
slider(0.0..=self.music.length, self.current_delta, move |f| {
|
||||
Message::ChangeDelta(f)
|
||||
})
|
||||
.step(&self.music.length / 10_000.),
|
||||
canvas(&self.music)
|
||||
.height(Length::FillPortion(1))
|
||||
.width(Length::FillPortion(1))
|
||||
]
|
||||
.spacing(0),
|
||||
]
|
||||
.height(Length::FillPortion(1))
|
||||
.width(Length::FillPortion(1)),
|
||||
@@ -332,6 +361,14 @@ fn load_path_saves() -> Vec<String> {
|
||||
fs::read_dir("./saves")
|
||||
.unwrap()
|
||||
.filter_map(|res| res.ok())
|
||||
.map(|e| e.path().file_name().unwrap().to_str().unwrap().to_string())
|
||||
.map(|e| {
|
||||
e.path()
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.trim_end_matches(".pmx")
|
||||
.to_string()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user