You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/src/button.rs

63 lines
1.6 KiB

use sfml::graphics::{Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Text, Font};
use sfml::system::Vector2f;
trait Clickable {
fn name(&self) -> &'static str;
fn is_clicked(&self, mouse_position: Vector2f) -> &'static str;
}
pub struct Button<'s> {
body: RectangleShape<'s>,
text: Text<'s>,
font: &'s Font,
callback: Option<&'s FnMut(i32)>,
}
impl<'s> Button<'s> {
pub fn new(size: Vector2f, pos: Vector2f, font: &'s Font) -> Self {
let mut body = RectangleShape::with_size(size);
body.set_position(pos);
let mut text = Text::new("", font, 13);
text.set_fill_color(&Color::BLUE);
text.set_position(pos);
Self { body , text, font, callback: None }
}
pub fn set_text(&mut self, text: &str) {
self.text.set_string(text);
}
pub fn set_position(&mut self, position: Vector2f) {
self.body.set_position(position);
self.text.set_position(position);
}
pub fn is_within(point: Vector2f, button: &Button) -> bool {
button.body.local_bounds().contains(point)
}
}
impl<'s> Drawable for Button<'s> {
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
&'a self,
render_target: &mut RenderTarget,
_: RenderStates<'texture, 'shader, 'shader_texture>,
) {
render_target.draw(&self.body);
render_target.draw(&self.text);
}
}
impl<'s> Clickable for Button<'s> {
fn name(&self) -> &'static str {
unimplemented!()
}
fn is_clicked(&self, mouse_position: Vector2f) -> &'static str {
unimplemented!()
}
}