Verified Commit ccc31916 authored by insert's avatar insert

Add alignments.

parent 9e7a7aa6
This diff is collapsed.
use renderer::*;
use ui::{ UiRenderer, Position2D, Alignment };
use native::{ Event, Window, take_event };
use ui::{ UiRenderer, Position2D };
use render::Texture;
fn main() {
......@@ -34,21 +34,24 @@ fn main() {
ui.begin();
ui.draw_rect(Position2D::new().set_alignment(Alignment::MID_CENTRE).set_pos(00.0, 0.0).set_size(640.0, 360.0),
&glm::vec4(0.0, 0.0, 0.0, 0.2));
ui.set_text_colour(&glm::vec3(1.0, 1.0, 1.0));
ui.draw_text("Quicksand", "ah yes, very epic and cool beans",
Position2D::new().set_pos(120.0, 300.0).set_height(72.0));
Position2D::new().set_alignment(Alignment::MID_CENTRE).set_pos(0.0, -20.0).set_height(72.0));
ui.draw_text("WorkSans", "sans font",
Position2D::new().set_pos(40.0, 10.0).set_height(280.0));
ui.draw_text("IndieFlower", "\"gamer\" - sans from sans undertale",
Position2D::new().set_pos(40.0, 540.0).set_height(80.0));
Position2D::new().set_alignment(Alignment::BOT_LEFT).set_pos(20.0, -20.0).set_height(80.0));
ui.set_text_colour(&glm::vec3(0.05, 0.68, 0.89));
ui.draw_text("WorkSans", "this text is a nice shade of blue",
Position2D::new().set_pos(120.0, 420.0).set_height(48.0));
Position2D::new().set_alignment(Alignment::MID_CENTRE).set_pos(0.0, 50.0).set_height(48.0));
ui.draw_rect(Position2D::new().set_pos(0.0, 0.0).set_size(100.0, 100.0),
&glm::vec4(1.0, 0.0, 0.0, 1.0));
......
use crate::native::get_active_viewport;
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub enum Alignment {
TOP_LEFT,
TOP_CENTRE,
......@@ -13,6 +13,7 @@ pub enum Alignment {
BOT_RIGHT,
}
#[derive(Debug)]
pub struct Position2D {
pub alignment: Alignment,
pub x: f32,
......@@ -62,6 +63,46 @@ impl Position2D {
}
}
pub fn translate(self) -> Position2D {
let vp = get_active_viewport().unwrap();
let x = match self.alignment {
Alignment::TOP_LEFT |
Alignment::MID_LEFT |
Alignment::BOT_LEFT =>
self.x,
Alignment::TOP_CENTRE |
Alignment::MID_CENTRE |
Alignment::BOT_CENTRE =>
vp.w as f32 / 2.0 - self.w / 2.0 + self.x,
Alignment::TOP_RIGHT |
Alignment::MID_RIGHT |
Alignment::BOT_RIGHT =>
vp.w as f32 - self.w + self.x,
};
let y = match self.alignment {
Alignment::TOP_LEFT |
Alignment::TOP_CENTRE |
Alignment::TOP_RIGHT =>
self.y,
Alignment::MID_LEFT |
Alignment::MID_CENTRE |
Alignment::MID_RIGHT =>
vp.h as f32 / 2.0 - self.h / 2.0 + self.y,
Alignment::BOT_LEFT |
Alignment::BOT_CENTRE |
Alignment::BOT_RIGHT =>
vp.h as f32 - self.h + self.y,
};
Position2D {
x,
y,
..self
}
}
pub fn generate(&self) -> glm::Mat4 {
let vp = get_active_viewport().unwrap();
let proj = glm::ortho(0.0, vp.w as f32, vp.h as f32, 0.0, -1.0, 1.0);
......
......@@ -96,6 +96,18 @@ impl<'a> Font<'a> {
let shader = get_active_program().expect("No active program.");
let glyphs: Vec<PositionedGlyph<'_>> = self.font.layout(&text, scale, offset).collect();
let position = Position2D {
w: glyphs
.iter()
.rev()
.map(|g| g.position().x as f32 + g.unpositioned().h_metrics().advance_width)
.next()
.unwrap_or(0.0),
h: height,
..position
}.translate();
for (i, g) in glyphs.iter().enumerate() {
if let Some(bb) = g.pixel_bounding_box() {
let c = text[i..].chars().next().unwrap();
......
......@@ -108,7 +108,7 @@ impl<'a> UiRenderer<'a> {
pub fn draw_rect(&mut self, position: Position2D, colour: &glm::Vec4) {
self.default_program.enable();
self.default_program.upload_vec4("colour", &colour);
self.default_program.upload_mat4("projection", &position.generate());
self.default_program.upload_mat4("projection", &position.translate().generate());
self.default_mesh.draw();
}
......@@ -116,14 +116,14 @@ impl<'a> UiRenderer<'a> {
self.texture_program.enable();
texture.enable(0);
self.texture_program.upload_i32("tex", 0);
self.texture_program.upload_mat4("projection", &position.generate());
self.texture_program.upload_mat4("projection", &position.translate().generate());
self.default_mesh.draw();
}
pub fn draw_attachment(&mut self, position: Position2D, attachment: i32) {
self.texture_program.enable();
self.texture_program.upload_i32("tex", attachment);
self.texture_program.upload_mat4("projection", &position.generate());
self.texture_program.upload_mat4("projection", &position.translate().generate());
self.default_mesh.draw();
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment