Skip to content

Commit

Permalink
feat: added models to gui
Browse files Browse the repository at this point in the history
  • Loading branch information
markisha64 committed Sep 1, 2024
1 parent d16b2a3 commit 2509310
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod checkbox;
mod encounters;
mod file_upload;
mod maps;
mod models;
mod number_field;
mod number_field_float;
mod parties;
Expand Down Expand Up @@ -176,6 +177,7 @@ fn app() -> Element {
shops::shops {},
encounters::encounters {},
maps::maps {},
models::models {}
},
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/gui/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use dioxus::prelude::*;

use crate::gui::checkbox;
use crate::json::Preset;

#[component]
pub fn models() -> Element {
let mut state = use_context::<Signal<Preset>>();
let read_state = state.read();

let enabled = read_state.randomizer.models.enabled;

let model_hue = read_state.randomizer.models.hue_enabled;

rsx! {
div {
class: "segment",
div {
class: "left",
checkbox::checkbox {
label: "Models",
id: "models.enabled",
checked: enabled,
tooltip: "Models",
onchange: move |x: bool| {
state.write().randomizer.models.enabled = x;
}
}
},
div {
class: "left",
div {
class: "left",
checkbox::checkbox {
label: "Model hue",
id: "models.color",
checked: model_hue,
disabled: !enabled,
tooltip: "Randomize model hue",
onchange: move |x: bool| {
state.write().randomizer.models.hue_enabled = x;
}
},
},
},
}
}
}
4 changes: 3 additions & 1 deletion src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ pub struct Randomizer {
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Models {
#[serde(default = "default_bool_true")]
#[serde(default = "default_bool_false")]
pub enabled: bool,
#[serde(default = "default_bool_true")]
pub hue_enabled: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
30 changes: 11 additions & 19 deletions src/rand/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tim::Tim;

use crate::{json::Randomizer, rand::Objects};

pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256StarStar) {
fn hue(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256StarStar) {
for model in &mut objects.model_objects {
let texture_packed = dmw3_pack::Packed::try_from(
model
Expand All @@ -21,9 +21,9 @@ pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256Sta

let mut texture_tim = Tim::from(texture_raw);

let mut hue_shift = 0.0;
let mut new_hue = 0.0;
for _ in 0..preset.shuffles {
hue_shift = (rng.next_u32() % 360) as f64;
new_hue = (rng.next_u32() % 360) as f64;
}

for i in 0..64 {
Expand Down Expand Up @@ -56,29 +56,15 @@ pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256Sta
let min = r.min(g).min(b);
let delta = max - min;

let h = if delta == 0.0 {
0.0
} else if max == r {
60.0 * (((g - b) / delta) % 6.0)
} else if max == g {
60.0 * (((b - r) / delta) + 2.0)
} else {
60.0 * (((r - g) / delta) + 4.0)
};

let h = if h < 0.0 { h + 360.0 } else { h };

let s = if max == 0.0 { 0.0 } else { delta / max };

let v = max;

let new_h = (h + hue_shift).rem_euclid(360.0);

let c = v * s;
let x = c * (1.0 - ((new_h / 60.0) % 2.0 - 1.0).abs());
let x = c * (1.0 - ((new_hue / 60.0) % 2.0 - 1.0).abs());
let m = v - c;

let (r, g, b) = match new_h {
let (r, g, b) = match new_hue {
0.0..=60.0 => (c, x, 0.0),
60.0..=120.0 => (x, c, 0.0),
120.0..=180.0 => (0.0, c, x),
Expand Down Expand Up @@ -142,3 +128,9 @@ pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256Sta
}
}
}

pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256StarStar) {
if preset.models.hue_enabled {
hue(preset, objects, rng);
}
}

0 comments on commit 2509310

Please sign in to comment.