2026-03-06 08:41:39 -06:00
|
|
|
mod layout;
|
2026-03-06 09:40:22 -06:00
|
|
|
mod seed;
|
2026-03-06 09:24:24 -06:00
|
|
|
mod ui;
|
2026-03-06 08:41:39 -06:00
|
|
|
|
2026-03-06 09:19:03 -06:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
2026-03-05 19:17:01 -06:00
|
|
|
use eframe::egui;
|
|
|
|
|
use egui::{Color32, Stroke};
|
2026-03-06 08:41:39 -06:00
|
|
|
use layout::{DungeonLayout, generate_layout};
|
2026-03-06 09:24:24 -06:00
|
|
|
use ui::{UiSettings, draw_side_panel};
|
2026-03-05 19:17:01 -06:00
|
|
|
|
|
|
|
|
fn main() -> eframe::Result<()> {
|
|
|
|
|
let options = eframe::NativeOptions::default();
|
|
|
|
|
|
|
|
|
|
eframe::run_native(
|
|
|
|
|
"Desktop Dungeon Generator",
|
|
|
|
|
options,
|
|
|
|
|
Box::new(|_cc| Ok(Box::new(DungeonApp::default()))),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct DungeonApp {
|
2026-03-06 09:24:24 -06:00
|
|
|
settings: UiSettings,
|
2026-03-06 08:41:39 -06:00
|
|
|
layout: DungeonLayout,
|
2026-03-05 19:17:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for DungeonApp {
|
|
|
|
|
fn default() -> Self {
|
2026-03-06 09:24:24 -06:00
|
|
|
let settings = UiSettings::default();
|
2026-03-06 08:52:56 -06:00
|
|
|
let layout = generate_layout(
|
2026-03-06 09:24:24 -06:00
|
|
|
settings.cols,
|
|
|
|
|
settings.rows,
|
|
|
|
|
settings.room_count,
|
2026-03-06 09:40:22 -06:00
|
|
|
settings.seed,
|
2026-03-06 09:24:24 -06:00
|
|
|
settings.min_room_size,
|
|
|
|
|
settings.max_room_size,
|
|
|
|
|
settings.square_rooms_only,
|
|
|
|
|
settings.corridor_randomness,
|
|
|
|
|
settings.dead_end_rooms_percent,
|
2026-03-06 08:52:56 -06:00
|
|
|
);
|
2026-03-06 08:41:39 -06:00
|
|
|
|
2026-03-06 09:40:22 -06:00
|
|
|
Self { settings, layout }
|
2026-03-05 19:17:01 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl eframe::App for DungeonApp {
|
|
|
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
2026-03-06 09:24:24 -06:00
|
|
|
let panel_result = draw_side_panel(ctx, &mut self.settings);
|
2026-03-06 08:41:39 -06:00
|
|
|
|
2026-03-06 09:24:24 -06:00
|
|
|
if self.settings.min_room_size > self.settings.max_room_size {
|
|
|
|
|
self.settings.max_room_size = self.settings.min_room_size;
|
2026-03-06 08:52:56 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 09:24:24 -06:00
|
|
|
if panel_result.settings_changed {
|
|
|
|
|
self.regenerate_layout();
|
2026-03-06 08:41:39 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-05 19:17:01 -06:00
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
|
|
|
|
let available = ui.available_size_before_wrap();
|
|
|
|
|
let (response, painter) = ui.allocate_painter(available, egui::Sense::hover());
|
|
|
|
|
let canvas = response.rect.shrink(12.0);
|
2026-03-06 09:24:24 -06:00
|
|
|
let geometry = draw_grid(&painter, canvas, self.settings.cols, self.settings.rows);
|
2026-03-06 08:41:39 -06:00
|
|
|
draw_layout(&painter, &geometry, &self.layout);
|
2026-03-05 19:17:01 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 09:24:24 -06:00
|
|
|
impl DungeonApp {
|
|
|
|
|
fn regenerate_layout(&mut self) {
|
|
|
|
|
self.layout = generate_layout(
|
|
|
|
|
self.settings.cols,
|
|
|
|
|
self.settings.rows,
|
|
|
|
|
self.settings.room_count,
|
2026-03-06 09:40:22 -06:00
|
|
|
self.settings.seed,
|
2026-03-06 09:24:24 -06:00
|
|
|
self.settings.min_room_size,
|
|
|
|
|
self.settings.max_room_size,
|
|
|
|
|
self.settings.square_rooms_only,
|
|
|
|
|
self.settings.corridor_randomness,
|
|
|
|
|
self.settings.dead_end_rooms_percent,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 08:41:39 -06:00
|
|
|
struct GridGeometry {
|
|
|
|
|
rect: egui::Rect,
|
|
|
|
|
cell_size: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw_grid(painter: &egui::Painter, area: egui::Rect, cols: usize, rows: usize) -> GridGeometry {
|
2026-03-05 19:17:01 -06:00
|
|
|
let cols = cols.max(1);
|
|
|
|
|
let rows = rows.max(1);
|
|
|
|
|
|
|
|
|
|
let cell_size = (area.width() / cols as f32).min(area.height() / rows as f32);
|
|
|
|
|
let grid_width = cell_size * cols as f32;
|
|
|
|
|
let grid_height = cell_size * rows as f32;
|
|
|
|
|
|
|
|
|
|
let grid_rect = egui::Rect::from_min_size(
|
|
|
|
|
egui::pos2(
|
|
|
|
|
area.center().x - (grid_width * 0.5),
|
|
|
|
|
area.center().y - (grid_height * 0.5),
|
|
|
|
|
),
|
|
|
|
|
egui::vec2(grid_width, grid_height),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let stroke = Stroke::new(1.0, Color32::from_gray(160));
|
|
|
|
|
painter.rect_stroke(grid_rect, 0.0, stroke, egui::StrokeKind::Middle);
|
|
|
|
|
|
|
|
|
|
for col in 1..cols {
|
|
|
|
|
let x = grid_rect.left() + (col as f32 * cell_size);
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(x, grid_rect.top()),
|
|
|
|
|
egui::pos2(x, grid_rect.bottom()),
|
|
|
|
|
],
|
|
|
|
|
stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for row in 1..rows {
|
|
|
|
|
let y = grid_rect.top() + (row as f32 * cell_size);
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(grid_rect.left(), y),
|
|
|
|
|
egui::pos2(grid_rect.right(), y),
|
|
|
|
|
],
|
|
|
|
|
stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-03-06 08:41:39 -06:00
|
|
|
|
|
|
|
|
GridGeometry {
|
|
|
|
|
rect: grid_rect,
|
|
|
|
|
cell_size,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw_layout(painter: &egui::Painter, geometry: &GridGeometry, layout: &DungeonLayout) {
|
|
|
|
|
let room_fill = Color32::from_rgb(70, 120, 160);
|
|
|
|
|
let corridor_fill = Color32::from_rgb(210, 190, 120);
|
2026-03-06 09:19:03 -06:00
|
|
|
let outline_stroke = Stroke::new(3.0, Color32::BLACK);
|
|
|
|
|
let mut corridor_cells_set = HashSet::new();
|
|
|
|
|
let mut corridor_edges_set = HashSet::new();
|
2026-03-06 08:41:39 -06:00
|
|
|
|
|
|
|
|
for corridor in &layout.corridors {
|
2026-03-06 09:19:03 -06:00
|
|
|
corridor_edges_set.insert(normalized_edge(corridor.from, corridor.to));
|
2026-03-06 08:41:39 -06:00
|
|
|
for (col, row) in corridor_cells(corridor.from, corridor.to) {
|
2026-03-06 09:19:03 -06:00
|
|
|
corridor_cells_set.insert((col, row));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for &(col, row) in &corridor_cells_set {
|
|
|
|
|
painter.rect_filled(cell_rect(geometry, col, row), 0.0, corridor_fill);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for &(col, row) in &corridor_cells_set {
|
|
|
|
|
let rect = cell_rect(geometry, col, row);
|
|
|
|
|
let right = (col + 1, row);
|
|
|
|
|
let bottom = (col, row + 1);
|
|
|
|
|
|
|
|
|
|
if col == 0 || !corridor_cells_set.contains(&(col - 1, row)) {
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(rect.left(), rect.top()),
|
|
|
|
|
egui::pos2(rect.left(), rect.bottom()),
|
|
|
|
|
],
|
|
|
|
|
outline_stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if !corridor_cells_set.contains(&right)
|
|
|
|
|
|| !corridor_edges_set.contains(&normalized_edge((col, row), right))
|
|
|
|
|
{
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(rect.right(), rect.top()),
|
|
|
|
|
egui::pos2(rect.right(), rect.bottom()),
|
|
|
|
|
],
|
|
|
|
|
outline_stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if row == 0 || !corridor_cells_set.contains(&(col, row - 1)) {
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(rect.left(), rect.top()),
|
|
|
|
|
egui::pos2(rect.right(), rect.top()),
|
|
|
|
|
],
|
|
|
|
|
outline_stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if !corridor_cells_set.contains(&bottom)
|
|
|
|
|
|| !corridor_edges_set.contains(&normalized_edge((col, row), bottom))
|
|
|
|
|
{
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(rect.left(), rect.bottom()),
|
|
|
|
|
egui::pos2(rect.right(), rect.bottom()),
|
|
|
|
|
],
|
|
|
|
|
outline_stroke,
|
|
|
|
|
);
|
2026-03-06 08:41:39 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for room in &layout.rooms {
|
|
|
|
|
let left = geometry.rect.left() + room.x as f32 * geometry.cell_size;
|
|
|
|
|
let top = geometry.rect.top() + room.y as f32 * geometry.cell_size;
|
|
|
|
|
let right = left + room.width as f32 * geometry.cell_size;
|
|
|
|
|
let bottom = top + room.height as f32 * geometry.cell_size;
|
|
|
|
|
let room_rect = egui::Rect::from_min_max(egui::pos2(left, top), egui::pos2(right, bottom));
|
|
|
|
|
|
|
|
|
|
painter.rect_filled(room_rect, 0.0, room_fill);
|
2026-03-06 09:10:40 -06:00
|
|
|
painter.rect_stroke(room_rect, 0.0, outline_stroke, egui::StrokeKind::Middle);
|
2026-03-06 08:41:39 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn corridor_cells(from: (usize, usize), to: (usize, usize)) -> Vec<(usize, usize)> {
|
|
|
|
|
let mut cells = Vec::new();
|
|
|
|
|
if from.0 == to.0 {
|
|
|
|
|
let x = from.0;
|
|
|
|
|
let start = from.1.min(to.1);
|
|
|
|
|
let end = from.1.max(to.1);
|
|
|
|
|
for y in start..=end {
|
|
|
|
|
cells.push((x, y));
|
|
|
|
|
}
|
|
|
|
|
} else if from.1 == to.1 {
|
|
|
|
|
let y = from.1;
|
|
|
|
|
let start = from.0.min(to.0);
|
|
|
|
|
let end = from.0.max(to.0);
|
|
|
|
|
for x in start..=end {
|
|
|
|
|
cells.push((x, y));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cells
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cell_rect(geometry: &GridGeometry, col: usize, row: usize) -> egui::Rect {
|
|
|
|
|
let left = geometry.rect.left() + col as f32 * geometry.cell_size;
|
|
|
|
|
let top = geometry.rect.top() + row as f32 * geometry.cell_size;
|
|
|
|
|
egui::Rect::from_min_size(
|
|
|
|
|
egui::pos2(left, top),
|
|
|
|
|
egui::vec2(geometry.cell_size, geometry.cell_size),
|
|
|
|
|
)
|
2026-03-05 19:17:01 -06:00
|
|
|
}
|
2026-03-06 09:19:03 -06:00
|
|
|
|
|
|
|
|
fn normalized_edge(a: (usize, usize), b: (usize, usize)) -> ((usize, usize), (usize, usize)) {
|
|
|
|
|
if a <= b { (a, b) } else { (b, a) }
|
|
|
|
|
}
|