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

master
mitchellhansen 6 years ago
parent 820b99ae4e
commit 35b4907d5d

@ -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!()
}
}

@ -46,6 +46,7 @@ mod timer;
mod input; mod input;
mod vkprocessor; mod vkprocessor;
mod util; mod util;
mod button;
// What next? // What next?
@ -54,6 +55,8 @@ mod util;
fn main() { fn main() {
let font = Font::from_file("resources/fonts/sansation.ttf").unwrap();
let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap(); let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap();
let mut processor = vkprocessor::VkProcessor::new(&instance); let mut processor = vkprocessor::VkProcessor::new(&instance);
processor.compile_kernel(String::from("simple-edge.compute")); processor.compile_kernel(String::from("simple-edge.compute"));
@ -72,8 +75,6 @@ fn main() {
let mut timer = Timer::new(); let mut timer = Timer::new();
let mut input = Input::new(); let mut input = Input::new();
let font = Font::from_file("resources/fonts/sansation.ttf").unwrap();
let xy = processor.xy; let xy = processor.xy;
let mut bg_texture = Texture::new(xy.0, xy.1).unwrap(); 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); 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); let mut background_sprite = Sprite::with_texture(&bg_texture);
background_sprite.set_position((0., 0.)); 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 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 step_size: f32 = 0.005;
let mut elapsed_time: f32; let mut elapsed_time: f32;
@ -165,6 +168,8 @@ fn main() {
window.draw(i); window.draw(i);
} }
window.draw(&button);
window.display(); window.display();
} }
} }

@ -1,42 +1,51 @@
use sfml::window::{Key, Event}; use sfml::window::{Key, Event};
use sfml::graphics::{ use sfml::graphics::{CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, Transformable, Font};
CircleShape, Color, Drawable, RectangleShape, RenderStates, RenderTarget, RenderWindow, Shape, use crate::button::Button;
Transformable, use sfml::system::Vector2f;
};
pub struct Slider<'a> { pub struct Slider<'a> {
value: f32, value: f32,
left_button: RectangleShape<'a>, left_button: Button<'a>,
right_button: RectangleShape<'a>, right_button: Button<'a>,
slider_button: Button<'a>,
rail: RectangleShape<'a>,
} }
impl<'a> Slider<'a> { impl<'a> Slider<'a> {
pub fn new(y_position: f32, value: Option<f32>) -> Self { pub fn new(position: Vector2f, value: Option<f32>, font: &'a Font) -> Self {
let val = match value { let val = match value {
Some(v) => v, Some(v) => v,
None => 0.0, 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 { let mut slider = Slider {
value: val, value: val,
left_button: RectangleShape::new(), left_button: Button::new(Vector2f::new(30.0, 50.0), left_button_position, font),
right_button: RectangleShape::new(), 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.left_button.set_callback(&|a| slider.set_value(a));
slider.right_button.set_position((50.0,y_position));
slider.left_button.set_size((20.0, 30.0)); slider.left_button.set_text("<");
slider.right_button.set_size((20.0, 30.0)); slider.right_button.set_text(">");
slider.left_button.set_fill_color(&sfml::graphics::Color::GREEN); slider.rail.set_position(position);
slider.right_button.set_fill_color(&sfml::graphics::Color::GREEN);
slider slider
} }
pub fn set_value(&mut self, inp: i32){
self.value += 1.0;
}
} }
impl<'s> Drawable for Slider<'s> { impl<'s> Drawable for Slider<'s> {
@ -46,6 +55,8 @@ impl<'s> Drawable for Slider<'s> {
_: RenderStates<'texture, 'shader, 'shader_texture>, _: RenderStates<'texture, 'shader, 'shader_texture>,
) { ) {
render_target.draw(&self.left_button); 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);
} }
} }
Loading…
Cancel
Save