Skip to content

Commit

Permalink
hotfix: add some bolstering to checking the valididy of our config toml
Browse files Browse the repository at this point in the history
  • Loading branch information
alphastrata committed Nov 10, 2023
1 parent 4260f26 commit 047c5cf
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 32 deletions.
64 changes: 33 additions & 31 deletions assets/Gallery/light-spirals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,40 @@ fn gradient(t: f32, a: vec3f, b: vec3f, c: vec3f, d: vec3f) -> vec3f {

- The scale and gradient I think are really nice and useful little numbers to have!


### fn scale(x: f32, a: f32, b: f32, c: f32, d: f32) -> f32:

Takes x from a range of a..b and returns it as a range from c..d
This little python script may be more understandable:
```py
# The same logic as our scale function above in the wgsl example
def scale(x, a, b, c, d):
return (x-a)/(b-a)*(d-c)+c

# Generate 10 values between 0 and 1
values = [i/10 for i in range(11)]

# Scale values of `x` from 0..1 to 0..100
scaled_values = [scale(x, 0, 1, 0, 100) for x in values]

# Print the scaled values
for i, val in enumerate(scaled_values):
print(f'Original: {values[i]}, Scaled: {val}')
```
which gives you:
```
Original: 0.0, Scaled: 0.0
Original: 0.1, Scaled: 10.0
Original: 0.2, Scaled: 20.0
Original: 0.3, Scaled: 30.0
Original: 0.4, Scaled: 40.0
Original: 0.5, Scaled: 50.0
Original: 0.6, Scaled: 60.0
Original: 0.7, Scaled: 70.0
Original: 0.8, Scaled: 80.0
Original: 0.9, Scaled: 90.0
Original: 1.0, Scaled: 100.0
```
it seems to work bidirectionally which is cool.
\`\`\`py
\# The same logic as our scale function above in the wgsl example
def scale(x, a, b, c, d):
return (x-a)/(b-a)\*(d-c)+c

````
# Generate 10 values between 0 and 1
values = [i/10 for i in range(11)]
# Scale values of `x` from 0..1 to 0..100
scaled_values = [scale(x, 0, 1, 0, 100) for x in values]
# Print the scaled values
for i, val in enumerate(scaled_values):
print(f'Original: {values[i]}, Scaled: {val}')
```
which gives you:
```
Original: 0.0, Scaled: 0.0
Original: 0.1, Scaled: 10.0
Original: 0.2, Scaled: 20.0
Original: 0.3, Scaled: 30.0
Original: 0.4, Scaled: 40.0
Original: 0.5, Scaled: 50.0
Original: 0.6, Scaled: 60.0
Original: 0.7, Scaled: 70.0
Original: 0.8, Scaled: 80.0
Original: 0.9, Scaled: 90.0
Original: 1.0, Scaled: 100.0
```
````

it seems to work bidirectionally which is cool.
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ fn main() {
Ok(config) => config,
Err(_) => {
let default_config = UserConfig::default();
let _ = default_config.save_to_toml(path);
default_config
}
};
Expand Down
40 changes: 40 additions & 0 deletions src/system/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,31 @@ use std::{

#[derive(Resource, Debug, Serialize, PartialEq, PartialOrd, Deserialize)]
pub struct UserConfig {
#[serde(default = "default_window_dims")]
window_dims: (f32, f32),
#[serde(default = "neg")]
decorations: bool,
#[serde(default = "neg")]
always_on_top: bool,
#[serde(default = "default_last_updated")]
last_updated: u64, //Toml doesn't supprot u128
}
// Provide a default function for window_dims
fn default_window_dims() -> (f32, f32) {
(800.0, 600.0) // Default window dimensions
}

fn neg() -> bool {
false
}

// Provide a default function for last_updated
fn default_last_updated() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}

impl UserConfig {
pub fn get_config_path() -> PathBuf {
Expand Down Expand Up @@ -64,6 +84,26 @@ impl UserConfig {
Ok(config)
}

pub fn config_is_valid(&self) -> bool {
// Check that the window dimensions are positive numbers
if self.window_dims.0 <= 0.0 || self.window_dims.1 <= 0.0 {
return false;
}

if self.last_updated >= 1 << 63 {
return false;
}

if self.decorations == true || self.decorations == false {
return false;
}

if self.always_on_top == true || self.always_on_top == false {
return false;
}

true
}
pub fn create_window_settings(&self) -> Window {
Window {
title: "shadplay".into(),
Expand Down

0 comments on commit 047c5cf

Please sign in to comment.