use crate::backend_sdl2::Sdl2Backend;
use crate::toolkit::theme::Theme;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
pub struct Button {
text: String,
x: i32,
y: i32,
width: i32,
height: i32,
hovered: bool,
}
impl Button {
pub fn new(text: &str) -> Self {
Self {
text: text.to_string(),
x: 0,
y: 0,
width: 150,
height: 40,
hovered: false,
}
}
pub fn position(mut self, x: i32, y: i32) -> Self {
self.x = x;
self.y = y;
self
}
pub fn size(mut self, width: i32, height: i32) -> Self {
self.width = width;
self.height = height;
self
}
pub fn render(&self, backend: &mut Sdl2Backend, theme: &Theme) {
let color = if self.hovered {
theme.button_hover
} else {
theme.button_normal
};
backend.canvas.set_draw_color(color);
let rect = Rect::new(self.x, self.y, self.width as u32, self.height as u32);
let _ = backend.canvas.fill_rect(rect);
backend.canvas.set_draw_color(theme.button_border);
let _ = backend.canvas.draw_rect(rect);
let text_width = self.text.len() as i32 * 8;
let text_x = self.x + (self.width - text_width) / 2;
let text_y = self.y + (self.height - 16) / 2;
backend.canvas.set_draw_color(Color::RGB(255, 255, 255));
let text_rect = Rect::new(text_x, text_y, text_width as u32, 16);
let _ = backend.canvas.fill_rect(text_rect);
}
pub fn is_clicked(&self, x: i32, y: i32) -> bool {
x >= self.x && x <= self.x + self.width && y >= self.y && y <= self.y + self.height
}
}