Compare commits

...

6 commits

Author SHA1 Message Date
griffi-gh 0b210eac5d use immut. buffers for primitives 2023-11-23 11:43:48 +01:00
griffi-gh 0fa723fc00 fix padding 2023-11-23 11:26:49 +01:00
griffi-gh a95eb96843 . 2023-11-23 11:11:04 +01:00
griffi-gh 0f4264292c restructure stuff 2023-11-23 11:10:42 +01:00
griffi-gh 4e47901117 add test 2023-11-23 11:10:35 +01:00
griffi-gh 447a848009 add traits 2023-11-23 10:53:44 +01:00
12 changed files with 159 additions and 41 deletions

2
Cargo.lock generated
View file

@ -1033,8 +1033,10 @@ dependencies = [
"glam",
"glium",
"hashbrown",
"kubi-logging",
"log",
"nohash-hasher",
"winit",
]
[[package]]

View file

@ -12,9 +12,11 @@ glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf", optiona
log = "0.4"
[dev-dependencies]
kubi-logging = { path = "../kubi-logging" }
glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf" }
winit = "0.29"
[features]
default = ["backend_glium", "builtin_elements"]
default = ["backend_glium", "builtin"]
backend_glium = ["dep:glium"]
builtin_elements = []
builtin = []

View file

@ -1,3 +1,82 @@
pub fn main() {
//TODO ui demo
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();
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![];
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);
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

@ -48,8 +48,9 @@ impl BufferPair {
}
pub fn ensure_buffer_size(&mut self, need_vtx: usize, need_idx: usize) {
let current_vtx_size = self.vertex_buffer.get_size();
let current_idx_size = self.index_buffer.get_size();
let current_vtx_size = self.vertex_buffer.get_size() / std::mem::size_of::<Vertex>();
let current_idx_size = self.index_buffer.get_size() / std::mem::size_of::<u32>();
//log::debug!("current vtx size: {}, current idx size: {}", current_vtx_size, current_idx_size);
if current_vtx_size >= need_vtx && current_idx_size >= need_idx {
return
}
@ -84,9 +85,10 @@ impl BufferPair {
return
}
self.ensure_buffer_size(vtx.len(), idx.len());
self.vertex_buffer.slice_mut(0..vtx.len()).unwrap().write(vtx);
self.index_buffer.slice_mut(0..idx.len()).unwrap().write(idx);
self.ensure_buffer_size(self.vertex_count, self.index_count);
self.vertex_buffer.slice_mut(0..self.vertex_count).unwrap().write(vtx);
self.index_buffer.slice_mut(0..self.index_count).unwrap().write(idx);
}
pub fn is_empty(&self) -> bool {

View file

@ -6,10 +6,16 @@ use crate::{
state::StateRepo
};
#[cfg(feature = "builtin_elements")] pub mod rect;
#[cfg(feature = "builtin_elements")] pub mod container;
#[cfg(feature = "builtin_elements")] pub mod spacer;
#[cfg(feature = "builtin_elements")] pub mod progress_bar;
#[cfg(feature = "builtin")]
mod builtin {
pub mod rect;
pub mod container;
pub mod spacer;
pub mod progress_bar;
}
#[cfg(feature = "builtin")]
pub use builtin::*;
pub trait UiElement {
fn name(&self) -> &'static str { "UiElement" }

View file

@ -1,6 +1,13 @@
use glam::{Vec2, vec2, Vec4};
use crate::{UiDirection, LayoutInfo, draw::UiDrawCommand, measure::Response, state::StateRepo, UiSize};
use super::UiElement;
use crate::{
UiDirection,
UiSize,
LayoutInfo,
draw::UiDrawCommand,
measure::Response,
state::StateRepo,
element::UiElement
};
pub enum Alignment {
Begin,
@ -75,6 +82,15 @@ impl Default for Container {
}
}
impl Container {
pub fn measure_max_size(&self, layout: &LayoutInfo) -> Vec2 {
layout.max_size - vec2(
self.padding.left + self.padding.right,
self.padding.top + self.padding.bottom,
)
}
}
impl UiElement for Container {
fn measure(&self, state: &StateRepo, layout: &LayoutInfo) -> Response {
let mut size = Vec2::ZERO;
@ -82,7 +98,7 @@ impl UiElement for Container {
for element in &self.elements {
let measure = element.measure(state, &LayoutInfo {
position: layout.position + size,
max_size: layout.max_size, // - size, //TODO
max_size: self.measure_max_size(layout), // - size TODO
direction: self.direction,
});
match self.direction {
@ -141,19 +157,17 @@ impl UiElement for Container {
for element in &self.elements {
//(passing max size from layout rather than actual bounds for the sake of consistency with measure() above)
//measure
let el_measure = element.measure(state, &LayoutInfo {
let layout = LayoutInfo {
position,
max_size: layout.max_size,
max_size: self.measure_max_size(layout),
direction: self.direction,
});
};
//measure
let el_measure = element.measure(state, &layout);
//process
element.process(&el_measure, state, &LayoutInfo {
position,
max_size: layout.max_size,
direction: self.direction,
}, draw);
element.process(&el_measure, state, &layout, draw);
//layout
match self.direction {

View file

@ -3,10 +3,11 @@ use crate::{
UiSize, LayoutInfo,
draw::UiDrawCommand,
measure::Response,
state::StateRepo
state::StateRepo,
element::UiElement
};
use super::UiElement;
#[derive(Debug, Clone, Copy)]
pub struct ProgressBar {
pub size: (UiSize, UiSize),
pub value: f32,

View file

@ -1,6 +1,12 @@
use glam::{vec2, Vec4};
use crate::{state::StateRepo, LayoutInfo, measure::Response, draw::UiDrawCommand, UiSize};
use super::UiElement;
use crate::{
LayoutInfo,
UiSize,
element::UiElement,
state::StateRepo,
measure::Response,
draw::UiDrawCommand
};
pub struct Rect {
pub size: (UiSize, UiSize),

View file

@ -1,6 +1,12 @@
use glam::vec2;
use crate::{state::StateRepo, LayoutInfo, measure::Response, draw::UiDrawCommand, UiDirection};
use super::UiElement;
use crate::{
LayoutInfo,
UiDirection,
element::UiElement,
state::StateRepo,
measure::Response,
draw::UiDrawCommand
};
pub struct Spacer(f32);

View file

@ -72,7 +72,7 @@ impl Default for KubiUi {
}
}
#[derive(Default)]
#[derive(Default, Debug, Clone, Copy)]
pub enum UiSize {
#[default]
Auto,

View file

@ -59,11 +59,11 @@ pub(super) fn init_cube_primitive(
display: NonSendSync<UniqueView<Renderer>>
) {
{
let vert = VertexBuffer::new(
let vert = VertexBuffer::immutable(
&display.display,
CUBE_VERTICES
).unwrap();
let index = IndexBuffer::new(
let index = IndexBuffer::immutable(
&display.display,
PrimitiveType::TrianglesList,
CUBE_INDICES
@ -71,11 +71,11 @@ pub(super) fn init_cube_primitive(
storages.add_unique_non_send_sync(CubePrimitive(vert, index));
}
{
let vert = VertexBuffer::new(
let vert = VertexBuffer::immutable(
&display.display,
CENTERED_CUBE_VERTICES
).unwrap();
let index = IndexBuffer::new(
let index = IndexBuffer::immutable(
&display.display,
PrimitiveType::TrianglesList,
CUBE_INDICES

View file

@ -18,11 +18,11 @@ pub(super) fn init_rect_primitive(
storages: AllStoragesView,
display: NonSendSync<UniqueView<Renderer>>
) {
let vert = VertexBuffer::new(
let vert = VertexBuffer::immutable(
&display.display,
RECT_VERTEX
).unwrap();
let index = IndexBuffer::new(
let index = IndexBuffer::immutable(
&display.display,
PrimitiveType::TrianglesList,
RECT_INDEX