diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..08cb09c --- /dev/null +++ b/src/color.rs @@ -0,0 +1,22 @@ +use iced::Color; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +pub mod color_serde { + use super::*; + + pub fn serialize(color: &Color, serializer: S) -> Result + where + S: Serializer, + { + let data = [color.r, color.g, color.b, color.a]; + data.serialize(serializer) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let data: [f32; 4] = Deserialize::deserialize(deserializer)?; + Ok(Color::from_rgba(data[0], data[1], data[2], data[3])) + } +} diff --git a/src/main.rs b/src/main.rs index 5dc2c50..4999847 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ use polygon_draw::Polygon; mod music; use music::Music; +mod color; + mod history; use history::Historic; diff --git a/src/main.rs.lock~ b/src/main.rs.lock~ new file mode 100644 index 0000000..058a880 --- /dev/null +++ b/src/main.rs.lock~ @@ -0,0 +1 @@ +dukantic@templeos \ No newline at end of file diff --git a/src/polygon_draw.rs b/src/polygon_draw.rs index 064c214..31c1c0c 100644 --- a/src/polygon_draw.rs +++ b/src/polygon_draw.rs @@ -1,12 +1,13 @@ use crate::utils::string_to_color; +use crate::color::color_serde; use std::f32::consts::PI; -use iced::Size; -use iced::Vector; use iced::mouse; use iced::widget::canvas; use iced::widget::canvas::{Frame, Stroke, Style}; +use iced::Size; +use iced::Vector; use iced::{Color, Point, Rectangle, Renderer, Theme}; use kira::sound::static_sound::StaticSoundData; use serde::{Deserialize, Serialize}; @@ -191,9 +192,8 @@ pub struct Polygon { pub sound: StaticSoundData, pub sound_name: String, pub name: String, - #[serde(skip)] + #[serde(with = "color_serde")] pub color: Color, - pub color_name: String, #[serde(skip)] pub show_color_picker: bool, } @@ -202,7 +202,6 @@ impl Polygon { pub fn update(&mut self) { let path = format!("./assets/{0}", &self.sound_name); self.sound = StaticSoundData::from_file(&path).expect("fail to load the sound"); - self.color = string_to_color(&self.color_name); } pub fn sound_to_play_btw(&self, before: f32, after: f32) -> Vec<&StaticSoundData> { let mut sound_to_play: Vec<&StaticSoundData> = vec![]; @@ -226,7 +225,6 @@ impl Polygon { points_teta: vec![], sound: sound, sound_name: "tick.ogg".to_string(), - color_name: "Black".to_string(), name: "".to_string(), color: Color::BLACK, show_color_picker: false,