remove button exec; add reset button

This commit is contained in:
Raptorox 2025-10-10 14:20:14 +02:00
parent c0042d7680
commit 76986010ba
No known key found for this signature in database
GPG key ID: 8B3556FC3ED1F6D8
3 changed files with 38 additions and 23 deletions

View file

@ -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);
}
}