diff --git a/crates/gosub_render_backend/src/lib.rs b/crates/gosub_render_backend/src/lib.rs index bd394fb93..caba549db 100644 --- a/crates/gosub_render_backend/src/lib.rs +++ b/crates/gosub_render_backend/src/lib.rs @@ -77,10 +77,12 @@ pub trait RenderBackend: Sized + Debug { pub trait Scene { fn draw_rect(&mut self, rect: &RenderRect); fn draw_text(&mut self, text: &RenderText); + + fn debug_draw_simple_text(&mut self, text: &str, pos: Point, size: FP); fn apply_scene(&mut self, scene: &B::Scene, transform: Option); fn reset(&mut self); - fn new(data: &mut B::WindowData<'_>) -> Self; + fn new() -> Self; } pub struct RenderRect { diff --git a/crates/gosub_render_backend/src/svg.rs b/crates/gosub_render_backend/src/svg.rs index 02e194583..ccbf1a39c 100644 --- a/crates/gosub_render_backend/src/svg.rs +++ b/crates/gosub_render_backend/src/svg.rs @@ -7,7 +7,7 @@ use crate::{ImageBuffer, RenderBackend}; pub trait SvgRenderer { type SvgDocument; - fn new(wd: &mut B::WindowData<'_>) -> Self; + fn new() -> Self; fn parse_external(data: String) -> Result; fn parse_internal(tree: DocumentHandle, id: NodeId) -> Result; diff --git a/crates/gosub_renderer/src/debug.rs b/crates/gosub_renderer/src/debug.rs new file mode 100644 index 000000000..898f3ed8d --- /dev/null +++ b/crates/gosub_renderer/src/debug.rs @@ -0,0 +1 @@ +pub mod scale; diff --git a/crates/gosub_renderer/src/debug/scale.rs b/crates/gosub_renderer/src/debug/scale.rs new file mode 100644 index 000000000..bc6730dc5 --- /dev/null +++ b/crates/gosub_renderer/src/debug/scale.rs @@ -0,0 +1,54 @@ +use gosub_render_backend::{ + Brush, Color, Point, Rect, RenderBackend, RenderRect, Scene, SizeU32, Transform, +}; +use std::cmp::max; +use std::f32::consts::PI; + +pub fn px_scale(size: SizeU32, offset: Point, width: f32) -> B::Scene { + let mut scene = B::Scene::new(); + + let len = max( + size.width as i32 - offset.x as i32, + size.height as i32 - offset.y as i32, + ) as u32; + + let scale = draw_scale::(len, 50); + + let transform = B::Transform::translate(offset.x, 0.0); + + scene.apply_scene(&scale, Some(transform)); + + let transform = B::Transform::translate(width, offset.y).pre_rotate(PI / 2.0); + + scene.apply_scene(&scale, Some(transform)); + + scene +} + +pub fn draw_scale(len: u32, interval: u32) -> B::Scene { + let mut scene = B::Scene::new(); + + let mut x = 0; + + while x < len { + let mut height = 50.0; + + if x % 100 == 0 { + height = 60.0; + } + + scene.draw_rect(&RenderRect { + rect: B::Rect::new(x as f32, 0.0, 2.0, height), + transform: None, + radius: None, + brush: B::Brush::color(Color::BLACK), + brush_transform: None, + border: None, + }); + + scene.debug_draw_simple_text(&format!("{}", x), Point::new(x as f32, height + 10.0), 12.0); + x += interval; + } + + scene +} diff --git a/crates/gosub_renderer/src/draw.rs b/crates/gosub_renderer/src/draw.rs index cb003e3ab..73e5ecca0 100644 --- a/crates/gosub_renderer/src/draw.rs +++ b/crates/gosub_renderer/src/draw.rs @@ -3,6 +3,7 @@ use std::sync::mpsc::Sender; use anyhow::anyhow; use url::Url; +use crate::debug::scale::px_scale; use crate::draw::img::request_img; use crate::render_tree::{load_html_rendertree, TreeDrawer}; use gosub_css3::colors::RgbColor; @@ -61,7 +62,7 @@ where if self.tree_scene.is_none() || self.size != Some(size) { self.size = Some(size); - let mut scene = B::Scene::new(data); + let mut scene = B::Scene::new(); // Apply new maximums to the scene transform if let Some(scene_transform) = self.scene_transform.as_mut() { @@ -78,7 +79,7 @@ where let mut drawer = Drawer { scene: &mut scene, drawer: self, - svg: B::SVGRenderer::new(data), + svg: B::SVGRenderer::new(), }; drawer.render(size); @@ -109,7 +110,7 @@ where if self.dirty { if let Some(id) = self.selected_element { - self.debug_annotate(id, data); + self.debug_annotate(id); } } @@ -117,6 +118,22 @@ where self.dirty = false; backend.apply_scene(data, scene, self.scene_transform.clone()); } + + if self.debug { + let pos = self + .scene_transform + .as_ref() + .map(|x| Point::new(x.tx(), x.ty())) + .unwrap_or(Point::ZERO); + + let scale = px_scale::( + size, + pos, + self.size.as_ref().map(|x| x.width as f32).unwrap_or(0.0), + ); + + backend.apply_scene(data, &scale, None); + } } fn mouse_move(&mut self, _backend: &mut B, data: &mut B::WindowData<'_>, x: FP, y: FP) -> bool { @@ -135,7 +152,7 @@ where if self.last_hover != Some(e) { self.last_hover = Some(e); if self.debug { - return self.debug_annotate(e, data); + return self.debug_annotate(e); } } return false; @@ -740,12 +757,12 @@ fn get_border_side( } impl TreeDrawer { - fn debug_annotate(&mut self, e: NodeId, data: &mut B::WindowData<'_>) -> bool { + fn debug_annotate(&mut self, e: NodeId) -> bool { let Some(node) = self.tree.get_node(e) else { return false; }; - let mut scene = B::Scene::new(data); + let mut scene = B::Scene::new(); let Some(layout) = self.tree.get_layout(e) else { return false; diff --git a/crates/gosub_renderer/src/lib.rs b/crates/gosub_renderer/src/lib.rs index 1878add30..9f53161f5 100644 --- a/crates/gosub_renderer/src/lib.rs +++ b/crates/gosub_renderer/src/lib.rs @@ -1,2 +1,3 @@ +mod debug; pub mod draw; pub mod render_tree; diff --git a/crates/gosub_svg/src/resvg.rs b/crates/gosub_svg/src/resvg.rs index d5b316944..4857cfb90 100644 --- a/crates/gosub_svg/src/resvg.rs +++ b/crates/gosub_svg/src/resvg.rs @@ -15,7 +15,7 @@ pub struct Resvg; impl SvgRenderer for Resvg { type SvgDocument = SVGDocument; - fn new(_: &mut B::WindowData<'_>) -> Self { + fn new() -> Self { Self } diff --git a/crates/gosub_typeface/src/lib.rs b/crates/gosub_typeface/src/lib.rs index 4ea9e241a..c380349d1 100644 --- a/crates/gosub_typeface/src/lib.rs +++ b/crates/gosub_typeface/src/lib.rs @@ -308,3 +308,5 @@ // } pub mod font; + +pub const ROBOTO_FONT: &[u8] = include_bytes!("../../../resources/fonts/Roboto-Regular.ttf"); diff --git a/crates/gosub_vello/src/debug.rs b/crates/gosub_vello/src/debug.rs new file mode 100644 index 000000000..481c63acc --- /dev/null +++ b/crates/gosub_vello/src/debug.rs @@ -0,0 +1 @@ +pub mod text; diff --git a/crates/gosub_vello/src/lib.rs b/crates/gosub_vello/src/lib.rs index e09ac6eec..a7fd31255 100644 --- a/crates/gosub_vello/src/lib.rs +++ b/crates/gosub_vello/src/lib.rs @@ -32,8 +32,10 @@ mod scene; mod text; mod transform; +mod debug; #[cfg(feature = "vello_svg")] mod vello_svg; + pub struct VelloBackend; impl Debug for VelloBackend { diff --git a/crates/gosub_vello/src/scene.rs b/crates/gosub_vello/src/scene.rs index 49fe87415..33e3e76f1 100644 --- a/crates/gosub_vello/src/scene.rs +++ b/crates/gosub_vello/src/scene.rs @@ -2,8 +2,9 @@ use vello::kurbo::RoundedRect; use vello::peniko::Fill; use vello::Scene as VelloScene; -use gosub_render_backend::{RenderBackend, RenderRect, RenderText, Scene as TScene}; +use gosub_render_backend::{Point, RenderBackend, RenderRect, RenderText, Scene as TScene, FP}; +use crate::debug::text::render_text_simple; use crate::{Border, BorderRenderOptions, Text, Transform, VelloBackend}; pub struct Scene(pub(crate) VelloScene); @@ -12,6 +13,10 @@ impl Scene { pub fn inner(&mut self) -> &mut VelloScene { &mut self.0 } + + pub fn create() -> Self { + Self(VelloScene::new()) + } } impl TScene for Scene { @@ -46,6 +51,10 @@ impl TScene for Scene { Text::show(&mut self.0, text) } + fn debug_draw_simple_text(&mut self, text: &str, pos: Point, size: FP) { + render_text_simple(self, text, pos, size) + } + fn apply_scene( &mut self, scene: &::Scene, @@ -62,7 +71,7 @@ impl TScene for Scene { self.0.reset() } - fn new(_data: &mut ::WindowData<'_>) -> Self { + fn new() -> Self { VelloScene::new().into() } } diff --git a/crates/gosub_vello/src/vello_svg.rs b/crates/gosub_vello/src/vello_svg.rs index 731c5d1c6..99dd2f4fb 100644 --- a/crates/gosub_vello/src/vello_svg.rs +++ b/crates/gosub_vello/src/vello_svg.rs @@ -13,7 +13,7 @@ pub struct VelloSVG; impl SvgRenderer for VelloSVG { type SvgDocument = SVGDocument; - fn new(_: &mut WindowData) -> Self { + fn new() -> Self { Self }