Skip to content

Commit

Permalink
add debugging scale
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Sep 7, 2024
1 parent ab4fd1c commit e8b3e6f
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 12 deletions.
4 changes: 3 additions & 1 deletion crates/gosub_render_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ pub trait RenderBackend: Sized + Debug {
pub trait Scene<B: RenderBackend> {
fn draw_rect(&mut self, rect: &RenderRect<B>);
fn draw_text(&mut self, text: &RenderText<B>);

fn debug_draw_simple_text(&mut self, text: &str, pos: Point, size: FP);
fn apply_scene(&mut self, scene: &B::Scene, transform: Option<B::Transform>);
fn reset(&mut self);

fn new(data: &mut B::WindowData<'_>) -> Self;
fn new() -> Self;
}

pub struct RenderRect<B: RenderBackend> {
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_render_backend/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{ImageBuffer, RenderBackend};
pub trait SvgRenderer<B: RenderBackend> {
type SvgDocument;

fn new(wd: &mut B::WindowData<'_>) -> Self;
fn new() -> Self;

fn parse_external(data: String) -> Result<Self::SvgDocument>;
fn parse_internal(tree: DocumentHandle, id: NodeId) -> Result<Self::SvgDocument>;
Expand Down
1 change: 1 addition & 0 deletions crates/gosub_renderer/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod scale;
54 changes: 54 additions & 0 deletions crates/gosub_renderer/src/debug/scale.rs
Original file line number Diff line number Diff line change
@@ -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<B: RenderBackend>(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::<B>(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<B: RenderBackend>(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
}
29 changes: 23 additions & 6 deletions crates/gosub_renderer/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -109,14 +110,30 @@ where

if self.dirty {
if let Some(id) = self.selected_element {
self.debug_annotate(id, data);
self.debug_annotate(id);
}
}

if let Some(scene) = &self.debugger_scene {
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::<B>(
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 {
Expand All @@ -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;
Expand Down Expand Up @@ -740,12 +757,12 @@ fn get_border_side<B: RenderBackend, L: Layouter>(
}

impl<B: RenderBackend, L: Layouter> TreeDrawer<B, L> {
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;
Expand Down
1 change: 1 addition & 0 deletions crates/gosub_renderer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod debug;
pub mod draw;
pub mod render_tree;
2 changes: 1 addition & 1 deletion crates/gosub_svg/src/resvg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Resvg;
impl<B: RenderBackend> SvgRenderer<B> for Resvg {
type SvgDocument = SVGDocument;

fn new(_: &mut B::WindowData<'_>) -> Self {
fn new() -> Self {
Self
}

Expand Down
2 changes: 2 additions & 0 deletions crates/gosub_typeface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,5 @@
// }

pub mod font;

pub const ROBOTO_FONT: &[u8] = include_bytes!("../../../resources/fonts/Roboto-Regular.ttf");
1 change: 1 addition & 0 deletions crates/gosub_vello/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod text;
2 changes: 2 additions & 0 deletions crates/gosub_vello/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions crates/gosub_vello/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<VelloBackend> for Scene {
Expand Down Expand Up @@ -46,6 +51,10 @@ impl TScene<VelloBackend> 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: &<VelloBackend as RenderBackend>::Scene,
Expand All @@ -62,7 +71,7 @@ impl TScene<VelloBackend> for Scene {
self.0.reset()
}

fn new(_data: &mut <VelloBackend as RenderBackend>::WindowData<'_>) -> Self {
fn new() -> Self {
VelloScene::new().into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/gosub_vello/src/vello_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct VelloSVG;
impl SvgRenderer<VelloBackend> for VelloSVG {
type SvgDocument = SVGDocument;

fn new(_: &mut WindowData) -> Self {
fn new() -> Self {
Self
}

Expand Down

0 comments on commit e8b3e6f

Please sign in to comment.