mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-29 02:08:48 -06:00
Compare commits
No commits in common. "0b210eac5dd2afd356b1e510e27383464cdb32ed" and "63f862c2be35f11cd42123ccc5454757ead1ed41" have entirely different histories.
0b210eac5d
...
63f862c2be
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1033,10 +1033,8 @@ dependencies = [
|
|||
"glam",
|
||||
"glium",
|
||||
"hashbrown",
|
||||
"kubi-logging",
|
||||
"log",
|
||||
"nohash-hasher",
|
||||
"winit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -12,11 +12,9 @@ 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"]
|
||||
default = ["backend_glium", "builtin_elements"]
|
||||
backend_glium = ["dep:glium"]
|
||||
builtin = []
|
||||
builtin_elements = []
|
||||
|
|
|
@ -1,82 +1,3 @@
|
|||
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();
|
||||
pub fn main() {
|
||||
//TODO ui demo
|
||||
}
|
||||
|
|
|
@ -48,9 +48,8 @@ impl BufferPair {
|
|||
}
|
||||
|
||||
pub fn ensure_buffer_size(&mut self, need_vtx: usize, need_idx: usize) {
|
||||
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);
|
||||
let current_vtx_size = self.vertex_buffer.get_size();
|
||||
let current_idx_size = self.index_buffer.get_size();
|
||||
if current_vtx_size >= need_vtx && current_idx_size >= need_idx {
|
||||
return
|
||||
}
|
||||
|
@ -85,10 +84,9 @@ impl BufferPair {
|
|||
return
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
|
|
|
@ -6,16 +6,10 @@ use crate::{
|
|||
state::StateRepo
|
||||
};
|
||||
|
||||
#[cfg(feature = "builtin")]
|
||||
mod builtin {
|
||||
pub mod rect;
|
||||
pub mod container;
|
||||
pub mod spacer;
|
||||
pub mod progress_bar;
|
||||
}
|
||||
|
||||
#[cfg(feature = "builtin")]
|
||||
pub use builtin::*;
|
||||
#[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;
|
||||
|
||||
pub trait UiElement {
|
||||
fn name(&self) -> &'static str { "UiElement" }
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
use glam::{Vec2, vec2, Vec4};
|
||||
use crate::{
|
||||
UiDirection,
|
||||
UiSize,
|
||||
LayoutInfo,
|
||||
draw::UiDrawCommand,
|
||||
measure::Response,
|
||||
state::StateRepo,
|
||||
element::UiElement
|
||||
};
|
||||
use crate::{UiDirection, LayoutInfo, draw::UiDrawCommand, measure::Response, state::StateRepo, UiSize};
|
||||
use super::UiElement;
|
||||
|
||||
pub enum Alignment {
|
||||
Begin,
|
||||
|
@ -82,15 +75,6 @@ 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;
|
||||
|
@ -98,7 +82,7 @@ impl UiElement for Container {
|
|||
for element in &self.elements {
|
||||
let measure = element.measure(state, &LayoutInfo {
|
||||
position: layout.position + size,
|
||||
max_size: self.measure_max_size(layout), // - size TODO
|
||||
max_size: layout.max_size, // - size, //TODO
|
||||
direction: self.direction,
|
||||
});
|
||||
match self.direction {
|
||||
|
@ -157,17 +141,19 @@ 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)
|
||||
|
||||
let layout = LayoutInfo {
|
||||
position,
|
||||
max_size: self.measure_max_size(layout),
|
||||
direction: self.direction,
|
||||
};
|
||||
|
||||
//measure
|
||||
let el_measure = element.measure(state, &layout);
|
||||
let el_measure = element.measure(state, &LayoutInfo {
|
||||
position,
|
||||
max_size: layout.max_size,
|
||||
direction: self.direction,
|
||||
});
|
||||
|
||||
//process
|
||||
element.process(&el_measure, state, &layout, draw);
|
||||
element.process(&el_measure, state, &LayoutInfo {
|
||||
position,
|
||||
max_size: layout.max_size,
|
||||
direction: self.direction,
|
||||
}, draw);
|
||||
|
||||
//layout
|
||||
match self.direction {
|
|
@ -3,11 +3,10 @@ use crate::{
|
|||
UiSize, LayoutInfo,
|
||||
draw::UiDrawCommand,
|
||||
measure::Response,
|
||||
state::StateRepo,
|
||||
element::UiElement
|
||||
state::StateRepo
|
||||
};
|
||||
use super::UiElement;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ProgressBar {
|
||||
pub size: (UiSize, UiSize),
|
||||
pub value: f32,
|
|
@ -1,12 +1,6 @@
|
|||
use glam::{vec2, Vec4};
|
||||
use crate::{
|
||||
LayoutInfo,
|
||||
UiSize,
|
||||
element::UiElement,
|
||||
state::StateRepo,
|
||||
measure::Response,
|
||||
draw::UiDrawCommand
|
||||
};
|
||||
use crate::{state::StateRepo, LayoutInfo, measure::Response, draw::UiDrawCommand, UiSize};
|
||||
use super::UiElement;
|
||||
|
||||
pub struct Rect {
|
||||
pub size: (UiSize, UiSize),
|
|
@ -1,12 +1,6 @@
|
|||
use glam::vec2;
|
||||
use crate::{
|
||||
LayoutInfo,
|
||||
UiDirection,
|
||||
element::UiElement,
|
||||
state::StateRepo,
|
||||
measure::Response,
|
||||
draw::UiDrawCommand
|
||||
};
|
||||
use crate::{state::StateRepo, LayoutInfo, measure::Response, draw::UiDrawCommand, UiDirection};
|
||||
use super::UiElement;
|
||||
|
||||
pub struct Spacer(f32);
|
||||
|
|
@ -72,7 +72,7 @@ impl Default for KubiUi {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy)]
|
||||
#[derive(Default)]
|
||||
pub enum UiSize {
|
||||
#[default]
|
||||
Auto,
|
||||
|
|
|
@ -59,25 +59,25 @@ pub(super) fn init_cube_primitive(
|
|||
display: NonSendSync<UniqueView<Renderer>>
|
||||
) {
|
||||
{
|
||||
let vert = VertexBuffer::immutable(
|
||||
let vert = VertexBuffer::new(
|
||||
&display.display,
|
||||
CUBE_VERTICES
|
||||
).unwrap();
|
||||
let index = IndexBuffer::immutable(
|
||||
let index = IndexBuffer::new(
|
||||
&display.display,
|
||||
PrimitiveType::TrianglesList,
|
||||
PrimitiveType::TrianglesList,
|
||||
CUBE_INDICES
|
||||
).unwrap();
|
||||
storages.add_unique_non_send_sync(CubePrimitive(vert, index));
|
||||
}
|
||||
{
|
||||
let vert = VertexBuffer::immutable(
|
||||
let vert = VertexBuffer::new(
|
||||
&display.display,
|
||||
CENTERED_CUBE_VERTICES
|
||||
).unwrap();
|
||||
let index = IndexBuffer::immutable(
|
||||
let index = IndexBuffer::new(
|
||||
&display.display,
|
||||
PrimitiveType::TrianglesList,
|
||||
PrimitiveType::TrianglesList,
|
||||
CUBE_INDICES
|
||||
).unwrap();
|
||||
storages.add_unique_non_send_sync(CenteredCubePrimitive(vert, index));
|
||||
|
|
|
@ -18,13 +18,13 @@ pub(super) fn init_rect_primitive(
|
|||
storages: AllStoragesView,
|
||||
display: NonSendSync<UniqueView<Renderer>>
|
||||
) {
|
||||
let vert = VertexBuffer::immutable(
|
||||
let vert = VertexBuffer::new(
|
||||
&display.display,
|
||||
RECT_VERTEX
|
||||
).unwrap();
|
||||
let index = IndexBuffer::immutable(
|
||||
let index = IndexBuffer::new(
|
||||
&display.display,
|
||||
PrimitiveType::TrianglesList,
|
||||
PrimitiveType::TrianglesList,
|
||||
RECT_INDEX
|
||||
).unwrap();
|
||||
storages.add_unique_non_send_sync(RectPrimitive(vert, index));
|
||||
|
|
Loading…
Reference in a new issue