From 529f32a792f7a9cd86b1b82c2caba662219f74a8 Mon Sep 17 00:00:00 2001 From: Dukantic Date: Tue, 3 Jun 2025 15:58:48 +0200 Subject: [PATCH] change color of polygon --- src/main.rs | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7782030..69b8760 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ mod polygon_draw; use iced::{ - Element, Task, Theme, + Color, Element, Task, Theme, time::{self, Duration}, widget::{Column, button, canvas, column, pick_list, row, slider, text}, }; use polygon_draw::{Polygon, PolygonFrame}; use std::f32::consts::PI; -use std::io::BufReader; use std::time::Instant; use kira::{ @@ -30,6 +29,7 @@ enum Message { AddPolygon(String), ChangeTeta(usize, f32), Remove(usize), + ChangeColor(usize, String), } struct MyApp { @@ -73,17 +73,17 @@ impl MyApp { Message::AddPolygon(s) => { let mut poly: Polygon; match s.as_str() { - "segment" => { + "Segment" => { poly = Polygon::segment(0.0, self.default_sound.clone()); - poly.name = "segment".to_string() + poly.name = "Segment".to_string() } - "triangle" => { + "Triangle" => { poly = Polygon::triangle(0.0, self.default_sound.clone()); - poly.name = "triangle".to_string() + poly.name = "Triangle".to_string() } - "square" => { + "Square" => { poly = Polygon::square(0.0, self.default_sound.clone()); - poly.name = "square".to_string() + poly.name = "Square".to_string() } _ => poly = Polygon::n_gon(0.0, 0, self.default_sound.clone()), } @@ -111,6 +111,18 @@ impl MyApp { self.poly_frame.polygons.remove(i - 1); } Message::ChangeTeta(i, teta) => self.poly_frame.polygons[i].global_teta = teta, + Message::ChangeColor(i, s) => { + let c: Color; + match s.as_str() { + "Green" => c = Color::from_rgb(0.0, 1.0, 0.0), + "Blue" => c = Color::from_rgb(0.0, 0.0, 1.0), + "Cyan" => c = Color::from_rgb(0.0, 1.0, 1.0), + "Yellow" => c = Color::from_rgb(1.0, 1.0, 0.0), + "Pink" => c = Color::from_rgb(1.0, 0.0, 1.0), + _ => c = Color::BLACK, + } + self.poly_frame.polygons[i].color = c; + } } } @@ -133,7 +145,14 @@ impl MyApp { move |f| { Message::ChangeTeta(current_index, f) } ) .step(2.0 * PI / 42f32), - button("Remove").on_press(Message::Remove(i)) + button("Remove").on_press(Message::Remove(i)), + pick_list( + ["Black", "Blue", "Green", "Pink", "Yellow", "Cyan"] + .map(|s| s.to_string()) + .to_vec(), + Some("Color".to_string()), + move |s| { Message::ChangeColor(current_index, s) } + ), ] .into() }) @@ -152,7 +171,7 @@ impl MyApp { ], text("Polygon options"), pick_list( - ["segment", "triangle", "square"] + ["Segment", "Triangle", "Square"] .map(|s| s.to_string()) .to_vec(), Some("Chose polygon".to_string()),