From 76986010bac63d96e476d065d2c7d7869ac54ffc Mon Sep 17 00:00:00 2001 From: Raptorox <70806316+Raptorox@users.noreply.github.com> Date: Fri, 10 Oct 2025 14:20:14 +0200 Subject: [PATCH] remove button exec; add reset button --- src/button.rs | 16 +++++++--------- src/constants.rs | 9 ++++++--- src/counter.rs | 36 +++++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/button.rs b/src/button.rs index 57fc8b8..26c20a2 100644 --- a/src/button.rs +++ b/src/button.rs @@ -1,21 +1,20 @@ 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}; +use crate::constants::button::{DEFAULT_BG_COLOR, DEFAULT_TXT_COLOR, PRECISION}; pub struct Button<'a> { background: CircleShape<'a>, - text: Text<'a>, - pub exec: Box u32> + text: Text<'a> } impl<'a> Button<'a> { - 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)); + pub fn new(text: String, font: &'a Font, font_size: u32, pos: impl Into, 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); + let mut text = Text::new(&text, font, font_size); text.set_position(background.position()); text.set_fill_color(DEFAULT_TXT_COLOR); @@ -27,8 +26,7 @@ impl<'a> Button<'a> { Button { background, - text, - exec + text } } diff --git a/src/constants.rs b/src/constants.rs index e83d437..50cb56e 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -14,7 +14,8 @@ pub mod counter { pub const DEFAULT_OUTLINE_COLOR: Color = Color::BLUE; pub const DEFAULT_OUTLINE_THICKNESS: f32 = 10.; - pub const OFFSET_PERC: f32 = 0.35; + pub const HOFFSET_PERC: f32 = 0.35; + pub const VOFFSET_PERC: f32 = 0.30; pub const FONT_SIZE: u32 = 32; } @@ -22,11 +23,13 @@ pub mod counter { pub mod button { use sfml::graphics::Color; - pub const RADIUS: f32 = 20.; + pub const RADIUS_BIG: f32 = 20.; + pub const RADIUS_SMALL: f32 = 8.; 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; + pub const FONT_SIZE_BIG: u32 = 32; + pub const FONT_SIZE_SMALL: u32 = 16; } diff --git a/src/counter.rs b/src/counter.rs index 4f7f1a3..69ab2b7 100644 --- a/src/counter.rs +++ b/src/counter.rs @@ -3,7 +3,7 @@ use sfml::{ system::Vector2f, }; -use crate::{button::Button, constants::{common::WINDOW_SIZE_F, counter::{DEFAULT_BG_COLOR, DEFAULT_OUTLINE_COLOR, DEFAULT_OUTLINE_THICKNESS, FONT_SIZE, OFFSET_PERC, SIZE}}}; +use crate::{button::Button, constants::{button::{FONT_SIZE_BIG, FONT_SIZE_SMALL, RADIUS_BIG, RADIUS_SMALL}, common::WINDOW_SIZE_F, counter::{DEFAULT_BG_COLOR, DEFAULT_OUTLINE_COLOR, DEFAULT_OUTLINE_THICKNESS, FONT_SIZE, HOFFSET_PERC, SIZE, VOFFSET_PERC}}}; pub struct Counter<'a> { count: u32, @@ -39,28 +39,34 @@ impl<'a> Counter<'a> { let dec_button = Button::new( "-".to_string(), font, + FONT_SIZE_BIG, Vector2f::new( - background.position().x - OFFSET_PERC*background.size().x, + background.position().x - HOFFSET_PERC*background.size().x, background.position().y, ), - Box::new(|count| if count == 0 { 0 } else { count - 1 }), + RADIUS_BIG ); let inc_button = Button::new( "+".to_string(), font, + FONT_SIZE_BIG, Vector2f::new( - background.position().x + OFFSET_PERC*background.size().x, + background.position().x + HOFFSET_PERC*background.size().x, background.position().y, ), - Box::new(|count| if count == u32::MAX { 0 } else { count + 1 }), + RADIUS_BIG ); let reset_button = Button::new( "R".to_string(), font, - Vector2f::new(0., 0.), - Box::new(|_count| 0), + FONT_SIZE_SMALL, + Vector2f::new( + background.position().x, + background.position().y + (1. - VOFFSET_PERC)*background.size().y/2. + ), + RADIUS_SMALL ); Counter { @@ -74,17 +80,21 @@ impl<'a> Counter<'a> { } pub fn inc(&mut self) { - self.count = (self.inc_button.exec)(self.count); + if self.count < u32::MAX { + self.count += 1; + } self.update(); } pub fn dec(&mut self) { - self.count = (self.dec_button.exec)(self.count); + if self.count > 0 { + self.count -= 1; + } self.update(); } pub fn reset(&mut self) { - self.count = (self.reset_button.exec)(self.count); + self.count = 0; self.update(); } @@ -108,9 +118,12 @@ impl<'a> Counter<'a> { if self.inc_button.contains(m_pos) { self.inc(); } + if self.reset_button.contains(m_pos) { + self.reset(); + } } } - + impl<'d> Drawable for Counter<'d> { fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( &'a self, @@ -121,5 +134,6 @@ impl<'d> Drawable for Counter<'d> { self.count_str.draw(target, states); self.dec_button.draw(target, states); self.inc_button.draw(target, states); + self.reset_button.draw(target, states); } }