color save

This commit is contained in:
2025-08-02 18:10:22 +02:00
parent b596dc3410
commit e81e92a6bc
4 changed files with 29 additions and 6 deletions

22
src/color.rs Normal file
View File

@@ -0,0 +1,22 @@
use iced::Color;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub mod color_serde {
use super::*;
pub fn serialize<S>(color: &Color, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let data = [color.r, color.g, color.b, color.a];
data.serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Color, D::Error>
where
D: Deserializer<'de>,
{
let data: [f32; 4] = Deserialize::deserialize(deserializer)?;
Ok(Color::from_rgba(data[0], data[1], data[2], data[3]))
}
}

View File

@@ -5,6 +5,8 @@ use polygon_draw::Polygon;
mod music; mod music;
use music::Music; use music::Music;
mod color;
mod history; mod history;
use history::Historic; use history::Historic;

1
src/main.rs.lock~ Normal file
View File

@@ -0,0 +1 @@
dukantic@templeos

View File

@@ -1,12 +1,13 @@
use crate::utils::string_to_color; use crate::utils::string_to_color;
use crate::color::color_serde;
use std::f32::consts::PI; use std::f32::consts::PI;
use iced::Size;
use iced::Vector;
use iced::mouse; use iced::mouse;
use iced::widget::canvas; use iced::widget::canvas;
use iced::widget::canvas::{Frame, Stroke, Style}; use iced::widget::canvas::{Frame, Stroke, Style};
use iced::Size;
use iced::Vector;
use iced::{Color, Point, Rectangle, Renderer, Theme}; use iced::{Color, Point, Rectangle, Renderer, Theme};
use kira::sound::static_sound::StaticSoundData; use kira::sound::static_sound::StaticSoundData;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -191,9 +192,8 @@ pub struct Polygon {
pub sound: StaticSoundData, pub sound: StaticSoundData,
pub sound_name: String, pub sound_name: String,
pub name: String, pub name: String,
#[serde(skip)] #[serde(with = "color_serde")]
pub color: Color, pub color: Color,
pub color_name: String,
#[serde(skip)] #[serde(skip)]
pub show_color_picker: bool, pub show_color_picker: bool,
} }
@@ -202,7 +202,6 @@ impl Polygon {
pub fn update(&mut self) { pub fn update(&mut self) {
let path = format!("./assets/{0}", &self.sound_name); let path = format!("./assets/{0}", &self.sound_name);
self.sound = StaticSoundData::from_file(&path).expect("fail to load the sound"); 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> { pub fn sound_to_play_btw(&self, before: f32, after: f32) -> Vec<&StaticSoundData> {
let mut sound_to_play: Vec<&StaticSoundData> = vec![]; let mut sound_to_play: Vec<&StaticSoundData> = vec![];
@@ -226,7 +225,6 @@ impl Polygon {
points_teta: vec![], points_teta: vec![],
sound: sound, sound: sound,
sound_name: "tick.ogg".to_string(), sound_name: "tick.ogg".to_string(),
color_name: "Black".to_string(),
name: "".to_string(), name: "".to_string(),
color: Color::BLACK, color: Color::BLACK,
show_color_picker: false, show_color_picker: false,