kubi/kubi-ui/src/draw.rs

221 lines
5.9 KiB
Rust
Raw Normal View History

use crate::{IfModified, text::{TextRenderer, FontHandle}};
2023-11-25 18:47:48 -06:00
use std::borrow::Cow;
2023-12-02 12:27:21 -06:00
use fontdue::layout::{Layout, CoordinateSystem, TextStyle};
2023-11-23 10:14:02 -06:00
use glam::{Vec2, Vec4, vec2};
2023-11-21 07:08:22 -06:00
2023-11-25 18:47:48 -06:00
#[derive(Clone, Debug, PartialEq)]
2023-11-22 04:56:46 -06:00
pub enum UiDrawCommand {
2023-11-21 07:08:22 -06:00
///Filled, colored rectangle
Rectangle {
///Position in pixels
position: Vec2,
///Size in pixels
size: Vec2,
///Color (RGBA)
color: Vec4,
2023-11-25 18:47:48 -06:00
},
Text {
///Position in pixels
position: Vec2,
///Font size
size: u8,
///Color (RGBA)
color: Vec4,
///Text to draw
text: Cow<'static, str>,
2023-12-02 10:47:25 -06:00
///Font handle to use
font: FontHandle,
2023-11-25 18:47:48 -06:00
},
2023-11-21 07:08:22 -06:00
}
2023-11-22 04:56:46 -06:00
#[derive(Default)]
pub struct UiDrawCommands {
pub commands: Vec<UiDrawCommand>,
2023-11-21 07:08:22 -06:00
}
impl UiDrawCommands {
pub fn add(&mut self, command: UiDrawCommand) {
self.commands.push(command);
}
}
2023-11-22 04:56:46 -06:00
// 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)
// }
// }
2023-11-30 17:54:32 -06:00
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2023-11-25 18:47:48 -06:00
pub enum BindTexture {
FontTexture,
//UserDefined(usize),
}
2023-11-22 04:56:46 -06:00
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct UiVertex {
pub position: Vec2,
pub color: Vec4,
2023-11-23 10:14:02 -06:00
pub uv: Vec2,
2023-11-22 04:56:46 -06:00
}
#[derive(Default)]
2023-11-22 13:26:44 -06:00
pub struct UiDrawCall {
2023-11-22 04:56:46 -06:00
pub vertices: Vec<UiVertex>,
pub indices: Vec<u32>,
2023-12-01 14:48:17 -06:00
pub bind_texture: Option<BindTexture>,
2023-11-22 04:56:46 -06:00
}
2023-11-21 07:08:22 -06:00
2023-11-22 13:26:44 -06:00
#[derive(Default)]
pub struct UiDrawPlan {
pub calls: Vec<UiDrawCall>
}
struct CallSwapper {
calls: Vec<UiDrawCall>,
call: UiDrawCall,
}
impl CallSwapper {
pub fn new() -> Self {
Self {
calls: vec![],
call: UiDrawCall::default(),
}
}
pub fn current(&self) -> &UiDrawCall {
&self.call
}
pub fn current_mut(&mut self) -> &mut UiDrawCall {
&mut self.call
}
pub fn swap(&mut self) {
self.calls.push(std::mem::replace(&mut self.call, UiDrawCall::default()));
}
pub fn finish(mut self) -> Vec<UiDrawCall> {
self.calls.push(self.call);
self.calls
}
}
2023-11-22 04:56:46 -06:00
impl UiDrawPlan {
pub fn build(draw_commands: &UiDrawCommands, tr: &mut TextRenderer) -> Self {
let mut swapper = CallSwapper::new();
let mut prev_command = None;
for command in &draw_commands.commands {
let do_swap = if let Some(prev_command) = prev_command {
std::mem::discriminant(prev_command) != std::mem::discriminant(command)
} else {
false
};
if do_swap {
swapper.swap();
}
if do_swap || prev_command.is_none() {
match command {
UiDrawCommand::Rectangle { .. } => (),
UiDrawCommand::Text { .. } => {
swapper.current_mut().bind_texture = Some(BindTexture::FontTexture);
}
}
}
2023-11-22 13:26:44 -06:00
match command {
2023-11-22 04:56:46 -06:00
UiDrawCommand::Rectangle { position, size, color } => {
2023-12-02 10:16:45 -06:00
let vidx = swapper.current().vertices.len() as u32;
swapper.current_mut().indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]);
swapper.current_mut().vertices.extend([
2023-11-22 04:56:46 -06:00
UiVertex {
position: *position,
color: *color,
2023-11-23 10:14:02 -06:00
uv: vec2(0.0, 0.0),
2023-11-22 04:56:46 -06:00
},
UiVertex {
position: *position + vec2(size.x, 0.0),
2023-11-22 04:56:46 -06:00
color: *color,
2023-11-23 10:14:02 -06:00
uv: vec2(1.0, 0.0),
2023-11-22 04:56:46 -06:00
},
UiVertex {
position: *position + *size,
color: *color,
2023-11-23 10:14:02 -06:00
uv: vec2(1.0, 1.0),
2023-11-22 04:56:46 -06:00
},
UiVertex {
position: *position + vec2(0.0, size.y),
2023-11-22 04:56:46 -06:00
color: *color,
2023-11-23 10:14:02 -06:00
uv: vec2(0.0, 1.0),
2023-11-22 04:56:46 -06:00
},
]);
2023-11-25 18:47:48 -06:00
},
2023-12-02 10:47:25 -06:00
UiDrawCommand::Text { position, size, color, text, font } => {
2023-12-02 12:27:21 -06:00
//XXX: should we be doing this every time?
let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
layout.append(
&[tr.internal_font(*font)],
2023-12-02 12:46:08 -06:00
&TextStyle::new(text, *size as f32, 0)
2023-12-02 12:27:21 -06:00
);
let glyphs = layout.glyphs();
//let mut rpos_x = 0.;
for layout_glyph in glyphs {
2023-12-02 12:46:08 -06:00
if !layout_glyph.char_data.rasterize() {
continue
}
2023-12-02 10:16:45 -06:00
let vidx = swapper.current().vertices.len() as u32;
2023-12-02 12:27:21 -06: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;
2023-12-02 10:16:45 -06:00
swapper.current_mut().indices.extend([vidx, vidx + 1, vidx + 2, vidx, vidx + 2, vidx + 3]);
let p0x = glyph.position.x as f32 / 1024.;
let p1x = (glyph.position.x + glyph.size.x as i32) as f32 / 1024.;
let p0y = glyph.position.y as f32 / 1024.;
let p1y = (glyph.position.y + glyph.size.y as i32) as f32 / 1024.;
swapper.current_mut().vertices.extend([
UiVertex {
2023-12-02 12:27:21 -06:00
position: *position + vec2(layout_glyph.x, layout_glyph.y),
2023-12-02 10:16:45 -06:00
color: *color,
uv: vec2(p0x, p0y),
},
UiVertex {
2023-12-02 12:27:21 -06:00
position: *position + vec2(layout_glyph.x + glyph.metrics.width as f32, layout_glyph.y),
2023-12-02 10:16:45 -06:00
color: *color,
uv: vec2(p1x, p0y),
},
UiVertex {
2023-12-02 12:27:21 -06:00
position: *position + vec2(layout_glyph.x + glyph.metrics.width as f32, layout_glyph.y + glyph.metrics.height as f32),
2023-12-02 10:16:45 -06:00
color: *color,
uv: vec2(p1x, p1y),
},
UiVertex {
2023-12-02 12:27:21 -06:00
position: *position + vec2(layout_glyph.x, layout_glyph.y + glyph.metrics.height as f32),
2023-12-02 10:16:45 -06:00
color: *color,
uv: vec2(p0x, p1y),
},
]);
}
2023-11-22 04:56:46 -06:00
}
}
prev_command = Some(command);
2023-11-22 04:56:46 -06:00
}
2023-11-22 13:26:44 -06:00
Self {
calls: swapper.finish()
2023-11-22 13:26:44 -06:00
}
2023-11-22 04:56:46 -06:00
}
2023-11-21 07:08:22 -06:00
}
impl IfModified<UiDrawPlan> for (bool, &UiDrawPlan) {
fn if_modified(&self) -> Option<&UiDrawPlan> {
match self.0 {
true => Some(self.1),
false => None,
}
}
}