mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 07:41: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 = [
|
dependencies = [
|
||||||
"bumpalo",
|
"bumpalo",
|
||||||
"const_format",
|
"const_format",
|
||||||
|
"inkwell 0.1.0",
|
||||||
|
"libloading 0.7.1",
|
||||||
|
"roc_build",
|
||||||
|
"roc_builtins",
|
||||||
|
"roc_collections",
|
||||||
|
"roc_gen_llvm",
|
||||||
|
"roc_load",
|
||||||
"roc_mono",
|
"roc_mono",
|
||||||
"roc_parse",
|
"roc_parse",
|
||||||
"roc_repl_eval",
|
"roc_repl_eval",
|
||||||
|
"roc_target",
|
||||||
|
"roc_types",
|
||||||
"rustyline",
|
"rustyline",
|
||||||
"rustyline-derive",
|
"rustyline-derive",
|
||||||
"target-lexicon",
|
"target-lexicon",
|
||||||
|
@ -2875,14 +2884,10 @@ name = "roc_repl_eval"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bumpalo",
|
"bumpalo",
|
||||||
"inkwell 0.1.0",
|
|
||||||
"libloading 0.7.1",
|
|
||||||
"roc_build",
|
|
||||||
"roc_builtins",
|
"roc_builtins",
|
||||||
"roc_can",
|
"roc_can",
|
||||||
"roc_collections",
|
"roc_collections",
|
||||||
"roc_fmt",
|
"roc_fmt",
|
||||||
"roc_gen_llvm",
|
|
||||||
"roc_load",
|
"roc_load",
|
||||||
"roc_module",
|
"roc_module",
|
||||||
"roc_mono",
|
"roc_mono",
|
||||||
|
@ -2891,7 +2896,6 @@ dependencies = [
|
||||||
"roc_reporting",
|
"roc_reporting",
|
||||||
"roc_target",
|
"roc_target",
|
||||||
"roc_types",
|
"roc_types",
|
||||||
"target-lexicon",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
use crate::{graphics::{
|
use crate::{
|
||||||
lowlevel::buffer::create_rect_buffers, lowlevel::ortho::update_ortho_buffer,
|
graphics::{
|
||||||
lowlevel::pipelines, primitives::rect::Rect, primitives::text::{build_glyph_brush, owned_section_from_text, Text}, colors,
|
colors::{self, from_hsb, to_wgpu_color},
|
||||||
}, rects_and_texts::RectsAndTexts};
|
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 pipelines::RectResources;
|
||||||
use roc_std::RocStr;
|
use roc_std::RocStr;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
@ -20,7 +27,7 @@ use winit::{
|
||||||
//
|
//
|
||||||
// See this link to learn wgpu: https://sotrh.github.io/learn-wgpu/
|
// 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
|
// Open window and create a surface
|
||||||
let mut event_loop = winit::event_loop::EventLoop::new();
|
let mut event_loop = winit::event_loop::EventLoop::new();
|
||||||
let mut needs_repaint = true;
|
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 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
|
// Render loop
|
||||||
window.request_redraw();
|
window.request_redraw();
|
||||||
|
|
||||||
|
@ -205,10 +199,19 @@ fn run_event_loop(title: &str) -> Result<(), Box<dyn Error>> {
|
||||||
.texture
|
.texture
|
||||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
|
||||||
for text_section in &rects_and_texts.text_sections_behind {
|
draw_rects(
|
||||||
let borrowed_text = text_section.to_borrowed();
|
&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
|
// draw first layer of text
|
||||||
|
@ -224,20 +227,20 @@ fn run_event_loop(title: &str) -> Result<(), Box<dyn Error>> {
|
||||||
.expect("Failed to draw first layer of text.");
|
.expect("Failed to draw first layer of text.");
|
||||||
|
|
||||||
// draw rects on top of first text layer
|
// draw rects on top of first text layer
|
||||||
// draw_rects(
|
draw_rects(
|
||||||
// &rendered_wgpu.rects_front,
|
&rects_and_texts.rects_front,
|
||||||
// &mut encoder,
|
&mut encoder,
|
||||||
// &view,
|
&view,
|
||||||
// &gpu_device,
|
&gpu_device,
|
||||||
// &rect_resources,
|
&rect_resources,
|
||||||
// wgpu::LoadOp::Load,
|
wgpu::LoadOp::Load,
|
||||||
// );
|
);
|
||||||
|
|
||||||
// for text_section in &rendered_wgpu.text_sections_front {
|
for text_section in &rects_and_texts.text_sections_front {
|
||||||
// let borrowed_text = text_section.to_borrowed();
|
let borrowed_text = text_section.to_borrowed();
|
||||||
|
|
||||||
// glyph_brush.queue(borrowed_text);
|
glyph_brush.queue(borrowed_text);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// draw text
|
// draw text
|
||||||
glyph_brush
|
glyph_brush
|
||||||
|
@ -315,5 +318,45 @@ fn begin_render_pass<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(title: RocStr) {
|
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::rect::Rect;
|
||||||
use crate::graphics::primitives::text::{owned_section_from_text, Text};
|
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) {
|
pub fn add_text_behind(&mut self, new_text_section: glyph_brush::OwnedSection) {
|
||||||
self.text_sections_behind.push(new_text_section);
|
self.text_sections_behind.push(new_text_section);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue