From a53f6f99012dc650082b7873c4e2f53040e54a5e Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Wed, 22 Nov 2023 19:01:32 +0100 Subject: [PATCH] migrate loading screen to guiv2, fix bugs --- kubi-ui/shaders/vertex.vert | 3 +- kubi-ui/src/backend/glium.rs | 16 +++++- kubi-ui/src/element/container.rs | 2 +- kubi-ui/src/element/progress_bar.rs | 26 +++++---- kubi-ui/src/lib.rs | 8 +-- kubi-ui/src/measure.rs | 8 --- kubi/src/loading_screen.rs | 88 ++++++++--------------------- 7 files changed, 57 insertions(+), 94 deletions(-) diff --git a/kubi-ui/shaders/vertex.vert b/kubi-ui/shaders/vertex.vert index a39f118..60edda6 100644 --- a/kubi-ui/shaders/vertex.vert +++ b/kubi-ui/shaders/vertex.vert @@ -9,5 +9,6 @@ out vec4 vtx_color; void main() { vtx_color = color; - gl_Position = vec4(vec2(1., -1.) * (position / resolution), 0., 1.); + vec2 pos2d = (vec2(2., -2.) * (position / resolution)) + vec2(-1, 1); + gl_Position = vec4(pos2d, 0., 1.); } diff --git a/kubi-ui/src/backend/glium.rs b/kubi-ui/src/backend/glium.rs index 5230094..72041ba 100644 --- a/kubi-ui/src/backend/glium.rs +++ b/kubi-ui/src/backend/glium.rs @@ -33,6 +33,8 @@ pub struct GliumUiRenderer { pub program: glium::Program, pub vertex_buffer: glium::VertexBuffer, pub index_buffer: glium::IndexBuffer, + pub vertex_count: usize, + pub index_count: usize, } impl GliumUiRenderer { @@ -47,6 +49,8 @@ impl GliumUiRenderer { program, vertex_buffer, index_buffer, + vertex_count: 0, + index_count: 0, } } @@ -68,12 +72,14 @@ impl GliumUiRenderer { } fn write_buffer_data(&mut self, vtx: &[Vertex], idx: &[u32]) { - log::info!("uploading {} vertices and {} indices", vtx.len(), idx.len()); + log::debug!("uploading {} vertices and {} indices", vtx.len(), idx.len()); self.ensure_buffer_size(vtx.len(), idx.len()); self.vertex_buffer.invalidate(); self.vertex_buffer.slice_mut(0..vtx.len()).unwrap().write(vtx); + self.vertex_count = vtx.len(); self.index_buffer.invalidate(); self.index_buffer.slice_mut(0..idx.len()).unwrap().write(idx); + self.index_count = idx.len(); } pub fn update(&mut self, plan: &UiDrawPlan) { @@ -83,14 +89,18 @@ impl GliumUiRenderer { } pub fn draw(&self, frame: &mut glium::Frame, resolution: Vec2) { + if self.index_count == 0 || self.vertex_count == 0 { + return + } + let params = DrawParameters { blend: Blend::alpha_blending(), ..Default::default() }; frame.draw( - &self.vertex_buffer, - &self.index_buffer, + self.vertex_buffer.slice(0..self.vertex_count).unwrap(), + self.index_buffer.slice(0..self.index_count).unwrap(), &self.program, &uniform! { resolution: resolution.to_array(), diff --git a/kubi-ui/src/element/container.rs b/kubi-ui/src/element/container.rs index 9da7dcc..5d6f664 100644 --- a/kubi-ui/src/element/container.rs +++ b/kubi-ui/src/element/container.rs @@ -1,5 +1,5 @@ use glam::{Vec2, Vec4}; -use crate::{UiDirection, LayoutInfo, draw::UiDrawCommand, measure::{IsMeasurable, Response}, state::StateRepo, UiSize}; +use crate::{UiDirection, LayoutInfo, draw::UiDrawCommand, measure::Response, state::StateRepo, UiSize}; use super::UiElement; #[derive(Default, Clone, Copy, Debug)] diff --git a/kubi-ui/src/element/progress_bar.rs b/kubi-ui/src/element/progress_bar.rs index 3ee9a99..52f3d1a 100644 --- a/kubi-ui/src/element/progress_bar.rs +++ b/kubi-ui/src/element/progress_bar.rs @@ -48,16 +48,20 @@ impl UiElement for ProgressBar { } fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec) { - draw.push(UiDrawCommand::Rectangle { - position: layout.position, - size: measure.desired_size, - color: self.color_background - }); - - draw.push(UiDrawCommand::Rectangle { - position: layout.position, - size: measure.desired_size * vec2(self.value, 1.0), - color: self.color_foreground - }); + let value = self.value.clamp(0., 1.); + if value < 1. { + draw.push(UiDrawCommand::Rectangle { + position: layout.position, + size: measure.desired_size, + color: self.color_background + }); + } + if value > 0. { + draw.push(UiDrawCommand::Rectangle { + position: layout.position, + size: measure.desired_size * vec2(value, 1.0), + color: self.color_foreground + }); + } } } diff --git a/kubi-ui/src/lib.rs b/kubi-ui/src/lib.rs index c338c58..e063df0 100644 --- a/kubi-ui/src/lib.rs +++ b/kubi-ui/src/lib.rs @@ -87,9 +87,9 @@ pub enum UiDirection { Horizontal, } -struct LayoutInfo { +pub struct LayoutInfo { ///Not availabe during measuring step - position: Vec2, - max_size: Vec2, - direction: UiDirection, + pub position: Vec2, + pub max_size: Vec2, + pub direction: UiDirection, } diff --git a/kubi-ui/src/measure.rs b/kubi-ui/src/measure.rs index e5c02d8..7be6e64 100644 --- a/kubi-ui/src/measure.rs +++ b/kubi-ui/src/measure.rs @@ -1,13 +1,5 @@ use glam::Vec2; -#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)] -pub enum IsMeasurable { - #[default] - No, - Maybe, - Yes -} - pub struct Response { pub desired_size: Vec2 } diff --git a/kubi/src/loading_screen.rs b/kubi/src/loading_screen.rs index 6da4029..4e8d6b8 100644 --- a/kubi/src/loading_screen.rs +++ b/kubi/src/loading_screen.rs @@ -1,60 +1,30 @@ -use shipyard::{UniqueView, UniqueViewMut, Workload, IntoWorkload, EntityId, Unique, AllStoragesViewMut, ViewMut, Get, SystemModificator, track}; +use kubi_ui::element::progress_bar::ProgressBar; +use shipyard::{UniqueView, UniqueViewMut, Workload, NonSendSync, IntoWorkload}; use winit::keyboard::KeyCode; -use glam::{Mat3, vec2}; use crate::{ - world::ChunkStorage, - state::{GameState, NextState, is_changing_state}, - transform::Transform2d, - legacy_gui::{ - LegacyGuiComponent, - progressbar::ProgressbarComponent - }, - rendering::{WindowSize, if_resized}, - input::RawKbmInputState, + world::ChunkStorage, + state::{GameState, NextState}, + rendering::WindowSize, + input::RawKbmInputState, + guiv2_integration::UiState, }; -#[derive(Unique, Clone, Copy)] -struct ProgressbarId(EntityId); - -fn spawn_loading_screen( - mut storages: AllStoragesViewMut, -) { - let size = *storages.borrow::>().unwrap(); - let entity = storages.add_entity(( - LegacyGuiComponent, - Transform2d(Mat3::from_scale_angle_translation( - vec2(size.0.x as f32, 16.), - 0., - vec2(0., 0.) - )), - ProgressbarComponent { - progress: 0.33 - }, - )); - storages.add_unique(ProgressbarId(entity)); -} - -fn resize_progress_bar( - size: UniqueView, - bar: UniqueView, - mut transforms: ViewMut -) { - let mut trans = (&mut transforms).get(bar.0).unwrap(); - trans.0.x_axis.x = size.0.x as f32; -} - -fn update_progress_bar_progress ( +fn render_progressbar( + mut ui: NonSendSync>, world: UniqueView, - mut bar: ViewMut, - eid: UniqueView, + size: UniqueView ) { - let mut bar = (&mut bar).get(eid.0).unwrap(); - let loaded = world.chunks.iter().fold(0, |acc, (&_, chunk)| { - acc + chunk.desired_state.matches_current(chunk.current_state) as usize - }); - let total = world.chunks.len(); - let progress = loaded as f32 / total as f32; - bar.progress = progress; + let value = { + let loaded = world.chunks.iter().fold(0, |acc, (&_, chunk)| { + acc + chunk.desired_state.matches_current(chunk.current_state) as usize + }); + let total = world.chunks.len(); + loaded as f32 / total as f32 + }; + ui.ui.add( + ProgressBar { value, ..Default::default() }, + size.0.as_vec2() + ); } fn switch_to_ingame_if_loaded( @@ -81,24 +51,10 @@ fn override_loading( } } -fn despawn_loading_screen_if_switching_state( - mut storages: AllStoragesViewMut, -) { - let state = storages.borrow::>().unwrap().0.unwrap(); - if state != GameState::LoadingWorld { - let progress_bar = storages.borrow::>().unwrap().0; - storages.delete_entity(progress_bar); - storages.remove_unique::().unwrap(); - } -} - pub fn update_loading_screen() -> Workload { ( - spawn_loading_screen.run_if_missing_unique::(), - resize_progress_bar.run_if(if_resized), - update_progress_bar_progress, + render_progressbar, override_loading, switch_to_ingame_if_loaded, - despawn_loading_screen_if_switching_state.run_if(is_changing_state), ).into_sequential_workload() }