2026-05-15 23:42:14 -05:00
|
|
|
use eframe::egui;
|
2026-05-16 01:10:04 -05:00
|
|
|
use egui::{Align2, FontId, Key, Pos2, Rect, vec2};
|
|
|
|
|
|
|
|
|
|
mod game;
|
2026-05-16 01:18:02 -05:00
|
|
|
use game::{GameState, Glyph, BOARD_W, BOARD_H};
|
2026-05-16 01:10:04 -05:00
|
|
|
|
|
|
|
|
const CELL_W: f32 = 14.0;
|
|
|
|
|
const CELL_H: f32 = 20.0;
|
2026-05-15 23:42:14 -05:00
|
|
|
|
2026-05-16 01:18:02 -05:00
|
|
|
const MENU_H: f32 = 24.0;
|
|
|
|
|
const PANEL_MARGIN: f32 = 8.0;
|
|
|
|
|
|
2026-05-15 23:42:14 -05:00
|
|
|
fn main() -> eframe::Result<()> {
|
2026-05-16 01:18:02 -05:00
|
|
|
let board_px_w = BOARD_W as f32 * CELL_W + 2.0 * PANEL_MARGIN;
|
|
|
|
|
let board_px_h = BOARD_H as f32 * CELL_H + MENU_H + 2.0 * PANEL_MARGIN;
|
|
|
|
|
let options = eframe::NativeOptions {
|
|
|
|
|
viewport: egui::ViewportBuilder::default()
|
|
|
|
|
.with_inner_size([board_px_w, board_px_h])
|
|
|
|
|
.with_min_inner_size([board_px_w, board_px_h]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
2026-05-15 23:42:14 -05:00
|
|
|
eframe::run_native(
|
|
|
|
|
"Viberogue",
|
|
|
|
|
options,
|
2026-05-16 01:10:04 -05:00
|
|
|
Box::new(|_cc| Ok(Box::new(App::new()))),
|
2026-05-15 23:42:14 -05:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 01:10:04 -05:00
|
|
|
struct App {
|
|
|
|
|
state: GameState,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
Self { state: GameState::new() }
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-15 23:42:14 -05:00
|
|
|
|
|
|
|
|
impl eframe::App for App {
|
|
|
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
2026-05-16 01:10:04 -05:00
|
|
|
ctx.input(|i| {
|
|
|
|
|
if i.key_pressed(Key::ArrowUp) { self.state.try_move( 0, -1); }
|
|
|
|
|
if i.key_pressed(Key::ArrowDown) { self.state.try_move( 0, 1); }
|
|
|
|
|
if i.key_pressed(Key::ArrowLeft) { self.state.try_move(-1, 0); }
|
|
|
|
|
if i.key_pressed(Key::ArrowRight) { self.state.try_move( 1, 0); }
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-15 23:42:14 -05:00
|
|
|
egui::TopBottomPanel::top("menu_bar").show(ctx, |ui| {
|
|
|
|
|
egui::MenuBar::new().ui(ui, |ui| {
|
|
|
|
|
ui.menu_button("File", |ui| {
|
|
|
|
|
if ui.button("Exit").clicked() {
|
|
|
|
|
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
2026-05-16 01:10:04 -05:00
|
|
|
let board = &self.state.board;
|
2026-05-16 01:18:02 -05:00
|
|
|
let available = ui.available_rect_before_wrap();
|
|
|
|
|
let board_px = vec2(board.width as f32 * CELL_W, board.height as f32 * CELL_H);
|
|
|
|
|
let offset = ((available.size() - board_px) / 2.0).max(egui::Vec2::ZERO);
|
|
|
|
|
let origin = available.min + offset;
|
2026-05-16 01:10:04 -05:00
|
|
|
let painter = ui.painter();
|
|
|
|
|
|
|
|
|
|
for y in 0..board.height {
|
|
|
|
|
for x in 0..board.width {
|
|
|
|
|
let (glyph, _) = board.get(x, y);
|
|
|
|
|
draw_glyph(painter, origin, x, y, glyph);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let player_glyph = Glyph::player();
|
|
|
|
|
draw_glyph(
|
|
|
|
|
painter,
|
|
|
|
|
origin,
|
|
|
|
|
self.state.player.x as usize,
|
|
|
|
|
self.state.player.y as usize,
|
|
|
|
|
&player_glyph,
|
|
|
|
|
);
|
2026-05-15 23:42:14 -05:00
|
|
|
});
|
2026-05-16 01:10:04 -05:00
|
|
|
|
|
|
|
|
ctx.request_repaint();
|
2026-05-15 23:42:14 -05:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-16 01:10:04 -05:00
|
|
|
|
|
|
|
|
fn draw_glyph(painter: &egui::Painter, origin: Pos2, x: usize, y: usize, glyph: &Glyph) {
|
|
|
|
|
let tl = origin + vec2(x as f32 * CELL_W, y as f32 * CELL_H);
|
|
|
|
|
let rect = Rect::from_min_size(tl, vec2(CELL_W, CELL_H));
|
|
|
|
|
let center = rect.center();
|
|
|
|
|
painter.rect_filled(rect, 0.0, glyph.bg);
|
|
|
|
|
painter.text(center, Align2::CENTER_CENTER, glyph.ch, FontId::monospace(CELL_H), glyph.fg);
|
|
|
|
|
}
|