pause and play, first step for the time line
This commit is contained in:
69
src/main.rs
69
src/main.rs
@@ -1,10 +1,12 @@
|
||||
mod polygon_draw;
|
||||
use iced::widget::text_input::Style;
|
||||
use polygon_draw::Polygon;
|
||||
|
||||
mod music;
|
||||
use music::Music;
|
||||
|
||||
mod utils;
|
||||
use utils::str_to_sec;
|
||||
|
||||
use std::fs;
|
||||
|
||||
@@ -17,6 +19,7 @@ use iced::{
|
||||
},
|
||||
};
|
||||
|
||||
use regex::Regex;
|
||||
use std::f32::consts::PI;
|
||||
use std::time::Instant;
|
||||
|
||||
@@ -24,6 +27,7 @@ use kira::{
|
||||
AudioManager, AudioManagerSettings, DefaultBackend, sound::static_sound::StaticSoundData,
|
||||
};
|
||||
|
||||
use crate::utils::delta_to_string;
|
||||
fn main() -> iced::Result {
|
||||
iced::application("My App", MyApp::update, MyApp::view)
|
||||
.theme(|_| Theme::Dark)
|
||||
@@ -46,6 +50,10 @@ enum Message {
|
||||
Save,
|
||||
Load,
|
||||
FileNameChanged(String),
|
||||
TogglePaused,
|
||||
SetMusicLength,
|
||||
LengthChange(String),
|
||||
ChangeDelta(f32),
|
||||
}
|
||||
|
||||
struct MyApp {
|
||||
@@ -57,6 +65,7 @@ struct MyApp {
|
||||
all_sounds: Vec<String>,
|
||||
all_saves: Vec<String>,
|
||||
current_delta: f32,
|
||||
str_music_length: String,
|
||||
}
|
||||
|
||||
impl MyApp {
|
||||
@@ -67,12 +76,13 @@ impl MyApp {
|
||||
Self {
|
||||
time_last_frame: Instant::now(),
|
||||
audio_manager: manager,
|
||||
show_save_panel: true,
|
||||
paused: false,
|
||||
show_save_panel: false,
|
||||
paused: true,
|
||||
all_sounds: load_path_sounds(),
|
||||
all_saves: load_path_saves(),
|
||||
music: Music::default(),
|
||||
current_delta: 0.0,
|
||||
str_music_length: "01:00:00".to_string(),
|
||||
},
|
||||
Task::none(),
|
||||
)
|
||||
@@ -133,6 +143,22 @@ impl MyApp {
|
||||
}
|
||||
Message::ToggleSavePanel => self.show_save_panel = !self.show_save_panel,
|
||||
Message::FileNameChanged(s) => self.music.file_name = s,
|
||||
Message::LengthChange(s) => self.str_music_length = s,
|
||||
Message::TogglePaused => {
|
||||
self.paused = !self.paused;
|
||||
if !self.paused {
|
||||
self.time_last_frame = Instant::now();
|
||||
}
|
||||
}
|
||||
Message::SetMusicLength => {
|
||||
if self.is_length_valid() {
|
||||
self.music.length = str_to_sec(&self.str_music_length)
|
||||
}
|
||||
}
|
||||
Message::ChangeDelta(f) => {
|
||||
self.current_delta = f;
|
||||
self.music.fix_teta(self.current_delta);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -175,7 +201,7 @@ impl MyApp {
|
||||
})
|
||||
.step(PI / 84f32), // 84 | 4 for do PI / 4
|
||||
]
|
||||
.spacing(20)
|
||||
.spacing(10)
|
||||
.into()
|
||||
})
|
||||
.collect();
|
||||
@@ -198,7 +224,7 @@ impl MyApp {
|
||||
.chain(ngon_options)
|
||||
.collect();
|
||||
|
||||
let polygon_column = scrollable(Column::with_children(polygon_rows));
|
||||
let polygon_column = scrollable(Column::with_children(polygon_rows).spacing(20));
|
||||
let mut save_panel: Vec<Element<Message>> = vec![
|
||||
button("Toggle Save Panel")
|
||||
.on_press(Message::ToggleSavePanel)
|
||||
@@ -242,7 +268,7 @@ impl MyApp {
|
||||
pick_list(all_options, Some("Choose polygon".to_string()), |s| {
|
||||
Message::AddPolygon(s)
|
||||
}),
|
||||
polygon_column.spacing(10),
|
||||
polygon_column,
|
||||
]
|
||||
.spacing(10)
|
||||
.height(Length::FillPortion(1))
|
||||
@@ -250,17 +276,42 @@ impl MyApp {
|
||||
]
|
||||
.height(Length::FillPortion(2))
|
||||
.spacing(20),
|
||||
row![text("futur time line")]
|
||||
.height(Length::FillPortion(1))
|
||||
.width(Length::FillPortion(1)),
|
||||
column![
|
||||
text("TimeLine"),
|
||||
row![
|
||||
button("Toggle Play").on_press(Message::TogglePaused),
|
||||
text(format!(
|
||||
"{}/{}",
|
||||
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.),
|
||||
]
|
||||
.height(Length::FillPortion(1))
|
||||
.width(Length::FillPortion(1)),
|
||||
]
|
||||
.spacing(25)
|
||||
.padding(25)
|
||||
.into()
|
||||
}
|
||||
|
||||
fn is_length_valid(&self) -> bool {
|
||||
let re = Regex::new(r"^\d{2}:\d{2}:\d{2}$").unwrap();
|
||||
re.is_match(self.str_music_length.as_str())
|
||||
}
|
||||
fn subscription(&self) -> iced::Subscription<Message> {
|
||||
iced::Subscription::batch([
|
||||
window::events().map(|(_id, event)| Message::WindowEvent(event)),
|
||||
//window::events().map(|(_id, event)| Message::WindowEvent(event)),
|
||||
iced::time::every(std::time::Duration::from_millis(16)).map(|_| Message::Tick),
|
||||
])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user