Skip to content

Commit

Permalink
Add debug layer ui
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed May 5, 2024
1 parent 4bc07f7 commit 3d6363b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
17 changes: 16 additions & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{collections::HashMap, path::PathBuf, str::FromStr};
use std::{
collections::{BTreeMap, HashMap},
path::PathBuf,
str::FromStr,
};

const STEPS_PER_FRAME: usize = 50;

Expand All @@ -10,6 +14,7 @@ use crate::{
random::Seed,
};
use egui::{epaint::Shadow, Color32, Frame, Margin};
use itertools::Itertools;
use std::env;

use macroquad::input::{
Expand Down Expand Up @@ -86,12 +91,21 @@ pub struct Editor {

/// whether to show the GenerationConfig settings
pub edit_preset: bool,

/// asd
pub visualize_debug_layers: HashMap<&'static str, bool>,
}

impl Editor {
pub fn new(config: GenerationConfig) -> Editor {
let configs: HashMap<String, GenerationConfig> = GenerationConfig::get_configs();
let gen = Generator::new(&config, Seed::from_u64(0)); // TODO: overwritten anyways? Option?

let mut visualize_debug_layers: HashMap<&'static str, bool> = HashMap::new();
for layer_name in gen.debug_layers.keys() {
visualize_debug_layers.insert(layer_name, false);
}

Editor {
state: EditorState::Paused(PausedState::Setup),
configs,
Expand All @@ -110,6 +124,7 @@ impl Editor {
auto_generate: false,
fixed_seed: false,
edit_preset: false,
visualize_debug_layers,
}
}

Expand Down
39 changes: 38 additions & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, isize};
use std::{collections::HashMap, env, isize};

use egui::RichText;
use tinyfiledialogs;
Expand Down Expand Up @@ -67,6 +67,29 @@ pub fn vec_edit_widget<T, F>(
});
}

pub fn hashmap_edit_widget<T, F>(
ui: &mut Ui,
hashmap: &mut HashMap<&'static str, T>,
edit_element: F,
label: &str,
collapsed: bool,
) where
F: Fn(&mut Ui, &mut T),
{
CollapsingHeader::new(label)
.default_open(!collapsed)
.show(ui, |ui| {
ui.vertical(|ui| {
for (val1, val2) in hashmap.iter_mut() {
ui.horizontal(|ui| {
ui.label(val1.to_string());
edit_element(ui, val2);
});
}
});
});
}

pub fn field_edit_widget<T, F>(
ui: &mut Ui,
value: &mut T,
Expand Down Expand Up @@ -162,6 +185,10 @@ pub fn edit_range_usize(ui: &mut Ui, values: &mut (usize, usize)) {
});
}

pub fn edit_bool(ui: &mut Ui, value: &mut bool) {
ui.add(egui::Checkbox::new(value, ""));
}

pub fn sidebar(ctx: &Context, editor: &mut Editor) {
egui::SidePanel::right("right_panel").show(ctx, |ui| {
// =======================================[ STATE CONTROL ]===================================
Expand Down Expand Up @@ -232,7 +259,17 @@ pub fn sidebar(ctx: &Context, editor: &mut Editor) {
});
}
ui.separator();
// =======================================[ DEBUG LAYERS ]===================================

hashmap_edit_widget(
ui,
&mut editor.visualize_debug_layers,
edit_bool,
"debug layers",
false,
);

ui.separator();
// =======================================[ CONFIG STORAGE ]===================================
ui.label("load/save config files:");
ui.horizontal(|ui| {
Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,13 @@ async fn main() {

draw_waypoints(&editor.config.waypoints);

for (_, debug_layer) in editor.gen.debug_layers.iter() {
draw_bool_grid(&debug_layer.grid, &debug_layer.color, &debug_layer.outline)
// draw debug layers
for (layer_name, debug_layer) in editor.gen.debug_layers.iter() {
if *editor.visualize_debug_layers.get(layer_name).unwrap() {
draw_bool_grid(&debug_layer.grid, &debug_layer.color, &debug_layer.outline)
}
}

// if let Some(edge_bugs) = &edge_bugs {
// draw_bool_grid(edge_bugs, Color::new(1.0, 0.0, 0.0, 0.1));
// }

egui_macroquad::draw();

fps_ctrl.wait_for_next_frame().await;
Expand Down
5 changes: 2 additions & 3 deletions src/rendering.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use crate::{map::BlockType, map::KernelType, position::Position, walker::CuteWalker};
use macroquad::color::colors;
use macroquad::color::Color;
Expand Down Expand Up @@ -32,9 +31,9 @@ pub fn draw_bool_grid(grid: &Array2<bool>, color: &Color, outline: &bool) {
for ((x, y), value) in grid.indexed_iter() {
if *value {
if *outline {
draw_rectangle(x as f32, y as f32, 1.0, 1.0, *color);
} else {
draw_rectangle_lines(x as f32, y as f32, 1.0, 1.0, 0.1, *color);
} else {
draw_rectangle(x as f32, y as f32, 1.0, 1.0, *color);
}
}
}
Expand Down

0 comments on commit 3d6363b

Please sign in to comment.