From 59a0c47038a9a2d385e1cc64cd0786e23fb8bd52 Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Sat, 12 Feb 2022 11:55:29 +0100 Subject: [PATCH] succesfull rendering of multi-level rects and texts --- cli_utils/Cargo.lock | 14 ++- examples/gui/platform/src/gui.rs | 111 +++++++++++++------ examples/gui/platform/src/rects_and_texts.rs | 12 +- 3 files changed, 96 insertions(+), 41 deletions(-) diff --git a/cli_utils/Cargo.lock b/cli_utils/Cargo.lock index 4c5fb429e6..22e8954259 100644 --- a/cli_utils/Cargo.lock +++ b/cli_utils/Cargo.lock @@ -2862,9 +2862,18 @@ version = "0.1.0" dependencies = [ "bumpalo", "const_format", + "inkwell 0.1.0", + "libloading 0.7.1", + "roc_build", + "roc_builtins", + "roc_collections", + "roc_gen_llvm", + "roc_load", "roc_mono", "roc_parse", "roc_repl_eval", + "roc_target", + "roc_types", "rustyline", "rustyline-derive", "target-lexicon", @@ -2875,14 +2884,10 @@ name = "roc_repl_eval" version = "0.1.0" dependencies = [ "bumpalo", - "inkwell 0.1.0", - "libloading 0.7.1", - "roc_build", "roc_builtins", "roc_can", "roc_collections", "roc_fmt", - "roc_gen_llvm", "roc_load", "roc_module", "roc_mono", @@ -2891,7 +2896,6 @@ dependencies = [ "roc_reporting", "roc_target", "roc_types", - "target-lexicon", ] [[package]] diff --git a/examples/gui/platform/src/gui.rs b/examples/gui/platform/src/gui.rs index d7b88b9ef7..63cb06272a 100644 --- a/examples/gui/platform/src/gui.rs +++ b/examples/gui/platform/src/gui.rs @@ -1,7 +1,14 @@ -use crate::{graphics::{ - lowlevel::buffer::create_rect_buffers, lowlevel::ortho::update_ortho_buffer, - lowlevel::pipelines, primitives::rect::Rect, primitives::text::{build_glyph_brush, owned_section_from_text, Text}, colors, -}, rects_and_texts::RectsAndTexts}; +use crate::{ + graphics::{ + colors::{self, from_hsb, to_wgpu_color}, + lowlevel::buffer::create_rect_buffers, + lowlevel::ortho::update_ortho_buffer, + lowlevel::pipelines, + primitives::rect::Rect, + primitives::text::{build_glyph_brush, owned_section_from_text, Text}, + }, + rects_and_texts::RectsAndTexts, +}; use pipelines::RectResources; use roc_std::RocStr; use std::error::Error; @@ -20,7 +27,7 @@ use winit::{ // // See this link to learn wgpu: https://sotrh.github.io/learn-wgpu/ -fn run_event_loop(title: &str) -> Result<(), Box> { +fn run_event_loop(title: &str, rects_and_texts: RectsAndTexts) -> Result<(), Box> { // Open window and create a surface let mut event_loop = winit::event_loop::EventLoop::new(); let mut needs_repaint = true; @@ -88,19 +95,6 @@ fn run_event_loop(title: &str) -> Result<(), Box> { let mut keyboard_modifiers = ModifiersState::empty(); - let mut rects_and_texts = RectsAndTexts::new(); - - let hello_text = owned_section_from_text(&Text { - position: (50.0, 50.0).into(), - area_bounds: (size.width as f32, size.height as f32).into(), - color: colors::WHITE, - text: "Hello, World!", - size: 40.0, - ..Default::default() - }); - - rects_and_texts.add_text_behind(hello_text); - // Render loop window.request_redraw(); @@ -205,10 +199,19 @@ fn run_event_loop(title: &str) -> Result<(), Box> { .texture .create_view(&wgpu::TextureViewDescriptor::default()); - for text_section in &rects_and_texts.text_sections_behind { - let borrowed_text = text_section.to_borrowed(); + draw_rects( + &rects_and_texts.rects_behind, + &mut encoder, + &view, + &gpu_device, + &rect_resources, + wgpu::LoadOp::Clear(to_wgpu_color(from_hsb(240, 10, 19))), + ); - glyph_brush.queue(borrowed_text); + for text_section in &rects_and_texts.text_sections_behind { + let borrowed_text = text_section.to_borrowed(); + + glyph_brush.queue(borrowed_text); } // draw first layer of text @@ -224,20 +227,20 @@ fn run_event_loop(title: &str) -> Result<(), Box> { .expect("Failed to draw first layer of text."); // draw rects on top of first text layer - // draw_rects( - // &rendered_wgpu.rects_front, - // &mut encoder, - // &view, - // &gpu_device, - // &rect_resources, - // wgpu::LoadOp::Load, - // ); + draw_rects( + &rects_and_texts.rects_front, + &mut encoder, + &view, + &gpu_device, + &rect_resources, + wgpu::LoadOp::Load, + ); - // for text_section in &rendered_wgpu.text_sections_front { - // let borrowed_text = text_section.to_borrowed(); + for text_section in &rects_and_texts.text_sections_front { + let borrowed_text = text_section.to_borrowed(); - // glyph_brush.queue(borrowed_text); - // } + glyph_brush.queue(borrowed_text); + } // draw text glyph_brush @@ -315,5 +318,45 @@ fn begin_render_pass<'a>( } pub fn render(title: RocStr) { - run_event_loop(title.as_str()).expect("Error running event loop") + let rects_behind = vec![ + Rect { + top_left_coords: (20.0, 20.0).into(), + width: 200.0, + height: 100.0, + color: (0.4, 0.2, 0.5, 1.0), + } + ]; + + let texts_behind = vec![ + Text { + position: (50.0, 50.0).into(), + color: colors::WHITE, + text: "Back", + size: 40.0, + ..Default::default() + } + ]; + + let rects_front = vec![ + Rect { + top_left_coords: (30.0, 30.0).into(), + width: 70.0, + height: 70.0, + color: (0.7, 0.2, 0.2, 0.6), + } + ]; + + let texts_front = vec![ + Text { + position: (70.0, 70.0).into(), + color: colors::WHITE, + text: "Front", + size: 40.0, + ..Default::default() + } + ]; + + let rects_and_texts = RectsAndTexts::init(rects_behind, texts_behind, rects_front, texts_front); + + run_event_loop(title.as_str(), rects_and_texts).expect("Error running event loop"); } diff --git a/examples/gui/platform/src/rects_and_texts.rs b/examples/gui/platform/src/rects_and_texts.rs index 6ecdaaa27f..2d8bfa23d4 100644 --- a/examples/gui/platform/src/rects_and_texts.rs +++ b/examples/gui/platform/src/rects_and_texts.rs @@ -1,4 +1,3 @@ - use crate::graphics::primitives::rect::Rect; use crate::graphics::primitives::text::{owned_section_from_text, Text}; @@ -20,6 +19,15 @@ impl RectsAndTexts { } } + 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(), + rects_behind, + rects_front, + } + } + pub fn add_text_behind(&mut self, new_text_section: glyph_brush::OwnedSection) { self.text_sections_behind.push(new_text_section); } @@ -48,4 +56,4 @@ impl RectsAndTexts { self.rects_behind.extend(rects_and_texts.rects_behind); self.rects_front.extend(rects_and_texts.rects_front); } -} \ No newline at end of file +}