diff --git a/src/button.rs b/src/button.rs index 1303903..57fc8b8 100644 --- a/src/button.rs +++ b/src/button.rs @@ -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 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, exec: Box 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) } } diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..e83d437 --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,32 @@ +pub mod common { + pub const WINDOW_SIZE: (u32, u32) = (800, 600); + pub const WINDOW_SIZE_F: (f32, f32) = (WINDOW_SIZE.0 as f32, WINDOW_SIZE.1 as f32); + + pub const FPS: u32 = 60; +} + +pub mod counter { + use sfml::{graphics::Color, system::Vector2f}; + + pub const SIZE: Vector2f = Vector2f::new(200., 70.); + + pub const DEFAULT_BG_COLOR: Color = Color::YELLOW; + pub const DEFAULT_OUTLINE_COLOR: Color = Color::BLUE; + pub const DEFAULT_OUTLINE_THICKNESS: f32 = 10.; + + pub const OFFSET_PERC: f32 = 0.35; + + pub const FONT_SIZE: u32 = 32; +} + +pub mod button { + use sfml::graphics::Color; + + pub const RADIUS: f32 = 20.; + pub const PRECISION: usize = 20; + + pub const DEFAULT_BG_COLOR: Color = Color::GREEN; + pub const DEFAULT_TXT_COLOR: Color = Color::BLACK; + + pub const FONT_SIZE: u32 = 32; +} diff --git a/src/counter.rs b/src/counter.rs index 77c4e4d..4f7f1a3 100644 --- a/src/counter.rs +++ b/src/counter.rs @@ -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) { + 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(); } } diff --git a/src/main.rs b/src/main.rs index 67a540e..a2d0fd7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,19 @@ use sfml::{graphics::{Color, Font, RenderTarget, RenderWindow}, window::{mouse, Event, Key, Style}, SfResult}; mod counter; -use counter::Counter; - mod button; +mod constants; + +use counter::Counter; +use crate::constants::common::{FPS, WINDOW_SIZE}; + fn main() -> SfResult<()>{ let font = Font::from_file("res/font.ttf").unwrap(); let mut c1 = Counter::new(0, &font); - let mut window = RenderWindow::new((800, 600), "Multi Counter", Style::CLOSE, &Default::default())?; - window.set_framerate_limit(60); + let mut window = RenderWindow::new(WINDOW_SIZE, "Multi Counter", Style::CLOSE, &Default::default())?; + window.set_framerate_limit(FPS); while window.is_open() { while let Some(event) = window.poll_event() { @@ -23,7 +26,7 @@ fn main() -> SfResult<()>{ _ => {} }, Event::MouseButtonPressed { button, x, y } => if button == mouse::Button::Left { - c1.handle_mouse(x, y); + c1.handle_mouse((x as f32, y as f32)); } _ => {} }