From ff13e4972072fa67dbf407ae7279397a2587f10e Mon Sep 17 00:00:00 2001 From: cool-mist Date: Sun, 11 May 2025 23:47:03 +0530 Subject: [PATCH] decorations --- src/game.rs | 25 ++++++++++++++++--------- src/game/button.rs | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/game.rs b/src/game.rs index cdae3c9..c870d2e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -3,7 +3,7 @@ use std::{ fmt::{self, Display, Formatter}, }; -use button::Button; +use button::{Button, ButtonColor}; use macroquad::{math, prelude::*, rand}; use sol_chess::{ board::{Board, BoardState}, @@ -189,9 +189,9 @@ impl Game { let mut selected_square = None; self.squares.iter().for_each(|square| { let color = if square.is_source { - Color::from_rgba(152, 152, 152, 255) + Color::from_rgba(112, 105, 141, 255) } else if square.is_target { - Color::from_rgba(152, 129, 123, 255) + Color::from_rgba(112, 150, 141, 255) } else { square.color }; @@ -279,7 +279,7 @@ impl Game { let dims = measure_text(self.heading_text.as_str(), None, f, 1.0); self.heading_rect = Rect::new( board_x + (board_width - dims.width) / 2.0, - board_y / 2.0, + 0.85 * board_y, dims.width, dims.height, ); @@ -316,13 +316,20 @@ impl Game { let btn_y = board_width + board_y + 0.3 * self.square_width; let btn_x_offset = 0.5 * (board_width / 2. - btn_w); - let reset_btn = Button::new("Reset", board_x + btn_x_offset, btn_y, btn_w, btn_h); + let reset_btn = Button::new( + "Reset", + Rect::new(board_x + btn_x_offset, btn_y, btn_w, btn_h), + ButtonColor::Yellow, + ); let mut next_btn = Button::new( "Next", - board_x + (0.5 * board_width) + btn_x_offset, - btn_y, - btn_w, - btn_h, + Rect::new( + board_x + (0.5 * board_width) + btn_x_offset, + btn_y, + btn_w, + btn_h, + ), + ButtonColor::Green, ); next_btn.is_active = false; diff --git a/src/game/button.rs b/src/game/button.rs index 2a99f1a..a35092c 100644 --- a/src/game/button.rs +++ b/src/game/button.rs @@ -1,17 +1,43 @@ use macroquad::prelude::*; pub struct Button { - pub text: String, pub is_active: bool, + text: String, is_down: bool, is_clicked: bool, rect: Rect, shadow_width: f32, + color: ButtonColor, +} + +pub enum ButtonColor { + Grey, + Green, + Yellow, +} + +impl ButtonColor { + fn to_bg_color(&self) -> Color { + match self { + ButtonColor::Grey => Color::from_rgba(140, 140, 140, 200), + ButtonColor::Green => Color::from_rgba(112, 140, 141, 200), + ButtonColor::Yellow => Color::from_rgba(123, 70, 85, 200), + } + } + + fn to_shadow_color(&self) -> Color { + let bg_color = self.to_bg_color(); + Color::from_rgba( + (bg_color.r * 255.) as u8, + (bg_color.g * 255.) as u8, + (bg_color.b * 255.) as u8, + 100, + ) + } } impl Button { - pub fn new(text: &str, x: f32, y: f32, width: f32, height: f32) -> Self { - let rect = Rect::new(x, y, width, height); + pub fn new(text: &str, rect: Rect, color: ButtonColor) -> Self { Self { text: text.to_string(), is_down: false, @@ -19,6 +45,7 @@ impl Button { is_active: true, rect, shadow_width: 5.0, + color, } } @@ -37,7 +64,11 @@ impl Button { } fn draw_button(&self) { - let bg_color = Color::from_rgba(190, 190, 190, 255); + let bg_color = match self.is_active { + true => self.color.to_bg_color(), + false => ButtonColor::Grey.to_bg_color(), + }; + let button_draw_offset = self.get_button_draw_offset(); draw_rectangle( self.rect.x + button_draw_offset, @@ -59,7 +90,7 @@ impl Button { return; } - let color = Color::from_rgba(0, 0, 0, 100); + let color = self.color.to_shadow_color(); draw_rectangle( self.rect.x + self.rect.w, self.rect.y + self.shadow_width,