[Refactor] Move Moon phase calculation to a new clock module

This commit is contained in:
Gergely Polonkai 2022-05-24 09:26:05 +02:00
parent 3d4224b560
commit 1d485c824b
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
3 changed files with 19 additions and 9 deletions

9
src/clock.rs Normal file
View File

@ -0,0 +1,9 @@
use chrono::prelude::Local;
pub fn get_moon_phase() -> f32 {
let local_timestamp = Local::now();
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
let moon_illumination = suncalc::moon_illumination(unixtime);
moon_illumination.phase as f32 * 28.0
}

View File

@ -6,6 +6,7 @@ use sctk::shm::AutoMemPool;
use sctk::window::{Event as WEvent, FallbackFrame};
use svg::node::element::path::Data as PathData;
mod clock;
mod config;
mod svg_clock;

View File

@ -17,6 +17,7 @@ use svg::{
};
use usvg::Tree;
use crate::clock::get_moon_phase;
use crate::config::Config;
const HOUR_NAMES: [&str; 24] = [
@ -296,16 +297,16 @@ fn get_range_path(radius: f32, range_name: &str, start_time: i32, end_time: i32)
Path::new().set("class", range_name).set("d", path_data)
}
fn get_moon_path(radius: f32, moon_phase: f64) -> Path {
let handle_x_pos = radius as f64 * 1.34;
fn get_moon_path(radius: f32, moon_phase: f32) -> Path {
let handle_x_pos = radius * 1.34;
let handle_y_pos = radius * 0.88;
let min_x = IMAGE_WIDTH as f64 / 2.0 - handle_x_pos;
let min_x = IMAGE_WIDTH as f32 / 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;
let h1_x: f32;
let h2_x: f32;
if moon_phase < 14.0 {
h1_x = min_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0);
@ -345,16 +346,15 @@ pub fn gen_svg(
let local_hour = local_timestamp.time().hour();
let local_minute = local_timestamp.time().minute();
let local_second = local_timestamp.time().second();
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
let utc_offset = local_timestamp.offset().local_minus_utc();
let local_time = local_timestamp.time().num_seconds_from_midnight() as i32;
let utc_rotation = seconds_to_degrees(utc_offset);
let moon_radius = IMAGE_WIDTH as f32 * 0.071428571;
// Calculate the Moon phase
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
let moon_illumination = suncalc::moon_illumination(unixtime);
let moon_radius = IMAGE_WIDTH as f32 * 0.071428571;
let moon_phase = moon_illumination.phase * 28.0;
let moon_phase = get_moon_phase();
let local_hour_font_size = IMAGE_WIDTH as f32 * 0.02357;
let sun_radius = IMAGE_WIDTH as f32 * 0.0142871;