diff --git a/src/gui.rs b/src/gui.rs index 21b84e6..2040af9 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -13,6 +13,7 @@ mod checkbox; mod encounters; mod file_upload; mod maps; +mod models; mod number_field; mod number_field_float; mod parties; @@ -176,6 +177,7 @@ fn app() -> Element { shops::shops {}, encounters::encounters {}, maps::maps {}, + models::models {} }, } } diff --git a/src/gui/models.rs b/src/gui/models.rs new file mode 100644 index 0000000..d4385b1 --- /dev/null +++ b/src/gui/models.rs @@ -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::>(); + 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; + } + }, + }, + }, + } + } +} diff --git a/src/json.rs b/src/json.rs index 2708bc7..3a797c5 100644 --- a/src/json.rs +++ b/src/json.rs @@ -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)] diff --git a/src/rand/models.rs b/src/rand/models.rs index 78e9e5a..f418142 100644 --- a/src/rand/models.rs +++ b/src/rand/models.rs @@ -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 @@ -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 { @@ -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), @@ -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); + } +}