Correctly center button text

This commit is contained in:
cool-mist 2025-05-11 16:39:39 +05:30
parent c8f1c043ee
commit dc4ae27074
2 changed files with 13 additions and 24 deletions

View File

@ -1,7 +1,7 @@
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use button::{Button, ButtonAction}; use button::{Button, ButtonAction};
use macroquad::prelude::*; use macroquad::{math, prelude::*};
use sol_chess::{ use sol_chess::{
board::{Board, BoardState}, board::{Board, BoardState},
generator, generator,
@ -85,18 +85,8 @@ impl Game {
} }
pub fn update_window_size(&mut self) { pub fn update_window_size(&mut self) {
let new_height = if screen_height() > 800.0 { let new_height = math::clamp(screen_height(),400.0, 800.0);
800.0 let new_width = math::clamp(screen_width(), 500.0, 1000.0);
} else {
screen_height()
};
let new_width = if screen_width() > 1000.0 {
1000.0
} else {
screen_width()
};
if new_height == self.window_height && new_width == self.window_width { if new_height == self.window_height && new_width == self.window_width {
return; return;
} }
@ -306,15 +296,14 @@ impl Game {
self.squares = rects; self.squares = rects;
let btn_height = 0.1 * self.window_height; let btn_height = 0.1 * self.window_height;
let btn_displ = (self.window_height let btn_y = board_width + board_y + 0.15 * self.square_width;
- (self.num_squares as f32 * self.square_width + board_y) let btn_w = board_width * 0.25;
- btn_height)
* 0.5; // The button will be centered between 2 squares
let btn_y = self.num_squares as f32 * self.square_width + board_y + btn_displ; let btn_x_offset = 0.5 * (board_width / 2. - btn_w);
let btn_w = self.num_squares as f32 * self.square_width * 0.5;
let reset_btn = Button::new( let reset_btn = Button::new(
"Reset", "Reset",
board_x, board_x + btn_x_offset,
btn_y, btn_y,
btn_w, btn_w,
btn_height, btn_height,
@ -322,7 +311,7 @@ impl Game {
); );
let mut next_btn = Button::new( let mut next_btn = Button::new(
"Next", "Next",
board_x + btn_w, board_x + (0.5 * board_width) + btn_x_offset,
btn_y, btn_y,
btn_w, btn_w,
btn_height, btn_height,

View File

@ -28,8 +28,6 @@ impl Button {
pub fn draw(&self) { pub fn draw(&self) {
let bg_color = Color::from_rgba(190, 190, 190, 255); let bg_color = Color::from_rgba(190, 190, 190, 255);
let font_size = (self.rect.h * 0.3).floor() as u16;
let dims = measure_text(&self.text, None, font_size, 1.0);
draw_rectangle(self.rect.x, self.rect.y, self.rect.w, self.rect.h, bg_color); draw_rectangle(self.rect.x, self.rect.y, self.rect.w, self.rect.h, bg_color);
let font_color = if self.is_active { let font_color = if self.is_active {
@ -38,10 +36,12 @@ impl Button {
Color::from_rgba(100, 0, 0, 255) Color::from_rgba(100, 0, 0, 255)
}; };
let font_size = (0.6 * self.rect.h) as u16;
let dims = measure_text(&self.text, None, font_size, 1.0);
draw_text( draw_text(
&self.text, &self.text,
self.rect.x + (self.rect.w - dims.width) * 0.5, self.rect.x + (self.rect.w - dims.width) * 0.5,
self.rect.y + (self.rect.h - dims.height) * 0.5, self.rect.y + (self.rect.h - dims.height) * 0.5 + dims.offset_y,
font_size as f32, font_size as f32,
font_color, font_color,
); );