From b8931937534974710b87952ec648f50144a0c279 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 23 Nov 2023 21:27:11 +0100 Subject: [PATCH] wip --- Cargo.lock | 10 -------- kubi-ui/Cargo.toml | 4 ++-- kubi-ui/src/texman.rs | 56 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd6ec0c..f473484 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -112,15 +112,6 @@ version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" -[[package]] -name = "approx" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" -dependencies = [ - "num-traits", -] - [[package]] name = "arrayref" version = "0.3.7" @@ -680,7 +671,6 @@ version = "0.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5418c17512bdf42730f9032c74e1ae39afc408745ebb2acf72fbc4691c17945" dependencies = [ - "approx", "serde", ] diff --git a/kubi-ui/Cargo.toml b/kubi-ui/Cargo.toml index aded733..719428a 100644 --- a/kubi-ui/Cargo.toml +++ b/kubi-ui/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] hashbrown = "0.14" nohash-hasher = "0.2" -glam = { version = "0.24", features = ["approx"] } +glam = "0.24" glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf", optional = true } fontdue = { version = "0.7", optional = true } rect_packer = { version = "0.2.1", optional = true } @@ -21,7 +21,7 @@ winit = "0.29" [features] default = ["builtin_elements", "texture_manager", "text_rendering", "builtin_font", "backend_glium"] backend_glium = ["dep:glium"] -texture_manager = [] +texture_manager = ["dep:rect_packer"] text_rendering = ["dep:fontdue", "texture_manager"] builtin_font = ["text_rendering"] builtin_elements = [] diff --git a/kubi-ui/src/texman.rs b/kubi-ui/src/texman.rs index 69761a2..44cdb52 100644 --- a/kubi-ui/src/texman.rs +++ b/kubi-ui/src/texman.rs @@ -1,3 +1,57 @@ -struct TextureSpace { +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, +} + +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 { + 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 { + // self.allocations.get(index).copied() + // } }