[Refactor] Move the hour_marker function to the svg_clock module
This commit is contained in:
parent
e61c247915
commit
b7aa26be89
96
src/main.rs
96
src/main.rs
@ -1,6 +1,5 @@
|
||||
extern crate smithay_client_toolkit as sctk;
|
||||
|
||||
use std::fmt;
|
||||
use std::fs;
|
||||
|
||||
use calloop::{timer::Timer, EventLoop};
|
||||
@ -19,34 +18,12 @@ use svg::Document;
|
||||
mod svg_clock;
|
||||
|
||||
use svg_clock::{
|
||||
cache_hour_name_paths, svg_to_usvg, HOUR_NAMES, HOUR_NAME_FONT_SIZE, IMAGE_WIDTH, OUTER_R,
|
||||
RING_WIDTH, UTC_HOUR_FONT_SIZE,
|
||||
cache_hour_name_paths, hour_marker, svg_to_usvg, HOUR_NAMES, HOUR_NAME_FONT_SIZE, IMAGE_WIDTH,
|
||||
OUTER_R, RING_WIDTH, UTC_HOUR_FONT_SIZE,
|
||||
};
|
||||
|
||||
sctk::default_environment!(SeasonalClock, desktop);
|
||||
|
||||
enum Season {
|
||||
Spring,
|
||||
Summer,
|
||||
Autumn,
|
||||
Winter,
|
||||
}
|
||||
|
||||
impl fmt::Display for Season {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Season::Spring => "spring",
|
||||
Season::Summer => "summer",
|
||||
Season::Autumn => "autumn",
|
||||
Season::Winter => "winter",
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Copy, Clone)]
|
||||
struct Config {
|
||||
latitude: f64,
|
||||
@ -68,75 +45,6 @@ fn time_to_degrees(timestamp: i32, // should be time/timestamp
|
||||
seconds_to_degrees(timestamp)
|
||||
}
|
||||
|
||||
fn hour_marker(
|
||||
hour: i32,
|
||||
hour_name_path_data: &(PathData, PathData),
|
||||
is_current_hour: bool,
|
||||
) -> Group {
|
||||
let season = match hour {
|
||||
0..=5 => Season::Winter,
|
||||
6..=11 => Season::Spring,
|
||||
12..=17 => Season::Summer,
|
||||
18..=23 => Season::Autumn,
|
||||
_ => panic!("Hour out of range"),
|
||||
};
|
||||
let rotation = hour * 15;
|
||||
|
||||
let delta_x = OUTER_R * (15f32.to_radians() / 2.0).sin();
|
||||
let delta_y = OUTER_R * (1.0 - (15f32.to_radians() / 2.0).cos());
|
||||
|
||||
let s_delta_x = 0.0 - RING_WIDTH * (15f32.to_radians() / 2.0).sin();
|
||||
let s_delta_y = RING_WIDTH * (15f32.to_radians() / 2.0).cos();
|
||||
|
||||
let i_delta_x = -2.0 * (OUTER_R - RING_WIDTH) * (15f32.to_radians() / 2.0).sin();
|
||||
|
||||
let x1 = IMAGE_WIDTH as f32 / 2.0 - delta_x;
|
||||
let y1 = (IMAGE_WIDTH as f32 / 2.0 - OUTER_R) + delta_y;
|
||||
|
||||
let path_data = PathData::new()
|
||||
.move_to((x1, y1))
|
||||
.elliptical_arc_by((OUTER_R, OUTER_R, 15, 0, 1, 2.0 * delta_x, 0))
|
||||
.line_by((s_delta_x, s_delta_y))
|
||||
.elliptical_arc_by((
|
||||
OUTER_R - RING_WIDTH,
|
||||
OUTER_R - RING_WIDTH,
|
||||
15,
|
||||
0,
|
||||
0,
|
||||
i_delta_x,
|
||||
0,
|
||||
))
|
||||
.close();
|
||||
let path = Path::new().set("class", "hour-outline").set("d", path_data);
|
||||
let hour_name_path = Path::new()
|
||||
.set("class", "hour-name")
|
||||
.set("d", hour_name_path_data.0.clone());
|
||||
let utc_hour_path = Path::new()
|
||||
.set("class", "utc")
|
||||
.set("d", hour_name_path_data.1.clone());
|
||||
|
||||
Group::new()
|
||||
.set(
|
||||
"class",
|
||||
format!(
|
||||
"hour {season}{}",
|
||||
if is_current_hour { " active" } else { "" }
|
||||
),
|
||||
)
|
||||
.set(
|
||||
"transform",
|
||||
format!(
|
||||
"rotate({}, {}, {})",
|
||||
rotation as f32 - 172.5,
|
||||
IMAGE_WIDTH / 2,
|
||||
IMAGE_WIDTH / 2
|
||||
),
|
||||
)
|
||||
.add(path)
|
||||
.add(hour_name_path)
|
||||
.add(utc_hour_path)
|
||||
}
|
||||
|
||||
fn get_range_path(radius: f32, range_name: &str, start_time: i32, end_time: i32) -> Path {
|
||||
let start_deg = time_to_degrees(start_time);
|
||||
let end_deg = time_to_degrees(end_time);
|
||||
|
@ -1,7 +1,9 @@
|
||||
use std::fmt;
|
||||
|
||||
use rctree::Node;
|
||||
use svg::{
|
||||
node::{
|
||||
element::{path::Data as PathData, Definitions, Path, Text, TextPath},
|
||||
element::{path::Data as PathData, Definitions, Group, Path, Text, TextPath},
|
||||
Text as TextNode,
|
||||
},
|
||||
Document,
|
||||
@ -19,6 +21,28 @@ pub const OUTER_R: f32 = (IMAGE_WIDTH as f32) / 2.0 - 3.0 * HOUR_NAME_FONT_SIZE;
|
||||
pub const RING_WIDTH: f32 = HOUR_NAME_FONT_SIZE * 3.0;
|
||||
pub const UTC_HOUR_FONT_SIZE: f32 = IMAGE_WIDTH as f32 * 0.021462;
|
||||
|
||||
enum Season {
|
||||
Spring,
|
||||
Summer,
|
||||
Autumn,
|
||||
Winter,
|
||||
}
|
||||
|
||||
impl fmt::Display for Season {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Season::Spring => "spring",
|
||||
Season::Summer => "summer",
|
||||
Season::Autumn => "autumn",
|
||||
Season::Winter => "winter",
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn svg_to_usvg(document: Document) -> Tree {
|
||||
let doc_str = document.to_string();
|
||||
|
||||
@ -159,3 +183,72 @@ pub fn cache_hour_name_paths() -> [(PathData, PathData); 24] {
|
||||
cache_hour_name_path(23),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn hour_marker(
|
||||
hour: i32,
|
||||
hour_name_path_data: &(PathData, PathData),
|
||||
is_current_hour: bool,
|
||||
) -> Group {
|
||||
let season = match hour {
|
||||
0..=5 => Season::Winter,
|
||||
6..=11 => Season::Spring,
|
||||
12..=17 => Season::Summer,
|
||||
18..=23 => Season::Autumn,
|
||||
_ => panic!("Hour out of range"),
|
||||
};
|
||||
let rotation = hour * 15;
|
||||
|
||||
let delta_x = OUTER_R * (15f32.to_radians() / 2.0).sin();
|
||||
let delta_y = OUTER_R * (1.0 - (15f32.to_radians() / 2.0).cos());
|
||||
|
||||
let s_delta_x = 0.0 - RING_WIDTH * (15f32.to_radians() / 2.0).sin();
|
||||
let s_delta_y = RING_WIDTH * (15f32.to_radians() / 2.0).cos();
|
||||
|
||||
let i_delta_x = -2.0 * (OUTER_R - RING_WIDTH) * (15f32.to_radians() / 2.0).sin();
|
||||
|
||||
let x1 = IMAGE_WIDTH as f32 / 2.0 - delta_x;
|
||||
let y1 = (IMAGE_WIDTH as f32 / 2.0 - OUTER_R) + delta_y;
|
||||
|
||||
let path_data = PathData::new()
|
||||
.move_to((x1, y1))
|
||||
.elliptical_arc_by((OUTER_R, OUTER_R, 15, 0, 1, 2.0 * delta_x, 0))
|
||||
.line_by((s_delta_x, s_delta_y))
|
||||
.elliptical_arc_by((
|
||||
OUTER_R - RING_WIDTH,
|
||||
OUTER_R - RING_WIDTH,
|
||||
15,
|
||||
0,
|
||||
0,
|
||||
i_delta_x,
|
||||
0,
|
||||
))
|
||||
.close();
|
||||
let path = Path::new().set("class", "hour-outline").set("d", path_data);
|
||||
let hour_name_path = Path::new()
|
||||
.set("class", "hour-name")
|
||||
.set("d", hour_name_path_data.0.clone());
|
||||
let utc_hour_path = Path::new()
|
||||
.set("class", "utc")
|
||||
.set("d", hour_name_path_data.1.clone());
|
||||
|
||||
Group::new()
|
||||
.set(
|
||||
"class",
|
||||
format!(
|
||||
"hour {season}{}",
|
||||
if is_current_hour { " active" } else { "" }
|
||||
),
|
||||
)
|
||||
.set(
|
||||
"transform",
|
||||
format!(
|
||||
"rotate({}, {}, {})",
|
||||
rotation as f32 - 172.5,
|
||||
IMAGE_WIDTH / 2,
|
||||
IMAGE_WIDTH / 2
|
||||
),
|
||||
)
|
||||
.add(path)
|
||||
.add(hour_name_path)
|
||||
.add(utc_hour_path)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user