Skip to content

Commit

Permalink
Fix various UI bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed May 4, 2024
1 parent 8d4af4a commit c50b871
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
11 changes: 11 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ pub struct GenerationConfig {
}

impl GenerationConfig {
/// returns an error if the configuration would result in a crash
pub fn validate(&self) -> Result<(), &'static str> {
// 1. Check that there is no inner kernel size of 0
for (inner_size, _) in self.inner_size_probs.iter() {
if *inner_size == 0 {
return Err("Invalid Config! (inner_size = 0)");
}
}
Ok(())
}

/// stores GenerationConfig in cwd as <name>.json
pub fn save(&self, path: &str) {
let mut file = File::create(path).expect("failed to create config file");
Expand Down
8 changes: 5 additions & 3 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ impl Generator {
pub fn new(config: &GenerationConfig, seed: Seed) -> Generator {
let spawn = Position::new(50, 250);
let map = Map::new(300, 300, BlockType::Hookable, spawn.clone());
let inner_size = config.inner_size_probs[0].0; // TODO: better initial?
let init_inner_kernel = Kernel::new(inner_size, 0.0);
let init_outer_kernel = Kernel::new(inner_size + 2, 0.0);
let init_inner_kernel = Kernel::new(5, 0.0);
let init_outer_kernel = Kernel::new(7, 0.0);
let walker = CuteWalker::new(spawn, init_inner_kernel, init_outer_kernel, config);
let rnd = Random::new(seed, config);

Expand All @@ -52,6 +51,9 @@ impl Generator {
}

if !self.walker.finished {
// validate config - TODO: add build flag which skips this?
config.validate()?;

// randomly mutate kernel
self.walker.mutate_kernel(config, &mut self.rnd);

Expand Down
44 changes: 22 additions & 22 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn vec_edit_widget<T, F>(
vec.push(Default::default());
};

if ui.button("-").clicked() && !vec.is_empty() {
if ui.button("-").clicked() && vec.len() > 1 {
vec.pop();
};
});
Expand Down Expand Up @@ -304,27 +304,27 @@ pub fn sidebar(ctx: &Context, editor: &mut Editor) {
true,
);

vec_edit_widget(
ui,
&mut editor.config.inner_size_probs,
edit_probability_tuple,
"inner size probs",
true,
false,
);
normalize_probs(&mut editor.config.inner_size_probs);

vec_edit_widget(
ui,
&mut editor.config.outer_margin_probs,
edit_probability_tuple,
"outer margin probs",
true,
false,
);
normalize_probs(&mut editor.config.outer_margin_probs);
ui.add_enabled_ui(editor.is_setup(), |ui| {
vec_edit_widget(
ui,
&mut editor.config.inner_size_probs,
edit_probability_tuple,
"inner size probs",
true,
false,
);
normalize_probs(&mut editor.config.inner_size_probs);

ui.separator();
vec_edit_widget(
ui,
&mut editor.config.outer_margin_probs,
edit_probability_tuple,
"outer margin probs",
true,
false,
);
normalize_probs(&mut editor.config.outer_margin_probs);
});

field_edit_widget(
ui,
Expand Down Expand Up @@ -359,7 +359,7 @@ pub fn sidebar(ctx: &Context, editor: &mut Editor) {
);

// only show these in setup mode
ui.add_visible_ui(editor.is_setup(), |ui| {
ui.add_enabled_ui(editor.is_setup(), |ui| {
vec_edit_widget(
ui,
&mut editor.config.waypoints,
Expand Down

0 comments on commit c50b871

Please sign in to comment.