Use new coordinate system in Magpie and text rendering

This commit is contained in:
mars 2022-10-31 00:06:10 -06:00
parent 4a5f27a95d
commit 23d699dfef
5 changed files with 33 additions and 15 deletions

View File

@ -1,5 +1,5 @@
use canary::DrawCommand;
use glium::{glutin, Display, Surface};
use canary::{DrawCommand, Vec2, PX_PER_MM};
use glium::Surface;
#[derive(Copy, Clone)]
pub struct Vertex {
@ -9,11 +9,15 @@ pub struct Vertex {
glium::implement_vertex!(Vertex, position normalize(false), color normalize(true));
impl From<&canary::MeshVertex> for Vertex {
fn from(v: &canary::MeshVertex) -> Self {
impl Vertex {
pub fn from_canary(size: Vec2, v: &canary::MeshVertex) -> Self {
// TODO do this in the vertex shader with a size uniform
let (r, g, b, a) = v.color.to_rgba_unmultiplied();
Self {
position: [v.position.x, v.position.y],
position: [
(v.position.x / size.x) * 2.0 - 1.0,
(v.position.y / size.y) * -2.0 + 1.0,
],
color: [r, g, b, a],
}
}
@ -62,11 +66,22 @@ impl Graphics {
let mut joined_vs: Vec<Vertex> = Vec::new();
let mut joined_is = Vec::new();
let (width, height) = {
let size = self.display.gl_window().window().inner_size();
let (width, height) = (size.width as f32, size.height as f32);
(width * PX_PER_MM, height * PX_PER_MM)
};
let size = Vec2 {
x: width,
y: height,
};
for command in commands.iter() {
match command {
canary::DrawCommand::Mesh { vertices, indices } => {
let voff = joined_vs.len() as canary::MeshIndex;
joined_vs.extend(vertices.iter().map(Vertex::from));
joined_vs.extend(vertices.iter().map(|v| Vertex::from_canary(size, v)));
joined_is.extend(indices.iter().map(|i| i + voff));
}
_ => unimplemented!(),

View File

@ -28,8 +28,8 @@ impl PanelImpl for MusicPlayerPanel {
fn draw(&mut self) {
let layout = TextLayout::new(&self.display_font, "Hello world!");
let offset = Vec2 { x: 0.0, y: 0.0 };
let scale = 0.1;
let offset = Vec2 { x: 100.0, y: 100.0 };
let scale = 14.0;
let color = Color::WHITE;
self.panel.draw_text_layout(&layout, offset, scale, color);
}

View File

@ -9,6 +9,9 @@ use std::sync::Arc;
pub mod text;
/// Proportion constant between pixels (at 96dpi) to millimeters (Canary's unit measurement).
pub const PX_PER_MM: f32 = 25.4 / 96.0;
/// Low-level script API callbacks.
///
/// If you're a casual user of Canary the struct you're looking for is

View File

@ -135,7 +135,7 @@ impl OutlineSink {
}
fn pf_vector_to_lyon(&mut self, v: &Vector2F) -> lyon::geom::Point<f32> {
let point = lyon::geom::Point::<f32>::new(v.x(), v.y()) / self.units_per_em;
let point = lyon::geom::Point::<f32>::new(v.x(), -v.y()) / self.units_per_em;
// TODO clean this up with helper math methods?
let bb = &mut self.bounding_box;
@ -148,12 +148,12 @@ impl OutlineSink {
bb.tr.x = point.x;
}
if point.y < bb.bl.y {
bb.bl.y = point.y;
if point.y < bb.tr.y {
bb.tr.y = point.y;
}
if point.y > bb.tr.y {
bb.tr.y = point.y;
if point.y > bb.bl.y {
bb.bl.y = point.y;
}
point

View File

@ -133,8 +133,8 @@ impl Font {
ycur += position.vert_advance;
let voff = vertices.len() as MeshIndex;
let xpos = xpos as f32 * pos_scale + offset.x;
let ypos = ypos as f32 * pos_scale + offset.y;
let xpos = offset.x + xpos as f32 * pos_scale;
let ypos = offset.y - ypos as f32 * pos_scale;
for v in glyph.vertices.iter() {
let x = v.x * scale + xpos;