wip text render, minor changes

This commit is contained in:
griffi-gh 2023-11-23 17:14:02 +01:00
parent 3231a292ad
commit a18537a915
11 changed files with 155 additions and 31 deletions

40
Cargo.lock generated
View file

@ -550,6 +550,16 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
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",
"ttf-parser 0.15.2",
]
[[package]]
name = "foreign-types"
version = "0.5.0"
@ -766,6 +776,15 @@ dependencies = [
"byteorder",
]
[[package]]
name = "hashbrown"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
dependencies = [
"ahash",
]
[[package]]
name = "hashbrown"
version = "0.14.2"
@ -841,7 +860,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [
"equivalent",
"hashbrown",
"hashbrown 0.14.2",
]
[[package]]
@ -949,7 +968,7 @@ dependencies = [
"glium",
"glutin",
"glutin-winit",
"hashbrown",
"hashbrown 0.14.2",
"image",
"kubi-logging",
"kubi-shared",
@ -991,7 +1010,7 @@ dependencies = [
"anyhow",
"flume",
"glam",
"hashbrown",
"hashbrown 0.14.2",
"kubi-logging",
"kubi-shared",
"log",
@ -1014,7 +1033,7 @@ dependencies = [
"bincode",
"bracket-noise",
"glam",
"hashbrown",
"hashbrown 0.14.2",
"nohash-hasher",
"num_enum",
"postcard",
@ -1030,9 +1049,10 @@ dependencies = [
name = "kubi-ui"
version = "0.0.0"
dependencies = [
"fontdue",
"glam",
"glium",
"hashbrown",
"hashbrown 0.14.2",
"kubi-logging",
"log",
"nohash-hasher",
@ -1366,7 +1386,7 @@ version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4586edfe4c648c71797a74c84bacb32b52b212eff5dfe2bb9f2c599844023e7"
dependencies = [
"ttf-parser",
"ttf-parser 0.20.0",
]
[[package]]
@ -1742,7 +1762,7 @@ name = "shipyard"
version = "0.6.0"
source = "git+https://github.com/leudz/shipyard?rev=8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49#8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49"
dependencies = [
"hashbrown",
"hashbrown 0.14.2",
"lock_api",
"rayon",
"serde",
@ -2011,6 +2031,12 @@ version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
[[package]]
name = "ttf-parser"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd"
[[package]]
name = "ttf-parser"
version = "0.20.0"

View file

@ -9,6 +9,7 @@ hashbrown = "0.14"
nohash-hasher = "0.2"
glam = { version = "0.24", features = ["approx"] }
glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf", optional = true }
fontdue = { version = "0.7", optional = true }
log = "0.4"
[dev-dependencies]
@ -17,6 +18,8 @@ glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf" }
winit = "0.29"
[features]
default = ["backend_glium", "builtin"]
default = ["builtin_elements", "text_rendering", "builtin_font", "backend_glium"]
backend_glium = ["dep:glium"]
builtin = []
text_rendering = ["dep:fontdue"]
builtin_font = ["text_rendering"]
builtin_elements = []

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 instant = Instant::now();
event_loop.run(|event, window_target| {
window_target.set_control_flow(ControlFlow::Poll);
match event {
@ -47,21 +46,6 @@ fn main() {
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![];
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()
}, 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");
#[derive(Clone, Copy)]
#[repr(C)]
struct Vertex {
position: [f32; 2],
color: [f32; 4],
uv: [f32; 2],
}
impl From<UiVertex> for Vertex {
@ -23,11 +25,12 @@ impl From<UiVertex> for Vertex {
Self {
position: v.position.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 {
vertex_buffer: glium::VertexBuffer<Vertex>,
@ -73,7 +76,7 @@ impl BufferPair {
}
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.index_count = idx.len();

View file

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

View file

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

9
kubi-ui/src/font.rs Normal file
View file

@ -0,0 +1,9 @@
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,6 +7,7 @@ pub mod draw;
pub mod backend;
pub mod measure;
pub mod state;
pub mod font;
use element::UiElement;
use state::StateRepo;