From 5fa8b883737aa149538e41a68a5ffb222ed2fe95 Mon Sep 17 00:00:00 2001 From: Dukantic Date: Wed, 9 Jul 2025 20:50:39 +0200 Subject: [PATCH] add small canvas on time line --- src/music.rs | 55 +++++++++++++++++++++++++++++++++----- src/polygon_draw.rs | 65 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 110 insertions(+), 10 deletions(-) diff --git a/src/music.rs b/src/music.rs index 82c72f4..49443b4 100644 --- a/src/music.rs +++ b/src/music.rs @@ -1,14 +1,17 @@ use crate::message::Message; use crate::utils::string_to_polygon; use crate::{polygon_draw::*, utils::string_to_color}; +use iced::widget::shader::wgpu::util::RenderEncoder; +use kira::Frame; use serde::{Deserialize, Serialize}; use kira::{AudioManager, sound::static_sound::StaticSoundData}; +use iced::Vector; use iced::event::Status; use iced::mouse::Cursor; -use iced::widget::canvas::Event; -use iced::{mouse, padding}; +use iced::widget::canvas::{Event, Geometry}; +use iced::{Size, mouse, padding}; use iced::widget::canvas; use iced::widget::canvas::Stroke; @@ -166,13 +169,23 @@ impl canvas::Program for Music { bounds: Rectangle, _cursor: mouse::Cursor, ) -> Vec { + let mut geo_small_frame: Vec = vec![]; + let mut geo_cursor: Vec = vec![]; let mut frame = canvas::Frame::new(renderer, bounds.size()); let mut toggle_color = true; let padding = 8.; let w = bounds.width - (padding * 2.); - for (delta, _) in &self.poly_frame { + for (delta, polyframe) in &self.poly_frame { let x = delta / self.length * w + 8.; - frame.fill_rectangle( + let mut back_frame = canvas::Frame::new( + renderer, + Size { + width: bounds.width, + height: bounds.height, + }, + ); + + back_frame.fill_rectangle( iced::Point { x: x, y: 0.0 }, frame.size(), if toggle_color { @@ -181,10 +194,33 @@ impl canvas::Program for Music { Color::from_rgb8(69, 104, 130) }, ); + geo_small_frame.push(back_frame.into_geometry()); toggle_color = !toggle_color; + let mut small_frame = canvas::Frame::new( + renderer, + Size { + width: bounds.width, + height: bounds.height, + }, + ); + + small_frame.translate(Vector { + x: x + (bounds.height / 10.), + y: (bounds.height / 10.), + }); + polyframe.draw_in_frame( + &mut small_frame, + Size { + width: (8. * bounds.height) / 10., + height: (8. * bounds.height) / 10., + }, + ); + + geo_small_frame.push(small_frame.into_geometry()); } let x = self.current_delta / self.length * w + 8.; - frame.stroke_rectangle( + let mut frame_cursor = canvas::Frame::new(renderer, bounds.size()); + frame_cursor.stroke_rectangle( iced::Point::new(x, 0.), iced::Size { width: 0., @@ -196,7 +232,7 @@ impl canvas::Program for Music { ..Stroke::default() }, ); - frame.stroke_rectangle( + frame_cursor.stroke_rectangle( iced::Point { x: 0.0, y: 0.0 }, frame.size(), Stroke { @@ -205,9 +241,14 @@ impl canvas::Program for Music { ..Stroke::default() }, ); + geo_cursor.push(frame_cursor.into_geometry()); + //vec_geo.push(frame.into_geometry()); // Then, we produce the geometry - vec![frame.into_geometry()] + let mut out = vec![frame.into_geometry()]; + out.append(&mut geo_small_frame); + out.append(&mut geo_cursor); + out } fn update( &self, diff --git a/src/polygon_draw.rs b/src/polygon_draw.rs index 2892fb9..4926b40 100644 --- a/src/polygon_draw.rs +++ b/src/polygon_draw.rs @@ -2,12 +2,12 @@ use crate::utils::string_to_color; use std::f32::consts::PI; +use iced::Size; use iced::Vector; 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::widget::canvas::{Frame, Stroke, Style}; +use iced::{Color, Point, Rectangle, Renderer, Theme}; use kira::sound::static_sound::StaticSoundData; use serde::{Deserialize, Serialize}; @@ -49,6 +49,65 @@ impl PolygonFrame { polygons: vec![], } } + pub fn draw_in_frame(&self, frame: &mut Frame, size: Size) { + let radius = size.height / 2.0 - (size.height / 10.); + let mut vec = Vector::new(0.0, -radius); + let c = Point { + x: size.width / 2., + y: size.height / 2., + }; + + // Draw Circle + let circle = canvas::Path::circle(c, radius); + frame.stroke( + &circle, + Stroke { + width: 6.0, + style: Style::Solid(Color::from_rgba8(210, 193, 182, 0.25)), + ..Stroke::default() + }, + ); + + // Draw all polygons by there points + for poly in &self.polygons { + let l = poly.points_teta.len(); + let mut line: canvas::Path; + let mut t1: f32; + let mut t2: f32; + let mut color: Color; + for i in 0..l { + t1 = poly.points_teta[i] + poly.global_teta; + t2 = poly.points_teta[(i + 1) % l] + poly.global_teta; + line = canvas::Path::line(c + vec.rotate(t1), c + vec.rotate(t2)); + color = poly.color.clone(); + color.a = 0.25; + frame.stroke( + &line, + Stroke { + width: 4.0, + style: Style::Solid(poly.color.clone()), + ..Stroke::default() + }, + ); + } + } + /* + // Draw the red dot on the current position + let dot = canvas::Path::circle(frame.center() + vec.rotate(self.teta), 10.0); + frame.fill(&dot, Color::from_rgb(1.0, 0.0, 0.0)); + */ + // Draw the frame + /* + frame.stroke_rectangle( + iced::Point { x: 0.0, y: 0.0 }, + size, + Stroke { + width: 8.0, + style: Style::Solid(Color::from_rgb8(207, 74, 28)), + ..Stroke::default() + }, + );*/ + } } impl canvas::Program for PolygonFrame {