|
extern crate piston_window; |
|
#[macro_use] |
|
extern crate conrod; |
|
use piston_window::{PistonWindow, WindowSettings, Graphics, G2dTexture, TextureSettings, G2d}; |
|
use piston_window::texture::UpdateTexture; |
|
|
|
widget_ids! { |
|
pub struct Ids { |
|
master, |
|
header, |
|
body, |
|
left_column, |
|
middle_column, |
|
right_column, |
|
footer, |
|
} |
|
} |
|
fn gui(ref mut ui: conrod::UiCell, ids: &mut Ids) { |
|
use conrod::{widget, Widget, Colorable, color}; |
|
widget::Canvas::new().flow_down(&[ |
|
(ids.header, widget::Canvas::new().color(color::BLUE).pad_bottom(20.0)), |
|
(ids.body, widget::Canvas::new().length(300.0).flow_right(&[ |
|
(ids.left_column, widget::Canvas::new().color(color::LIGHT_ORANGE).pad(20.0)), |
|
(ids.middle_column, widget::Canvas::new().color(color::ORANGE)), |
|
(ids.right_column, widget::Canvas::new().color(color::DARK_ORANGE).pad(20.0)), |
|
])), |
|
(ids.footer, widget::Canvas::new().color(color::BLUE)), |
|
]).set(ids.master, ui); |
|
} |
|
|
|
pub fn main() { |
|
const WIDTH: u32 = 800; |
|
const HEIGHT: u32 = 600; |
|
|
|
let mut window: PistonWindow = WindowSettings::new("Goggles", [WIDTH, HEIGHT]) |
|
.exit_on_esc(true) |
|
.build() |
|
.unwrap_or_else(|e| { |
|
panic!("Failed to build PistonWindow: {}", e) |
|
}); |
|
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build(); |
|
// Create a texture to use for efficiently caching text on the GPU. |
|
let mut text_vertex_data = Vec::new(); |
|
let (mut glyph_cache, mut text_texture_cache) = { |
|
const SCALE_TOLERANCE: f32 = 0.1; |
|
const POSITION_TOLERANCE: f32 = 0.1; |
|
let cache = conrod::text::GlyphCache::new(WIDTH, |
|
HEIGHT, |
|
SCALE_TOLERANCE, |
|
POSITION_TOLERANCE); |
|
let buffer_len = WIDTH as usize * HEIGHT as usize; |
|
let init = vec![128; buffer_len]; |
|
let settings = TextureSettings::new(); |
|
let factory = &mut window.factory; |
|
let texture = G2dTexture::from_memory_alpha(factory, &init, WIDTH, HEIGHT, &settings) |
|
.unwrap(); |
|
(cache, texture) |
|
}; |
|
|
|
let mut ids = Ids::new(ui.widget_id_generator()); |
|
let mut image_map = conrod::image::Map::new(); |
|
|
|
while let Some(event) = window.next() { |
|
gui(ui.set_widgets(), &mut ids); |
|
window.draw_2d(&event, |context, graphics| { |
|
let cache_queued_glyphs = |graphics: &mut G2d, |
|
cache: &mut G2dTexture, |
|
rect: conrod::text::rt::Rect<u32>, |
|
data: &[u8]| { |
|
let offset = [rect.min.x, rect.min.y]; |
|
let size = [rect.width(), rect.height()]; |
|
let format = piston_window::texture::Format::Rgba8; |
|
let encoder = &mut graphics.encoder; |
|
text_vertex_data.clear(); |
|
text_vertex_data.extend(data.iter().flat_map(|&b| vec![255, 255, 255, b])); |
|
UpdateTexture::update(cache, encoder, format, &text_vertex_data[..], offset, size) |
|
.expect("failed to update texture") |
|
}; |
|
fn texture_from_image<T>(img: &T) -> &T { |
|
img |
|
} |
|
if let Some(primitives) = ui.draw_if_changed() { |
|
conrod::backend::piston::draw::primitives(primitives, |
|
context, |
|
graphics, |
|
&mut text_texture_cache, |
|
&mut glyph_cache, |
|
&image_map, |
|
cache_queued_glyphs, |
|
texture_from_image); |
|
} |
|
}); |
|
} |
|
} |
This comment has been minimized.
simon-whitehead commentedOct 16, 2017
Hi - did you ever fix this? I have this exact issue and I'm unsure how to resolve it.