extern crate conrod_core;
extern crate conrod_example_shared;
extern crate conrod_piston;
extern crate find_folder;
extern crate piston_window;
use self::piston_window::texture::UpdateTexture;
use self::piston_window::OpenGL;
use self::piston_window::{Flip, G2d, G2dTexture, Texture, TextureSettings};
use self::piston_window::{PistonWindow, UpdateEvent, Window, WindowSettings};
pub fn main() {
const WIDTH: u32 = conrod_example_shared::WIN_W;
const HEIGHT: u32 = conrod_example_shared::WIN_H;
let mut window: PistonWindow =
WindowSettings::new("All Widgets - Piston Backend", [WIDTH, HEIGHT])
.graphics_api(OpenGL::V3_2) .samples(4)
.exit_on_esc(true)
.vsync(true)
.build()
.unwrap();
let mut ui = conrod_core::UiBuilder::new([WIDTH as f64, HEIGHT as f64])
.theme(conrod_example_shared::theme())
.build();
let assets = find_folder::Search::KidsThenParents(3, 5)
.for_folder("assets")
.unwrap();
let font_path = assets.join("fonts/NotoSans/NotoSans-Regular.ttf");
ui.fonts.insert_from_file(font_path).unwrap();
let mut texture_context = window.create_texture_context();
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_core::text::GlyphCache::builder()
.dimensions(WIDTH, HEIGHT)
.scale_tolerance(SCALE_TOLERANCE)
.position_tolerance(POSITION_TOLERANCE)
.build();
let buffer_len = WIDTH as usize * HEIGHT as usize;
let init = vec![128; buffer_len];
let settings = TextureSettings::new();
let texture =
G2dTexture::from_memory_alpha(&mut texture_context, &init, WIDTH, HEIGHT, &settings)
.unwrap();
(cache, texture)
};
let ids = conrod_example_shared::Ids::new(ui.widget_id_generator());
let rust_logo: G2dTexture = {
let assets = find_folder::Search::ParentsThenKids(5, 3)
.for_folder("assets")
.unwrap();
let path = assets.join("images/rust.png");
let settings = TextureSettings::new();
Texture::from_path(&mut texture_context, &path, Flip::None, &settings).unwrap()
};
let mut image_map = conrod_core::image::Map::new();
let rust_logo = image_map.insert(rust_logo);
let mut app = conrod_example_shared::DemoApp::new(rust_logo);
while let Some(event) = window.next() {
let size = window.size();
let (win_w, win_h) = (
size.width as conrod_core::Scalar,
size.height as conrod_core::Scalar,
);
if let Some(e) = conrod_piston::event::convert(event.clone(), win_w, win_h) {
ui.handle_event(e);
}
event.update(|_| {
let mut ui = ui.set_widgets();
conrod_example_shared::gui(&mut ui, &ids, &mut app);
});
window.draw_2d(&event, |context, graphics, device| {
if let Some(primitives) = ui.draw_if_changed() {
let cache_queued_glyphs = |_graphics: &mut G2d,
cache: &mut G2dTexture,
rect: conrod_core::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;
text_vertex_data.clear();
text_vertex_data.extend(data.iter().flat_map(|&b| vec![255, 255, 255, b]));
UpdateTexture::update(
cache,
&mut texture_context,
format,
&text_vertex_data[..],
offset,
size,
)
.expect("failed to update texture")
};
fn texture_from_image<T>(img: &T) -> &T {
img
}
conrod_piston::draw::primitives(
primitives,
context,
graphics,
&mut text_texture_cache,
&mut glyph_cache,
&image_map,
cache_queued_glyphs,
texture_from_image,
);
texture_context.encoder.flush(device);
}
});
}
}