Skip to content

Commit

Permalink
Undo a lot of odd changes made to this branch, so that it's more in l…
Browse files Browse the repository at this point in the history
…ine with the main branch
  • Loading branch information
CaspianA1 committed May 30, 2024
1 parent 453910c commit 04c4a36
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 26 deletions.
2 changes: 1 addition & 1 deletion assets/app_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"o1": {"Windowed": [1200, 800, false, null]},
"o2": "FullscreenDesktop",
"screen_option": { "Windowed": [1024, 768, false, null] },
"screen_option": "Fullscreen",

"hide_cursor": true,
"use_linear_filtering": true,
Expand Down
File renamed without changes
170 changes: 170 additions & 0 deletions src/dashboard_defs/clock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
use crate::{
texture::TexturePool,

window_tree::{
Window,
ColorSDL,
GeneralLine,
WindowContents,
WindowUpdaterParams
},

utility_types::{
vec2f::Vec2f,
update_rate::UpdateRate,
dynamic_optional::DynamicOptional,
generic_result::{GenericResult, MaybeError}
},

dashboard_defs::shared_window_state::SharedWindowState
};

use chrono::{Local, Timelike};

// This is called raw because it's centered at (0, 0) and is unrotated.
type RawClockHand = GeneralLine<(f32, f32)>;

const NUM_CLOCK_HANDS: usize = 4;
const CLOCK_CENTER: (f32, f32) = (0.5, 0.5);

//////////

// These extents are defined assuming that the clock is pointing to 12:00
pub struct ClockHandConfig {
x_extent: f32,
minor_y_extent: f32,
major_y_extent: f32,
color: ColorSDL
}

impl ClockHandConfig {
pub const fn new(x_extent: f32, minor_y_extent: f32, major_y_extent: f32, color: ColorSDL) -> Self {
Self {x_extent, minor_y_extent, major_y_extent, color}
}

fn make_geometry(&self) -> RawClockHand {
let hand = [
// The minor part of the hand
(-self.x_extent, 0.0),
(0.0, self.minor_y_extent),
(self.x_extent, 0.0),

// The major part of the hand
(0.0, -self.major_y_extent),
(-self.x_extent, 0.0)
];

(self.color, hand.to_vec())
}
}

pub struct ClockHandConfigs {
pub milliseconds: ClockHandConfig,
pub seconds: ClockHandConfig,
pub minutes: ClockHandConfig,
pub hours: ClockHandConfig
}

pub struct ClockHands {
milliseconds: RawClockHand,
seconds: RawClockHand,
minutes: RawClockHand,
hours: RawClockHand
}

impl ClockHands {
pub fn new_with_window(
update_rate: UpdateRate,
top_left: Vec2f,
size: Vec2f,
hand_configs: ClockHandConfigs,
dial_texture_path: &str,
texture_pool: &mut TexturePool) -> GenericResult<(Self, Window)> {

fn updater_fn(params: WindowUpdaterParams) -> MaybeError {
let curr_time = Local::now();

let time_units: [(u32, u32); NUM_CLOCK_HANDS] = [
(curr_time.timestamp_subsec_millis(), 1000),
(curr_time.second(), 60),
(curr_time.minute(), 60),
(curr_time.hour() % 12, 12)
];

let inner_shared_state = params.shared_window_state.get::<SharedWindowState>();
let clock_hands = &inner_shared_state.clock_hands;

let clock_hands_as_list: [&RawClockHand; NUM_CLOCK_HANDS] = [
&clock_hands.milliseconds, &clock_hands.seconds, &clock_hands.minutes, &clock_hands.hours
];

//////////

let WindowContents::Many(all_contents) = params.window.get_contents_mut()
else {panic!("The clock's window contents was expected to be a list!")};

let WindowContents::Lines(rotated_hands) = &mut all_contents[1]
else {panic!("The second item in the clock's window contents was not a set of lines!")};

//////////

let mut prev_time_fract = 0.0;

for ((raw_hand, rotated_hand), time_unit) in
clock_hands_as_list.into_iter().zip(rotated_hands.iter_mut().rev()).zip(time_units) {

let time_fract = (time_unit.0 as f32 + prev_time_fract) / time_unit.1 as f32;
prev_time_fract = time_fract;

let angle = time_fract * std::f32::consts::TAU;
let (cos_angle, sin_angle) = (angle.cos(), angle.sin());

rotated_hand.1.iter_mut().zip(&raw_hand.1).for_each(|(dest, raw)| {
*dest = Vec2f::new(
(raw.0 * cos_angle - raw.1 * sin_angle) + CLOCK_CENTER.0,
(raw.0 * sin_angle + raw.1 * cos_angle) + CLOCK_CENTER.1
);
});
}

Ok(())
}

//////////

let texture_contents = WindowContents::make_texture_contents(dial_texture_path, texture_pool)?;

let clock_hand_configs_as_list: [&ClockHandConfig; NUM_CLOCK_HANDS] = [
&hand_configs.milliseconds, &hand_configs.seconds,
&hand_configs.minutes, &hand_configs.hours
];

let raw_clock_hands = clock_hand_configs_as_list.map(|config| config.make_geometry());

let line_contents = WindowContents::Lines(
raw_clock_hands.iter().rev().map(|(color, clock_hand)| {
(*color, vec![Vec2f::ZERO; clock_hand.len()])
}).collect());

let clock_window = Window::new(
Some((updater_fn, update_rate)),
DynamicOptional::NONE,
WindowContents::Many(vec![texture_contents, line_contents]),
None,
top_left,
size,
None
);

Ok((
ClockHands {
milliseconds: raw_clock_hands[0].clone(),
seconds: raw_clock_hands[1].clone(),
minutes: raw_clock_hands[2].clone(),
hours: raw_clock_hands[3].clone()
},

clock_window
))
}
}
59 changes: 47 additions & 12 deletions src/dashboard_defs/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
vec2f::Vec2f,
dynamic_optional::DynamicOptional,
generic_result::{GenericResult, MaybeError},
update_rate::{UpdateRateCreator}
update_rate::{UpdateRate, UpdateRateCreator}
},

window_tree::{
Expand All @@ -25,9 +25,11 @@ use crate::{
dashboard_defs::{
error::make_error_window,
credit::make_credit_window,
weather::make_weather_window,
shared_window_state::SharedWindowState,
twilio::{make_twilio_window, TwilioState},
surprise::{make_surprise_window, SurpriseCreationInfo},
clock::{ClockHandConfig, ClockHandConfigs, ClockHands},
spinitron::{make_spinitron_windows, SpinitronModelWindowInfo, SpinitronModelWindowsInfo}
}
};
Expand All @@ -42,6 +44,7 @@ use crate::{
#[derive(serde::Deserialize)]
struct ApiKeys {
spinitron: String,
openweathermap: String,
twilio_account_sid: String,
twilio_auth_token: String
}
Expand Down Expand Up @@ -73,15 +76,14 @@ pub fn make_dashboard(
let main_windows_gap_size = 0.01;

let theme_color_1 = ColorSDL::RGB(255, 133, 133);
let theme_color_2 = ColorSDL::RGB(255, 255, 255);
let shared_update_rate = update_rate_creator.new_instance(15.0);
let api_keys: ApiKeys = json_utils::load_from_file("assets/api_keys.json")?;

////////// Defining the Spinitron window extents

// Note: `tl` = top left
let spin_tl = Vec2f::new_scalar(main_windows_gap_size);
let spin_size = Vec2f::new(0.55, 0.81); // Increase the second parameter to increase the height
let spin_size = Vec2f::new(0.55, 0.81);
let spin_text_height = 0.03;
let spin_tr = spin_tl.x() + spin_size.x();

Expand All @@ -92,7 +94,7 @@ pub fn make_dashboard(
let persona_text_height = 0.0;

let show_tl = Vec2f::new(persona_tl.x() + persona_size.x() + main_windows_gap_size, spin_tl.y());
let show_size = Vec2f::new(0.2, 0.3);
let show_size = persona_size;

let text_scalar = Vec2f::new_scalar(0.55);
let show_text_tl = Vec2f::translate(&(spin_tl + text_scalar), 0.03, -0.24);
Expand Down Expand Up @@ -186,7 +188,10 @@ pub fn make_dashboard(

let twilio_window = make_twilio_window(
&twilio_state,

// This is how often the history windows check for new messages (this is low so that it'll be fast in the beginning)
update_rate_creator.new_instance(0.25),

Vec2f::new(0.58, 0.40), // Position
Vec2f::new(0.4, 0.55),
0.025,
Expand Down Expand Up @@ -215,17 +220,46 @@ pub fn make_dashboard(
"By: Caspian Ahlberg"
);

////////// Making some static texture windows
////////// Making a clock window

// Texture path, top left, size (TODO: make animated textures possible)
let main_static_texture_info = [
];
let clock_size_x = 0.3;
let clock_tl = Vec2f::new(1.0 - clock_size_x, 0.0);
let clock_size = Vec2f::new(clock_size_x, 1.0);

let foreground_static_texture_info = [
];
let (clock_hands, clock_window) = ClockHands::new_with_window(
UpdateRate::ONCE_PER_FRAME,
clock_tl,
clock_size,

let background_static_texture_info = [
];
ClockHandConfigs {
milliseconds: ClockHandConfig::new(0.01, 0.2, 0.5, ColorSDL::RGBA(255, 0, 0, 100)), // Milliseconds
seconds: ClockHandConfig::new(0.01, 0.02, 0.48, ColorSDL::WHITE), // Seconds
minutes: ClockHandConfig::new(0.01, 0.02, 0.35, ColorSDL::YELLOW), // Minutes
hours: ClockHandConfig::new(0.01, 0.02, 0.2, ColorSDL::BLACK) // Hours
},

"assets/watch_dial.png",
texture_pool
)?;

////////// Making a weather window

let weather_window = make_weather_window(
Vec2f::ZERO,
Vec2f::new(0.4, 0.3),
update_rate_creator,
&api_keys.openweathermap,
"Brunswick",
"ME",
"US"
);

////////// Making some static texture windows

// Texture path, top left, size (TODO: make animated textures possible)
let main_static_texture_info = [];
let foreground_static_texture_info = [];
let background_static_texture_info = [];

let add_static_texture_set =
|set: &mut Vec<Window>, all_info: &[(&'static str, Vec2f, Vec2f, bool)], texture_pool: &mut TexturePool| {
Expand Down Expand Up @@ -346,6 +380,7 @@ pub fn make_dashboard(

let boxed_shared_state = DynamicOptional::new(
SharedWindowState {
clock_hands,
spinitron_state,
twilio_state,
font_info: &FONT_INFO,
Expand Down
2 changes: 2 additions & 0 deletions src/dashboard_defs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod clock;
mod error;
mod credit;
mod twilio;
mod weather;
mod surprise;
mod spinitron;
mod shared_window_state;
Expand Down
3 changes: 2 additions & 1 deletion src/dashboard_defs/shared_window_state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{
spinitron::state::SpinitronState,
texture::{FontInfo, TextureCreationInfo},
dashboard_defs::{twilio::TwilioState}
dashboard_defs::{twilio::TwilioState, clock::ClockHands}
};

pub struct SharedWindowState<'a> {
pub clock_hands: ClockHands,
pub spinitron_state: SpinitronState,
pub twilio_state: TwilioState<'a>,

Expand Down
Loading

0 comments on commit 04c4a36

Please sign in to comment.