use std::fmt;
use ggez::{graphics, Context, GameResult};
use crate::ChessGui;
#[derive(Copy, Clone, Eq, PartialEq, Default, Debug)]
pub enum Align {
Left,
Right,
#[default]
Center,
}
#[derive(Copy, Clone)]
pub struct Button {
pub id: &'static str,
enable: bool,
rect: graphics::Rect,
image_path: Option<&'static str>,
color: graphics::Color,
text: &'static str,
align: Align,
func: Option<fn(&mut ChessGui)>,
}
impl Button {
#[allow(clippy::too_many_arguments)]
pub fn new(
id: &'static str,
enable: bool,
rect: graphics::Rect,
color: graphics::Color,
text: &'static str,
align: Align,
func: Option<fn(&mut ChessGui)>,
) -> Self {
Button {
id,
enable,
rect,
image_path: None,
color,
text,
align,
func,
}
}
pub fn is_enable(&self) -> bool {
self.enable
}
pub fn enable(&mut self) {
self.enable = true;
}
pub fn disable(&mut self) {
self.enable = false;
}
pub fn set_image(&mut self, path: Option<&'static str>) -> Self {
self.image_path = path;
*self
}
pub fn contains(&self, x: f32, y: f32) -> bool {
self.rect.contains([x, y])
}
pub fn draw(&self, ctx: &mut Context, font_path: &str, font_scale: f32) -> GameResult {
if self.enable {
if self.image_path.is_some() {
self.draw_image(ctx)?;
} else {
self.draw_rect(ctx)?;
self.draw_text(ctx, font_path, font_scale)?;
}
}
Ok(())
}
fn draw_rect(&self, ctx: &mut Context) -> GameResult {
let mesh = graphics::MeshBuilder::new()
.rectangle(graphics::DrawMode::stroke(3.0), self.rect, self.color)?
.build(ctx)?;
graphics::draw(ctx, &mesh, graphics::DrawParam::default())?;
Ok(())
}
fn draw_text(&self, ctx: &mut Context, font_path: &str, font_scale: f32) -> GameResult {
let font = graphics::Font::new(ctx, font_path)?;
let text = graphics::Text::new((self.text, font, font_scale));
let dest_point = match self.align {
Align::Left => [self.rect.x, self.rect.y],
Align::Right => [
self.rect.x + self.rect.w - text.width(ctx),
self.rect.y + self.rect.h - text.height(ctx),
],
Align::Center => [
self.rect.x + (self.rect.w - text.width(ctx)) / 2.0,
self.rect.y + (self.rect.h - text.height(ctx)) / 2.0,
],
};
graphics::draw(ctx, &text, (dest_point, self.color))?;
Ok(())
}
fn draw_image(&self, ctx: &mut Context) -> GameResult {
let image = graphics::Image::new(ctx, self.image_path.unwrap()).expect("Image load error");
let image_scale = [
self.rect.w / image.width() as f32,
self.rect.h / image.height() as f32,
];
let dp = graphics::DrawParam::new()
.dest(self.rect.point())
.scale(image_scale);
graphics::draw(ctx, &image, dp)?;
Ok(())
}
pub fn clicked(&self, chess_gui: &mut ChessGui) {
if self.enable {
if let Some(func) = self.func {
func(chess_gui);
}
}
}
}
impl fmt::Display for Button {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.id)
}
}
impl fmt::Debug for Button {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.id)
}
}