This commit is contained in:
griffi-gh 2023-11-23 23:13:17 +01:00
parent b893193753
commit c986e36f88
6 changed files with 77 additions and 75 deletions

3
Cargo.lock generated
View file

@ -548,6 +548,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0793f5137567643cf65ea42043a538804ff0fbf288649e2141442b602d81f9bc"
dependencies = [
"hashbrown 0.13.2",
"rayon",
"ttf-parser 0.15.2",
]
@ -773,6 +774,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
dependencies = [
"ahash",
"rayon",
]
[[package]]
@ -1046,6 +1048,7 @@ dependencies = [
"kubi-logging",
"log",
"nohash-hasher",
"rayon",
"rect_packer",
"winit",
]

View file

@ -9,9 +9,10 @@ hashbrown = "0.14"
nohash-hasher = "0.2"
glam = "0.24"
glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf", optional = true }
fontdue = { version = "0.7", optional = true }
fontdue = "0.7"
rect_packer = { version = "0.2.1", optional = true }
log = "0.4"
rayon = { version = "1.8", optional = true }
[dev-dependencies]
kubi-logging = { path = "../kubi-logging" }
@ -19,9 +20,8 @@ glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf" }
winit = "0.29"
[features]
default = ["builtin_elements", "texture_manager", "text_rendering", "builtin_font", "backend_glium"]
default = ["builtin_elements", "builtin_font", "backend_glium", "parallel"]
backend_glium = ["dep:glium"]
texture_manager = ["dep:rect_packer"]
text_rendering = ["dep:fontdue", "texture_manager"]
builtin_font = ["text_rendering"]
builtin_font = []
builtin_elements = []
parallel = ["dep:rayon", "fontdue/parallel"]

View file

@ -1,9 +0,0 @@
use fontdue::Font;
#[cfg(feature = "builtin_font")]
const BIN_FONT: &[u8] = include_bytes!("../assets/font/ProggyTiny.ttf");
pub struct FontRenderer {
pub font_stack: Vec<Font>,
pub cache: ()
}

View file

@ -7,15 +7,19 @@ pub mod draw;
pub mod backend;
pub mod measure;
pub mod state;
#[cfg(feature = "text_rendering")]
pub mod font;
#[cfg(feature = "texture_manager")]
pub mod texman;
pub mod text;
use element::UiElement;
use state::StateRepo;
use event::UiEvent;
use draw::{UiDrawCommands, UiDrawPlan};
use text::TextRenderer;
pub struct ElementContext<'a> {
pub state: &'a mut StateRepo,
pub draw: &'a mut UiDrawCommands,
pub text: &'a mut TextRenderer,
}
pub struct KubiUi {
mouse_position: Vec2,
@ -25,6 +29,7 @@ pub struct KubiUi {
draw_commands: UiDrawCommands,
draw_plan: UiDrawPlan,
draw_plan_modified: bool,
font_renderer: TextRenderer,
}
impl KubiUi {
@ -38,6 +43,7 @@ impl KubiUi {
draw_commands: UiDrawCommands::default(),
draw_plan: UiDrawPlan::default(),
draw_plan_modified: false,
font_renderer: TextRenderer::new(),
}
}

View file

@ -1,57 +0,0 @@
use rect_packer::{Packer, Config as PackerConfig};
use glam::{IVec2, ivec2};
#[derive(Clone, Copy)]
pub struct TextureAllocation {
// pub index: usize,
pub position: IVec2,
pub size: IVec2,
in_texture_size: IVec2,
}
impl TextureAllocation {
pub fn uv(&self) -> (f32, f32, f32, f32) {
let p0 = self.position.as_vec2() / self.in_texture_size.as_vec2();
let p1 = (self.position + self.size).as_vec2() / self.in_texture_size.as_vec2();
(p0.x, p0.y, p1.x, p1.y)
}
}
pub struct TextureSpace {
size: IVec2,
packer: Packer,
// allocations: Vec<TextureAllocation>,
}
impl TextureSpace {
pub fn new(size: IVec2) -> Self {
Self {
size,
packer: Packer::new(PackerConfig {
width: size.x,
height: size.y,
border_padding: 1,
rectangle_padding: 1,
}),
// allocations: Vec::new(),
}
}
pub fn size(&self) -> IVec2 {
self.size
}
pub fn allocate(&mut self, size: IVec2) -> Option<TextureAllocation> {
let position = self.packer.pack(size.x, size.y, false)
.map(|rect| ivec2(rect.x, rect.y))?;
Some(TextureAllocation {
position,
size,
in_texture_size: self.size
})
}
// pub fn lookup(&self, index: usize) -> Option<TextureAllocation> {
// self.allocations.get(index).copied()
// }
}

59
kubi-ui/src/text.rs Normal file
View file

@ -0,0 +1,59 @@
use fontdue::{Font, Metrics};
use hashbrown::HashMap;
use nohash_hasher::BuildNoHashHasher;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
#[cfg(feature = "builtin_font")]
const BIN_FONT: &[u8] = include_bytes!("../assets/font/ProggyTiny.ttf");
// pub struct Glyph {
// pub metrics: Metrics,
// pub data: Box<[u8]>
// }
// pub struct FontData {
// font: Font,
// cache: HashMap<(u8, char), Glyph, BuildNoHashHasher<u8>>
// }
// impl FontData {
// pub fn new() -> Self {
// FontData {
// font: Font::from_bytes(BIN_FONT, fontdue::FontSettings::default()).unwrap(),
// cache: HashMap::default()
// }
// }
// fn prebake(&mut self, size: u8) {
// self.cache = (33..=126).par_bridge().map(|c| {
// let (metrics, data) = self.font.rasterize(
// char::from(c),
// c as f32,
// );
// Glyph { metrics, data: data.into_boxed_slice() }
// }).collect();
// }
// }
pub struct TextRenderer {
font_stack: Vec<Font>,
cache: ()
}
impl TextRenderer {
pub fn new() -> Self {
let font = Font::from_bytes(BIN_FONT, fontdue::FontSettings::default()).unwrap();
TextRenderer {
font_stack: vec![font],
cache: ()
}
}
}
impl Default for TextRenderer {
fn default() -> Self {
Self::new()
}
}