48 lines
No EOL
1.5 KiB
Rust
48 lines
No EOL
1.5 KiB
Rust
use sfml::{graphics::{CircleShape, Drawable, Font, Shape, Text, Transformable}, system::Vector2f};
|
|
|
|
use crate::constants::button::{DEFAULT_BG_COLOR, DEFAULT_TXT_COLOR, PRECISION};
|
|
|
|
pub struct Button<'a> {
|
|
background: CircleShape<'a>,
|
|
text: Text<'a>
|
|
}
|
|
|
|
impl<'a> Button<'a> {
|
|
pub fn new(text: String, font: &'a Font, font_size: u32, pos: impl Into<Vector2f>, radius: f32) -> Self {
|
|
let mut background = CircleShape::new(radius, PRECISION);
|
|
background.set_origin((radius, radius));
|
|
background.set_position(pos);
|
|
background.set_fill_color(DEFAULT_BG_COLOR);
|
|
|
|
let mut text = Text::new(&text, font, font_size);
|
|
text.set_position(background.position());
|
|
text.set_fill_color(DEFAULT_TXT_COLOR);
|
|
|
|
let bounds = text.local_bounds();
|
|
text.set_origin((
|
|
bounds.left + bounds.width / 2.,
|
|
bounds.top + bounds.height / 2.,
|
|
));
|
|
|
|
Button {
|
|
background,
|
|
text
|
|
}
|
|
}
|
|
|
|
pub fn contains(&self, m_pos: Vector2f) -> bool {
|
|
self.background.global_bounds().contains(m_pos)
|
|
}
|
|
}
|
|
|
|
impl<'d> Drawable for Button<'d> {
|
|
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
|
|
&'a self,
|
|
target: &mut dyn sfml::graphics::RenderTarget,
|
|
states: &sfml::graphics::RenderStates<'texture, 'shader, 'shader_texture>,
|
|
) {
|
|
|
|
self.background.draw(target, states);
|
|
self.text.draw(target, states);
|
|
}
|
|
}
|