Size window to fit board including panel margin; center board on resize
Accounts for egui's 8px CentralPanel margin in the minimum window size. When the window is larger than the minimum, the board is centered in the available area. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+5
-2
@@ -1,5 +1,8 @@
|
||||
use eframe::egui::Color32;
|
||||
|
||||
pub const BOARD_W: usize = 60;
|
||||
pub const BOARD_H: usize = 25;
|
||||
|
||||
pub struct Glyph {
|
||||
pub ch: char,
|
||||
pub fg: Color32,
|
||||
@@ -64,8 +67,8 @@ pub struct GameState {
|
||||
|
||||
impl GameState {
|
||||
pub fn new() -> Self {
|
||||
let w = 60;
|
||||
let h = 25;
|
||||
let w = BOARD_W;
|
||||
let h = BOARD_H;
|
||||
|
||||
let mut board = Board {
|
||||
width: w,
|
||||
|
||||
+16
-3
@@ -2,13 +2,23 @@ use eframe::egui;
|
||||
use egui::{Align2, FontId, Key, Pos2, Rect, vec2};
|
||||
|
||||
mod game;
|
||||
use game::{GameState, Glyph};
|
||||
use game::{GameState, Glyph, BOARD_W, BOARD_H};
|
||||
|
||||
const CELL_W: f32 = 14.0;
|
||||
const CELL_H: f32 = 20.0;
|
||||
|
||||
const MENU_H: f32 = 24.0;
|
||||
const PANEL_MARGIN: f32 = 8.0;
|
||||
|
||||
fn main() -> eframe::Result<()> {
|
||||
let options = eframe::NativeOptions::default();
|
||||
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()
|
||||
};
|
||||
eframe::run_native(
|
||||
"Viberogue",
|
||||
options,
|
||||
@@ -47,7 +57,10 @@ impl eframe::App for App {
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
let board = &self.state.board;
|
||||
let origin = ui.available_rect_before_wrap().min;
|
||||
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;
|
||||
let painter = ui.painter();
|
||||
|
||||
for y in 0..board.height {
|
||||
|
||||
Reference in New Issue
Block a user