First button ever rendered!

This commit is contained in:
Richard Feldman 2022-02-22 21:42:00 -05:00
parent 4399a6dfee
commit 5b92eb87ec
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
2 changed files with 73 additions and 4 deletions

View file

@ -11,10 +11,13 @@ use crate::{
}, },
roc::{RocElem, RocElemTag}, roc::{RocElem, RocElemTag},
}; };
use cgmath::{Vector2, Vector4};
use glyph_brush::OwnedSection;
use pipelines::RectResources; use pipelines::RectResources;
use roc_std::RocStr; use roc_std::RocStr;
use std::error::Error; use std::error::Error;
use wgpu::{CommandEncoder, LoadOp, RenderPass, TextureView}; use wgpu::{CommandEncoder, LoadOp, RenderPass, TextureView};
use wgpu_glyph::GlyphBrush;
use winit::{ use winit::{
dpi::PhysicalSize, dpi::PhysicalSize,
event, event,
@ -230,6 +233,8 @@ fn run_event_loop(title: &str, root: RocElem) -> Result<(), Box<dyn Error>> {
display_elem( display_elem(
&root, &root,
&mut staging_belt,
&mut glyph_brush,
&mut cmd_encoder, &mut cmd_encoder,
&view, &view,
&gpu_device, &gpu_device,
@ -396,6 +401,8 @@ pub fn render(title: RocStr, root: RocElem) {
fn display_elem( fn display_elem(
elem: &RocElem, elem: &RocElem,
staging_belt: &mut wgpu::util::StagingBelt,
glyph_brush: &mut GlyphBrush<()>,
cmd_encoder: &mut CommandEncoder, cmd_encoder: &mut CommandEncoder,
texture_view: &TextureView, texture_view: &TextureView,
gpu_device: &wgpu::Device, gpu_device: &wgpu::Device,
@ -425,6 +432,8 @@ fn display_elem(
display_elem( display_elem(
&*button.child, &*button.child,
staging_belt,
glyph_brush,
cmd_encoder, cmd_encoder,
texture_view, texture_view,
gpu_device, gpu_device,
@ -434,6 +443,22 @@ fn display_elem(
} }
Text => { Text => {
let text = unsafe { &elem.entry().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 => { Row => {
todo!("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<f32> = Vector2::new(50.0, 60.0);
let area_bounds: Vector2<f32> = 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::BuiltInLineBreaker> {
wgpu_glyph::Layout::default().h_align(if is_centered {
wgpu_glyph::HorizontalAlign::Center
} else {
wgpu_glyph::HorizontalAlign::Left
})
}

View file

@ -5,7 +5,7 @@ use crate::graphics::primitives::text::{owned_section_from_text, Text};
pub struct RectsAndTexts { pub struct RectsAndTexts {
pub text_sections_behind: Vec<glyph_brush::OwnedSection>, // displayed in front of rect_behind, behind everything else pub text_sections_behind: Vec<glyph_brush::OwnedSection>, // displayed in front of rect_behind, behind everything else
pub text_sections_front: Vec<glyph_brush::OwnedSection>, // displayed in front of everything pub text_sections_front: Vec<glyph_brush::OwnedSection>, // displayed in front of everything
pub rects_behind: Vec<RectElt>, // displayed at lowest depth pub rects_behind: Vec<RectElt>, // displayed at lowest depth
pub rects_front: Vec<RectElt>, // displayed in front of text_sections_behind, behind text_sections_front pub rects_front: Vec<RectElt>, // displayed in front of text_sections_behind, behind text_sections_front
} }
@ -19,10 +19,21 @@ impl RectsAndTexts {
} }
} }
pub fn init(rects_behind: Vec<RectElt>, texts_behind: Vec<Text>, rects_front: Vec<RectElt>, texts_front: Vec<Text>) -> Self { pub fn init(
rects_behind: Vec<RectElt>,
texts_behind: Vec<Text>,
rects_front: Vec<RectElt>,
texts_front: Vec<Text>,
) -> Self {
Self { Self {
text_sections_behind: texts_behind.iter().map(|txt| owned_section_from_text(txt)).collect(), text_sections_behind: texts_behind
text_sections_front: texts_front.iter().map(|txt| owned_section_from_text(txt)).collect(), .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_behind,
rects_front, rects_front,
} }