Skip to content

Instantly share code, notes, and snippets.

@dpzmick
Created April 22, 2017 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpzmick/66536505f08ac0d26736938f44633f34 to your computer and use it in GitHub Desktop.
Save dpzmick/66536505f08ac0d26736938f44633f34 to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate conrod;
use conrod::Borderable;
use conrod::Positionable;
use conrod::Widget;
use conrod::backend::glium::glium::DisplayBuild;
use conrod::backend::glium::glium::Surface;
use conrod::backend::glium::glium;
use conrod::glium::backend::glutin_backend::GlutinFacade;
use conrod::widget::BorderedRectangle;
use conrod::widget::Line;
use std::collections::HashMap;
const WIDTH: u32 = 3000;
const HEIGHT: u32 = 2000;
widget_ids! {
struct RectIds {
r1, r2, r3, r4,
r1r2, r1r3, r1r4,
r2r3
}
}
struct Example {
cons: Vec<(conrod::widget::Id, conrod::widget::Id, conrod::widget::Id)>,
rect_locs: HashMap<usize, conrod::Point>,
}
impl Example {
fn new(ids: &RectIds) -> Self
{
let mut cons = Vec::new();
cons.push((ids.r1, ids.r2, ids.r1r2));
cons.push((ids.r1, ids.r3, ids.r1r3));
cons.push((ids.r1, ids.r4, ids.r1r4));
cons.push((ids.r2, ids.r3, ids.r2r3));
let mut locs = HashMap::new();
for r in [ids.r1, ids.r2, ids.r3, ids.r4].iter() {
locs.insert(r.index(), [0.0, 0.0]);
}
Self {
cons: cons,
rect_locs: locs,
}
}
fn draw(&mut self, ui: &mut conrod::UiCell)
{
for (k, v) in self.rect_locs.iter_mut() {
BorderedRectangle::new([50.0, 50.0])
.xy(*v)
.border_color(conrod::color::LIGHT_RED)
.set(conrod::widget::Id::new(*k), ui);
let mut last = None;
for ev in ui.widget_input(conrod::widget::Id::new(*k)).events() {
match ev {
conrod::event::Widget::Drag(ev) if ev.button == conrod::input::MouseButton::Left => {
last = Some(ev);
},
_ => ()
}
}
if let Some(ev) = last {
v[0] += ev.to[0];
v[1] += ev.to[1];
}
}
for &(r1, r2, line) in self.cons.iter() {
let l1 = self.rect_locs[&r1.index()];
let l2 = self.rect_locs[&r2.index()];
Line::new(l1, l2)
.thickness(5.0)
.set(line, ui);
}
}
}
pub struct MinExample {
display: GlutinFacade,
ui: conrod::Ui,
renderer: conrod::backend::glium::Renderer,
image_map: conrod::image::Map<glium::texture::Texture2d>,
ui_needs_update: bool,
example: Example,
}
impl MinExample {
fn draw_ui(&mut self)
{
self.example.draw(&mut self.ui.set_widgets());
}
}
impl MinExample {
pub fn new() -> Self
{
let display = glium::glutin::WindowBuilder::new()
.with_vsync()
.with_dimensions(WIDTH, HEIGHT)
.with_title("Hello Conrod!")
.build_glium()
.unwrap();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();
let renderer = conrod::backend::glium::Renderer::new(&display).unwrap();
let image_map = conrod::image::Map::<glium::texture::Texture2d>::new();
let ids = RectIds::new(ui.widget_id_generator());
let example = Example::new(&ids);
Self {
display,
ui,
renderer,
image_map,
ui_needs_update: false,
example: example,
}
}
pub fn event_loop(&mut self) -> bool
{
let mut events: Vec<_> = self.display.poll_events().collect();
if events.is_empty() && !self.ui_needs_update {
events.extend(self.display.wait_events().next());
}
self.ui_needs_update = false;
// Handle all events.
for event in events {
// Use the `winit` backend feature to convert the winit event to a conrod one.
if let Some(event) = conrod::backend::winit::convert(event.clone(), &self.display) {
self.ui.handle_event(event);
self.ui_needs_update = true;
}
match event {
glium::glutin::Event::Closed => return false,
_ => {},
}
}
self.draw_ui();
if let Some(primitives) = self.ui.draw_if_changed() {
self.renderer
.fill(&self.display, primitives, &self.image_map);
let mut target = self.display.draw();
target.clear_color(0.0, 0.0, 0.0, 1.0);
self.renderer
.draw(&self.display, &mut target, &self.image_map)
.unwrap();
target.finish().unwrap();
}
true
}
}
fn main()
{
let mut gui = MinExample::new();
'main: loop {
let sixteen_ms = std::time::Duration::from_millis(16);
std::thread::sleep(sixteen_ms);
while gui.event_loop() { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment