remove button exec; add reset button
This commit is contained in:
parent
c0042d7680
commit
76986010ba
3 changed files with 38 additions and 23 deletions
|
|
@ -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<dyn Fn(u32) -> u32>
|
||||
text: Text<'a>
|
||||
}
|
||||
|
||||
impl<'a> Button<'a> {
|
||||
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));
|
||||
pub fn new(text: String, font: &'a Font, font_size: u32, pos: impl Into<Vector2f>, 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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue