diff --git a/examples/gui/platform/src/gui.rs b/examples/gui/platform/src/gui.rs index f6b8ee58ca..fd0cf096a2 100644 --- a/examples/gui/platform/src/gui.rs +++ b/examples/gui/platform/src/gui.rs @@ -11,10 +11,13 @@ use crate::{ }, roc::{RocElem, RocElemTag}, }; +use cgmath::{Vector2, Vector4}; +use glyph_brush::OwnedSection; use pipelines::RectResources; use roc_std::RocStr; use std::error::Error; use wgpu::{CommandEncoder, LoadOp, RenderPass, TextureView}; +use wgpu_glyph::GlyphBrush; use winit::{ dpi::PhysicalSize, event, @@ -230,6 +233,8 @@ fn run_event_loop(title: &str, root: RocElem) -> Result<(), Box> { display_elem( &root, + &mut staging_belt, + &mut glyph_brush, &mut cmd_encoder, &view, &gpu_device, @@ -396,6 +401,8 @@ pub fn render(title: RocStr, root: RocElem) { fn display_elem( elem: &RocElem, + staging_belt: &mut wgpu::util::StagingBelt, + glyph_brush: &mut GlyphBrush<()>, cmd_encoder: &mut CommandEncoder, texture_view: &TextureView, gpu_device: &wgpu::Device, @@ -425,6 +432,8 @@ fn display_elem( display_elem( &*button.child, + staging_belt, + glyph_brush, cmd_encoder, texture_view, gpu_device, @@ -434,6 +443,22 @@ fn display_elem( } Text => { let text = unsafe { &elem.entry().text }; + + glyph_brush.queue(owned_section_from_str(text.as_str()).to_borrowed()); + + // TODO don't hardcode any of this! + let area_bounds = (200, 300); + + glyph_brush + .draw_queued( + gpu_device, + staging_belt, + cmd_encoder, + texture_view, + area_bounds.0, + area_bounds.1, + ) + .expect("Failed to draw text element"); } Row => { todo!("Row"); @@ -443,3 +468,36 @@ fn display_elem( } } } + +fn owned_section_from_str(string: &str) -> OwnedSection { + let layout = layout_from_str(string, false); + + // TODO don't hardcode any of this! + let position: Vector2 = Vector2::new(50.0, 60.0); + let area_bounds: Vector2 = Vector2::new(200.0, 300.0); + let color /*: RgbaTup */ = colors::WHITE; + let size: f32 = 40.0; + + OwnedSection { + screen_position: position.into(), + bounds: area_bounds.into(), + layout, + ..OwnedSection::default() + } + .add_text( + glyph_brush::OwnedText::new(string) + .with_color(Vector4::from(color)) + .with_scale(size), + ) +} + +fn layout_from_str( + string: &str, + is_centered: bool, +) -> wgpu_glyph::Layout { + wgpu_glyph::Layout::default().h_align(if is_centered { + wgpu_glyph::HorizontalAlign::Center + } else { + wgpu_glyph::HorizontalAlign::Left + }) +} diff --git a/examples/gui/platform/src/rects_and_texts.rs b/examples/gui/platform/src/rects_and_texts.rs index f82d2f516a..3eed8654ac 100644 --- a/examples/gui/platform/src/rects_and_texts.rs +++ b/examples/gui/platform/src/rects_and_texts.rs @@ -5,7 +5,7 @@ use crate::graphics::primitives::text::{owned_section_from_text, Text}; pub struct RectsAndTexts { pub text_sections_behind: Vec, // displayed in front of rect_behind, behind everything else pub text_sections_front: Vec, // displayed in front of everything - pub rects_behind: Vec, // displayed at lowest depth + pub rects_behind: Vec, // displayed at lowest depth pub rects_front: Vec, // displayed in front of text_sections_behind, behind text_sections_front } @@ -19,10 +19,21 @@ impl RectsAndTexts { } } - pub fn init(rects_behind: Vec, texts_behind: Vec, rects_front: Vec, texts_front: Vec) -> Self { + pub fn init( + rects_behind: Vec, + texts_behind: Vec, + rects_front: Vec, + texts_front: Vec, + ) -> Self { Self { - text_sections_behind: texts_behind.iter().map(|txt| owned_section_from_text(txt)).collect(), - text_sections_front: texts_front.iter().map(|txt| owned_section_from_text(txt)).collect(), + text_sections_behind: texts_behind + .iter() + .map(|txt| owned_section_from_text(txt)) + .collect(), + text_sections_front: texts_front + .iter() + .map(|txt| owned_section_from_text(txt)) + .collect(), rects_behind, rects_front, }