From 48ace5dc01a68665ee0ae1883340ac14427519c7 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 23 May 2022 17:52:40 +0200 Subject: [PATCH] [Refactor] Move the get_moon_path function to the svg_clock module --- src/main.rs | 47 ++++------------------------------------------- src/svg_clock.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/main.rs b/src/main.rs index c6fd09f..3e59cd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,15 +11,16 @@ use sctk::shm::AutoMemPool; use sctk::window::{Event as WEvent, FallbackFrame}; use serde::Deserialize; use svg::node::element::path::Data as PathData; -use svg::node::element::{Circle, Group, Line, Path, Rectangle, Style, Text}; +use svg::node::element::{Circle, Group, Line, Rectangle, Style, Text}; use svg::node::Text as TextNode; use svg::Document; mod svg_clock; use svg_clock::{ - cache_hour_name_paths, get_range_path, hour_marker, seconds_to_degrees, svg_to_usvg, - HOUR_NAMES, HOUR_NAME_FONT_SIZE, IMAGE_WIDTH, OUTER_R, RING_WIDTH, UTC_HOUR_FONT_SIZE, + cache_hour_name_paths, get_moon_path, get_range_path, hour_marker, seconds_to_degrees, + svg_to_usvg, HOUR_NAMES, HOUR_NAME_FONT_SIZE, IMAGE_WIDTH, OUTER_R, RING_WIDTH, + UTC_HOUR_FONT_SIZE, }; sctk::default_environment!(SeasonalClock, desktop); @@ -36,46 +37,6 @@ struct CompleteConfig { seasonal_clock: Config, } -fn get_moon_path(radius: f32, moon_phase: f64) -> Path { - let handle_x_pos = radius as f64 * 1.34; - let handle_y_pos = radius * 0.88; - let min_x = IMAGE_WIDTH as f64 / 2.0 - handle_x_pos; - let max_x = min_x + 2.0 * handle_x_pos; - let top_y = IMAGE_WIDTH as f32 / 2.0 - handle_y_pos; - let bottom_y = IMAGE_WIDTH as f32 / 2.0 + handle_y_pos; - - let h1_x: f64; - let h2_x: f64; - - if moon_phase < 14.0 { - h1_x = min_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0); - h2_x = max_x; - } else { - h1_x = min_x; - h2_x = max_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0) - } - - let path_data = PathData::new() - .move_to((IMAGE_WIDTH as f32 / 2.0, IMAGE_WIDTH as f32 / 2.0 - radius)) - .cubic_curve_to(( - h1_x, - top_y, - h1_x, - bottom_y, - IMAGE_WIDTH as f32 / 2.0, - IMAGE_WIDTH as f32 / 2.0 + radius, - )) - .cubic_curve_to(( - h2_x, - bottom_y, - h2_x, - top_y, - IMAGE_WIDTH / 2, - IMAGE_WIDTH as f32 / 2.0 - radius, - )); - Path::new().set("class", "moon").set("d", path_data) -} - fn gen_svg(config: &Option, hour_name_path_cache: &[(PathData, PathData); 24]) -> Document { let local_timestamp = Local::now(); let utc_hour = local_timestamp.with_timezone(&Utc).time().hour(); diff --git a/src/svg_clock.rs b/src/svg_clock.rs index c8b027c..4c2a264 100644 --- a/src/svg_clock.rs +++ b/src/svg_clock.rs @@ -286,3 +286,43 @@ pub fn get_range_path(radius: f32, range_name: &str, start_time: i32, end_time: Path::new().set("class", range_name).set("d", path_data) } + +pub fn get_moon_path(radius: f32, moon_phase: f64) -> Path { + let handle_x_pos = radius as f64 * 1.34; + let handle_y_pos = radius * 0.88; + let min_x = IMAGE_WIDTH as f64 / 2.0 - handle_x_pos; + let max_x = min_x + 2.0 * handle_x_pos; + let top_y = IMAGE_WIDTH as f32 / 2.0 - handle_y_pos; + let bottom_y = IMAGE_WIDTH as f32 / 2.0 + handle_y_pos; + + let h1_x: f64; + let h2_x: f64; + + if moon_phase < 14.0 { + h1_x = min_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0); + h2_x = max_x; + } else { + h1_x = min_x; + h2_x = max_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0) + } + + let path_data = PathData::new() + .move_to((IMAGE_WIDTH as f32 / 2.0, IMAGE_WIDTH as f32 / 2.0 - radius)) + .cubic_curve_to(( + h1_x, + top_y, + h1_x, + bottom_y, + IMAGE_WIDTH as f32 / 2.0, + IMAGE_WIDTH as f32 / 2.0 + radius, + )) + .cubic_curve_to(( + h2_x, + bottom_y, + h2_x, + top_y, + IMAGE_WIDTH / 2, + IMAGE_WIDTH as f32 / 2.0 - radius, + )); + Path::new().set("class", "moon").set("d", path_data) +}