[Refactor] Move the get_moon_path function to the svg_clock module

This commit is contained in:
Gergely Polonkai 2022-05-23 17:52:40 +02:00
parent af637bcd10
commit 48ace5dc01
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
2 changed files with 44 additions and 43 deletions

View File

@ -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<Config>, hour_name_path_cache: &[(PathData, PathData); 24]) -> Document {
let local_timestamp = Local::now();
let utc_hour = local_timestamp.with_timezone(&Utc).time().hour();

View File

@ -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)
}