move to constants
This commit is contained in:
parent
b2b8a6e0d2
commit
c0042d7680
4 changed files with 66 additions and 27 deletions
|
|
@ -1,4 +1,6 @@
|
|||
use sfml::{graphics::{CircleShape, Color, Drawable, Font, Shape, Text, Transformable}, system::Vector2f};
|
||||
use sfml::{graphics::{CircleShape, Drawable, Font, Shape, Text, Transformable}, system::Vector2f};
|
||||
|
||||
use crate::constants::button::{DEFAULT_BG_COLOR, DEFAULT_TXT_COLOR, FONT_SIZE, PRECISION, RADIUS};
|
||||
|
||||
pub struct Button<'a> {
|
||||
background: CircleShape<'a>,
|
||||
|
|
@ -7,15 +9,16 @@ pub struct Button<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Button<'a> {
|
||||
pub fn new(text: String, font: &'a Font, pos: Vector2f, exec: Box<dyn Fn(u32) -> u32>) -> Self {
|
||||
let mut background = CircleShape::new(20., 20);
|
||||
background.set_origin((20., 20.));
|
||||
pub fn new(text: String, font: &'a Font, pos: impl Into<Vector2f>, exec: Box<dyn Fn(u32) -> u32>) -> Self {
|
||||
let mut background = CircleShape::new(RADIUS, PRECISION);
|
||||
background.set_origin((RADIUS, RADIUS));
|
||||
background.set_position(pos);
|
||||
background.set_fill_color(Color::GREEN);
|
||||
background.set_fill_color(DEFAULT_BG_COLOR);
|
||||
|
||||
let mut text = Text::new(&text, font, 32);
|
||||
let mut text = Text::new(&text, font, FONT_SIZE);
|
||||
text.set_position(background.position());
|
||||
text.set_fill_color(Color::BLACK);
|
||||
text.set_fill_color(DEFAULT_TXT_COLOR);
|
||||
|
||||
let bounds = text.local_bounds();
|
||||
text.set_origin((
|
||||
bounds.left + bounds.width / 2.,
|
||||
|
|
@ -29,8 +32,8 @@ impl<'a> Button<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn contains(&self, x: i32, y: i32) -> bool {
|
||||
self.background.global_bounds().contains(Vector2f::new(x as f32, y as f32))
|
||||
pub fn contains(&self, m_pos: Vector2f) -> bool {
|
||||
self.background.global_bounds().contains(m_pos)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue