This commit is contained in:
Raptorox 2025-10-10 14:45:50 +02:00
parent 76986010ba
commit 19a75825fc
No known key found for this signature in database
GPG key ID: 8B3556FC3ED1F6D8
3 changed files with 58 additions and 33 deletions

View file

@ -1,14 +1,23 @@
use sfml::{graphics::{CircleShape, 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, PRECISION};
pub struct Button<'a> {
background: CircleShape<'a>,
text: Text<'a>
text: Text<'a>,
}
impl<'a> Button<'a> {
pub fn new(text: String, font: &'a Font, font_size: u32, pos: impl Into<Vector2f>, radius: f32) -> Self {
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);
@ -17,17 +26,14 @@ impl<'a> Button<'a> {
let mut text = Text::new(&text, font, font_size);
text.set_position(background.position());
text.set_fill_color(DEFAULT_TXT_COLOR);
let bounds = text.local_bounds();
text.set_origin((
bounds.left + bounds.width / 2.,
bounds.top + bounds.height / 2.,
));
Button {
background,
text
}
Button { background, text }
}
pub fn contains(&self, m_pos: Vector2f) -> bool {
@ -37,12 +43,11 @@ impl<'a> Button<'a> {
impl<'d> Drawable for Button<'d> {
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
&'a self,
target: &mut dyn sfml::graphics::RenderTarget,
states: &sfml::graphics::RenderStates<'texture, 'shader, 'shader_texture>,
) {
&'a self,
target: &mut dyn sfml::graphics::RenderTarget,
states: &sfml::graphics::RenderStates<'texture, 'shader, 'shader_texture>,
) {
self.background.draw(target, states);
self.text.draw(target, states);
}
}
}

View file

@ -3,7 +3,17 @@ use sfml::{
system::Vector2f,
};
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}}};
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,
@ -20,7 +30,7 @@ impl<'a> Counter<'a> {
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.));
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.,
@ -29,7 +39,7 @@ impl<'a> Counter<'a> {
let mut count_str = Text::new(&count.to_string(), font, FONT_SIZE);
count_str.set_fill_color(Color::BLACK);
count_str.set_position((WINDOW_SIZE_F.0/2., WINDOW_SIZE_F.1/2.));
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.,
@ -41,10 +51,10 @@ impl<'a> Counter<'a> {
font,
FONT_SIZE_BIG,
Vector2f::new(
background.position().x - HOFFSET_PERC*background.size().x,
background.position().x - HOFFSET_PERC * background.size().x,
background.position().y,
),
RADIUS_BIG
RADIUS_BIG,
);
let inc_button = Button::new(
@ -52,10 +62,10 @@ impl<'a> Counter<'a> {
font,
FONT_SIZE_BIG,
Vector2f::new(
background.position().x + HOFFSET_PERC*background.size().x,
background.position().x + HOFFSET_PERC * background.size().x,
background.position().y,
),
RADIUS_BIG
RADIUS_BIG,
);
let reset_button = Button::new(
@ -64,9 +74,9 @@ impl<'a> Counter<'a> {
FONT_SIZE_SMALL,
Vector2f::new(
background.position().x,
background.position().y + (1. - VOFFSET_PERC)*background.size().y/2.
background.position().y + (1. - VOFFSET_PERC) * background.size().y / 2.,
),
RADIUS_SMALL
RADIUS_SMALL,
);
Counter {
@ -123,7 +133,7 @@ impl<'a> Counter<'a> {
}
}
}
impl<'d> Drawable for Counter<'d> {
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
&'a self,

View file

@ -1,18 +1,26 @@
use sfml::{graphics::{Color, Font, RenderTarget, RenderWindow}, window::{mouse, Event, Key, Style}, SfResult};
use sfml::{
SfResult,
graphics::{Color, Font, RenderTarget, RenderWindow},
window::{Event, Key, Style, mouse},
};
mod counter;
mod button;
mod constants;
mod counter;
use counter::Counter;
use crate::constants::common::{FPS, WINDOW_SIZE};
use counter::Counter;
fn main() -> SfResult<()>{
fn main() -> SfResult<()> {
let font = Font::from_file("res/font.ttf").unwrap();
let mut c1 = Counter::new(0, &font);
let mut window = RenderWindow::new(WINDOW_SIZE, "Multi Counter", Style::CLOSE, &Default::default())?;
let mut window = RenderWindow::new(
WINDOW_SIZE,
"Multi Counter",
Style::CLOSE,
&Default::default(),
)?;
window.set_framerate_limit(FPS);
while window.is_open() {
@ -25,8 +33,10 @@ fn main() -> SfResult<()>{
Key::Space => c1.reset(),
_ => {}
},
Event::MouseButtonPressed { button, x, y } => if button == mouse::Button::Left {
c1.handle_mouse((x as f32, y as f32));
Event::MouseButtonPressed { button, x, y } => {
if button == mouse::Button::Left {
c1.handle_mouse((x as f32, y as f32));
}
}
_ => {}
}
@ -36,6 +46,6 @@ fn main() -> SfResult<()>{
window.draw(&c1);
window.display();
}
Ok(())
}