vizualizer of different polygon frame

This commit is contained in:
2025-07-08 21:55:05 +02:00
parent 47c41be544
commit 26a96fd933
2 changed files with 141 additions and 37 deletions

View File

@@ -1,19 +1,28 @@
use crate::utils::string_to_polygon;
use crate::{polygon_draw::*, utils::string_to_color};
use iced::Length::Fill;
use serde::{Deserialize, Serialize};
use kira::{AudioManager, sound::static_sound::StaticSoundData};
use iced::mouse;
use iced::widget::canvas;
use iced::widget::canvas::Stroke;
use iced::widget::canvas::Style;
use iced::{Color, Rectangle, Renderer, Theme};
use iced::{Vector, color};
use std::f32::consts::PI;
use std::time::Duration;
#[derive(Serialize, Deserialize)]
pub struct Music {
pub poly_frame: Vec<(f32, PolygonFrame)>,
pub nb_sec_for_rev: f32,
pub file_name: String,
pub length: f32,
#[serde(skip)]
teta: f32,
#[serde(skip)]
pub current_delta: f32,
}
impl Music {
@@ -21,7 +30,7 @@ impl Music {
if let Some(i) = self
.poly_frame
.windows(2)
.position(|w| w[0].0 < delta && delta < w[1].0)
.position(|w| w[0].0 <= delta && delta < w[1].0)
{
&mut self.poly_frame[i].1
} else {
@@ -33,7 +42,7 @@ impl Music {
if let Some(i) = self
.poly_frame
.windows(2)
.position(|w| w[0].0 < delta && delta < w[1].0)
.position(|w| w[0].0 <= delta && delta < w[1].0)
{
&self.poly_frame[i].1
} else {
@@ -48,9 +57,10 @@ impl Music {
(10.0, PolygonFrame::default()),
],
nb_sec_for_rev: 1.0,
file_name: "Polymusic.json".to_string(),
file_name: "Default Name".to_string(),
length: 60.0,
teta: 0.,
current_delta: 0.,
}
}
@@ -116,3 +126,60 @@ impl Music {
self.find_poly_frame(delta).polygons.remove(i);
}
}
impl<Message> canvas::Program<Message> for Music {
// No internal state
type State = ();
fn draw(
&self,
_state: &(),
renderer: &Renderer,
_theme: &Theme,
bounds: Rectangle,
_cursor: mouse::Cursor,
) -> Vec<canvas::Geometry> {
let mut frame = canvas::Frame::new(renderer, bounds.size());
let radius = frame.size().width.min(frame.size().height) / 2.0 - 32.0;
let mut toggle_color = true;
let padding = 8.;
let w = bounds.width - (padding * 2.);
for (delta, _) in &self.poly_frame {
let x = delta / self.length * w + padding;
frame.fill_rectangle(
iced::Point { x: x, y: 0.0 },
frame.size(),
if toggle_color {
Color::from_rgb(0.3, 0.3, 0.3)
} else {
Color::from_rgb(0.1, 0.1, 0.1)
},
);
toggle_color = !toggle_color;
}
frame.stroke_rectangle(
iced::Point { x: 0.0, y: 0.0 },
frame.size(),
Stroke {
width: 16.0,
..Stroke::default()
},
);
let x = self.current_delta / self.length * w + padding;
frame.stroke_rectangle(
iced::Point::new(x, 0.),
iced::Size {
width: 0.,
height: bounds.height,
},
Stroke {
width: 4.0,
style: Style::Solid(Color::from_rgb(1.0, 0.0, 0.0)),
..Stroke::default()
},
);
// Then, we produce the geometry
vec![frame.into_geometry()]
}
}