This commit is contained in:
cool-mist
2026-01-26 20:00:50 +05:30
parent dc61d6d956
commit 280150b0b0
18 changed files with 779 additions and 325 deletions
+173 -84
View File
@@ -1,53 +1,66 @@
use super::{Game, constants};
use crate::widgets::*;
use crate::{game::GameMode, widgets::*};
use macroquad::{math, prelude::*};
impl Game {
fn initialize_drawables(&mut self) {
fn initialize_drawables(&mut self, new_width: f32, new_height: f32) {
self.window_height = new_height;
self.window_width = new_width;
let min_dimension = f32::min(self.window_height, self.window_width);
let square_width = constants::BOARD_SQUARE_WIDTH_MULTIPLIER * min_dimension;
let num_squares = self.board.num_squares;
let board_width = square_width * num_squares as f32;
let board_x = (self.window_width - board_width) / 2.0;
let board_x = (self.window_width * 0.6 - board_width) / 2.0;
let board_y = (self.window_height - board_width) / 2.0;
let board_rect = Rect::new(board_x, board_y, board_width, board_width);
self.board.initialize_drawables(square_width, board_rect);
self.heading_font_size = constants::HEADING_FONT_SIZE_MULTIPLIER * min_dimension;
let f = self.heading_font_size.floor() as u16;
let dims = measure_text(
let heading_font_size = constants::HEADING_FONT_SIZE_MULTIPLIER * min_dimension;
let f = heading_font_size.floor() as u16;
let heading_text_dims = measure_text(
self.heading_text.as_str(),
Some(&self.resources.font()),
f,
1.0,
);
self.heading_rect = Rect::new(
board_x + (board_width - dims.width) / 2.0,
board_y - dims.height - constants::TOP_HEADING_OFFSET_MULTIPLIER * square_width,
dims.width,
dims.height,
let heading_rect = Rect::new(
(self.window_width - heading_text_dims.width) / 2.0,
board_y
- heading_text_dims.height
- constants::TOP_HEADING_OFFSET_MULTIPLIER * square_width,
heading_text_dims.width,
heading_text_dims.height,
);
self.heading
.initialize_drawables(heading_rect, heading_font_size, &self.resources);
// Buttons
let btn_h = constants::BUTTON_HEIGHT_MULTIPLIER * min_dimension;
let btn_w = board_width * constants::BUTTON_WIDTH_MULTIPLIER;
let btn_w_sq = btn_h;
let btn_w = constants::BUTTON_WIDTH_MULTIPLIER * board_width;
// Bottom row
let bottom_row_y =
board_width + board_y + constants::BOTTOM_BUTTON_ROW_OFFSET_MULTIPLIER * square_width;
self.reset_btn.initialize_drawables(Rect::new(
board_x + (square_width - btn_w_sq) / 2.,
board_width + board_y + constants::BOTTOM_ROW_OFFSET_MULTIPLIER * square_width;
self.rules_btn.initialize_drawables(Rect::new(
board_x + (square_width - btn_w) / 2.,
bottom_row_y,
btn_w_sq,
btn_w,
btn_h,
));
let btn_next_x_offset = (board_width - square_width) + (square_width - btn_w_sq) / 2.;
self.reset_btn.initialize_drawables(Rect::new(
board_x + square_width + (square_width - btn_w) / 2.,
bottom_row_y,
btn_w,
btn_h,
));
let btn_next_x_offset = (board_width - square_width) + (square_width - btn_w) / 2.;
self.next_btn.initialize_drawables(Rect::new(
board_x + btn_next_x_offset,
bottom_row_y,
btn_w_sq,
btn_w,
btn_h,
));
@@ -57,77 +70,131 @@ impl Game {
btn_w * 2.,
btn_h,
);
self.id_text = IdTextInput::new(id_text_rect);
self.id_text_btn.initialize_drawables(id_text_rect);
// Left column
let left_column_x =
board_x - constants::BOTTOM_RIGHT_ROW_OFFSET_MULTIPLIER * square_width - btn_w;
self.rules_btn.initialize_drawables(Rect::new(
left_column_x,
board_y + board_rect.h - square_width + (square_width - btn_h) / 2.,
btn_w,
btn_h,
));
self.rules_font_size = heading_font_size;
// Right column
let right_column_x =
board_x + board_width + constants::BOTTOM_RIGHT_ROW_OFFSET_MULTIPLIER * square_width;
self.easy_btn.initialize_drawables(Rect::new(
right_column_x,
board_y + square_width + (square_width - btn_h) / 2.,
btn_w,
board_x + board_width + constants::RIGHT_COLUMN_OFFSET_MULTIPLIER * square_width;
self.get_game_mode_button(&GameMode::Easy)
.initialize_drawables(Rect::new(
right_column_x,
board_y + (square_width - btn_h) / 2.,
btn_w,
btn_h,
));
self.get_game_mode_button(&GameMode::Medium)
.initialize_drawables(Rect::new(
right_column_x,
board_y + square_width + (square_width - btn_h) / 2.,
btn_w,
btn_h,
));
self.get_game_mode_button(&GameMode::Hard)
.initialize_drawables(Rect::new(
right_column_x,
board_y + 2. * square_width + (square_width - btn_h) / 2.,
btn_w,
btn_h,
));
self.get_game_mode_button(&GameMode::Custom)
.initialize_drawables(Rect::new(
right_column_x,
board_y + 3. * square_width + (square_width - btn_h) / 2.,
btn_w,
btn_h,
));
// Right column 2
let right_column_2_x =
right_column_x + btn_w + constants::RIGHT_COLUMN_OFFSET_MULTIPLIER * square_width;
self.generate_btn.initialize_drawables(Rect::new(
right_column_2_x,
board_y + board_width - square_width + (square_width - btn_h) / 2.,
2. * btn_w,
btn_h,
));
self.medium_btn.initialize_drawables(Rect::new(
right_column_x,
board_y + 2. * square_width + (square_width - btn_h) / 2.,
btn_w,
btn_h,
));
self.pieces_label.initialize_drawables(
Rect::new(
right_column_2_x,
board_y + (square_width + btn_h) / 2.,
btn_w,
btn_h,
),
0.30 * btn_h,
&self.resources,
);
self.pieces_counter.initialize_drawables(
Rect::new(
right_column_2_x,
board_y + square_width + (2. * square_width - 2. * btn_h) / 2.,
btn_w,
2. * btn_h,
),
30.,
&self.resources,
);
self.hard_btn.initialize_drawables(Rect::new(
right_column_x,
board_y + 3. * square_width + (square_width - btn_h) / 2.,
btn_w,
btn_h,
));
// Right column 3
let right_column_3_x = right_column_2_x + btn_w;
self.max_age_label.initialize_drawables(
Rect::new(
right_column_2_x + btn_w,
board_y + (square_width + btn_h) / 2.,
btn_w,
btn_h,
),
0.30 * btn_h,
&self.resources,
);
self.max_age_counter.initialize_drawables(
Rect::new(
right_column_3_x,
board_y + square_width + (2. * square_width - 2. * btn_h) / 2.,
btn_w,
2. * btn_h,
),
30.,
&self.resources,
);
}
pub fn draw(&mut self) {
self.update_window_size();
self.draw_heading();
self.board.draw(
self.show_rules,
self.heading_font_size,
&self.resources,
&self.settings,
);
self.id_text.draw(&self.resources, &self.puzzle.board.id);
self.draw_board();
self.draw_id_text_button();
self.draw_buttons();
self.draw_setting_controls();
self.draw_debug();
}
fn draw_heading(&self) {
let f = self.heading_font_size.floor() as u16;
let dims = measure_text(
self.heading_text.as_str(),
Some(&self.resources.font()),
f,
1.0,
);
let draw_text_params = TextParams {
font_size: f,
font: Some(&self.resources.font()),
color: BLACK,
..Default::default()
self.heading.draw(&self.resources);
}
fn draw_board(&mut self) {
let params = BoardDrawParams {
show_rules: self.show_rules,
rules_font_size: self.rules_font_size,
};
draw_text_ex(
self.heading_text.as_str(),
self.heading_rect.x,
self.heading_rect.y + dims.offset_y,
draw_text_params,
);
self.board.draw(&params, &self.resources, &self.settings);
}
fn get_game_mode_button<'a>(&'a mut self, mode: &GameMode) -> &'a mut ButtonWidget {
self.game_mode_btns.get_mut(mode).unwrap()
}
fn draw_game_mode_button(&mut self, mode: &GameMode, color: &UiColor, text: &str) {
let btn = self.game_mode_btns.get_mut(mode).unwrap();
btn.draw(text, color, &self.resources);
}
fn draw_buttons(&mut self) {
@@ -143,26 +210,50 @@ impl Game {
&self.resources,
);
self.easy_btn.draw(
self.draw_game_mode_button(
&GameMode::Easy,
&UiColor::Yellow,
constants::EASY_BUTTON_TEXT,
&UiColor::Yellow,
&self.resources,
);
self.medium_btn.draw(
self.draw_game_mode_button(
&GameMode::Medium,
&UiColor::Yellow,
constants::MEDIUM_BUTTON_TEXT,
&UiColor::Yellow,
&self.resources,
);
self.hard_btn.draw(
constants::HARD_BUTTON_TEXT,
self.draw_game_mode_button(
&GameMode::Hard,
&UiColor::Yellow,
constants::HARD_BUTTON_TEXT,
);
self.draw_game_mode_button(
&GameMode::Custom,
&UiColor::Blue,
constants::CUSTOM_BUTTON_TEXT,
);
self.generate_btn.draw(
constants::GENERATE_BUTTON_TEXT,
&UiColor::Pink,
&self.resources,
);
self.rules_btn
.draw(&self.rules_text, &UiColor::Brown, &self.resources);
.draw(&self.rules_btn_text, &UiColor::Brown, &self.resources);
}
fn draw_setting_controls(&mut self) {
self.pieces_label.draw(&self.resources);
self.pieces_counter.draw(&self.resources);
self.max_age_label.draw(&self.resources);
self.max_age_counter.draw(&self.resources);
}
fn draw_id_text_button(&self) {
// self.id_text_btn
// .draw(&self.puzzle.board.id, &UiColor::Yellow, &self.resources);
}
fn draw_debug(&self) {
@@ -198,8 +289,6 @@ impl Game {
return;
}
self.window_height = new_height;
self.window_width = new_width;
self.initialize_drawables();
self.initialize_drawables(new_width, new_height);
}
}