fighting with the borrow checker. I don't think this child-parent updating works in rust

This commit is contained in:
2019-07-07 02:33:01 -07:00
parent 820b99ae4e
commit 35b4907d5d
3 changed files with 98 additions and 19 deletions

View File

@@ -0,0 +1,63 @@
use sfml::graphics::{CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Text, Font};
use sfml::window::{Event, Key, Style};
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 set_callback(&mut self, callback: &'s FnMut(i32)){
self.callback = Some(callback);
}
}
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!()
}
}