hUI/hui/src/draw.rs

311 lines
11 KiB
Rust
Raw Normal View History

2024-02-20 17:19:10 +00:00
//! Stuff related to tesselation and UI rendering.
2024-02-20 19:56:58 +00:00
use crate::{
rectangle::Corners,
text::{FontHandle, TextRenderer},
IfModified
};
2024-02-17 20:43:46 +00:00
2024-02-24 22:32:09 +00:00
pub(crate) mod atlas;
//pub(crate) use atlas::{TextureAllocation, TextureAtlasManager};
//pub use atlas::TextureHandle;
2024-02-19 18:40:18 +00:00
2024-02-24 22:32:09 +00:00
mod corner_radius;
2024-02-20 16:30:26 +00:00
pub use corner_radius::RoundedCorners;
2024-02-24 22:32:09 +00:00
2024-02-17 20:43:46 +00:00
use std::borrow::Cow;
use fontdue::layout::{Layout, CoordinateSystem, TextStyle};
use glam::{Vec2, Vec4, vec2};
2024-02-19 20:12:12 +00:00
/// Available draw commands
/// - Rectangle: Filled, colored rectangle, with optional rounded corners
/// - Circle: Simple filled, colored circle
/// - Text: Draw text using the specified font, size, color, and position
2024-02-17 20:43:46 +00:00
#[derive(Clone, Debug, PartialEq)]
pub enum UiDrawCommand {
///Filled, colored rectangle
Rectangle {
///Position in pixels
position: Vec2,
///Size in pixels
size: Vec2,
///Color (RGBA)
2024-02-20 19:56:58 +00:00
color: Corners<Vec4>,
2024-02-19 02:41:48 +00:00
///Rounded corners
2024-02-19 18:40:18 +00:00
rounded_corners: Option<RoundedCorners>,
2024-02-17 20:43:46 +00:00
},
2024-02-20 17:19:10 +00:00
/// Filled, colored circle
2024-02-19 20:12:12 +00:00
Circle {
///Position in pixels
position: Vec2,
///Radius in pixels
radius: f32,
///Color (RGBA)
color: Vec4,
},
2024-02-20 17:19:10 +00:00
/// Draw text using the specified font, size, color, and position
2024-02-17 20:43:46 +00:00
Text {
///Position in pixels
position: Vec2,
///Font size
size: u8,
///Color (RGBA)
color: Vec4,
///Text to draw
text: Cow<'static, str>,
///Font handle to use
font: FontHandle,
},
}
2024-02-20 17:19:10 +00:00
/// List of draw commands
2024-02-17 20:43:46 +00:00
#[derive(Default)]
2024-02-19 20:12:12 +00:00
pub struct UiDrawCommandList {
2024-02-17 20:43:46 +00:00
pub commands: Vec<UiDrawCommand>,
}
2024-02-19 20:12:12 +00:00
impl UiDrawCommandList {
2024-02-20 17:19:10 +00:00
/// Add a draw command to the list
2024-02-17 20:43:46 +00:00
pub fn add(&mut self, command: UiDrawCommand) {
self.commands.push(command);
}
}
// impl UiDrawCommands {
// pub fn compare(&self, other: &Self) -> bool {
// // if self.commands.len() != other.commands.len() { return false }
// // self.commands.iter().zip(other.commands.iter()).all(|(a, b)| a == b)
// }
// }
2024-02-20 17:19:10 +00:00
/// Texture to bind for a draw call
///
/// - FontTexture: The internally managed font texture
/// - UserDefined: User-defined texture, value is user-defined and usually depends on the render backend
2024-02-17 20:43:46 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BindTexture {
2024-02-20 17:19:10 +00:00
/// The internally managed font texture
2024-02-17 20:43:46 +00:00
FontTexture,
2024-02-20 17:19:10 +00:00
/// User-defined texture, value is user-defined and usually depends on the render backend
UserDefined(usize),
2024-02-17 20:43:46 +00:00
}
2024-02-20 17:19:10 +00:00
/// A vertex for UI rendering
2024-02-19 04:36:38 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Default)]
2024-02-17 20:43:46 +00:00
pub struct UiVertex {
pub position: Vec2,
pub color: Vec4,
pub uv: Vec2,
}
2024-02-20 17:19:10 +00:00
/// Represents a single draw call, should be handled by the render backend
2024-02-17 20:43:46 +00:00
#[derive(Default)]
pub struct UiDrawCall {
pub vertices: Vec<UiVertex>,
pub indices: Vec<u32>,
}
2024-02-21 19:13:58 +00:00
impl UiDrawCall {
2024-02-20 17:19:10 +00:00
/// Tesselate the UI and build a complete draw plan from a list of draw commands
2024-02-19 20:12:12 +00:00
pub fn build(draw_commands: &UiDrawCommandList, tr: &mut TextRenderer) -> Self {
2024-02-21 19:13:58 +00:00
let mut draw_call = UiDrawCall::default();
2024-02-17 20:43:46 +00:00
for command in &draw_commands.commands {
match command {
2024-02-19 18:40:18 +00:00
UiDrawCommand::Rectangle { position, size, color, rounded_corners } => {
2024-02-21 19:13:58 +00:00
let vidx = draw_call.vertices.len() as u32;
2024-02-20 16:30:26 +00:00
if let Some(corner) = rounded_corners.filter(|x| x.radius.max_f32() > 0.0) {
2024-02-19 04:36:38 +00:00
//this code is stupid as fuck
//Random vert in the center for no reason
//lol
2024-02-21 19:13:58 +00:00
draw_call.vertices.push(UiVertex {
2024-02-19 04:36:38 +00:00
position: *position + *size * vec2(0.5, 0.5),
2024-02-20 19:56:58 +00:00
color: (color.bottom_left + color.bottom_right + color.top_left + color.top_right) / 4.,
2024-02-19 04:36:38 +00:00
uv: vec2(0., 0.),
});
2024-02-19 03:37:28 +00:00
2024-02-20 19:24:36 +00:00
//TODO: fix some corners tris being invisible (but it's already close enough lol)
2024-02-19 18:40:18 +00:00
let rounded_corner_verts = corner.point_count.get() as u32;
2024-02-19 03:37:28 +00:00
for i in 0..rounded_corner_verts {
let cratio = i as f32 / rounded_corner_verts as f32;
let angle = cratio * std::f32::consts::PI * 0.5;
let x = angle.sin();
let y = angle.cos();
//Top-right corner
2024-02-21 19:13:58 +00:00
draw_call.vertices.push(UiVertex {
2024-02-19 18:40:18 +00:00
position: *position + vec2(x, 1. - y) * corner.radius.top_right + vec2(size.x - corner.radius.top_right, 0.),
2024-02-20 19:56:58 +00:00
color: color.top_right,
2024-02-19 03:37:28 +00:00
uv: vec2(0.0, 0.0),
});
2024-02-19 04:36:38 +00:00
//Bottom-right corner
2024-02-21 19:13:58 +00:00
draw_call.vertices.push(UiVertex {
2024-02-19 18:40:18 +00:00
position: *position + vec2(x - 1., y) * corner.radius.bottom_right + vec2(size.x, size.y - corner.radius.bottom_right),
2024-02-20 19:56:58 +00:00
color: color.bottom_right,
2024-02-19 03:37:28 +00:00
uv: vec2(0.0, 0.0),
});
//Bottom-left corner
2024-02-21 19:13:58 +00:00
draw_call.vertices.push(UiVertex {
2024-02-19 18:40:18 +00:00
position: *position + vec2(1. - x, y) * corner.radius.bottom_left + vec2(0., size.y - corner.radius.bottom_left),
2024-02-20 19:56:58 +00:00
color: color.bottom_left,
2024-02-19 03:37:28 +00:00
uv: vec2(0.0, 0.0),
});
2024-02-19 04:36:38 +00:00
//Top-left corner
2024-02-21 19:13:58 +00:00
draw_call.vertices.push(UiVertex {
2024-02-19 18:40:18 +00:00
position: *position + vec2(1. - x, 1. - y) * corner.radius.top_left,
2024-02-20 19:56:58 +00:00
color: color.top_left,
2024-02-19 03:37:28 +00:00
uv: vec2(0.0, 0.0),
});
2024-02-19 04:36:38 +00:00
// mental illness:
if i > 0 {
2024-02-21 19:13:58 +00:00
draw_call.indices.extend([
2024-02-19 04:36:38 +00:00
//Top-right corner
vidx,
vidx + 1 + (i - 1) * 4,
vidx + 1 + i * 4,
//Bottom-right corner
vidx,
vidx + 1 + (i - 1) * 4 + 1,
vidx + 1 + i * 4 + 1,
//Bottom-left corner
vidx,
vidx + 1 + (i - 1) * 4 + 2,
vidx + 1 + i * 4 + 2,
//Top-left corner
vidx,
vidx + 1 + (i - 1) * 4 + 3,
vidx + 1 + i * 4 + 3,
]);
}
2024-02-19 03:37:28 +00:00
}
2024-02-19 04:36:38 +00:00
//Fill in the rest
//mental illness 2:
2024-02-21 19:13:58 +00:00
draw_call.indices.extend([
2024-02-19 04:46:43 +00:00
//Top
vidx,
2024-02-19 04:50:46 +00:00
vidx + 4,
2024-02-19 04:46:43 +00:00
vidx + 1,
//Right?, i think
vidx,
vidx + 1 + (rounded_corner_verts - 1) * 4,
vidx + 1 + (rounded_corner_verts - 1) * 4 + 1,
//Left???
vidx,
vidx + 1 + (rounded_corner_verts - 1) * 4 + 2,
vidx + 1 + (rounded_corner_verts - 1) * 4 + 3,
//Bottom???
vidx,
vidx + 3,
vidx + 2,
]);
2024-02-19 02:41:48 +00:00
} else {
2024-02-21 19:13:58 +00:00
draw_call.indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]);
draw_call.vertices.extend([
2024-02-19 02:41:48 +00:00
UiVertex {
position: *position,
2024-02-20 19:56:58 +00:00
color: color.top_left,
2024-02-19 02:41:48 +00:00
uv: vec2(0.0, 0.0),
},
UiVertex {
position: *position + vec2(size.x, 0.0),
2024-02-20 19:56:58 +00:00
color: color.top_right,
2024-02-19 02:41:48 +00:00
uv: vec2(1.0, 0.0),
},
UiVertex {
position: *position + *size,
2024-02-20 19:56:58 +00:00
color: color.bottom_right,
2024-02-19 02:41:48 +00:00
uv: vec2(1.0, 1.0),
},
UiVertex {
position: *position + vec2(0.0, size.y),
2024-02-20 19:56:58 +00:00
color: color.bottom_left,
2024-02-19 02:41:48 +00:00
uv: vec2(0.0, 1.0),
},
]);
}
2024-02-17 20:43:46 +00:00
},
2024-02-19 20:12:12 +00:00
UiDrawCommand::Circle { .. } => {
todo!("circle draw command not implemented yet")
},
2024-02-17 20:43:46 +00:00
UiDrawCommand::Text { position, size, color, text, font } => {
2024-02-20 16:30:26 +00:00
if text.is_empty() {
continue
}
2024-02-17 20:43:46 +00:00
//XXX: should we be doing this every time?
let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
layout.append(
&[tr.internal_font(*font)],
&TextStyle::new(text, *size as f32, 0)
);
let glyphs = layout.glyphs();
//let mut rpos_x = 0.;
for layout_glyph in glyphs {
if !layout_glyph.char_data.rasterize() {
continue
}
2024-02-20 18:59:39 +00:00
let font_texture_size = (
tr.font_texture().size.x as f32,
tr.font_texture().size.y as f32
);
2024-02-21 19:13:58 +00:00
let vidx = draw_call.vertices.len() as u32;
2024-02-17 20:43:46 +00:00
let glyph = tr.glyph(*font, layout_glyph.parent, layout_glyph.key.px as u8);
//rpos_x += glyph.metrics.advance_width;//glyph.metrics.advance_width;
2024-02-21 19:13:58 +00:00
draw_call.indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]);
2024-02-20 18:59:39 +00:00
let p0x = glyph.position.x as f32 / font_texture_size.0;
let p1x = (glyph.position.x + glyph.size.x as i32) as f32 / font_texture_size.0;
let p0y = glyph.position.y as f32 / font_texture_size.1;
let p1y = (glyph.position.y + glyph.size.y as i32) as f32 / font_texture_size.1;
2024-02-21 19:13:58 +00:00
draw_call.vertices.extend([
2024-02-17 20:43:46 +00:00
UiVertex {
position: *position + vec2(layout_glyph.x, layout_glyph.y),
color: *color,
uv: vec2(p0x, p0y),
},
UiVertex {
position: *position + vec2(layout_glyph.x + glyph.metrics.width as f32, layout_glyph.y),
color: *color,
uv: vec2(p1x, p0y),
},
UiVertex {
position: *position + vec2(layout_glyph.x + glyph.metrics.width as f32, layout_glyph.y + glyph.metrics.height as f32),
color: *color,
uv: vec2(p1x, p1y),
},
UiVertex {
position: *position + vec2(layout_glyph.x, layout_glyph.y + glyph.metrics.height as f32),
color: *color,
uv: vec2(p0x, p1y),
},
]);
2024-02-20 16:30:26 +00:00
#[cfg(all(
feature = "pixel_perfect_text",
not(feature = "pixel_perfect")
))] {
2024-02-21 19:13:58 +00:00
for vtx in &mut draw_call.vertices[(vidx as usize)..] {
2024-02-20 16:30:26 +00:00
vtx.position = vtx.position.round()
}
}
2024-02-17 20:43:46 +00:00
}
}
}
}
2024-02-21 19:13:58 +00:00
#[cfg(feature = "pixel_perfect")]
draw_call.vertices.iter_mut().for_each(|v| {
v.position = v.position.round()
});
draw_call
2024-02-17 20:43:46 +00:00
}
}
2024-02-24 22:32:09 +00:00
#[allow(deprecated)]
2024-02-21 19:13:58 +00:00
impl IfModified<UiDrawCall> for (bool, &UiDrawCall) {
fn if_modified(&self) -> Option<&UiDrawCall> {
2024-02-17 20:43:46 +00:00
match self.0 {
true => Some(self.1),
false => None,
}
}
}