Compare commits

..

5 commits

Author SHA1 Message Date
griffi-gh cc32082c21 wip 2023-11-23 23:13:17 +01:00
griffi-gh f8cd34443a wip 2023-11-23 21:27:11 +01:00
griffi-gh f56c7a4b8f build 2023-11-23 18:01:18 +01:00
griffi-gh cf8263fee0 wip texture managment 2023-11-23 17:59:14 +01:00
griffi-gh b17519cbc3 wip text render, minor changes 2023-11-23 17:14:02 +01:00
11 changed files with 227 additions and 42 deletions

60
Cargo.lock generated
View file

@ -112,15 +112,6 @@ version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" 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]] [[package]]
name = "arrayref" name = "arrayref"
version = "0.3.7" version = "0.3.7"
@ -550,6 +541,17 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "fontdue"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0793f5137567643cf65ea42043a538804ff0fbf288649e2141442b602d81f9bc"
dependencies = [
"hashbrown 0.13.2",
"rayon",
"ttf-parser 0.15.2",
]
[[package]] [[package]]
name = "foreign-types" name = "foreign-types"
version = "0.5.0" version = "0.5.0"
@ -670,7 +672,6 @@ version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5418c17512bdf42730f9032c74e1ae39afc408745ebb2acf72fbc4691c17945" checksum = "b5418c17512bdf42730f9032c74e1ae39afc408745ebb2acf72fbc4691c17945"
dependencies = [ dependencies = [
"approx",
"serde", "serde",
] ]
@ -766,6 +767,16 @@ dependencies = [
"byteorder", "byteorder",
] ]
[[package]]
name = "hashbrown"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
dependencies = [
"ahash",
"rayon",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.2" version = "0.14.2"
@ -841,7 +852,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown 0.14.2",
] ]
[[package]] [[package]]
@ -949,7 +960,7 @@ dependencies = [
"glium", "glium",
"glutin", "glutin",
"glutin-winit", "glutin-winit",
"hashbrown", "hashbrown 0.14.2",
"image", "image",
"kubi-logging", "kubi-logging",
"kubi-shared", "kubi-shared",
@ -991,7 +1002,7 @@ dependencies = [
"anyhow", "anyhow",
"flume", "flume",
"glam", "glam",
"hashbrown", "hashbrown 0.14.2",
"kubi-logging", "kubi-logging",
"kubi-shared", "kubi-shared",
"log", "log",
@ -1014,7 +1025,7 @@ dependencies = [
"bincode", "bincode",
"bracket-noise", "bracket-noise",
"glam", "glam",
"hashbrown", "hashbrown 0.14.2",
"nohash-hasher", "nohash-hasher",
"num_enum", "num_enum",
"postcard", "postcard",
@ -1030,12 +1041,15 @@ dependencies = [
name = "kubi-ui" name = "kubi-ui"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"fontdue",
"glam", "glam",
"glium", "glium",
"hashbrown", "hashbrown 0.14.2",
"kubi-logging", "kubi-logging",
"log", "log",
"nohash-hasher", "nohash-hasher",
"rayon",
"rect_packer",
"winit", "winit",
] ]
@ -1366,7 +1380,7 @@ version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4586edfe4c648c71797a74c84bacb32b52b212eff5dfe2bb9f2c599844023e7" checksum = "d4586edfe4c648c71797a74c84bacb32b52b212eff5dfe2bb9f2c599844023e7"
dependencies = [ dependencies = [
"ttf-parser", "ttf-parser 0.20.0",
] ]
[[package]] [[package]]
@ -1559,6 +1573,12 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "rect_packer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8ffb4dfda4b01cc420847665dc480760d596ce186f2772a66ed32fe9acb1c45"
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.3.5" version = "0.3.5"
@ -1742,7 +1762,7 @@ name = "shipyard"
version = "0.6.0" version = "0.6.0"
source = "git+https://github.com/leudz/shipyard?rev=8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49#8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49" source = "git+https://github.com/leudz/shipyard?rev=8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49#8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49"
dependencies = [ dependencies = [
"hashbrown", "hashbrown 0.14.2",
"lock_api", "lock_api",
"rayon", "rayon",
"serde", "serde",
@ -2011,6 +2031,12 @@ version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
[[package]]
name = "ttf-parser"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd"
[[package]] [[package]]
name = "ttf-parser" name = "ttf-parser"
version = "0.20.0" version = "0.20.0"

View file

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

View file

@ -0,0 +1,7 @@
Copyright (c) 2004, 2005 Tristan Grimmer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Binary file not shown.

View file

@ -0,0 +1,87 @@
use std::time::Instant;
use glam::{Vec2, IVec2, UVec2};
use glium::{backend::glutin::SimpleWindowBuilder, Surface};
use winit::{
event::{Event, WindowEvent},
event_loop::{EventLoopBuilder, ControlFlow}
};
use kubi_ui::{
KubiUi,
backend::glium::GliumUiRenderer, element::{progress_bar::ProgressBar, container::{Container, Sides}, UiElement}, UiSize
};
fn main() {
kubi_logging::init();
let event_loop = EventLoopBuilder::new().build().unwrap();
let (window, display) = SimpleWindowBuilder::new().build(&event_loop);
let mut kui = KubiUi::new();
let mut backend = GliumUiRenderer::new(&display);
let instant = Instant::now();
let mut pcnt = 0;
event_loop.run(|event, window_target| {
window_target.set_control_flow(ControlFlow::Poll);
match event {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
window_target.exit();
},
Event::AboutToWait => {
let mut frame = display.draw();
frame.clear_color_srgb(0.5, 0.5, 0.5, 0.);
let resolution = UVec2::from(display.get_framebuffer_dimensions()).as_vec2();
kui.begin();
kui.add(Container {
gap: 5.,
padding: Sides::all(5.),
elements: vec![
Box::new(ProgressBar {
value: 0.5,
..Default::default()
}),
Box::new(ProgressBar {
value: instant.elapsed().as_secs_f32().sin().powi(2),
..Default::default()
}),
Box::new(Container {
gap: 1.,
elements: {
let mut elements: Vec<Box<dyn UiElement>> = vec![];
let cnt = instant.elapsed().as_secs() * 10000;
if pcnt != cnt {
log::info!("{cnt}");
pcnt = cnt;
}
for i in 0..cnt {
elements.push(Box::new(ProgressBar {
value: (instant.elapsed().as_secs_f32() + (i as f32 / 10.)).sin().powi(2),
size: (UiSize::Auto, UiSize::Pixels(5.)),
..Default::default()
}));
}
elements
},
..Default::default()
})
],
..Default::default()
}, resolution);
kui.end();
let plan = kui.draw_plan();
if plan.0 {
backend.update(plan.1);
}
backend.draw(&mut frame, resolution);
frame.finish().unwrap();
}
_ => (),
}
}).unwrap();
}

View file

@ -20,7 +20,6 @@ fn main() {
let mut backend = GliumUiRenderer::new(&display); let mut backend = GliumUiRenderer::new(&display);
let instant = Instant::now(); let instant = Instant::now();
event_loop.run(|event, window_target| { event_loop.run(|event, window_target| {
window_target.set_control_flow(ControlFlow::Poll); window_target.set_control_flow(ControlFlow::Poll);
match event { match event {
@ -47,21 +46,6 @@ fn main() {
value: instant.elapsed().as_secs_f32().sin().powi(2), value: instant.elapsed().as_secs_f32().sin().powi(2),
..Default::default() ..Default::default()
}), }),
Box::new(Container {
gap: 1.,
elements: {
let mut elements: Vec<Box<dyn UiElement>> = vec![];
for i in 0..10000 {
elements.push(Box::new(ProgressBar {
value: (instant.elapsed().as_secs_f32() + (i as f32 / 10.)).sin().powi(2),
size: (UiSize::Auto, UiSize::Pixels(5.)),
..Default::default()
}));
}
elements
},
..Default::default()
})
], ],
..Default::default() ..Default::default()
}, resolution); }, resolution);

View file

@ -13,9 +13,11 @@ const VERTEX_SHADER: &str = include_str!("../../shaders/vertex.vert");
const FRAGMENT_SHADER: &str = include_str!("../../shaders/fragment.frag"); const FRAGMENT_SHADER: &str = include_str!("../../shaders/fragment.frag");
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
#[repr(C)]
struct Vertex { struct Vertex {
position: [f32; 2], position: [f32; 2],
color: [f32; 4], color: [f32; 4],
uv: [f32; 2],
} }
impl From<UiVertex> for Vertex { impl From<UiVertex> for Vertex {
@ -23,11 +25,12 @@ impl From<UiVertex> for Vertex {
Self { Self {
position: v.position.to_array(), position: v.position.to_array(),
color: v.color.to_array(), color: v.color.to_array(),
uv: v.uv.to_array(),
} }
} }
} }
implement_vertex!(Vertex, position, color); implement_vertex!(Vertex, position, color, uv);
struct BufferPair { struct BufferPair {
vertex_buffer: glium::VertexBuffer<Vertex>, vertex_buffer: glium::VertexBuffer<Vertex>,
@ -73,7 +76,7 @@ impl BufferPair {
} }
pub fn write_data(&mut self, vtx: &[Vertex], idx: &[u32]) { pub fn write_data(&mut self, vtx: &[Vertex], idx: &[u32]) {
log::debug!("uploading {} vertices and {} indices", vtx.len(), idx.len()); //log::trace!("uploading {} vertices and {} indices", vtx.len(), idx.len());
self.vertex_count = vtx.len(); self.vertex_count = vtx.len();
self.index_count = idx.len(); self.index_count = idx.len();

View file

@ -1,5 +1,4 @@
use std::num::NonZeroU16; use glam::{Vec2, Vec4, vec2};
use glam::{Vec2, Vec4};
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub enum UiDrawCommand { pub enum UiDrawCommand {
@ -30,6 +29,7 @@ pub struct UiDrawCommands {
pub struct UiVertex { pub struct UiVertex {
pub position: Vec2, pub position: Vec2,
pub color: Vec4, pub color: Vec4,
pub uv: Vec2,
//pub texture: Option<NonZeroU16>, //pub texture: Option<NonZeroU16>,
} }
@ -56,18 +56,22 @@ impl UiDrawPlan {
UiVertex { UiVertex {
position: *position, position: *position,
color: *color, color: *color,
uv: vec2(0.0, 0.0),
}, },
UiVertex { UiVertex {
position: *position + Vec2::new(size.x, 0.0), position: *position + Vec2::new(size.x, 0.0),
color: *color, color: *color,
uv: vec2(1.0, 0.0),
}, },
UiVertex { UiVertex {
position: *position + *size, position: *position + *size,
color: *color, color: *color,
uv: vec2(1.0, 1.0),
}, },
UiVertex { UiVertex {
position: *position + Vec2::new(0.0, size.y), position: *position + Vec2::new(0.0, size.y),
color: *color, color: *color,
uv: vec2(0.0, 1.0),
}, },
]); ]);
} }

View file

@ -6,7 +6,7 @@ use crate::{
state::StateRepo state::StateRepo
}; };
#[cfg(feature = "builtin")] #[cfg(feature = "builtin_elements")]
mod builtin { mod builtin {
pub mod rect; pub mod rect;
pub mod container; pub mod container;
@ -14,7 +14,7 @@ mod builtin {
pub mod progress_bar; pub mod progress_bar;
} }
#[cfg(feature = "builtin")] #[cfg(feature = "builtin_elements")]
pub use builtin::*; pub use builtin::*;
pub trait UiElement { pub trait UiElement {

View file

@ -7,11 +7,19 @@ pub mod draw;
pub mod backend; pub mod backend;
pub mod measure; pub mod measure;
pub mod state; pub mod state;
pub mod text;
use element::UiElement; use element::UiElement;
use state::StateRepo; use state::StateRepo;
use event::UiEvent; use event::UiEvent;
use draw::{UiDrawCommands, UiDrawPlan}; 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 { pub struct KubiUi {
mouse_position: Vec2, mouse_position: Vec2,
@ -21,6 +29,7 @@ pub struct KubiUi {
draw_commands: UiDrawCommands, draw_commands: UiDrawCommands,
draw_plan: UiDrawPlan, draw_plan: UiDrawPlan,
draw_plan_modified: bool, draw_plan_modified: bool,
font_renderer: TextRenderer,
} }
impl KubiUi { impl KubiUi {
@ -34,6 +43,7 @@ impl KubiUi {
draw_commands: UiDrawCommands::default(), draw_commands: UiDrawCommands::default(),
draw_plan: UiDrawPlan::default(), draw_plan: UiDrawPlan::default(),
draw_plan_modified: false, draw_plan_modified: false,
font_renderer: TextRenderer::new(),
} }
} }

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()
}
}