mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
succesfull rendering of multi-level rects and texts
This commit is contained in:
parent
51d86f101f
commit
59a0c47038
3 changed files with 96 additions and 41 deletions
14
cli_utils/Cargo.lock
generated
14
cli_utils/Cargo.lock
generated
|
@ -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]]
|
||||
|
|
|
@ -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<dyn Error>> {
|
||||
fn run_event_loop(title: &str, rects_and_texts: RectsAndTexts) -> Result<(), Box<dyn Error>> {
|
||||
// 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<dyn Error>> {
|
|||
|
||||
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,6 +199,15 @@ fn run_event_loop(title: &str) -> Result<(), Box<dyn Error>> {
|
|||
.texture
|
||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
|
||||
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))),
|
||||
);
|
||||
|
||||
for text_section in &rects_and_texts.text_sections_behind {
|
||||
let borrowed_text = text_section.to_borrowed();
|
||||
|
||||
|
@ -224,20 +227,20 @@ fn run_event_loop(title: &str) -> Result<(), Box<dyn Error>> {
|
|||
.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");
|
||||
}
|
||||
|
|
|
@ -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<Rect>, texts_behind: Vec<Text>, rects_front: Vec<Rect>, texts_front: Vec<Text>) -> 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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue