add a simple counter
This commit is contained in:
parent
cabacd32e2
commit
b2b8a6e0d2
4 changed files with 208 additions and 2 deletions
47
src/button.rs
Normal file
47
src/button.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
use sfml::{graphics::{CircleShape, Color, Drawable, Font, Shape, Text, Transformable}, system::Vector2f};
|
||||
|
||||
pub struct Button<'a> {
|
||||
background: CircleShape<'a>,
|
||||
text: Text<'a>,
|
||||
pub exec: Box<dyn Fn(u32) -> u32>
|
||||
}
|
||||
|
||||
impl<'a> Button<'a> {
|
||||
pub fn new(text: String, font: &'a Font, pos: Vector2f, exec: Box<dyn Fn(u32) -> u32>) -> Self {
|
||||
let mut background = CircleShape::new(20., 20);
|
||||
background.set_origin((20., 20.));
|
||||
background.set_position(pos);
|
||||
background.set_fill_color(Color::GREEN);
|
||||
|
||||
let mut text = Text::new(&text, font, 32);
|
||||
text.set_position(background.position());
|
||||
text.set_fill_color(Color::BLACK);
|
||||
let bounds = text.local_bounds();
|
||||
text.set_origin((
|
||||
bounds.left + bounds.width / 2.,
|
||||
bounds.top + bounds.height / 2.,
|
||||
));
|
||||
|
||||
Button {
|
||||
background,
|
||||
text,
|
||||
exec
|
||||
}
|
||||
}
|
||||
|
||||
pub fn contains(&self, x: i32, y: i32) -> bool {
|
||||
self.background.global_bounds().contains(Vector2f::new(x as f32, y as f32))
|
||||
}
|
||||
}
|
||||
|
||||
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>,
|
||||
) {
|
||||
|
||||
self.background.draw(target, states);
|
||||
self.text.draw(target, states);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue