From ceab381519a72196640b0c2ed13f3649a7d1bf27 Mon Sep 17 00:00:00 2001 From: Imberflur Date: Mon, 11 Feb 2019 23:14:55 -0500 Subject: [PATCH] move conrod ui rendering and setup into ui::Ui Former-commit-id: 1dc43fb7d73671d0b798238fecfcbf8d07ffbfcb --- voxygen/src/menu/title.rs | 78 ++++++++++----------------------- voxygen/src/ui/mod.rs | 92 +++++++++++++++++++++++++++++---------- 2 files changed, 92 insertions(+), 78 deletions(-) diff --git a/voxygen/src/menu/title.rs b/voxygen/src/menu/title.rs index d2fb636b01..868a86f739 100644 --- a/voxygen/src/menu/title.rs +++ b/voxygen/src/menu/title.rs @@ -1,12 +1,8 @@ // Library use vek::*; use image; -use conrod_core::Ui; -use conrod_core::UiBuilder; use conrod_core::widget::image::Image as ImageWidget; -use conrod_core::image::Map as ImageMap; -use conrod_core::{Positionable, Sizeable}; -use conrod_core::Widget; +use conrod_core::{Positionable, Sizeable, Widget}; // Crate use crate::{ @@ -19,33 +15,31 @@ use crate::{ Consts, UiLocals, Renderer, - Texture, - UiPipeline, - create_ui_quad_mesh, + }, + ui::{ + Ui }, }; pub struct TitleState { - ui: Ui, - image_map: ImageMap>, + ui: Ui } impl TitleState { /// Create a new `TitleState` pub fn new(renderer: &mut Renderer) -> Self { - let mut ui = UiBuilder::new([500.0, 500.0]).build(); - let widget_id = ui.widget_id_generator().next(); - let mut image_map = ImageMap::new(); - let img = image::open(concat!(env!("CARGO_MANIFEST_DIR"), "/test_assets/test.png")).unwrap(); - let img = renderer.create_texture(&img).unwrap(); - let img_id = image_map.insert(img); - ImageWidget::new(img_id) - .x_y(0.0, 0.0) - .w_h(500.0,500.0) - .set(widget_id, &mut ui.set_widgets()); + let mut ui = Ui::new(renderer, [500.0, 500.0]).unwrap(); + let widget_id = ui.new_widget(); + let image = image::open(concat!(env!("CARGO_MANIFEST_DIR"), "/test_assets/test.png")).unwrap(); + let title_img = ui.new_image(renderer, &image).unwrap(); + ui.set_widgets(|ui_cell| { + ImageWidget::new(title_img) + .x_y(0.0, 0.0) + .w_h(500.0,500.0) + .set(widget_id, ui_cell); + }); Self { - ui, - image_map + ui } } } @@ -70,43 +64,19 @@ impl PlayState for TitleState { } + global_state.window.renderer_mut().clear(BG_COLOR); + // Maintain the UI //self.ui.maintain(global_state.window.renderer_mut()); // Draw the UI to the screen - //self.ui.render(global_state.window.renderer_mut()); - if let Some(mut primitives) = Some(self.ui.draw()){//_if_changed() { - // Clear the screen - global_state.window.renderer_mut().clear(BG_COLOR); - - //render the primatives one at a time - while let Some(prim) = primitives.next() { - let mut renderer = global_state.window.renderer_mut(); - use conrod_core::render::{Primitive, PrimitiveKind}; - let Primitive {kind, scizzor, rect, ..} = prim; - match kind { - PrimitiveKind::Image { image_id, color, source_rect } => { - - let mut locals = renderer.create_consts(&[UiLocals::default()]).unwrap(); - renderer.update_consts(&mut locals, &[UiLocals::new( - [0.0, 0.0, 1.0, 1.0], - )]); - let model = renderer.create_model(&create_ui_quad_mesh()).unwrap(); - global_state.window.renderer_mut().render_ui_element(&model, &locals, self.image_map.get(&image_id).unwrap()) - } - _ => {println!("should not reach here");} - } - } - - - // Finish the frame - global_state.window.renderer_mut().flush(); - global_state.window - .swap_buffers() - .expect("Failed to swap window buffers"); - } - + self.ui.render(global_state.window.renderer_mut()); + // Finish the frame + global_state.window.renderer_mut().flush(); + global_state.window + .swap_buffers() + .expect("Failed to swap window buffers"); } } diff --git a/voxygen/src/ui/mod.rs b/voxygen/src/ui/mod.rs index 89183c37ff..f3efcfa633 100644 --- a/voxygen/src/ui/mod.rs +++ b/voxygen/src/ui/mod.rs @@ -1,15 +1,27 @@ -pub mod element; -pub mod size_request; -pub mod span; +//pub mod element; +//pub mod size_request; +//pub mod span; // Reexports -pub use self::{ +/*pub use self::{ span::Span, size_request::SizeRequest, -}; +};*/ + +// TODO: what was the purpose of size request? +// TODO: cache entire UI render +// TODO: do we need to store locals for each widget? +// TODO: sizing? : renderer.get_resolution().map(|e| e as f32) // Library use image::DynamicImage; +use conrod_core::{ + Ui as CrUi, + UiBuilder, + UiCell, + image::{Map, Id as ImgId}, + widget::Id as WidgId, +}; // Crate use crate::{ @@ -20,15 +32,17 @@ use crate::{ Model, Texture, UiPipeline, + UiLocals, + Consts, create_ui_quad_mesh, }, }; // Local -use self::element::{ +/*use self::element::{ Element, Bounds, -}; +};*/ #[derive(Debug)] pub enum UiError { @@ -53,33 +67,63 @@ impl Cache { } pub struct Ui { - base: Box, + ui: CrUi, + image_map: Map>, cache: Cache, + locals: Consts, } impl Ui { - pub fn new(renderer: &mut Renderer, base: Box) -> Result { + pub fn new(renderer: &mut Renderer, dim: [f64; 2]) -> Result { Ok(Self { - base, + ui: UiBuilder::new(dim).build(), + image_map: Map::new(), cache: Cache::new(renderer)?, + locals: renderer.create_consts(&[UiLocals::default()])?, + }) } - pub fn maintain(&mut self, renderer: &mut Renderer) { - self.base.maintain( - renderer, - &self.cache, - Bounds::new(0.0, 0.0, 1.0, 1.0), - renderer.get_resolution().map(|e| e as f32), - ) + pub fn new_image(&mut self, renderer: &mut Renderer, image: &DynamicImage) -> Result { + Ok(self.image_map.insert(renderer.create_texture(image)?)) } - pub fn render(&self, renderer: &mut Renderer) { - self.base.render( - renderer, - &self.cache, - Bounds::new(0.0, 0.0, 1.0, 1.0), - renderer.get_resolution().map(|e| e as f32), - ); + pub fn new_widget(&mut self) -> WidgId { + self.ui.widget_id_generator().next() + } + + pub fn set_widgets(&mut self, f: F) where F: FnOnce(&mut UiCell) { + f(&mut self.ui.set_widgets()); + } + + // TODO: change render back to &self and use maintain for mutable operations + pub fn maintain(&mut self, renderer: &mut Renderer) { + + //renderer.get_resolution().map(|e| e as f32), + } + + pub fn render(&mut self, renderer: &mut Renderer) { + + if let Some(mut primitives) = Some(self.ui.draw()) { + //render the primatives one at a time + while let Some(prim) = primitives.next() { + use conrod_core::render::{Primitive, PrimitiveKind}; + let Primitive {kind, scizzor, rect, ..} = prim; + match kind { + PrimitiveKind::Image { image_id, color, source_rect } => { + renderer.update_consts(&mut self.locals, &[UiLocals::new( + [0.0, 0.0, 1.0, 1.0], + )]); + let tex = self.image_map.get(&image_id).expect("Image does not exist in image map"); + renderer.render_ui_element(&self.cache.model(), &self.locals, &tex); + } + PrimitiveKind::Other {..} => {println!("primitive kind other with id");} + PrimitiveKind::Rectangle {..} => {println!("primitive kind rect");} + PrimitiveKind::Text {..} => {println!("primitive kind text");} + PrimitiveKind::TrianglesMultiColor {..} => {println!("primitive kind multicolor");} + PrimitiveKind::TrianglesSingleColor {..} => {println!("primitive kind singlecolor");} + } + } + } } }