add color picker and better buttons
This commit is contained in:
166
src/main.rs
166
src/main.rs
@@ -13,11 +13,12 @@ use utils::{is_delta_format_valid, str_to_sec};
|
||||
use std::fs;
|
||||
|
||||
use iced::widget::{TextInput, column, text};
|
||||
use iced::{Application, Element, Font, font};
|
||||
use iced::{Application, Element, Font, Subscription, font};
|
||||
use iced::{
|
||||
Color, Length, Task, Theme,
|
||||
widget::{Column, button, canvas, container, pick_list, row, scrollable, slider},
|
||||
};
|
||||
use iced_aw::widget::{ColorPicker, color_picker};
|
||||
|
||||
use std::f32::consts::PI;
|
||||
use std::time::Instant;
|
||||
@@ -50,6 +51,7 @@ fn main() -> iced::Result {
|
||||
};
|
||||
iced::application("My App", MyApp::update, MyApp::view)
|
||||
.theme(move |_| polytheme.clone())
|
||||
.font(iced_fonts::REQUIRED_FONT_BYTES)
|
||||
.font(FONT_BYTES)
|
||||
.default_font(FONT)
|
||||
.antialiasing(true)
|
||||
@@ -119,14 +121,12 @@ impl MyApp {
|
||||
}
|
||||
}
|
||||
Message::Remove(i) => {
|
||||
self.music.remove_polygon(self.current_delta, i - 1);
|
||||
self.music.remove_polygon(self.current_delta, i);
|
||||
}
|
||||
Message::ChangeTeta(i, teta) => {
|
||||
self.music.set_teta(self.current_delta, i, teta);
|
||||
}
|
||||
Message::ChangeColor(i, s) => {
|
||||
self.music.set_color(self.current_delta, i, s);
|
||||
}
|
||||
|
||||
Message::ChangeSound(i, s) => {
|
||||
let sound = StaticSoundData::from_file(format!("./assets/{s}"))
|
||||
.expect("Fail to load audio");
|
||||
@@ -199,16 +199,52 @@ impl MyApp {
|
||||
self.music.fix_teta(self.current_delta);
|
||||
self.update_canvas_if_paused();
|
||||
}
|
||||
Message::ChangeDegree(i, s) => {
|
||||
let mut mut_s = s;
|
||||
if mut_s.len() == 0 {
|
||||
mut_s = "0".to_string();
|
||||
}
|
||||
match mut_s.parse::<f32>() {
|
||||
Ok(val) => {
|
||||
if val <= 360. {
|
||||
self.update(Message::ChangeTeta(i, (val).to_radians()))
|
||||
}
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
Message::ChangeNbPerSec(s) => {
|
||||
let mut_s = s.trim_end_matches(" rev/sec");
|
||||
if mut_s.len() != s.len() {
|
||||
match mut_s.parse::<f32>() {
|
||||
Ok(val) => {
|
||||
let val = (val * 10.).floor() / 10.;
|
||||
if val >= 1. {
|
||||
self.music.nb_sec_for_rev = val
|
||||
}
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::CancelColor(i) => {
|
||||
self.music.set_color_picker(self.current_delta, i, false);
|
||||
self.paused = false;
|
||||
}
|
||||
Message::SubmitColor(i) => {
|
||||
if !self.paused {
|
||||
self.update(Message::TogglePaused);
|
||||
}
|
||||
self.music.set_color_picker(self.current_delta, i, true);
|
||||
}
|
||||
Message::ChooseColor(i, color) => {
|
||||
self.music.set_color(self.current_delta, i, color);
|
||||
self.music.set_color_picker(self.current_delta, i, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self) -> iced::Element<Message> {
|
||||
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();
|
||||
//Create all polygon options
|
||||
@@ -219,30 +255,44 @@ impl MyApp {
|
||||
.iter()
|
||||
.map(|polygon| {
|
||||
let current_index = i;
|
||||
i += 1;
|
||||
column![
|
||||
let but = button(text("").size(20).center()).on_press(Message::SubmitColor(i));
|
||||
let c = column![
|
||||
row![
|
||||
text(&polygon.name).font(FONT),
|
||||
button("Remove").on_press(Message::Remove(i)),
|
||||
pick_list(
|
||||
["Black", "Blue", "Green", "Pink", "Yellow", "Cyan"]
|
||||
.map(|s| s.to_string())
|
||||
.to_vec(),
|
||||
Some(&polygon.color_name),
|
||||
move |s| { Message::ChangeColor(current_index, s) }
|
||||
text(&polygon.name).size(24),
|
||||
button(text("").size(20)).on_press(Message::Remove(i)),
|
||||
color_picker(
|
||||
polygon.show_color_picker,
|
||||
polygon.color,
|
||||
but,
|
||||
Message::CancelColor(i),
|
||||
move |color| Message::ChooseColor(i, color)
|
||||
),
|
||||
pick_list(entries.clone(), Some(&polygon.sound_name), move |s| {
|
||||
Message::ChangeSound(current_index, s)
|
||||
}),
|
||||
})
|
||||
.text_size(20),
|
||||
]
|
||||
.spacing(20),
|
||||
slider(0.0..=2.0 * PI, polygon.global_teta, move |f| {
|
||||
Message::ChangeTeta(current_index, f)
|
||||
})
|
||||
.step(PI / 84f32), // 84 | 4 for do PI / 4
|
||||
row![
|
||||
TextInput::new("90", &polygon.global_teta.to_degrees().floor().to_string())
|
||||
.on_input(move |new_value| Message::ChangeDegree(
|
||||
current_index,
|
||||
new_value
|
||||
))
|
||||
.width(Length::FillPortion(1)),
|
||||
slider(0.0..=2.0 * PI, polygon.global_teta, move |f| {
|
||||
Message::ChangeTeta(current_index, f)
|
||||
})
|
||||
.step(PI / 84f32) // 84 | 4 for do PI / 4
|
||||
.width(Length::FillPortion(9))
|
||||
.height(32),
|
||||
]
|
||||
.spacing(10),
|
||||
]
|
||||
.spacing(10)
|
||||
.into()
|
||||
.into();
|
||||
i += 1;
|
||||
c
|
||||
})
|
||||
.collect();
|
||||
let ngon_options: Vec<String> = (5..=42).map(|sides| format!("Ngon{sides}")).collect();
|
||||
@@ -292,12 +342,6 @@ impl MyApp {
|
||||
column![
|
||||
text("Polymusic").size(32.0),
|
||||
row(save_panel).spacing(20),
|
||||
row![
|
||||
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))
|
||||
@@ -305,10 +349,11 @@ impl MyApp {
|
||||
.width(Length::FillPortion(1))
|
||||
),
|
||||
column![
|
||||
text("Polygon options"),
|
||||
text("Polygon options").size(26),
|
||||
pick_list(all_options, Some("Choose polygon".to_string()), |s| {
|
||||
Message::AddPolygon(s)
|
||||
}),
|
||||
})
|
||||
.text_size(18),
|
||||
polygon_column,
|
||||
]
|
||||
.spacing(10)
|
||||
@@ -319,19 +364,35 @@ impl MyApp {
|
||||
.spacing(20),
|
||||
column![
|
||||
row![
|
||||
button(text(if self.paused { "" } else { "" }).size(28))
|
||||
.on_press(Message::TogglePaused),
|
||||
TextInput::new("MM:SS:CS", &self.str_time)
|
||||
.on_input(|new_value| Message::ChangeDeltaString(new_value))
|
||||
.size(28),
|
||||
text("/").size(30),
|
||||
TextInput::new("MM:SS:CS", &self.str_music_length)
|
||||
.on_input(|new_value| Message::LengthChange(new_value))
|
||||
.size(28),
|
||||
button(text("").size(28)).on_press(Message::AddPoint),
|
||||
button(text("").size(28)).on_press(Message::RemovePoint),
|
||||
button(text("").size(28)).on_press(Message::SlidePointLeft),
|
||||
button(text("").size(28)).on_press(Message::SlidePointRight),
|
||||
button(text(if self.paused { "" } else { "" }).size(28).center())
|
||||
.on_press(Message::TogglePaused)
|
||||
.width(Length::FillPortion(1)),
|
||||
row![
|
||||
TextInput::new("MM:SS:CS", &self.str_time)
|
||||
.on_input(|new_value| Message::ChangeDeltaString(new_value))
|
||||
.size(28),
|
||||
text("/").size(30),
|
||||
TextInput::new("MM:SS:CS", &self.str_music_length)
|
||||
.on_input(|new_value| Message::LengthChange(new_value))
|
||||
.size(28),
|
||||
]
|
||||
.width(Length::FillPortion(10)),
|
||||
TextInput::new("1.0", &format!("{:.1} rev/sec", &self.music.nb_sec_for_rev))
|
||||
.on_input(|new_value| Message::ChangeNbPerSec(new_value))
|
||||
.size(28)
|
||||
.width(Length::FillPortion(2)),
|
||||
button(text("").size(28).center())
|
||||
.on_press(Message::SlidePointLeft)
|
||||
.width(Length::FillPortion(1)),
|
||||
button(text("").size(28).center())
|
||||
.on_press(Message::AddPoint)
|
||||
.width(Length::FillPortion(1)),
|
||||
button(text("").size(28).center())
|
||||
.on_press(Message::SlidePointRight)
|
||||
.width(Length::FillPortion(1)),
|
||||
button(text("").size(28).center())
|
||||
.on_press(Message::RemovePoint)
|
||||
.width(Length::FillPortion(1)),
|
||||
]
|
||||
.spacing(20),
|
||||
column![
|
||||
@@ -356,10 +417,11 @@ impl MyApp {
|
||||
}
|
||||
|
||||
fn subscription(&self) -> iced::Subscription<Message> {
|
||||
iced::Subscription::batch([
|
||||
//window::events().map(|(_id, event)| Message::WindowEvent(event)),
|
||||
iced::time::every(std::time::Duration::from_millis(16)).map(|_| Message::Tick),
|
||||
])
|
||||
if self.paused {
|
||||
Subscription::none() // ➝ désactive toutes les subscriptions
|
||||
} else {
|
||||
iced::time::every(std::time::Duration::from_millis(16)).map(|_| Message::Tick)
|
||||
}
|
||||
}
|
||||
|
||||
fn update_canvas_if_paused(&mut self) {
|
||||
|
||||
Reference in New Issue
Block a user