move to constants
This commit is contained in:
parent
b2b8a6e0d2
commit
c0042d7680
4 changed files with 66 additions and 27 deletions
|
|
@ -3,7 +3,7 @@ use sfml::{
|
|||
system::Vector2f,
|
||||
};
|
||||
|
||||
use crate::button::Button;
|
||||
use crate::{button::Button, constants::{common::WINDOW_SIZE_F, counter::{DEFAULT_BG_COLOR, DEFAULT_OUTLINE_COLOR, DEFAULT_OUTLINE_THICKNESS, FONT_SIZE, OFFSET_PERC, SIZE}}};
|
||||
|
||||
pub struct Counter<'a> {
|
||||
count: u32,
|
||||
|
|
@ -16,20 +16,20 @@ pub struct Counter<'a> {
|
|||
|
||||
impl<'a> Counter<'a> {
|
||||
pub fn new(count: u32, font: &'a Font) -> Self {
|
||||
let mut background = RectangleShape::with_size(Vector2f::new(200., 70.));
|
||||
background.set_fill_color(Color::YELLOW);
|
||||
background.set_outline_color(Color::BLUE);
|
||||
background.set_outline_thickness(10.);
|
||||
background.set_position((400., 300.));
|
||||
let mut background = RectangleShape::with_size(SIZE);
|
||||
background.set_fill_color(DEFAULT_BG_COLOR);
|
||||
background.set_outline_color(DEFAULT_OUTLINE_COLOR);
|
||||
background.set_outline_thickness(DEFAULT_OUTLINE_THICKNESS);
|
||||
background.set_position((WINDOW_SIZE_F.0/2., WINDOW_SIZE_F.1/2.));
|
||||
let bounds = background.local_bounds();
|
||||
background.set_origin((
|
||||
bounds.left + bounds.width / 2.,
|
||||
bounds.top + bounds.height / 2.,
|
||||
));
|
||||
|
||||
let mut count_str = Text::new(&count.to_string(), font, 32);
|
||||
let mut count_str = Text::new(&count.to_string(), font, FONT_SIZE);
|
||||
count_str.set_fill_color(Color::BLACK);
|
||||
count_str.set_position((400., 300.));
|
||||
count_str.set_position((WINDOW_SIZE_F.0/2., WINDOW_SIZE_F.1/2.));
|
||||
let bounds = count_str.local_bounds();
|
||||
count_str.set_origin((
|
||||
bounds.left + bounds.width / 2.,
|
||||
|
|
@ -40,7 +40,7 @@ impl<'a> Counter<'a> {
|
|||
"-".to_string(),
|
||||
font,
|
||||
Vector2f::new(
|
||||
background.position().x - 70.,
|
||||
background.position().x - OFFSET_PERC*background.size().x,
|
||||
background.position().y,
|
||||
),
|
||||
Box::new(|count| if count == 0 { 0 } else { count - 1 }),
|
||||
|
|
@ -50,7 +50,7 @@ impl<'a> Counter<'a> {
|
|||
"+".to_string(),
|
||||
font,
|
||||
Vector2f::new(
|
||||
background.position().x + 70.,
|
||||
background.position().x + OFFSET_PERC*background.size().x,
|
||||
background.position().y,
|
||||
),
|
||||
Box::new(|count| if count == u32::MAX { 0 } else { count + 1 }),
|
||||
|
|
@ -100,11 +100,12 @@ impl<'a> Counter<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn handle_mouse(&mut self, x: i32, y: i32) {
|
||||
if self.dec_button.contains(x, y) {
|
||||
pub fn handle_mouse(&mut self, m_pos: impl Into<Vector2f>) {
|
||||
let m_pos = m_pos.into();
|
||||
if self.dec_button.contains(m_pos) {
|
||||
self.dec();
|
||||
}
|
||||
if self.inc_button.contains(x, y) {
|
||||
if self.inc_button.contains(m_pos) {
|
||||
self.inc();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue