//! An example demonstrating all widgets in a long, vertically scrollable window. 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; // Construct the window. let mut window: PistonWindow = WindowSettings::new("All Widgets - Piston Backend", [WIDTH, HEIGHT]) .graphics_api(OpenGL::V3_2) // If not working, try `OpenGL::V2_1`. .samples(4) .exit_on_esc(true) .vsync(true) .build() .unwrap(); // construct our `Ui`. let mut ui = conrod_core::UiBuilder::new([WIDTH as f64, HEIGHT as f64]) .theme(conrod_example_shared::theme()) .build(); // Add a `Font` to the `Ui`'s `font::Map` from file. 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(); // Create texture context to perform operations on textures. let mut texture_context = window.create_texture_context(); // 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_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) }; // Instantiate the generated list of widget identifiers. let ids = conrod_example_shared::Ids::new(ui.widget_id_generator()); // Load the rust logo from file to a piston_window texture. 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() }; // Create our `conrod_core::image::Map` which describes each of our widget->image mappings. let mut image_map = conrod_core::image::Map::new(); let rust_logo = image_map.insert(rust_logo); // A demonstration of some state that we'd like to control with the App. let mut app = conrod_example_shared::DemoApp::new(rust_logo); // Poll events from the window. while let Some(event) = window.next() { // Convert the src event to a conrod event. 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() { // A function used for caching glyphs to the texture cache. let cache_queued_glyphs = |_graphics: &mut G2d, cache: &mut G2dTexture, rect: conrod_core::text::rt::Rect, 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") }; // Specify how to get the drawable texture from the image. In this case, the image // *is* the texture. fn texture_from_image(img: &T) -> &T { img } // Draw the conrod `render::Primitives`. 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); } }); } }