From 35b4907d5dd1896555ed314b08ab949efc961086 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 7 Jul 2019 02:33:01 -0700 Subject: [PATCH] fighting with the borrow checker. I don't think this child-parent updating works in rust --- src/button.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 11 ++++++--- src/slider.rs | 43 ++++++++++++++++++++++------------- 3 files changed, 98 insertions(+), 19 deletions(-) diff --git a/src/button.rs b/src/button.rs index e69de29b..634e012c 100644 --- a/src/button.rs +++ b/src/button.rs @@ -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!() + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 37a08741..f65e8763 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,7 @@ mod timer; mod input; mod vkprocessor; mod util; +mod button; // What next? @@ -54,6 +55,8 @@ mod util; fn main() { + let font = Font::from_file("resources/fonts/sansation.ttf").unwrap(); + let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap(); let mut processor = vkprocessor::VkProcessor::new(&instance); processor.compile_kernel(String::from("simple-edge.compute")); @@ -72,8 +75,6 @@ fn main() { let mut timer = Timer::new(); let mut input = Input::new(); - let font = Font::from_file("resources/fonts/sansation.ttf").unwrap(); - let xy = processor.xy; let mut bg_texture = Texture::new(xy.0, xy.1).unwrap(); bg_texture.update_from_pixels(processor.read_image().as_slice(), xy.0, xy.1, 0, 0); @@ -81,10 +82,12 @@ fn main() { let mut background_sprite = Sprite::with_texture(&bg_texture); background_sprite.set_position((0., 0.)); - let mut slider = Slider::new(40.0, None); + let mut slider = Slider::new(Vector2f::new(40.0, 40.0), None, &font); let mut selected_colors = Vec::new(); + let mut button = button::Button::new(Vector2f::new(40.0,40.0), Vector2f::new(100.0,100.0), &font); + button.set_text("Text"); let step_size: f32 = 0.005; let mut elapsed_time: f32; @@ -165,6 +168,8 @@ fn main() { window.draw(i); } + window.draw(&button); + window.display(); } } diff --git a/src/slider.rs b/src/slider.rs index 12fc4706..210047bf 100644 --- a/src/slider.rs +++ b/src/slider.rs @@ -1,42 +1,51 @@ use sfml::window::{Key, Event}; -use sfml::graphics::{ - CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, - Transformable, -}; +use sfml::graphics::{CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Font}; +use crate::button::Button; +use sfml::system::Vector2f; pub struct Slider<'a> { value: f32, - left_button: RectangleShape<'a>, - right_button: RectangleShape<'a>, + left_button: Button<'a>, + right_button: Button<'a>, + slider_button: Button<'a>, + rail: RectangleShape<'a>, } impl<'a> Slider<'a> { - pub fn new(y_position: f32, value: Option) -> Self { + pub fn new(position: Vector2f, value: Option, font: &'a Font) -> Self { let val = match value { Some(v) => v, None => 0.0, }; + let left_button_position = position + Vector2f::new(0.0,0.0); + let right_button_position = position + Vector2f::new(150.0,0.0); + let slider_position = position + Vector2f::new(val,0.0); + let mut slider = Slider { value: val, - left_button: RectangleShape::new(), - right_button: RectangleShape::new(), + left_button: Button::new(Vector2f::new(30.0, 50.0), left_button_position, font), + right_button: Button::new(Vector2f::new(30.0, 50.0), right_button_position, font), + slider_button: Button::new(Vector2f::new(30.0, 50.0), slider_position, font), + rail: RectangleShape::with_size(Vector2f::new(150.0, 2.0)), }; - slider.left_button.set_position((0.0,y_position)); - slider.right_button.set_position((50.0,y_position)); + slider.left_button.set_callback(&|a| slider.set_value(a)); - slider.left_button.set_size((20.0, 30.0)); - slider.right_button.set_size((20.0, 30.0)); + slider.left_button.set_text("<"); + slider.right_button.set_text(">"); - slider.left_button.set_fill_color(&sfml::graphics::Color::GREEN); - slider.right_button.set_fill_color(&sfml::graphics::Color::GREEN); + slider.rail.set_position(position); slider } + pub fn set_value(&mut self, inp: i32){ + self.value += 1.0; + } + } impl<'s> Drawable for Slider<'s> { @@ -46,6 +55,8 @@ impl<'s> Drawable for Slider<'s> { _: RenderStates<'texture, 'shader, 'shader_texture>, ) { render_target.draw(&self.left_button); - render_target.draw(&self.right_button) + render_target.draw(&self.right_button); + render_target.draw(&self.slider_button); + render_target.draw(&self.rail); } } \ No newline at end of file